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