Field::addField()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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