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