Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ContentService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ContentService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
53 | class ContentService implements ContentServiceInterface |
||
54 | { |
||
55 | /** |
||
56 | * @var \eZ\Publish\Core\Repository\Repository |
||
57 | */ |
||
58 | protected $repository; |
||
59 | |||
60 | /** |
||
61 | * @var \eZ\Publish\SPI\Persistence\Handler |
||
62 | */ |
||
63 | protected $persistenceHandler; |
||
64 | |||
65 | /** |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $settings; |
||
69 | |||
70 | /** |
||
71 | * @var \eZ\Publish\Core\Repository\Helper\DomainMapper |
||
72 | */ |
||
73 | protected $domainMapper; |
||
74 | |||
75 | /** |
||
76 | * @var \eZ\Publish\Core\Repository\Helper\RelationProcessor |
||
77 | */ |
||
78 | protected $relationProcessor; |
||
79 | |||
80 | /** |
||
81 | * @var \eZ\Publish\Core\Repository\Helper\NameSchemaService |
||
82 | */ |
||
83 | protected $nameSchemaService; |
||
84 | |||
85 | /** |
||
86 | * @var \eZ\Publish\Core\Repository\Helper\FieldTypeRegistry |
||
87 | */ |
||
88 | protected $fieldTypeRegistry; |
||
89 | |||
90 | /** |
||
91 | * Setups service with reference to repository object that created it & corresponding handler. |
||
92 | * |
||
93 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
94 | * @param \eZ\Publish\SPI\Persistence\Handler $handler |
||
95 | * @param \eZ\Publish\Core\Repository\Helper\DomainMapper $domainMapper |
||
96 | * @param \eZ\Publish\Core\Repository\Helper\RelationProcessor $relationProcessor |
||
97 | * @param \eZ\Publish\Core\Repository\Helper\NameSchemaService $nameSchemaService |
||
98 | * @param \eZ\Publish\Core\Repository\Helper\FieldTypeRegistry $fieldTypeRegistry, |
||
|
|||
99 | * @param array $settings |
||
100 | */ |
||
101 | public function __construct( |
||
102 | RepositoryInterface $repository, |
||
103 | Handler $handler, |
||
104 | Helper\DomainMapper $domainMapper, |
||
105 | Helper\RelationProcessor $relationProcessor, |
||
106 | Helper\NameSchemaService $nameSchemaService, |
||
107 | Helper\FieldTypeRegistry $fieldTypeRegistry, |
||
108 | array $settings = array() |
||
109 | ) { |
||
110 | $this->repository = $repository; |
||
111 | $this->persistenceHandler = $handler; |
||
112 | $this->domainMapper = $domainMapper; |
||
113 | $this->relationProcessor = $relationProcessor; |
||
114 | $this->nameSchemaService = $nameSchemaService; |
||
115 | $this->fieldTypeRegistry = $fieldTypeRegistry; |
||
116 | // Union makes sure default settings are ignored if provided in argument |
||
117 | $this->settings = $settings + array( |
||
118 | // Version archive limit (0-50), only enforced on publish, not on un-publish. |
||
119 | 'default_version_archive_limit' => 5, |
||
120 | ); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Loads a content info object. |
||
125 | * |
||
126 | * To load fields use loadContent |
||
127 | * |
||
128 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the content |
||
129 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content with the given id does not exist |
||
130 | * |
||
131 | * @param int $contentId |
||
132 | * |
||
133 | * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo |
||
134 | */ |
||
135 | View Code Duplication | public function loadContentInfo($contentId) |
|
144 | |||
145 | /** |
||
146 | * Loads a content info object. |
||
147 | * |
||
148 | * To load fields use loadContent |
||
149 | * |
||
150 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content with the given id does not exist |
||
151 | * |
||
152 | * @param mixed $id |
||
153 | * @param bool $isRemoteId |
||
154 | * |
||
155 | * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo |
||
156 | */ |
||
157 | public function internalLoadContentInfo($id, $isRemoteId = false) |
||
173 | |||
174 | /** |
||
175 | * Loads a content info object for the given remoteId. |
||
176 | * |
||
177 | * To load fields use loadContent |
||
178 | * |
||
179 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the content |
||
180 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content with the given remote id does not exist |
||
181 | * |
||
182 | * @param string $remoteId |
||
183 | * |
||
184 | * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo |
||
185 | */ |
||
186 | View Code Duplication | public function loadContentInfoByRemoteId($remoteId) |
|
196 | |||
197 | /** |
||
198 | * Loads a version info of the given content object. |
||
199 | * |
||
200 | * If no version number is given, the method returns the current version |
||
201 | * |
||
202 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the version with the given number does not exist |
||
203 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to load this version |
||
204 | * |
||
205 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
206 | * @param int $versionNo the version number. If not given the current version is returned. |
||
207 | * |
||
208 | * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo |
||
209 | */ |
||
210 | public function loadVersionInfo(ContentInfo $contentInfo, $versionNo = null) |
||
214 | |||
215 | /** |
||
216 | * Loads a version info of the given content object id. |
||
217 | * |
||
218 | * If no version number is given, the method returns the current version |
||
219 | * |
||
220 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the version with the given number does not exist |
||
221 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to load this version |
||
222 | * |
||
223 | * @param mixed $contentId |
||
224 | * @param int $versionNo the version number. If not given the current version is returned. |
||
225 | * |
||
226 | * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo |
||
227 | */ |
||
228 | public function loadVersionInfoById($contentId, $versionNo = null) |
||
264 | |||
265 | /** |
||
266 | * {@inheritdoc} |
||
267 | */ |
||
268 | public function loadContentByContentInfo(ContentInfo $contentInfo, array $languages = null, $versionNo = null, $useAlwaysAvailable = true) |
||
269 | { |
||
270 | // Change $useAlwaysAvailable to false to avoid contentInfo lookup if we know alwaysAvailable is disabled |
||
271 | if ($useAlwaysAvailable && !$contentInfo->alwaysAvailable) { |
||
272 | $useAlwaysAvailable = false; |
||
273 | } |
||
274 | |||
275 | // As we have content info we can avoid that current version is looked up using spi in loadContent() if not set |
||
276 | if ($versionNo === null) { |
||
277 | $versionNo = $contentInfo->currentVersionNo; |
||
278 | } |
||
279 | |||
280 | return $this->loadContent( |
||
281 | $contentInfo->id, |
||
282 | $languages, |
||
283 | $versionNo, |
||
284 | $useAlwaysAvailable |
||
285 | ); |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * {@inheritdoc} |
||
290 | */ |
||
291 | public function loadContentByVersionInfo(APIVersionInfo $versionInfo, array $languages = null, $useAlwaysAvailable = true) |
||
292 | { |
||
293 | // Change $useAlwaysAvailable to false to avoid contentInfo lookup if we know alwaysAvailable is disabled |
||
294 | if ($useAlwaysAvailable && !$versionInfo->getContentInfo()->alwaysAvailable) { |
||
295 | $useAlwaysAvailable = false; |
||
296 | } |
||
297 | |||
298 | return $this->loadContent( |
||
299 | $versionInfo->getContentInfo()->id, |
||
300 | $languages, |
||
301 | $versionInfo->versionNo, |
||
302 | $useAlwaysAvailable |
||
303 | ); |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * {@inheritdoc} |
||
308 | */ |
||
309 | View Code Duplication | public function loadContent($contentId, array $languages = null, $versionNo = null, $useAlwaysAvailable = true) |
|
310 | { |
||
311 | $content = $this->internalLoadContent($contentId, $languages, $versionNo, false, $useAlwaysAvailable); |
||
312 | |||
313 | if (!$this->repository->canUser('content', 'read', $content)) { |
||
314 | throw new UnauthorizedException('content', 'read', array('contentId' => $contentId)); |
||
315 | } |
||
316 | if ( |
||
317 | !$content->getVersionInfo()->isPublished() |
||
318 | && !$this->repository->canUser('content', 'versionread', $content) |
||
319 | ) { |
||
320 | throw new UnauthorizedException('content', 'versionread', array('contentId' => $contentId, 'versionNo' => $versionNo)); |
||
321 | } |
||
322 | |||
323 | return $content; |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Loads content in a version of the given content object. |
||
328 | * |
||
329 | * If no version number is given, the method returns the current version |
||
330 | * |
||
331 | * @internal |
||
332 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the content or version with the given id and languages does not exist |
||
333 | * |
||
334 | * @param mixed $id |
||
335 | * @param array|null $languages A language priority, filters returned fields and is used as prioritized language code on |
||
336 | * returned value object. If not given all languages are returned. |
||
337 | * @param int|null $versionNo the version number. If not given the current version is returned |
||
338 | * @param bool $isRemoteId |
||
339 | * @param bool $useAlwaysAvailable Add Main language to \$languages if true (default) and if alwaysAvailable is true |
||
340 | * |
||
341 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
342 | */ |
||
343 | public function internalLoadContent($id, array $languages = null, $versionNo = null, $isRemoteId = false, $useAlwaysAvailable = true) |
||
344 | { |
||
345 | try { |
||
346 | // Get Content ID if lookup by remote ID |
||
347 | if ($isRemoteId) { |
||
348 | $spiContentInfo = $this->persistenceHandler->contentHandler()->loadContentInfoByRemoteId($id); |
||
349 | $id = $spiContentInfo->id; |
||
350 | } |
||
351 | |||
352 | // Get current version if $versionNo is not defined |
||
353 | if ($versionNo === null) { |
||
354 | if (!isset($spiContentInfo)) { |
||
355 | $spiContentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo($id); |
||
356 | } |
||
357 | |||
358 | $versionNo = $spiContentInfo->currentVersionNo; |
||
359 | } |
||
360 | |||
361 | $loadLanguages = $languages; |
||
362 | $alwaysAvailableLanguageCode = null; |
||
363 | // Set main language on $languages filter if not empty (all) and $useAlwaysAvailable being true |
||
364 | if (!empty($loadLanguages) && $useAlwaysAvailable) { |
||
365 | if (!isset($spiContentInfo)) { |
||
366 | $spiContentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo($id); |
||
367 | } |
||
368 | |||
369 | if ($spiContentInfo->alwaysAvailable) { |
||
370 | $loadLanguages[] = $alwaysAvailableLanguageCode = $spiContentInfo->mainLanguageCode; |
||
371 | $loadLanguages = array_unique($loadLanguages); |
||
372 | } |
||
373 | } |
||
374 | |||
375 | $spiContent = $this->persistenceHandler->contentHandler()->load( |
||
376 | $id, |
||
377 | $versionNo, |
||
378 | $loadLanguages |
||
379 | ); |
||
380 | } catch (APINotFoundException $e) { |
||
381 | throw new NotFoundException( |
||
382 | 'Content', |
||
383 | array( |
||
384 | $isRemoteId ? 'remoteId' : 'id' => $id, |
||
385 | 'languages' => $languages, |
||
386 | 'versionNo' => $versionNo, |
||
387 | ), |
||
388 | $e |
||
389 | ); |
||
390 | } |
||
391 | |||
392 | return $this->domainMapper->buildContentDomainObject( |
||
393 | $spiContent, |
||
394 | null, |
||
395 | empty($languages) ? null : $languages, |
||
396 | $alwaysAvailableLanguageCode |
||
397 | ); |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * Loads content in a version for the content object reference by the given remote id. |
||
402 | * |
||
403 | * If no version is given, the method returns the current version |
||
404 | * |
||
405 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content or version with the given remote id does not exist |
||
406 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the user has no access to read content and in case of un-published content: read versions |
||
407 | * |
||
408 | * @param string $remoteId |
||
409 | * @param array $languages A language filter for fields. If not given all languages are returned |
||
410 | * @param int $versionNo the version number. If not given the current version is returned |
||
411 | * @param bool $useAlwaysAvailable Add Main language to \$languages if true (default) and if alwaysAvailable is true |
||
412 | * |
||
413 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
414 | */ |
||
415 | View Code Duplication | public function loadContentByRemoteId($remoteId, array $languages = null, $versionNo = null, $useAlwaysAvailable = true) |
|
416 | { |
||
417 | $content = $this->internalLoadContent($remoteId, $languages, $versionNo, true, $useAlwaysAvailable); |
||
418 | |||
419 | if (!$this->repository->canUser('content', 'read', $content)) { |
||
420 | throw new UnauthorizedException('content', 'read', array('remoteId' => $remoteId)); |
||
421 | } |
||
422 | |||
423 | if ( |
||
424 | !$content->getVersionInfo()->isPublished() |
||
425 | && !$this->repository->canUser('content', 'versionread', $content) |
||
426 | ) { |
||
427 | throw new UnauthorizedException('content', 'versionread', array('remoteId' => $remoteId, 'versionNo' => $versionNo)); |
||
428 | } |
||
429 | |||
430 | return $content; |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * Creates a new content draft assigned to the authenticated user. |
||
435 | * |
||
436 | * If a different userId is given in $contentCreateStruct it is assigned to the given user |
||
437 | * but this required special rights for the authenticated user |
||
438 | * (this is useful for content staging where the transfer process does not |
||
439 | * have to authenticate with the user which created the content object in the source server). |
||
440 | * The user has to publish the draft if it should be visible. |
||
441 | * In 4.x at least one location has to be provided in the location creation array. |
||
442 | * |
||
443 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create the content in the given location |
||
444 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the provided remoteId exists in the system, required properties on |
||
445 | * struct are missing or invalid, or if multiple locations are under the |
||
446 | * same parent. |
||
447 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $contentCreateStruct is not valid, |
||
448 | * or if a required field is missing / set to an empty value. |
||
449 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType, |
||
450 | * or value is set for non-translatable field in language |
||
451 | * other than main. |
||
452 | * |
||
453 | * @param \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct $contentCreateStruct |
||
454 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct[] $locationCreateStructs For each location parent under which a location should be created for the content |
||
455 | * |
||
456 | * @return \eZ\Publish\API\Repository\Values\Content\Content - the newly created content draft |
||
457 | */ |
||
458 | public function createContent(APIContentCreateStruct $contentCreateStruct, array $locationCreateStructs = array()) |
||
459 | { |
||
460 | if ($contentCreateStruct->mainLanguageCode === null) { |
||
461 | throw new InvalidArgumentException('$contentCreateStruct', "'mainLanguageCode' property must be set"); |
||
462 | } |
||
463 | |||
464 | if ($contentCreateStruct->contentType === null) { |
||
465 | throw new InvalidArgumentException('$contentCreateStruct', "'contentType' property must be set"); |
||
466 | } |
||
467 | |||
468 | $contentCreateStruct = clone $contentCreateStruct; |
||
469 | |||
470 | if ($contentCreateStruct->ownerId === null) { |
||
471 | $contentCreateStruct->ownerId = $this->repository->getCurrentUserReference()->getUserId(); |
||
472 | } |
||
473 | |||
474 | if ($contentCreateStruct->alwaysAvailable === null) { |
||
475 | $contentCreateStruct->alwaysAvailable = false; |
||
476 | } |
||
477 | |||
478 | $contentCreateStruct->contentType = $this->repository->getContentTypeService()->loadContentType( |
||
479 | $contentCreateStruct->contentType->id |
||
480 | ); |
||
481 | |||
482 | if (empty($contentCreateStruct->sectionId)) { |
||
483 | if (isset($locationCreateStructs[0])) { |
||
484 | $location = $this->repository->getLocationService()->loadLocation( |
||
485 | $locationCreateStructs[0]->parentLocationId |
||
486 | ); |
||
487 | $contentCreateStruct->sectionId = $location->contentInfo->sectionId; |
||
488 | } else { |
||
489 | $contentCreateStruct->sectionId = 1; |
||
490 | } |
||
491 | } |
||
492 | |||
493 | if (!$this->repository->canUser('content', 'create', $contentCreateStruct, $locationCreateStructs)) { |
||
494 | throw new UnauthorizedException( |
||
495 | 'content', |
||
496 | 'create', |
||
497 | array( |
||
498 | 'parentLocationId' => isset($locationCreateStructs[0]) ? |
||
499 | $locationCreateStructs[0]->parentLocationId : |
||
500 | null, |
||
501 | 'sectionId' => $contentCreateStruct->sectionId, |
||
502 | ) |
||
503 | ); |
||
504 | } |
||
505 | |||
506 | if (!empty($contentCreateStruct->remoteId)) { |
||
507 | try { |
||
508 | $this->loadContentByRemoteId($contentCreateStruct->remoteId); |
||
509 | |||
510 | throw new InvalidArgumentException( |
||
511 | '$contentCreateStruct', |
||
512 | "Another content with remoteId '{$contentCreateStruct->remoteId}' exists" |
||
513 | ); |
||
514 | } catch (APINotFoundException $e) { |
||
515 | // Do nothing |
||
516 | } |
||
517 | } else { |
||
518 | $contentCreateStruct->remoteId = $this->domainMapper->getUniqueHash($contentCreateStruct); |
||
519 | } |
||
520 | |||
521 | $spiLocationCreateStructs = $this->buildSPILocationCreateStructs($locationCreateStructs); |
||
522 | |||
523 | $languageCodes = $this->getLanguageCodesForCreate($contentCreateStruct); |
||
524 | $fields = $this->mapFieldsForCreate($contentCreateStruct); |
||
525 | |||
526 | $fieldValues = array(); |
||
527 | $spiFields = array(); |
||
528 | $allFieldErrors = array(); |
||
529 | $inputRelations = array(); |
||
530 | $locationIdToContentIdMapping = array(); |
||
531 | |||
532 | foreach ($contentCreateStruct->contentType->getFieldDefinitions() as $fieldDefinition) { |
||
533 | /** @var $fieldType \eZ\Publish\Core\FieldType\FieldType */ |
||
534 | $fieldType = $this->fieldTypeRegistry->getFieldType( |
||
535 | $fieldDefinition->fieldTypeIdentifier |
||
536 | ); |
||
537 | |||
538 | foreach ($languageCodes as $languageCode) { |
||
539 | $isEmptyValue = false; |
||
540 | $valueLanguageCode = $fieldDefinition->isTranslatable ? $languageCode : $contentCreateStruct->mainLanguageCode; |
||
541 | $isLanguageMain = $languageCode === $contentCreateStruct->mainLanguageCode; |
||
542 | if (isset($fields[$fieldDefinition->identifier][$valueLanguageCode])) { |
||
543 | $fieldValue = $fields[$fieldDefinition->identifier][$valueLanguageCode]->value; |
||
544 | } else { |
||
545 | $fieldValue = $fieldDefinition->defaultValue; |
||
546 | } |
||
547 | |||
548 | $fieldValue = $fieldType->acceptValue($fieldValue); |
||
549 | |||
550 | View Code Duplication | if ($fieldType->isEmptyValue($fieldValue)) { |
|
551 | $isEmptyValue = true; |
||
552 | if ($fieldDefinition->isRequired) { |
||
553 | $allFieldErrors[$fieldDefinition->id][$languageCode] = new ValidationError( |
||
554 | "Value for required field definition '%identifier%' with language '%languageCode%' is empty", |
||
555 | null, |
||
556 | ['%identifier%' => $fieldDefinition->identifier, '%languageCode%' => $languageCode], |
||
557 | 'empty' |
||
558 | ); |
||
559 | } |
||
560 | } else { |
||
561 | $fieldErrors = $fieldType->validate( |
||
562 | $fieldDefinition, |
||
563 | $fieldValue |
||
564 | ); |
||
565 | if (!empty($fieldErrors)) { |
||
566 | $allFieldErrors[$fieldDefinition->id][$languageCode] = $fieldErrors; |
||
567 | } |
||
568 | } |
||
569 | |||
570 | if (!empty($allFieldErrors)) { |
||
571 | continue; |
||
572 | } |
||
573 | |||
574 | $this->relationProcessor->appendFieldRelations( |
||
575 | $inputRelations, |
||
576 | $locationIdToContentIdMapping, |
||
577 | $fieldType, |
||
578 | $fieldValue, |
||
579 | $fieldDefinition->id |
||
580 | ); |
||
581 | $fieldValues[$fieldDefinition->identifier][$languageCode] = $fieldValue; |
||
582 | |||
583 | // Only non-empty value for: translatable field or in main language |
||
584 | if ( |
||
585 | (!$isEmptyValue && $fieldDefinition->isTranslatable) || |
||
586 | (!$isEmptyValue && $isLanguageMain) |
||
587 | ) { |
||
588 | $spiFields[] = new SPIField( |
||
589 | array( |
||
590 | 'id' => null, |
||
591 | 'fieldDefinitionId' => $fieldDefinition->id, |
||
592 | 'type' => $fieldDefinition->fieldTypeIdentifier, |
||
593 | 'value' => $fieldType->toPersistenceValue($fieldValue), |
||
594 | 'languageCode' => $languageCode, |
||
595 | 'versionNo' => null, |
||
596 | ) |
||
597 | ); |
||
598 | } |
||
599 | } |
||
600 | } |
||
601 | |||
602 | if (!empty($allFieldErrors)) { |
||
603 | throw new ContentFieldValidationException($allFieldErrors); |
||
604 | } |
||
605 | |||
606 | $spiContentCreateStruct = new SPIContentCreateStruct( |
||
607 | array( |
||
608 | 'name' => $this->nameSchemaService->resolve( |
||
609 | $contentCreateStruct->contentType->nameSchema, |
||
610 | $contentCreateStruct->contentType, |
||
611 | $fieldValues, |
||
612 | $languageCodes |
||
613 | ), |
||
614 | 'typeId' => $contentCreateStruct->contentType->id, |
||
615 | 'sectionId' => $contentCreateStruct->sectionId, |
||
616 | 'ownerId' => $contentCreateStruct->ownerId, |
||
617 | 'locations' => $spiLocationCreateStructs, |
||
618 | 'fields' => $spiFields, |
||
619 | 'alwaysAvailable' => $contentCreateStruct->alwaysAvailable, |
||
620 | 'remoteId' => $contentCreateStruct->remoteId, |
||
621 | 'modified' => isset($contentCreateStruct->modificationDate) ? $contentCreateStruct->modificationDate->getTimestamp() : time(), |
||
622 | 'initialLanguageId' => $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode( |
||
623 | $contentCreateStruct->mainLanguageCode |
||
624 | )->id, |
||
625 | ) |
||
626 | ); |
||
627 | |||
628 | $defaultObjectStates = $this->getDefaultObjectStates(); |
||
629 | |||
630 | $this->repository->beginTransaction(); |
||
631 | try { |
||
632 | $spiContent = $this->persistenceHandler->contentHandler()->create($spiContentCreateStruct); |
||
633 | $this->relationProcessor->processFieldRelations( |
||
634 | $inputRelations, |
||
635 | $spiContent->versionInfo->contentInfo->id, |
||
636 | $spiContent->versionInfo->versionNo, |
||
637 | $contentCreateStruct->contentType |
||
638 | ); |
||
639 | |||
640 | foreach ($defaultObjectStates as $objectStateGroupId => $objectState) { |
||
641 | $this->persistenceHandler->objectStateHandler()->setContentState( |
||
642 | $spiContent->versionInfo->contentInfo->id, |
||
643 | $objectStateGroupId, |
||
644 | $objectState->id |
||
645 | ); |
||
646 | } |
||
647 | |||
648 | $this->repository->commit(); |
||
649 | } catch (Exception $e) { |
||
650 | $this->repository->rollback(); |
||
651 | throw $e; |
||
652 | } |
||
653 | |||
654 | return $this->domainMapper->buildContentDomainObject($spiContent); |
||
655 | } |
||
656 | |||
657 | /** |
||
658 | * Returns an array of default content states with content state group id as key. |
||
659 | * |
||
660 | * @return \eZ\Publish\SPI\Persistence\Content\ObjectState[] |
||
661 | */ |
||
662 | protected function getDefaultObjectStates() |
||
663 | { |
||
664 | $defaultObjectStatesMap = array(); |
||
665 | $objectStateHandler = $this->persistenceHandler->objectStateHandler(); |
||
666 | |||
667 | foreach ($objectStateHandler->loadAllGroups() as $objectStateGroup) { |
||
668 | foreach ($objectStateHandler->loadObjectStates($objectStateGroup->id) as $objectState) { |
||
669 | // Only register the first object state which is the default one. |
||
670 | $defaultObjectStatesMap[$objectStateGroup->id] = $objectState; |
||
671 | break; |
||
672 | } |
||
673 | } |
||
674 | |||
675 | return $defaultObjectStatesMap; |
||
676 | } |
||
677 | |||
678 | /** |
||
679 | * Returns all language codes used in given $fields. |
||
680 | * |
||
681 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if no field value is set in main language |
||
682 | * |
||
683 | * @param \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct $contentCreateStruct |
||
684 | * |
||
685 | * @return string[] |
||
686 | */ |
||
687 | protected function getLanguageCodesForCreate(APIContentCreateStruct $contentCreateStruct) |
||
688 | { |
||
689 | $languageCodes = array(); |
||
690 | |||
691 | View Code Duplication | foreach ($contentCreateStruct->fields as $field) { |
|
692 | if ($field->languageCode === null || isset($languageCodes[$field->languageCode])) { |
||
693 | continue; |
||
694 | } |
||
695 | |||
696 | $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode( |
||
697 | $field->languageCode |
||
698 | ); |
||
699 | $languageCodes[$field->languageCode] = true; |
||
700 | } |
||
701 | |||
702 | if (!isset($languageCodes[$contentCreateStruct->mainLanguageCode])) { |
||
703 | $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode( |
||
704 | $contentCreateStruct->mainLanguageCode |
||
705 | ); |
||
706 | $languageCodes[$contentCreateStruct->mainLanguageCode] = true; |
||
707 | } |
||
708 | |||
709 | return array_keys($languageCodes); |
||
710 | } |
||
711 | |||
712 | /** |
||
713 | * Returns an array of fields like $fields[$field->fieldDefIdentifier][$field->languageCode]. |
||
714 | * |
||
715 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType |
||
716 | * or value is set for non-translatable field in language |
||
717 | * other than main |
||
718 | * |
||
719 | * @param \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct $contentCreateStruct |
||
720 | * |
||
721 | * @return array |
||
722 | */ |
||
723 | protected function mapFieldsForCreate(APIContentCreateStruct $contentCreateStruct) |
||
724 | { |
||
725 | $fields = array(); |
||
726 | |||
727 | foreach ($contentCreateStruct->fields as $field) { |
||
728 | $fieldDefinition = $contentCreateStruct->contentType->getFieldDefinition($field->fieldDefIdentifier); |
||
729 | |||
730 | if ($fieldDefinition === null) { |
||
731 | throw new ContentValidationException( |
||
732 | "Field definition '%identifier%' does not exist in given ContentType", |
||
733 | ['%identifier%' => $field->fieldDefIdentifier] |
||
734 | ); |
||
735 | } |
||
736 | |||
737 | if ($field->languageCode === null) { |
||
738 | $field = $this->cloneField( |
||
739 | $field, |
||
740 | array('languageCode' => $contentCreateStruct->mainLanguageCode) |
||
741 | ); |
||
742 | } |
||
743 | |||
744 | View Code Duplication | if (!$fieldDefinition->isTranslatable && ($field->languageCode != $contentCreateStruct->mainLanguageCode)) { |
|
745 | throw new ContentValidationException( |
||
746 | "A value is set for non translatable field definition '%identifier%' with language '%languageCode%'", |
||
747 | ['%identifier%' => $field->fieldDefIdentifier, '%languageCode%' => $field->languageCode] |
||
748 | ); |
||
749 | } |
||
750 | |||
751 | $fields[$field->fieldDefIdentifier][$field->languageCode] = $field; |
||
752 | } |
||
753 | |||
754 | return $fields; |
||
755 | } |
||
756 | |||
757 | /** |
||
758 | * Clones $field with overriding specific properties from given $overrides array. |
||
759 | * |
||
760 | * @param Field $field |
||
761 | * @param array $overrides |
||
762 | * |
||
763 | * @return Field |
||
764 | */ |
||
765 | private function cloneField(Field $field, array $overrides = array()) |
||
766 | { |
||
767 | $fieldData = array_merge( |
||
768 | array( |
||
769 | 'id' => $field->id, |
||
770 | 'value' => $field->value, |
||
771 | 'languageCode' => $field->languageCode, |
||
772 | 'fieldDefIdentifier' => $field->fieldDefIdentifier, |
||
773 | ), |
||
774 | $overrides |
||
775 | ); |
||
776 | |||
777 | return new Field($fieldData); |
||
778 | } |
||
779 | |||
780 | /** |
||
781 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
782 | * |
||
783 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct[] $locationCreateStructs |
||
784 | * |
||
785 | * @return \eZ\Publish\SPI\Persistence\Content\Location\CreateStruct[] |
||
786 | */ |
||
787 | protected function buildSPILocationCreateStructs(array $locationCreateStructs) |
||
788 | { |
||
789 | $spiLocationCreateStructs = array(); |
||
790 | $parentLocationIdSet = array(); |
||
791 | $mainLocation = true; |
||
792 | |||
793 | foreach ($locationCreateStructs as $locationCreateStruct) { |
||
794 | if (isset($parentLocationIdSet[$locationCreateStruct->parentLocationId])) { |
||
795 | throw new InvalidArgumentException( |
||
796 | '$locationCreateStructs', |
||
797 | "Multiple LocationCreateStructs with the same parent Location '{$locationCreateStruct->parentLocationId}' are given" |
||
798 | ); |
||
799 | } |
||
800 | |||
801 | $parentLocationIdSet[$locationCreateStruct->parentLocationId] = true; |
||
802 | $parentLocation = $this->repository->getLocationService()->loadLocation( |
||
803 | $locationCreateStruct->parentLocationId |
||
804 | ); |
||
805 | |||
806 | $spiLocationCreateStructs[] = $this->domainMapper->buildSPILocationCreateStruct( |
||
807 | $locationCreateStruct, |
||
808 | $parentLocation, |
||
809 | $mainLocation, |
||
810 | // For Content draft contentId and contentVersionNo are set in ContentHandler upon draft creation |
||
811 | null, |
||
812 | null |
||
813 | ); |
||
814 | |||
815 | // First Location in the list will be created as main Location |
||
816 | $mainLocation = false; |
||
817 | } |
||
818 | |||
819 | return $spiLocationCreateStructs; |
||
820 | } |
||
821 | |||
822 | /** |
||
823 | * Updates the metadata. |
||
824 | * |
||
825 | * (see {@link ContentMetadataUpdateStruct}) of a content object - to update fields use updateContent |
||
826 | * |
||
827 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update the content meta data |
||
828 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the remoteId in $contentMetadataUpdateStruct is set but already exists |
||
829 | * |
||
830 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
831 | * @param \eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct $contentMetadataUpdateStruct |
||
832 | * |
||
833 | * @return \eZ\Publish\API\Repository\Values\Content\Content the content with the updated attributes |
||
834 | */ |
||
835 | public function updateContentMetadata(ContentInfo $contentInfo, ContentMetadataUpdateStruct $contentMetadataUpdateStruct) |
||
836 | { |
||
837 | $propertyCount = 0; |
||
838 | foreach ($contentMetadataUpdateStruct as $propertyName => $propertyValue) { |
||
839 | if (isset($contentMetadataUpdateStruct->$propertyName)) { |
||
840 | $propertyCount += 1; |
||
841 | } |
||
842 | } |
||
843 | if ($propertyCount === 0) { |
||
844 | throw new InvalidArgumentException( |
||
845 | '$contentMetadataUpdateStruct', |
||
846 | 'At least one property must be set' |
||
847 | ); |
||
848 | } |
||
849 | |||
850 | $loadedContentInfo = $this->loadContentInfo($contentInfo->id); |
||
851 | |||
852 | if (!$this->repository->canUser('content', 'edit', $loadedContentInfo)) { |
||
853 | throw new UnauthorizedException('content', 'edit', array('contentId' => $loadedContentInfo->id)); |
||
854 | } |
||
855 | |||
856 | if (isset($contentMetadataUpdateStruct->remoteId)) { |
||
857 | try { |
||
858 | $existingContentInfo = $this->loadContentInfoByRemoteId($contentMetadataUpdateStruct->remoteId); |
||
859 | |||
860 | if ($existingContentInfo->id !== $loadedContentInfo->id) { |
||
861 | throw new InvalidArgumentException( |
||
862 | '$contentMetadataUpdateStruct', |
||
863 | "Another content with remoteId '{$contentMetadataUpdateStruct->remoteId}' exists" |
||
864 | ); |
||
865 | } |
||
866 | } catch (APINotFoundException $e) { |
||
867 | // Do nothing |
||
868 | } |
||
869 | } |
||
870 | |||
871 | $this->repository->beginTransaction(); |
||
872 | try { |
||
873 | if ($propertyCount > 1 || !isset($contentMetadataUpdateStruct->mainLocationId)) { |
||
874 | $this->persistenceHandler->contentHandler()->updateMetadata( |
||
875 | $loadedContentInfo->id, |
||
876 | new SPIMetadataUpdateStruct( |
||
877 | array( |
||
878 | 'ownerId' => $contentMetadataUpdateStruct->ownerId, |
||
879 | 'publicationDate' => isset($contentMetadataUpdateStruct->publishedDate) ? |
||
880 | $contentMetadataUpdateStruct->publishedDate->getTimestamp() : |
||
881 | null, |
||
882 | 'modificationDate' => isset($contentMetadataUpdateStruct->modificationDate) ? |
||
883 | $contentMetadataUpdateStruct->modificationDate->getTimestamp() : |
||
884 | null, |
||
885 | 'mainLanguageId' => isset($contentMetadataUpdateStruct->mainLanguageCode) ? |
||
886 | $this->repository->getContentLanguageService()->loadLanguage( |
||
887 | $contentMetadataUpdateStruct->mainLanguageCode |
||
888 | )->id : |
||
889 | null, |
||
890 | 'alwaysAvailable' => $contentMetadataUpdateStruct->alwaysAvailable, |
||
891 | 'remoteId' => $contentMetadataUpdateStruct->remoteId, |
||
892 | ) |
||
893 | ) |
||
894 | ); |
||
895 | } |
||
896 | |||
897 | // Change main location |
||
898 | if (isset($contentMetadataUpdateStruct->mainLocationId) |
||
899 | && $loadedContentInfo->mainLocationId !== $contentMetadataUpdateStruct->mainLocationId) { |
||
900 | $this->persistenceHandler->locationHandler()->changeMainLocation( |
||
901 | $loadedContentInfo->id, |
||
902 | $contentMetadataUpdateStruct->mainLocationId |
||
903 | ); |
||
904 | } |
||
905 | |||
906 | // Republish URL aliases to update always-available flag |
||
907 | if (isset($contentMetadataUpdateStruct->alwaysAvailable) |
||
908 | && $loadedContentInfo->alwaysAvailable !== $contentMetadataUpdateStruct->alwaysAvailable) { |
||
909 | $content = $this->loadContent($loadedContentInfo->id); |
||
910 | $this->publishUrlAliasesForContent($content, false); |
||
911 | } |
||
912 | |||
913 | $this->repository->commit(); |
||
914 | } catch (Exception $e) { |
||
915 | $this->repository->rollback(); |
||
916 | throw $e; |
||
917 | } |
||
918 | |||
919 | return isset($content) ? $content : $this->loadContent($loadedContentInfo->id); |
||
920 | } |
||
921 | |||
922 | /** |
||
923 | * Publishes URL aliases for all locations of a given content. |
||
924 | * |
||
925 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
926 | * @param bool $updatePathIdentificationString this parameter is legacy storage specific for updating |
||
927 | * ezcontentobject_tree.path_identification_string, it is ignored by other storage engines |
||
928 | */ |
||
929 | protected function publishUrlAliasesForContent(APIContent $content, $updatePathIdentificationString = true) |
||
930 | { |
||
931 | $urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema($content); |
||
932 | $locations = $this->repository->getLocationService()->loadLocations( |
||
933 | $content->getVersionInfo()->getContentInfo() |
||
934 | ); |
||
935 | foreach ($locations as $location) { |
||
936 | foreach ($urlAliasNames as $languageCode => $name) { |
||
937 | $this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation( |
||
938 | $location->id, |
||
939 | $location->parentLocationId, |
||
940 | $name, |
||
941 | $languageCode, |
||
942 | $content->contentInfo->alwaysAvailable, |
||
943 | $updatePathIdentificationString ? $languageCode === $content->contentInfo->mainLanguageCode : false |
||
944 | ); |
||
945 | } |
||
946 | } |
||
947 | } |
||
948 | |||
949 | /** |
||
950 | * Deletes a content object including all its versions and locations including their subtrees. |
||
951 | * |
||
952 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete the content (in one of the locations of the given content object) |
||
953 | * |
||
954 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
955 | * |
||
956 | * @return mixed[] Affected Location Id's |
||
957 | */ |
||
958 | public function deleteContent(ContentInfo $contentInfo) |
||
959 | { |
||
960 | $contentInfo = $this->internalLoadContentInfo($contentInfo->id); |
||
961 | |||
962 | if (!$this->repository->canUser('content', 'remove', $contentInfo)) { |
||
963 | throw new UnauthorizedException('content', 'remove', array('contentId' => $contentInfo->id)); |
||
964 | } |
||
965 | |||
966 | $affectedLocations = []; |
||
967 | $this->repository->beginTransaction(); |
||
968 | try { |
||
969 | // Load Locations first as deleting Content also deletes belonging Locations |
||
970 | $spiLocations = $this->persistenceHandler->locationHandler()->loadLocationsByContent($contentInfo->id); |
||
971 | $this->persistenceHandler->contentHandler()->deleteContent($contentInfo->id); |
||
972 | foreach ($spiLocations as $spiLocation) { |
||
973 | $this->persistenceHandler->urlAliasHandler()->locationDeleted($spiLocation->id); |
||
974 | $affectedLocations[] = $spiLocation->id; |
||
975 | } |
||
976 | $this->repository->commit(); |
||
977 | } catch (Exception $e) { |
||
978 | $this->repository->rollback(); |
||
979 | throw $e; |
||
980 | } |
||
981 | |||
982 | return $affectedLocations; |
||
983 | } |
||
984 | |||
985 | /** |
||
986 | * Creates a draft from a published or archived version. |
||
987 | * |
||
988 | * If no version is given, the current published version is used. |
||
989 | * 4.x: The draft is created with the initialLanguage code of the source version or if not present with the main language. |
||
990 | * It can be changed on updating the version. |
||
991 | * |
||
992 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the current-user is not allowed to create the draft |
||
993 | * |
||
994 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
995 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
996 | * @param \eZ\Publish\API\Repository\Values\User\User $creator if set given user is used to create the draft - otherwise the current-user is used |
||
997 | * |
||
998 | * @return \eZ\Publish\API\Repository\Values\Content\Content - the newly created content draft |
||
999 | */ |
||
1000 | public function createContentDraft(ContentInfo $contentInfo, APIVersionInfo $versionInfo = null, User $creator = null) |
||
1001 | { |
||
1002 | $contentInfo = $this->loadContentInfo($contentInfo->id); |
||
1003 | |||
1004 | if ($versionInfo !== null) { |
||
1005 | // Check that given $contentInfo and $versionInfo belong to the same content |
||
1006 | if ($versionInfo->getContentInfo()->id != $contentInfo->id) { |
||
1007 | throw new InvalidArgumentException( |
||
1008 | '$versionInfo', |
||
1009 | 'VersionInfo does not belong to the same content as given ContentInfo' |
||
1010 | ); |
||
1011 | } |
||
1012 | |||
1013 | $versionInfo = $this->loadVersionInfoById($contentInfo->id, $versionInfo->versionNo); |
||
1014 | |||
1015 | switch ($versionInfo->status) { |
||
1016 | case VersionInfo::STATUS_PUBLISHED: |
||
1017 | case VersionInfo::STATUS_ARCHIVED: |
||
1018 | break; |
||
1019 | |||
1020 | default: |
||
1021 | // @todo: throw an exception here, to be defined |
||
1022 | throw new BadStateException( |
||
1023 | '$versionInfo', |
||
1024 | 'Draft can not be created from a draft version' |
||
1025 | ); |
||
1026 | } |
||
1027 | |||
1028 | $versionNo = $versionInfo->versionNo; |
||
1029 | } elseif ($contentInfo->published) { |
||
1030 | $versionNo = $contentInfo->currentVersionNo; |
||
1031 | } else { |
||
1032 | // @todo: throw an exception here, to be defined |
||
1033 | throw new BadStateException( |
||
1034 | '$contentInfo', |
||
1035 | 'Content is not published, draft can be created only from published or archived version' |
||
1036 | ); |
||
1037 | } |
||
1038 | |||
1039 | if ($creator === null) { |
||
1040 | $creator = $this->repository->getCurrentUserReference(); |
||
1041 | } |
||
1042 | |||
1043 | if (!$this->repository->canUser('content', 'edit', $contentInfo)) { |
||
1044 | throw new UnauthorizedException('content', 'edit', array('contentId' => $contentInfo->id)); |
||
1045 | } |
||
1046 | |||
1047 | $this->repository->beginTransaction(); |
||
1048 | try { |
||
1049 | $spiContent = $this->persistenceHandler->contentHandler()->createDraftFromVersion( |
||
1050 | $contentInfo->id, |
||
1051 | $versionNo, |
||
1052 | $creator->getUserId() |
||
1053 | ); |
||
1054 | $this->repository->commit(); |
||
1055 | } catch (Exception $e) { |
||
1056 | $this->repository->rollback(); |
||
1057 | throw $e; |
||
1058 | } |
||
1059 | |||
1060 | return $this->domainMapper->buildContentDomainObject($spiContent); |
||
1061 | } |
||
1062 | |||
1063 | /** |
||
1064 | * Loads drafts for a user. |
||
1065 | * |
||
1066 | * If no user is given the drafts for the authenticated user a returned |
||
1067 | * |
||
1068 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the current-user is not allowed to load the draft list |
||
1069 | * |
||
1070 | * @param \eZ\Publish\API\Repository\Values\User\UserReference $user |
||
1071 | * |
||
1072 | * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo the drafts ({@link VersionInfo}) owned by the given user |
||
1073 | */ |
||
1074 | public function loadContentDrafts(User $user = null) |
||
1075 | { |
||
1076 | if ($user === null) { |
||
1077 | $user = $this->repository->getCurrentUserReference(); |
||
1078 | } |
||
1079 | |||
1080 | // throw early if user has absolutely no access to versionread |
||
1081 | if ($this->repository->hasAccess('content', 'versionread') === false) { |
||
1082 | throw new UnauthorizedException('content', 'versionread'); |
||
1083 | } |
||
1084 | |||
1085 | $spiVersionInfoList = $this->persistenceHandler->contentHandler()->loadDraftsForUser($user->getUserId()); |
||
1086 | $versionInfoList = array(); |
||
1087 | View Code Duplication | foreach ($spiVersionInfoList as $spiVersionInfo) { |
|
1088 | $versionInfo = $this->domainMapper->buildVersionInfoDomainObject($spiVersionInfo); |
||
1089 | if (!$this->repository->canUser('content', 'versionread', $versionInfo)) { |
||
1090 | throw new UnauthorizedException('content', 'versionread', array('contentId' => $versionInfo->contentInfo->id)); |
||
1091 | } |
||
1092 | |||
1093 | $versionInfoList[] = $versionInfo; |
||
1094 | } |
||
1095 | |||
1096 | return $versionInfoList; |
||
1097 | } |
||
1098 | |||
1099 | /** |
||
1100 | * Translate a version. |
||
1101 | * |
||
1102 | * updates the destination version given in $translationInfo with the provided translated fields in $translationValues |
||
1103 | * |
||
1104 | * @example Examples/translation_5x.php |
||
1105 | * |
||
1106 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the current-user is not allowed to update this version |
||
1107 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the given destination version is not a draft |
||
1108 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $translationValues is not valid, or if a required field is missing or is set to an empty value. |
||
1109 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType |
||
1110 | * or value is set for non-translatable field in language |
||
1111 | * other than main. |
||
1112 | * |
||
1113 | * @param \eZ\Publish\API\Repository\Values\Content\TranslationInfo $translationInfo |
||
1114 | * @param \eZ\Publish\API\Repository\Values\Content\TranslationValues $translationValues |
||
1115 | * @param \eZ\Publish\API\Repository\Values\User\User $modifier If set, this user is taken as modifier of the version |
||
1116 | * |
||
1117 | * @return \eZ\Publish\API\Repository\Values\Content\Content the content draft with the translated fields |
||
1118 | * |
||
1119 | * @since 5.0 |
||
1120 | */ |
||
1121 | public function translateVersion(TranslationInfo $translationInfo, APITranslationValues $translationValues, User $modifier = null) |
||
1122 | { |
||
1123 | throw new NotImplementedException(__METHOD__); |
||
1124 | } |
||
1125 | |||
1126 | /** |
||
1127 | * Updates the fields of a draft. |
||
1128 | * |
||
1129 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update this version |
||
1130 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
1131 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if a property on the struct is invalid. |
||
1132 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $contentCreateStruct is not valid, |
||
1133 | * or if a required field is missing / set to an empty value. |
||
1134 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType, |
||
1135 | * or value is set for non-translatable field in language |
||
1136 | * other than main. |
||
1137 | * |
||
1138 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
1139 | * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct |
||
1140 | * |
||
1141 | * @return \eZ\Publish\API\Repository\Values\Content\Content the content draft with the updated fields |
||
1142 | */ |
||
1143 | public function updateContent(APIVersionInfo $versionInfo, APIContentUpdateStruct $contentUpdateStruct) |
||
1144 | { |
||
1145 | $contentUpdateStruct = clone $contentUpdateStruct; |
||
1146 | |||
1147 | /** @var $content \eZ\Publish\Core\Repository\Values\Content\Content */ |
||
1148 | $content = $this->loadContent( |
||
1149 | $versionInfo->getContentInfo()->id, |
||
1150 | null, |
||
1151 | $versionInfo->versionNo |
||
1152 | ); |
||
1153 | if (!$content->versionInfo->isDraft()) { |
||
1154 | throw new BadStateException( |
||
1155 | '$versionInfo', |
||
1156 | 'Version is not a draft and can not be updated' |
||
1157 | ); |
||
1158 | } |
||
1159 | |||
1160 | if (!$this->repository->canUser('content', 'edit', $content)) { |
||
1161 | throw new UnauthorizedException('content', 'edit', array('contentId' => $content->id)); |
||
1162 | } |
||
1163 | |||
1164 | $mainLanguageCode = $content->contentInfo->mainLanguageCode; |
||
1165 | $languageCodes = $this->getLanguageCodesForUpdate($contentUpdateStruct, $content); |
||
1166 | $contentType = $this->repository->getContentTypeService()->loadContentType( |
||
1167 | $content->contentInfo->contentTypeId |
||
1168 | ); |
||
1169 | $fields = $this->mapFieldsForUpdate( |
||
1170 | $contentUpdateStruct, |
||
1171 | $contentType, |
||
1172 | $mainLanguageCode |
||
1173 | ); |
||
1174 | |||
1175 | $fieldValues = array(); |
||
1176 | $spiFields = array(); |
||
1177 | $allFieldErrors = array(); |
||
1178 | $inputRelations = array(); |
||
1179 | $locationIdToContentIdMapping = array(); |
||
1180 | |||
1181 | foreach ($contentType->getFieldDefinitions() as $fieldDefinition) { |
||
1182 | /** @var $fieldType \eZ\Publish\SPI\FieldType\FieldType */ |
||
1183 | $fieldType = $this->fieldTypeRegistry->getFieldType( |
||
1184 | $fieldDefinition->fieldTypeIdentifier |
||
1185 | ); |
||
1186 | |||
1187 | foreach ($languageCodes as $languageCode) { |
||
1188 | $isCopied = $isEmpty = $isRetained = false; |
||
1189 | $isLanguageNew = !in_array($languageCode, $content->versionInfo->languageCodes); |
||
1190 | $valueLanguageCode = $fieldDefinition->isTranslatable ? $languageCode : $mainLanguageCode; |
||
1191 | $isFieldUpdated = isset($fields[$fieldDefinition->identifier][$valueLanguageCode]); |
||
1192 | $isProcessed = isset($fieldValues[$fieldDefinition->identifier][$valueLanguageCode]); |
||
1193 | |||
1194 | if (!$isFieldUpdated && !$isLanguageNew) { |
||
1195 | $isRetained = true; |
||
1196 | $fieldValue = $content->getField($fieldDefinition->identifier, $valueLanguageCode)->value; |
||
1197 | } elseif (!$isFieldUpdated && $isLanguageNew && !$fieldDefinition->isTranslatable) { |
||
1198 | $isCopied = true; |
||
1199 | $fieldValue = $content->getField($fieldDefinition->identifier, $valueLanguageCode)->value; |
||
1200 | } elseif ($isFieldUpdated) { |
||
1201 | $fieldValue = $fields[$fieldDefinition->identifier][$valueLanguageCode]->value; |
||
1202 | } else { |
||
1203 | $fieldValue = $fieldDefinition->defaultValue; |
||
1204 | } |
||
1205 | |||
1206 | $fieldValue = $fieldType->acceptValue($fieldValue); |
||
1207 | |||
1208 | View Code Duplication | if ($fieldType->isEmptyValue($fieldValue)) { |
|
1209 | $isEmpty = true; |
||
1210 | if ($fieldDefinition->isRequired) { |
||
1211 | $allFieldErrors[$fieldDefinition->id][$languageCode] = new ValidationError( |
||
1212 | "Value for required field definition '%identifier%' with language '%languageCode%' is empty", |
||
1213 | null, |
||
1214 | ['%identifier%' => $fieldDefinition->identifier, '%languageCode%' => $languageCode], |
||
1215 | 'empty' |
||
1216 | ); |
||
1217 | } |
||
1218 | } else { |
||
1219 | $fieldErrors = $fieldType->validate( |
||
1220 | $fieldDefinition, |
||
1221 | $fieldValue |
||
1222 | ); |
||
1223 | if (!empty($fieldErrors)) { |
||
1224 | $allFieldErrors[$fieldDefinition->id][$languageCode] = $fieldErrors; |
||
1225 | } |
||
1226 | } |
||
1227 | |||
1228 | if (!empty($allFieldErrors)) { |
||
1229 | continue; |
||
1230 | } |
||
1231 | |||
1232 | $this->relationProcessor->appendFieldRelations( |
||
1233 | $inputRelations, |
||
1234 | $locationIdToContentIdMapping, |
||
1235 | $fieldType, |
||
1236 | $fieldValue, |
||
1237 | $fieldDefinition->id |
||
1238 | ); |
||
1239 | $fieldValues[$fieldDefinition->identifier][$languageCode] = $fieldValue; |
||
1240 | |||
1241 | if ($isRetained || $isCopied || ($isLanguageNew && $isEmpty) || $isProcessed) { |
||
1242 | continue; |
||
1243 | } |
||
1244 | |||
1245 | $spiFields[] = new SPIField( |
||
1246 | array( |
||
1247 | 'id' => $isLanguageNew ? |
||
1248 | null : |
||
1249 | $content->getField($fieldDefinition->identifier, $languageCode)->id, |
||
1250 | 'fieldDefinitionId' => $fieldDefinition->id, |
||
1251 | 'type' => $fieldDefinition->fieldTypeIdentifier, |
||
1252 | 'value' => $fieldType->toPersistenceValue($fieldValue), |
||
1253 | 'languageCode' => $languageCode, |
||
1254 | 'versionNo' => $versionInfo->versionNo, |
||
1255 | ) |
||
1256 | ); |
||
1257 | } |
||
1258 | } |
||
1259 | |||
1260 | if (!empty($allFieldErrors)) { |
||
1261 | throw new ContentFieldValidationException($allFieldErrors); |
||
1262 | } |
||
1263 | |||
1264 | $spiContentUpdateStruct = new SPIContentUpdateStruct( |
||
1265 | array( |
||
1266 | 'name' => $this->nameSchemaService->resolveNameSchema( |
||
1267 | $content, |
||
1268 | $fieldValues, |
||
1269 | $languageCodes, |
||
1270 | $contentType |
||
1271 | ), |
||
1272 | 'creatorId' => $contentUpdateStruct->creatorId ?: $this->repository->getCurrentUserReference()->getUserId(), |
||
1273 | 'fields' => $spiFields, |
||
1274 | 'modificationDate' => time(), |
||
1275 | 'initialLanguageId' => $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode( |
||
1276 | $contentUpdateStruct->initialLanguageCode |
||
1277 | )->id, |
||
1278 | ) |
||
1279 | ); |
||
1280 | $existingRelations = $this->loadRelations($versionInfo); |
||
1281 | |||
1282 | $this->repository->beginTransaction(); |
||
1283 | try { |
||
1284 | $spiContent = $this->persistenceHandler->contentHandler()->updateContent( |
||
1285 | $versionInfo->getContentInfo()->id, |
||
1286 | $versionInfo->versionNo, |
||
1287 | $spiContentUpdateStruct |
||
1288 | ); |
||
1289 | $this->relationProcessor->processFieldRelations( |
||
1290 | $inputRelations, |
||
1291 | $spiContent->versionInfo->contentInfo->id, |
||
1292 | $spiContent->versionInfo->versionNo, |
||
1293 | $contentType, |
||
1294 | $existingRelations |
||
1295 | ); |
||
1296 | $this->repository->commit(); |
||
1297 | } catch (Exception $e) { |
||
1298 | $this->repository->rollback(); |
||
1299 | throw $e; |
||
1300 | } |
||
1301 | |||
1302 | return $this->domainMapper->buildContentDomainObject( |
||
1303 | $spiContent, |
||
1304 | $contentType |
||
1305 | ); |
||
1306 | } |
||
1307 | |||
1308 | /** |
||
1309 | * Returns all language codes used in given $fields. |
||
1310 | * |
||
1311 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if no field value exists in initial language |
||
1312 | * |
||
1313 | * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct |
||
1314 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
1315 | * |
||
1316 | * @return array |
||
1317 | */ |
||
1318 | protected function getLanguageCodesForUpdate(APIContentUpdateStruct $contentUpdateStruct, APIContent $content) |
||
1319 | { |
||
1320 | if ($contentUpdateStruct->initialLanguageCode !== null) { |
||
1321 | $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode( |
||
1322 | $contentUpdateStruct->initialLanguageCode |
||
1323 | ); |
||
1324 | } else { |
||
1325 | $contentUpdateStruct->initialLanguageCode = $content->contentInfo->mainLanguageCode; |
||
1326 | } |
||
1327 | |||
1328 | $languageCodes = array_fill_keys($content->versionInfo->languageCodes, true); |
||
1329 | $languageCodes[$contentUpdateStruct->initialLanguageCode] = true; |
||
1330 | |||
1331 | View Code Duplication | foreach ($contentUpdateStruct->fields as $field) { |
|
1332 | if ($field->languageCode === null || isset($languageCodes[$field->languageCode])) { |
||
1333 | continue; |
||
1334 | } |
||
1335 | |||
1336 | $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode( |
||
1337 | $field->languageCode |
||
1338 | ); |
||
1339 | $languageCodes[$field->languageCode] = true; |
||
1340 | } |
||
1341 | |||
1342 | return array_keys($languageCodes); |
||
1343 | } |
||
1344 | |||
1345 | /** |
||
1346 | * Returns an array of fields like $fields[$field->fieldDefIdentifier][$field->languageCode]. |
||
1347 | * |
||
1348 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType |
||
1349 | * or value is set for non-translatable field in language |
||
1350 | * other than main |
||
1351 | * |
||
1352 | * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct |
||
1353 | * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType |
||
1354 | * @param string $mainLanguageCode |
||
1355 | * |
||
1356 | * @return array |
||
1357 | */ |
||
1358 | protected function mapFieldsForUpdate( |
||
1359 | APIContentUpdateStruct $contentUpdateStruct, |
||
1360 | ContentType $contentType, |
||
1361 | $mainLanguageCode |
||
1362 | ) { |
||
1363 | $fields = array(); |
||
1364 | |||
1365 | foreach ($contentUpdateStruct->fields as $field) { |
||
1366 | $fieldDefinition = $contentType->getFieldDefinition($field->fieldDefIdentifier); |
||
1367 | |||
1368 | if ($fieldDefinition === null) { |
||
1369 | throw new ContentValidationException( |
||
1370 | "Field definition '%identifier%' does not exist in given ContentType", |
||
1371 | ['%identifier%' => $field->fieldDefIdentifier] |
||
1372 | ); |
||
1373 | } |
||
1374 | |||
1375 | if ($field->languageCode === null) { |
||
1376 | if ($fieldDefinition->isTranslatable) { |
||
1377 | $languageCode = $contentUpdateStruct->initialLanguageCode; |
||
1378 | } else { |
||
1379 | $languageCode = $mainLanguageCode; |
||
1380 | } |
||
1381 | $field = $this->cloneField($field, array('languageCode' => $languageCode)); |
||
1382 | } |
||
1383 | |||
1384 | View Code Duplication | if (!$fieldDefinition->isTranslatable && ($field->languageCode != $mainLanguageCode)) { |
|
1385 | throw new ContentValidationException( |
||
1386 | "A value is set for non translatable field definition '%identifier%' with language '%languageCode%'", |
||
1387 | ['%identifier%' => $field->fieldDefIdentifier, '%languageCode%' => $field->languageCode] |
||
1388 | ); |
||
1389 | } |
||
1390 | |||
1391 | $fields[$field->fieldDefIdentifier][$field->languageCode] = $field; |
||
1392 | } |
||
1393 | |||
1394 | return $fields; |
||
1395 | } |
||
1396 | |||
1397 | /** |
||
1398 | * Publishes a content version. |
||
1399 | * |
||
1400 | * Publishes a content version and deletes archive versions if they overflow max archive versions. |
||
1401 | * Max archive versions are currently a configuration, but might be moved to be a param of ContentType in the future. |
||
1402 | * |
||
1403 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to publish this version |
||
1404 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
1405 | * |
||
1406 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
1407 | * |
||
1408 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
1409 | */ |
||
1410 | public function publishVersion(APIVersionInfo $versionInfo) |
||
1411 | { |
||
1412 | $content = $this->internalLoadContent( |
||
1413 | $versionInfo->contentInfo->id, |
||
1414 | null, |
||
1415 | $versionInfo->versionNo |
||
1416 | ); |
||
1417 | |||
1418 | if (!$this->repository->canUser('content', 'publish', $content)) { |
||
1419 | throw new UnauthorizedException('content', 'publish', array('contentId' => $content->id)); |
||
1420 | } |
||
1421 | |||
1422 | $this->repository->beginTransaction(); |
||
1423 | try { |
||
1424 | $content = $this->internalPublishVersion($content->getVersionInfo()); |
||
1425 | $this->repository->commit(); |
||
1426 | } catch (Exception $e) { |
||
1427 | $this->repository->rollback(); |
||
1428 | throw $e; |
||
1429 | } |
||
1430 | |||
1431 | return $content; |
||
1432 | } |
||
1433 | |||
1434 | /** |
||
1435 | * Publishes a content version. |
||
1436 | * |
||
1437 | * Publishes a content version and deletes archive versions if they overflow max archive versions. |
||
1438 | * Max archive versions are currently a configuration, but might be moved to be a param of ContentType in the future. |
||
1439 | * |
||
1440 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
1441 | * |
||
1442 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
1443 | * @param int|null $publicationDate If null existing date is kept if there is one, otherwise current time is used. |
||
1444 | * |
||
1445 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
1446 | */ |
||
1447 | protected function internalPublishVersion(APIVersionInfo $versionInfo, $publicationDate = null) |
||
1448 | { |
||
1449 | if (!$versionInfo->isDraft()) { |
||
1450 | throw new BadStateException('$versionInfo', 'Only versions in draft status can be published.'); |
||
1451 | } |
||
1452 | |||
1453 | $currentTime = time(); |
||
1454 | if ($publicationDate === null && $versionInfo->versionNo === 1) { |
||
1455 | $publicationDate = $currentTime; |
||
1456 | } |
||
1457 | |||
1458 | $metadataUpdateStruct = new SPIMetadataUpdateStruct(); |
||
1459 | $metadataUpdateStruct->publicationDate = $publicationDate; |
||
1460 | $metadataUpdateStruct->modificationDate = $currentTime; |
||
1461 | |||
1462 | $contentId = $versionInfo->getContentInfo()->id; |
||
1463 | $spiContent = $this->persistenceHandler->contentHandler()->publish( |
||
1464 | $contentId, |
||
1465 | $versionInfo->versionNo, |
||
1466 | $metadataUpdateStruct |
||
1467 | ); |
||
1468 | |||
1469 | $content = $this->domainMapper->buildContentDomainObject($spiContent); |
||
1470 | |||
1471 | $this->publishUrlAliasesForContent($content); |
||
1472 | |||
1473 | // Delete version archive overflow if any, limit is 0-50 (however 0 will mean 1 if content is unpublished) |
||
1474 | $archiveList = $this->persistenceHandler->contentHandler()->listVersions( |
||
1475 | $contentId, |
||
1476 | APIVersionInfo::STATUS_ARCHIVED, |
||
1477 | 100 // Limited to avoid publishing taking to long, besides SE limitations this is why limit is max 50 |
||
1478 | ); |
||
1479 | |||
1480 | $maxVersionArchiveCount = max(0, min(50, $this->settings['default_version_archive_limit'])); |
||
1481 | while (!empty($archiveList) && count($archiveList) > $maxVersionArchiveCount) { |
||
1482 | /** @var \eZ\Publish\SPI\Persistence\Content\VersionInfo $archiveVersion */ |
||
1483 | $archiveVersion = array_shift($archiveList); |
||
1484 | $this->persistenceHandler->contentHandler()->deleteVersion( |
||
1485 | $contentId, |
||
1486 | $archiveVersion->versionNo |
||
1487 | ); |
||
1488 | } |
||
1489 | |||
1490 | return $content; |
||
1491 | } |
||
1492 | |||
1493 | /** |
||
1494 | * Removes the given version. |
||
1495 | * |
||
1496 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is in |
||
1497 | * published state or is the last version of the Content |
||
1498 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to remove this version |
||
1499 | * |
||
1500 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
1501 | */ |
||
1502 | public function deleteVersion(APIVersionInfo $versionInfo) |
||
1503 | { |
||
1504 | if ($versionInfo->isPublished()) { |
||
1505 | throw new BadStateException( |
||
1506 | '$versionInfo', |
||
1507 | 'Version is published and can not be removed' |
||
1508 | ); |
||
1509 | } |
||
1510 | |||
1511 | View Code Duplication | if (!$this->repository->canUser('content', 'versionremove', $versionInfo)) { |
|
1512 | throw new UnauthorizedException( |
||
1513 | 'content', |
||
1514 | 'versionremove', |
||
1515 | array('contentId' => $versionInfo->contentInfo->id, 'versionNo' => $versionInfo->versionNo) |
||
1516 | ); |
||
1517 | } |
||
1518 | |||
1519 | $versionList = $this->persistenceHandler->contentHandler()->listVersions( |
||
1520 | $versionInfo->contentInfo->id, |
||
1521 | null, |
||
1522 | 2 |
||
1523 | ); |
||
1524 | |||
1525 | if (count($versionList) === 1) { |
||
1526 | throw new BadStateException( |
||
1527 | '$versionInfo', |
||
1528 | 'Version is the last version of the Content and can not be removed' |
||
1529 | ); |
||
1530 | } |
||
1531 | |||
1532 | $this->repository->beginTransaction(); |
||
1533 | try { |
||
1534 | $this->persistenceHandler->contentHandler()->deleteVersion( |
||
1535 | $versionInfo->getContentInfo()->id, |
||
1536 | $versionInfo->versionNo |
||
1537 | ); |
||
1538 | $this->repository->commit(); |
||
1539 | } catch (Exception $e) { |
||
1540 | $this->repository->rollback(); |
||
1541 | throw $e; |
||
1542 | } |
||
1543 | } |
||
1544 | |||
1545 | /** |
||
1546 | * Loads all versions for the given content. |
||
1547 | * |
||
1548 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to list versions |
||
1549 | * |
||
1550 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
1551 | * |
||
1552 | * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo[] Sorted by creation date |
||
1553 | */ |
||
1554 | public function loadVersions(ContentInfo $contentInfo) |
||
1555 | { |
||
1556 | if (!$this->repository->canUser('content', 'versionread', $contentInfo)) { |
||
1557 | throw new UnauthorizedException('content', 'versionread', array('contentId' => $contentInfo->id)); |
||
1558 | } |
||
1559 | |||
1560 | $spiVersionInfoList = $this->persistenceHandler->contentHandler()->listVersions($contentInfo->id); |
||
1561 | |||
1562 | $versions = array(); |
||
1563 | View Code Duplication | foreach ($spiVersionInfoList as $spiVersionInfo) { |
|
1564 | $versionInfo = $this->domainMapper->buildVersionInfoDomainObject($spiVersionInfo); |
||
1565 | if (!$this->repository->canUser('content', 'versionread', $versionInfo)) { |
||
1566 | throw new UnauthorizedException('content', 'versionread', array('versionId' => $versionInfo->id)); |
||
1567 | } |
||
1568 | |||
1569 | $versions[] = $versionInfo; |
||
1570 | } |
||
1571 | |||
1572 | return $versions; |
||
1573 | } |
||
1574 | |||
1575 | /** |
||
1576 | * Copies the content to a new location. If no version is given, |
||
1577 | * all versions are copied, otherwise only the given version. |
||
1578 | * |
||
1579 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to copy the content to the given location |
||
1580 | * |
||
1581 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
1582 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $destinationLocationCreateStruct the target location where the content is copied to |
||
1583 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
1584 | * |
||
1585 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
1586 | */ |
||
1587 | public function copyContent(ContentInfo $contentInfo, LocationCreateStruct $destinationLocationCreateStruct, APIVersionInfo $versionInfo = null) |
||
1588 | { |
||
1589 | $destinationLocation = $this->repository->getLocationService()->loadLocation( |
||
1590 | $destinationLocationCreateStruct->parentLocationId |
||
1591 | ); |
||
1592 | View Code Duplication | if (!$this->repository->canUser('content', 'create', $contentInfo, [$destinationLocation])) { |
|
1593 | throw new UnauthorizedException( |
||
1594 | 'content', |
||
1595 | 'create', |
||
1596 | [ |
||
1597 | 'parentLocationId' => $destinationLocationCreateStruct->parentLocationId, |
||
1598 | 'sectionId' => $contentInfo->sectionId, |
||
1599 | ] |
||
1600 | ); |
||
1601 | } |
||
1602 | |||
1603 | $defaultObjectStates = $this->getDefaultObjectStates(); |
||
1604 | |||
1605 | $this->repository->beginTransaction(); |
||
1606 | try { |
||
1607 | $spiContent = $this->persistenceHandler->contentHandler()->copy( |
||
1608 | $contentInfo->id, |
||
1609 | $versionInfo ? $versionInfo->versionNo : null |
||
1610 | ); |
||
1611 | |||
1612 | foreach ($defaultObjectStates as $objectStateGroupId => $objectState) { |
||
1613 | $this->persistenceHandler->objectStateHandler()->setContentState( |
||
1614 | $spiContent->versionInfo->contentInfo->id, |
||
1615 | $objectStateGroupId, |
||
1616 | $objectState->id |
||
1617 | ); |
||
1618 | } |
||
1619 | |||
1620 | $content = $this->internalPublishVersion( |
||
1621 | $this->domainMapper->buildVersionInfoDomainObject($spiContent->versionInfo), |
||
1622 | $spiContent->versionInfo->creationDate |
||
1623 | ); |
||
1624 | |||
1625 | $this->repository->getLocationService()->createLocation( |
||
1626 | $content->getVersionInfo()->getContentInfo(), |
||
1627 | $destinationLocationCreateStruct |
||
1628 | ); |
||
1629 | $this->repository->commit(); |
||
1630 | } catch (Exception $e) { |
||
1631 | $this->repository->rollback(); |
||
1632 | throw $e; |
||
1633 | } |
||
1634 | |||
1635 | return $this->internalLoadContent($content->id); |
||
1636 | } |
||
1637 | |||
1638 | /** |
||
1639 | * Loads all outgoing relations for the given version. |
||
1640 | * |
||
1641 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read this version |
||
1642 | * |
||
1643 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
1644 | * |
||
1645 | * @return \eZ\Publish\API\Repository\Values\Content\Relation[] |
||
1646 | */ |
||
1647 | public function loadRelations(APIVersionInfo $versionInfo) |
||
1682 | |||
1683 | /** |
||
1684 | * Loads all incoming relations for a content object. |
||
1685 | * |
||
1686 | * The relations come only from published versions of the source content objects |
||
1687 | * |
||
1688 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read this version |
||
1689 | * |
||
1690 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
1691 | * |
||
1692 | * @return \eZ\Publish\API\Repository\Values\Content\Relation[] |
||
1693 | */ |
||
1694 | public function loadReverseRelations(ContentInfo $contentInfo) |
||
1720 | |||
1721 | /** |
||
1722 | * Adds a relation of type common. |
||
1723 | * |
||
1724 | * The source of the relation is the content and version |
||
1725 | * referenced by $versionInfo. |
||
1726 | * |
||
1727 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to edit this version |
||
1728 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
1729 | * |
||
1730 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $sourceVersion |
||
1731 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $destinationContent the destination of the relation |
||
1732 | * |
||
1733 | * @return \eZ\Publish\API\Repository\Values\Content\Relation the newly created relation |
||
1734 | */ |
||
1735 | public function addRelation(APIVersionInfo $sourceVersion, ContentInfo $destinationContent) |
||
1776 | |||
1777 | /** |
||
1778 | * Removes a relation of type COMMON from a draft. |
||
1779 | * |
||
1780 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed edit this version |
||
1781 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
1782 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if there is no relation of type COMMON for the given destination |
||
1783 | * |
||
1784 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $sourceVersion |
||
1785 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $destinationContent |
||
1786 | */ |
||
1787 | public function deleteRelation(APIVersionInfo $sourceVersion, ContentInfo $destinationContent) |
||
1837 | |||
1838 | /** |
||
1839 | * Adds translation information to the content object. |
||
1840 | * |
||
1841 | * @example Examples/translation_5x.php |
||
1842 | * |
||
1843 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed add a translation info |
||
1844 | * |
||
1845 | * @param \eZ\Publish\API\Repository\Values\Content\TranslationInfo $translationInfo |
||
1846 | * |
||
1847 | * @since 5.0 |
||
1848 | */ |
||
1849 | public function addTranslationInfo(TranslationInfo $translationInfo) |
||
1853 | |||
1854 | /** |
||
1855 | * lists the translations done on this content object. |
||
1856 | * |
||
1857 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed read translation infos |
||
1858 | * |
||
1859 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
1860 | * @param array $filter |
||
1861 | * |
||
1862 | * @todo TBD - filter by source version destination version and languages |
||
1863 | * |
||
1864 | * @return \eZ\Publish\API\Repository\Values\Content\TranslationInfo[] |
||
1865 | * |
||
1866 | * @since 5.0 |
||
1867 | */ |
||
1868 | public function loadTranslationInfos(ContentInfo $contentInfo, array $filter = array()) |
||
1872 | |||
1873 | /** |
||
1874 | * Remove Content Object translation from all Versions (including archived ones) of a Content Object. |
||
1875 | * |
||
1876 | * NOTE: this operation is risky and permanent, so user interface (ideally CLI) should provide |
||
1877 | * a warning before performing it. |
||
1878 | * |
||
1879 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the specified translation |
||
1880 | * is the only one a Version has or it is the main translation of a Content Object. |
||
1881 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed |
||
1882 | * to delete the content (in one of the locations of the given Content Object). |
||
1883 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if languageCode argument |
||
1884 | * is invalid for the given content. |
||
1885 | * |
||
1886 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
1887 | * @param string $languageCode |
||
1888 | * |
||
1889 | * @since 6.11 |
||
1890 | */ |
||
1891 | public function removeTranslation(ContentInfo $contentInfo, $languageCode) |
||
1892 | { |
||
1893 | if ($contentInfo->mainLanguageCode === $languageCode) { |
||
1894 | throw new BadStateException( |
||
1895 | '$languageCode', |
||
1896 | 'Specified translation is the main translation of the Content Object' |
||
1897 | ); |
||
1898 | } |
||
1899 | // before actual removal, collect information on Versions |
||
1900 | $versions = []; |
||
1901 | foreach ($this->loadVersions($contentInfo) as $versionInfo) { |
||
1902 | // check if user is authorized to delete Version |
||
1903 | View Code Duplication | if (!$this->repository->canUser('content', 'delete', $versionInfo)) { |
|
1904 | throw new UnauthorizedException( |
||
1905 | 'content', |
||
1906 | 'delete', |
||
1907 | [ |
||
1908 | 'contentId' => $contentInfo->id, |
||
1909 | 'versionNo' => $versionInfo->versionNo, |
||
1910 | ] |
||
1911 | ); |
||
1912 | } |
||
1913 | // check if the specified translation exists for the Version |
||
1914 | if (!in_array($languageCode, $versionInfo->languageCodes)) { |
||
1915 | // if translation does not exist, simply ignore Version (see InvalidArgumentException later on) |
||
1916 | continue; |
||
1917 | } |
||
1918 | // check if the specified translation is not the only one |
||
1919 | if (count($versionInfo->languageCodes) < 2) { |
||
1920 | throw new BadStateException( |
||
1921 | '$languageCode', |
||
1922 | 'Specified translation is the only one Content Object Version has' |
||
1923 | ); |
||
1924 | } |
||
1925 | // add version to the list |
||
1926 | $versions[] = $versionInfo->versionNo; |
||
1927 | } |
||
1928 | |||
1929 | // if there are no Versions with the given translation, $languageCode arg is invalid |
||
1930 | if (empty($versions)) { |
||
1931 | throw new InvalidArgumentException( |
||
1932 | '$languageCode', |
||
1933 | sprintf( |
||
1934 | 'Specified translation does not exist in the Content Object(id=%d)', |
||
1935 | $contentInfo->id |
||
1936 | ) |
||
1937 | ); |
||
1938 | } |
||
1939 | |||
1940 | $this->repository->beginTransaction(); |
||
1941 | try { |
||
1942 | $this->persistenceHandler->contentHandler()->removeTranslationFromContent($contentInfo->id, $languageCode); |
||
1943 | $locationIds = array_map( |
||
1944 | function (Location $location) { |
||
1945 | return $location->id; |
||
1946 | }, |
||
1947 | $this->repository->getLocationService()->loadLocations($contentInfo) |
||
1948 | ); |
||
1949 | $this->persistenceHandler->urlAliasHandler()->translationRemoved($locationIds, $languageCode); |
||
1950 | $this->repository->commit(); |
||
1951 | } catch (Exception $e) { |
||
1952 | $this->repository->rollback(); |
||
1953 | // cover generic unexpected exception to fulfill API promise on @throws |
||
1954 | throw new BadStateException('$contentInfo', 'Translation removal failed', $e); |
||
1955 | } |
||
1956 | } |
||
1957 | |||
1958 | /** |
||
1959 | * Instantiates a new content create struct object. |
||
1960 | * |
||
1961 | * alwaysAvailable is set to the ContentType's defaultAlwaysAvailable |
||
1962 | * |
||
1963 | * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType |
||
1964 | * @param string $mainLanguageCode |
||
1965 | * |
||
1966 | * @return \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct |
||
1967 | */ |
||
1968 | public function newContentCreateStruct(ContentType $contentType, $mainLanguageCode) |
||
1978 | |||
1979 | /** |
||
1980 | * Instantiates a new content meta data update struct. |
||
1981 | * |
||
1982 | * @return \eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct |
||
1983 | */ |
||
1984 | public function newContentMetadataUpdateStruct() |
||
1988 | |||
1989 | /** |
||
1990 | * Instantiates a new content update struct. |
||
1991 | * |
||
1992 | * @return \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct |
||
1993 | */ |
||
1994 | public function newContentUpdateStruct() |
||
1998 | |||
1999 | /** |
||
2000 | * Instantiates a new TranslationInfo object. |
||
2001 | * |
||
2002 | * @return \eZ\Publish\API\Repository\Values\Content\TranslationInfo |
||
2003 | */ |
||
2004 | public function newTranslationInfo() |
||
2008 | |||
2009 | /** |
||
2010 | * Instantiates a Translation object. |
||
2011 | * |
||
2012 | * @return \eZ\Publish\API\Repository\Values\Content\TranslationValues |
||
2013 | */ |
||
2014 | public function newTranslationValues() |
||
2018 | } |
||
2019 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.