1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
<?php
/*
* Spring Signage Ltd - http://www.springsignage.com
* Copyright (C) 2015 Spring Signage Ltd
* (DisplayGroup.php)
*/
namespace Xibo\Factory;
use Xibo\Entity\DisplayGroup;
use Xibo\Entity\User;
use Xibo\Exception\NotFoundException;
use Xibo\Service\LogServiceInterface;
use Xibo\Service\SanitizerServiceInterface;
use Xibo\Storage\StorageServiceInterface;
/**
* Class DisplayGroupFactory
* @package Xibo\Factory
*/
class DisplayGroupFactory extends BaseFactory
{
/**
* @var PermissionFactory
*/
private $permissionFactory;
/**
* @var TagFactory
*/
private $tagFactory;
/**
* Construct a factory
* @param StorageServiceInterface $store
* @param LogServiceInterface $log
* @param SanitizerServiceInterface $sanitizerService
* @param User $user
* @param UserFactory $userFactory
* @param PermissionFactory $permissionFactory
* @param TagFactory $tagFactory
*/
public function __construct($store, $log, $sanitizerService, $user, $userFactory, $permissionFactory, $tagFactory)
{
$this->setCommonDependencies($store, $log, $sanitizerService);
$this->setAclDependencies($user, $userFactory);
$this->permissionFactory = $permissionFactory;
$this->tagFactory = $tagFactory;
}
/**
* Create Empty
* @return DisplayGroup
*/
public function createEmpty()
{
return new DisplayGroup(
$this->getStore(),
$this->getLog(),
$this,
$this->permissionFactory,
$this->tagFactory
);
}
/**
* @param int $displayGroupId
* @return DisplayGroup
* @throws NotFoundException
*/
public function getById($displayGroupId)
{
$groups = $this->query(null, ['disableUserCheck' => 1, 'displayGroupId' => $displayGroupId, 'isDisplaySpecific' => -1]);
if (count($groups) <= 0)
throw new NotFoundException();
return $groups[0];
}
/**
* @param int $displayId
* @return array[DisplayGroup]
*/
public function getByDisplayId($displayId)
{
return $this->query(null, ['disableUserCheck' => 1, 'displayId' => $displayId, 'isDisplaySpecific' => -1]);
}
/**
* Get Display Groups by MediaId
* @param int $mediaId
* @return array[DisplayGroup]
*/
public function getByMediaId($mediaId)
{
return $this->query(null, ['disableUserCheck' => 1, 'mediaId' => $mediaId, 'isDisplaySpecific' => -1]);
}
/**
* Get Display Groups by eventId
* @param int $eventId
* @return array[DisplayGroup]
*/
public function getByEventId($eventId)
{
return $this->query(null, ['disableUserCheck' => 1, 'eventId' => $eventId, 'isDisplaySpecific' => -1]);
}
/**
* Get Display Groups by isDynamic
* @param int $isDynamic
* @return array[DisplayGroup]
*/
public function getByIsDynamic($isDynamic)
{
return $this->query(null, ['disableUserCheck' => 1, 'isDynamic' => $isDynamic]);
}
/**
* Get Display Groups by their ParentId
* @param int $parentId
* @return array[DisplayGroup]
*/
public function getByParentId($parentId)
{
return $this->query(null, ['disableUserCheck' => 1, 'parentId' => $parentId]);
}
/**
* Get Relationship Tree
* @param $displayGroupId
* @return DisplayGroup[]
*/
public function getRelationShipTree($displayGroupId)
{
$tree = [];
foreach ($this->getStore()->select('
SELECT `displaygroup`.displayGroupId, `displaygroup`.displayGroup, depth, 1 AS level
FROM `lkdgdg`
INNER JOIN `displaygroup`
ON `lkdgdg`.childId = `displaygroup`.displayGroupId
WHERE `lkdgdg`.parentId = :displayGroupId
UNION ALL
SELECT `displaygroup`.displayGroupId, `displaygroup`.displayGroup, depth * -1, 0 AS level
FROM `lkdgdg`
INNER JOIN `displaygroup`
ON `lkdgdg`.parentId = `displaygroup`.displayGroupId
WHERE `lkdgdg`.childId = :displayGroupId AND `lkdgdg`.parentId <> :displayGroupId
ORDER BY level, depth, displayGroup
', [
'displayGroupId' => $displayGroupId
]) as $row) {
$item = $this->createEmpty()->hydrate($row);
$item->depth = intval($row['depth']);
$item->level = intval($row['level']);
$tree[] = $item;
}
return $tree;
}
/**
* Get Display Groups assigned to Notifications
* @param int $notificationId
* @return array[DisplayGroup]
*/
public function getByNotificationId($notificationId)
{
return $this->query(null, ['disableUserCheck' => 1, 'notificationId' => $notificationId, 'isDisplaySpecific' => -1]);
}
/**
* @param array $sortOrder
* @param array $filterBy
* @return array[DisplayGroup]
*/
public function query($sortOrder = null, $filterBy = [])
{
if ($sortOrder == null)
$sortOrder = ['displayGroup'];
$entries = [];
$params = [];
$select = '
SELECT `displaygroup`.displayGroupId,
`displaygroup`.displayGroup,
`displaygroup`.isDisplaySpecific,
`displaygroup`.description,
`displaygroup`.isDynamic,
`displaygroup`.dynamicCriteria,
`displaygroup`.userId,
(
SELECT GROUP_CONCAT(DISTINCT tag)
FROM tag
INNER JOIN lktagdisplaygroup
ON lktagdisplaygroup.tagId = tag.tagId
WHERE lktagdisplaygroup.displayGroupId = displaygroup.displayGroupID
GROUP BY lktagdisplaygroup.displayGroupId
) AS tags
';
$body = '
FROM `displaygroup`
';
if ($this->getSanitizer()->getInt('mediaId', $filterBy) !== null) {
$body .= '
INNER JOIN lkmediadisplaygroup
ON lkmediadisplaygroup.displayGroupId = `displaygroup`.displayGroupId
AND lkmediadisplaygroup.mediaId = :mediaId
';
$params['mediaId'] = $this->getSanitizer()->getInt('mediaId', $filterBy);
}
if ($this->getSanitizer()->getInt('eventId', $filterBy) !== null) {
$body .= '
INNER JOIN `lkscheduledisplaygroup`
ON `lkscheduledisplaygroup`.displayGroupId = `displaygroup`.displayGroupId
AND `lkscheduledisplaygroup`.eventId = :eventId
';
$params['eventId'] = $this->getSanitizer()->getInt('eventId', $filterBy);
}
$body .= ' WHERE 1 = 1 ';
// View Permissions
$this->viewPermissionSql('Xibo\Entity\DisplayGroup', $body, $params, '`displaygroup`.displayGroupId', '`displaygroup`.userId', $filterBy);
if ($this->getSanitizer()->getInt('displayGroupId', $filterBy) !== null) {
$body .= ' AND displaygroup.displayGroupId = :displayGroupId ';
$params['displayGroupId'] = $this->getSanitizer()->getInt('displayGroupId', $filterBy);
}
if ($this->getSanitizer()->getInt('parentId', $filterBy) !== null) {
$body .= ' AND `displaygroup`.displayGroupId IN (SELECT `childId` FROM `lkdgdg` WHERE `parentId` = :parentId AND `depth` = 1) ';
$params['parentId'] = $this->getSanitizer()->getInt('parentId', $filterBy);
}
if ($this->getSanitizer()->getInt('isDisplaySpecific', 0, $filterBy) != -1) {
$body .= ' AND displaygroup.isDisplaySpecific = :isDisplaySpecific ';
$params['isDisplaySpecific'] = $this->getSanitizer()->getInt('isDisplaySpecific', 0, $filterBy);
}
if ($this->getSanitizer()->getInt('isDynamic', $filterBy) !== null) {
$body .= ' AND `displaygroup`.isDynamic = :isDynamic ';
$params['isDynamic'] = $this->getSanitizer()->getInt('isDynamic', $filterBy);
}
if ($this->getSanitizer()->getString('dynamicCriteria', $filterBy) !== null) {
$body .= ' AND `displaygroup`.dynamicCriteria = :dynamicCriteria ';
$params['dynamicCriteria'] = $this->getSanitizer()->getString('dynamicCriteria', $filterBy);
}
if ($this->getSanitizer()->getInt('displayId', $filterBy) !== null) {
$body .= ' AND displaygroup.displayGroupId IN (SELECT displayGroupId FROM lkdisplaydg WHERE displayId = :displayId) ';
$params['displayId'] = $this->getSanitizer()->getInt('displayId', $filterBy);
}
if ($this->getSanitizer()->getInt('nestedDisplayId', $filterBy) !== null) {
$body .= '
AND displaygroup.displayGroupId IN (
SELECT DISTINCT parentId
FROM `lkdgdg`
INNER JOIN `lkdisplaydg`
ON `lkdisplaydg`.displayGroupId = `lkdgdg`.childId
WHERE displayId = :nestedDisplayId
)
';
$params['nestedDisplayId'] = $this->getSanitizer()->getInt('nestedDisplayId', $filterBy);
}
if ($this->getSanitizer()->getInt('notificationId', $filterBy) !== null) {
$body .= ' AND displaygroup.displayGroupId IN (SELECT displayGroupId FROM `lknotificationdg` WHERE notificationId = :notificationId) ';
$params['notificationId'] = $this->getSanitizer()->getInt('notificationId', $filterBy);
}
// Filter by DisplayGroup Name?
if ($this->getSanitizer()->getString('displayGroup', $filterBy) != null) {
// convert into a space delimited array
$names = explode(' ', $this->getSanitizer()->getString('displayGroup', $filterBy));
$i = 0;
foreach ($names as $searchName) {
$i++;
// Not like, or like?
if (substr($searchName, 0, 1) == '-') {
$body .= " AND `displaygroup`.displayGroup NOT LIKE :search$i ";
$params['search' . $i] = '%' . ltrim(($searchName), '-') . '%';
}
else {
$body .= " AND `displaygroup`.displayGroup LIKE :search$i ";
$params['search' . $i] = '%' . $searchName . '%';
}
}
}
// Tags
if ($this->getSanitizer()->getString('tags', $filterBy) != '') {
$tagFilter = $this->getSanitizer()->getString('tags', $filterBy);
if (trim($tagFilter) === '--no-tag') {
$body .= ' AND `displaygroup`.displaygroupId NOT IN (
SELECT `lktagdisplaygroup`.displaygroupId
FROM tag
INNER JOIN `lktagdisplaygroup`
ON `lktagdisplaygroup`.tagId = tag.tagId
)
';
} else {
$operator = $this->getSanitizer()->getCheckbox('exactTags') == 1 ? '=' : 'LIKE';
$body .= " AND `displaygroup`.displaygroupId IN (
SELECT `lktagdisplaygroup`.displaygroupId
FROM tag
INNER JOIN `lktagdisplaygroup`
ON `lktagdisplaygroup`.tagId = tag.tagId
";
$i = 0;
foreach (explode(',', $tagFilter) as $tag) {
$i++;
if ($i == 1)
$body .= ' WHERE `tag` ' . $operator . ' :tags' . $i;
else
$body .= ' OR `tag` ' . $operator . ' :tags' . $i;
if ($operator === '=')
$params['tags' . $i] = $tag;
else
$params['tags' . $i] = '%' . $tag . '%';
}
$body .= " ) ";
}
}
// Sorting?
$order = '';
if (is_array($sortOrder))
$order .= 'ORDER BY ' . implode(',', $sortOrder);
$limit = '';
// Paging
if ($filterBy !== null && $this->getSanitizer()->getInt('start', $filterBy) !== null && $this->getSanitizer()->getInt('length', $filterBy) !== null) {
$limit = ' LIMIT ' . intval($this->getSanitizer()->getInt('start', $filterBy), 0) . ', ' . $this->getSanitizer()->getInt('length', 10, $filterBy);
}
$sql = $select . $body . $order . $limit;
foreach ($this->getStore()->select($sql, $params) as $row) {
$entries[] = $this->createEmpty()->hydrate($row, ['intProperties' => ['isDisplaySpecific', 'isDynamic']]);
}
// Paging
if ($limit != '' && count($entries) > 0) {
$results = $this->getStore()->select('SELECT COUNT(*) AS total ' . $body, $params);
$this->_countLast = intval($results[0]['total']);
}
return $entries;
}
}
|