Completed
Push — master ( 48855d...06b3fd )
by
unknown
37:10 queued 16:09
created

Handler::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the Content Handler 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\Persistence\Legacy\Content;
10
11
use eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway as LocationGateway;
12
use eZ\Publish\SPI\Persistence\Content\Handler as BaseContentHandler;
13
use eZ\Publish\SPI\Persistence\Content\Type\Handler as ContentTypeHandler;
14
use eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter;
15
use eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway as UrlAliasGateway;
16
use eZ\Publish\SPI\Persistence\Content;
17
use eZ\Publish\SPI\Persistence\Content\CreateStruct;
18
use eZ\Publish\SPI\Persistence\Content\UpdateStruct;
19
use eZ\Publish\SPI\Persistence\Content\MetadataUpdateStruct;
20
use eZ\Publish\SPI\Persistence\Content\VersionInfo;
21
use eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct as RelationCreateStruct;
22
use eZ\Publish\Core\Base\Exceptions\NotFoundException as NotFound;
23
24
/**
25
 * The Content Handler stores Content and ContentType objects.
26
 */
27
class Handler implements BaseContentHandler
28
{
29
    /**
30
     * Content gateway.
31
     *
32
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Gateway
33
     */
34
    protected $contentGateway;
35
36
    /**
37
     * Location gateway.
38
     *
39
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway
40
     */
41
    protected $locationGateway;
42
43
    /**
44
     * Mapper.
45
     *
46
     * @var Mapper
47
     */
48
    protected $mapper;
49
50
    /**
51
     * FieldHandler.
52
     *
53
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\FieldHandler
54
     */
55
    protected $fieldHandler;
56
57
    /**
58
     * URL slug converter.
59
     *
60
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter
61
     */
62
    protected $slugConverter;
63
64
    /**
65
     * UrlAlias gateway.
66
     *
67
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway
68
     */
69
    protected $urlAliasGateway;
70
71
    /**
72
     * ContentType handler.
73
     *
74
     * @var \eZ\Publish\SPI\Persistence\Content\Type\Handler
75
     */
76
    protected $contentTypeHandler;
77
78
    /**
79
     * Tree handler.
80
     *
81
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\TreeHandler
82
     */
83
    protected $treeHandler;
84
85
    /**
86
     * Creates a new content handler.
87
     *
88
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\Gateway $contentGateway
89
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway $locationGateway
90
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\Mapper $mapper
91
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\FieldHandler $fieldHandler
92
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter $slugConverter
93
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway $urlAliasGateway
94
     * @param \eZ\Publish\SPI\Persistence\Content\Type\Handler $contentTypeHandler
95
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\TreeHandler $treeHandler
96
     */
97 View Code Duplication
    public function __construct(
98
        Gateway $contentGateway,
99
        LocationGateway $locationGateway,
100
        Mapper $mapper,
101
        FieldHandler $fieldHandler,
102
        SlugConverter $slugConverter,
103
        UrlAliasGateway $urlAliasGateway,
104
        ContentTypeHandler $contentTypeHandler,
105
        TreeHandler $treeHandler
106
    ) {
107
        $this->contentGateway = $contentGateway;
108
        $this->locationGateway = $locationGateway;
109
        $this->mapper = $mapper;
110
        $this->fieldHandler = $fieldHandler;
111
        $this->slugConverter = $slugConverter;
112
        $this->urlAliasGateway = $urlAliasGateway;
113
        $this->contentTypeHandler = $contentTypeHandler;
114
        $this->treeHandler = $treeHandler;
115
    }
116
117
    /**
118
     * Creates a new Content entity in the storage engine.
119
     *
120
     * The values contained inside the $content will form the basis of stored
121
     * entity.
122
     *
123
     * Will contain always a complete list of fields.
124
     *
125
     * @param \eZ\Publish\SPI\Persistence\Content\CreateStruct $struct Content creation struct.
126
     *
127
     * @return \eZ\Publish\SPI\Persistence\Content Content value object
128
     */
129
    public function create(CreateStruct $struct)
130
    {
131
        return $this->internalCreate($struct);
132
    }
133
134
    /**
135
     * Creates a new Content entity in the storage engine.
136
     *
137
     * The values contained inside the $content will form the basis of stored
138
     * entity.
139
     *
140
     * Will contain always a complete list of fields.
141
     *
142
     * @param \eZ\Publish\SPI\Persistence\Content\CreateStruct $struct Content creation struct.
143
     * @param mixed $versionNo Used by self::copy() to maintain version numbers
144
     *
145
     * @return \eZ\Publish\SPI\Persistence\Content Content value object
146
     */
147
    protected function internalCreate(CreateStruct $struct, $versionNo = 1)
148
    {
149
        $content = new Content();
150
151
        $content->fields = $struct->fields;
152
        $content->versionInfo = $this->mapper->createVersionInfoFromCreateStruct($struct, $versionNo);
153
154
        $content->versionInfo->contentInfo->id = $this->contentGateway->insertContentObject($struct, $versionNo);
155
        $content->versionInfo->id = $this->contentGateway->insertVersion(
156
            $content->versionInfo,
157
            $struct->fields
158
        );
159
160
        $contentType = $this->contentTypeHandler->load($struct->typeId);
161
        $this->fieldHandler->createNewFields($content, $contentType);
162
163
        // Create node assignments
164
        foreach ($struct->locations as $location) {
165
            $location->contentId = $content->versionInfo->contentInfo->id;
166
            $location->contentVersion = $content->versionInfo->versionNo;
167
            $this->locationGateway->createNodeAssignment(
168
                $location,
169
                $location->parentId,
170
                LocationGateway::NODE_ASSIGNMENT_OP_CODE_CREATE
171
            );
172
        }
173
174
        // Create names
175 View Code Duplication
        foreach ($content->versionInfo->names as $language => $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...
176
            $this->contentGateway->setName(
177
                $content->versionInfo->contentInfo->id,
178
                $content->versionInfo->versionNo,
179
                $name,
180
                $language
181
            );
182
        }
183
184
        return $content;
185
    }
186
187
    /**
188
     * Performs the publishing operations required to set the version identified by $updateStruct->versionNo and
189
     * $updateStruct->id as the published one.
190
     *
191
     * The publish procedure will:
192
     * - Create location nodes based on the node assignments
193
     * - Update the content object using the provided metadata update struct
194
     * - Update the node assignments
195
     * - Update location nodes of the content with the new published version
196
     * - Set content and version status to published
197
     *
198
     * @param int $contentId
199
     * @param int $versionNo
200
     * @param \eZ\Publish\SPI\Persistence\Content\MetadataUpdateStruct $metaDataUpdateStruct
201
     *
202
     * @return \eZ\Publish\SPI\Persistence\Content The published Content
203
     */
204
    public function publish($contentId, $versionNo, MetadataUpdateStruct $metaDataUpdateStruct)
205
    {
206
        // Archive currently published version
207
        $versionInfo = $this->loadVersionInfo($contentId, $versionNo);
208
        if ($versionInfo->contentInfo->currentVersionNo != $versionNo) {
209
            $this->setStatus(
210
                $contentId,
211
                VersionInfo::STATUS_ARCHIVED,
212
                $versionInfo->contentInfo->currentVersionNo
213
            );
214
        }
215
216
        // Set always available name for the content
217
        $metaDataUpdateStruct->name = $versionInfo->names[$versionInfo->contentInfo->mainLanguageCode];
218
219
        $this->contentGateway->updateContent($contentId, $metaDataUpdateStruct, $versionInfo);
220
        $this->locationGateway->createLocationsFromNodeAssignments(
221
            $contentId,
222
            $versionNo
223
        );
224
225
        $this->locationGateway->updateLocationsContentVersionNo($contentId, $versionNo);
226
        $this->setStatus($contentId, VersionInfo::STATUS_PUBLISHED, $versionNo);
227
228
        return $this->load($contentId, $versionNo);
229
    }
230
231
    /**
232
     * Creates a new draft version from $contentId in $version.
233
     *
234
     * Copies all fields from $contentId in $srcVersion and creates a new
235
     * version of the referred Content from it.
236
     *
237
     * Note: When creating a new draft in the old admin interface there will
238
     * also be an entry in the `eznode_assignment` created for the draft. This
239
     * is ignored in this implementation.
240
     *
241
     * @param mixed $contentId
242
     * @param mixed $srcVersion
243
     * @param mixed $userId
244
     *
245
     * @return \eZ\Publish\SPI\Persistence\Content
246
     */
247
    public function createDraftFromVersion($contentId, $srcVersion, $userId)
248
    {
249
        $content = $this->load($contentId, $srcVersion);
250
251
        // Create new version
252
        $content->versionInfo = $this->mapper->createVersionInfoForContent(
253
            $content,
254
            $this->contentGateway->getLastVersionNumber($contentId) + 1,
255
            $userId
256
        );
257
        $content->versionInfo->id = $this->contentGateway->insertVersion(
258
            $content->versionInfo,
259
            $content->fields
260
        );
261
262
        // Clone fields from previous version and append them to the new one
263
        $this->fieldHandler->createExistingFieldsInNewVersion($content);
264
265
        // Create relations for new version
266
        $relations = $this->contentGateway->loadRelations($contentId, $srcVersion);
267
        foreach ($relations as $relation) {
268
            $this->contentGateway->insertRelation(
269
                new RelationCreateStruct(
270
                    array(
271
                        'sourceContentId' => $contentId,
272
                        'sourceContentVersionNo' => $content->versionInfo->versionNo,
273
                        'sourceFieldDefinitionId' => $relation['ezcontentobject_link_contentclassattribute_id'],
274
                        'destinationContentId' => $relation['ezcontentobject_link_to_contentobject_id'],
275
                        'type' => (int)$relation['ezcontentobject_link_relation_type'],
276
                    )
277
                )
278
            );
279
        }
280
281
        // Create content names for new version
282
        foreach ($content->versionInfo->names as $language => $name) {
283
            $this->contentGateway->setName(
284
                $contentId,
285
                $content->versionInfo->versionNo,
286
                $name,
287
                $language
288
            );
289
        }
290
291
        return $content;
292
    }
293
294
    /**
295
     * Returns the raw data of a content object identified by $id, in a struct.
296
     *
297
     * A version to load must be specified. If you want to load the current
298
     * version of a content object use SearchHandler::findSingle() with the
299
     * ContentId criterion.
300
     *
301
     * Optionally a translation filter may be specified. If specified only the
302
     * translations with the listed language codes will be retrieved. If not,
303
     * all translations will be retrieved.
304
     *
305
     * @param int|string $id
306
     * @param int|string $version
307
     * @param string[] $translations
308
     *
309
     * @return \eZ\Publish\SPI\Persistence\Content Content value object
310
     */
311
    public function load($id, $version, array $translations = null)
312
    {
313
        $rows = $this->contentGateway->load($id, $version, $translations);
314
315
        if (empty($rows)) {
316
            throw new NotFound('content', "contentId: $id, versionNo: $version");
317
        }
318
319
        $contentObjects = $this->mapper->extractContentFromRows(
320
            $rows,
321
            $this->contentGateway->loadVersionedNameData(array(array('id' => $id, 'version' => $version)))
322
        );
323
        $content = $contentObjects[0];
324
        unset($rows, $contentObjects);
325
326
        $this->fieldHandler->loadExternalFieldData($content);
327
328
        return $content;
329
    }
330
331
    /**
332
     * {@inheritdoc}
333
     */
334
    public function loadContentList(array $contentIds, array $translations = null): array
335
    {
336
        $rawList = $this->contentGateway->loadContentList($contentIds, $translations);
0 ignored issues
show
Bug introduced by
It seems like $translations defined by parameter $translations on line 334 can also be of type array; however, eZ\Publish\Core\Persiste...eway::loadContentList() does only seem to accept null|array<integer,string>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
337
        if (empty($rawList)) {
338
            return [];
339
        }
340
341
        $idVersionPairs = [];
342
        foreach ($rawList as $row) {
343
            // As there is only one version per id, set id as key to avoid duplicates
344
            $idVersionPairs[$row['ezcontentobject_id']] = [
345
                'id' => $row['ezcontentobject_id'],
346
                'version' => $row['ezcontentobject_version_version'],
347
            ];
348
        }
349
350
        $contentObjects = $this->mapper->extractContentFromRows(
351
            $rawList,
352
            $this->contentGateway->loadVersionedNameData(array_values($idVersionPairs))
353
        );
354
355
        unset($rawList, $idVersionPairs, $idVersionTranslationPairs);
356
357
        $result = [];
358
        foreach ($contentObjects as $content) {
359
            $this->fieldHandler->loadExternalFieldData($content);
360
            $result[$content->versionInfo->contentInfo->id] = $content;
361
        }
362
363
        return $result;
364
    }
365
366
    /**
367
     * Returns the metadata object for a content identified by $contentId.
368
     *
369
     * @param int|string $contentId
370
     *
371
     * @return \eZ\Publish\SPI\Persistence\Content\ContentInfo
372
     */
373
    public function loadContentInfo($contentId)
374
    {
375
        return $this->treeHandler->loadContentInfo($contentId);
376
    }
377
378
    public function loadContentInfoList(array $contentIds)
379
    {
380
        $list = $this->mapper->extractContentInfoFromRows(
381
            $this->contentGateway->loadContentInfoList($contentIds)
382
        );
383
384
        $listByContentId = [];
385
        foreach ($list as $item) {
386
            $listByContentId[$item->id] = $item;
387
        }
388
389
        return $listByContentId;
390
    }
391
392
    /**
393
     * Returns the metadata object for a content identified by $remoteId.
394
     *
395
     * @param mixed $remoteId
396
     *
397
     * @return \eZ\Publish\SPI\Persistence\Content\ContentInfo
398
     */
399
    public function loadContentInfoByRemoteId($remoteId)
400
    {
401
        return $this->mapper->extractContentInfoFromRow(
402
            $this->contentGateway->loadContentInfoByRemoteId($remoteId)
403
        );
404
    }
405
406
    /**
407
     * Returns the version object for a content/version identified by $contentId and $versionNo.
408
     *
409
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If version is not found
410
     *
411
     * @param int|string $contentId
412
     * @param int $versionNo Version number to load
413
     *
414
     * @return \eZ\Publish\SPI\Persistence\Content\VersionInfo
415
     */
416
    public function loadVersionInfo($contentId, $versionNo)
417
    {
418
        $rows = $this->contentGateway->loadVersionInfo($contentId, $versionNo);
419
        if (empty($rows)) {
420
            throw new NotFound('content', $contentId);
421
        }
422
423
        $versionInfo = $this->mapper->extractVersionInfoListFromRows(
424
            $rows,
425
            $this->contentGateway->loadVersionedNameData(array(array('id' => $contentId, 'version' => $versionNo)))
426
        );
427
428
        return reset($versionInfo);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression reset($versionInfo); of type eZ\Publish\SPI\Persisten...ntent\VersionInfo|false adds false to the return on line 428 which is incompatible with the return type declared by the interface eZ\Publish\SPI\Persisten...andler::loadVersionInfo of type eZ\Publish\SPI\Persistence\Content\VersionInfo. It seems like you forgot to handle an error condition.
Loading history...
429
    }
430
431
    /**
432
     * Returns all versions with draft status created by the given $userId.
433
     *
434
     * @param int $userId
435
     *
436
     * @return \eZ\Publish\SPI\Persistence\Content\VersionInfo[]
437
     */
438 View Code Duplication
    public function loadDraftsForUser($userId)
439
    {
440
        $rows = $this->contentGateway->listVersionsForUser($userId, VersionInfo::STATUS_DRAFT);
441
        if (empty($rows)) {
442
            return array();
443
        }
444
445
        $idVersionPairs = array_map(
446
            function ($row) {
447
                return array(
448
                    'id' => $row['ezcontentobject_version_contentobject_id'],
449
                    'version' => $row['ezcontentobject_version_version'],
450
                );
451
            },
452
            $rows
453
        );
454
        $nameRows = $this->contentGateway->loadVersionedNameData($idVersionPairs);
455
456
        return $this->mapper->extractVersionInfoListFromRows($rows, $nameRows);
457
    }
458
459
    /**
460
     * Sets the status of object identified by $contentId and $version to $status.
461
     *
462
     * The $status can be one of VersionInfo::STATUS_DRAFT, VersionInfo::STATUS_PUBLISHED, VersionInfo::STATUS_ARCHIVED
463
     * When status is set to VersionInfo::STATUS_PUBLISHED content status is updated to ContentInfo::STATUS_PUBLISHED
464
     *
465
     * @param int $contentId
466
     * @param int $status
467
     * @param int $version
468
     *
469
     * @return bool
470
     */
471
    public function setStatus($contentId, $status, $version)
472
    {
473
        return $this->contentGateway->setStatus($contentId, $version, $status);
474
    }
475
476
    /**
477
     * Updates a content object meta data, identified by $contentId.
478
     *
479
     * @param int $contentId
480
     * @param \eZ\Publish\SPI\Persistence\Content\MetadataUpdateStruct $content
481
     *
482
     * @return \eZ\Publish\SPI\Persistence\Content\ContentInfo
483
     */
484
    public function updateMetadata($contentId, MetadataUpdateStruct $content)
485
    {
486
        $this->contentGateway->updateContent($contentId, $content);
487
        $this->updatePathIdentificationString($contentId, $content);
488
489
        return $this->loadContentInfo($contentId);
490
    }
491
492
    /**
493
     * Updates path identification string for locations of given $contentId if main language
494
     * is set in update struct.
495
     *
496
     * This is specific to the Legacy storage engine, as path identification string is deprecated.
497
     *
498
     * @param int $contentId
499
     * @param \eZ\Publish\SPI\Persistence\Content\MetadataUpdateStruct $content
500
     */
501
    protected function updatePathIdentificationString($contentId, MetadataUpdateStruct $content)
502
    {
503
        if (isset($content->mainLanguageId)) {
504
            $contentLocationsRows = $this->locationGateway->loadLocationDataByContent($contentId);
505
            foreach ($contentLocationsRows as $row) {
506
                $locationName = '';
507
                $urlAliasRows = $this->urlAliasGateway->loadLocationEntries(
508
                    $row['node_id'],
509
                    false,
510
                    $content->mainLanguageId
511
                );
512
                if (!empty($urlAliasRows)) {
513
                    $locationName = $urlAliasRows[0]['text'];
514
                }
515
                $this->locationGateway->updatePathIdentificationString(
516
                    $row['node_id'],
517
                    $row['parent_node_id'],
518
                    $this->slugConverter->convert(
519
                        $locationName,
520
                        'node_' . $row['node_id'],
521
                        'urlalias_compat'
522
                    )
523
                );
524
            }
525
        }
526
    }
527
528
    /**
529
     * Updates a content version, identified by $contentId and $versionNo.
530
     *
531
     * @param int $contentId
532
     * @param int $versionNo
533
     * @param \eZ\Publish\SPI\Persistence\Content\UpdateStruct $updateStruct
534
     *
535
     * @return \eZ\Publish\SPI\Persistence\Content
536
     */
537
    public function updateContent($contentId, $versionNo, UpdateStruct $updateStruct)
538
    {
539
        $content = $this->load($contentId, $versionNo);
540
        $this->contentGateway->updateVersion($contentId, $versionNo, $updateStruct);
541
        $contentType = $this->contentTypeHandler->load($content->versionInfo->contentInfo->contentTypeId);
542
        $this->fieldHandler->updateFields($content, $updateStruct, $contentType);
543
        foreach ($updateStruct->name as $language => $name) {
544
            $this->contentGateway->setName(
545
                $contentId,
546
                $versionNo,
547
                $name,
548
                $language
549
            );
550
        }
551
552
        return $this->load($contentId, $versionNo);
553
    }
554
555
    /**
556
     * Deletes all versions and fields, all locations (subtree), and all relations.
557
     *
558
     * Removes the relations, but not the related objects. All subtrees of the
559
     * assigned nodes of this content objects are removed (recursively).
560
     *
561
     * @param int $contentId
562
     *
563
     * @return bool
564
     */
565
    public function deleteContent($contentId)
566
    {
567
        $contentLocations = $this->contentGateway->getAllLocationIds($contentId);
568
        if (empty($contentLocations)) {
569
            $this->removeRawContent($contentId);
570
        } else {
571
            foreach ($contentLocations as $locationId) {
572
                $this->treeHandler->removeSubtree($locationId);
573
            }
574
        }
575
    }
576
577
    /**
578
     * Deletes raw content data.
579
     *
580
     * @param int $contentId
581
     */
582
    public function removeRawContent($contentId)
583
    {
584
        $this->treeHandler->removeRawContent($contentId);
585
    }
586
587
    /**
588
     * Deletes given version, its fields, node assignment, relations and names.
589
     *
590
     * Removes the relations, but not the related objects.
591
     *
592
     * @param int $contentId
593
     * @param int $versionNo
594
     *
595
     * @return bool
596
     */
597
    public function deleteVersion($contentId, $versionNo)
598
    {
599
        $versionInfo = $this->loadVersionInfo($contentId, $versionNo);
600
601
        $this->locationGateway->deleteNodeAssignment($contentId, $versionNo);
602
603
        $this->fieldHandler->deleteFields($contentId, $versionInfo);
604
605
        $this->contentGateway->deleteRelations($contentId, $versionNo);
606
        $this->contentGateway->deleteVersions($contentId, $versionNo);
607
        $this->contentGateway->deleteNames($contentId, $versionNo);
608
    }
609
610
    /**
611
     * Returns the versions for $contentId.
612
     *
613
     * Result is returned with oldest version first (sorted by created, alternatively version id if auto increment).
614
     *
615
     * @param int $contentId
616
     * @param mixed|null $status Optional argument to filter versions by status, like {@see VersionInfo::STATUS_ARCHIVED}.
617
     * @param int $limit Limit for items returned, -1 means none.
618
     *
619
     * @return \eZ\Publish\SPI\Persistence\Content\VersionInfo[]
620
     */
621
    public function listVersions($contentId, $status = null, $limit = -1)
622
    {
623
        return $this->treeHandler->listVersions($contentId, $status, $limit);
624
    }
625
626
    /**
627
     * Copy Content with Fields, Versions & Relations from $contentId in $version.
628
     *
629
     * {@inheritdoc}
630
     *
631
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If content or version is not found
632
     *
633
     * @param mixed $contentId
634
     * @param mixed|null $versionNo Copy all versions if left null
635
     *
636
     * @return \eZ\Publish\SPI\Persistence\Content
637
     */
638
    public function copy($contentId, $versionNo = null)
639
    {
640
        $currentVersionNo = isset($versionNo) ?
641
            $versionNo :
642
            $this->loadContentInfo($contentId)->currentVersionNo;
643
644
        // Copy content in given version or current version
645
        $createStruct = $this->mapper->createCreateStructFromContent(
646
            $this->load($contentId, $currentVersionNo)
647
        );
648
        $content = $this->internalCreate($createStruct, $currentVersionNo);
649
650
        // If version was not passed also copy other versions
651
        if (!isset($versionNo)) {
652
            $contentType = $this->contentTypeHandler->load($createStruct->typeId);
653
654
            foreach ($this->listVersions($contentId) as $versionInfo) {
655
                if ($versionInfo->versionNo === $currentVersionNo) {
656
                    continue;
657
                }
658
659
                $versionContent = $this->load($contentId, $versionInfo->versionNo);
660
661
                $versionContent->versionInfo->contentInfo->id = $content->versionInfo->contentInfo->id;
662
                $versionContent->versionInfo->modificationDate = $createStruct->modified;
663
                $versionContent->versionInfo->creationDate = $createStruct->modified;
664
                $versionContent->versionInfo->id = $this->contentGateway->insertVersion(
665
                    $versionContent->versionInfo,
666
                    $versionContent->fields
667
                );
668
669
                $this->fieldHandler->createNewFields($versionContent, $contentType);
670
671
                // Create names
672 View Code Duplication
                foreach ($versionContent->versionInfo->names as $language => $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...
673
                    $this->contentGateway->setName(
674
                        $content->versionInfo->contentInfo->id,
675
                        $versionInfo->versionNo,
676
                        $name,
677
                        $language
678
                    );
679
                }
680
            }
681
682
            // Batch copy relations for all versions
683
            $this->contentGateway->copyRelations($contentId, $content->versionInfo->contentInfo->id);
684
        } else {
685
            // Batch copy relations for published version
686
            $this->contentGateway->copyRelations($contentId, $content->versionInfo->contentInfo->id, $versionNo);
687
        }
688
689
        return $content;
690
    }
691
692
    /**
693
     * Creates a relation between $sourceContentId in $sourceContentVersionNo
694
     * and $destinationContentId with a specific $type.
695
     *
696
     * @todo Should the existence verifications happen here or is this supposed to be handled at a higher level?
697
     *
698
     * @param \eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct $createStruct
699
     *
700
     * @return \eZ\Publish\SPI\Persistence\Content\Relation
701
     */
702
    public function addRelation(RelationCreateStruct $createStruct)
703
    {
704
        $relation = $this->mapper->createRelationFromCreateStruct($createStruct);
705
706
        $relation->id = $this->contentGateway->insertRelation($createStruct);
707
708
        return $relation;
709
    }
710
711
    /**
712
     * Removes a relation by relation Id.
713
     *
714
     * @todo Should the existence verifications happen here or is this supposed to be handled at a higher level?
715
     *
716
     * @param mixed $relationId
717
     * @param int $type {@see \eZ\Publish\API\Repository\Values\Content\Relation::COMMON,
718
     *                 \eZ\Publish\API\Repository\Values\Content\Relation::EMBED,
719
     *                 \eZ\Publish\API\Repository\Values\Content\Relation::LINK,
720
     *                 \eZ\Publish\API\Repository\Values\Content\Relation::FIELD}
721
     */
722
    public function removeRelation($relationId, $type)
723
    {
724
        $this->contentGateway->deleteRelation($relationId, $type);
725
    }
726
727
    /**
728
     * Loads relations from $sourceContentId. Optionally, loads only those with $type and $sourceContentVersionNo.
729
     *
730
     * @param mixed $sourceContentId Source Content ID
731
     * @param mixed|null $sourceContentVersionNo Source Content Version, null if not specified
732
     * @param int|null $type {@see \eZ\Publish\API\Repository\Values\Content\Relation::COMMON,
733
     *                 \eZ\Publish\API\Repository\Values\Content\Relation::EMBED,
734
     *                 \eZ\Publish\API\Repository\Values\Content\Relation::LINK,
735
     *                 \eZ\Publish\API\Repository\Values\Content\Relation::FIELD}
736
     *
737
     * @return \eZ\Publish\SPI\Persistence\Content\Relation[]
738
     */
739
    public function loadRelations($sourceContentId, $sourceContentVersionNo = null, $type = null)
740
    {
741
        return $this->mapper->extractRelationsFromRows(
742
            $this->contentGateway->loadRelations($sourceContentId, $sourceContentVersionNo, $type)
743
        );
744
    }
745
746
    /**
747
     * Loads relations from $contentId. Optionally, loads only those with $type.
748
     *
749
     * Only loads relations against published versions.
750
     *
751
     * @param mixed $destinationContentId Destination Content ID
752
     * @param int|null $type {@see \eZ\Publish\API\Repository\Values\Content\Relation::COMMON,
753
     *                 \eZ\Publish\API\Repository\Values\Content\Relation::EMBED,
754
     *                 \eZ\Publish\API\Repository\Values\Content\Relation::LINK,
755
     *                 \eZ\Publish\API\Repository\Values\Content\Relation::FIELD}
756
     *
757
     * @return \eZ\Publish\SPI\Persistence\Content\Relation[]
758
     */
759
    public function loadReverseRelations($destinationContentId, $type = null)
760
    {
761
        return $this->mapper->extractRelationsFromRows(
762
            $this->contentGateway->loadReverseRelations($destinationContentId, $type)
763
        );
764
    }
765
766
    /**
767
     * {@inheritdoc}
768
     */
769
    public function removeTranslationFromContent($contentId, $languageCode)
770
    {
771
        @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...
772
            __METHOD__ . ' is deprecated, use deleteTranslationFromContent instead',
773
            E_USER_DEPRECATED
774
        );
775
        $this->deleteTranslationFromContent($contentId, $languageCode);
776
    }
777
778
    /**
779
     * {@inheritdoc}
780
     */
781
    public function deleteTranslationFromContent($contentId, $languageCode)
782
    {
783
        $this->fieldHandler->deleteTranslationFromContentFields(
784
            $contentId,
785
            $this->listVersions($contentId),
786
            $languageCode
787
        );
788
        $this->contentGateway->deleteTranslationFromContent($contentId, $languageCode);
789
    }
790
791
    /**
792
     * {@inheritdoc}
793
     */
794
    public function deleteTranslationFromDraft($contentId, $versionNo, $languageCode)
795
    {
796
        $versionInfo = $this->loadVersionInfo($contentId, $versionNo);
797
798
        $this->fieldHandler->deleteTranslationFromVersionFields(
799
            $versionInfo,
800
            $languageCode
801
        );
802
        $this->contentGateway->deleteTranslationFromVersion(
803
            $contentId,
804
            $versionNo,
805
            $languageCode
806
        );
807
808
        // get all [languageCode => name] entries except the removed Translation
809
        $names = array_filter(
810
            $versionInfo->names,
811
            function ($lang) use ($languageCode) {
812
                return $lang !== $languageCode;
813
            },
814
            ARRAY_FILTER_USE_KEY
815
        );
816
        // set new Content name
817
        foreach ($names as $language => $name) {
818
            $this->contentGateway->setName(
819
                $contentId,
820
                $versionNo,
821
                $name,
822
                $language
823
            );
824
        }
825
826
        // reload entire Version w/o removed Translation
827
        return $this->load($contentId, $versionNo);
828
    }
829
}
830