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