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

FieldValue::cmdDeleteFieldValue()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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