|
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\Address as AddressModel; |
|
13
|
|
|
use gplcart\modules\cli\controllers\Base; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Handles commands related to user addresses |
|
17
|
|
|
*/ |
|
18
|
|
|
class Address extends Base |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Address model instance |
|
23
|
|
|
* @var \gplcart\core\models\Address $address |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $address; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param AddressModel $role |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(AddressModel $role) |
|
31
|
|
|
{ |
|
32
|
|
|
parent::__construct(); |
|
33
|
|
|
|
|
34
|
|
|
$this->address = $role; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Callback for "address-get" command |
|
39
|
|
|
*/ |
|
40
|
|
|
public function cmdGetAddress() |
|
41
|
|
|
{ |
|
42
|
|
|
$result = $this->getListAddress(); |
|
43
|
|
|
$this->outputFormat($result); |
|
44
|
|
|
$this->outputFormatTableAddress($result); |
|
45
|
|
|
$this->output(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Callback for "address-delete" command |
|
50
|
|
|
*/ |
|
51
|
|
|
public function cmdDeleteAddress() |
|
52
|
|
|
{ |
|
53
|
|
|
$id = $this->getParam(0); |
|
54
|
|
|
|
|
55
|
|
|
if (empty($id)) { |
|
56
|
|
|
$this->errorExit($this->text('Invalid ID')); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if ($this->getParam('user')) { |
|
60
|
|
|
|
|
61
|
|
|
$deleted = $count = 0; |
|
62
|
|
|
foreach ($this->address->getList(array('user_id' => $id)) as $item) { |
|
63
|
|
|
$count++; |
|
64
|
|
|
$deleted += (int) $this->address->delete($item['address_id']); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$result = ($count == $deleted); |
|
68
|
|
|
|
|
69
|
|
|
} else { |
|
70
|
|
|
$result = $this->address->delete($id); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (!$result) { |
|
74
|
|
|
$this->errorExit($this->text('An error occurred')); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$this->output(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Callback for "address-add" command |
|
82
|
|
|
*/ |
|
83
|
|
|
public function cmdAddAddress() |
|
84
|
|
|
{ |
|
85
|
|
|
if ($this->getParam()) { |
|
86
|
|
|
$this->submitAddAddress(); |
|
87
|
|
|
} else { |
|
88
|
|
|
$this->wizardAddAddress(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$this->output(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Callback for "address-update" command |
|
96
|
|
|
*/ |
|
97
|
|
|
public function cmdUpdateAddress() |
|
98
|
|
|
{ |
|
99
|
|
|
$this->submitUpdateAddress(); |
|
100
|
|
|
$this->output(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Returns an array of addresses |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function getListAddress() |
|
108
|
|
|
{ |
|
109
|
|
|
$id = $this->getParam(0); |
|
110
|
|
|
|
|
111
|
|
|
if (isset($id)) { |
|
112
|
|
|
|
|
113
|
|
|
if (!is_numeric($id)) { |
|
114
|
|
|
$this->errorExit($this->text('Invalid ID')); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if ($this->getParam('user')) { |
|
118
|
|
|
$list = $this->address->getList(array('user_id' => $id)); |
|
119
|
|
|
$this->limitArray($list); |
|
120
|
|
|
return $list; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$result = $this->address->get($id); |
|
124
|
|
|
|
|
125
|
|
|
if (empty($result)) { |
|
126
|
|
|
$this->errorExit($this->text('Invalid ID')); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return array($result); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$list = $this->address->getList(); |
|
133
|
|
|
$this->limitArray($list); |
|
134
|
|
|
return $list; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Output table format |
|
139
|
|
|
* @param array $items |
|
140
|
|
|
*/ |
|
141
|
|
|
protected function outputFormatTableAddress(array $items) |
|
142
|
|
|
{ |
|
143
|
|
|
$header = array( |
|
144
|
|
|
$this->text('ID'), |
|
145
|
|
|
$this->text('User ID'), |
|
146
|
|
|
$this->text('Full name'), |
|
147
|
|
|
$this->text('Address'), |
|
148
|
|
|
$this->text('City ID'), |
|
149
|
|
|
$this->text('Phone') |
|
150
|
|
|
); |
|
151
|
|
|
|
|
152
|
|
|
$rows = array(); |
|
153
|
|
|
foreach ($items as $item) { |
|
154
|
|
|
$rows[] = array( |
|
155
|
|
|
$item['address_id'], |
|
156
|
|
|
$item['user_id'], |
|
157
|
|
|
$item['full_name'], |
|
158
|
|
|
$item['address_id'], |
|
159
|
|
|
$item['city_id'], |
|
160
|
|
|
$item['phone'], |
|
161
|
|
|
); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
$this->outputFormatTable($rows, $header); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Updates an address |
|
169
|
|
|
*/ |
|
170
|
|
|
protected function submitUpdateAddress() |
|
171
|
|
|
{ |
|
172
|
|
|
$params = $this->getParam(); |
|
173
|
|
|
|
|
174
|
|
|
if (empty($params[0]) || count($params) < 2) { |
|
175
|
|
|
$this->errorExit($this->text('Invalid command')); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
$this->setSubmitted(null, $this->getParam()); |
|
179
|
|
|
$this->setSubmitted('update', $params[0]); |
|
180
|
|
|
$this->setSubmittedJson('data'); |
|
181
|
|
|
|
|
182
|
|
|
$this->validateComponent('address'); |
|
183
|
|
|
$this->updateAddress($params[0]); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Updates an address |
|
188
|
|
|
* @param string $address_id |
|
189
|
|
|
*/ |
|
190
|
|
|
protected function updateAddress($address_id) |
|
191
|
|
|
{ |
|
192
|
|
|
if (!$this->isError() && !$this->address->update($address_id, $this->getSubmitted())) { |
|
193
|
|
|
$this->errorExit($this->text('Address has not been updated')); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Add a new address at once |
|
199
|
|
|
*/ |
|
200
|
|
|
protected function submitAddAddress() |
|
201
|
|
|
{ |
|
202
|
|
|
$this->setSubmitted(null, $this->getParam()); |
|
203
|
|
|
$this->setSubmittedJson('data'); |
|
204
|
|
|
$this->validateComponent('address'); |
|
205
|
|
|
$this->addAddress(); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Add a new address step by step |
|
210
|
|
|
*/ |
|
211
|
|
|
protected function wizardAddAddress() |
|
212
|
|
|
{ |
|
213
|
|
|
$types = $this->address->getTypes(); |
|
214
|
|
|
$this->validateMenu('type', $this->text('Address type'), 'address', array_combine($types, $types), 'shipping'); |
|
215
|
|
|
|
|
216
|
|
|
$this->validatePrompt('user_id', $this->text('User ID'), 'address'); |
|
217
|
|
|
$this->validatePrompt('country', $this->text('Country code'), 'address'); |
|
218
|
|
|
$this->validatePrompt('state_id', $this->text('State ID'), 'address'); |
|
219
|
|
|
$this->validatePrompt('city_id', $this->text('City ID'), 'address'); |
|
220
|
|
|
$this->validatePrompt('address_1', $this->text('Address'), 'address'); |
|
221
|
|
|
$this->validatePrompt('address_2', $this->text('Additional address'), 'address'); |
|
222
|
|
|
$this->validatePrompt('phone', $this->text('Phone'), 'address'); |
|
223
|
|
|
$this->validatePrompt('postcode', $this->text('Post code/ZIP'), 'address'); |
|
224
|
|
|
$this->validatePrompt('company', $this->text('Company'), 'address'); |
|
225
|
|
|
$this->validatePrompt('fax', $this->text('Fax'), 'address'); |
|
226
|
|
|
$this->validatePrompt('first_name', $this->text('First name'), 'address'); |
|
227
|
|
|
$this->validatePrompt('middle_name', $this->text('Middle name'), 'address'); |
|
228
|
|
|
$this->validatePrompt('last_name', $this->text('Last name'), 'address'); |
|
229
|
|
|
$this->validatePrompt('data', $this->text('Data'), 'address'); |
|
230
|
|
|
|
|
231
|
|
|
$this->setSubmittedJson('data'); |
|
232
|
|
|
$this->validateComponent('address'); |
|
233
|
|
|
$this->addAddress(); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Add a new address |
|
238
|
|
|
*/ |
|
239
|
|
|
protected function addAddress() |
|
240
|
|
|
{ |
|
241
|
|
|
if (!$this->isError()) { |
|
242
|
|
|
$id = $this->address->add($this->getSubmitted()); |
|
243
|
|
|
if (empty($id)) { |
|
244
|
|
|
$this->errorExit($this->text('Address has not been added')); |
|
245
|
|
|
} |
|
246
|
|
|
$this->line($id); |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
} |
|
251
|
|
|
|