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