Completed
Push — sf_cache ( f6a6ab...bf1241 )
by André
35:58 queued 24:16
created

Handler::deleteContent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 11
rs 9.4285
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
    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
325
        $this->fieldHandler->loadExternalFieldData($content);
326
327
        return $content;
328
    }
329
330
    /**
331
     * Returns the metadata object for a content identified by $contentId.
332
     *
333
     * @param int|string $contentId
334
     *
335
     * @return \eZ\Publish\SPI\Persistence\Content\ContentInfo
336
     */
337
    public function loadContentInfo($contentId)
338
    {
339
        return $this->treeHandler->loadContentInfo($contentId);
340
    }
341
342
    /**
343
     * Returns the metadata object for a content identified by $remoteId.
344
     *
345
     * @param mixed $remoteId
346
     *
347
     * @return \eZ\Publish\SPI\Persistence\Content\ContentInfo
348
     */
349
    public function loadContentInfoByRemoteId($remoteId)
350
    {
351
        return $this->mapper->extractContentInfoFromRow(
352
            $this->contentGateway->loadContentInfoByRemoteId($remoteId)
353
        );
354
    }
355
356
    /**
357
     * Returns the version object for a content/version identified by $contentId and $versionNo.
358
     *
359
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If version is not found
360
     *
361
     * @param int|string $contentId
362
     * @param int $versionNo Version number to load
363
     *
364
     * @return \eZ\Publish\SPI\Persistence\Content\VersionInfo
365
     */
366
    public function loadVersionInfo($contentId, $versionNo)
367
    {
368
        $rows = $this->contentGateway->loadVersionInfo($contentId, $versionNo);
369
        if (empty($rows)) {
370
            throw new NotFound('content', $contentId);
371
        }
372
373
        $versionInfo = $this->mapper->extractVersionInfoListFromRows(
374
            $rows,
375
            $this->contentGateway->loadVersionedNameData(array(array('id' => $contentId, 'version' => $versionNo)))
376
        );
377
378
        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 378 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...
379
    }
380
381
    /**
382
     * Returns all versions with draft status created by the given $userId.
383
     *
384
     * @param int $userId
385
     *
386
     * @return \eZ\Publish\SPI\Persistence\Content\VersionInfo[]
387
     */
388 View Code Duplication
    public function loadDraftsForUser($userId)
389
    {
390
        $rows = $this->contentGateway->listVersionsForUser($userId, VersionInfo::STATUS_DRAFT);
391
        if (empty($rows)) {
392
            return array();
393
        }
394
395
        $idVersionPairs = array_map(
396
            function ($row) {
397
                return array(
398
                    'id' => $row['ezcontentobject_version_contentobject_id'],
399
                    'version' => $row['ezcontentobject_version_version'],
400
                );
401
            },
402
            $rows
403
        );
404
        $nameRows = $this->contentGateway->loadVersionedNameData($idVersionPairs);
405
406
        return $this->mapper->extractVersionInfoListFromRows($rows, $nameRows);
407
    }
408
409
    /**
410
     * Sets the status of object identified by $contentId and $version to $status.
411
     *
412
     * The $status can be one of VersionInfo::STATUS_DRAFT, VersionInfo::STATUS_PUBLISHED, VersionInfo::STATUS_ARCHIVED
413
     * When status is set to VersionInfo::STATUS_PUBLISHED content status is updated to ContentInfo::STATUS_PUBLISHED
414
     *
415
     * @param int $contentId
416
     * @param int $status
417
     * @param int $version
418
     *
419
     * @return bool
420
     */
421
    public function setStatus($contentId, $status, $version)
422
    {
423
        return $this->contentGateway->setStatus($contentId, $version, $status);
424
    }
425
426
    /**
427
     * Updates a content object meta data, identified by $contentId.
428
     *
429
     * @param int $contentId
430
     * @param \eZ\Publish\SPI\Persistence\Content\MetadataUpdateStruct $content
431
     *
432
     * @return \eZ\Publish\SPI\Persistence\Content\ContentInfo
433
     */
434
    public function updateMetadata($contentId, MetadataUpdateStruct $content)
435
    {
436
        $this->contentGateway->updateContent($contentId, $content);
437
        $this->updatePathIdentificationString($contentId, $content);
438
439
        return $this->loadContentInfo($contentId);
440
    }
441
442
    /**
443
     * Updates path identification string for locations of given $contentId if main language
444
     * is set in update struct.
445
     *
446
     * This is specific to the Legacy storage engine, as path identification string is deprecated.
447
     *
448
     * @param int $contentId
449
     * @param \eZ\Publish\SPI\Persistence\Content\MetadataUpdateStruct $content
450
     */
451
    protected function updatePathIdentificationString($contentId, MetadataUpdateStruct $content)
452
    {
453
        if (isset($content->mainLanguageId)) {
454
            $contentLocationsRows = $this->locationGateway->loadLocationDataByContent($contentId);
455
            foreach ($contentLocationsRows as $row) {
456
                $locationName = '';
457
                $urlAliasRows = $this->urlAliasGateway->loadLocationEntries(
458
                    $row['node_id'],
459
                    false,
460
                    $content->mainLanguageId
461
                );
462
                if (!empty($urlAliasRows)) {
463
                    $locationName = $urlAliasRows[0]['text'];
464
                }
465
                $this->locationGateway->updatePathIdentificationString(
466
                    $row['node_id'],
467
                    $row['parent_node_id'],
468
                    $this->slugConverter->convert(
469
                        $locationName,
470
                        'node_' . $row['node_id'],
471
                        'urlalias_compat'
472
                    )
473
                );
474
            }
475
        }
476
    }
477
478
    /**
479
     * Updates a content version, identified by $contentId and $versionNo.
480
     *
481
     * @param int $contentId
482
     * @param int $versionNo
483
     * @param \eZ\Publish\SPI\Persistence\Content\UpdateStruct $updateStruct
484
     *
485
     * @return \eZ\Publish\SPI\Persistence\Content
486
     */
487
    public function updateContent($contentId, $versionNo, UpdateStruct $updateStruct)
488
    {
489
        $content = $this->load($contentId, $versionNo);
490
        $this->contentGateway->updateVersion($contentId, $versionNo, $updateStruct);
491
        $contentType = $this->contentTypeHandler->load($content->versionInfo->contentInfo->contentTypeId);
492
        $this->fieldHandler->updateFields($content, $updateStruct, $contentType);
493
        foreach ($updateStruct->name as $language => $name) {
494
            $this->contentGateway->setName(
495
                $contentId,
496
                $versionNo,
497
                $name,
498
                $language
499
            );
500
        }
501
502
        return $this->load($contentId, $versionNo);
503
    }
504
505
    /**
506
     * Deletes all versions and fields, all locations (subtree), and all relations.
507
     *
508
     * Removes the relations, but not the related objects. All subtrees of the
509
     * assigned nodes of this content objects are removed (recursively).
510
     *
511
     * @param int $contentId
512
     *
513
     * @return bool
514
     */
515
    public function deleteContent($contentId)
516
    {
517
        $contentLocations = $this->contentGateway->getAllLocationIds($contentId);
518
        if (empty($contentLocations)) {
519
            $this->removeRawContent($contentId);
520
        } else {
521
            foreach ($contentLocations as $locationId) {
522
                $this->treeHandler->removeSubtree($locationId);
523
            }
524
        }
525
    }
526
527
    /**
528
     * Deletes raw content data.
529
     *
530
     * @param int $contentId
531
     */
532
    public function removeRawContent($contentId)
533
    {
534
        $this->treeHandler->removeRawContent($contentId);
535
    }
536
537
    /**
538
     * Deletes given version, its fields, node assignment, relations and names.
539
     *
540
     * Removes the relations, but not the related objects.
541
     *
542
     * @param int $contentId
543
     * @param int $versionNo
544
     *
545
     * @return bool
546
     */
547
    public function deleteVersion($contentId, $versionNo)
548
    {
549
        $versionInfo = $this->loadVersionInfo($contentId, $versionNo);
550
551
        $this->locationGateway->deleteNodeAssignment($contentId, $versionNo);
552
553
        $this->fieldHandler->deleteFields($contentId, $versionInfo);
554
555
        $this->contentGateway->deleteRelations($contentId, $versionNo);
556
        $this->contentGateway->deleteVersions($contentId, $versionNo);
557
        $this->contentGateway->deleteNames($contentId, $versionNo);
558
    }
559
560
    /**
561
     * Returns the versions for $contentId.
562
     *
563
     * Result is returned with oldest version first (sorted by created, alternatively version id if auto increment).
564
     *
565
     * @param int $contentId
566
     * @param mixed|null $status Optional argument to filter versions by status, like {@see VersionInfo::STATUS_ARCHIVED}.
567
     * @param int $limit Limit for items returned, -1 means none.
568
     *
569
     * @return \eZ\Publish\SPI\Persistence\Content\VersionInfo[]
570
     */
571
    public function listVersions($contentId, $status = null, $limit = -1)
572
    {
573
        return $this->treeHandler->listVersions($contentId, $status, $limit);
574
    }
575
576
    /**
577
     * Copy Content with Fields, Versions & Relations from $contentId in $version.
578
     *
579
     * {@inheritdoc}
580
     *
581
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If content or version is not found
582
     *
583
     * @param mixed $contentId
584
     * @param mixed|null $versionNo Copy all versions if left null
585
     *
586
     * @return \eZ\Publish\SPI\Persistence\Content
587
     */
588
    public function copy($contentId, $versionNo = null)
589
    {
590
        $currentVersionNo = isset($versionNo) ?
591
            $versionNo :
592
            $this->loadContentInfo($contentId)->currentVersionNo;
593
594
        // Copy content in given version or current version
595
        $createStruct = $this->mapper->createCreateStructFromContent(
596
            $this->load($contentId, $currentVersionNo)
597
        );
598
        $content = $this->internalCreate($createStruct, $currentVersionNo);
599
600
        // If version was not passed also copy other versions
601
        if (!isset($versionNo)) {
602
            $contentType = $this->contentTypeHandler->load($createStruct->typeId);
603
604
            foreach ($this->listVersions($contentId) as $versionInfo) {
605
                if ($versionInfo->versionNo === $currentVersionNo) {
606
                    continue;
607
                }
608
609
                $versionContent = $this->load($contentId, $versionInfo->versionNo);
610
611
                $versionContent->versionInfo->contentInfo->id = $content->versionInfo->contentInfo->id;
612
                $versionContent->versionInfo->modificationDate = $createStruct->modified;
613
                $versionContent->versionInfo->creationDate = $createStruct->modified;
614
                $versionContent->versionInfo->id = $this->contentGateway->insertVersion(
615
                    $versionContent->versionInfo,
616
                    $versionContent->fields
617
                );
618
619
                $this->fieldHandler->createNewFields($versionContent, $contentType);
620
621
                // Create names
622 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...
623
                    $this->contentGateway->setName(
624
                        $content->versionInfo->contentInfo->id,
625
                        $versionInfo->versionNo,
626
                        $name,
627
                        $language
628
                    );
629
                }
630
            }
631
632
            // Batch copy relations for all versions
633
            $this->contentGateway->copyRelations($contentId, $content->versionInfo->contentInfo->id);
634
        } else {
635
            // Batch copy relations for published version
636
            $this->contentGateway->copyRelations($contentId, $content->versionInfo->contentInfo->id, $versionNo);
637
        }
638
639
        return $content;
640
    }
641
642
    /**
643
     * Creates a relation between $sourceContentId in $sourceContentVersionNo
644
     * and $destinationContentId with a specific $type.
645
     *
646
     * @todo Should the existence verifications happen here or is this supposed to be handled at a higher level?
647
     *
648
     * @param \eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct $createStruct
649
     *
650
     * @return \eZ\Publish\SPI\Persistence\Content\Relation
651
     */
652
    public function addRelation(RelationCreateStruct $createStruct)
653
    {
654
        $relation = $this->mapper->createRelationFromCreateStruct($createStruct);
655
656
        $relation->id = $this->contentGateway->insertRelation($createStruct);
657
658
        return $relation;
659
    }
660
661
    /**
662
     * Removes a relation by relation Id.
663
     *
664
     * @todo Should the existence verifications happen here or is this supposed to be handled at a higher level?
665
     *
666
     * @param mixed $relationId
667
     * @param int $type {@see \eZ\Publish\API\Repository\Values\Content\Relation::COMMON,
668
     *                 \eZ\Publish\API\Repository\Values\Content\Relation::EMBED,
669
     *                 \eZ\Publish\API\Repository\Values\Content\Relation::LINK,
670
     *                 \eZ\Publish\API\Repository\Values\Content\Relation::FIELD}
671
     */
672
    public function removeRelation($relationId, $type)
673
    {
674
        $this->contentGateway->deleteRelation($relationId, $type);
675
    }
676
677
    /**
678
     * Loads relations from $sourceContentId. Optionally, loads only those with $type and $sourceContentVersionNo.
679
     *
680
     * @param mixed $sourceContentId Source Content ID
681
     * @param mixed|null $sourceContentVersionNo Source Content Version, null if not specified
682
     * @param int|null $type {@see \eZ\Publish\API\Repository\Values\Content\Relation::COMMON,
683
     *                 \eZ\Publish\API\Repository\Values\Content\Relation::EMBED,
684
     *                 \eZ\Publish\API\Repository\Values\Content\Relation::LINK,
685
     *                 \eZ\Publish\API\Repository\Values\Content\Relation::FIELD}
686
     *
687
     * @return \eZ\Publish\SPI\Persistence\Content\Relation[]
688
     */
689
    public function loadRelations($sourceContentId, $sourceContentVersionNo = null, $type = null)
690
    {
691
        return $this->mapper->extractRelationsFromRows(
692
            $this->contentGateway->loadRelations($sourceContentId, $sourceContentVersionNo, $type)
693
        );
694
    }
695
696
    /**
697
     * Loads relations from $contentId. Optionally, loads only those with $type.
698
     *
699
     * Only loads relations against published versions.
700
     *
701
     * @param mixed $destinationContentId Destination Content ID
702
     * @param int|null $type {@see \eZ\Publish\API\Repository\Values\Content\Relation::COMMON,
703
     *                 \eZ\Publish\API\Repository\Values\Content\Relation::EMBED,
704
     *                 \eZ\Publish\API\Repository\Values\Content\Relation::LINK,
705
     *                 \eZ\Publish\API\Repository\Values\Content\Relation::FIELD}
706
     *
707
     * @return \eZ\Publish\SPI\Persistence\Content\Relation[]
708
     */
709
    public function loadReverseRelations($destinationContentId, $type = null)
710
    {
711
        return $this->mapper->extractRelationsFromRows(
712
            $this->contentGateway->loadReverseRelations($destinationContentId, $type)
713
        );
714
    }
715
}
716