|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
ÁTICA - Aplicación web para la gestión documental de centros educativos |
|
4
|
|
|
|
|
5
|
|
|
Copyright (C) 2015-2017: Luis Ramón López López |
|
6
|
|
|
|
|
7
|
|
|
This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
it under the terms of the GNU Affero General Public License as published by |
|
9
|
|
|
the Free Software Foundation, either version 3 of the License, or |
|
10
|
|
|
(at your option) any later version. |
|
11
|
|
|
|
|
12
|
|
|
This program is distributed in the hope that it will be useful, |
|
13
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
GNU Affero General Public License for more details. |
|
16
|
|
|
|
|
17
|
|
|
You should have received a copy of the GNU Affero General Public License |
|
18
|
|
|
along with this program. If not, see [http://www.gnu.org/licenses/]. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace AppBundle\Repository; |
|
22
|
|
|
|
|
23
|
|
|
use AppBundle\Entity\Documentation\Folder; |
|
24
|
|
|
use AppBundle\Entity\Element; |
|
25
|
|
|
use AppBundle\Entity\Organization; |
|
26
|
|
|
use AppBundle\Entity\User; |
|
27
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
28
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
29
|
|
|
use Gedmo\Tree\Entity\Repository\NestedTreeRepository; |
|
30
|
|
|
|
|
31
|
|
|
class ElementRepository extends NestedTreeRepository |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @param Organization $organization |
|
35
|
|
|
* @return null|object |
|
36
|
|
|
*/ |
|
37
|
|
|
public function findCurrentOneByOrganization(Organization $organization) |
|
38
|
|
|
{ |
|
39
|
|
|
$item = $organization->getElement() ?: $this->findOneBy(['organization' => $organization, 'parent' => null], ['left' => 'DESC']); |
|
40
|
|
|
|
|
41
|
|
|
return $item; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param Organization $organization |
|
46
|
|
|
* @param string $rootName |
|
47
|
|
|
* @return null|object |
|
48
|
|
|
*/ |
|
49
|
|
|
public function findOneByOrganizationAndRootName(Organization $organization, $rootName) |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->findOneBy(['organization' => $organization, 'name' => $rootName, 'parent' => null]); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param Organization $organization |
|
56
|
|
|
* @param string $path |
|
57
|
|
|
* @return null|object |
|
58
|
|
|
*/ |
|
59
|
|
|
public function findOneByOrganizationAndPath(Organization $organization, $path) |
|
60
|
|
|
{ |
|
61
|
|
|
if (!$path) { |
|
62
|
|
|
return null; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$items = explode('/', $path); |
|
66
|
|
|
$itemName = array_shift($items); |
|
67
|
|
|
$current = null; |
|
68
|
|
|
|
|
69
|
|
|
while ($itemName) { |
|
70
|
|
|
$current = $this->findOneBy(['organization' => $organization, 'name' => $itemName, 'parent' => $current]); |
|
71
|
|
|
$itemName = array_shift($items); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $current; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param Organization $organization |
|
79
|
|
|
* @param string $code |
|
80
|
|
|
* @return null|object |
|
81
|
|
|
*/ |
|
82
|
|
|
public function findOneByOrganizationAndCurrentCode(Organization $organization, $code) |
|
83
|
|
|
{ |
|
84
|
|
|
$item = $this->findCurrentOneByOrganization($organization); |
|
85
|
|
|
|
|
86
|
|
|
if (null === $item) { |
|
87
|
|
|
return null; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $this->getChildrenQueryBuilder($item) |
|
91
|
|
|
->andWhere('node.code = :code') |
|
92
|
|
|
->setParameter('code', $code) |
|
93
|
|
|
->getQuery() |
|
94
|
|
|
->getOneOrNullResult(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param Element[] $elements |
|
99
|
|
|
* @param boolean $final |
|
100
|
|
|
* |
|
101
|
|
|
* @return QueryBuilder |
|
102
|
|
|
*/ |
|
103
|
|
|
public function findAllSubProfilesQueryBuilder(array $elements, $final = false) { |
|
104
|
|
|
$collection = new ArrayCollection(); |
|
105
|
|
|
|
|
106
|
|
|
foreach ($elements as $profile) { |
|
107
|
|
|
$children = $this->getChildren($profile, false, null, 'ASC', true); |
|
108
|
|
|
foreach ($children as $element) { |
|
109
|
|
|
if (!$collection->contains($element)) { |
|
110
|
|
|
$collection->add($element); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$qb = $this->createQueryBuilder('e') |
|
116
|
|
|
->andWhere('e IN (:profiles)') |
|
117
|
|
|
->orderBy('e.left') |
|
118
|
|
|
->setParameter('profiles', $collection); |
|
119
|
|
|
|
|
120
|
|
|
if ($final) { |
|
121
|
|
|
$qb = $qb |
|
122
|
|
|
->andWhere('e.left = e.right - 1'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $qb; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param Organization $organization |
|
130
|
|
|
* @return QueryBuilder |
|
131
|
|
|
*/ |
|
132
|
|
|
public function findAllProfilesByOrganizationQueryBuilder(Organization $organization) |
|
133
|
|
|
{ |
|
134
|
|
|
$profileElements = $this->createQueryBuilder('e') |
|
135
|
|
|
->innerJoin('e.profile', 'p') |
|
136
|
|
|
->where('e.organization = :organization') |
|
137
|
|
|
->setParameter('organization', $organization) |
|
138
|
|
|
->getQuery() |
|
139
|
|
|
->getResult(); |
|
140
|
|
|
|
|
141
|
|
|
return $this->findAllSubProfilesQueryBuilder($profileElements); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param Folder $folder |
|
146
|
|
|
* @param integer $permission |
|
147
|
|
|
* @param boolean $final |
|
148
|
|
|
* |
|
149
|
|
|
* @return QueryBuilder |
|
150
|
|
|
*/ |
|
151
|
|
|
public function findAllProfilesByFolderPermissionQueryBuilder(Folder $folder, $permission, $final = false) |
|
152
|
|
|
{ |
|
153
|
|
|
$profileElements = $this->getEntityManager()->createQuery( |
|
154
|
|
|
'SELECT e FROM AppBundle:Element e WHERE e IN (SELECT DISTINCT IDENTITY(fp.element) FROM AppBundle:Documentation\FolderPermission fp WHERE fp.folder = :folder AND fp.permission = :permission)') |
|
155
|
|
|
->setParameter('folder', $folder) |
|
156
|
|
|
->setParameter('permission', $permission) |
|
157
|
|
|
->getResult(); |
|
158
|
|
|
|
|
159
|
|
|
return $this->findAllSubProfilesQueryBuilder($profileElements, $final); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param Folder $folder |
|
164
|
|
|
* @param int $permission |
|
165
|
|
|
* @param User $user |
|
166
|
|
|
* @param boolean $final |
|
167
|
|
|
* |
|
168
|
|
|
* @return Element[] |
|
169
|
|
|
*/ |
|
170
|
|
|
public function findAllProfilesByFolderPermissionAndUser(Folder $folder, $permission, User $user, $final = false) |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->findAllProfilesByFolderPermissionQueryBuilder($folder, $permission, $final) |
|
173
|
|
|
->innerJoin('AppBundle:Role', 'r', 'WITH', 'r.element = e') |
|
174
|
|
|
->join('r.user', 'u') |
|
175
|
|
|
->andWhere('r.user = :user') |
|
176
|
|
|
->setParameter('user', $user) |
|
177
|
|
|
->getQuery()->getResult(); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param Folder $folder |
|
182
|
|
|
* @param integer $permission |
|
183
|
|
|
* @param boolean $final |
|
184
|
|
|
* |
|
185
|
|
|
* @return Element[] |
|
186
|
|
|
*/ |
|
187
|
|
|
public function findAllProfilesByFolderPermission(Folder $folder, $permission, $final = false) |
|
188
|
|
|
{ |
|
189
|
|
|
return $this->findAllProfilesByFolderPermissionQueryBuilder($folder, $permission, $final)->getQuery()->getResult(); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @param Element $element |
|
194
|
|
|
* |
|
195
|
|
|
* @return Element[] |
|
196
|
|
|
*/ |
|
197
|
|
|
public function findAllAncestorProfiles(Element $element) { |
|
198
|
|
|
$elements = [$element]; |
|
199
|
|
|
|
|
200
|
|
|
while ($element->getParent() && $element->getProfile() === null) { |
|
201
|
|
|
$element = $element->getParent(); |
|
202
|
|
|
$elements[] = $element; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
return $elements; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @param Element[] $elements |
|
210
|
|
|
* @return Element[] |
|
211
|
|
|
*/ |
|
212
|
|
|
public function findAllAncestorProfilesInArray(array $elements) { |
|
213
|
|
|
$results = []; |
|
214
|
|
|
|
|
215
|
|
|
foreach ($elements as $element) { |
|
216
|
|
|
$results[] = $this->findAllAncestorProfiles($element); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
return array_unique(call_user_func_array('array_merge', $results)); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* @param User $user |
|
224
|
|
|
* @param Organization $organization |
|
225
|
|
|
* @return Element[] |
|
226
|
|
|
*/ |
|
227
|
|
|
public function findAllProfilesByUserAndOrganization(User $user, Organization $organization) |
|
228
|
|
|
{ |
|
229
|
|
|
$profiles = $this->getEntityManager()->createQuery( |
|
230
|
|
|
'SELECT e FROM AppBundle:Element e WHERE e.organization = :organization AND e.id IN ( |
|
231
|
|
|
SELECT IDENTITY(r.element) FROM AppBundle:Role r WHERE r.user = :user |
|
232
|
|
|
)') |
|
233
|
|
|
->setParameter('organization', $organization) |
|
234
|
|
|
->setParameter('user', $user) |
|
235
|
|
|
->getResult(); |
|
236
|
|
|
|
|
237
|
|
|
return $this->getEntityManager()->getRepository('AppBundle:Element') |
|
238
|
|
|
->findAllAncestorProfilesInArray($profiles); |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|