Completed
Push — master ( fa7820...1cf59b )
by Iurii
08:39
created

Field::outputFormatTableField()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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