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\commands; |
11
|
|
|
|
12
|
|
|
use gplcart\core\models\ImageStyle as ImageStyleModel; |
13
|
|
|
use gplcart\modules\cli\controllers\Command; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Handles commands related to image styles |
17
|
|
|
*/ |
18
|
|
|
class ImageStyle extends Command |
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
|
|
|
*/ |
40
|
|
|
public function cmdUpdateImageStyle() |
41
|
|
|
{ |
42
|
|
|
$params = $this->getParam(); |
43
|
|
|
|
44
|
|
|
if (empty($params[0]) || count($params) < 2) { |
45
|
|
|
$this->errorAndExit($this->text('Invalid command')); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (!is_numeric($params[0])) { |
49
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->setSubmitted(null, $params); |
53
|
|
|
$this->setSubmittedList('actions'); |
54
|
|
|
$this->setSubmitted('update', $params[0]); |
55
|
|
|
|
56
|
|
|
$this->validateComponent('image_style'); |
57
|
|
|
$this->updateImageStyle($params[0]); |
58
|
|
|
$this->output(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Callback for "imagestyle-add" |
63
|
|
|
*/ |
64
|
|
|
public function cmdAddImageStyle() |
65
|
|
|
{ |
66
|
|
|
if ($this->getParam()) { |
67
|
|
|
$this->submitAddImageStyle(); |
68
|
|
|
} else { |
69
|
|
|
$this->wizardAddImageStyle(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->output(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Callback for "imagestyle-delete" command |
77
|
|
|
*/ |
78
|
|
|
public function cmdDeleteImageStyle() |
79
|
|
|
{ |
80
|
|
|
$id = $this->getParam(0); |
81
|
|
|
|
82
|
|
|
if (empty($id) || !is_numeric($id)) { |
83
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (!$this->image_style->delete($id)) { |
87
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->output(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Callback for "imagestyle-clear" command |
95
|
|
|
*/ |
96
|
|
|
public function cmdClearImageStyle() |
97
|
|
|
{ |
98
|
|
|
$id = $this->getParam(0); |
99
|
|
|
$all = $this->getParam('all'); |
100
|
|
|
|
101
|
|
|
if (!isset($id) && empty($all)) { |
102
|
|
|
$this->errorAndExit($this->text('Invalid command')); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$result = false; |
106
|
|
|
|
107
|
|
|
if (isset($id)) { |
108
|
|
|
|
109
|
|
|
if (!is_numeric($id)) { |
110
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$result = $this->image_style->clearCache($id); |
114
|
|
|
} else if (!empty($all)) { |
115
|
|
|
$result = $this->image_style->clearCache(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (empty($result)) { |
119
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->output(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Callback for "imagestyle-get" command |
127
|
|
|
*/ |
128
|
|
|
public function cmdGetImageStyle() |
129
|
|
|
{ |
130
|
|
|
$list = $this->getListImageStyle(); |
131
|
|
|
$this->outputFormat($list); |
132
|
|
|
$this->outputFormatTableImageStyle($list); |
133
|
|
|
$this->output(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Returns an array of image styles |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
protected function getListImageStyle() |
141
|
|
|
{ |
142
|
|
|
$id = $this->getParam(0); |
143
|
|
|
|
144
|
|
|
if (!isset($id)) { |
145
|
|
|
$list = $this->image_style->getList(); |
146
|
|
|
$this->limitArray($list); |
147
|
|
|
return $list; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if (!is_numeric($id)) { |
151
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$result = $this->image_style->get($id); |
155
|
|
|
|
156
|
|
|
if (empty($result)) { |
157
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return array($result); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Output image styles in a table |
165
|
|
|
* @param array $items |
166
|
|
|
*/ |
167
|
|
|
protected function outputFormatTableImageStyle(array $items) |
168
|
|
|
{ |
169
|
|
|
$header = array( |
170
|
|
|
$this->text('ID'), |
171
|
|
|
$this->text('Name'), |
172
|
|
|
$this->text('Default'), |
173
|
|
|
$this->text('In database'), |
174
|
|
|
$this->text('Enabled') |
175
|
|
|
); |
176
|
|
|
|
177
|
|
|
$rows = array(); |
178
|
|
|
|
179
|
|
|
foreach ($items as $item) { |
180
|
|
|
$rows[] = array( |
181
|
|
|
$item['imagestyle_id'], |
182
|
|
|
$this->text($item['name']), |
183
|
|
|
empty($item['default']) ? $this->text('No') : $this->text('Yes'), |
184
|
|
|
empty($item['in_database']) ? $this->text('No') : $this->text('Yes'), |
185
|
|
|
empty($item['status']) ? $this->text('No') : $this->text('Yes'), |
186
|
|
|
); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$this->outputFormatTable($rows, $header); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Adds an image style |
194
|
|
|
*/ |
195
|
|
|
protected function addImageStyle() |
196
|
|
|
{ |
197
|
|
|
if (!$this->isError()) { |
198
|
|
|
$id = $this->image_style->add($this->getSubmitted()); |
199
|
|
|
if (empty($id)) { |
200
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
201
|
|
|
} |
202
|
|
|
$this->line($id); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Updates an image style |
208
|
|
|
* @param int $imagestyle_id |
209
|
|
|
*/ |
210
|
|
|
protected function updateImageStyle($imagestyle_id) |
211
|
|
|
{ |
212
|
|
|
if (!$this->isError() && !$this->image_style->update($imagestyle_id, $this->getSubmitted())) { |
213
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Add an image style at once |
219
|
|
|
*/ |
220
|
|
|
protected function submitAddImageStyle() |
221
|
|
|
{ |
222
|
|
|
$this->setSubmitted(null, $this->getParam()); |
223
|
|
|
$this->setSubmittedList('actions'); |
224
|
|
|
$this->validateComponent('image_style'); |
225
|
|
|
$this->addImageStyle(); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Adds an image style step-by-step |
230
|
|
|
*/ |
231
|
|
|
protected function wizardAddImageStyle() |
232
|
|
|
{ |
233
|
|
|
$this->validatePrompt('name', $this->text('Name'), 'image_style'); |
234
|
|
|
$this->validatePromptList('actions', $this->text('Actions'), 'image_style'); |
235
|
|
|
$this->validatePrompt('status', $this->text('Status'), 'image_style', 0); |
236
|
|
|
$this->setSubmittedList('actions'); |
237
|
|
|
|
238
|
|
|
$this->validateComponent('image_style'); |
239
|
|
|
$this->addImageStyle(); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
} |
243
|
|
|
|