Completed
Push — master ( 496f6c...fa7820 )
by Iurii
01:48
created

Category::wizardAddCategory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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