1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DNADesign\Elemental\Extensions; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Config\Config; |
6
|
|
|
use SilverStripe\ORM\DataExtension; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @deprecated 2.1..3.0 This extension is not implemented by default, and will be removed |
10
|
|
|
* in 3.0.0. Please use the `$owns` API with `$cascade_duplicates` instead. |
11
|
|
|
* See {@link ElementalPageExtension} for an example of this. |
12
|
|
|
*/ |
13
|
|
|
class ElementalDuplicationExtension extends DataExtension |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Duplicate items |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
public function onAfterDuplicate($original, $doWrite = true) |
20
|
|
|
{ |
21
|
|
|
$thisClass = $this->owner->ClassName; |
22
|
|
|
|
23
|
|
|
// Duplicate has_one's and has_many's |
24
|
|
|
$duplicateRelations = Config::inst()->get($thisClass, 'duplicate_relations'); |
25
|
|
|
if ($duplicateRelations && !empty($duplicateRelations)) { |
26
|
|
|
foreach ($duplicateRelations as $relation) { |
27
|
|
|
$items = $original->$relation(); |
28
|
|
|
foreach ($items as $item) { |
29
|
|
|
$duplicateItem = $item->duplicate(false); |
30
|
|
|
$duplicateItem->{$thisClass.'ID'} = $this->owner->ID; |
31
|
|
|
$duplicateItem->write(); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// Duplicate many_many's |
37
|
|
|
$duplicateManyManyRelations = Config::inst()->get($thisClass, 'duplicate_many_many_relations'); |
38
|
|
|
if ($duplicateManyManyRelations && !empty($duplicateManyManyRelations)) { |
39
|
|
|
foreach ($duplicateManyManyRelations as $relation) { |
40
|
|
|
$items = $original->$relation(); |
41
|
|
|
foreach ($items as $item) { |
42
|
|
|
$this->owner->$relation()->add($item); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function onBeforeDuplicate($original, $doWrite = true) |
49
|
|
|
{ |
50
|
|
|
$thisClass = $this->owner->ClassName; |
51
|
|
|
$clearRelations = Config::inst()->get($thisClass, 'duplicate_clear_relations'); |
52
|
|
|
|
53
|
|
|
if ($clearRelations && !empty($clearRelations)) { |
54
|
|
|
foreach ($clearRelations as $clearRelation) { |
55
|
|
|
$clearRelation = $clearRelation . 'ID'; |
56
|
|
|
$this->owner->$clearRelation = 0; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|