~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Controller/DayPart.php

  • Committer: Dan Garner
  • Date: 2016-02-15 17:54:45 UTC
  • mto: (454.4.130)
  • mto: This revision was merged to the branch mainline in revision 484.
  • Revision ID: git-v1:dd226a6f84464ff28758a249e1fd52ca4a35d911
Correction to Layout Duration ToolTip
xibosignage/xibo#721

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * Xibo - Digital Signage - http://www.xibo.org.uk
4
 
 * Copyright (C) 2012-2016 Spring Signage Ltd - http://www.springsignage.com
5
 
 *
6
 
 * This file is part of Xibo.
7
 
 *
8
 
 * Xibo is free software: you can redistribute it and/or modify
9
 
 * it under the terms of the GNU Affero General Public License as published by
10
 
 * the Free Software Foundation, either version 3 of the License, or
11
 
 * any later version.
12
 
 *
13
 
 * Xibo is distributed in the hope that it will be useful,
14
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
 * GNU Affero General Public License for more details.
17
 
 *
18
 
 * You should have received a copy of the GNU Affero General Public License
19
 
 * along with Xibo.  If not, see <http://www.gnu.org/licenses/>.
20
 
 */
21
 
namespace Xibo\Controller;
22
 
 
23
 
use Xibo\Exception\AccessDeniedException;
24
 
use Xibo\Exception\XiboException;
25
 
use Xibo\Factory\DayPartFactory;
26
 
use Xibo\Factory\DisplayFactory;
27
 
use Xibo\Factory\DisplayGroupFactory;
28
 
use Xibo\Factory\LayoutFactory;
29
 
use Xibo\Factory\MediaFactory;
30
 
use Xibo\Factory\ScheduleFactory;
31
 
use Xibo\Service\ConfigServiceInterface;
32
 
use Xibo\Service\DateServiceInterface;
33
 
use Xibo\Service\LogServiceInterface;
34
 
use Xibo\Service\SanitizerServiceInterface;
35
 
 
36
 
/**
37
 
 * Class DayPart
38
 
 * @package Xibo\Controller
39
 
 */
40
 
class DayPart extends Base
41
 
{
42
 
    /** @var  DayPartFactory */
43
 
    private $dayPartFactory;
44
 
 
45
 
    /** @var DisplayGroupFactory */
46
 
    private $displayGroupFactory;
47
 
 
48
 
    /** @var  DisplayFactory */
49
 
    private $displayFactory;
50
 
 
51
 
    /** @var  LayoutFactory */
52
 
    private $layoutFactory;
53
 
 
54
 
    /** @var  MediaFactory */
55
 
    private $mediaFactory;
56
 
 
57
 
    /** @var  ScheduleFactory */
58
 
    private $scheduleFactory;
59
 
 
60
 
    /**
61
 
     * Set common dependencies.
62
 
     * @param LogServiceInterface $log
63
 
     * @param SanitizerServiceInterface $sanitizerService
64
 
     * @param \Xibo\Helper\ApplicationState $state
65
 
     * @param \Xibo\Entity\User $user
66
 
     * @param \Xibo\Service\HelpServiceInterface $help
67
 
     * @param DateServiceInterface $date
68
 
     * @param ConfigServiceInterface $config
69
 
     * @param DayPartFactory $dayPartFactory
70
 
     * @param DisplayGroupFactory $displayGroupFactory
71
 
     * @param DisplayFactory $displayFactory
72
 
     * @param LayoutFactory $layoutFactory
73
 
     * @param MediaFactory $mediaFactory
74
 
     * @param ScheduleFactory $scheduleFactory
75
 
     */
76
 
    public function __construct($log, $sanitizerService, $state, $user, $help, $date, $config, $dayPartFactory, $displayGroupFactory, $displayFactory, $layoutFactory, $mediaFactory, $scheduleFactory)
77
 
    {
78
 
        $this->setCommonDependencies($log, $sanitizerService, $state, $user, $help, $date, $config);
79
 
 
80
 
        $this->dayPartFactory = $dayPartFactory;
81
 
        $this->displayGroupFactory = $displayGroupFactory;
82
 
        $this->displayFactory = $displayFactory;
83
 
        $this->layoutFactory = $layoutFactory;
84
 
        $this->mediaFactory = $mediaFactory;
85
 
        $this->scheduleFactory = $scheduleFactory;
86
 
    }
87
 
 
88
 
    /**
89
 
     * View Route
90
 
     */
91
 
    public function displayPage()
92
 
    {
93
 
        $this->getState()->template = 'daypart-page';
94
 
    }
95
 
 
96
 
    /**
97
 
     * Search
98
 
     */
99
 
    public function grid()
100
 
    {
101
 
        $filter = [
102
 
            'dayPartId' => $this->getSanitizer()->getInt('dayPartId'),
103
 
            'name' => $this->getSanitizer()->getString('name'),
104
 
            'isAlways' => 0,
105
 
            'isCustom' => 0
106
 
        ];
107
 
 
108
 
        $dayParts = $this->dayPartFactory->query($this->gridRenderSort(), $this->gridRenderFilter($filter));
109
 
 
110
 
        foreach ($dayParts as $dayPart) {
111
 
            /* @var \Xibo\Entity\DayPart $dayPart */
112
 
 
113
 
            if ($this->isApi())
114
 
                break;
115
 
 
116
 
            $dayPart->includeProperty('buttons');
117
 
 
118
 
            // Default Layout
119
 
            $dayPart->buttons[] = array(
120
 
                'id' => 'daypart_button_edit',
121
 
                'url' => $this->urlFor('daypart.edit.form', ['id' => $dayPart->dayPartId]),
122
 
                'text' => __('Edit')
123
 
            );
124
 
 
125
 
            if ($this->getUser()->checkDeleteable($dayPart)) {
126
 
                $dayPart->buttons[] = array(
127
 
                    'id' => 'daypart_button_delete',
128
 
                    'url' => $this->urlFor('daypart.delete.form', ['id' => $dayPart->dayPartId]),
129
 
                    'text' => __('Delete'),
130
 
                    'multi-select' => true,
131
 
                    'dataAttributes' => array(
132
 
                        array('name' => 'commit-url', 'value' => $this->urlFor('daypart.delete', ['id' => $dayPart->dayPartId])),
133
 
                        array('name' => 'commit-method', 'value' => 'delete'),
134
 
                        array('name' => 'id', 'value' => 'daypart_button_delete'),
135
 
                        array('name' => 'text', 'value' => __('Delete')),
136
 
                        array('name' => 'rowtitle', 'value' => $dayPart->name)
137
 
                    )
138
 
                );
139
 
            }
140
 
 
141
 
            if ($this->getUser()->checkPermissionsModifyable($dayPart)) {
142
 
 
143
 
                $dayPart->buttons[] = ['divider' => true];
144
 
 
145
 
                // Edit Permissions
146
 
                $dayPart->buttons[] = array(
147
 
                    'id' => 'daypart_button_permissions',
148
 
                    'url' => $this->urlFor('user.permissions.form', ['entity' => 'DayPart', 'id' => $dayPart->dayPartId]),
149
 
                    'text' => __('Permissions')
150
 
                );
151
 
            }
152
 
        }
153
 
 
154
 
        $this->getState()->template = 'grid';
155
 
        $this->getState()->recordsTotal = $this->dayPartFactory->countLast();
156
 
        $this->getState()->setData($dayParts);
157
 
    }
158
 
 
159
 
    /**
160
 
     * Add Daypart Form
161
 
     */
162
 
    public function addForm()
163
 
    {
164
 
        $this->getState()->template = 'daypart-form-add';
165
 
        $this->getState()->setData([
166
 
            'extra' => [
167
 
                'exceptions' => []
168
 
            ]
169
 
        ]);
170
 
    }
171
 
 
172
 
    /**
173
 
     * Edit Daypart
174
 
     * @param int $dayPartId
175
 
     */
176
 
    public function editForm($dayPartId)
177
 
    {
178
 
        $dayPart = $this->dayPartFactory->getById($dayPartId);
179
 
 
180
 
        if (!$this->getUser()->checkEditable($dayPart))
181
 
            throw new AccessDeniedException();
182
 
 
183
 
        if ($dayPart->isAlways === 1 || $dayPart->isCustom === 1)
184
 
            throw new AccessDeniedException();
185
 
 
186
 
        $this->getState()->template = 'daypart-form-edit';
187
 
        $this->getState()->setData([
188
 
            'dayPart' => $dayPart,
189
 
            'extra' => [
190
 
                'exceptions' => $dayPart->exceptions
191
 
            ]
192
 
        ]);
193
 
    }
194
 
 
195
 
    /**
196
 
     * Delete Daypart
197
 
     * @param int $dayPartId
198
 
     */
199
 
    public function deleteForm($dayPartId)
200
 
    {
201
 
        $dayPart = $this->dayPartFactory->getById($dayPartId);
202
 
 
203
 
        if (!$this->getUser()->checkDeleteable($dayPart))
204
 
            throw new AccessDeniedException();
205
 
 
206
 
        if ($dayPart->isAlways === 1 || $dayPart->isCustom === 1)
207
 
            throw new AccessDeniedException();
208
 
 
209
 
        // Get a count of schedules for this day part
210
 
        $schedules = $this->scheduleFactory->getByDayPartId($dayPartId);
211
 
 
212
 
        $this->getState()->template = 'daypart-form-delete';
213
 
        $this->getState()->setData([
214
 
            'countSchedules' => count($schedules),
215
 
            'dayPart' => $dayPart
216
 
        ]);
217
 
    }
218
 
 
219
 
    /**
220
 
     * Add
221
 
     * @SWG\Post(
222
 
     *  path="/daypart",
223
 
     *  operationId="dayPartAdd",
224
 
     *  tags={"dayPart"},
225
 
     *  summary="Daypart Add",
226
 
     *  description="Add a Daypart",
227
 
     *  @SWG\Parameter(
228
 
     *      name="name",
229
 
     *      in="formData",
230
 
     *      description="The Daypart Name",
231
 
     *      type="string",
232
 
     *      required=true
233
 
     *   ),
234
 
     *  @SWG\Parameter(
235
 
     *      name="description",
236
 
     *      in="formData",
237
 
     *      description="A description for the dayPart",
238
 
     *      type="string",
239
 
     *      required=false
240
 
     *   ),
241
 
     *  @SWG\Parameter(
242
 
     *      name="startTime",
243
 
     *      in="formData",
244
 
     *      description="The start time for this day part",
245
 
     *      type="string",
246
 
     *      required=true
247
 
     *   ),
248
 
     *  @SWG\Parameter(
249
 
     *      name="endTime",
250
 
     *      in="formData",
251
 
     *      description="The end time for this day part",
252
 
     *      type="string",
253
 
     *      required=true
254
 
     *   ),
255
 
     *  @SWG\Parameter(
256
 
     *      name="exceptionDays",
257
 
     *      in="formData",
258
 
     *      description="String array of exception days",
259
 
     *      type="array",
260
 
     *      required=false,
261
 
     *      @SWG\Items(type="string")
262
 
     *   ),
263
 
     *  @SWG\Parameter(
264
 
     *      name="exceptionStartTimes",
265
 
     *      in="formData",
266
 
     *      description="String array of exception start times to match the exception days",
267
 
     *      type="array",
268
 
     *      required=false,
269
 
     *      @SWG\Items(type="string")
270
 
     *   ),
271
 
     *  @SWG\Parameter(
272
 
     *      name="exceptionEndTimes",
273
 
     *      in="formData",
274
 
     *      description="String array of exception end times to match the exception days",
275
 
     *      type="array",
276
 
     *      required=false,
277
 
     *      @SWG\Items(type="string")
278
 
     *   ),
279
 
     *  @SWG\Response(
280
 
     *      response=201,
281
 
     *      description="successful operation",
282
 
     *      @SWG\Schema(ref="#/definitions/DayPart"),
283
 
     *      @SWG\Header(
284
 
     *          header="Location",
285
 
     *          description="Location of the new record",
286
 
     *          type="string"
287
 
     *      )
288
 
     *  )
289
 
     * )
290
 
     */
291
 
    public function add()
292
 
    {
293
 
        $dayPart = $this->dayPartFactory->createEmpty();
294
 
        $this->handleCommonInputs($dayPart);
295
 
 
296
 
        $dayPart
297
 
            ->setScheduleFactory($this->scheduleFactory)
298
 
            ->save();
299
 
 
300
 
        // Return
301
 
        $this->getState()->hydrate([
302
 
            'httpStatus' => 201,
303
 
            'message' => sprintf(__('Added %s'), $dayPart->name),
304
 
            'id' => $dayPart->dayPartId,
305
 
            'data' => $dayPart
306
 
        ]);
307
 
    }
308
 
 
309
 
    /**
310
 
     * Edit
311
 
     * @param int $dayPartId
312
 
     *
313
 
     * @SWG\Put(
314
 
     *  path="/daypart/{dayPartId}",
315
 
     *  operationId="dayPartAdd",
316
 
     *  tags={"dayPart"},
317
 
     *  summary="Daypart Add",
318
 
     *  description="Add a Daypart",
319
 
     *  @SWG\Parameter(
320
 
     *      name="dayPartId",
321
 
     *      in="path",
322
 
     *      description="The Daypart Id",
323
 
     *      type="integer",
324
 
     *      required=true
325
 
     *   ),
326
 
     *  @SWG\Parameter(
327
 
     *      name="name",
328
 
     *      in="formData",
329
 
     *      description="The Daypart Name",
330
 
     *      type="string",
331
 
     *      required=true
332
 
     *   ),
333
 
     *  @SWG\Parameter(
334
 
     *      name="description",
335
 
     *      in="formData",
336
 
     *      description="A description for the dayPart",
337
 
     *      type="string",
338
 
     *      required=false
339
 
     *   ),
340
 
     *  @SWG\Parameter(
341
 
     *      name="startTime",
342
 
     *      in="formData",
343
 
     *      description="The start time for this day part",
344
 
     *      type="string",
345
 
     *      required=true
346
 
     *   ),
347
 
     *  @SWG\Parameter(
348
 
     *      name="endTime",
349
 
     *      in="formData",
350
 
     *      description="The end time for this day part",
351
 
     *      type="string",
352
 
     *      required=true
353
 
     *   ),
354
 
     *  @SWG\Parameter(
355
 
     *      name="exceptionDays",
356
 
     *      in="formData",
357
 
     *      description="String array of exception days",
358
 
     *      type="array",
359
 
     *      required=false,
360
 
     *      @SWG\Items(type="string")
361
 
     *   ),
362
 
     *  @SWG\Parameter(
363
 
     *      name="exceptionStartTimes",
364
 
     *      in="formData",
365
 
     *      description="String array of exception start times to match the exception days",
366
 
     *      type="array",
367
 
     *      required=false,
368
 
     *      @SWG\Items(type="string")
369
 
     *   ),
370
 
     *  @SWG\Parameter(
371
 
     *      name="exceptionEndTimes",
372
 
     *      in="formData",
373
 
     *      description="String array of exception end times to match the exception days",
374
 
     *      type="array",
375
 
     *      required=false,
376
 
     *      @SWG\Items(type="string")
377
 
     *   ),
378
 
     *  @SWG\Response(
379
 
     *      response=200,
380
 
     *      description="successful operation",
381
 
     *      @SWG\Schema(ref="#/definitions/DayPart")
382
 
     *  )
383
 
     * )
384
 
     *
385
 
     * @throws XiboException
386
 
     */
387
 
    public function edit($dayPartId)
388
 
    {
389
 
        $dayPart = $this->dayPartFactory->getById($dayPartId)
390
 
            ->setDateService($this->getDate())
391
 
            ->setChildObjectDependencies($this->displayGroupFactory, $this->displayFactory, $this->layoutFactory, $this->mediaFactory, $this->scheduleFactory, $this->dayPartFactory)
392
 
            ->load();
393
 
 
394
 
        if (!$this->getUser()->checkEditable($dayPart))
395
 
            throw new AccessDeniedException();
396
 
 
397
 
        if ($dayPart->isAlways === 1 || $dayPart->isCustom === 1)
398
 
            throw new AccessDeniedException();
399
 
 
400
 
        $this->handleCommonInputs($dayPart);
401
 
        $dayPart
402
 
            ->setScheduleFactory($this->scheduleFactory)
403
 
            ->save();
404
 
 
405
 
        // Return
406
 
        $this->getState()->hydrate([
407
 
            'httpStatus' => 200,
408
 
            'message' => sprintf(__('Edited %s'), $dayPart->name),
409
 
            'id' => $dayPart->dayPartId,
410
 
            'data' => $dayPart
411
 
        ]);
412
 
    }
413
 
 
414
 
    /**
415
 
     * Handle common inputs
416
 
     * @param $dayPart
417
 
     */
418
 
    private function handleCommonInputs($dayPart)
419
 
    {
420
 
        $dayPart->userId = $this->getUser()->userId;
421
 
        $dayPart->name = $this->getSanitizer()->getString('name');
422
 
        $dayPart->description = $this->getSanitizer()->getString('description');
423
 
        $dayPart->isRetired = $this->getSanitizer()->getCheckbox('isRetired');
424
 
        $dayPart->startTime = $this->getSanitizer()->getString('startTime');
425
 
        $dayPart->endTime = $this->getSanitizer()->getString('endTime');
426
 
 
427
 
        // Exceptions
428
 
        $exceptionDays = $this->getSanitizer()->getStringArray('exceptionDay');
429
 
        $exceptionStartTimes = $this->getSanitizer()->getStringArray('exceptionStartTimes');
430
 
        $exceptionEndTimes = $this->getSanitizer()->getStringArray('exceptionEndTimes');
431
 
 
432
 
        // Clear down existing exceptions
433
 
        $dayPart->exceptions = [];
434
 
 
435
 
        $i = -1;
436
 
        foreach ($exceptionDays as $exceptionDay) {
437
 
            // Pull the corrisponding start/end time out of the same position in the array
438
 
            $i++;
439
 
 
440
 
            $exceptionDayStartTime = isset($exceptionStartTimes[$i]) ? $exceptionStartTimes[$i] : '';
441
 
            $exceptionDayEndTime = isset($exceptionEndTimes[$i]) ? $exceptionEndTimes[$i] : '';
442
 
 
443
 
            if ($exceptionDay == '' || $exceptionDayStartTime == '' || $exceptionDayEndTime == '')
444
 
                continue;
445
 
 
446
 
            // Is this already set?
447
 
            $found = false;
448
 
            foreach ($dayPart->exceptions as $exception) {
449
 
 
450
 
                if ($exception['day'] == $exceptionDay) {
451
 
                    $exception['start'] = $exceptionDayStartTime;
452
 
                    $exception['end'] = $exceptionDayEndTime;
453
 
 
454
 
                    $found = true;
455
 
                    break;
456
 
                }
457
 
            }
458
 
 
459
 
            // Otherwise add it
460
 
            if (!$found) {
461
 
                $dayPart->exceptions[] = [
462
 
                    'day' => $exceptionDay,
463
 
                    'start' => $exceptionDayStartTime,
464
 
                    'end' => $exceptionDayEndTime
465
 
                ];
466
 
            }
467
 
        }
468
 
    }
469
 
 
470
 
    /**
471
 
     * Delete
472
 
     * @param int $dayPartId
473
 
     *
474
 
     * @SWG\Delete(
475
 
     *  path="/daypart/{dayPartId}",
476
 
     *  operationId="dayPartDelete",
477
 
     *  tags={"dayPart"},
478
 
     *  summary="Delete DayPart",
479
 
     *  description="Delete the provided dayPart",
480
 
     *  @SWG\Parameter(
481
 
     *      name="dayPartId",
482
 
     *      in="path",
483
 
     *      description="The Daypart Id to Delete",
484
 
     *      type="integer",
485
 
     *      required=true
486
 
     *   ),
487
 
     *  @SWG\Response(
488
 
     *      response=204,
489
 
     *      description="successful operation"
490
 
     *  )
491
 
     * )
492
 
     *
493
 
     * @throws XiboException
494
 
     */
495
 
    public function delete($dayPartId)
496
 
    {
497
 
        $dayPart = $this->dayPartFactory->getById($dayPartId);
498
 
 
499
 
        if (!$this->getUser()->checkDeleteable($dayPart))
500
 
            throw new AccessDeniedException();
501
 
 
502
 
        $dayPart
503
 
            ->setDateService($this->getDate())
504
 
            ->setScheduleFactory($this->scheduleFactory)
505
 
            ->delete();
506
 
 
507
 
        // Return
508
 
        $this->getState()->hydrate([
509
 
            'httpStatus' => 204,
510
 
            'message' => sprintf(__('Deleted %s'), $dayPart->name)
511
 
        ]);
512
 
    }
513
 
}
 
 
b'\\ No newline at end of file'