|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Blast Project package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (C) 2015-2017 Libre Informatique |
|
7
|
|
|
* |
|
8
|
|
|
* This file is licenced under the GNU LGPL v3. |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Blast\Bundle\CoreBundle\Admin\Traits; |
|
14
|
|
|
|
|
15
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
|
16
|
|
|
|
|
17
|
|
|
trait CollectionsManager |
|
18
|
|
|
{ |
|
19
|
|
|
protected $managedCollections = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* function getManagedCollections. |
|
23
|
|
|
* |
|
24
|
|
|
* @return array |
|
25
|
|
|
* */ |
|
26
|
|
|
public function getManagedCollections() |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->managedCollections; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* function addManagedCollections. |
|
33
|
|
|
* |
|
34
|
|
|
* @param $collections array or string, describing the collections to manage |
|
35
|
|
|
* |
|
36
|
|
|
* @return CoreAdmin $this |
|
37
|
|
|
* */ |
|
38
|
|
|
public function addManagedCollections($collections) |
|
39
|
|
|
{ |
|
40
|
|
|
if (!is_array($collections)) { |
|
41
|
|
|
$collections = array($collections); |
|
42
|
|
|
} |
|
43
|
|
|
$this->managedCollections = array_merge($this->managedCollections, $collections); |
|
44
|
|
|
|
|
45
|
|
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function prePersistCollectionsManager($object) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->preUpdateOrPersistCollectionsManager($object); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function preUpdateCollectionsManager($object) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->preUpdateOrPersistCollectionsManager($object); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
protected function preUpdateOrPersistCollectionsManager($object) |
|
59
|
|
|
{ |
|
60
|
|
|
// global configuration |
|
61
|
|
|
$this->configureCollectionsManager(); |
|
62
|
|
|
|
|
63
|
|
|
// for each given collection |
|
64
|
|
|
foreach ($this->managedCollections as $coll) { |
|
65
|
|
|
if (isset($this->formFieldDescriptions[$coll])) { |
|
66
|
|
|
if ($coll == 'packagings') { |
|
67
|
|
|
dump($this->formFieldDescriptions[$coll]); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
// preparing stuff |
|
70
|
|
|
if ($admin_code = $this->formFieldDescriptions[$coll]->getOption('admin_code')) { |
|
71
|
|
|
$targetAdmin = $this->getConfigurationPool()->getAdminByAdminCode($admin_code); |
|
|
|
|
|
|
72
|
|
|
} else { |
|
73
|
|
|
$target = $this->getModelManager() |
|
|
|
|
|
|
74
|
|
|
->getEntityManager($object) |
|
75
|
|
|
->getClassMetadata($this->getClass()) |
|
|
|
|
|
|
76
|
|
|
->associationMappings[$coll]['targetEntity'] |
|
77
|
|
|
; |
|
78
|
|
|
|
|
79
|
|
|
$rctarget = new \ReflectionClass($target); |
|
80
|
|
|
$targetAdmin = $this->getConfigurationPool()->getAdminByClass($rctarget->getName()); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$rcentity = new \ReflectionClass($this->getClass()); |
|
|
|
|
|
|
84
|
|
|
$method = 'get' . ucfirst($coll); |
|
85
|
|
|
|
|
86
|
|
|
$subObjects = $object->$method(); |
|
87
|
|
|
|
|
88
|
|
|
// insert/update (forcing the foreign key to be set to $this->getId(), for instance) |
|
89
|
|
|
if ($subObjects != null) { |
|
90
|
|
|
foreach ($subObjects as $subobj) { |
|
91
|
|
|
if ($this->formFieldDescriptions[$coll]->getMappingType() != ClassMetadataInfo::MANY_TO_MANY) { |
|
92
|
|
|
$subobj->{'set' . ucfirst($rcentity->getShortName())}($object); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if ($targetAdmin != null) { |
|
96
|
|
|
$targetAdmin->prePersist($subobj); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if (!$subObjects instanceof Doctrine\ORM\PersitentCollection || $subObjects->count() == 0) { |
|
|
|
|
|
|
102
|
|
|
continue; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
// delete |
|
106
|
|
|
foreach ($subObjects->getSnapshot() as $subobj) { |
|
107
|
|
|
if (!$subObjects->contains($subobj)) { |
|
108
|
|
|
$this->getModelManager()->delete($subobj); |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
View Code Duplication |
private function configureCollectionsManager() |
|
|
|
|
|
|
118
|
|
|
{ |
|
119
|
|
|
$blast = $this->getConfigurationPool()->getContainer()->getParameter('blast'); |
|
|
|
|
|
|
120
|
|
|
$key = 'collections'; // name of the key in the blast.yml |
|
121
|
|
|
// merge configuration/parameters |
|
122
|
|
|
foreach ($this->getCurrentComposition() as $class) { |
|
|
|
|
|
|
123
|
|
|
if (isset($blast[$class]) && isset($blast[$class]['manage']) && isset($blast[$class]['manage'][$key])) { |
|
124
|
|
|
if (!is_array($blast[$class]['manage'][$key])) { |
|
125
|
|
|
$blast[$class]['manage'][$key] = [$blast[$class]['manage'][$key]]; |
|
126
|
|
|
} |
|
127
|
|
|
$this->addManagedCollections($blast[$class]['manage'][$key]); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: