23
23
namespace Xibo\Entity;
26
use Xibo\Exception\NotFoundException;
27
26
use Xibo\Factory\PermissionFactory;
28
use Xibo\Factory\RegionFactory;
29
27
use Xibo\Factory\WidgetFactory;
30
use Xibo\Service\DateServiceInterface;
31
use Xibo\Service\LogServiceInterface;
32
use Xibo\Storage\StorageServiceInterface;
36
* @package Xibo\Entity
40
class Playlist implements \JsonSerializable
45
* @SWG\Property(description="The ID of this Playlist")
48
32
public $playlistId;
51
* @SWG\Property(description="The userId of the User that owns this Playlist")
57
* @SWG\Property(description="The Name of the Playlist")
63
* @SWG\Property(description="An array of Tags")
69
* @SWG\Property(description="An array of Regions this Playlist is assigned to")
75
* @SWG\Property(description="An array of Widgets assigned to this Playlist")
81
* @SWG\Property(description="An array of permissions")
84
public $permissions = [];
87
* @SWG\Property(description="The display order of the Playlist when assigned to a Region")
93
* @var DateServiceInterface
98
* @var PermissionFactory
100
private $permissionFactory;
105
private $widgetFactory;
110
private $regionFactory;
113
* Entity constructor.
114
* @param StorageServiceInterface $store
115
* @param LogServiceInterface $log
116
* @param DateServiceInterface $date
117
* @param PermissionFactory $permissionFactory
118
* @param WidgetFactory $widgetFactory
120
public function __construct($store, $log, $date, $permissionFactory, $widgetFactory)
122
$this->setCommonDependencies($store, $log);
124
$this->dateService = $date;
125
$this->permissionFactory = $permissionFactory;
126
$this->widgetFactory = $widgetFactory;
128
$this->excludeProperty('regions');
132
* @param $regionFactory
135
public function setChildObjectDependencies($regionFactory)
137
$this->regionFactory = $regionFactory;
42
* The regions that this Playlist belongs to
47
public function __construct()
50
$this->widgets = array();
51
$this->tags = array();
52
$this->regionIds = array();
141
55
public function __clone()
143
57
$this->hash = null;
144
58
$this->playlistId = null;
146
$this->permissions = [];
148
60
$this->widgets = array_map(function ($object) { return clone $object; }, $this->widgets);
194
* Get Widget at Index
197
* @throws NotFoundException
199
public function getWidgetAt($index)
201
if ($index <= count($this->widgets)) {
202
$zeroBased = $index - 1;
203
if (isset($this->widgets[$zeroBased])) {
204
return $this->widgets[$zeroBased];
208
throw new NotFoundException(sprintf(__('Widget not found at index %d'), $index));
212
* @param Widget $widget
214
public function assignWidget($widget)
218
$widget->displayOrder = count($this->widgets) + 1;
219
$this->widgets[] = $widget;
106
* Assign this Playlist to a Region
107
* @param int $regionId
109
public function assignRegion($regionId)
111
if (!in_array($regionId, $this->regionIds))
112
$this->regionIds[] = $regionId;
224
* @param array $loadOptions
226
public function load($loadOptions = [])
118
public function load()
228
if ($this->playlistId == null || $this->loaded)
232
$options = array_merge([
233
'playlistIncludeRegionAssignments' => true,
234
'loadPermissions' => true,
235
'loadWidgets' => true
238
$this->getLog()->debug('Load Playlist with %s', json_encode($options));
240
120
// Load permissions
241
if ($options['loadPermissions'])
242
$this->permissions = $this->permissionFactory->getByObjectId(get_class(), $this->playlistId);
121
$this->permissions = PermissionFactory::getByObjectId(get_class(), $this->playlistId);
123
$this->widgets = WidgetFactory::getByPlaylistId($this->playlistId);
244
125
// Load the widgets
245
if ($options['loadWidgets']) {
246
foreach ($this->widgetFactory->getByPlaylistId($this->playlistId) as $widget) {
247
/* @var Widget $widget */
249
$this->widgets[] = $widget;
253
if ($options['playlistIncludeRegionAssignments']) {
254
// Load the region assignments
255
foreach ($this->regionFactory->getByPlaylistId($this->playlistId) as $region) {
256
/* @var Region $region */
257
$this->regions[] = $region;
126
foreach ($this->widgets as $widget) {
127
/* @var Widget $widget */
261
131
$this->hash = $this->hash();
262
$this->loaded = true;
268
137
public function save()
272
141
else if ($this->hash != $this->hash())
275
// Sort the widgets by their display order
276
usort($this->widgets, function($a, $b) {
281
return $a->displayOrder - $b->displayOrder;
284
// Assert the Playlist on all widgets and apply a display order
285
// this keeps the widgets in numerical order on each playlist
287
144
foreach ($this->widgets as $widget) {
288
145
/* @var Widget $widget */
291
147
// Assert the playlistId
292
148
$widget->playlistId = $this->playlistId;
293
// Assert the displayOrder
294
$widget->displayOrder = $i;
152
// Manage the assignments to regions
153
$this->linkRegions();
325
182
// Unlink regions
326
foreach ($this->regions as $region) {
327
/* @var Region $region */
328
$region->unassignPlaylist($this);
183
$this->unlinkRegions();
332
185
// Delete this playlist
333
$this->getStore()->update('DELETE FROM `playlist` WHERE playlistId = :playlistId', array('playlistId' => $this->playlistId));
186
\Xibo\Storage\PDOConnect::update('DELETE FROM `playlist` WHERE playlistId = :playlistId', array('playlistId' => $this->playlistId));
339
189
private function add()
341
$this->getLog()->debug('Adding Playlist ' . $this->name);
191
\Xibo\Helper\Log::Audit('Adding Playlist ' . $this->name);
343
193
$sql = 'INSERT INTO `playlist` (`name`, `ownerId`) VALUES (:name, :ownerId)';
344
$this->playlistId = $this->getStore()->insert($sql, array(
194
$this->playlistId = \Xibo\Storage\PDOConnect::insert($sql, array(
345
195
'name' => $this->name,
346
196
'ownerId' => $this->ownerId
353
200
private function update()
355
$this->getLog()->debug('Updating Playlist ' . $this->name . '. Id = ' . $this->playlistId);
202
\Xibo\Helper\Log::Audit('Updating Playlist ' . $this->name . '. Id = ' . $this->playlistId);
357
204
$sql = 'UPDATE `playlist` SET `name` = :name WHERE `playlistId` = :playlistId';
358
$this->getStore()->update($sql, array(
205
\Xibo\Storage\PDOConnect::update($sql, array(
359
206
'playlistId' => $this->playlistId,
360
207
'name' => $this->name
365
* Notify all Layouts of a change to this playlist
366
* This only sets the Layout Status to require a build and to update the layout modified date
367
* once the build is triggered, either from the UI or maintenance it will assess the layout
368
* and call save() if required.
369
* Layout->save() will ultimately notify the interested display groups.
371
public function notifyLayouts()
214
private function linkRegions()
373
$this->getStore()->update('
374
UPDATE `layout` SET `status` = 3, `modifiedDT` = :modifiedDt WHERE layoutId IN (
375
SELECT `region`.layoutId
376
FROM `lkregionplaylist`
378
ON region.regionId = `lkregionplaylist`.regionId
379
WHERE `lkregionplaylist`.playlistId = :playlistId
382
'playlistId' => $this->playlistId,
383
'modifiedDt' => $this->dateService->getLocalDate()
217
foreach ($this->regionIds as $regionId) {
219
\Xibo\Storage\PDOConnect::insert('INSERT INTO `lkregionplaylist` (regionId, playlistId, displayOrder) VALUES (:regionId, :playlistId, :displayOrder) ON DUPLICATE KEY UPDATE regionId = :regionId2', array(
220
'regionId' => $regionId,
221
'regionId2' => $regionId,
222
'playlistId' => $this->playlistId,
223
'displayOrder' => $order
391
public function hasLayouts()
231
private function unlinkRegions()
393
$results = $this->getStore()->select('SELECT COUNT(*) AS qty FROM `lkregionplaylist` WHERE playlistId = :playlistId', ['playlistId' => $this->playlistId]);
395
return ($results[0]['qty'] > 0);
233
foreach ($this->regionIds as $regionId) {
234
\Xibo\Storage\PDOConnect::update('DELETE FROM `lkregionplaylist` WHERE regionId = :regionId AND playlistId = :playlistId', array(
235
'regionId' => $regionId,
236
'playlistId' => $this->playlistId
b'\\ No newline at end of file'