Completed
Push — master ( 65f512...fe0390 )
by Łukasz
26:26
created

Handler::loadDraftsForUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20

Duplication

Lines 20
Ratio 100 %

Importance

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