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