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\Store as StoreModel; |
13
|
|
|
use gplcart\modules\cli\controllers\Command; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Handles commands related to stores |
17
|
|
|
*/ |
18
|
|
|
class Store extends Command |
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->errorAndExit($this->text('Invalid argument')); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (!$this->store->delete($id)) { |
78
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
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
|
|
|
$params = $this->getParam(); |
104
|
|
|
|
105
|
|
|
if (empty($params[0]) || count($params) < 2) { |
106
|
|
|
$this->errorAndExit($this->text('Invalid command')); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (!is_numeric($params[0])) { |
110
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->setSubmitted(null, $params); |
114
|
|
|
$this->setSubmitted('update', $params[0]); |
115
|
|
|
$this->setSubmittedJson('data'); |
116
|
|
|
|
117
|
|
|
$this->validateComponent('store'); |
118
|
|
|
|
119
|
|
|
$this->updateStore($params[0]); |
120
|
|
|
$this->output(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Sets status for one or several stores |
125
|
|
|
* @param bool $status |
126
|
|
|
*/ |
127
|
|
|
protected function setStatusStore($status) |
128
|
|
|
{ |
129
|
|
|
$id = $this->getParam(0); |
130
|
|
|
$all = $this->getParam('all'); |
131
|
|
|
|
132
|
|
|
if (!isset($id) && empty($all)) { |
133
|
|
|
$this->errorAndExit($this->text('Invalid command')); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$result = false; |
137
|
|
|
|
138
|
|
|
if (isset($id)) { |
139
|
|
|
|
140
|
|
|
if (!is_numeric($id)) { |
141
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$result = $this->store->update($id, array('status' => $status)); |
145
|
|
|
|
146
|
|
|
} else if (!empty($all)) { |
147
|
|
|
|
148
|
|
|
$updated = $count = 0; |
149
|
|
|
foreach ((array) $this->store->getList() as $store) { |
150
|
|
|
$count++; |
151
|
|
|
$updated += (int) $this->store->update($store['store_id'], array('status' => $status)); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$result = $count && $count == $updated; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (empty($result)) { |
158
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Returns an array of stores |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
|
|
protected function getListStore() |
167
|
|
|
{ |
168
|
|
|
$id = $this->getParam(0); |
169
|
|
|
|
170
|
|
|
if (!isset($id)) { |
171
|
|
|
return $this->store->getList(array('limit' => $this->getLimit())); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if (!is_numeric($id)) { |
175
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$result = $this->store->get($id); |
179
|
|
|
|
180
|
|
|
if (empty($result)) { |
181
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return array($result); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Output table format |
189
|
|
|
* @param array $items |
190
|
|
|
*/ |
191
|
|
|
protected function outputFormatTableStore(array $items) |
192
|
|
|
{ |
193
|
|
|
$header = array( |
194
|
|
|
$this->text('ID'), |
195
|
|
|
$this->text('Name'), |
196
|
|
|
$this->text('Domain'), |
197
|
|
|
$this->text('Path'), |
198
|
|
|
$this->text('Enabled') |
199
|
|
|
); |
200
|
|
|
|
201
|
|
|
$rows = array(); |
202
|
|
|
|
203
|
|
|
foreach ($items as $item) { |
204
|
|
|
$rows[] = array( |
205
|
|
|
$item['store_id'], |
206
|
|
|
$item['name'], |
207
|
|
|
$item['domain'], |
208
|
|
|
$item['basepath'], |
209
|
|
|
empty($item['status']) ? $this->text('No') : $this->text('Yes') |
210
|
|
|
); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
$this->outputFormatTable($rows, $header); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Updates a store |
218
|
|
|
* @param string $store_id |
219
|
|
|
*/ |
220
|
|
|
protected function updateStore($store_id) |
221
|
|
|
{ |
222
|
|
|
if (!$this->isError() && !$this->store->update($store_id, $this->getSubmitted())) { |
223
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Add a new store at once |
229
|
|
|
*/ |
230
|
|
|
protected function submitAddStore() |
231
|
|
|
{ |
232
|
|
|
$this->setSubmitted(null, $this->getParam()); |
233
|
|
|
$this->setSubmittedJson('data'); |
234
|
|
|
|
235
|
|
|
$this->validateComponent('store'); |
236
|
|
|
$this->addStore(); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Add a store |
241
|
|
|
*/ |
242
|
|
|
protected function addStore() |
243
|
|
|
{ |
244
|
|
|
if (!$this->isError()) { |
245
|
|
|
$id = $this->store->add($this->getSubmitted()); |
246
|
|
|
if (empty($id)) { |
247
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
248
|
|
|
} |
249
|
|
|
$this->line($id); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Add a new store step by step |
255
|
|
|
*/ |
256
|
|
|
protected function wizardAddStore() |
257
|
|
|
{ |
258
|
|
|
$this->validatePrompt('name', $this->text('Name'), 'store'); |
259
|
|
|
$this->validatePrompt('domain', $this->text('Domain or IP'), 'store'); |
260
|
|
|
$this->validatePrompt('basepath', $this->text('Path'), 'store', ''); |
261
|
|
|
$this->validatePrompt('status', $this->text('Status'), 'store', 0); |
262
|
|
|
$this->setSubmittedJson('data'); |
263
|
|
|
|
264
|
|
|
$this->validateComponent('store'); |
265
|
|
|
$this->addStore(); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|