Completed
Push — master ( 311420...74254e )
by Iurii
01:12
created

ImageStyle::validateInputActionsImageStyle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
/**
4
 * @package CLI
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2018, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\cli\controllers;
11
12
use gplcart\core\models\ImageStyle as ImageStyleModel;
13
use gplcart\modules\cli\controllers\Base;
14
15
/**
16
 * Handles commands related to image styles
17
 */
18
class ImageStyle extends Base
19
{
20
21
    /**
22
     * Image style model class instance
23
     * @var \gplcart\core\models\ImageStyle $image_style
24
     */
25
    protected $image_style;
26
27
    /**
28
     * @param ImageStyleModel $image_style
29
     */
30
    public function __construct(ImageStyleModel $image_style)
31
    {
32
        parent::__construct();
33
34
        $this->image_style = $image_style;
35
    }
36
37
    /**
38
     * Callback for "imagestyle-update"
39
     * Update an image style
40
     */
41
    public function cmdUpdateImageStyle()
42
    {
43
        $this->submitUpdateImageStyle();
44
        $this->output();
45
    }
46
47
    /**
48
     * Callback for "imagestyle-add"
49
     * Adds a new image style
50
     */
51
    public function cmdAddImageStyle()
52
    {
53
        if ($this->getParam()) {
54
            $this->submitAddImageStyle();
55
        } else {
56
            $this->wizardAddImageStyle();
57
        }
58
59
        $this->output();
60
    }
61
62
    /**
63
     * Callback for "imagestyle-delete" command
64
     * Delete an image style
65
     */
66
    public function cmdDeleteImageStyle()
67
    {
68
        $id = $this->getParam(0);
69
70
        if (empty($id)) {
71
            $this->errorExit($this->text('Invalid ID'));
72
        }
73
74
        if (!$this->image_style->delete($id)) {
75
            $this->errorExit($this->text('Image style has not been deleted'));
76
        }
77
78
        $this->output();
79
    }
80
81
    /**
82
     * Callback for "imagestyle-clear-cache" command
83
     * Delete cached images
84
     */
85
    public function cmdClearCacheImageStyle()
86
    {
87
        $id = $this->getParam(0);
88
        $all = $this->getParam('all');
89
90
        $result = false;
91
92
        if (!empty($id)) {
93
            $result = $this->image_style->clearCache($id);
94
        } else if (!empty($all)) {
95
            $result = $this->image_style->clearCache();
96
        } else {
97
            $this->errorExit($this->text('Invalid command'));
98
        }
99
100
        if (!$result) {
101
            $this->errorExit($this->text('An error occurred'));
102
        }
103
104
        $this->output();
105
    }
106
107
    /**
108
     * Callback for "imagestyle-get" command
109
     * List one or a list of image styles
110
     */
111
    public function cmdGetImageStyle()
112
    {
113
        $list = $this->getListImageStyle();
114
        $this->outputFormat($list);
115
        $this->outputFormatTableImageStyle($list);
116
        $this->output();
117
    }
118
119
    /**
120
     * Returns an array of image styles
121
     * @return array
122
     */
123
    protected function getListImageStyle()
124
    {
125
        $id = $this->getParam(0);
126
127
        if (isset($id)) {
128
            $result = $this->image_style->get($id);
129
            if (empty($result)) {
130
                $this->errorExit($this->text('Invalid ID'));
131
            }
132
            return array($result);
133
        }
134
135
        $list = $this->image_style->getList();
136
        $this->limitItems($list);
137
        return $list;
138
    }
139
140
    /**
141
     * Output image styles in a table
142
     * @param array $items
143
     */
144
    protected function outputFormatTableImageStyle(array $items)
145
    {
146
        $header = array(
147
            $this->text('ID'),
148
            $this->text('Name'),
149
            $this->text('Default'),
150
            $this->text('In database'),
151
            $this->text('Enabled')
152
        );
153
154
        $rows = array();
155
        foreach ($items as $item) {
156
            $rows[] = array(
157
                $item['imagestyle_id'],
158
                $this->text($item['name']),
159
                empty($item['default']) ? $this->text('No') : $this->text('Yes'),
160
                empty($item['in_database']) ? $this->text('No') : $this->text('Yes'),
161
                empty($item['status']) ? $this->text('No') : $this->text('Yes'),
162
            );
163
        }
164
165
        $this->outputFormatTable($rows, $header);
166
    }
167
168
    /**
169
     * Adds an image style
170
     */
171
    protected function addImageStyle()
172
    {
173
        if (!$this->isError()) {
174
            $id = $this->image_style->add($this->getSubmitted());
175
            if (empty($id) || !is_int($id)) {
176
                $this->errorExit($this->text('Image style has not been added'));
177
            }
178
            $this->line($id);
179
        }
180
    }
181
182
    /**
183
     * Updates an image style
184
     * @param int $imagestyle_id
185
     */
186
    protected function updateImageStyle($imagestyle_id)
187
    {
188
        if (!$this->isError()) {
189
            if (!$this->image_style->update($imagestyle_id, $this->getSubmitted())) {
190
                $this->errorExit($this->text('Image style has not been updated'));
191
            }
192
        }
193
    }
194
195
    /**
196
     * Add an image style at once
197
     */
198
    protected function submitAddImageStyle()
199
    {
200
        $this->setSubmitted(null, $this->getParam());
201
        $this->setSubmittedActionsImageStyle();
202
        $this->validateComponent('image_style');
203
        $this->addImageStyle();
204
    }
205
206
    /**
207
     * Updates an image style
208
     */
209
    protected function submitUpdateImageStyle()
210
    {
211
        $params = $this->getParam();
212
        if (empty($params[0]) || count($params) < 2) {
213
            $this->errorExit($this->text('Invalid command'));
214
        }
215
216
        $this->setSubmitted(null, $params);
217
        $this->setSubmittedActionsImageStyle();
218
        $this->setSubmitted('update', $params[0]);
219
220
        $this->validateComponent('image_style');
221
        $this->updateImageStyle($params[0]);
222
    }
223
224
    /**
225
     * Adds an image style step-by-step
226
     */
227
    protected function wizardAddImageStyle()
228
    {
229
        $this->validateInput('name', $this->text('Name'), 'image_style');
230
        $input = $this->validateInputActionsImageStyle();
231
        $this->validateInput('status', $this->text('Status'), 'image_style', 0);
232
233
        // Restore original actions input
234
        // After validation it gets modified
235
        $this->setSubmitted('actions', $input);
236
        $this->setSubmittedActionsImageStyle();
237
238
        $this->validateComponent('image_style');
239
        $this->addImageStyle();
240
    }
241
242
    /**
243
     * Validate actions
244
     * @return string
245
     */
246
    protected function validateInputActionsImageStyle()
247
    {
248
        $input = $this->prompt($this->text('Actions'));
249
        if (!$this->isValidInput($this->explodeByPipe($input), 'actions', 'image_style')) {
250
            $this->errors();
251
            $this->validateInputActionsImageStyle();
252
        }
253
254
        return $input;
255
    }
256
257
    /**
258
     * Sets image style actions
259
     */
260
    protected function setSubmittedActionsImageStyle()
261
    {
262
        $actions = $this->getSubmitted('actions');
263
        if (isset($actions)) {
264
            $this->setSubmitted('actions', $this->explodeByPipe($actions));
265
        }
266
    }
267
268
}
269