Completed
Push — 6.13 ( 878d09...f11ecd )
by
unknown
26:52
created

ContentService::publishVersion()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the eZ\Publish\Core\Repository\ContentService class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Repository;
10
11
use eZ\Publish\API\Repository\ContentService as ContentServiceInterface;
12
use eZ\Publish\API\Repository\Repository as RepositoryInterface;
13
use eZ\Publish\Core\Repository\Values\Content\Location;
14
use eZ\Publish\API\Repository\Values\Content\Language;
15
use eZ\Publish\SPI\Persistence\Handler;
16
use eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct as APIContentUpdateStruct;
17
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
18
use eZ\Publish\API\Repository\Values\Content\TranslationInfo;
19
use eZ\Publish\API\Repository\Values\Content\TranslationValues as APITranslationValues;
20
use eZ\Publish\API\Repository\Values\Content\ContentCreateStruct as APIContentCreateStruct;
21
use eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct;
22
use eZ\Publish\API\Repository\Values\Content\Content as APIContent;
23
use eZ\Publish\API\Repository\Values\Content\VersionInfo as APIVersionInfo;
24
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
25
use eZ\Publish\API\Repository\Values\User\User;
26
use eZ\Publish\API\Repository\Values\Content\LocationCreateStruct;
27
use eZ\Publish\API\Repository\Values\Content\Field;
28
use eZ\Publish\API\Repository\Values\Content\Relation as APIRelation;
29
use eZ\Publish\API\Repository\Exceptions\NotFoundException as APINotFoundException;
30
use eZ\Publish\Core\Base\Exceptions\BadStateException;
31
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
32
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
33
use eZ\Publish\Core\Base\Exceptions\ContentValidationException;
34
use eZ\Publish\Core\Base\Exceptions\ContentFieldValidationException;
35
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
36
use eZ\Publish\Core\FieldType\ValidationError;
37
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
38
use eZ\Publish\Core\Repository\Values\Content\ContentCreateStruct;
39
use eZ\Publish\Core\Repository\Values\Content\ContentUpdateStruct;
40
use eZ\Publish\Core\Repository\Values\Content\TranslationValues;
41
use eZ\Publish\SPI\Persistence\Content\MetadataUpdateStruct as SPIMetadataUpdateStruct;
42
use eZ\Publish\SPI\Persistence\Content\CreateStruct as SPIContentCreateStruct;
43
use eZ\Publish\SPI\Persistence\Content\UpdateStruct as SPIContentUpdateStruct;
44
use eZ\Publish\SPI\Persistence\Content\Field as SPIField;
45
use eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct as SPIRelationCreateStruct;
46
use Exception;
47
use eZ\Publish\API\Repository\Exceptions\NotImplementedException;
48
49
/**
50
 * This class provides service methods for managing content.
51
 *
52
 * @example Examples/content.php
53
 */
54
class ContentService implements ContentServiceInterface
55
{
56
    /**
57
     * @var \eZ\Publish\Core\Repository\Repository
58
     */
59
    protected $repository;
60
61
    /**
62
     * @var \eZ\Publish\SPI\Persistence\Handler
63
     */
64
    protected $persistenceHandler;
65
66
    /**
67
     * @var array
68
     */
69
    protected $settings;
70
71
    /**
72
     * @var \eZ\Publish\Core\Repository\Helper\DomainMapper
73
     */
74
    protected $domainMapper;
75
76
    /**
77
     * @var \eZ\Publish\Core\Repository\Helper\RelationProcessor
78
     */
79
    protected $relationProcessor;
80
81
    /**
82
     * @var \eZ\Publish\Core\Repository\Helper\NameSchemaService
83
     */
84
    protected $nameSchemaService;
85
86
    /**
87
     * @var \eZ\Publish\Core\Repository\Helper\FieldTypeRegistry
88
     */
89
    protected $fieldTypeRegistry;
90
91
    /**
92
     * Setups service with reference to repository object that created it & corresponding handler.
93
     *
94
     * @param \eZ\Publish\API\Repository\Repository $repository
95
     * @param \eZ\Publish\SPI\Persistence\Handler $handler
96
     * @param \eZ\Publish\Core\Repository\Helper\DomainMapper $domainMapper
97
     * @param \eZ\Publish\Core\Repository\Helper\RelationProcessor $relationProcessor
98
     * @param \eZ\Publish\Core\Repository\Helper\NameSchemaService $nameSchemaService
99
     * @param \eZ\Publish\Core\Repository\Helper\FieldTypeRegistry $fieldTypeRegistry,
0 ignored issues
show
Documentation introduced by
There is no parameter named $fieldTypeRegistry,. Did you maybe mean $fieldTypeRegistry?

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 method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
100
     * @param array $settings
101
     */
102
    public function __construct(
103
        RepositoryInterface $repository,
104
        Handler $handler,
105
        Helper\DomainMapper $domainMapper,
106
        Helper\RelationProcessor $relationProcessor,
107
        Helper\NameSchemaService $nameSchemaService,
108
        Helper\FieldTypeRegistry $fieldTypeRegistry,
109
        array $settings = array()
110
    ) {
111
        $this->repository = $repository;
0 ignored issues
show
Documentation Bug introduced by
$repository is of type object<eZ\Publish\API\Repository\Repository>, but the property $repository was declared to be of type object<eZ\Publish\Core\Repository\Repository>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
112
        $this->persistenceHandler = $handler;
113
        $this->domainMapper = $domainMapper;
114
        $this->relationProcessor = $relationProcessor;
115
        $this->nameSchemaService = $nameSchemaService;
116
        $this->fieldTypeRegistry = $fieldTypeRegistry;
117
        // Union makes sure default settings are ignored if provided in argument
118
        $this->settings = $settings + array(
119
            // Version archive limit (0-50), only enforced on publish, not on un-publish.
120
            'default_version_archive_limit' => 5,
121
        );
122
    }
123
124
    /**
125
     * Loads a content info object.
126
     *
127
     * To load fields use loadContent
128
     *
129
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the content
130
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content with the given id does not exist
131
     *
132
     * @param int $contentId
133
     *
134
     * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo
135
     */
136 View Code Duplication
    public function loadContentInfo($contentId)
137
    {
138
        $contentInfo = $this->internalLoadContentInfo($contentId);
139
        if (!$this->repository->canUser('content', 'read', $contentInfo)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
140
            throw new UnauthorizedException('content', 'read', array('contentId' => $contentId));
141
        }
142
143
        return $contentInfo;
144
    }
145
146
    /**
147
     * Loads a content info object.
148
     *
149
     * To load fields use loadContent
150
     *
151
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content with the given id does not exist
152
     *
153
     * @param mixed $id
154
     * @param bool $isRemoteId
155
     *
156
     * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo
157
     */
158
    public function internalLoadContentInfo($id, $isRemoteId = false)
159
    {
160
        try {
161
            $method = $isRemoteId ? 'loadContentInfoByRemoteId' : 'loadContentInfo';
162
163
            return $this->domainMapper->buildContentInfoDomainObject(
164
                $this->persistenceHandler->contentHandler()->$method($id)
165
            );
166
        } catch (APINotFoundException $e) {
167
            throw new NotFoundException(
168
                'Content',
169
                $id,
170
                $e
171
            );
172
        }
173
    }
174
175
    /**
176
     * Loads a content info object for the given remoteId.
177
     *
178
     * To load fields use loadContent
179
     *
180
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the content
181
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content with the given remote id does not exist
182
     *
183
     * @param string $remoteId
184
     *
185
     * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo
186
     */
187 View Code Duplication
    public function loadContentInfoByRemoteId($remoteId)
188
    {
189
        $contentInfo = $this->internalLoadContentInfo($remoteId, true);
190
191
        if (!$this->repository->canUser('content', 'read', $contentInfo)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
192
            throw new UnauthorizedException('content', 'read', array('remoteId' => $remoteId));
193
        }
194
195
        return $contentInfo;
196
    }
197
198
    /**
199
     * Loads a version info of the given content object.
200
     *
201
     * If no version number is given, the method returns the current version
202
     *
203
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the version with the given number does not exist
204
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to load this version
205
     *
206
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
207
     * @param int $versionNo the version number. If not given the current version is returned.
208
     *
209
     * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo
210
     */
211
    public function loadVersionInfo(ContentInfo $contentInfo, $versionNo = null)
212
    {
213
        return $this->loadVersionInfoById($contentInfo->id, $versionNo);
214
    }
215
216
    /**
217
     * Loads a version info of the given content object id.
218
     *
219
     * If no version number is given, the method returns the current version
220
     *
221
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the version with the given number does not exist
222
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to load this version
223
     *
224
     * @param mixed $contentId
225
     * @param int $versionNo the version number. If not given the current version is returned.
226
     *
227
     * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo
228
     */
229
    public function loadVersionInfoById($contentId, $versionNo = null)
230
    {
231
        if ($versionNo === null) {
232
            $versionNo = $this->loadContentInfo($contentId)->currentVersionNo;
233
        }
234
235
        try {
236
            $spiVersionInfo = $this->persistenceHandler->contentHandler()->loadVersionInfo(
237
                $contentId,
238
                $versionNo
239
            );
240
        } catch (APINotFoundException $e) {
241
            throw new NotFoundException(
242
                'VersionInfo',
243
                array(
244
                    'contentId' => $contentId,
245
                    'versionNo' => $versionNo,
246
                ),
247
                $e
248
            );
249
        }
250
251
        $versionInfo = $this->domainMapper->buildVersionInfoDomainObject($spiVersionInfo);
252
253
        if ($versionInfo->isPublished()) {
254
            $function = 'read';
255
        } else {
256
            $function = 'versionread';
257
        }
258
259
        if (!$this->repository->canUser('content', $function, $versionInfo)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
260
            throw new UnauthorizedException('content', $function, array('contentId' => $contentId));
261
        }
262
263
        return $versionInfo;
264
    }
265
266
    /**
267
     * {@inheritdoc}
268
     */
269
    public function loadContentByContentInfo(ContentInfo $contentInfo, array $languages = null, $versionNo = null, $useAlwaysAvailable = true)
270
    {
271
        // Change $useAlwaysAvailable to false to avoid contentInfo lookup if we know alwaysAvailable is disabled
272
        if ($useAlwaysAvailable && !$contentInfo->alwaysAvailable) {
273
            $useAlwaysAvailable = false;
274
        }
275
276
        // As we have content info we can avoid that current version is looked up using spi in loadContent() if not set
277
        if ($versionNo === null) {
278
            $versionNo = $contentInfo->currentVersionNo;
279
        }
280
281
        return $this->loadContent(
282
            $contentInfo->id,
283
            $languages,
284
            $versionNo,
285
            $useAlwaysAvailable
286
        );
287
    }
288
289
    /**
290
     * {@inheritdoc}
291
     */
292
    public function loadContentByVersionInfo(APIVersionInfo $versionInfo, array $languages = null, $useAlwaysAvailable = true)
293
    {
294
        // Change $useAlwaysAvailable to false to avoid contentInfo lookup if we know alwaysAvailable is disabled
295
        if ($useAlwaysAvailable && !$versionInfo->getContentInfo()->alwaysAvailable) {
296
            $useAlwaysAvailable = false;
297
        }
298
299
        return $this->loadContent(
300
            $versionInfo->getContentInfo()->id,
301
            $languages,
302
            $versionInfo->versionNo,
303
            $useAlwaysAvailable
304
        );
305
    }
306
307
    /**
308
     * {@inheritdoc}
309
     */
310 View Code Duplication
    public function loadContent($contentId, array $languages = null, $versionNo = null, $useAlwaysAvailable = true)
311
    {
312
        $content = $this->internalLoadContent($contentId, $languages, $versionNo, false, $useAlwaysAvailable);
313
314
        if (!$this->repository->canUser('content', 'read', $content)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
315
            throw new UnauthorizedException('content', 'read', array('contentId' => $contentId));
316
        }
317
        if (
318
            !$content->getVersionInfo()->isPublished()
319
            && !$this->repository->canUser('content', 'versionread', $content)
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
320
        ) {
321
            throw new UnauthorizedException('content', 'versionread', array('contentId' => $contentId, 'versionNo' => $versionNo));
322
        }
323
324
        return $content;
325
    }
326
327
    /**
328
     * Loads content in a version of the given content object.
329
     *
330
     * If no version number is given, the method returns the current version
331
     *
332
     * @internal
333
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the content or version with the given id and languages does not exist
334
     *
335
     * @param mixed $id
336
     * @param array|null $languages A language priority, filters returned fields and is used as prioritized language code on
337
     *                         returned value object. If not given all languages are returned.
338
     * @param int|null $versionNo the version number. If not given the current version is returned
339
     * @param bool $isRemoteId
340
     * @param bool $useAlwaysAvailable Add Main language to \$languages if true (default) and if alwaysAvailable is true
341
     *
342
     * @return \eZ\Publish\API\Repository\Values\Content\Content
343
     */
344
    public function internalLoadContent($id, array $languages = null, $versionNo = null, $isRemoteId = false, $useAlwaysAvailable = true)
345
    {
346
        try {
347
            // Get Content ID if lookup by remote ID
348
            if ($isRemoteId) {
349
                $spiContentInfo = $this->persistenceHandler->contentHandler()->loadContentInfoByRemoteId($id);
350
                $id = $spiContentInfo->id;
351
                // Set $isRemoteId to false as the next loads will be for content id now that we have it (for exception use now)
352
                $isRemoteId = false;
353
            }
354
355
            // Get current version if $versionNo is not defined
356
            if ($versionNo === null) {
357
                if (!isset($spiContentInfo)) {
358
                    $spiContentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo($id);
359
                }
360
361
                $versionNo = $spiContentInfo->currentVersionNo;
362
            }
363
364
            $loadLanguages = $languages;
365
            $alwaysAvailableLanguageCode = null;
366
            // Set main language on $languages filter if not empty (all) and $useAlwaysAvailable being true
367
            if (!empty($loadLanguages) && $useAlwaysAvailable) {
368
                if (!isset($spiContentInfo)) {
369
                    $spiContentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo($id);
370
                }
371
372
                if ($spiContentInfo->alwaysAvailable) {
373
                    $loadLanguages[] = $alwaysAvailableLanguageCode = $spiContentInfo->mainLanguageCode;
374
                    $loadLanguages = array_unique($loadLanguages);
375
                }
376
            }
377
378
            $spiContent = $this->persistenceHandler->contentHandler()->load(
379
                $id,
380
                $versionNo,
381
                $loadLanguages
0 ignored issues
show
Bug introduced by
It seems like $loadLanguages defined by $languages on line 364 can also be of type array; however, eZ\Publish\SPI\Persistence\Content\Handler::load() does only seem to accept null|array<integer,string>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
382
            );
383
        } catch (APINotFoundException $e) {
384
            throw new NotFoundException(
385
                'Content',
386
                array(
387
                    $isRemoteId ? 'remoteId' : 'id' => $id,
388
                    'languages' => $languages,
389
                    'versionNo' => $versionNo,
390
                ),
391
                $e
392
            );
393
        }
394
395
        return $this->domainMapper->buildContentDomainObject(
396
            $spiContent,
397
            null,
398
            empty($languages) ? null : $languages,
399
            $alwaysAvailableLanguageCode
400
        );
401
    }
402
403
    /**
404
     * Loads content in a version for the content object reference by the given remote id.
405
     *
406
     * If no version is given, the method returns the current version
407
     *
408
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content or version with the given remote id does not exist
409
     * @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
410
     *
411
     * @param string $remoteId
412
     * @param array $languages A language filter for fields. If not given all languages are returned
413
     * @param int $versionNo the version number. If not given the current version is returned
414
     * @param bool $useAlwaysAvailable Add Main language to \$languages if true (default) and if alwaysAvailable is true
415
     *
416
     * @return \eZ\Publish\API\Repository\Values\Content\Content
417
     */
418 View Code Duplication
    public function loadContentByRemoteId($remoteId, array $languages = null, $versionNo = null, $useAlwaysAvailable = true)
419
    {
420
        $content = $this->internalLoadContent($remoteId, $languages, $versionNo, true, $useAlwaysAvailable);
421
422
        if (!$this->repository->canUser('content', 'read', $content)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
423
            throw new UnauthorizedException('content', 'read', array('remoteId' => $remoteId));
424
        }
425
426
        if (
427
            !$content->getVersionInfo()->isPublished()
428
            && !$this->repository->canUser('content', 'versionread', $content)
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
429
        ) {
430
            throw new UnauthorizedException('content', 'versionread', array('remoteId' => $remoteId, 'versionNo' => $versionNo));
431
        }
432
433
        return $content;
434
    }
435
436
    /**
437
     * Bulk-load Content items by the list of ContentInfo Value Objects.
438
     *
439
     * Note: it does not throw exceptions on load, just ignores erroneous Content item.
440
     * Moreover, since the method works on pre-loaded ContentInfo list, it is assumed that user is
441
     * allowed to access every Content on the list.
442
     *
443
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo[] $contentInfoList
444
     * @param string[] $languages A language priority, filters returned fields and is used as prioritized language code on
445
     *                            returned value object. If not given all languages are returned.
446
     * @param bool $useAlwaysAvailable Add Main language to \$languages if true (default) and if alwaysAvailable is true,
447
     *                                 unless all languages have been asked for.
448
     *
449
     * @return \eZ\Publish\API\Repository\Values\Content\Content[] list of Content items with Content Ids as keys
450
     */
451
    public function loadContentListByContentInfo(
452
        array $contentInfoList,
453
        array $languages = [],
454
        $useAlwaysAvailable = true
455
    ) {
456
        $loadAllLanguages = $languages === Language::ALL;
457
        $contentIds = [];
458
        $translations = $languages;
459
        foreach ($contentInfoList as $contentInfo) {
460
            $contentIds[] = $contentInfo->id;
461
            // Unless we are told to load all languages, we add main language to translations so they are loaded too
462
            // Might in some case load more languages then intended, but prioritised handling will pick right one
463
            if (!$loadAllLanguages && $useAlwaysAvailable && $contentInfo->alwaysAvailable) {
464
                $translations[] = $contentInfo->mainLanguageCode;
465
            }
466
        }
467
        $translations = array_unique($translations);
468
469
        $spiContentList = $this->persistenceHandler->contentHandler()->loadContentList(
470
            $contentIds,
471
            $translations
472
        );
473
        $contentList = [];
474
        foreach ($spiContentList as $contentId => $spiContent) {
475
            $contentInfo = $spiContent->versionInfo->contentInfo;
476
            $contentList[$contentId] = $this->domainMapper->buildContentDomainObject(
477
                $spiContent,
478
                null,
479
                $languages,
480
                $contentInfo->alwaysAvailable ? $contentInfo->mainLanguageCode : null
481
            );
482
        }
483
484
        return $contentList;
485
    }
486
487
    /**
488
     * Creates a new content draft assigned to the authenticated user.
489
     *
490
     * If a different userId is given in $contentCreateStruct it is assigned to the given user
491
     * but this required special rights for the authenticated user
492
     * (this is useful for content staging where the transfer process does not
493
     * have to authenticate with the user which created the content object in the source server).
494
     * The user has to publish the draft if it should be visible.
495
     * In 4.x at least one location has to be provided in the location creation array.
496
     *
497
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create the content in the given location
498
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the provided remoteId exists in the system, required properties on
499
     *                                                                        struct are missing or invalid, or if multiple locations are under the
500
     *                                                                        same parent.
501
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $contentCreateStruct is not valid,
502
     *                                                                               or if a required field is missing / set to an empty value.
503
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType,
504
     *                                                                          or value is set for non-translatable field in language
505
     *                                                                          other than main.
506
     *
507
     * @param \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct $contentCreateStruct
508
     * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct[] $locationCreateStructs For each location parent under which a location should be created for the content
509
     *
510
     * @return \eZ\Publish\API\Repository\Values\Content\Content - the newly created content draft
511
     */
512
    public function createContent(APIContentCreateStruct $contentCreateStruct, array $locationCreateStructs = array())
513
    {
514
        if ($contentCreateStruct->mainLanguageCode === null) {
515
            throw new InvalidArgumentException('$contentCreateStruct', "'mainLanguageCode' property must be set");
516
        }
517
518
        if ($contentCreateStruct->contentType === null) {
519
            throw new InvalidArgumentException('$contentCreateStruct', "'contentType' property must be set");
520
        }
521
522
        $contentCreateStruct = clone $contentCreateStruct;
523
524
        if ($contentCreateStruct->ownerId === null) {
525
            $contentCreateStruct->ownerId = $this->repository->getCurrentUserReference()->getUserId();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...tCurrentUserReference() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user reference.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
526
        }
527
528
        if ($contentCreateStruct->alwaysAvailable === null) {
529
            $contentCreateStruct->alwaysAvailable = false;
530
        }
531
532
        $contentCreateStruct->contentType = $this->repository->getContentTypeService()->loadContentType(
533
            $contentCreateStruct->contentType->id
534
        );
535
536
        if (empty($contentCreateStruct->sectionId)) {
537
            if (isset($locationCreateStructs[0])) {
538
                $location = $this->repository->getLocationService()->loadLocation(
539
                    $locationCreateStructs[0]->parentLocationId
540
                );
541
                $contentCreateStruct->sectionId = $location->contentInfo->sectionId;
542
            } else {
543
                $contentCreateStruct->sectionId = 1;
544
            }
545
        }
546
547
        if (!$this->repository->canUser('content', 'create', $contentCreateStruct, $locationCreateStructs)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
548
            throw new UnauthorizedException(
549
                'content',
550
                'create',
551
                array(
552
                    'parentLocationId' => isset($locationCreateStructs[0]) ?
553
                            $locationCreateStructs[0]->parentLocationId :
554
                            null,
555
                    'sectionId' => $contentCreateStruct->sectionId,
556
                )
557
            );
558
        }
559
560
        if (!empty($contentCreateStruct->remoteId)) {
561
            try {
562
                $this->loadContentByRemoteId($contentCreateStruct->remoteId);
563
564
                throw new InvalidArgumentException(
565
                    '$contentCreateStruct',
566
                    "Another content with remoteId '{$contentCreateStruct->remoteId}' exists"
567
                );
568
            } catch (APINotFoundException $e) {
569
                // Do nothing
570
            }
571
        } else {
572
            $contentCreateStruct->remoteId = $this->domainMapper->getUniqueHash($contentCreateStruct);
573
        }
574
575
        $spiLocationCreateStructs = $this->buildSPILocationCreateStructs($locationCreateStructs);
576
577
        $languageCodes = $this->getLanguageCodesForCreate($contentCreateStruct);
578
        $fields = $this->mapFieldsForCreate($contentCreateStruct);
579
580
        $fieldValues = array();
581
        $spiFields = array();
582
        $allFieldErrors = array();
583
        $inputRelations = array();
584
        $locationIdToContentIdMapping = array();
585
586
        foreach ($contentCreateStruct->contentType->getFieldDefinitions() as $fieldDefinition) {
587
            /** @var $fieldType \eZ\Publish\Core\FieldType\FieldType */
588
            $fieldType = $this->fieldTypeRegistry->getFieldType(
589
                $fieldDefinition->fieldTypeIdentifier
590
            );
591
592
            foreach ($languageCodes as $languageCode) {
593
                $isEmptyValue = false;
594
                $valueLanguageCode = $fieldDefinition->isTranslatable ? $languageCode : $contentCreateStruct->mainLanguageCode;
595
                $isLanguageMain = $languageCode === $contentCreateStruct->mainLanguageCode;
596
                if (isset($fields[$fieldDefinition->identifier][$valueLanguageCode])) {
597
                    $fieldValue = $fields[$fieldDefinition->identifier][$valueLanguageCode]->value;
598
                } else {
599
                    $fieldValue = $fieldDefinition->defaultValue;
600
                }
601
602
                $fieldValue = $fieldType->acceptValue($fieldValue);
603
604 View Code Duplication
                if ($fieldType->isEmptyValue($fieldValue)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
605
                    $isEmptyValue = true;
606
                    if ($fieldDefinition->isRequired) {
607
                        $allFieldErrors[$fieldDefinition->id][$languageCode] = new ValidationError(
608
                            "Value for required field definition '%identifier%' with language '%languageCode%' is empty",
609
                            null,
610
                            ['%identifier%' => $fieldDefinition->identifier, '%languageCode%' => $languageCode],
611
                            'empty'
612
                        );
613
                    }
614
                } else {
615
                    $fieldErrors = $fieldType->validate(
616
                        $fieldDefinition,
617
                        $fieldValue
618
                    );
619
                    if (!empty($fieldErrors)) {
620
                        $allFieldErrors[$fieldDefinition->id][$languageCode] = $fieldErrors;
621
                    }
622
                }
623
624
                if (!empty($allFieldErrors)) {
625
                    continue;
626
                }
627
628
                $this->relationProcessor->appendFieldRelations(
629
                    $inputRelations,
630
                    $locationIdToContentIdMapping,
631
                    $fieldType,
632
                    $fieldValue,
633
                    $fieldDefinition->id
634
                );
635
                $fieldValues[$fieldDefinition->identifier][$languageCode] = $fieldValue;
636
637
                // Only non-empty value for: translatable field or in main language
638
                if (
639
                    (!$isEmptyValue && $fieldDefinition->isTranslatable) ||
640
                    (!$isEmptyValue && $isLanguageMain)
641
                ) {
642
                    $spiFields[] = new SPIField(
643
                        array(
644
                            'id' => null,
645
                            'fieldDefinitionId' => $fieldDefinition->id,
646
                            'type' => $fieldDefinition->fieldTypeIdentifier,
647
                            'value' => $fieldType->toPersistenceValue($fieldValue),
648
                            'languageCode' => $languageCode,
649
                            'versionNo' => null,
650
                        )
651
                    );
652
                }
653
            }
654
        }
655
656
        if (!empty($allFieldErrors)) {
657
            throw new ContentFieldValidationException($allFieldErrors);
658
        }
659
660
        $spiContentCreateStruct = new SPIContentCreateStruct(
661
            array(
662
                'name' => $this->nameSchemaService->resolve(
663
                    $contentCreateStruct->contentType->nameSchema,
664
                    $contentCreateStruct->contentType,
665
                    $fieldValues,
666
                    $languageCodes
667
                ),
668
                'typeId' => $contentCreateStruct->contentType->id,
669
                'sectionId' => $contentCreateStruct->sectionId,
670
                'ownerId' => $contentCreateStruct->ownerId,
671
                'locations' => $spiLocationCreateStructs,
672
                'fields' => $spiFields,
673
                'alwaysAvailable' => $contentCreateStruct->alwaysAvailable,
674
                'remoteId' => $contentCreateStruct->remoteId,
675
                'modified' => isset($contentCreateStruct->modificationDate) ? $contentCreateStruct->modificationDate->getTimestamp() : time(),
676
                'initialLanguageId' => $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode(
677
                    $contentCreateStruct->mainLanguageCode
678
                )->id,
679
            )
680
        );
681
682
        $defaultObjectStates = $this->getDefaultObjectStates();
683
684
        $this->repository->beginTransaction();
685
        try {
686
            $spiContent = $this->persistenceHandler->contentHandler()->create($spiContentCreateStruct);
687
            $this->relationProcessor->processFieldRelations(
688
                $inputRelations,
689
                $spiContent->versionInfo->contentInfo->id,
690
                $spiContent->versionInfo->versionNo,
691
                $contentCreateStruct->contentType
692
            );
693
694
            foreach ($defaultObjectStates as $objectStateGroupId => $objectState) {
695
                $this->persistenceHandler->objectStateHandler()->setContentState(
696
                    $spiContent->versionInfo->contentInfo->id,
697
                    $objectStateGroupId,
698
                    $objectState->id
699
                );
700
            }
701
702
            $this->repository->commit();
703
        } catch (Exception $e) {
704
            $this->repository->rollback();
705
            throw $e;
706
        }
707
708
        return $this->domainMapper->buildContentDomainObject($spiContent);
709
    }
710
711
    /**
712
     * Returns an array of default content states with content state group id as key.
713
     *
714
     * @return \eZ\Publish\SPI\Persistence\Content\ObjectState[]
715
     */
716
    protected function getDefaultObjectStates()
717
    {
718
        $defaultObjectStatesMap = array();
719
        $objectStateHandler = $this->persistenceHandler->objectStateHandler();
720
721
        foreach ($objectStateHandler->loadAllGroups() as $objectStateGroup) {
722
            foreach ($objectStateHandler->loadObjectStates($objectStateGroup->id) as $objectState) {
723
                // Only register the first object state which is the default one.
724
                $defaultObjectStatesMap[$objectStateGroup->id] = $objectState;
725
                break;
726
            }
727
        }
728
729
        return $defaultObjectStatesMap;
730
    }
731
732
    /**
733
     * Returns all language codes used in given $fields.
734
     *
735
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if no field value is set in main language
736
     *
737
     * @param \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct $contentCreateStruct
738
     *
739
     * @return string[]
740
     */
741
    protected function getLanguageCodesForCreate(APIContentCreateStruct $contentCreateStruct)
742
    {
743
        $languageCodes = array();
744
745
        foreach ($contentCreateStruct->fields as $field) {
746
            if ($field->languageCode === null || isset($languageCodes[$field->languageCode])) {
747
                continue;
748
            }
749
750
            $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode(
751
                $field->languageCode
752
            );
753
            $languageCodes[$field->languageCode] = true;
754
        }
755
756
        if (!isset($languageCodes[$contentCreateStruct->mainLanguageCode])) {
757
            $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode(
758
                $contentCreateStruct->mainLanguageCode
759
            );
760
            $languageCodes[$contentCreateStruct->mainLanguageCode] = true;
761
        }
762
763
        return array_keys($languageCodes);
764
    }
765
766
    /**
767
     * Returns an array of fields like $fields[$field->fieldDefIdentifier][$field->languageCode].
768
     *
769
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType
770
     *                                                                          or value is set for non-translatable field in language
771
     *                                                                          other than main
772
     *
773
     * @param \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct $contentCreateStruct
774
     *
775
     * @return array
776
     */
777
    protected function mapFieldsForCreate(APIContentCreateStruct $contentCreateStruct)
778
    {
779
        $fields = array();
780
781
        foreach ($contentCreateStruct->fields as $field) {
782
            $fieldDefinition = $contentCreateStruct->contentType->getFieldDefinition($field->fieldDefIdentifier);
783
784
            if ($fieldDefinition === null) {
785
                throw new ContentValidationException(
786
                    "Field definition '%identifier%' does not exist in given ContentType",
787
                    ['%identifier%' => $field->fieldDefIdentifier]
788
                );
789
            }
790
791
            if ($field->languageCode === null) {
792
                $field = $this->cloneField(
793
                    $field,
794
                    array('languageCode' => $contentCreateStruct->mainLanguageCode)
795
                );
796
            }
797
798 View Code Duplication
            if (!$fieldDefinition->isTranslatable && ($field->languageCode != $contentCreateStruct->mainLanguageCode)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
799
                throw new ContentValidationException(
800
                    "A value is set for non translatable field definition '%identifier%' with language '%languageCode%'",
801
                    ['%identifier%' => $field->fieldDefIdentifier, '%languageCode%' => $field->languageCode]
802
                );
803
            }
804
805
            $fields[$field->fieldDefIdentifier][$field->languageCode] = $field;
806
        }
807
808
        return $fields;
809
    }
810
811
    /**
812
     * Clones $field with overriding specific properties from given $overrides array.
813
     *
814
     * @param Field $field
815
     * @param array $overrides
816
     *
817
     * @return Field
818
     */
819
    private function cloneField(Field $field, array $overrides = [])
820
    {
821
        $fieldData = array_merge(
822
            [
823
                'id' => $field->id,
824
                'value' => $field->value,
825
                'languageCode' => $field->languageCode,
826
                'fieldDefIdentifier' => $field->fieldDefIdentifier,
827
                'fieldTypeIdentifier' => $field->fieldTypeIdentifier,
828
            ],
829
            $overrides
830
        );
831
832
        return new Field($fieldData);
833
    }
834
835
    /**
836
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
837
     *
838
     * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct[] $locationCreateStructs
839
     *
840
     * @return \eZ\Publish\SPI\Persistence\Content\Location\CreateStruct[]
841
     */
842
    protected function buildSPILocationCreateStructs(array $locationCreateStructs)
843
    {
844
        $spiLocationCreateStructs = array();
845
        $parentLocationIdSet = array();
846
        $mainLocation = true;
847
848
        foreach ($locationCreateStructs as $locationCreateStruct) {
849
            if (isset($parentLocationIdSet[$locationCreateStruct->parentLocationId])) {
850
                throw new InvalidArgumentException(
851
                    '$locationCreateStructs',
852
                    "Multiple LocationCreateStructs with the same parent Location '{$locationCreateStruct->parentLocationId}' are given"
853
                );
854
            }
855
856
            $parentLocationIdSet[$locationCreateStruct->parentLocationId] = true;
857
            $parentLocation = $this->repository->getLocationService()->loadLocation(
858
                $locationCreateStruct->parentLocationId
859
            );
860
861
            $spiLocationCreateStructs[] = $this->domainMapper->buildSPILocationCreateStruct(
862
                $locationCreateStruct,
863
                $parentLocation,
864
                $mainLocation,
865
                // For Content draft contentId and contentVersionNo are set in ContentHandler upon draft creation
866
                null,
867
                null
868
            );
869
870
            // First Location in the list will be created as main Location
871
            $mainLocation = false;
872
        }
873
874
        return $spiLocationCreateStructs;
875
    }
876
877
    /**
878
     * Updates the metadata.
879
     *
880
     * (see {@link ContentMetadataUpdateStruct}) of a content object - to update fields use updateContent
881
     *
882
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update the content meta data
883
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the remoteId in $contentMetadataUpdateStruct is set but already exists
884
     *
885
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
886
     * @param \eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct $contentMetadataUpdateStruct
887
     *
888
     * @return \eZ\Publish\API\Repository\Values\Content\Content the content with the updated attributes
889
     */
890
    public function updateContentMetadata(ContentInfo $contentInfo, ContentMetadataUpdateStruct $contentMetadataUpdateStruct)
891
    {
892
        $propertyCount = 0;
893
        foreach ($contentMetadataUpdateStruct as $propertyName => $propertyValue) {
0 ignored issues
show
Bug introduced by
The expression $contentMetadataUpdateStruct of type object<eZ\Publish\API\Re...ntMetadataUpdateStruct> is not traversable.
Loading history...
894
            if (isset($contentMetadataUpdateStruct->$propertyName)) {
895
                $propertyCount += 1;
896
            }
897
        }
898
        if ($propertyCount === 0) {
899
            throw new InvalidArgumentException(
900
                '$contentMetadataUpdateStruct',
901
                'At least one property must be set'
902
            );
903
        }
904
905
        $loadedContentInfo = $this->loadContentInfo($contentInfo->id);
906
907
        if (!$this->repository->canUser('content', 'edit', $loadedContentInfo)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
908
            throw new UnauthorizedException('content', 'edit', array('contentId' => $loadedContentInfo->id));
909
        }
910
911
        if (isset($contentMetadataUpdateStruct->remoteId)) {
912
            try {
913
                $existingContentInfo = $this->loadContentInfoByRemoteId($contentMetadataUpdateStruct->remoteId);
914
915
                if ($existingContentInfo->id !== $loadedContentInfo->id) {
916
                    throw new InvalidArgumentException(
917
                        '$contentMetadataUpdateStruct',
918
                        "Another content with remoteId '{$contentMetadataUpdateStruct->remoteId}' exists"
919
                    );
920
                }
921
            } catch (APINotFoundException $e) {
922
                // Do nothing
923
            }
924
        }
925
926
        $this->repository->beginTransaction();
927
        try {
928
            if ($propertyCount > 1 || !isset($contentMetadataUpdateStruct->mainLocationId)) {
929
                $this->persistenceHandler->contentHandler()->updateMetadata(
930
                    $loadedContentInfo->id,
931
                    new SPIMetadataUpdateStruct(
932
                        array(
933
                            'ownerId' => $contentMetadataUpdateStruct->ownerId,
934
                            'publicationDate' => isset($contentMetadataUpdateStruct->publishedDate) ?
935
                                $contentMetadataUpdateStruct->publishedDate->getTimestamp() :
936
                                null,
937
                            'modificationDate' => isset($contentMetadataUpdateStruct->modificationDate) ?
938
                                $contentMetadataUpdateStruct->modificationDate->getTimestamp() :
939
                                null,
940
                            'mainLanguageId' => isset($contentMetadataUpdateStruct->mainLanguageCode) ?
941
                                $this->repository->getContentLanguageService()->loadLanguage(
942
                                    $contentMetadataUpdateStruct->mainLanguageCode
943
                                )->id :
944
                                null,
945
                            'alwaysAvailable' => $contentMetadataUpdateStruct->alwaysAvailable,
946
                            'remoteId' => $contentMetadataUpdateStruct->remoteId,
947
                        )
948
                    )
949
                );
950
            }
951
952
            // Change main location
953
            if (isset($contentMetadataUpdateStruct->mainLocationId)
954
                && $loadedContentInfo->mainLocationId !== $contentMetadataUpdateStruct->mainLocationId) {
955
                $this->persistenceHandler->locationHandler()->changeMainLocation(
956
                    $loadedContentInfo->id,
957
                    $contentMetadataUpdateStruct->mainLocationId
958
                );
959
            }
960
961
            // Republish URL aliases to update always-available flag
962
            if (isset($contentMetadataUpdateStruct->alwaysAvailable)
963
                && $loadedContentInfo->alwaysAvailable !== $contentMetadataUpdateStruct->alwaysAvailable) {
964
                $content = $this->loadContent($loadedContentInfo->id);
965
                $this->publishUrlAliasesForContent($content, false);
966
            }
967
968
            $this->repository->commit();
969
        } catch (Exception $e) {
970
            $this->repository->rollback();
971
            throw $e;
972
        }
973
974
        return isset($content) ? $content : $this->loadContent($loadedContentInfo->id);
975
    }
976
977
    /**
978
     * Publishes URL aliases for all locations of a given content.
979
     *
980
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
981
     * @param bool $updatePathIdentificationString this parameter is legacy storage specific for updating
982
     *                      ezcontentobject_tree.path_identification_string, it is ignored by other storage engines
983
     */
984
    protected function publishUrlAliasesForContent(APIContent $content, $updatePathIdentificationString = true)
985
    {
986
        $urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema($content);
987
        $locations = $this->repository->getLocationService()->loadLocations(
988
            $content->getVersionInfo()->getContentInfo()
989
        );
990
        foreach ($locations as $location) {
991 View Code Duplication
            foreach ($urlAliasNames as $languageCode => $name) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
992
                $this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation(
993
                    $location->id,
994
                    $location->parentLocationId,
995
                    $name,
996
                    $languageCode,
997
                    $content->contentInfo->alwaysAvailable,
998
                    $updatePathIdentificationString ? $languageCode === $content->contentInfo->mainLanguageCode : false
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $languageCode (integer) and $content->contentInfo->mainLanguageCode (string) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
999
                );
1000
            }
1001
            // archive URL aliases of Translations that got deleted
1002
            $this->persistenceHandler->urlAliasHandler()->archiveUrlAliasesForDeletedTranslations(
1003
                $location->id,
1004
                $location->parentLocationId,
1005
                $content->versionInfo->languageCodes
1006
            );
1007
        }
1008
    }
1009
1010
    /**
1011
     * Deletes a content object including all its versions and locations including their subtrees.
1012
     *
1013
     * @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)
1014
     *
1015
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
1016
     *
1017
     * @return mixed[] Affected Location Id's
1018
     */
1019
    public function deleteContent(ContentInfo $contentInfo)
1020
    {
1021
        $contentInfo = $this->internalLoadContentInfo($contentInfo->id);
1022
1023
        if (!$this->repository->canUser('content', 'remove', $contentInfo)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1024
            throw new UnauthorizedException('content', 'remove', array('contentId' => $contentInfo->id));
1025
        }
1026
1027
        $affectedLocations = [];
1028
        $this->repository->beginTransaction();
1029
        try {
1030
            // Load Locations first as deleting Content also deletes belonging Locations
1031
            $spiLocations = $this->persistenceHandler->locationHandler()->loadLocationsByContent($contentInfo->id);
1032
            $this->persistenceHandler->contentHandler()->deleteContent($contentInfo->id);
1033
            foreach ($spiLocations as $spiLocation) {
1034
                $this->persistenceHandler->urlAliasHandler()->locationDeleted($spiLocation->id);
1035
                $affectedLocations[] = $spiLocation->id;
1036
            }
1037
            $this->repository->commit();
1038
        } catch (Exception $e) {
1039
            $this->repository->rollback();
1040
            throw $e;
1041
        }
1042
1043
        return $affectedLocations;
1044
    }
1045
1046
    /**
1047
     * Creates a draft from a published or archived version.
1048
     *
1049
     * If no version is given, the current published version is used.
1050
     * 4.x: The draft is created with the initialLanguage code of the source version or if not present with the main language.
1051
     * It can be changed on updating the version.
1052
     *
1053
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the current-user is not allowed to create the draft
1054
     *
1055
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
1056
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
1057
     * @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
1058
     *
1059
     * @return \eZ\Publish\API\Repository\Values\Content\Content - the newly created content draft
1060
     */
1061
    public function createContentDraft(ContentInfo $contentInfo, APIVersionInfo $versionInfo = null, User $creator = null)
1062
    {
1063
        $contentInfo = $this->loadContentInfo($contentInfo->id);
1064
1065
        if ($versionInfo !== null) {
1066
            // Check that given $contentInfo and $versionInfo belong to the same content
1067
            if ($versionInfo->getContentInfo()->id != $contentInfo->id) {
1068
                throw new InvalidArgumentException(
1069
                    '$versionInfo',
1070
                    'VersionInfo does not belong to the same content as given ContentInfo'
1071
                );
1072
            }
1073
1074
            $versionInfo = $this->loadVersionInfoById($contentInfo->id, $versionInfo->versionNo);
1075
1076
            switch ($versionInfo->status) {
1077
                case VersionInfo::STATUS_PUBLISHED:
1078
                case VersionInfo::STATUS_ARCHIVED:
1079
                    break;
1080
1081
                default:
1082
                    // @todo: throw an exception here, to be defined
1083
                    throw new BadStateException(
1084
                        '$versionInfo',
1085
                        'Draft can not be created from a draft version'
1086
                    );
1087
            }
1088
1089
            $versionNo = $versionInfo->versionNo;
1090
        } elseif ($contentInfo->published) {
1091
            $versionNo = $contentInfo->currentVersionNo;
1092
        } else {
1093
            // @todo: throw an exception here, to be defined
1094
            throw new BadStateException(
1095
                '$contentInfo',
1096
                'Content is not published, draft can be created only from published or archived version'
1097
            );
1098
        }
1099
1100
        if ($creator === null) {
1101
            $creator = $this->repository->getCurrentUserReference();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...tCurrentUserReference() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user reference.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1102
        }
1103
1104
        if (!$this->repository->canUser('content', 'edit', $contentInfo)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1105
            throw new UnauthorizedException('content', 'edit', array('contentId' => $contentInfo->id));
1106
        }
1107
1108
        $this->repository->beginTransaction();
1109
        try {
1110
            $spiContent = $this->persistenceHandler->contentHandler()->createDraftFromVersion(
1111
                $contentInfo->id,
1112
                $versionNo,
1113
                $creator->getUserId()
1114
            );
1115
            $this->repository->commit();
1116
        } catch (Exception $e) {
1117
            $this->repository->rollback();
1118
            throw $e;
1119
        }
1120
1121
        return $this->domainMapper->buildContentDomainObject($spiContent);
1122
    }
1123
1124
    /**
1125
     * Loads drafts for a user.
1126
     *
1127
     * If no user is given the drafts for the authenticated user a returned
1128
     *
1129
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the current-user is not allowed to load the draft list
1130
     *
1131
     * @param \eZ\Publish\API\Repository\Values\User\UserReference $user
1132
     *
1133
     * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo the drafts ({@link VersionInfo}) owned by the given user
1134
     */
1135
    public function loadContentDrafts(User $user = null)
1136
    {
1137
        if ($user === null) {
1138
            $user = $this->repository->getCurrentUserReference();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...tCurrentUserReference() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user reference.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1139
        }
1140
1141
        // throw early if user has absolutely no access to versionread
1142
        if ($this->repository->hasAccess('content', 'versionread') === false) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::hasAccess() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::hasAccess() instead. Check if user has access to a given module / function. Low level function, use canUser instead if you have objects to check against.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1143
            throw new UnauthorizedException('content', 'versionread');
1144
        }
1145
1146
        $spiVersionInfoList = $this->persistenceHandler->contentHandler()->loadDraftsForUser($user->getUserId());
1147
        $versionInfoList = array();
1148 View Code Duplication
        foreach ($spiVersionInfoList as $spiVersionInfo) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1149
            $versionInfo = $this->domainMapper->buildVersionInfoDomainObject($spiVersionInfo);
1150
            // @todo: Change this to filter returned drafts by permissions instead of throwing
1151
            if (!$this->repository->canUser('content', 'versionread', $versionInfo)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1152
                throw new UnauthorizedException('content', 'versionread', array('contentId' => $versionInfo->contentInfo->id));
1153
            }
1154
1155
            $versionInfoList[] = $versionInfo;
1156
        }
1157
1158
        return $versionInfoList;
1159
    }
1160
1161
    /**
1162
     * Translate a version.
1163
     *
1164
     * updates the destination version given in $translationInfo with the provided translated fields in $translationValues
1165
     *
1166
     * @example Examples/translation_5x.php
1167
     *
1168
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the current-user is not allowed to update this version
1169
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the given destination version is not a draft
1170
     * @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.
1171
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType
1172
     *                                                                          or value is set for non-translatable field in language
1173
     *                                                                          other than main.
1174
     *
1175
     * @param \eZ\Publish\API\Repository\Values\Content\TranslationInfo $translationInfo
1176
     * @param \eZ\Publish\API\Repository\Values\Content\TranslationValues $translationValues
1177
     * @param \eZ\Publish\API\Repository\Values\User\User $modifier If set, this user is taken as modifier of the version
1178
     *
1179
     * @return \eZ\Publish\API\Repository\Values\Content\Content the content draft with the translated fields
1180
     *
1181
     * @since 5.0
1182
     */
1183
    public function translateVersion(TranslationInfo $translationInfo, APITranslationValues $translationValues, User $modifier = null)
1184
    {
1185
        throw new NotImplementedException(__METHOD__);
1186
    }
1187
1188
    /**
1189
     * Updates the fields of a draft.
1190
     *
1191
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update this version
1192
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft
1193
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if a property on the struct is invalid.
1194
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $contentCreateStruct is not valid,
1195
     *                                                                               or if a required field is missing / set to an empty value.
1196
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType,
1197
     *                                                                          or value is set for non-translatable field in language
1198
     *                                                                          other than main.
1199
     *
1200
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
1201
     * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct
1202
     *
1203
     * @return \eZ\Publish\API\Repository\Values\Content\Content the content draft with the updated fields
1204
     */
1205
    public function updateContent(APIVersionInfo $versionInfo, APIContentUpdateStruct $contentUpdateStruct)
1206
    {
1207
        $contentUpdateStruct = clone $contentUpdateStruct;
1208
1209
        /** @var $content \eZ\Publish\Core\Repository\Values\Content\Content */
1210
        $content = $this->loadContent(
1211
            $versionInfo->getContentInfo()->id,
1212
            null,
1213
            $versionInfo->versionNo
1214
        );
1215
        if (!$content->versionInfo->isDraft()) {
1216
            throw new BadStateException(
1217
                '$versionInfo',
1218
                'Version is not a draft and can not be updated'
1219
            );
1220
        }
1221
1222
        if (!$this->repository->canUser('content', 'edit', $content)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1223
            throw new UnauthorizedException('content', 'edit', array('contentId' => $content->id));
1224
        }
1225
1226
        $mainLanguageCode = $content->contentInfo->mainLanguageCode;
1227
        if ($contentUpdateStruct->initialLanguageCode === null) {
1228
            $contentUpdateStruct->initialLanguageCode = $mainLanguageCode;
1229
        }
1230
1231
        $allLanguageCodes = $this->getLanguageCodesForUpdate($contentUpdateStruct, $content);
1232
        foreach ($allLanguageCodes as $languageCode) {
1233
            $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode($languageCode);
1234
        }
1235
1236
        $updatedLanguageCodes = $this->getUpdatedLanguageCodes($contentUpdateStruct);
1237
        $contentType = $this->repository->getContentTypeService()->loadContentType(
1238
            $content->contentInfo->contentTypeId
1239
        );
1240
        $fields = $this->mapFieldsForUpdate(
1241
            $contentUpdateStruct,
1242
            $contentType,
1243
            $mainLanguageCode
1244
        );
1245
1246
        $fieldValues = array();
1247
        $spiFields = array();
1248
        $allFieldErrors = array();
1249
        $inputRelations = array();
1250
        $locationIdToContentIdMapping = array();
1251
1252
        foreach ($contentType->getFieldDefinitions() as $fieldDefinition) {
1253
            /** @var $fieldType \eZ\Publish\SPI\FieldType\FieldType */
1254
            $fieldType = $this->fieldTypeRegistry->getFieldType(
1255
                $fieldDefinition->fieldTypeIdentifier
1256
            );
1257
1258
            foreach ($allLanguageCodes as $languageCode) {
1259
                $isCopied = $isEmpty = $isRetained = false;
1260
                $isLanguageNew = !in_array($languageCode, $content->versionInfo->languageCodes);
1261
                $isLanguageUpdated = in_array($languageCode, $updatedLanguageCodes);
1262
                $valueLanguageCode = $fieldDefinition->isTranslatable ? $languageCode : $mainLanguageCode;
1263
                $isFieldUpdated = isset($fields[$fieldDefinition->identifier][$valueLanguageCode]);
1264
                $isProcessed = isset($fieldValues[$fieldDefinition->identifier][$valueLanguageCode]);
1265
1266
                if (!$isFieldUpdated && !$isLanguageNew) {
1267
                    $isRetained = true;
1268
                    $fieldValue = $content->getField($fieldDefinition->identifier, $valueLanguageCode)->value;
1269
                } elseif (!$isFieldUpdated && $isLanguageNew && !$fieldDefinition->isTranslatable) {
1270
                    $isCopied = true;
1271
                    $fieldValue = $content->getField($fieldDefinition->identifier, $valueLanguageCode)->value;
1272
                } elseif ($isFieldUpdated) {
1273
                    $fieldValue = $fields[$fieldDefinition->identifier][$valueLanguageCode]->value;
1274
                } else {
1275
                    $fieldValue = $fieldDefinition->defaultValue;
1276
                }
1277
1278
                $fieldValue = $fieldType->acceptValue($fieldValue);
1279
1280 View Code Duplication
                if ($fieldType->isEmptyValue($fieldValue)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1281
                    $isEmpty = true;
1282
                    if ($isLanguageUpdated && $fieldDefinition->isRequired) {
1283
                        $allFieldErrors[$fieldDefinition->id][$languageCode] = new ValidationError(
1284
                            "Value for required field definition '%identifier%' with language '%languageCode%' is empty",
1285
                            null,
1286
                            ['%identifier%' => $fieldDefinition->identifier, '%languageCode%' => $languageCode],
1287
                            'empty'
1288
                        );
1289
                    }
1290
                } elseif ($isLanguageUpdated) {
1291
                    $fieldErrors = $fieldType->validate(
1292
                        $fieldDefinition,
1293
                        $fieldValue
1294
                    );
1295
                    if (!empty($fieldErrors)) {
1296
                        $allFieldErrors[$fieldDefinition->id][$languageCode] = $fieldErrors;
1297
                    }
1298
                }
1299
1300
                if (!empty($allFieldErrors)) {
1301
                    continue;
1302
                }
1303
1304
                $this->relationProcessor->appendFieldRelations(
1305
                    $inputRelations,
1306
                    $locationIdToContentIdMapping,
1307
                    $fieldType,
1308
                    $fieldValue,
0 ignored issues
show
Compatibility introduced by
$fieldValue of type object<eZ\Publish\SPI\FieldType\Value> is not a sub-type of object<eZ\Publish\Core\FieldType\Value>. It seems like you assume a concrete implementation of the interface eZ\Publish\SPI\FieldType\Value to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
1309
                    $fieldDefinition->id
1310
                );
1311
                $fieldValues[$fieldDefinition->identifier][$languageCode] = $fieldValue;
1312
1313
                if ($isRetained || $isCopied || ($isLanguageNew && $isEmpty) || $isProcessed) {
1314
                    continue;
1315
                }
1316
1317
                $spiFields[] = new SPIField(
1318
                    array(
1319
                        'id' => $isLanguageNew ?
1320
                            null :
1321
                            $content->getField($fieldDefinition->identifier, $languageCode)->id,
1322
                        'fieldDefinitionId' => $fieldDefinition->id,
1323
                        'type' => $fieldDefinition->fieldTypeIdentifier,
1324
                        'value' => $fieldType->toPersistenceValue($fieldValue),
1325
                        'languageCode' => $languageCode,
1326
                        'versionNo' => $versionInfo->versionNo,
1327
                    )
1328
                );
1329
            }
1330
        }
1331
1332
        if (!empty($allFieldErrors)) {
1333
            throw new ContentFieldValidationException($allFieldErrors);
1334
        }
1335
1336
        $spiContentUpdateStruct = new SPIContentUpdateStruct(
1337
            array(
1338
                'name' => $this->nameSchemaService->resolveNameSchema(
1339
                    $content,
1340
                    $fieldValues,
1341
                    $allLanguageCodes,
1342
                    $contentType
1343
                ),
1344
                'creatorId' => $contentUpdateStruct->creatorId ?: $this->repository->getCurrentUserReference()->getUserId(),
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...tCurrentUserReference() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user reference.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1345
                'fields' => $spiFields,
1346
                'modificationDate' => time(),
1347
                'initialLanguageId' => $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode(
1348
                    $contentUpdateStruct->initialLanguageCode
1349
                )->id,
1350
            )
1351
        );
1352
        $existingRelations = $this->loadRelations($versionInfo);
1353
1354
        $this->repository->beginTransaction();
1355
        try {
1356
            $spiContent = $this->persistenceHandler->contentHandler()->updateContent(
1357
                $versionInfo->getContentInfo()->id,
1358
                $versionInfo->versionNo,
1359
                $spiContentUpdateStruct
1360
            );
1361
            $this->relationProcessor->processFieldRelations(
1362
                $inputRelations,
1363
                $spiContent->versionInfo->contentInfo->id,
1364
                $spiContent->versionInfo->versionNo,
1365
                $contentType,
1366
                $existingRelations
1367
            );
1368
            $this->repository->commit();
1369
        } catch (Exception $e) {
1370
            $this->repository->rollback();
1371
            throw $e;
1372
        }
1373
1374
        return $this->domainMapper->buildContentDomainObject(
1375
            $spiContent,
1376
            $contentType
1377
        );
1378
    }
1379
1380
    /**
1381
     * Returns only updated language codes.
1382
     *
1383
     * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct
1384
     *
1385
     * @return array
1386
     */
1387 View Code Duplication
    private function getUpdatedLanguageCodes(APIContentUpdateStruct $contentUpdateStruct)
1388
    {
1389
        $languageCodes = [
1390
            $contentUpdateStruct->initialLanguageCode => true,
1391
        ];
1392
1393
        foreach ($contentUpdateStruct->fields as $field) {
1394
            if ($field->languageCode === null || isset($languageCodes[$field->languageCode])) {
1395
                continue;
1396
            }
1397
1398
            $languageCodes[$field->languageCode] = true;
1399
        }
1400
1401
        return array_keys($languageCodes);
1402
    }
1403
1404
    /**
1405
     * Returns all language codes used in given $fields.
1406
     *
1407
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if no field value exists in initial language
1408
     *
1409
     * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct
1410
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1411
     *
1412
     * @return array
1413
     */
1414
    protected function getLanguageCodesForUpdate(APIContentUpdateStruct $contentUpdateStruct, APIContent $content)
1415
    {
1416
        $languageCodes = array_fill_keys($content->versionInfo->languageCodes, true);
1417
        $languageCodes[$contentUpdateStruct->initialLanguageCode] = true;
1418
1419
        $updatedLanguageCodes = $this->getUpdatedLanguageCodes($contentUpdateStruct);
1420
        foreach ($updatedLanguageCodes as $languageCode) {
1421
            $languageCodes[$languageCode] = true;
1422
        }
1423
1424
        return array_keys($languageCodes);
1425
    }
1426
1427
    /**
1428
     * Returns an array of fields like $fields[$field->fieldDefIdentifier][$field->languageCode].
1429
     *
1430
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType
1431
     *                                                                          or value is set for non-translatable field in language
1432
     *                                                                          other than main
1433
     *
1434
     * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct
1435
     * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType
1436
     * @param string $mainLanguageCode
1437
     *
1438
     * @return array
1439
     */
1440
    protected function mapFieldsForUpdate(
1441
        APIContentUpdateStruct $contentUpdateStruct,
1442
        ContentType $contentType,
1443
        $mainLanguageCode
1444
    ) {
1445
        $fields = array();
1446
1447
        foreach ($contentUpdateStruct->fields as $field) {
1448
            $fieldDefinition = $contentType->getFieldDefinition($field->fieldDefIdentifier);
1449
1450
            if ($fieldDefinition === null) {
1451
                throw new ContentValidationException(
1452
                    "Field definition '%identifier%' does not exist in given ContentType",
1453
                    ['%identifier%' => $field->fieldDefIdentifier]
1454
                );
1455
            }
1456
1457
            if ($field->languageCode === null) {
1458
                if ($fieldDefinition->isTranslatable) {
1459
                    $languageCode = $contentUpdateStruct->initialLanguageCode;
1460
                } else {
1461
                    $languageCode = $mainLanguageCode;
1462
                }
1463
                $field = $this->cloneField($field, array('languageCode' => $languageCode));
1464
            }
1465
1466 View Code Duplication
            if (!$fieldDefinition->isTranslatable && ($field->languageCode != $mainLanguageCode)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1467
                throw new ContentValidationException(
1468
                    "A value is set for non translatable field definition '%identifier%' with language '%languageCode%'",
1469
                    ['%identifier%' => $field->fieldDefIdentifier, '%languageCode%' => $field->languageCode]
1470
                );
1471
            }
1472
1473
            $fields[$field->fieldDefIdentifier][$field->languageCode] = $field;
1474
        }
1475
1476
        return $fields;
1477
    }
1478
1479
    /**
1480
     * Publishes a content version.
1481
     *
1482
     * Publishes a content version and deletes archive versions if they overflow max archive versions.
1483
     * Max archive versions are currently a configuration, but might be moved to be a param of ContentType in the future.
1484
     *
1485
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to publish this version
1486
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft
1487
     *
1488
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
1489
     *
1490
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1491
     */
1492
    public function publishVersion(APIVersionInfo $versionInfo)
1493
    {
1494
        $content = $this->internalLoadContent(
1495
            $versionInfo->contentInfo->id,
1496
            null,
1497
            $versionInfo->versionNo
1498
        );
1499
1500
        if (!$this->repository->canUser('content', 'publish', $content)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1501
            throw new UnauthorizedException('content', 'publish', array('contentId' => $content->id));
1502
        }
1503
1504
        $this->repository->beginTransaction();
1505
        try {
1506
            $content = $this->internalPublishVersion($content->getVersionInfo());
1507
            $this->repository->commit();
1508
        } catch (Exception $e) {
1509
            $this->repository->rollback();
1510
            throw $e;
1511
        }
1512
1513
        return $content;
1514
    }
1515
1516
    /**
1517
     * Publishes a content version.
1518
     *
1519
     * Publishes a content version and deletes archive versions if they overflow max archive versions.
1520
     * Max archive versions are currently a configuration, but might be moved to be a param of ContentType in the future.
1521
     *
1522
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft
1523
     *
1524
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
1525
     * @param int|null $publicationDate If null existing date is kept if there is one, otherwise current time is used.
1526
     *
1527
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1528
     */
1529
    protected function internalPublishVersion(APIVersionInfo $versionInfo, $publicationDate = null)
1530
    {
1531
        if (!$versionInfo->isDraft()) {
1532
            throw new BadStateException('$versionInfo', 'Only versions in draft status can be published.');
1533
        }
1534
1535
        $currentTime = time();
1536
        if ($publicationDate === null && $versionInfo->versionNo === 1) {
1537
            $publicationDate = $currentTime;
1538
        }
1539
1540
        $metadataUpdateStruct = new SPIMetadataUpdateStruct();
1541
        $metadataUpdateStruct->publicationDate = $publicationDate;
1542
        $metadataUpdateStruct->modificationDate = $currentTime;
1543
1544
        $contentId = $versionInfo->getContentInfo()->id;
1545
        $spiContent = $this->persistenceHandler->contentHandler()->publish(
1546
            $contentId,
1547
            $versionInfo->versionNo,
1548
            $metadataUpdateStruct
1549
        );
1550
1551
        $content = $this->domainMapper->buildContentDomainObject($spiContent);
1552
1553
        $this->publishUrlAliasesForContent($content);
1554
1555
        // Delete version archive overflow if any, limit is 0-50 (however 0 will mean 1 if content is unpublished)
1556
        $archiveList = $this->persistenceHandler->contentHandler()->listVersions(
1557
            $contentId,
1558
            APIVersionInfo::STATUS_ARCHIVED,
1559
            100 // Limited to avoid publishing taking to long, besides SE limitations this is why limit is max 50
1560
        );
1561
1562
        $maxVersionArchiveCount = max(0, min(50, $this->settings['default_version_archive_limit']));
1563
        while (!empty($archiveList) && count($archiveList) > $maxVersionArchiveCount) {
1564
            /** @var \eZ\Publish\SPI\Persistence\Content\VersionInfo $archiveVersion */
1565
            $archiveVersion = array_shift($archiveList);
1566
            $this->persistenceHandler->contentHandler()->deleteVersion(
1567
                $contentId,
1568
                $archiveVersion->versionNo
1569
            );
1570
        }
1571
1572
        return $content;
1573
    }
1574
1575
    /**
1576
     * Removes the given version.
1577
     *
1578
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is in
1579
     *         published state or is the last version of the Content
1580
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to remove this version
1581
     *
1582
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
1583
     */
1584
    public function deleteVersion(APIVersionInfo $versionInfo)
1585
    {
1586
        if ($versionInfo->isPublished()) {
1587
            throw new BadStateException(
1588
                '$versionInfo',
1589
                'Version is published and can not be removed'
1590
            );
1591
        }
1592
1593 View Code Duplication
        if (!$this->repository->canUser('content', 'versionremove', $versionInfo)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1594
            throw new UnauthorizedException(
1595
                'content',
1596
                'versionremove',
1597
                array('contentId' => $versionInfo->contentInfo->id, 'versionNo' => $versionInfo->versionNo)
1598
            );
1599
        }
1600
1601
        $versionList = $this->persistenceHandler->contentHandler()->listVersions(
1602
            $versionInfo->contentInfo->id,
1603
            null,
1604
            2
1605
        );
1606
1607
        if (count($versionList) === 1) {
1608
            throw new BadStateException(
1609
                '$versionInfo',
1610
                'Version is the last version of the Content and can not be removed'
1611
            );
1612
        }
1613
1614
        $this->repository->beginTransaction();
1615
        try {
1616
            $this->persistenceHandler->contentHandler()->deleteVersion(
1617
                $versionInfo->getContentInfo()->id,
1618
                $versionInfo->versionNo
1619
            );
1620
            $this->repository->commit();
1621
        } catch (Exception $e) {
1622
            $this->repository->rollback();
1623
            throw $e;
1624
        }
1625
    }
1626
1627
    /**
1628
     * Loads all versions for the given content.
1629
     *
1630
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to list versions
1631
     *
1632
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
1633
     *
1634
     * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo[] Sorted by creation date
1635
     */
1636
    public function loadVersions(ContentInfo $contentInfo)
1637
    {
1638
        if (!$this->repository->canUser('content', 'versionread', $contentInfo)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1639
            throw new UnauthorizedException('content', 'versionread', array('contentId' => $contentInfo->id));
1640
        }
1641
1642
        $spiVersionInfoList = $this->persistenceHandler->contentHandler()->listVersions($contentInfo->id);
1643
1644
        $versions = array();
1645 View Code Duplication
        foreach ($spiVersionInfoList as $spiVersionInfo) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1646
            $versionInfo = $this->domainMapper->buildVersionInfoDomainObject($spiVersionInfo);
1647
            if (!$this->repository->canUser('content', 'versionread', $versionInfo)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1648
                throw new UnauthorizedException('content', 'versionread', array('versionId' => $versionInfo->id));
1649
            }
1650
1651
            $versions[] = $versionInfo;
1652
        }
1653
1654
        return $versions;
1655
    }
1656
1657
    /**
1658
     * Copies the content to a new location. If no version is given,
1659
     * all versions are copied, otherwise only the given version.
1660
     *
1661
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to copy the content to the given location
1662
     *
1663
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
1664
     * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $destinationLocationCreateStruct the target location where the content is copied to
1665
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
1666
     *
1667
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1668
     */
1669
    public function copyContent(ContentInfo $contentInfo, LocationCreateStruct $destinationLocationCreateStruct, APIVersionInfo $versionInfo = null)
1670
    {
1671
        $destinationLocation = $this->repository->getLocationService()->loadLocation(
1672
            $destinationLocationCreateStruct->parentLocationId
1673
        );
1674 View Code Duplication
        if (!$this->repository->canUser('content', 'create', $contentInfo, [$destinationLocation])) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1675
            throw new UnauthorizedException(
1676
                'content',
1677
                'create',
1678
                [
1679
                    'parentLocationId' => $destinationLocationCreateStruct->parentLocationId,
1680
                    'sectionId' => $contentInfo->sectionId,
1681
                ]
1682
            );
1683
        }
1684
1685
        $defaultObjectStates = $this->getDefaultObjectStates();
1686
1687
        $this->repository->beginTransaction();
1688
        try {
1689
            $spiContent = $this->persistenceHandler->contentHandler()->copy(
1690
                $contentInfo->id,
1691
                $versionInfo ? $versionInfo->versionNo : null
1692
            );
1693
1694
            foreach ($defaultObjectStates as $objectStateGroupId => $objectState) {
1695
                $this->persistenceHandler->objectStateHandler()->setContentState(
1696
                    $spiContent->versionInfo->contentInfo->id,
1697
                    $objectStateGroupId,
1698
                    $objectState->id
1699
                );
1700
            }
1701
1702
            $content = $this->internalPublishVersion(
1703
                $this->domainMapper->buildVersionInfoDomainObject($spiContent->versionInfo),
1704
                $spiContent->versionInfo->creationDate
1705
            );
1706
1707
            $this->repository->getLocationService()->createLocation(
1708
                $content->getVersionInfo()->getContentInfo(),
1709
                $destinationLocationCreateStruct
1710
            );
1711
            $this->repository->commit();
1712
        } catch (Exception $e) {
1713
            $this->repository->rollback();
1714
            throw $e;
1715
        }
1716
1717
        return $this->internalLoadContent($content->id);
1718
    }
1719
1720
    /**
1721
     * Loads all outgoing relations for the given version.
1722
     *
1723
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read this version
1724
     *
1725
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
1726
     *
1727
     * @return \eZ\Publish\API\Repository\Values\Content\Relation[]
1728
     */
1729
    public function loadRelations(APIVersionInfo $versionInfo)
1730
    {
1731
        if ($versionInfo->isPublished()) {
1732
            $function = 'read';
1733
        } else {
1734
            $function = 'versionread';
1735
        }
1736
1737
        if (!$this->repository->canUser('content', $function, $versionInfo)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1738
            throw new UnauthorizedException('content', $function);
1739
        }
1740
1741
        $contentInfo = $versionInfo->getContentInfo();
1742
        $spiRelations = $this->persistenceHandler->contentHandler()->loadRelations(
1743
            $contentInfo->id,
1744
            $versionInfo->versionNo
1745
        );
1746
1747
        /** @var $relations \eZ\Publish\API\Repository\Values\Content\Relation[] */
1748
        $relations = array();
1749 View Code Duplication
        foreach ($spiRelations as $spiRelation) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1750
            $destinationContentInfo = $this->internalLoadContentInfo($spiRelation->destinationContentId);
1751
            if (!$this->repository->canUser('content', 'read', $destinationContentInfo)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1752
                continue;
1753
            }
1754
1755
            $relations[] = $this->domainMapper->buildRelationDomainObject(
1756
                $spiRelation,
1757
                $contentInfo,
1758
                $destinationContentInfo
1759
            );
1760
        }
1761
1762
        return $relations;
1763
    }
1764
1765
    /**
1766
     * Loads all incoming relations for a content object.
1767
     *
1768
     * The relations come only from published versions of the source content objects
1769
     *
1770
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read this version
1771
     *
1772
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
1773
     *
1774
     * @return \eZ\Publish\API\Repository\Values\Content\Relation[]
1775
     */
1776
    public function loadReverseRelations(ContentInfo $contentInfo)
1777
    {
1778
        if (!$this->repository->canUser('content', 'reverserelatedlist', $contentInfo)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1779
            throw new UnauthorizedException('content', 'reverserelatedlist', array('contentId' => $contentInfo->id));
1780
        }
1781
1782
        $spiRelations = $this->persistenceHandler->contentHandler()->loadReverseRelations(
1783
            $contentInfo->id
1784
        );
1785
1786
        $returnArray = array();
1787 View Code Duplication
        foreach ($spiRelations as $spiRelation) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1788
            $sourceContentInfo = $this->internalLoadContentInfo($spiRelation->sourceContentId);
1789
            if (!$this->repository->canUser('content', 'read', $sourceContentInfo)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1790
                continue;
1791
            }
1792
1793
            $returnArray[] = $this->domainMapper->buildRelationDomainObject(
1794
                $spiRelation,
1795
                $sourceContentInfo,
1796
                $contentInfo
1797
            );
1798
        }
1799
1800
        return $returnArray;
1801
    }
1802
1803
    /**
1804
     * Adds a relation of type common.
1805
     *
1806
     * The source of the relation is the content and version
1807
     * referenced by $versionInfo.
1808
     *
1809
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to edit this version
1810
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft
1811
     *
1812
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $sourceVersion
1813
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $destinationContent the destination of the relation
1814
     *
1815
     * @return \eZ\Publish\API\Repository\Values\Content\Relation the newly created relation
1816
     */
1817
    public function addRelation(APIVersionInfo $sourceVersion, ContentInfo $destinationContent)
1818
    {
1819
        $sourceVersion = $this->loadVersionInfoById(
1820
            $sourceVersion->contentInfo->id,
1821
            $sourceVersion->versionNo
1822
        );
1823
1824
        if (!$sourceVersion->isDraft()) {
1825
            throw new BadStateException(
1826
                '$sourceVersion',
1827
                'Relations of type common can only be added to versions of status draft'
1828
            );
1829
        }
1830
1831
        if (!$this->repository->canUser('content', 'edit', $sourceVersion)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1832
            throw new UnauthorizedException('content', 'edit', array('contentId' => $sourceVersion->contentInfo->id));
1833
        }
1834
1835
        $sourceContentInfo = $sourceVersion->getContentInfo();
1836
1837
        $this->repository->beginTransaction();
1838
        try {
1839
            $spiRelation = $this->persistenceHandler->contentHandler()->addRelation(
1840
                new SPIRelationCreateStruct(
1841
                    array(
1842
                        'sourceContentId' => $sourceContentInfo->id,
1843
                        'sourceContentVersionNo' => $sourceVersion->versionNo,
1844
                        'sourceFieldDefinitionId' => null,
1845
                        'destinationContentId' => $destinationContent->id,
1846
                        'type' => APIRelation::COMMON,
1847
                    )
1848
                )
1849
            );
1850
            $this->repository->commit();
1851
        } catch (Exception $e) {
1852
            $this->repository->rollback();
1853
            throw $e;
1854
        }
1855
1856
        return $this->domainMapper->buildRelationDomainObject($spiRelation, $sourceContentInfo, $destinationContent);
1857
    }
1858
1859
    /**
1860
     * Removes a relation of type COMMON from a draft.
1861
     *
1862
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed edit this version
1863
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft
1864
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if there is no relation of type COMMON for the given destination
1865
     *
1866
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $sourceVersion
1867
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $destinationContent
1868
     */
1869
    public function deleteRelation(APIVersionInfo $sourceVersion, ContentInfo $destinationContent)
1870
    {
1871
        $sourceVersion = $this->loadVersionInfoById(
1872
            $sourceVersion->contentInfo->id,
1873
            $sourceVersion->versionNo
1874
        );
1875
1876
        if (!$sourceVersion->isDraft()) {
1877
            throw new BadStateException(
1878
                '$sourceVersion',
1879
                'Relations of type common can only be removed from versions of status draft'
1880
            );
1881
        }
1882
1883
        if (!$this->repository->canUser('content', 'edit', $sourceVersion)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1884
            throw new UnauthorizedException('content', 'edit', array('contentId' => $sourceVersion->contentInfo->id));
1885
        }
1886
1887
        $spiRelations = $this->persistenceHandler->contentHandler()->loadRelations(
1888
            $sourceVersion->getContentInfo()->id,
1889
            $sourceVersion->versionNo,
1890
            APIRelation::COMMON
1891
        );
1892
1893
        if (empty($spiRelations)) {
1894
            throw new InvalidArgumentException(
1895
                '$sourceVersion',
1896
                'There are no relations of type COMMON for the given destination'
1897
            );
1898
        }
1899
1900
        // there should be only one relation of type COMMON for each destination,
1901
        // but in case there were ever more then one, we will remove them all
1902
        // @todo: alternatively, throw BadStateException?
1903
        $this->repository->beginTransaction();
1904
        try {
1905
            foreach ($spiRelations as $spiRelation) {
1906
                if ($spiRelation->destinationContentId == $destinationContent->id) {
1907
                    $this->persistenceHandler->contentHandler()->removeRelation(
1908
                        $spiRelation->id,
1909
                        APIRelation::COMMON
1910
                    );
1911
                }
1912
            }
1913
            $this->repository->commit();
1914
        } catch (Exception $e) {
1915
            $this->repository->rollback();
1916
            throw $e;
1917
        }
1918
    }
1919
1920
    /**
1921
     * Adds translation information to the content object.
1922
     *
1923
     * @example Examples/translation_5x.php
1924
     *
1925
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed add a translation info
1926
     *
1927
     * @param \eZ\Publish\API\Repository\Values\Content\TranslationInfo $translationInfo
1928
     *
1929
     * @since 5.0
1930
     */
1931
    public function addTranslationInfo(TranslationInfo $translationInfo)
1932
    {
1933
        throw new NotImplementedException(__METHOD__);
1934
    }
1935
1936
    /**
1937
     * lists the translations done on this content object.
1938
     *
1939
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed read translation infos
1940
     *
1941
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
1942
     * @param array $filter
1943
     *
1944
     * @todo TBD - filter by source version destination version and languages
1945
     *
1946
     * @return \eZ\Publish\API\Repository\Values\Content\TranslationInfo[]
1947
     *
1948
     * @since 5.0
1949
     */
1950
    public function loadTranslationInfos(ContentInfo $contentInfo, array $filter = array())
1951
    {
1952
        throw new NotImplementedException(__METHOD__);
1953
    }
1954
1955
    /**
1956
     * {@inheritdoc}
1957
     */
1958
    public function removeTranslation(ContentInfo $contentInfo, $languageCode)
1959
    {
1960
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
1961
            __METHOD__ . ' is deprecated, use deleteTranslation instead',
1962
            E_USER_DEPRECATED
1963
        );
1964
        $this->deleteTranslation($contentInfo, $languageCode);
1965
    }
1966
1967
    /**
1968
     * Delete Content item Translation from all Versions (including archived ones) of a Content Object.
1969
     *
1970
     * NOTE: this operation is risky and permanent, so user interface should provide a warning before performing it.
1971
     *
1972
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the specified Translation
1973
     *         is the Main Translation of a Content Item.
1974
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed
1975
     *         to delete the content (in one of the locations of the given Content Item).
1976
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if languageCode argument
1977
     *         is invalid for the given content.
1978
     *
1979
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
1980
     * @param string $languageCode
1981
     *
1982
     * @since 6.13
1983
     */
1984
    public function deleteTranslation(ContentInfo $contentInfo, $languageCode)
1985
    {
1986
        if ($contentInfo->mainLanguageCode === $languageCode) {
1987
            throw new BadStateException(
1988
                '$languageCode',
1989
                'Specified translation is the main translation of the Content Object'
1990
            );
1991
        }
1992
1993
        $translationWasFound = false;
1994
        $this->repository->beginTransaction();
1995
        try {
1996
            foreach ($this->loadVersions($contentInfo) as $versionInfo) {
1997
                if (!$this->repository->canUser('content', 'remove', $versionInfo)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1998
                    throw new UnauthorizedException(
1999
                        'content',
2000
                        'remove',
2001
                        ['contentId' => $contentInfo->id, 'versionNo' => $versionInfo->versionNo]
2002
                    );
2003
                }
2004
2005
                if (!in_array($languageCode, $versionInfo->languageCodes)) {
2006
                    continue;
2007
                }
2008
2009
                $translationWasFound = true;
2010
2011
                // If the translation is the version's only one, delete the version
2012
                if (count($versionInfo->languageCodes) < 2) {
2013
                    $this->persistenceHandler->contentHandler()->deleteVersion(
2014
                        $versionInfo->getContentInfo()->id,
2015
                        $versionInfo->versionNo
2016
                    );
2017
                }
2018
            }
2019
2020
            if (!$translationWasFound) {
2021
                throw new InvalidArgumentException(
2022
                    '$languageCode',
2023
                    sprintf(
2024
                        '%s does not exist in the Content item(id=%d)',
2025
                        $languageCode,
2026
                        $contentInfo->id
2027
                    )
2028
                );
2029
            }
2030
2031
            $this->persistenceHandler->contentHandler()->deleteTranslationFromContent(
2032
                $contentInfo->id,
2033
                $languageCode
2034
            );
2035
            $locationIds = array_map(
2036
                function (Location $location) {
2037
                    return $location->id;
2038
                },
2039
                $this->repository->getLocationService()->loadLocations($contentInfo)
2040
            );
2041
            $this->persistenceHandler->urlAliasHandler()->translationRemoved(
2042
                $locationIds,
2043
                $languageCode
2044
            );
2045
            $this->repository->commit();
2046
        } catch (InvalidArgumentException $e) {
2047
            $this->repository->rollback();
2048
            throw $e;
2049
        } catch (BadStateException $e) {
2050
            $this->repository->rollback();
2051
            throw $e;
2052
        } catch (UnauthorizedException $e) {
2053
            $this->repository->rollback();
2054
            throw $e;
2055
        } catch (Exception $e) {
2056
            $this->repository->rollback();
2057
            // cover generic unexpected exception to fulfill API promise on @throws
2058
            throw new BadStateException('$contentInfo', 'Translation removal failed', $e);
2059
        }
2060
    }
2061
2062
    /**
2063
     * Delete specified Translation from a Content Draft.
2064
     *
2065
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the specified Translation
2066
     *         is the only one the Content Draft has or it is the main Translation of a Content Object.
2067
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed
2068
     *         to edit the Content (in one of the locations of the given Content Object).
2069
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if languageCode argument
2070
     *         is invalid for the given Draft.
2071
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if specified Version was not found
2072
     *
2073
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo Content Version Draft
2074
     * @param string $languageCode Language code of the Translation to be removed
2075
     *
2076
     * @return \eZ\Publish\API\Repository\Values\Content\Content Content Draft w/o the specified Translation
2077
     *
2078
     * @since 6.12
2079
     */
2080
    public function deleteTranslationFromDraft(APIVersionInfo $versionInfo, $languageCode)
2081
    {
2082
        if (!$versionInfo->isDraft()) {
2083
            throw new BadStateException(
2084
                '$versionInfo',
2085
                'Version is not a draft, so Translations cannot be modified. Create a Draft before proceeding'
2086
            );
2087
        }
2088
2089
        if ($versionInfo->contentInfo->mainLanguageCode === $languageCode) {
2090
            throw new BadStateException(
2091
                '$languageCode',
2092
                'Specified Translation is the main Translation of the Content Object. Change it before proceeding.'
2093
            );
2094
        }
2095
2096
        if (!$this->repository->canUser('content', 'edit', $versionInfo->contentInfo)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Check if user has access to a given action on a given value object. Indicates if the current user is allowed to perform an action given by the function on the given
objects.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
2097
            throw new UnauthorizedException(
2098
                'content', 'edit', ['contentId' => $versionInfo->contentInfo->id]
2099
            );
2100
        }
2101
2102
        if (!in_array($languageCode, $versionInfo->languageCodes)) {
2103
            throw new InvalidArgumentException(
2104
                '$languageCode',
2105
                sprintf(
2106
                    'The Version (ContentId=%d, VersionNo=%d) is not translated into %s',
2107
                    $versionInfo->contentInfo->id,
2108
                    $versionInfo->versionNo,
2109
                    $languageCode
2110
                )
2111
            );
2112
        }
2113
2114
        if (count($versionInfo->languageCodes) === 1) {
2115
            throw new BadStateException(
2116
                '$languageCode',
2117
                'Specified Translation is the only one Content Object Version has'
2118
            );
2119
        }
2120
2121
        $this->repository->beginTransaction();
2122
        try {
2123
            $spiContent = $this->persistenceHandler->contentHandler()->deleteTranslationFromDraft(
2124
                $versionInfo->contentInfo->id,
2125
                $versionInfo->versionNo,
2126
                $languageCode
2127
            );
2128
            $this->repository->commit();
2129
2130
            return $this->domainMapper->buildContentDomainObject($spiContent);
2131
        } catch (APINotFoundException $e) {
2132
            // avoid wrapping expected NotFoundException in BadStateException handled below
2133
            $this->repository->rollback();
2134
            throw $e;
2135
        } catch (Exception $e) {
2136
            $this->repository->rollback();
2137
            // cover generic unexpected exception to fulfill API promise on @throws
2138
            throw new BadStateException('$contentInfo', 'Translation removal failed', $e);
2139
        }
2140
    }
2141
2142
    /**
2143
     * Instantiates a new content create struct object.
2144
     *
2145
     * alwaysAvailable is set to the ContentType's defaultAlwaysAvailable
2146
     *
2147
     * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType
2148
     * @param string $mainLanguageCode
2149
     *
2150
     * @return \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct
2151
     */
2152
    public function newContentCreateStruct(ContentType $contentType, $mainLanguageCode)
2153
    {
2154
        return new ContentCreateStruct(
2155
            array(
2156
                'contentType' => $contentType,
2157
                'mainLanguageCode' => $mainLanguageCode,
2158
                'alwaysAvailable' => $contentType->defaultAlwaysAvailable,
2159
            )
2160
        );
2161
    }
2162
2163
    /**
2164
     * Instantiates a new content meta data update struct.
2165
     *
2166
     * @return \eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct
2167
     */
2168
    public function newContentMetadataUpdateStruct()
2169
    {
2170
        return new ContentMetadataUpdateStruct();
2171
    }
2172
2173
    /**
2174
     * Instantiates a new content update struct.
2175
     *
2176
     * @return \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct
2177
     */
2178
    public function newContentUpdateStruct()
2179
    {
2180
        return new ContentUpdateStruct();
2181
    }
2182
2183
    /**
2184
     * Instantiates a new TranslationInfo object.
2185
     *
2186
     * @return \eZ\Publish\API\Repository\Values\Content\TranslationInfo
2187
     */
2188
    public function newTranslationInfo()
2189
    {
2190
        return new TranslationInfo();
2191
    }
2192
2193
    /**
2194
     * Instantiates a Translation object.
2195
     *
2196
     * @return \eZ\Publish\API\Repository\Values\Content\TranslationValues
2197
     */
2198
    public function newTranslationValues()
2199
    {
2200
        return new TranslationValues();
2201
    }
2202
}
2203