~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Entity/Playlist.php

  • Committer: Dan Garner
  • Date: 2015-03-26 14:08:33 UTC
  • Revision ID: git-v1:70d14044444f8dc5d602b99890d59dea46d9470c
Moved web servable files to web folder

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
namespace Xibo\Entity;
24
24
 
25
25
 
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;
33
28
 
34
 
/**
35
 
 * Class Playlist
36
 
 * @package Xibo\Entity
37
 
 *
38
 
 * @SWG\Definition()
39
 
 */
40
 
class Playlist implements \JsonSerializable
 
29
class Playlist
41
30
{
42
 
    use EntityTrait;
43
 
 
44
 
    /**
45
 
     * @SWG\Property(description="The ID of this Playlist")
46
 
     * @var int
47
 
     */
 
31
    private $hash;
48
32
    public $playlistId;
49
 
 
50
 
    /**
51
 
     * @SWG\Property(description="The userId of the User that owns this Playlist")
52
 
     * @var int
53
 
     */
54
33
    public $ownerId;
55
34
 
56
 
    /**
57
 
     * @SWG\Property(description="The Name of the Playlist")
58
 
     * @var string
59
 
     */
60
35
    public $name;
61
36
 
62
 
    /**
63
 
     * @SWG\Property(description="An array of Tags")
64
 
     * @var Tag[]
65
 
     */
66
 
    public $tags = [];
67
 
 
68
 
    /**
69
 
     * @SWG\Property(description="An array of Regions this Playlist is assigned to")
70
 
     * @var Region[]
71
 
     */
72
 
    public $regions = [];
73
 
 
74
 
    /**
75
 
     * @SWG\Property(description="An array of Widgets assigned to this Playlist")
76
 
     * @var Widget[]
77
 
     */
78
 
    public $widgets = [];
79
 
 
80
 
    /**
81
 
     * @SWG\Property(description="An array of permissions")
82
 
     * @var Permission[]
83
 
     */
84
 
    public $permissions = [];
85
 
 
86
 
    /**
87
 
     * @SWG\Property(description="The display order of the Playlist when assigned to a Region")
88
 
     * @var int
89
 
     */
90
 
    public $displayOrder;
91
 
 
92
 
    /**
93
 
     * @var DateServiceInterface
94
 
     */
95
 
    private $dateService;
96
 
 
97
 
    /**
98
 
     * @var PermissionFactory
99
 
     */
100
 
    private $permissionFactory;
101
 
 
102
 
    /**
103
 
     * @var WidgetFactory
104
 
     */
105
 
    private $widgetFactory;
106
 
 
107
 
    /**
108
 
     * @var RegionFactory
109
 
     */
110
 
    private $regionFactory;
111
 
 
112
 
    /**
113
 
     * Entity constructor.
114
 
     * @param StorageServiceInterface $store
115
 
     * @param LogServiceInterface $log
116
 
     * @param DateServiceInterface $date
117
 
     * @param PermissionFactory $permissionFactory
118
 
     * @param WidgetFactory $widgetFactory
119
 
     */
120
 
    public function __construct($store, $log, $date, $permissionFactory, $widgetFactory)
121
 
    {
122
 
        $this->setCommonDependencies($store, $log);
123
 
 
124
 
        $this->dateService = $date;
125
 
        $this->permissionFactory = $permissionFactory;
126
 
        $this->widgetFactory = $widgetFactory;
127
 
 
128
 
        $this->excludeProperty('regions');
129
 
    }
130
 
 
131
 
    /**
132
 
     * @param $regionFactory
133
 
     * @return $this
134
 
     */
135
 
    public function setChildObjectDependencies($regionFactory)
136
 
    {
137
 
        $this->regionFactory = $regionFactory;
138
 
        return $this;
 
37
    public $tags;
 
38
    public $widgets;
 
39
    public $permissions;
 
40
 
 
41
    /**
 
42
     * The regions that this Playlist belongs to
 
43
     * @var array[int]
 
44
     */
 
45
    public $regionIds;
 
46
 
 
47
    public function __construct()
 
48
    {
 
49
        $this->hash = null;
 
50
        $this->widgets = array();
 
51
        $this->tags = array();
 
52
        $this->regionIds = array();
139
53
    }
140
54
 
141
55
    public function __clone()
142
56
    {
143
57
        $this->hash = null;
144
58
        $this->playlistId = null;
145
 
        $this->regions = [];
146
 
        $this->permissions = [];
147
59
 
148
60
        $this->widgets = array_map(function ($object) { return clone $object; }, $this->widgets);
149
61
    }
191
103
    }
192
104
 
193
105
    /**
194
 
     * Get Widget at Index
195
 
     * @param int $index
196
 
     * @return Widget
197
 
     * @throws NotFoundException
198
 
     */
199
 
    public function getWidgetAt($index)
200
 
    {
201
 
        if ($index <= count($this->widgets)) {
202
 
            $zeroBased = $index - 1;
203
 
            if (isset($this->widgets[$zeroBased])) {
204
 
                return $this->widgets[$zeroBased];
205
 
            }
206
 
        }
207
 
 
208
 
        throw new NotFoundException(sprintf(__('Widget not found at index %d'), $index));
209
 
    }
210
 
 
211
 
    /**
212
 
     * @param Widget $widget
213
 
     */
214
 
    public function assignWidget($widget)
215
 
    {
216
 
        $this->load();
217
 
 
218
 
        $widget->displayOrder = count($this->widgets) + 1;
219
 
        $this->widgets[] = $widget;
 
106
     * Assign this Playlist to a Region
 
107
     * @param int $regionId
 
108
     */
 
109
    public function assignRegion($regionId)
 
110
    {
 
111
        if (!in_array($regionId, $this->regionIds))
 
112
            $this->regionIds[] = $regionId;
220
113
    }
221
114
 
222
115
    /**
223
116
     * Load
224
 
     * @param array $loadOptions
225
117
     */
226
 
    public function load($loadOptions = [])
 
118
    public function load()
227
119
    {
228
 
        if ($this->playlistId == null || $this->loaded)
229
 
            return;
230
 
 
231
 
        // Options
232
 
        $options = array_merge([
233
 
            'playlistIncludeRegionAssignments' => true,
234
 
            'loadPermissions' => true,
235
 
            'loadWidgets' => true
236
 
        ], $loadOptions);
237
 
 
238
 
        $this->getLog()->debug('Load Playlist with %s', json_encode($options));
239
 
 
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);
 
122
 
 
123
        $this->widgets = WidgetFactory::getByPlaylistId($this->playlistId);
243
124
 
244
125
        // Load the widgets
245
 
        if ($options['loadWidgets']) {
246
 
            foreach ($this->widgetFactory->getByPlaylistId($this->playlistId) as $widget) {
247
 
                /* @var Widget $widget */
248
 
                $widget->load();
249
 
                $this->widgets[] = $widget;
250
 
            }
251
 
        }
252
 
 
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;
258
 
            }
 
126
        foreach ($this->widgets as $widget) {
 
127
            /* @var Widget $widget */
 
128
            $widget->load();
259
129
        }
260
130
 
261
131
        $this->hash = $this->hash();
262
 
        $this->loaded = true;
263
132
    }
264
133
 
265
134
    /**
266
 
     * Save
 
135
     * Saves
267
136
     */
268
137
    public function save()
269
138
    {
272
141
        else if ($this->hash != $this->hash())
273
142
            $this->update();
274
143
 
275
 
        // Sort the widgets by their display order
276
 
        usort($this->widgets, function($a, $b) {
277
 
            /**
278
 
             * @var Widget $a
279
 
             * @var Widget$b
280
 
             */
281
 
            return $a->displayOrder - $b->displayOrder;
282
 
        });
283
 
 
284
 
        // Assert the Playlist on all widgets and apply a display order
285
 
        // this keeps the widgets in numerical order on each playlist
286
 
        $i = 0;
287
144
        foreach ($this->widgets as $widget) {
288
145
            /* @var Widget $widget */
289
 
            $i++;
290
146
 
291
147
            // Assert the playlistId
292
148
            $widget->playlistId = $this->playlistId;
293
 
            // Assert the displayOrder
294
 
            $widget->displayOrder = $i;
295
149
            $widget->save();
296
150
        }
 
151
 
 
152
        // Manage the assignments to regions
 
153
        $this->linkRegions();
297
154
    }
298
155
 
299
156
    /**
302
159
    public function delete()
303
160
    {
304
161
        // We must ensure everything is loaded before we delete
305
 
        if (!$this->loaded)
 
162
        if ($this->hash == null)
306
163
            $this->load();
307
164
 
308
 
        $this->getLog()->debug('Deleting ' . $this);
 
165
        \Xibo\Helper\Log::Audit('Deleting ' . $this);
309
166
 
310
167
        // Delete Permissions
311
168
        foreach ($this->permissions as $permission) {
323
180
        }
324
181
 
325
182
        // Unlink regions
326
 
        foreach ($this->regions as $region) {
327
 
            /* @var Region $region */
328
 
            $region->unassignPlaylist($this);
329
 
            $region->save();
330
 
        }
 
183
        $this->unlinkRegions();
331
184
 
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));
334
187
    }
335
188
 
336
 
    /**
337
 
     * Add
338
 
     */
339
189
    private function add()
340
190
    {
341
 
        $this->getLog()->debug('Adding Playlist ' . $this->name);
 
191
        \Xibo\Helper\Log::Audit('Adding Playlist ' . $this->name);
342
192
 
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
347
197
        ));
348
198
    }
349
199
 
350
 
    /**
351
 
     * Update
352
 
     */
353
200
    private function update()
354
201
    {
355
 
        $this->getLog()->debug('Updating Playlist ' . $this->name . '. Id = ' . $this->playlistId);
 
202
        \Xibo\Helper\Log::Audit('Updating Playlist ' . $this->name . '. Id = ' . $this->playlistId);
356
203
 
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
361
208
        ));
362
209
    }
363
210
 
364
211
    /**
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.
 
212
     * Link regions
370
213
     */
371
 
    public function notifyLayouts()
 
214
    private function linkRegions()
372
215
    {
373
 
        $this->getStore()->update('
374
 
            UPDATE `layout` SET `status` = 3, `modifiedDT` = :modifiedDt WHERE layoutId IN (
375
 
              SELECT `region`.layoutId
376
 
                FROM `lkregionplaylist`
377
 
                  INNER JOIN `region`
378
 
                  ON region.regionId = `lkregionplaylist`.regionId
379
 
               WHERE `lkregionplaylist`.playlistId = :playlistId
380
 
            )
381
 
        ', [
382
 
            'playlistId' => $this->playlistId,
383
 
            'modifiedDt' => $this->dateService->getLocalDate()
384
 
        ]);
 
216
        $order = 0;
 
217
        foreach ($this->regionIds as $regionId) {
 
218
            $order++;
 
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
 
224
            ));
 
225
        }
385
226
    }
386
227
 
387
228
    /**
388
 
     * Has layouts
389
 
     * @return bool
 
229
     * Unlink all Regions
390
230
     */
391
 
    public function hasLayouts()
 
231
    private function unlinkRegions()
392
232
    {
393
 
        $results = $this->getStore()->select('SELECT COUNT(*) AS qty FROM `lkregionplaylist` WHERE playlistId = :playlistId', ['playlistId' => $this->playlistId]);
394
 
 
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
 
237
            ));
 
238
        }
396
239
    }
397
240
}
 
 
b'\\ No newline at end of file'