|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Chamilo\CoreBundle\Traits\Repository\ORM; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
|
6
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
7
|
|
|
use Gedmo\Tool\Wrapper\EntityWrapper; |
|
8
|
|
|
use Gedmo\Tree\RepositoryUtils; |
|
9
|
|
|
use Gedmo\Tree\RepositoryUtilsInterface; |
|
10
|
|
|
use Gedmo\Exception\InvalidArgumentException; |
|
11
|
|
|
use Gedmo\Tree\TreeListener; |
|
12
|
|
|
|
|
13
|
|
|
trait TreeRepositoryTrait |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Tree listener on event manager |
|
17
|
|
|
* |
|
18
|
|
|
* @var TreeListener |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $listener = null; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Repository utils |
|
24
|
|
|
* |
|
25
|
|
|
* @var RepositoryUtils |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $repoUtils = null; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return EntityManager |
|
31
|
|
|
*/ |
|
32
|
|
|
abstract protected function getEntityManager(); |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return ClassMetadata |
|
36
|
|
|
*/ |
|
37
|
|
|
abstract protected function getClassMetadata(); |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* This method should be called in your repository __construct(). |
|
41
|
|
|
* Example: |
|
42
|
|
|
* |
|
43
|
|
|
* class MyTreeRepository extends EntityRepository |
|
44
|
|
|
* { |
|
45
|
|
|
* use NestedTreeRepository; // or ClosureTreeRepository, or MaterializedPathRepository. |
|
46
|
|
|
* |
|
47
|
|
|
* public function __construct(EntityManager $em, ClassMetadata $class) |
|
48
|
|
|
* { |
|
49
|
|
|
* parent::__construct($em, $class); |
|
50
|
|
|
* |
|
51
|
|
|
* $this->initializeTreeRepository($em, $class); |
|
52
|
|
|
* } |
|
53
|
|
|
* |
|
54
|
|
|
* // ... |
|
55
|
|
|
* } |
|
56
|
|
|
* |
|
57
|
|
|
*/ |
|
58
|
|
|
public function initializeTreeRepository(EntityManager $em, ClassMetadata $class) |
|
59
|
|
|
{ |
|
60
|
|
|
$treeListener = null; |
|
61
|
|
|
foreach ($em->getEventManager()->getListeners() as $listeners) { |
|
62
|
|
|
foreach ($listeners as $listener) { |
|
63
|
|
|
if ($listener instanceof TreeListener) { |
|
64
|
|
|
$treeListener = $listener; |
|
65
|
|
|
break; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
if ($treeListener) { |
|
69
|
|
|
break; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (null === $treeListener) { |
|
74
|
|
|
throw new \Gedmo\Exception\InvalidMappingException('Tree listener was not found on your entity manager, it must be hooked into the event manager'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$this->listener = $treeListener; |
|
78
|
|
|
if (!$this->validate()) { |
|
79
|
|
|
throw new \Gedmo\Exception\InvalidMappingException('This repository cannot be used for tree type: '.$treeListener->getStrategy($em, $class->name)->getName()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$this->repoUtils = new RepositoryUtils($this->getEntityManager(), $this->getClassMetadata(), $this->listener, $this); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return \Doctrine\ORM\QueryBuilder |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function getQueryBuilder() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->getEntityManager()->createQueryBuilder(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Sets the RepositoryUtilsInterface instance |
|
95
|
|
|
* |
|
96
|
|
|
* @param \Gedmo\Tree\RepositoryUtilsInterface $repoUtils |
|
97
|
|
|
* |
|
98
|
|
|
* @return static |
|
99
|
|
|
*/ |
|
100
|
|
|
public function setRepoUtils(RepositoryUtilsInterface $repoUtils) |
|
101
|
|
|
{ |
|
102
|
|
|
$this->repoUtils = $repoUtils; |
|
103
|
|
|
|
|
104
|
|
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Returns the RepositoryUtilsInterface instance |
|
109
|
|
|
* |
|
110
|
|
|
* @return \Gedmo\Tree\RepositoryUtilsInterface|null |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getRepoUtils() |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->repoUtils; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* {@inheritDoc} |
|
119
|
|
|
*/ |
|
120
|
|
|
public function childCount($node = null, $direct = false) |
|
121
|
|
|
{ |
|
122
|
|
|
$meta = $this->getClassMetadata(); |
|
123
|
|
|
|
|
124
|
|
|
if (is_object($node)) { |
|
125
|
|
|
if (!($node instanceof $meta->name)) { |
|
126
|
|
|
throw new InvalidArgumentException("Node is not related to this repository"); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$wrapped = new EntityWrapper($node, $this->getEntityManager()); |
|
130
|
|
|
|
|
131
|
|
|
if (!$wrapped->hasValidIdentifier()) { |
|
132
|
|
|
throw new InvalidArgumentException("Node is not managed by UnitOfWork"); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$qb = $this->getChildrenQueryBuilder($node, $direct); |
|
137
|
|
|
|
|
138
|
|
|
// We need to remove the ORDER BY DQL part since some vendors could throw an error |
|
139
|
|
|
// in count queries |
|
140
|
|
|
$dqlParts = $qb->getDQLParts(); |
|
141
|
|
|
|
|
142
|
|
|
// We need to check first if there's an ORDER BY DQL part, because resetDQLPart doesn't |
|
143
|
|
|
// check if its internal array has an "orderby" index |
|
144
|
|
|
if (isset($dqlParts['orderBy'])) { |
|
145
|
|
|
$qb->resetDQLPart('orderBy'); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
$aliases = $qb->getRootAliases(); |
|
149
|
|
|
$alias = $aliases[0]; |
|
150
|
|
|
|
|
151
|
|
|
$qb->select('COUNT('.$alias.')'); |
|
152
|
|
|
|
|
153
|
|
|
return (int) $qb->getQuery()->getSingleScalarResult(); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @see \Gedmo\Tree\RepositoryUtilsInterface::childrenHierarchy |
|
158
|
|
|
*/ |
|
159
|
|
|
public function childrenHierarchy($node = null, $direct = false, array $options = array(), $includeNode = false) |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->repoUtils->childrenHierarchy($node, $direct, $options, $includeNode); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @see \Gedmo\Tree\RepositoryUtilsInterface::buildTree |
|
166
|
|
|
*/ |
|
167
|
|
|
public function buildTree(array $nodes, array $options = array()) |
|
168
|
|
|
{ |
|
169
|
|
|
return $this->repoUtils->buildTree($nodes, $options); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @see \Gedmo\Tree\RepositoryUtilsInterface::buildTreeArray |
|
174
|
|
|
*/ |
|
175
|
|
|
public function buildTreeArray(array $nodes) |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->repoUtils->buildTreeArray($nodes); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @see \Gedmo\Tree\RepositoryUtilsInterface::setChildrenIndex |
|
182
|
|
|
*/ |
|
183
|
|
|
public function setChildrenIndex($childrenIndex) |
|
184
|
|
|
{ |
|
185
|
|
|
$this->repoUtils->setChildrenIndex($childrenIndex); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @see \Gedmo\Tree\RepositoryUtilsInterface::getChildrenIndex |
|
190
|
|
|
*/ |
|
191
|
|
|
public function getChildrenIndex() |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->repoUtils->getChildrenIndex(); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Checks if current repository is right |
|
198
|
|
|
* for currently used tree strategy |
|
199
|
|
|
* |
|
200
|
|
|
* @return bool |
|
201
|
|
|
*/ |
|
202
|
|
|
abstract protected function validate(); |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Get all root nodes query builder |
|
206
|
|
|
* |
|
207
|
|
|
* @param string - Sort by field |
|
|
|
|
|
|
208
|
|
|
* @param string - Sort direction ("asc" or "desc") |
|
209
|
|
|
* |
|
210
|
|
|
* @return \Doctrine\ORM\QueryBuilder - QueryBuilder object |
|
211
|
|
|
*/ |
|
212
|
|
|
abstract public function getRootNodesQueryBuilder($sortByField = null, $direction = 'asc'); |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Get all root nodes query |
|
216
|
|
|
* |
|
217
|
|
|
* @param string - Sort by field |
|
|
|
|
|
|
218
|
|
|
* @param string - Sort direction ("asc" or "desc") |
|
219
|
|
|
* |
|
220
|
|
|
* @return \Doctrine\ORM\Query - Query object |
|
221
|
|
|
*/ |
|
222
|
|
|
abstract public function getRootNodesQuery($sortByField = null, $direction = 'asc'); |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Returns a QueryBuilder configured to return an array of nodes suitable for buildTree method |
|
226
|
|
|
* |
|
227
|
|
|
* @param object $node - Root node |
|
228
|
|
|
* @param bool $direct - Obtain direct children? |
|
229
|
|
|
* @param array $options - Options |
|
230
|
|
|
* @param boolean $includeNode - Include node in results? |
|
231
|
|
|
* |
|
232
|
|
|
* @return \Doctrine\ORM\QueryBuilder - QueryBuilder object |
|
233
|
|
|
*/ |
|
234
|
|
|
abstract public function getNodesHierarchyQueryBuilder($node = null, $direct = false, array $options = array(), $includeNode = false); |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Returns a Query configured to return an array of nodes suitable for buildTree method |
|
238
|
|
|
* |
|
239
|
|
|
* @param object $node - Root node |
|
240
|
|
|
* @param bool $direct - Obtain direct children? |
|
241
|
|
|
* @param array $options - Options |
|
242
|
|
|
* @param boolean $includeNode - Include node in results? |
|
243
|
|
|
* |
|
244
|
|
|
* @return \Doctrine\ORM\Query - Query object |
|
245
|
|
|
*/ |
|
246
|
|
|
abstract public function getNodesHierarchyQuery($node = null, $direct = false, array $options = array(), $includeNode = false); |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Get list of children followed by given $node. This returns a QueryBuilder object |
|
250
|
|
|
* |
|
251
|
|
|
* @param object $node - if null, all tree nodes will be taken |
|
252
|
|
|
* @param boolean $direct - true to take only direct children |
|
253
|
|
|
* @param string $sortByField - field name to sort by |
|
254
|
|
|
* @param string $direction - sort direction : "ASC" or "DESC" |
|
255
|
|
|
* @param bool $includeNode - Include the root node in results? |
|
256
|
|
|
* |
|
257
|
|
|
* @return \Doctrine\ORM\QueryBuilder - QueryBuilder object |
|
258
|
|
|
*/ |
|
259
|
|
|
abstract public function getChildrenQueryBuilder($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false); |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Get list of children followed by given $node. This returns a Query |
|
263
|
|
|
* |
|
264
|
|
|
* @param object $node - if null, all tree nodes will be taken |
|
265
|
|
|
* @param boolean $direct - true to take only direct children |
|
266
|
|
|
* @param string $sortByField - field name to sort by |
|
267
|
|
|
* @param string $direction - sort direction : "ASC" or "DESC" |
|
268
|
|
|
* @param bool $includeNode - Include the root node in results? |
|
269
|
|
|
* |
|
270
|
|
|
* @return \Doctrine\ORM\Query - Query object |
|
271
|
|
|
*/ |
|
272
|
|
|
abstract public function getChildrenQuery($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false); |
|
273
|
|
|
} |
|
274
|
|
|
|