Page::updatePage()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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