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\Review as ReviewModel; |
13
|
|
|
use gplcart\modules\cli\controllers\Command; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Handles commands related to product reviews |
17
|
|
|
*/ |
18
|
|
|
class Review extends Command |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Review model instance |
23
|
|
|
* @var \gplcart\core\models\Review $review |
24
|
|
|
*/ |
25
|
|
|
protected $review; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ReviewModel $review_id |
29
|
|
|
*/ |
30
|
|
|
public function __construct(ReviewModel $review_id) |
31
|
|
|
{ |
32
|
|
|
parent::__construct(); |
33
|
|
|
|
34
|
|
|
$this->review = $review_id; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Callback for "review-get" command |
39
|
|
|
*/ |
40
|
|
|
public function cmdGetReview() |
41
|
|
|
{ |
42
|
|
|
$result = $this->getListReview(); |
43
|
|
|
$this->outputFormat($result); |
44
|
|
|
$this->outputFormatTableReview($result); |
45
|
|
|
$this->output(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Callback for "review-delete" command |
50
|
|
|
*/ |
51
|
|
|
public function cmdDeleteReview() |
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('user')) { |
69
|
|
|
$options = array('user_id' => $id); |
70
|
|
|
} else if ($this->getParam('product')) { |
71
|
|
|
$options = array('product_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->review->getList($options) as $item) { |
82
|
|
|
$count++; |
83
|
|
|
$deleted += (int) $this->review->delete($item['review_id']); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$result = $count && $count == $deleted; |
87
|
|
|
|
88
|
|
|
} else if (!empty($id)) { |
89
|
|
|
$result = $this->review->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 "review-on" command |
101
|
|
|
*/ |
102
|
|
|
public function cmdOnReview() |
103
|
|
|
{ |
104
|
|
|
$this->setStatusReview(true); |
105
|
|
|
$this->output(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Callback for "review-off" command |
110
|
|
|
*/ |
111
|
|
|
public function cmdOffReview() |
112
|
|
|
{ |
113
|
|
|
$this->setStatusReview(false); |
114
|
|
|
$this->output(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Sets status for one or several reviews |
119
|
|
|
* @param bool $status |
120
|
|
|
*/ |
121
|
|
|
protected function setStatusReview($status) |
122
|
|
|
{ |
123
|
|
|
$id = $this->getParam(0); |
124
|
|
|
$all = $this->getParam('all'); |
125
|
|
|
|
126
|
|
|
if (!isset($id) && empty($all)) { |
127
|
|
|
$this->errorAndExit($this->text('Invalid command')); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if (isset($id) && (empty($id) || !is_numeric($id))) { |
131
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$options = null; |
135
|
|
|
|
136
|
|
|
if (isset($id)) { |
137
|
|
|
|
138
|
|
|
if ($this->getParam('user')) { |
139
|
|
|
$options = array('user_id' => $id); |
140
|
|
|
} else if ($this->getParam('product')) { |
141
|
|
|
$options = array('product_id' => $id); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
} else if (!empty($all)) { |
145
|
|
|
$options = array(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if (isset($options)) { |
149
|
|
|
|
150
|
|
|
$deleted = $count = 0; |
151
|
|
|
foreach ($this->review->getList($options) as $item) { |
152
|
|
|
$count++; |
153
|
|
|
$deleted += (int) $this->review->update($item['review_id'], array('status' => $status)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$result = $count && $count == $deleted; |
157
|
|
|
|
158
|
|
|
} else if (!empty($id)) { |
159
|
|
|
$result = $this->review->update($id, array('status' => $status)); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (empty($result)) { |
163
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Callback for "review-add" command |
169
|
|
|
*/ |
170
|
|
|
public function cmdAddReview() |
171
|
|
|
{ |
172
|
|
|
if ($this->getParam()) { |
173
|
|
|
$this->submitAddReview(); |
174
|
|
|
} else { |
175
|
|
|
$this->wizardAddReview(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$this->output(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Callback for "review-update" command |
183
|
|
|
*/ |
184
|
|
|
public function cmdUpdateReview() |
185
|
|
|
{ |
186
|
|
|
$params = $this->getParam(); |
187
|
|
|
|
188
|
|
|
if (empty($params[0]) || count($params) < 2) { |
189
|
|
|
$this->errorAndExit($this->text('Invalid command')); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
if (!is_numeric($params[0])) { |
193
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$this->setSubmitted(null, $params); |
197
|
|
|
$this->setSubmitted('update', $params[0]); |
198
|
|
|
$this->validateComponent('review'); |
199
|
|
|
|
200
|
|
|
$this->updateReview($params[0]); |
201
|
|
|
$this->output(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Returns an array of reviews |
206
|
|
|
* @return array |
207
|
|
|
*/ |
208
|
|
|
protected function getListReview() |
209
|
|
|
{ |
210
|
|
|
$id = $this->getParam(0); |
211
|
|
|
|
212
|
|
|
if (!isset($id)) { |
213
|
|
|
return $this->review->getList(array('limit' => $this->getLimit())); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
if (!is_numeric($id)) { |
217
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
if ($this->getParam('user')) { |
221
|
|
|
return $this->review->getList(array('user_id' => $id, 'limit' => $this->getLimit())); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
if ($this->getParam('product')) { |
225
|
|
|
return $this->review->getList(array('product_id' => $id, 'limit' => $this->getLimit())); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$result = $this->review->get($id); |
229
|
|
|
|
230
|
|
|
if (empty($result)) { |
231
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return array($result); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Output table format |
239
|
|
|
* @param array $items |
240
|
|
|
*/ |
241
|
|
|
protected function outputFormatTableReview(array $items) |
242
|
|
|
{ |
243
|
|
|
$header = array( |
244
|
|
|
$this->text('ID'), |
245
|
|
|
$this->text('User'), |
246
|
|
|
$this->text('Product'), |
247
|
|
|
$this->text('Text'), |
248
|
|
|
$this->text('Status'), |
249
|
|
|
$this->text('Created') |
250
|
|
|
); |
251
|
|
|
|
252
|
|
|
$rows = array(); |
253
|
|
|
|
254
|
|
|
foreach ($items as $item) { |
255
|
|
|
$rows[] = array( |
256
|
|
|
$item['review_id'], |
257
|
|
|
$item['user_id'], |
258
|
|
|
$item['product_id'], |
259
|
|
|
$this->truncate($item['text'], 50), |
260
|
|
|
$item['status'], |
261
|
|
|
$this->date($item['created']) |
262
|
|
|
); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
$this->outputFormatTable($rows, $header); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Add a new review |
270
|
|
|
*/ |
271
|
|
|
protected function addReview() |
272
|
|
|
{ |
273
|
|
|
if (!$this->isError()) { |
274
|
|
|
$id = $this->review->add($this->getSubmitted()); |
275
|
|
|
if (empty($id)) { |
276
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
277
|
|
|
} |
278
|
|
|
$this->line($id); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Updates a review |
284
|
|
|
* @param string $review_id |
285
|
|
|
*/ |
286
|
|
|
protected function updateReview($review_id) |
287
|
|
|
{ |
288
|
|
|
if (!$this->isError() && !$this->review->update($review_id, $this->getSubmitted())) { |
289
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Add a new review at once |
295
|
|
|
*/ |
296
|
|
|
protected function submitAddReview() |
297
|
|
|
{ |
298
|
|
|
$this->setSubmitted(null, $this->getParam()); |
299
|
|
|
$this->validateComponent('review'); |
300
|
|
|
$this->addReview(); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Add a new review step by step |
305
|
|
|
*/ |
306
|
|
|
protected function wizardAddReview() |
307
|
|
|
{ |
308
|
|
|
$this->validatePrompt('user_id', $this->text('User'), 'review'); |
309
|
|
|
$this->validatePrompt('product_id', $this->text('Product'), 'review'); |
310
|
|
|
$this->validatePrompt('text', $this->text('Text'), 'review'); |
311
|
|
|
$this->validatePrompt('status', $this->text('Status'), 'review', 0); |
312
|
|
|
|
313
|
|
|
$this->validateComponent('review'); |
314
|
|
|
$this->addReview(); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
} |
318
|
|
|
|