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\ProductClass as ProductClassModel; |
13
|
|
|
use gplcart\modules\cli\controllers\Command; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Handles commands related to product classes |
17
|
|
|
*/ |
18
|
|
|
class ProductClass extends Command |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Product class model instance |
23
|
|
|
* @var \gplcart\core\models\ProductClass $product_class |
24
|
|
|
*/ |
25
|
|
|
protected $product_class; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ProductClassModel $product_class |
29
|
|
|
*/ |
30
|
|
|
public function __construct(ProductClassModel $product_class) |
31
|
|
|
{ |
32
|
|
|
parent::__construct(); |
33
|
|
|
|
34
|
|
|
$this->product_class = $product_class; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Callback for "product-class-get" command |
39
|
|
|
*/ |
40
|
|
|
public function cmdGetProductClass() |
41
|
|
|
{ |
42
|
|
|
$result = $this->getListProductClass(); |
43
|
|
|
$this->outputFormat($result); |
44
|
|
|
$this->outputFormatTableProductClass($result); |
45
|
|
|
$this->output(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Callback for "product-class-delete" command |
50
|
|
|
*/ |
51
|
|
|
public function cmdDeleteProductClass() |
52
|
|
|
{ |
53
|
|
|
$id = $this->getParam(0); |
54
|
|
|
|
55
|
|
|
$result = null; |
56
|
|
|
|
57
|
|
|
if (isset($id)) { |
58
|
|
|
|
59
|
|
|
if (empty($id) || !is_numeric($id)) { |
60
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$result = $this->product_class->delete($id); |
64
|
|
|
|
65
|
|
|
} else if ($this->getParam('all')) { |
66
|
|
|
|
67
|
|
|
$deleted = $count = 0; |
68
|
|
|
foreach ($this->product_class->getList() as $item) { |
69
|
|
|
$count++; |
70
|
|
|
$deleted += (int) $this->product_class->delete($item['product_class_id']); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$result = $count && $count == $deleted; |
74
|
|
|
|
75
|
|
|
} else { |
76
|
|
|
$this->errorAndExit($this->text('Invalid command')); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (empty($result)) { |
80
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->output(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Callback for "product-class-add" command |
88
|
|
|
*/ |
89
|
|
|
public function cmdAddProductClass() |
90
|
|
|
{ |
91
|
|
|
if ($this->getParam()) { |
92
|
|
|
$this->submitAddProductClass(); |
93
|
|
|
} else { |
94
|
|
|
$this->wizardAddProductClass(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->output(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Callback for "product-class-update" command |
102
|
|
|
*/ |
103
|
|
|
public function cmdUpdateProductClass() |
104
|
|
|
{ |
105
|
|
|
$params = $this->getParam(); |
106
|
|
|
|
107
|
|
|
if (empty($params[0]) || count($params) < 2) { |
108
|
|
|
$this->errorAndExit($this->text('Invalid command')); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (!is_numeric($params[0])) { |
112
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->setSubmitted(null, $params); |
116
|
|
|
$this->setSubmitted('update', $params[0]); |
117
|
|
|
$this->validateComponent('product_class'); |
118
|
|
|
|
119
|
|
|
$this->updateProductClass($params[0]); |
120
|
|
|
$this->output(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Returns an array of product classes |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
protected function getListProductClass() |
128
|
|
|
{ |
129
|
|
|
$id = $this->getParam(0); |
130
|
|
|
|
131
|
|
|
if (!isset($id)) { |
132
|
|
|
return $this->product_class->getList(array('limit' => $this->getLimit())); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (!is_numeric($id)) { |
136
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$result = $this->product_class->get($id); |
140
|
|
|
|
141
|
|
|
if (empty($result)) { |
142
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return array($result); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Output table format |
150
|
|
|
* @param array $items |
151
|
|
|
*/ |
152
|
|
|
protected function outputFormatTableProductClass(array $items) |
153
|
|
|
{ |
154
|
|
|
$header = array( |
155
|
|
|
$this->text('ID'), |
156
|
|
|
$this->text('Title'), |
157
|
|
|
$this->text('Enabled') |
158
|
|
|
); |
159
|
|
|
|
160
|
|
|
$rows = array(); |
161
|
|
|
|
162
|
|
|
foreach ($items as $item) { |
163
|
|
|
$rows[] = array( |
164
|
|
|
$item['product_class_id'], |
165
|
|
|
$item['title'], |
166
|
|
|
empty($item['status']) ? $this->text('No') : $this->text('Yes') |
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$this->outputFormatTable($rows, $header); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Add a new product class |
175
|
|
|
*/ |
176
|
|
|
protected function addProductClass() |
177
|
|
|
{ |
178
|
|
|
if (!$this->isError()) { |
179
|
|
|
$id = $this->product_class->add($this->getSubmitted()); |
180
|
|
|
if (empty($id)) { |
181
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
182
|
|
|
} |
183
|
|
|
$this->line($id); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Updates a product class |
189
|
|
|
* @param string $product_class_id |
190
|
|
|
*/ |
191
|
|
|
protected function updateProductClass($product_class_id) |
192
|
|
|
{ |
193
|
|
|
if (!$this->isError() && !$this->product_class->update($product_class_id, $this->getSubmitted())) { |
194
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Add a new product class at once |
200
|
|
|
*/ |
201
|
|
|
protected function submitAddProductClass() |
202
|
|
|
{ |
203
|
|
|
$this->setSubmitted(null, $this->getParam()); |
204
|
|
|
$this->validateComponent('product_class'); |
205
|
|
|
$this->addProductClass(); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Add a new product class step by step |
210
|
|
|
*/ |
211
|
|
|
protected function wizardAddProductClass() |
212
|
|
|
{ |
213
|
|
|
$this->validatePrompt('title', $this->text('Title'), 'product_class'); |
214
|
|
|
$this->validatePrompt('status', $this->text('Status'), 'product_class', 0); |
215
|
|
|
$this->validateComponent('product_class'); |
216
|
|
|
$this->addProductClass(); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
} |
220
|
|
|
|