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