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\Cart as CartModel; |
13
|
|
|
use gplcart\modules\cli\controllers\Command; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Handles commands related to cart functionality |
17
|
|
|
*/ |
18
|
|
|
class Cart extends Command |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Cart model instance |
23
|
|
|
* @var \gplcart\core\models\Cart $cart |
24
|
|
|
*/ |
25
|
|
|
protected $cart; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param CartModel $cart |
29
|
|
|
*/ |
30
|
|
|
public function __construct(CartModel $cart) |
31
|
|
|
{ |
32
|
|
|
parent::__construct(); |
33
|
|
|
|
34
|
|
|
$this->cart = $cart; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Callback for "cart-get" command |
39
|
|
|
*/ |
40
|
|
|
public function cmdGetCart() |
41
|
|
|
{ |
42
|
|
|
$result = $this->getListCart(); |
43
|
|
|
$this->outputFormat($result); |
44
|
|
|
$this->outputFormatTableCart($result); |
45
|
|
|
$this->output(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Callback for "cart-delete" command |
50
|
|
|
*/ |
51
|
|
|
public function cmdDeleteCart() |
52
|
|
|
{ |
53
|
|
|
$id = $this->getParam(0); |
54
|
|
|
|
55
|
|
|
$options = null; |
56
|
|
|
|
57
|
|
|
if ($this->getParam('user')) { |
58
|
|
|
$options = array('user_id' => $id); |
59
|
|
|
} else if ($this->getParam('sku')) { |
60
|
|
|
$options = array('sku' => $id); |
61
|
|
|
} else if ($this->getParam('order')) { |
62
|
|
|
|
63
|
|
|
if (!is_numeric($id)) { |
64
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$options = array('order_id' => $id); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (isset($options)) { |
71
|
|
|
|
72
|
|
|
$deleted = $count = 0; |
73
|
|
|
foreach ($this->cart->getList($options) as $item) { |
74
|
|
|
$count++; |
75
|
|
|
$deleted += (int) $this->cart->delete($item['cart_id']); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$result = $count && $count == $deleted; |
79
|
|
|
|
80
|
|
|
} else { |
81
|
|
|
|
82
|
|
|
if (empty($id) || !is_numeric($id)) { |
83
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$result = $this->cart->delete($id); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (empty($result)) { |
90
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->output(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Callback for "cart-add" command |
98
|
|
|
*/ |
99
|
|
|
public function cmdAddCart() |
100
|
|
|
{ |
101
|
|
|
if ($this->getParam()) { |
102
|
|
|
$this->submitAddCart(); |
103
|
|
|
} else { |
104
|
|
|
$this->wizardAddCart(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->output(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns an array of cart items |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
protected function getListCart() |
115
|
|
|
{ |
116
|
|
|
$id = $this->getParam(0); |
117
|
|
|
|
118
|
|
|
if (!isset($id)) { |
119
|
|
|
return $this->cart->getList(array('limit' => $this->getLimit())); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ($this->getParam('user')) { |
123
|
|
|
return $this->cart->getList(array('user_id' => $id, 'limit' => $this->getLimit())); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if ($this->getParam('sku')) { |
127
|
|
|
return $this->cart->getList(array('sku' => $id, 'limit' => $this->getLimit())); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if (!is_numeric($id)) { |
131
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ($this->getParam('order')) { |
135
|
|
|
return $this->cart->getList(array('order_id' => $id, 'limit' => $this->getLimit())); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if ($this->getParam('store')) { |
139
|
|
|
return $this->cart->getList(array('store_id' => $id, 'limit' => $this->getLimit())); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$result = $this->cart->get($id); |
143
|
|
|
|
144
|
|
|
if (empty($result)) { |
145
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return array($result); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Output table format |
153
|
|
|
* @param array $items |
154
|
|
|
*/ |
155
|
|
|
protected function outputFormatTableCart(array $items) |
156
|
|
|
{ |
157
|
|
|
$header = array( |
158
|
|
|
$this->text('ID'), |
159
|
|
|
$this->text('User ID'), |
160
|
|
|
$this->text('Store ID'), |
161
|
|
|
$this->text('SKU'), |
162
|
|
|
$this->text('Order ID'), |
163
|
|
|
$this->text('Quantity'), |
164
|
|
|
$this->text('Created') |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
$rows = array(); |
168
|
|
|
|
169
|
|
|
foreach ($items as $item) { |
170
|
|
|
$rows[] = array( |
171
|
|
|
$item['cart_id'], |
172
|
|
|
$item['user_id'], |
173
|
|
|
$item['store_id'], |
174
|
|
|
$item['sku'], |
175
|
|
|
$item['order_id'], |
176
|
|
|
$item['quantity'], |
177
|
|
|
$this->date($item['created']) |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$this->outputFormatTable($rows, $header); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Add a new cart item at once |
186
|
|
|
*/ |
187
|
|
|
protected function submitAddCart() |
188
|
|
|
{ |
189
|
|
|
$this->setSubmitted(null, $this->getParam()); |
190
|
|
|
$this->setSubmittedJson('data'); |
191
|
|
|
$this->validateComponent('cart'); |
192
|
|
|
$this->addAddCart(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Add a new cart item step by step |
197
|
|
|
*/ |
198
|
|
|
protected function wizardAddCart() |
199
|
|
|
{ |
200
|
|
|
// Required |
201
|
|
|
$this->validatePrompt('user_id', $this->text('User ID'), 'cart'); |
202
|
|
|
$this->validatePrompt('product_id', $this->text('Product ID'), 'cart'); |
203
|
|
|
$this->validatePrompt('sku', $this->text('Product SKU'), 'cart'); |
204
|
|
|
$this->validatePrompt('store_id', $this->text('Store ID'), 'cart'); |
205
|
|
|
|
206
|
|
|
// Optional |
207
|
|
|
$this->validatePrompt('quantity', $this->text('Quantity'), 'cart', 1); |
208
|
|
|
$this->validatePrompt('order_id', $this->text('Order ID'), 'cart', 0); |
209
|
|
|
$this->validatePrompt('data', $this->text('Data'), 'cart'); |
210
|
|
|
|
211
|
|
|
$this->setSubmittedJson('data'); |
212
|
|
|
$this->validateComponent('cart'); |
213
|
|
|
$this->addAddCart(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Add a new cart item |
218
|
|
|
*/ |
219
|
|
|
protected function addAddCart() |
220
|
|
|
{ |
221
|
|
|
if (!$this->isError()) { |
222
|
|
|
$id = $this->cart->add($this->getSubmitted()); |
223
|
|
|
if (empty($id)) { |
224
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
225
|
|
|
} |
226
|
|
|
$this->line($id); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
} |
231
|
|
|
|