|
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; |
|
11
|
|
|
|
|
12
|
|
|
use gplcart\core\models\Store as StoreModel; |
|
13
|
|
|
use gplcart\modules\cli\controllers\Base; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Handles commands related to stores |
|
17
|
|
|
*/ |
|
18
|
|
|
class Store extends Base |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Store model instance |
|
23
|
|
|
* @var \gplcart\core\models\Store $store |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $store; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param StoreModel $store |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(StoreModel $store) |
|
31
|
|
|
{ |
|
32
|
|
|
parent::__construct(); |
|
33
|
|
|
|
|
34
|
|
|
$this->store = $store; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Callback for "store-on" command |
|
39
|
|
|
*/ |
|
40
|
|
|
public function cmdOnStore() |
|
41
|
|
|
{ |
|
42
|
|
|
$this->setStatusStore(true); |
|
43
|
|
|
$this->output(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Callback for "store-off" command |
|
48
|
|
|
*/ |
|
49
|
|
|
public function cmdOffStore() |
|
50
|
|
|
{ |
|
51
|
|
|
$this->setStatusStore(false); |
|
52
|
|
|
$this->output(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Callback for "store-get" command |
|
57
|
|
|
*/ |
|
58
|
|
|
public function cmdGetStore() |
|
59
|
|
|
{ |
|
60
|
|
|
$result = $this->getListStore(); |
|
61
|
|
|
$this->outputFormat($result); |
|
62
|
|
|
$this->outputFormatTableStore($result); |
|
63
|
|
|
$this->output(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Callback for "store-delete" command |
|
68
|
|
|
*/ |
|
69
|
|
|
public function cmdDeleteStore() |
|
70
|
|
|
{ |
|
71
|
|
|
$id = $this->getParam(0); |
|
72
|
|
|
|
|
73
|
|
|
if (empty($id) || !is_numeric($id)) { |
|
74
|
|
|
$this->errorExit($this->text('Invalid ID')); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if (!$this->store->delete($id)) { |
|
78
|
|
|
$this->errorExit($this->text('An error occurred')); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$this->output(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Callback for "store-add" command |
|
86
|
|
|
*/ |
|
87
|
|
|
public function cmdAddStore() |
|
88
|
|
|
{ |
|
89
|
|
|
if ($this->getParam()) { |
|
90
|
|
|
$this->submitAddStore(); |
|
91
|
|
|
} else { |
|
92
|
|
|
$this->wizardAddStore(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$this->output(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Callback for "store-update" command |
|
100
|
|
|
*/ |
|
101
|
|
|
public function cmdUpdateStore() |
|
102
|
|
|
{ |
|
103
|
|
|
$this->submitUpdateStore(); |
|
104
|
|
|
$this->output(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Sets status for one or several stores |
|
109
|
|
|
* @param bool $status |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function setStatusStore($status) |
|
112
|
|
|
{ |
|
113
|
|
|
$id = $this->getParam(0); |
|
114
|
|
|
$all = $this->getParam('all'); |
|
115
|
|
|
|
|
116
|
|
|
if (empty($id) && empty($all)) { |
|
117
|
|
|
$this->errorExit($this->text('Invalid command')); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$result = false; |
|
121
|
|
|
|
|
122
|
|
|
if (!empty($id)) { |
|
123
|
|
|
if (!is_numeric($id)) { |
|
124
|
|
|
$this->errorExit($this->text('Invalid ID')); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$result = $this->store->update($id, array('status' => $status)); |
|
128
|
|
|
|
|
129
|
|
|
} else if (!empty($all)) { |
|
130
|
|
|
$updated = $count = 0; |
|
131
|
|
|
foreach ((array)$this->store->getList() as $store) { |
|
132
|
|
|
$count++; |
|
133
|
|
|
$updated += (int)$this->store->update($store['store_id'], array('status' => $status)); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$result = ($count == $updated); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
if (!$result) { |
|
140
|
|
|
$this->errorExit($this->text('An error occurred')); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Returns an array of stores |
|
146
|
|
|
* @return array |
|
147
|
|
|
*/ |
|
148
|
|
|
protected function getListStore() |
|
149
|
|
|
{ |
|
150
|
|
|
$id = $this->getParam(0); |
|
151
|
|
|
|
|
152
|
|
|
if (isset($id)) { |
|
153
|
|
|
$result = $this->store->get($id); |
|
154
|
|
|
if (empty($result)) { |
|
155
|
|
|
$this->errorExit($this->text('Invalid ID')); |
|
156
|
|
|
} |
|
157
|
|
|
return array($result); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$list = $this->store->getList(); |
|
161
|
|
|
$this->limitItems($list); |
|
162
|
|
|
return $list; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Output table format |
|
167
|
|
|
* @param array $items |
|
168
|
|
|
*/ |
|
169
|
|
|
protected function outputFormatTableStore(array $items) |
|
170
|
|
|
{ |
|
171
|
|
|
$header = array( |
|
172
|
|
|
$this->text('ID'), |
|
173
|
|
|
$this->text('Name'), |
|
174
|
|
|
$this->text('Domain'), |
|
175
|
|
|
$this->text('Path'), |
|
176
|
|
|
$this->text('Enabled') |
|
177
|
|
|
); |
|
178
|
|
|
|
|
179
|
|
|
$rows = array(); |
|
180
|
|
|
foreach ($items as $item) { |
|
181
|
|
|
$rows[] = array( |
|
182
|
|
|
$item['store_id'], |
|
183
|
|
|
$item['name'], |
|
184
|
|
|
$item['domain'], |
|
185
|
|
|
$item['basepath'], |
|
186
|
|
|
empty($item['status']) ? $this->text('No') : $this->text('Yes') |
|
187
|
|
|
); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
$this->outputFormatTable($rows, $header); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Updates a store |
|
195
|
|
|
*/ |
|
196
|
|
|
protected function submitUpdateStore() |
|
197
|
|
|
{ |
|
198
|
|
|
$params = $this->getParam(); |
|
199
|
|
|
|
|
200
|
|
|
if (empty($params[0]) || count($params) < 2) { |
|
201
|
|
|
$this->errorExit($this->text('Invalid command')); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
$this->setSubmitted(null, $this->getParam()); |
|
205
|
|
|
$this->setSubmitted('update', $params[0]); |
|
206
|
|
|
$this->setSubmittedDataStore(); |
|
207
|
|
|
|
|
208
|
|
|
$this->validateComponent('store'); |
|
209
|
|
|
$this->updateStore($params[0]); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Updates a store |
|
214
|
|
|
* @param string $store_id |
|
215
|
|
|
*/ |
|
216
|
|
|
protected function updateStore($store_id) |
|
217
|
|
|
{ |
|
218
|
|
|
if (!$this->isError() && !$this->store->update($store_id, $this->getSubmitted())) { |
|
219
|
|
|
$this->errorExit($this->text('Store has not been updated')); |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Add a new store at once |
|
225
|
|
|
*/ |
|
226
|
|
|
protected function submitAddStore() |
|
227
|
|
|
{ |
|
228
|
|
|
$this->setSubmitted(null, $this->getParam()); |
|
229
|
|
|
$this->setSubmittedDataStore(); |
|
230
|
|
|
|
|
231
|
|
|
$this->validateComponent('store'); |
|
232
|
|
|
$this->addStore(); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Add a store |
|
237
|
|
|
*/ |
|
238
|
|
|
protected function addStore() |
|
239
|
|
|
{ |
|
240
|
|
|
if (!$this->isError()) { |
|
241
|
|
|
$id = $this->store->add($this->getSubmitted()); |
|
242
|
|
|
if (empty($id)) { |
|
243
|
|
|
$this->errorExit($this->text('Store has not been added')); |
|
244
|
|
|
} |
|
245
|
|
|
$this->line($id); |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Add a new store step by step |
|
251
|
|
|
*/ |
|
252
|
|
|
protected function wizardAddStore() |
|
253
|
|
|
{ |
|
254
|
|
|
$this->validateInput('name', $this->text('Name'), 'store'); |
|
255
|
|
|
$this->validateInput('domain', $this->text('Domain or IP'), 'store'); |
|
256
|
|
|
$this->validateInput('basepath', $this->text('Path'), 'store', ''); |
|
257
|
|
|
$this->validateInput('status', $this->text('Status'), 'store', 0); |
|
258
|
|
|
|
|
259
|
|
|
$this->setSubmittedDataStore(); |
|
260
|
|
|
|
|
261
|
|
|
$this->validateComponent('store'); |
|
262
|
|
|
$this->addStore(); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Sets decoded JSON for "data" field |
|
267
|
|
|
*/ |
|
268
|
|
|
protected function setSubmittedDataStore() |
|
269
|
|
|
{ |
|
270
|
|
|
$json = $this->getSubmitted('data'); |
|
271
|
|
|
|
|
272
|
|
|
if (isset($json)) { |
|
273
|
|
|
|
|
274
|
|
|
$decoded = json_decode($json, true); |
|
275
|
|
|
|
|
276
|
|
|
if (!is_array($decoded)) { |
|
277
|
|
|
// json_decode() returns null on invalid JSON |
|
278
|
|
|
// So we pass false to trigger validation error |
|
279
|
|
|
$decoded = false; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
$this->setSubmitted('data', $decoded); |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
} |
|
286
|
|
|
|