~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Controller/Command.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:
1
 
<?php
2
 
/*
3
 
 * Spring Signage Ltd - http://www.springsignage.com
4
 
 * Copyright (C) 2015 Spring Signage Ltd
5
 
 * (Command.php)
6
 
 */
7
 
 
8
 
 
9
 
namespace Xibo\Controller;
10
 
 
11
 
 
12
 
use Xibo\Exception\AccessDeniedException;
13
 
use Xibo\Exception\XiboException;
14
 
use Xibo\Factory\CommandFactory;
15
 
use Xibo\Factory\DisplayProfileFactory;
16
 
use Xibo\Service\ConfigServiceInterface;
17
 
use Xibo\Service\DateServiceInterface;
18
 
use Xibo\Service\LogServiceInterface;
19
 
use Xibo\Service\SanitizerServiceInterface;
20
 
 
21
 
/**
22
 
 * Class Command
23
 
 * Command Controller
24
 
 * @package Xibo\Controller
25
 
 */
26
 
class Command extends Base
27
 
{
28
 
    /**
29
 
     * @var CommandFactory
30
 
     */
31
 
    private $commandFactory;
32
 
 
33
 
    /**
34
 
     * @var DisplayProfileFactory
35
 
     */
36
 
    private $displayProfileFactory;
37
 
 
38
 
    /**
39
 
     * Set common dependencies.
40
 
     * @param LogServiceInterface $log
41
 
     * @param SanitizerServiceInterface $sanitizerService
42
 
     * @param \Xibo\Helper\ApplicationState $state
43
 
     * @param \Xibo\Entity\User $user
44
 
     * @param \Xibo\Service\HelpServiceInterface $help
45
 
     * @param DateServiceInterface $date
46
 
     * @param ConfigServiceInterface $config
47
 
     * @param CommandFactory $commandFactory
48
 
     * @param DisplayProfileFactory $displayProfileFactory
49
 
     */
50
 
    public function __construct($log, $sanitizerService, $state, $user, $help, $date, $config, $commandFactory, $displayProfileFactory)
51
 
    {
52
 
        $this->setCommonDependencies($log, $sanitizerService, $state, $user, $help, $date, $config);
53
 
 
54
 
        $this->commandFactory = $commandFactory;
55
 
        $this->displayProfileFactory = $displayProfileFactory;
56
 
    }
57
 
 
58
 
    public function displayPage()
59
 
    {
60
 
        $this->getState()->template = 'command-page';
61
 
    }
62
 
 
63
 
    /**
64
 
     * @SWG\Get(
65
 
     *  path="/command",
66
 
     *  operationId="commandSearch",
67
 
     *  tags={"command"},
68
 
     *  summary="Command Search",
69
 
     *  description="Search this users Commands",
70
 
     *  @SWG\Parameter(
71
 
     *      name="commandId",
72
 
     *      in="formData",
73
 
     *      description="Filter by Command Id",
74
 
     *      type="integer",
75
 
     *      required=false
76
 
     *   ),
77
 
     *  @SWG\Parameter(
78
 
     *      name="command",
79
 
     *      in="formData",
80
 
     *      description="Filter by Command Name",
81
 
     *      type="string",
82
 
     *      required=false
83
 
     *   ),
84
 
     *  @SWG\Parameter(
85
 
     *      name="code",
86
 
     *      in="formData",
87
 
     *      description="Filter by Command Code",
88
 
     *      type="string",
89
 
     *      required=false
90
 
     *   ),
91
 
     *  @SWG\Response(
92
 
     *      response=200,
93
 
     *      description="successful operation",
94
 
     *      @SWG\Schema(
95
 
     *          type="array",
96
 
     *          @SWG\Items(ref="#/definitions/Command")
97
 
     *      )
98
 
     *  )
99
 
     * )
100
 
     */
101
 
    function grid()
102
 
    {
103
 
        $filter = [
104
 
            'commandId' => $this->getSanitizer()->getInt('commandId'),
105
 
            'command' => $this->getSanitizer()->getString('command'),
106
 
            'code' => $this->getSanitizer()->getString('code')
107
 
        ];
108
 
 
109
 
        $commands = $this->commandFactory->query($this->gridRenderSort(), $this->gridRenderFilter($filter));
110
 
 
111
 
        foreach ($commands as $command) {
112
 
            /* @var \Xibo\Entity\Command $command */
113
 
 
114
 
            if ($this->isApi())
115
 
                break;
116
 
 
117
 
            $command->includeProperty('buttons');
118
 
 
119
 
            // Default Layout
120
 
            $command->buttons[] = array(
121
 
                'id' => 'command_button_edit',
122
 
                'url' => $this->urlFor('command.edit.form', ['id' => $command->commandId]),
123
 
                'text' => __('Edit')
124
 
            );
125
 
 
126
 
            if ($this->getUser()->checkDeleteable($command)) {
127
 
                $command->buttons[] = array(
128
 
                    'id' => 'command_button_delete',
129
 
                    'url' => $this->urlFor('command.delete.form', ['id' => $command->commandId]),
130
 
                    'text' => __('Delete'),
131
 
                    'multi-select' => true,
132
 
                    'dataAttributes' => array(
133
 
                        array('name' => 'commit-url', 'value' => $this->urlFor('command.delete', ['id' => $command->commandId])),
134
 
                        array('name' => 'commit-method', 'value' => 'delete'),
135
 
                        array('name' => 'id', 'value' => 'command_button_delete'),
136
 
                        array('name' => 'text', 'value' => __('Delete')),
137
 
                        array('name' => 'rowtitle', 'value' => $command->command)
138
 
                    )
139
 
                );
140
 
            }
141
 
        }
142
 
 
143
 
        $this->getState()->template = 'grid';
144
 
        $this->getState()->recordsTotal = $this->commandFactory->countLast();
145
 
        $this->getState()->setData($commands);
146
 
    }
147
 
 
148
 
    /**
149
 
     * Add Command Form
150
 
     */
151
 
    public function addForm()
152
 
    {
153
 
        $this->getState()->template = 'command-form-add';
154
 
    }
155
 
 
156
 
    /**
157
 
     * Edit Command
158
 
     * @param int $commandId
159
 
     * @throws XiboException
160
 
     */
161
 
    public function editForm($commandId)
162
 
    {
163
 
        $command = $this->commandFactory->getById($commandId);
164
 
 
165
 
        if ($command->getOwnerId() != $this->getUser()->userId && $this->getUser()->userTypeId != 1)
166
 
            throw new AccessDeniedException();
167
 
 
168
 
        $this->getState()->template = 'command-form-edit';
169
 
        $this->getState()->setData([
170
 
            'command' => $command
171
 
        ]);
172
 
    }
173
 
 
174
 
    /**
175
 
     * Delete Command
176
 
     * @param int $commandId
177
 
     * @throws XiboException
178
 
     */
179
 
    public function deleteForm($commandId)
180
 
    {
181
 
        $command = $this->commandFactory->getById($commandId);
182
 
 
183
 
        if ($command->getOwnerId() != $this->getUser()->userId && $this->getUser()->userTypeId != 1)
184
 
            throw new AccessDeniedException();
185
 
 
186
 
        $this->getState()->template = 'command-form-delete';
187
 
        $this->getState()->setData([
188
 
            'command' => $command
189
 
        ]);
190
 
    }
191
 
 
192
 
    /**
193
 
     * Add Command
194
 
     *
195
 
     * @SWG\Post(
196
 
     *  path="/command",
197
 
     *  operationId="commandAdd",
198
 
     *  tags={"command"},
199
 
     *  summary="Command Add",
200
 
     *  description="Add a Command",
201
 
     *  @SWG\Parameter(
202
 
     *      name="command",
203
 
     *      in="formData",
204
 
     *      description="The Command Name",
205
 
     *      type="string",
206
 
     *      required=true
207
 
     *   ),
208
 
     *  @SWG\Parameter(
209
 
     *      name="description",
210
 
     *      in="formData",
211
 
     *      description="A description for the command",
212
 
     *      type="string",
213
 
     *      required=false
214
 
     *   ),
215
 
     *  @SWG\Parameter(
216
 
     *      name="code",
217
 
     *      in="formData",
218
 
     *      description="A unique code for this command",
219
 
     *      type="string",
220
 
     *      required=true
221
 
     *   ),
222
 
     *  @SWG\Response(
223
 
     *      response=201,
224
 
     *      description="successful operation",
225
 
     *      @SWG\Schema(ref="#/definitions/Command"),
226
 
     *      @SWG\Header(
227
 
     *          header="Location",
228
 
     *          description="Location of the new record",
229
 
     *          type="string"
230
 
     *      )
231
 
     *  )
232
 
     * )
233
 
     */
234
 
    public function add()
235
 
    {
236
 
        $command = $this->commandFactory->create();
237
 
        $command->command = $this->getSanitizer()->getString('command');
238
 
        $command->description = $this->getSanitizer()->getString('description');
239
 
        $command->code = $this->getSanitizer()->getString('code');
240
 
        $command->userId = $this->getUser()->userId;
241
 
        $command->save();
242
 
 
243
 
        // Return
244
 
        $this->getState()->hydrate([
245
 
            'httpStatus' => 201,
246
 
            'message' => sprintf(__('Added %s'), $command->command),
247
 
            'id' => $command->commandId,
248
 
            'data' => $command
249
 
        ]);
250
 
    }
251
 
 
252
 
    /**
253
 
     * Edit Command
254
 
     * @param int $commandId
255
 
     *
256
 
     * @SWG\Put(
257
 
     *  path="/command/{commandId}",
258
 
     *  operationId="commandEdit",
259
 
     *  tags={"command"},
260
 
     *  summary="Edit Command",
261
 
     *  description="Edit the provided command",
262
 
     *  @SWG\Parameter(
263
 
     *      name="commandId",
264
 
     *      in="path",
265
 
     *      description="The Command Id to Edit",
266
 
     *      type="integer",
267
 
     *      required=true
268
 
     *   ),
269
 
     *  @SWG\Parameter(
270
 
     *      name="command",
271
 
     *      in="formData",
272
 
     *      description="The Command Name",
273
 
     *      type="string",
274
 
     *      required=true
275
 
     *   ),
276
 
     *  @SWG\Parameter(
277
 
     *      name="description",
278
 
     *      in="formData",
279
 
     *      description="A description for the command",
280
 
     *      type="string",
281
 
     *      required=false
282
 
     *   ),
283
 
     *  @SWG\Response(
284
 
     *      response=200,
285
 
     *      description="successful operation",
286
 
     *      @SWG\Schema(ref="#/definitions/Command")
287
 
     *  )
288
 
     * )
289
 
     *
290
 
     * @throws XiboException
291
 
     */
292
 
    public function edit($commandId)
293
 
    {
294
 
        $command = $this->commandFactory->getById($commandId);
295
 
 
296
 
        if ($command->getOwnerId() != $this->getUser()->userId && $this->getUser()->userTypeId != 1)
297
 
            throw new AccessDeniedException();
298
 
 
299
 
        $command->command = $this->getSanitizer()->getString('command');
300
 
        $command->description = $this->getSanitizer()->getString('description');
301
 
        $command->save();
302
 
 
303
 
        // Return
304
 
        $this->getState()->hydrate([
305
 
            'httpStatus' => 200,
306
 
            'message' => sprintf(__('Edited %s'), $command->command),
307
 
            'id' => $command->commandId,
308
 
            'data' => $command
309
 
        ]);
310
 
    }
311
 
 
312
 
    /**
313
 
     * Delete Command
314
 
     * @param int $commandId
315
 
     *
316
 
     * @SWG\Delete(
317
 
     *  path="/command/{commandId}",
318
 
     *  operationId="commandDelete",
319
 
     *  tags={"command"},
320
 
     *  summary="Delete Command",
321
 
     *  description="Delete the provided command",
322
 
     *  @SWG\Parameter(
323
 
     *      name="commandId",
324
 
     *      in="path",
325
 
     *      description="The Command Id to Delete",
326
 
     *      type="integer",
327
 
     *      required=true
328
 
     *   ),
329
 
     *  @SWG\Response(
330
 
     *      response=204,
331
 
     *      description="successful operation"
332
 
     *  )
333
 
     * )
334
 
     */
335
 
    public function delete($commandId)
336
 
    {
337
 
        $command = $this->commandFactory->getById($commandId);
338
 
 
339
 
        if ($command->getOwnerId() != $this->getUser()->userId && $this->getUser()->userTypeId != 1)
340
 
            throw new AccessDeniedException();
341
 
 
342
 
        $command->setChildObjectDependencies($this->displayProfileFactory);
343
 
        $command->delete();
344
 
 
345
 
        // Return
346
 
        $this->getState()->hydrate([
347
 
            'httpStatus' => 204,
348
 
            'message' => sprintf(__('Deleted %s'), $command->command)
349
 
        ]);
350
 
    }
351
 
}
 
 
b'\\ No newline at end of file'