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\Page as PageModel; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Handles commands related to pages |
16
|
|
|
*/ |
17
|
|
|
class Page extends Base |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Page model instance |
22
|
|
|
* @var \gplcart\core\models\Page $page |
23
|
|
|
*/ |
24
|
|
|
protected $page; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param PageModel $page |
28
|
|
|
*/ |
29
|
|
|
public function __construct(PageModel $page) |
30
|
|
|
{ |
31
|
|
|
parent::__construct(); |
32
|
|
|
|
33
|
|
|
$this->page = $page; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Callback for "page-get" command |
38
|
|
|
*/ |
39
|
|
|
public function cmdGetPage() |
40
|
|
|
{ |
41
|
|
|
$result = $this->getListPage(); |
42
|
|
|
$this->outputFormat($result); |
43
|
|
|
$this->outputFormatTablePage($result); |
44
|
|
|
$this->output(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Callback for "page-delete" command |
49
|
|
|
*/ |
50
|
|
|
public function cmdDeletePage() |
51
|
|
|
{ |
52
|
|
|
$id = $this->getParam(0); |
53
|
|
|
$all = $this->getParam('all'); |
54
|
|
|
$blog = $this->getParam('blog'); |
55
|
|
|
|
56
|
|
|
if (empty($id) && empty($all) && empty($blog)) { |
57
|
|
|
$this->errorAndExit($this->text('Invalid command')); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (isset($id) && (empty($id) || !is_numeric($id))) { |
61
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$options = null; |
65
|
|
|
|
66
|
|
|
if (isset($id)) { |
67
|
|
|
|
68
|
|
|
if ($this->getParam('store')) { |
69
|
|
|
$options = array('store_id' => $id); |
70
|
|
|
} else if ($this->getParam('category')) { |
71
|
|
|
$options = array('category_id' => $id); |
72
|
|
|
} else if ($this->getParam('user')) { |
73
|
|
|
$options = array('user_id' => $id); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
} else if ($blog) { |
77
|
|
|
$options = array('blog_post' => true); |
78
|
|
|
} else if ($all) { |
79
|
|
|
$options = array(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (isset($options)) { |
83
|
|
|
|
84
|
|
|
$deleted = $count = 0; |
85
|
|
|
foreach ($this->page->getList($options) as $item) { |
86
|
|
|
$count++; |
87
|
|
|
$deleted += (int) $this->page->delete($item['page_id']); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$result = $count && $count == $deleted; |
91
|
|
|
|
92
|
|
|
} else { |
93
|
|
|
$result = $this->page->delete($id); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (empty($result)) { |
97
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->output(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Callback for "page-add" command |
105
|
|
|
*/ |
106
|
|
|
public function cmdAddPage() |
107
|
|
|
{ |
108
|
|
|
if ($this->getParam()) { |
109
|
|
|
$this->submitAddPage(); |
110
|
|
|
} else { |
111
|
|
|
$this->wizardAddPage(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->output(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Callback for "page-update" command |
119
|
|
|
*/ |
120
|
|
|
public function cmdUpdatePage() |
121
|
|
|
{ |
122
|
|
|
$params = $this->getParam(); |
123
|
|
|
|
124
|
|
|
if (empty($params[0]) || count($params) < 2) { |
125
|
|
|
$this->errorAndExit($this->text('Invalid command')); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (!is_numeric($params[0])) { |
129
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$this->setSubmitted(null, $params); |
133
|
|
|
$this->setSubmitted('update', $params[0]); |
134
|
|
|
$this->validateComponent('page'); |
135
|
|
|
|
136
|
|
|
$this->updatePage($params[0]); |
137
|
|
|
$this->output(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Returns an array of pages |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
protected function getListPage() |
145
|
|
|
{ |
146
|
|
|
$id = $this->getParam(0); |
147
|
|
|
|
148
|
|
|
if (!isset($id)) { |
149
|
|
|
return $this->page->getList(array('limit' => $this->getLimit())); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if ($this->getParam('user')) { |
153
|
|
|
return $this->page->getList(array('user_id' => $id, 'limit' => $this->getLimit())); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if ($this->getParam('category')) { |
157
|
|
|
return $this->page->getList(array('category_id' => $id, 'limit' => $this->getLimit())); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if ($this->getParam('store')) { |
161
|
|
|
return $this->page->getList(array('store_id' => $id, 'limit' => $this->getLimit())); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if (!is_numeric($id)) { |
165
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$result = $this->page->get($id); |
169
|
|
|
|
170
|
|
|
if (empty($result)) { |
171
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return array($result); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Output table format |
179
|
|
|
* @param array $items |
180
|
|
|
*/ |
181
|
|
|
protected function outputFormatTablePage(array $items) |
182
|
|
|
{ |
183
|
|
|
$header = array( |
184
|
|
|
$this->text('ID'), |
185
|
|
|
$this->text('Title'), |
186
|
|
|
$this->text('Category'), |
187
|
|
|
$this->text('Store'), |
188
|
|
|
$this->text('User'), |
189
|
|
|
$this->text('Enabled') |
190
|
|
|
); |
191
|
|
|
|
192
|
|
|
$rows = array(); |
193
|
|
|
|
194
|
|
|
foreach ($items as $item) { |
195
|
|
|
$rows[] = array( |
196
|
|
|
$item['page_id'], |
197
|
|
|
$item['title'], |
198
|
|
|
$item['category_id'], |
199
|
|
|
$item['store_id'], |
200
|
|
|
$item['user_id'], |
201
|
|
|
empty($item['status']) ? $this->text('No') : $this->text('Yes') |
202
|
|
|
); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$this->outputFormatTable($rows, $header); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Add a new page |
210
|
|
|
*/ |
211
|
|
|
protected function addPage() |
212
|
|
|
{ |
213
|
|
|
if (!$this->isError()) { |
214
|
|
|
$id = $this->page->add($this->getSubmitted()); |
215
|
|
|
if (empty($id)) { |
216
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
217
|
|
|
} |
218
|
|
|
$this->line($id); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Updates a page |
224
|
|
|
* @param string $page_id |
225
|
|
|
*/ |
226
|
|
|
protected function updatePage($page_id) |
227
|
|
|
{ |
228
|
|
|
if (!$this->isError() && !$this->page->update($page_id, $this->getSubmitted())) { |
229
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Add a new page at once |
235
|
|
|
*/ |
236
|
|
|
protected function submitAddPage() |
237
|
|
|
{ |
238
|
|
|
$this->setSubmitted(null, $this->getParam()); |
239
|
|
|
$this->validateComponent('page'); |
240
|
|
|
$this->addPage(); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Add a new page step by step |
245
|
|
|
*/ |
246
|
|
|
protected function wizardAddPage() |
247
|
|
|
{ |
248
|
|
|
// Required |
249
|
|
|
$this->validatePrompt('title', $this->text('Title'), 'page'); |
250
|
|
|
$this->validatePrompt('description', $this->text('Description'), 'page'); |
251
|
|
|
$this->validatePrompt('store_id', $this->text('Store ID'), 'page'); |
252
|
|
|
|
253
|
|
|
// Optional |
254
|
|
|
$this->validatePrompt('user_id', $this->text('User ID'), 'page', 0); |
255
|
|
|
$this->validatePrompt('meta_title', $this->text('Meta title'), 'page', ''); |
256
|
|
|
$this->validatePrompt('meta_description', $this->text('Meta description'), 'page', ''); |
257
|
|
|
$this->validatePrompt('category_id', $this->text('Category ID'), 'page', 0); |
258
|
|
|
$this->validatePrompt('blog_post', $this->text('Blog post'), 'page', 0); |
259
|
|
|
$this->validatePrompt('status', $this->text('Status'), 'page', 0); |
260
|
|
|
|
261
|
|
|
$this->validateComponent('page'); |
262
|
|
|
$this->addPage(); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
} |
266
|
|
|
|