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