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\Category as CategoryModel; |
13
|
|
|
use gplcart\modules\cli\controllers\Command; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Handles commands related to categories |
17
|
|
|
*/ |
18
|
|
|
class Category extends Command |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Category model instance |
23
|
|
|
* @var \gplcart\core\models\Category $category |
24
|
|
|
*/ |
25
|
|
|
protected $category; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param CategoryModel $category |
29
|
|
|
*/ |
30
|
|
|
public function __construct(CategoryModel $category) |
31
|
|
|
{ |
32
|
|
|
parent::__construct(); |
33
|
|
|
|
34
|
|
|
$this->category = $category; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Callback for "category-get" command |
39
|
|
|
*/ |
40
|
|
|
public function cmdGetCategory() |
41
|
|
|
{ |
42
|
|
|
$result = $this->getListCategory(); |
43
|
|
|
$this->outputFormat($result); |
44
|
|
|
$this->outputFormatTableCategory($result); |
45
|
|
|
$this->output(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Callback for "category-delete" command |
50
|
|
|
*/ |
51
|
|
|
public function cmdDeleteCategory() |
52
|
|
|
{ |
53
|
|
|
$id = $this->getParam(0); |
54
|
|
|
$all = $this->getParam('all'); |
55
|
|
|
|
56
|
|
|
if (empty($id) && empty($all)) { |
57
|
|
|
$this->errorAndExit($this->text('Invalid command')); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (empty($all) && (empty($id) || !is_numeric($id))) { |
61
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$options = null; |
65
|
|
|
|
66
|
|
|
if ($this->getParam('group')) { |
67
|
|
|
$options = array('category_group_id' => $id); |
68
|
|
|
} else if ($this->getParam('store')) { |
69
|
|
|
$options = array('store_id' => $id); |
70
|
|
|
} else if (!empty($all)) { |
71
|
|
|
$options = array(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (isset($options)) { |
75
|
|
|
|
76
|
|
|
$deleted = $count = 0; |
77
|
|
|
foreach ($this->category->getList($options) as $item) { |
78
|
|
|
$count++; |
79
|
|
|
$deleted += (int) $this->category->delete($item['category_id']); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$result = $count && $count == $deleted; |
83
|
|
|
|
84
|
|
|
} else { |
85
|
|
|
|
86
|
|
|
$result = $this->category->delete($id); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (empty($result)) { |
90
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->output(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Callback for "category-add" command |
98
|
|
|
*/ |
99
|
|
|
public function cmdAddCategory() |
100
|
|
|
{ |
101
|
|
|
if ($this->getParam()) { |
102
|
|
|
$this->submitAddCategory(); |
103
|
|
|
} else { |
104
|
|
|
$this->wizardAddCategory(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->output(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Callback for "category-update" command |
112
|
|
|
*/ |
113
|
|
|
public function cmdUpdateCategory() |
114
|
|
|
{ |
115
|
|
|
$params = $this->getParam(); |
116
|
|
|
|
117
|
|
|
if (empty($params[0]) || count($params) < 2) { |
118
|
|
|
$this->errorAndExit($this->text('Invalid command')); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (!is_numeric($params[0])) { |
122
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$this->setSubmitted(null, $params); |
126
|
|
|
$this->setSubmitted('update', $params[0]); |
127
|
|
|
$this->validateComponent('category'); |
128
|
|
|
|
129
|
|
|
$this->updateCategory($params[0]); |
130
|
|
|
$this->output(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Returns an array of categories |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
|
protected function getListCategory() |
138
|
|
|
{ |
139
|
|
|
$id = $this->getParam(0); |
140
|
|
|
|
141
|
|
|
if (!isset($id)) { |
142
|
|
|
return $this->category->getList(array('limit' => $this->getLimit())); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (!is_numeric($id)) { |
146
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if ($this->getParam('group')) { |
150
|
|
|
return $this->category->getList(array('category_group_id' => $id, 'limit' => $this->getLimit())); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if ($this->getParam('parent')) { |
154
|
|
|
return $this->category->getList(array('parent_id' => $id, 'limit' => $this->getLimit())); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$result = $this->category->get($id); |
158
|
|
|
|
159
|
|
|
if (empty($result)) { |
160
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return array($result); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Output table format |
168
|
|
|
* @param array $items |
169
|
|
|
*/ |
170
|
|
|
protected function outputFormatTableCategory(array $items) |
171
|
|
|
{ |
172
|
|
|
$header = array( |
173
|
|
|
$this->text('ID'), |
174
|
|
|
$this->text('Title'), |
175
|
|
|
$this->text('Parent category'), |
176
|
|
|
$this->text('Alias'), |
177
|
|
|
$this->text('Weight'), |
178
|
|
|
$this->text('Enabled') |
179
|
|
|
); |
180
|
|
|
|
181
|
|
|
$rows = array(); |
182
|
|
|
|
183
|
|
|
foreach ($items as $item) { |
184
|
|
|
$rows[] = array( |
185
|
|
|
$item['category_id'], |
186
|
|
|
$item['title'], |
187
|
|
|
$item['parent_id'], |
188
|
|
|
$item['alias'], |
189
|
|
|
$item['weight'], |
190
|
|
|
empty($item['status']) ? $this->text('No') : $this->text('Yes') |
191
|
|
|
); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$this->outputFormatTable($rows, $header); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Add a new category |
199
|
|
|
*/ |
200
|
|
|
protected function addCategory() |
201
|
|
|
{ |
202
|
|
|
if (!$this->isError()) { |
203
|
|
|
$id = $this->category->add($this->getSubmitted()); |
204
|
|
|
if (empty($id)) { |
205
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
206
|
|
|
} |
207
|
|
|
$this->line($id); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Updates a category |
213
|
|
|
* @param string $category_id |
214
|
|
|
*/ |
215
|
|
|
protected function updateCategory($category_id) |
216
|
|
|
{ |
217
|
|
|
if (!$this->isError() && !$this->category->update($category_id, $this->getSubmitted())) { |
218
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Add a new category at once |
224
|
|
|
*/ |
225
|
|
|
protected function submitAddCategory() |
226
|
|
|
{ |
227
|
|
|
$this->setSubmitted(null, $this->getParam()); |
228
|
|
|
$this->validateComponent('category'); |
229
|
|
|
$this->addCategory(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Add a new category step by step |
234
|
|
|
*/ |
235
|
|
|
protected function wizardAddCategory() |
236
|
|
|
{ |
237
|
|
|
// Required |
238
|
|
|
$this->validatePrompt('title', $this->text('Title'), 'category'); |
239
|
|
|
$this->validatePrompt('category_group_id', $this->text('Category group ID'), 'category'); |
240
|
|
|
|
241
|
|
|
// Optional |
242
|
|
|
$this->validatePrompt('parent_id', $this->text('Parent category ID'), 'category', 0); |
243
|
|
|
$this->validatePrompt('description_1', $this->text('First description'), 'category', ''); |
244
|
|
|
$this->validatePrompt('description_2', $this->text('Second description'), 'category', ''); |
245
|
|
|
$this->validatePrompt('meta_title', $this->text('Meta title'), 'category', ''); |
246
|
|
|
$this->validatePrompt('meta_description', $this->text('Meta description'), 'category', ''); |
247
|
|
|
$this->validatePrompt('status', $this->text('Status'), 'category', 0); |
248
|
|
|
$this->validatePrompt('weight', $this->text('Weight'), 'category', 0); |
249
|
|
|
|
250
|
|
|
$this->validateComponent('category'); |
251
|
|
|
$this->addCategory(); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
} |
255
|
|
|
|