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