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