forked from ezsystems/ezpublish-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocationService.php
890 lines (796 loc) · 35.8 KB
/
LocationService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
<?php
/**
* File containing the eZ\Publish\Core\Repository\LocationService class.
*
* @copyright Copyright (C) 1999-2013 eZ Systems AS. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
* @version //autogentag//
* @package eZ\Publish\Core\Repository
*/
namespace eZ\Publish\Core\Repository;
use eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct;
use eZ\Publish\API\Repository\Values\Content\LocationCreateStruct;
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\Core\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\Content\Location as APILocation;
use eZ\Publish\API\Repository\Values\Content\LocationList;
use eZ\Publish\SPI\Persistence\Content\Location as SPILocation;
use eZ\Publish\SPI\Persistence\Content\Location\UpdateStruct;
use eZ\Publish\API\Repository\LocationService as LocationServiceInterface;
use eZ\Publish\API\Repository\Repository as RepositoryInterface;
use eZ\Publish\SPI\Persistence\Handler;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\LogicalAnd as CriterionLogicalAnd;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\LogicalNot as CriterionLogicalNot;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Subtree as CriterionSubtree;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\ParentLocationId as CriterionParentLocationId;
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
use eZ\Publish\API\Repository\Exceptions\NotFoundException as APINotFoundException;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
use eZ\Publish\Core\Base\Exceptions\BadStateException;
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
use Exception;
/**
* Location service, used for complex subtree operations
*
* @example Examples/location.php
*
* @package eZ\Publish\Core\Repository
*/
class LocationService implements LocationServiceInterface
{
/**
* @var \eZ\Publish\Core\Repository\Repository
*/
protected $repository;
/**
* @var \eZ\Publish\SPI\Persistence\Handler
*/
protected $persistenceHandler;
/**
* @var array
*/
protected $settings;
/**
* @var \eZ\Publish\Core\Repository\DomainMapper
*/
protected $domainMapper;
/**
* @var \eZ\Publish\Core\Repository\NameSchemaService
*/
protected $nameSchemaService;
/**
* Setups service with reference to repository object that created it & corresponding handler
*
* @param \eZ\Publish\API\Repository\Repository $repository
* @param \eZ\Publish\SPI\Persistence\Handler $handler
* @param \eZ\Publish\Core\Repository\DomainMapper $domainMapper
* @param \eZ\Publish\Core\Repository\NameSchemaService $nameSchemaService
* @param array $settings
*/
public function __construct(
RepositoryInterface $repository,
Handler $handler,
DomainMapper $domainMapper,
NameSchemaService $nameSchemaService,
array $settings = array()
)
{
$this->repository = $repository;
$this->persistenceHandler = $handler;
$this->domainMapper = $domainMapper;
$this->nameSchemaService = $nameSchemaService;
// Union makes sure default settings are ignored if provided in argument
$this->settings = $settings + array(
//'defaultSetting' => array(),
);
}
/**
* Copies the subtree starting from $subtree as a new subtree of $targetLocation
*
* Only the items on which the user has read access are copied.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed copy the subtree to the given parent location
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user does not have read access to the whole source subtree
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the target location is a sub location of the given location
*
* @param \eZ\Publish\API\Repository\Values\Content\Location $subtree - the subtree denoted by the location to copy
* @param \eZ\Publish\API\Repository\Values\Content\Location $targetParentLocation - the target parent location for the copy operation
*
* @return \eZ\Publish\API\Repository\Values\Content\Location The newly created location of the copied subtree
*/
public function copySubtree( APILocation $subtree, APILocation $targetParentLocation )
{
$loadedSubtree = $this->loadLocation( $subtree->id );
$loadedTargetLocation = $this->loadLocation( $targetParentLocation->id );
if ( stripos( $loadedTargetLocation->pathString, $loadedSubtree->pathString ) !== false )
throw new InvalidArgumentException( "targetParentLocation", "target parent location is a sub location of the given subtree" );
// check create permission on target
if ( !$this->repository->canUser( 'content', 'create', $loadedSubtree->getContentInfo(), $loadedTargetLocation ) )
throw new UnauthorizedException( 'content', 'create' );
/** Check read access to whole source subtree
* @var boolean|\eZ\Publish\API\Repository\Values\Content\Query\Criterion $contentReadCriterion
*/
$contentReadCriterion = $this->repository->getSearchService()->getPermissionsCriterion();
if ( $contentReadCriterion === false )
{
throw new UnauthorizedException( 'content', 'read' );
}
else if ( $contentReadCriterion !== true )
{
// Query if there are any content in subtree current user don't have access to
$query = new Query(
array(
'limit' => 0,
'filter' => new CriterionLogicalAnd(
new CriterionSubtree( $loadedSubtree->pathString ),
new CriterionLogicalNot( $contentReadCriterion )
)
)
);
$result = $this->repository->getSearchService()->findContent( $query, array(), false );
if ( $result->totalCount > 0 )
{
throw new UnauthorizedException( 'content', 'read' );
}
}
$this->repository->beginTransaction();
try
{
$newLocation = $this->persistenceHandler->locationHandler()->copySubtree(
$loadedSubtree->id,
$loadedTargetLocation->id
);
$content = $this->repository->getContentService()->loadContent( $newLocation->contentId );
$urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema( $content );
foreach ( $urlAliasNames as $languageCode => $name )
{
$this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation(
$newLocation->id,
$loadedTargetLocation->id,
$name,
$languageCode,
$content->contentInfo->alwaysAvailable
);
}
$this->persistenceHandler->urlAliasHandler()->locationCopied(
$newLocation->id,
$loadedSubtree->parentLocationId,
$loadedTargetLocation->id
);
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
return $this->buildDomainLocationObject( $newLocation );
}
/**
* Loads a location object from its $locationId
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to read this location
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the specified location is not found
*
* @param mixed $locationId
*
* @return \eZ\Publish\API\Repository\Values\Content\Location
*/
public function loadLocation( $locationId )
{
$spiLocation = $this->persistenceHandler->locationHandler()->load( $locationId );
$location = $this->buildDomainLocationObject( $spiLocation );
if ( !$this->repository->canUser( 'content', 'read', $location->getContentInfo(), $location ) )
throw new UnauthorizedException( 'content', 'read' );
return $location;
}
/**
* Loads a location object from its $remoteId
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to read this location
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException If more than one location with same remote ID was found
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the specified location is not found
*
* @param string $remoteId
*
* @return \eZ\Publish\API\Repository\Values\Content\Location
*/
public function loadLocationByRemoteId( $remoteId )
{
if ( !is_string( $remoteId ) )
throw new InvalidArgumentValue( "remoteId", $remoteId );
$spiLocation = $this->persistenceHandler->locationHandler()->loadByRemoteId( $remoteId );
$location = $this->buildDomainLocationObject( $spiLocation );
if ( !$this->repository->canUser( 'content', 'read', $location->getContentInfo(), $location ) )
throw new UnauthorizedException( 'content', 'read' );
return $location;
}
/**
* Loads the locations for the given content object.
*
* If a $rootLocation is given, only locations that belong to this location are returned.
* The location list is also filtered by permissions on reading locations.
*
* @todo permissions check is missing
*
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if there is no published version yet
*
* @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
* @param \eZ\Publish\API\Repository\Values\Content\Location $rootLocation
*
* @return \eZ\Publish\API\Repository\Values\Content\Location[] An array of {@link Location}
*/
public function loadLocations( ContentInfo $contentInfo, APILocation $rootLocation = null )
{
if ( !$contentInfo->published )
{
throw new BadStateException( "\$contentInfo", "ContentInfo has no published versions" );
}
$spiLocations = $this->persistenceHandler->locationHandler()->loadLocationsByContent(
$contentInfo->id,
$rootLocation !== null ? $rootLocation->id : null
);
$locations = array();
foreach ( $spiLocations as $spiLocation )
{
$locations[] = $this->buildDomainLocationObject( $spiLocation );
}
return $locations;
}
/**
* Loads children which are readable by the current user of a location object sorted by sortField and sortOrder
*
* @param \eZ\Publish\API\Repository\Values\Content\Location $location
*
* @param int $offset the start offset for paging
* @param int $limit the number of locations returned. If $limit = -1 all children starting at $offset are returned
*
* @return \eZ\Publish\API\Repository\Values\Content\LocationList
*/
public function loadLocationChildren( APILocation $location, $offset = 0, $limit = -1 )
{
if ( !$this->domainMapper->isValidLocationSortField( $location->sortField ) )
throw new InvalidArgumentValue( "sortField", $location->sortField, "Location" );
if ( !$this->domainMapper->isValidLocationSortOrder( $location->sortOrder ) )
throw new InvalidArgumentValue( "sortOrder", $location->sortOrder, "Location" );
if ( !is_int( $offset ) )
throw new InvalidArgumentValue( "offset", $offset );
if ( !is_int( $limit ) )
throw new InvalidArgumentValue( "limit", $limit );
$searchResult = $this->searchChildrenLocations(
$location->id,
$location->sortField,
$location->sortOrder,
$offset,
$limit
);
$childLocations = array();
foreach ( $searchResult->searchHits as $spiSearchHit )
{
$spiContentLocations = $this->persistenceHandler->locationHandler()->loadLocationsByContent(
$spiSearchHit->valueObject->versionInfo->contentInfo->id,
$location->id
);
foreach ( $spiContentLocations as $spiLocation )
{
if ( $spiLocation->parentId == $location->id )
{
$childLocation = $this->buildDomainLocationObject( $spiLocation );
if ( $this->repository->canUser( 'content', 'read', $childLocation->getContentInfo(), $childLocation ) )
{
$childLocations[] = $childLocation;
}
}
}
}
return new LocationList(
array(
"locations" => $childLocations,
"totalCount" => (int)$searchResult->totalCount
)
);
}
/**
* Returns the number of children which are readable by the current user of a location object
*
* @param \eZ\Publish\API\Repository\Values\Content\Location $location
*
* @return int
*/
public function getLocationChildCount( APILocation $location )
{
return $this->searchChildrenLocations(
$location->id,
null,
APILocation::SORT_ORDER_ASC,
0,
0
)->totalCount;
}
/**
* Searches children locations of the provided parent location id
*
* @param mixed $parentLocationId
* @param int $sortField
* @param int $sortOrder
* @param int $offset
* @param int $limit
*
* @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult
*/
protected function searchChildrenLocations( $parentLocationId, $sortField = null, $sortOrder = APILocation::SORT_ORDER_ASC, $offset = 0, $limit = -1 )
{
$query = new Query(
array(
'filter' => new CriterionParentLocationId( $parentLocationId ),
'offset' => ( $offset >= 0 ? (int)$offset : 0 ),
'limit' => ( $limit >= 0 ? (int)$limit : null )
)
);
if ( $sortField !== null )
$query->sortClauses = array( $this->getSortClauseBySortField( $sortField, $sortOrder ) );
if ( !$this->repository->getSearchService()->addPermissionsCriterion( $query->filter ) )
{
return array();
}
return $this->persistenceHandler->searchHandler()->findContent( $query );
}
/**
* Creates the new $location in the content repository for the given content
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to create this location
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the content is already below the specified parent
* or the parent is a sub location of the location of the content
* or if set the remoteId exists already
*
* @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
*
* @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $locationCreateStruct
*
* @return \eZ\Publish\API\Repository\Values\Content\Location the newly created Location
*/
public function createLocation( ContentInfo $contentInfo, LocationCreateStruct $locationCreateStruct )
{
$content = $this->repository->getContentService()->loadContent( $contentInfo->id );
$parentLocation = $this->loadLocation( $locationCreateStruct->parentLocationId );
if ( !$this->repository->canUser( 'content', 'create', $content->contentInfo, $parentLocation ) )
{
throw new UnauthorizedException( 'content', 'create' );
}
// Check if the parent is a sub location of one of the existing content locations (this also solves the
// situation where parent location actually one of the content locations),
// or if the content already has location below given location create struct parent
$existingContentLocations = $this->loadLocations( $content->contentInfo );
if ( !empty( $existingContentLocations ) )
{
foreach ( $existingContentLocations as $existingContentLocation )
{
if ( stripos( $parentLocation->pathString, $existingContentLocation->pathString ) !== false )
{
throw new InvalidArgumentException(
"\$locationCreateStruct",
"Specified parent is a sub location of one of the existing content locations."
);
}
if ( $parentLocation->id == $existingContentLocation->parentLocationId )
{
throw new InvalidArgumentException(
"\$locationCreateStruct",
"Content is already below the specified parent."
);
}
}
}
$spiLocationCreateStruct = $this->domainMapper->buildSPILocationCreateStruct(
$locationCreateStruct,
$parentLocation,
$content->contentInfo->mainLocationId !== null ? $content->contentInfo->mainLocationId : true,
$content->contentInfo->id,
$content->contentInfo->currentVersionNo
);
$this->repository->beginTransaction();
try
{
$newLocation = $this->persistenceHandler->locationHandler()->create( $spiLocationCreateStruct );
$urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema( $content );
foreach ( $urlAliasNames as $languageCode => $name )
{
$this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation(
$newLocation->id,
$newLocation->parentId,
$name,
$languageCode,
$content->contentInfo->alwaysAvailable,
// @todo: this is legacy storage specific for updating ezcontentobject_tree.path_identification_string, to be removed
$languageCode === $content->contentInfo->mainLanguageCode
);
}
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
return $this->buildDomainLocationObject( $newLocation );
}
/**
* Updates $location in the content repository
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to update this location
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if if set the remoteId exists already
*
* @param \eZ\Publish\API\Repository\Values\Content\Location $location
* @param \eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct $locationUpdateStruct
*
* @return \eZ\Publish\API\Repository\Values\Content\Location the updated Location
*/
public function updateLocation( APILocation $location, LocationUpdateStruct $locationUpdateStruct )
{
if ( $locationUpdateStruct->priority !== null && !is_int( $locationUpdateStruct->priority ) )
throw new InvalidArgumentValue( "priority", $locationUpdateStruct->priority, "LocationUpdateStruct" );
if ( $locationUpdateStruct->remoteId !== null && ( !is_string( $locationUpdateStruct->remoteId ) || empty( $locationUpdateStruct->remoteId ) ) )
throw new InvalidArgumentValue( "remoteId", $locationUpdateStruct->remoteId, "LocationUpdateStruct" );
if ( $locationUpdateStruct->sortField !== null && !$this->domainMapper->isValidLocationSortField( $locationUpdateStruct->sortField ) )
throw new InvalidArgumentValue( "sortField", $locationUpdateStruct->sortField, "LocationUpdateStruct" );
if ( $locationUpdateStruct->sortOrder !== null && !$this->domainMapper->isValidLocationSortOrder( $locationUpdateStruct->sortOrder ) )
throw new InvalidArgumentValue( "sortOrder", $locationUpdateStruct->sortOrder, "LocationUpdateStruct" );
$loadedLocation = $this->loadLocation( $location->id );
if ( $locationUpdateStruct->remoteId !== null )
{
try
{
$existingLocation = $this->loadLocationByRemoteId( $locationUpdateStruct->remoteId );
if ( $existingLocation !== null )
throw new InvalidArgumentException( "locationUpdateStruct", "location with provided remote ID already exists" );
}
catch ( APINotFoundException $e )
{
}
}
if ( !$this->repository->canUser( 'content', 'edit', $loadedLocation->getContentInfo(), $loadedLocation ) )
throw new UnauthorizedException( 'content', 'edit' );
$updateStruct = new UpdateStruct();
$updateStruct->priority = $locationUpdateStruct->priority !== null ? $locationUpdateStruct->priority : $loadedLocation->priority;
$updateStruct->remoteId = $locationUpdateStruct->remoteId !== null ? trim( $locationUpdateStruct->remoteId ) : $loadedLocation->remoteId;
$updateStruct->sortField = $locationUpdateStruct->sortField !== null ? $locationUpdateStruct->sortField : $loadedLocation->sortField;
$updateStruct->sortOrder = $locationUpdateStruct->sortOrder !== null ? $locationUpdateStruct->sortOrder : $loadedLocation->sortOrder;
$this->repository->beginTransaction();
try
{
$this->persistenceHandler->locationHandler()->update( $updateStruct, $loadedLocation->id );
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
return $this->loadLocation( $loadedLocation->id );
}
/**
* Swaps the contents held by $location1 and $location2
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to swap content
*
* @param \eZ\Publish\API\Repository\Values\Content\Location $location1
* @param \eZ\Publish\API\Repository\Values\Content\Location $location2
*/
public function swapLocation( APILocation $location1, APILocation $location2 )
{
$loadedLocation1 = $this->loadLocation( $location1->id );
$loadedLocation2 = $this->loadLocation( $location2->id );
if ( !$this->repository->canUser( 'content', 'edit', $loadedLocation1->getContentInfo(), $loadedLocation1 ) )
throw new UnauthorizedException( 'content', 'edit' );
if ( !$this->repository->canUser( 'content', 'edit', $loadedLocation2->getContentInfo(), $loadedLocation2 ) )
throw new UnauthorizedException( 'content', 'edit' );
$this->repository->beginTransaction();
try
{
$this->persistenceHandler->locationHandler()->swap( $loadedLocation1->id, $loadedLocation2->id );
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
}
/**
* Hides the $location and marks invisible all descendants of $location.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to hide this location
*
* @param \eZ\Publish\API\Repository\Values\Content\Location $location
*
* @return \eZ\Publish\API\Repository\Values\Content\Location $location, with updated hidden value
*/
public function hideLocation( APILocation $location )
{
if ( !$this->repository->canUser( 'content', 'hide', $location->getContentInfo(), $location ) )
throw new UnauthorizedException( 'content', 'hide' );
$this->repository->beginTransaction();
try
{
$this->persistenceHandler->locationHandler()->hide( $location->id );
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
return $this->loadLocation( $location->id );
}
/**
* Unhides the $location.
*
* This method and marks visible all descendants of $locations
* until a hidden location is found.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to unhide this location
*
* @param \eZ\Publish\API\Repository\Values\Content\Location $location
*
* @return \eZ\Publish\API\Repository\Values\Content\Location $location, with updated hidden value
*/
public function unhideLocation( APILocation $location )
{
if ( !$this->repository->canUser( 'content', 'hide', $location->getContentInfo(), $location ) )
throw new UnauthorizedException( 'content', 'hide' );
$this->repository->beginTransaction();
try
{
$this->persistenceHandler->locationHandler()->unHide( $location->id );
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
return $this->loadLocation( $location->id );
}
/**
* Moves the subtree to $newParentLocation
*
* If a user has the permission to move the location to a target location
* he can do it regardless of an existing descendant on which the user has no permission.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to move this location to the target
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user does not have read access to the whole source subtree
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the new parent is in a subtree of the location
*
* @param \eZ\Publish\API\Repository\Values\Content\Location $location
* @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation
*/
public function moveSubtree( APILocation $location, APILocation $newParentLocation )
{
$location = $this->loadLocation( $location->id );
$newParentLocation = $this->loadLocation( $newParentLocation->id );
// check create permission on target location
if ( !$this->repository->canUser( 'content', 'create', $location->getContentInfo(), $newParentLocation ) )
throw new UnauthorizedException( 'content', 'create' );
/** Check read access to whole source subtree
* @var boolean|\eZ\Publish\API\Repository\Values\Content\Query\Criterion $contentReadCriterion
*/
$contentReadCriterion = $this->repository->getSearchService()->getPermissionsCriterion();
if ( $contentReadCriterion === false )
{
throw new UnauthorizedException( 'content', 'read' );
}
else if ( $contentReadCriterion !== true )
{
// Query if there are any content in subtree current user don't have access to
$query = new Query(
array(
'limit' => 0,
'filter' => new CriterionLogicalAnd(
new CriterionSubtree( $location->pathString ),
new CriterionLogicalNot( $contentReadCriterion )
)
)
);
$result = $this->repository->getSearchService()->findContent( $query, array(), false );
if ( $result->totalCount > 0 )
{
throw new UnauthorizedException( 'content', 'read' );
}
}
if ( strpos( $newParentLocation->pathString, $location->pathString ) === 0 )
{
throw new InvalidArgumentException(
"\$newParentLocation",
"new parent location is in a subtree of the given \$location"
);
}
$this->repository->beginTransaction();
try
{
$this->persistenceHandler->locationHandler()->move( $location->id, $newParentLocation->id );
$content = $this->repository->getContentService()->loadContent( $location->contentId );
$urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema( $content );
foreach ( $urlAliasNames as $languageCode => $name )
{
$this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation(
$location->id,
$newParentLocation->id,
$name,
$languageCode,
$content->contentInfo->alwaysAvailable
);
}
$this->persistenceHandler->urlAliasHandler()->locationMoved(
$location->id,
$location->parentLocationId,
$newParentLocation->id
);
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
}
/**
* Deletes $location and all its descendants.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user is not allowed to delete this location or a descendant
*
* @param \eZ\Publish\API\Repository\Values\Content\Location $location
*/
public function deleteLocation( APILocation $location )
{
$location = $this->loadLocation( $location->id );
if ( !$this->repository->canUser( 'content', 'manage_locations', $location->getContentInfo() ) )
throw new UnauthorizedException( 'content', 'manage_locations' );
if ( !$this->repository->canUser( 'content', 'remove', $location->getContentInfo(), $location ) )
throw new UnauthorizedException( 'content', 'remove' );
/** Check remove access to descendants
* @var boolean|\eZ\Publish\API\Repository\Values\Content\Query\Criterion $contentReadCriterion
*/
$contentReadCriterion = $this->repository->getSearchService()->getPermissionsCriterion( 'content', 'remove' );
if ( $contentReadCriterion === false )
{
throw new UnauthorizedException( 'content', 'remove' );
}
else if ( $contentReadCriterion !== true )
{
// Query if there are any content in subtree current user don't have access to
$query = new Query(
array(
'limit' => 0,
'filter' => new CriterionLogicalAnd(
array(
new CriterionSubtree( $location->pathString ),
new CriterionLogicalNot( $contentReadCriterion )
)
)
)
);
$result = $this->repository->getSearchService()->findContent( $query, array(), false );
if ( $result->totalCount > 0 )
{
throw new UnauthorizedException( 'content', 'remove' );
}
}
$this->repository->beginTransaction();
try
{
$this->persistenceHandler->locationHandler()->removeSubtree( $location->id );
$this->persistenceHandler->urlAliasHandler()->locationDeleted( $location->id );
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
}
/**
* Instantiates a new location create class
*
* @param mixed $parentLocationId the parent under which the new location should be created
*
* @return \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct
*/
public function newLocationCreateStruct( $parentLocationId )
{
return new LocationCreateStruct(
array(
'parentLocationId' => $parentLocationId
)
);
}
/**
* Instantiates a new location update class
*
* @return \eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct
*/
public function newLocationUpdateStruct()
{
return new LocationUpdateStruct();
}
/**
* Builds domain location object from provided persistence location
*
* @param \eZ\Publish\SPI\Persistence\Content\Location $spiLocation
*
* @return \eZ\Publish\API\Repository\Values\Content\Location
*/
protected function buildDomainLocationObject( SPILocation $spiLocation )
{
// TODO: this is hardcoded workaround for missing ContentInfo on root location
if ( $spiLocation->id == 1 )
$contentInfo = new ContentInfo(
array(
'id' => 0,
'name' => 'Top Level Nodes',
'sectionId' => 1,
'mainLocationId' => 1,
'contentTypeId' => 1,
)
);
else
$contentInfo = $this->repository->getContentService()->internalLoadContentInfo( $spiLocation->contentId );
return new Location(
array(
'contentInfo' => $contentInfo,
'id' => $spiLocation->id,
'priority' => $spiLocation->priority,
'hidden' => $spiLocation->hidden,
'invisible' => $spiLocation->invisible,
'remoteId' => $spiLocation->remoteId,
'parentLocationId' => $spiLocation->parentId,
'pathString' => $spiLocation->pathString,
'depth' => $spiLocation->depth,
'sortField' => $spiLocation->sortField,
'sortOrder' => $spiLocation->sortOrder,
)
);
}
/**
* Instantiates a correct sort clause object based on provided location sort field and sort order
*
* @param int $sortField
* @param int $sortOrder
*
* @return \eZ\Publish\API\Repository\Values\Content\Query\SortClause
*/
protected function getSortClauseBySortField( $sortField, $sortOrder = APILocation::SORT_ORDER_ASC )
{
$sortOrder = $sortOrder == APILocation::SORT_ORDER_DESC ? Query::SORT_DESC : Query::SORT_ASC;
switch ( $sortField )
{
case APILocation::SORT_FIELD_PATH:
return new SortClause\LocationPathString( $sortOrder );
case APILocation::SORT_FIELD_PUBLISHED:
return new SortClause\DatePublished( $sortOrder );
case APILocation::SORT_FIELD_MODIFIED:
return new SortClause\DateModified( $sortOrder );
case APILocation::SORT_FIELD_SECTION:
return new SortClause\SectionIdentifier( $sortOrder );
case APILocation::SORT_FIELD_DEPTH:
return new SortClause\LocationDepth( $sortOrder );
//@todo: enable
// case APILocation::SORT_FIELD_CLASS_IDENTIFIER:
//@todo: enable
// case APILocation::SORT_FIELD_CLASS_NAME:
case APILocation::SORT_FIELD_PRIORITY:
return new SortClause\LocationPriority( $sortOrder );
case APILocation::SORT_FIELD_NAME:
return new SortClause\ContentName( $sortOrder );
//@todo: enable
// case APILocation::SORT_FIELD_MODIFIED_SUBNODE:
//@todo: enable
// case APILocation::SORT_FIELD_NODE_ID:
//@todo: enable
// case APILocation::SORT_FIELD_CONTENTOBJECT_ID:
default:
return new SortClause\LocationPathString( $sortOrder );
}
}
}