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