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\Collection as CollectionModel; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Handles commands related to collections |
16
|
|
|
*/ |
17
|
|
|
class Collection extends Base |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Collection model instance |
22
|
|
|
* @var \gplcart\core\models\Collection $collection |
23
|
|
|
*/ |
24
|
|
|
protected $collection; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param CollectionModel $collection |
28
|
|
|
*/ |
29
|
|
|
public function __construct(CollectionModel $collection) |
30
|
|
|
{ |
31
|
|
|
parent::__construct(); |
32
|
|
|
|
33
|
|
|
$this->collection = $collection; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Callback for "collection-get" command |
38
|
|
|
*/ |
39
|
|
|
public function cmdGetCollection() |
40
|
|
|
{ |
41
|
|
|
$result = $this->getListCollection(); |
42
|
|
|
$this->outputFormat($result); |
43
|
|
|
$this->outputFormatTableCollection($result); |
44
|
|
|
$this->output(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Callback for "collection-delete" command |
49
|
|
|
*/ |
50
|
|
|
public function cmdDeleteCollection() |
51
|
|
|
{ |
52
|
|
|
$id = $this->getParam(0); |
53
|
|
|
$all = $this->getParam('all'); |
54
|
|
|
|
55
|
|
|
if (empty($id) && empty($all)) { |
56
|
|
|
$this->errorAndExit($this->text('Invalid command')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$options = $result = null; |
|
|
|
|
60
|
|
|
|
61
|
|
|
if (isset($id)) { |
62
|
|
|
|
63
|
|
|
if ($this->getParam('type')) { |
64
|
|
|
$options = array('type' => $id); |
65
|
|
|
} else if ($this->getParam('store')) { |
66
|
|
|
|
67
|
|
|
if (!is_numeric($id)) { |
68
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$options = array('store_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->collection->getList($options) as $item) { |
82
|
|
|
$count++; |
83
|
|
|
$deleted += (int) $this->collection->delete($item['collection_id']); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$result = $count && $count == $deleted; |
87
|
|
|
|
88
|
|
|
} else { |
89
|
|
|
|
90
|
|
|
if (!is_numeric($id)) { |
91
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$result = $this->collection->delete($id); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (empty($result)) { |
98
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->output(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Callback for "collection-add" command |
106
|
|
|
*/ |
107
|
|
|
public function cmdAddCollection() |
108
|
|
|
{ |
109
|
|
|
if ($this->getParam()) { |
110
|
|
|
$this->submitAddCollection(); |
111
|
|
|
} else { |
112
|
|
|
$this->wizardAddCollection(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->output(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Callback for "collection-update" command |
120
|
|
|
*/ |
121
|
|
|
public function cmdUpdateCollection() |
122
|
|
|
{ |
123
|
|
|
$params = $this->getParam(); |
124
|
|
|
|
125
|
|
|
if (empty($params[0]) || count($params) < 2) { |
126
|
|
|
$this->errorAndExit($this->text('Invalid command')); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (!is_numeric($params[0])) { |
130
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$this->setSubmitted(null, $params); |
134
|
|
|
$this->setSubmitted('update', $params[0]); |
135
|
|
|
$this->validateComponent('collection'); |
136
|
|
|
|
137
|
|
|
$this->updateCollection($params[0]); |
138
|
|
|
$this->output(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Returns an array of collections |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
protected function getListCollection() |
146
|
|
|
{ |
147
|
|
|
$id = $this->getParam(0); |
148
|
|
|
|
149
|
|
|
if (!isset($id)) { |
150
|
|
|
return $this->collection->getList(array('limit' => $this->getLimit())); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if ($this->getParam('type')) { |
154
|
|
|
return $this->collection->getList(array('type' => $id, 'limit' => $this->getLimit())); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (!is_numeric($id)) { |
158
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if ($this->getParam('store')) { |
162
|
|
|
return $this->collection->getList(array('store_id' => $id, 'limit' => $this->getLimit())); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$result = $this->collection->get($id); |
166
|
|
|
|
167
|
|
|
if (empty($result)) { |
168
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return array($result); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Output table format |
176
|
|
|
* @param array $items |
177
|
|
|
*/ |
178
|
|
|
protected function outputFormatTableCollection(array $items) |
179
|
|
|
{ |
180
|
|
|
$header = array( |
181
|
|
|
$this->text('ID'), |
182
|
|
|
$this->text('Name'), |
183
|
|
|
$this->text('Type'), |
184
|
|
|
$this->text('Store'), |
185
|
|
|
$this->text('Enabled') |
186
|
|
|
); |
187
|
|
|
|
188
|
|
|
$rows = array(); |
189
|
|
|
|
190
|
|
|
foreach ($items as $item) { |
191
|
|
|
$rows[] = array( |
192
|
|
|
$item['collection_id'], |
193
|
|
|
$item['title'], |
194
|
|
|
$item['type'], |
195
|
|
|
$item['store_id'], |
196
|
|
|
empty($item['status']) ? $this->text('No') : $this->text('Yes') |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$this->outputFormatTable($rows, $header); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Add a new collection |
205
|
|
|
*/ |
206
|
|
|
protected function addCollection() |
207
|
|
|
{ |
208
|
|
|
if (!$this->isError()) { |
209
|
|
|
$id = $this->collection->add($this->getSubmitted()); |
210
|
|
|
if (empty($id)) { |
211
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
212
|
|
|
} |
213
|
|
|
$this->line($id); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Updates a collection |
219
|
|
|
* @param string $collection_id |
220
|
|
|
*/ |
221
|
|
|
protected function updateCollection($collection_id) |
222
|
|
|
{ |
223
|
|
|
if (!$this->isError() && !$this->collection->update($collection_id, $this->getSubmitted())) { |
224
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Add a new collection at once |
230
|
|
|
*/ |
231
|
|
|
protected function submitAddCollection() |
232
|
|
|
{ |
233
|
|
|
$this->setSubmitted(null, $this->getParam()); |
234
|
|
|
$this->validateComponent('collection'); |
235
|
|
|
$this->addCollection(); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Add a new collection step by step |
240
|
|
|
*/ |
241
|
|
|
protected function wizardAddCollection() |
242
|
|
|
{ |
243
|
|
|
$this->validatePrompt('title', $this->text('Title'), 'collection'); |
244
|
|
|
$this->validateMenu('type', $this->text('Type'), 'collection', $this->collection->getTypes()); |
245
|
|
|
$this->validatePrompt('store_id', $this->text('Store'), 'collection'); |
246
|
|
|
$this->validatePrompt('description', $this->text('Description'), 'collection', ''); |
247
|
|
|
$this->validatePrompt('status', $this->text('Status'), 'collection', 0); |
248
|
|
|
|
249
|
|
|
$this->validateComponent('collection'); |
250
|
|
|
$this->addCollection(); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
} |
254
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.