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