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\Currency as CurrencyModel; |
13
|
|
|
use gplcart\core\models\Product as ProductModel; |
14
|
|
|
use gplcart\modules\cli\controllers\Command; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Handles commands related to products |
18
|
|
|
*/ |
19
|
|
|
class Product extends Command |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Product model instance |
24
|
|
|
* @var \gplcart\core\models\Product $product |
25
|
|
|
*/ |
26
|
|
|
protected $product; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Currency model instance |
30
|
|
|
* @var \gplcart\core\models\Currency $currency |
31
|
|
|
*/ |
32
|
|
|
protected $currency; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param ProductModel $product |
36
|
|
|
* @param CurrencyModel $currency |
37
|
|
|
*/ |
38
|
|
|
public function __construct(ProductModel $product, CurrencyModel $currency) |
39
|
|
|
{ |
40
|
|
|
parent::__construct(); |
41
|
|
|
|
42
|
|
|
$this->product = $product; |
43
|
|
|
$this->currency = $currency; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Callback for "product-get" command |
48
|
|
|
*/ |
49
|
|
|
public function cmdGetProduct() |
50
|
|
|
{ |
51
|
|
|
$result = $this->getListProduct(); |
52
|
|
|
$this->outputFormat($result); |
53
|
|
|
$this->outputFormatTableProduct($result); |
54
|
|
|
$this->output(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Callback for "product-delete" command |
59
|
|
|
*/ |
60
|
|
|
public function cmdDeleteProduct() |
61
|
|
|
{ |
62
|
|
|
$id = $this->getParam(0); |
63
|
|
|
$all = $this->getParam('all'); |
64
|
|
|
|
65
|
|
|
if (!isset($id) && empty($all)) { |
66
|
|
|
$this->errorAndExit($this->text('Invalid command')); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (isset($id) && !is_numeric($id)) { |
70
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$options = null; |
74
|
|
|
|
75
|
|
|
if (isset($id)) { |
76
|
|
|
|
77
|
|
|
if ($this->getParam('user')) { |
78
|
|
|
$options = array('user_id' => $id); |
79
|
|
|
} else if ($this->getParam('store')) { |
80
|
|
|
$options = array('store_id' => $id); |
81
|
|
|
} else if ($this->getParam('category')) { |
82
|
|
|
$options = array('category_id' => $id); |
83
|
|
|
} else if ($this->getParam('class')) { |
84
|
|
|
$options = array('product_class_id' => $id); |
85
|
|
|
} else if ($this->getParam('brand')) { |
86
|
|
|
$options = array('brand_category_id' => $id); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
} else if ($all) { |
90
|
|
|
$options = array(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (isset($options)) { |
94
|
|
|
|
95
|
|
|
$deleted = $count = 0; |
96
|
|
|
foreach ($this->product->getList($options) as $item) { |
97
|
|
|
$count++; |
98
|
|
|
$deleted += (int) $this->product->delete($item['product_id']); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$result = $count && $count == $deleted; |
102
|
|
|
|
103
|
|
|
} else if (!empty($id)) { |
104
|
|
|
$result = $this->product->delete($id); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (empty($result)) { |
108
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$this->output(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Callback for "product-add" command |
116
|
|
|
*/ |
117
|
|
|
public function cmdAddProduct() |
118
|
|
|
{ |
119
|
|
|
if ($this->getParam()) { |
120
|
|
|
$this->submitAddProduct(); |
121
|
|
|
} else { |
122
|
|
|
$this->wizardAddProduct(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$this->output(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Callback for "product-update" command |
130
|
|
|
*/ |
131
|
|
|
public function cmdUpdateProduct() |
132
|
|
|
{ |
133
|
|
|
$params = $this->getParam(); |
134
|
|
|
|
135
|
|
|
if (empty($params[0]) || count($params) < 2) { |
136
|
|
|
$this->errorAndExit($this->text('Invalid command')); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if (!is_numeric($params[0])) { |
140
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$this->setSubmitted(null, $params); |
144
|
|
|
$this->setSubmitted('update', $params[0]); |
145
|
|
|
$this->setSubmitted('form', true); // Internal flag. Specifies that the product submitted manually |
146
|
|
|
|
147
|
|
|
$this->validateComponent('product'); |
148
|
|
|
$this->updateProduct($params[0]); |
149
|
|
|
$this->output(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Returns an array of products |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
protected function getListProduct() |
157
|
|
|
{ |
158
|
|
|
$id = $this->getParam(0); |
159
|
|
|
|
160
|
|
|
if (!isset($id)) { |
161
|
|
|
return $this->product->getList(array('limit' => $this->getLimit())); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if (!is_numeric($id)) { |
165
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if ($this->getParam('user')) { |
169
|
|
|
return $this->product->getList(array('user_id' => $id, 'limit' => $this->getLimit())); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if ($this->getParam('category')) { |
173
|
|
|
return $this->product->getList(array('category_id' => $id, 'limit' => $this->getLimit())); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
if ($this->getParam('brand')) { |
177
|
|
|
return $this->product->getList(array('brand_category_id' => $id, 'limit' => $this->getLimit())); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if ($this->getParam('class')) { |
181
|
|
|
return $this->product->getList(array('product_class_id' => $id, 'limit' => $this->getLimit())); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
if ($this->getParam('store')) { |
185
|
|
|
return $this->product->getList(array('store_id' => $id, 'limit' => $this->getLimit())); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$result = $this->product->get($id); |
189
|
|
|
|
190
|
|
|
if (empty($result)) { |
191
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return array($result); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Output table format |
199
|
|
|
* @param array $items |
200
|
|
|
*/ |
201
|
|
|
protected function outputFormatTableProduct(array $items) |
202
|
|
|
{ |
203
|
|
|
$header = array( |
204
|
|
|
$this->text('ID'), |
205
|
|
|
$this->text('Title'), |
206
|
|
|
$this->text('Category'), |
207
|
|
|
$this->text('Brand'), |
208
|
|
|
$this->text('Product class'), |
209
|
|
|
$this->text('Price'), |
210
|
|
|
$this->text('Currency'), |
211
|
|
|
$this->text('Store'), |
212
|
|
|
$this->text('User'), |
213
|
|
|
$this->text('Enabled') |
214
|
|
|
); |
215
|
|
|
|
216
|
|
|
$rows = array(); |
217
|
|
|
|
218
|
|
|
foreach ($items as $item) { |
219
|
|
|
$rows[] = array( |
220
|
|
|
$item['product_id'], |
221
|
|
|
$this->truncate($item['title'], 50), |
222
|
|
|
$item['category_id'], |
223
|
|
|
$item['brand_category_id'], |
224
|
|
|
$item['product_class_id'], |
225
|
|
|
$item['price'], |
226
|
|
|
$item['currency'], |
227
|
|
|
$item['store_id'], |
228
|
|
|
$item['user_id'], |
229
|
|
|
empty($item['status']) ? $this->text('No') : $this->text('Yes') |
230
|
|
|
); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$this->outputFormatTable($rows, $header); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Add a new product |
238
|
|
|
*/ |
239
|
|
|
protected function addProduct() |
240
|
|
|
{ |
241
|
|
|
if (!$this->isError()) { |
242
|
|
|
$id = $this->product->add($this->getSubmitted()); |
243
|
|
|
if (empty($id)) { |
244
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
245
|
|
|
} |
246
|
|
|
$this->line($id); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Updates a product |
252
|
|
|
* @param string $product_id |
253
|
|
|
*/ |
254
|
|
|
protected function updateProduct($product_id) |
255
|
|
|
{ |
256
|
|
|
if (!$this->isError() && !$this->product->update($product_id, $this->getSubmitted())) { |
257
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Add a new product at once |
263
|
|
|
*/ |
264
|
|
|
protected function submitAddProduct() |
265
|
|
|
{ |
266
|
|
|
$this->setSubmitted(null, $this->getParam()); |
267
|
|
|
$this->setSubmitted('form', true); // Internal flag. Specifies that the product submitted manually |
268
|
|
|
$this->validateComponent('product'); |
269
|
|
|
$this->addProduct(); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Add a new product step by step |
274
|
|
|
*/ |
275
|
|
|
protected function wizardAddProduct() |
276
|
|
|
{ |
277
|
|
|
// Required |
278
|
|
|
$this->validatePrompt('title', $this->text('Title'), 'product'); |
279
|
|
|
|
280
|
|
|
// Optional |
281
|
|
|
$this->validatePrompt('description', $this->text('Description'), 'product'); |
282
|
|
|
$this->validatePrompt('store_id', $this->text('Store ID'), 'product', 0); |
283
|
|
|
$this->validatePrompt('sku', $this->text('SKU'), 'product', ''); |
284
|
|
|
$this->validatePrompt('stock', $this->text('Stock'), 'product', 0); |
285
|
|
|
$this->validatePrompt('price', $this->text('Price'), 'product', 0); |
286
|
|
|
$this->validatePrompt('currency', $this->text('Currency code'), 'product', $this->currency->getDefault()); |
287
|
|
|
$this->validatePrompt('product_class_id', $this->text('Product class ID'), 'product', 0); |
288
|
|
|
$this->validatePrompt('user_id', $this->text('User ID'), 'product', 0); |
289
|
|
|
$this->validatePrompt('meta_title', $this->text('Meta title'), 'product', ''); |
290
|
|
|
$this->validatePrompt('meta_description', $this->text('Meta description'), 'product', ''); |
291
|
|
|
$this->validatePrompt('category_id', $this->text('Category ID'), 'product', 0); |
292
|
|
|
$this->validatePrompt('brand_category_id', $this->text('Brand category ID'), 'product', 0); |
293
|
|
|
|
294
|
|
|
$size_units = $this->product->getSizeUnits(); |
295
|
|
|
$this->validateMenu('size_unit', $this->text('Size unit'), 'product', $size_units, key($size_units)); |
296
|
|
|
|
297
|
|
|
$this->validatePrompt('length', $this->text('Length'), 'product', 0); |
298
|
|
|
$this->validatePrompt('width', $this->text('Width'), 'product', 0); |
299
|
|
|
$this->validatePrompt('height', $this->text('Height'), 'product', 0); |
300
|
|
|
|
301
|
|
|
$weight_units = $this->product->getWeightUnits(); |
302
|
|
|
$this->validateMenu('weight_unit', $this->text('Weight unit'), 'product', $weight_units, key($weight_units)); |
303
|
|
|
$this->validatePrompt('weight', $this->text('Weight'), 'product', 0); |
304
|
|
|
|
305
|
|
|
$this->validatePrompt('subtract', $this->text('Subtract'), 'page', 0); |
306
|
|
|
$this->validatePrompt('status', $this->text('Status'), 'page', 0); |
307
|
|
|
|
308
|
|
|
$this->setSubmitted('form', true); // Internal flag. Specifies that the product submitted manually |
309
|
|
|
$this->validateComponent('product'); |
310
|
|
|
$this->addProduct(); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
} |
314
|
|
|
|