~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Widget/ShellCommand.php

  • Committer: Dan Garner
  • Date: 2015-08-11 09:29:02 UTC
  • mto: This revision was merged to the branch mainline in revision 453.
  • Revision ID: git-v1:a86fb4369b7395c13367577d23b14c0ab4528c1a
Transitions fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
namespace Xibo\Widget;
22
22
 
23
23
use InvalidArgumentException;
 
24
use Xibo\Helper\Sanitize;
24
25
 
25
 
class ShellCommand extends ModuleWidget
 
26
class ShellCommand extends Module
26
27
{
27
28
    public function validate()
28
29
    {
29
 
        if ($this->getOption('windowsCommand') == '' && $this->getOption('linuxCommand') == '' && $this->getOption('commandCode') == '')
 
30
        if ($this->getOption('windowsCommand') == '' && $this->getOption('linuxCommand') == '')
30
31
            throw new InvalidArgumentException(__('You must enter a command'));
31
32
    }
32
33
 
33
34
    /**
34
 
     * Adds a Shell Command Widget
35
 
     * @SWG\Post(
36
 
     *  path="/playlist/widget/shellCommand/{playlistId}",
37
 
     *  operationId="WidgetShellCommandAdd",
38
 
     *  tags={"widget"},
39
 
     *  summary="Add a Shell Command Widget",
40
 
     *  description="Add a new Shell Command Widget to the specified playlist",
41
 
     *  @SWG\Parameter(
42
 
     *      name="playlistId",
43
 
     *      in="path",
44
 
     *      description="The playlist ID to add a Widget to",
45
 
     *      type="integer",
46
 
     *      required=true
47
 
     *   ),
48
 
     *  @SWG\Parameter(
49
 
     *      name="name",
50
 
     *      in="formData",
51
 
     *      description="Optional Widget Name",
52
 
     *      type="string",
53
 
     *      required=false
54
 
     *  ),
55
 
     *  @SWG\Parameter(
56
 
     *      name="duration",
57
 
     *      in="formData",
58
 
     *      description="The Widget Duration",
59
 
     *      type="integer",
60
 
     *      required=false
61
 
     *  ),
62
 
     *  @SWG\Parameter(
63
 
     *      name="useDuration",
64
 
     *      in="formData",
65
 
     *      description="(0, 1) Select 1 only if you will provide duration parameter as well",
66
 
     *      type="integer",
67
 
     *      required=false
68
 
     *  ),
69
 
     *  @SWG\Parameter(
70
 
     *      name="windowsCommand",
71
 
     *      in="formData",
72
 
     *      description="Enter a Windows command line compatible command",
73
 
     *      type="string",
74
 
     *      required=false
75
 
     *   ),
76
 
     *  @SWG\Parameter(
77
 
     *      name="linuxCommand",
78
 
     *      in="formData",
79
 
     *      description="Enter a Android / Linux command line compatible command",
80
 
     *      type="string",
81
 
     *      required=false
82
 
     *   ),
83
 
     *  @SWG\Parameter(
84
 
     *      name="launchThroughCmd",
85
 
     *      in="formData",
86
 
     *      description="flag (0,1) Windows only, Should the player launch this command through the windows command line (cmd.exe)? This is useful for batch files, if you try to terminate this command only the command line will be terminated",
87
 
     *      type="integer",
88
 
     *      required=false
89
 
     *   ),
90
 
     *  @SWG\Parameter(
91
 
     *      name="terminateCommand",
92
 
     *      in="formData",
93
 
     *      description="flag (0,1) Should the player forcefully terminate the command after the duration specified, 0 to let the command terminate naturally",
94
 
     *      type="integer",
95
 
     *      required=false
96
 
     *   ),
97
 
     *  @SWG\Parameter(
98
 
     *      name="useTaskkill",
99
 
     *      in="formData",
100
 
     *      description="flag (0,1) Windows only, should the player use taskkill to terminate commands",
101
 
     *      type="integer",
102
 
     *      required=false
103
 
     *   ),
104
 
     *  @SWG\Parameter(
105
 
     *      name="commandCode",
106
 
     *      in="formData",
107
 
     *      description="Enter a reference code for exiting command in CMS",
108
 
     *      type="string",
109
 
     *      required=false
110
 
     *   ),
111
 
     *  @SWG\Response(
112
 
     *      response=201,
113
 
     *      description="successful operation",
114
 
     *      @SWG\Schema(ref="#/definitions/Widget"),
115
 
     *      @SWG\Header(
116
 
     *          header="Location",
117
 
     *          description="Location of the new widget",
118
 
     *          type="string"
119
 
     *      )
120
 
     *  )
121
 
     * )
 
35
     * Add Media
122
36
     */
123
37
    public function add()
124
38
    {
125
39
        // Any Options (we need to encode shell commands, as they sit on the options rather than the raw
126
 
        $this->setOption('name', $this->getSanitizer()->getString('name'));
127
 
        $this->setUseDuration($this->getSanitizer()->getCheckbox('useDuration'));
128
 
        $this->setDuration($this->getSanitizer()->getInt('duration', $this->getDuration()));
129
 
 
130
 
        // Commands
131
 
        $windows = $this->getSanitizer()->getString('windowsCommand');
132
 
        $linux = $this->getSanitizer()->getString('linuxCommand');
133
 
 
134
 
        $this->setOption('launchThroughCmd', $this->getSanitizer()->getCheckbox('launchThroughCmd'));
135
 
        $this->setOption('terminateCommand', $this->getSanitizer()->getCheckbox('terminateCommand'));
136
 
        $this->setOption('useTaskkill', $this->getSanitizer()->getCheckbox('useTaskkill'));
137
 
        $this->setOption('commandCode', $this->getSanitizer()->getString('commandCode'));
138
 
        $this->setOption('windowsCommand', urlencode($windows));
139
 
        $this->setOption('linuxCommand', urlencode($linux));
 
40
        $this->setDuration(1);
 
41
        $this->SetOption('windowsCommand', urlencode(Sanitize::getString('windowsCommand')));
 
42
        $this->SetOption('linuxCommand', urlencode(Sanitize::getString('linuxCommand')));
140
43
 
141
44
        // Save the widget
142
45
        $this->validate();
149
52
    public function edit()
150
53
    {
151
54
        // Any Options (we need to encode shell commands, as they sit on the options rather than the raw
152
 
        $this->setUseDuration($this->getSanitizer()->getCheckbox('useDuration'));
153
 
        $this->setDuration($this->getSanitizer()->getInt('duration', $this->getDuration()));
154
 
        $this->setOption('name', $this->getSanitizer()->getString('name'));
155
 
 
156
 
        // Commands
157
 
        $windows = $this->getSanitizer()->getString('windowsCommand');
158
 
        $linux = $this->getSanitizer()->getString('linuxCommand');
159
 
 
160
 
        $this->setOption('launchThroughCmd', $this->getSanitizer()->getCheckbox('launchThroughCmd'));
161
 
        $this->setOption('terminateCommand', $this->getSanitizer()->getCheckbox('terminateCommand'));
162
 
        $this->setOption('useTaskkill', $this->getSanitizer()->getCheckbox('useTaskkill'));
163
 
        $this->setOption('commandCode', $this->getSanitizer()->getString('commandCode'));
164
 
        $this->setOption('windowsCommand', urlencode($windows));
165
 
        $this->setOption('linuxCommand', urlencode($linux));
 
55
        $this->setDuration(1);
 
56
        $this->SetOption('windowsCommand', urlencode(Sanitize::getString('windowsCommand')));
 
57
        $this->SetOption('linuxCommand', urlencode(Sanitize::getString('linuxCommand')));
166
58
 
167
59
        // Save the widget
168
60
        $this->validate();
174
66
        if ($this->module->previewEnabled == 0)
175
67
            return parent::Preview($width, $height);
176
68
 
177
 
        $windows = $this->getOption('windowsCommand');
178
 
        $linux = $this->getOption('linuxCommand');
179
 
 
180
 
        if ($windows == '' && $linux == '') {
181
 
            return __('Stored Command: %s', $this->getOption('commandCode'));
182
 
        }
183
 
        else {
184
 
 
185
 
            $preview  = '<p>' . __('Windows Command') . ': ' . urldecode($windows) . '</p>';
186
 
            $preview .= '<p>' . __('Linux Command') . ': ' . urldecode($linux) . '</p>';
187
 
 
188
 
            return $preview;
189
 
        }
 
69
        $msgWindows = __('Windows Command');
 
70
        $msgLinux = __('Linux Command');
 
71
 
 
72
        $preview = '';
 
73
        $preview .= '<p>' . $msgWindows . ': ' . urldecode($this->GetOption('windowsCommand')) . '</p>';
 
74
        $preview .= '<p>' . $msgLinux . ': ' . urldecode($this->GetOption('linuxCommand')) . '</p>';
 
75
 
 
76
        return $preview;
190
77
    }
191
78
 
192
79
    public function hoverPreview()
199
86
        // Client dependant
200
87
        return 2;
201
88
    }
202
 
 
203
 
    /**
204
 
     * @param array $data
205
 
     * @return array
206
 
     */
207
 
    public function setTemplateData($data)
208
 
    {
209
 
        $data['commands'] = $this->commandFactory->query();
210
 
        return $data;
211
 
    }
212
89
}