Completed
Push — master ( 54924a...1b4550 )
by Iurii
02:15
created

Product::outputFormatTableProduct()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 34
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 26
nc 3
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\Currency as CurrencyModel;
13
use gplcart\core\models\Product as ProductModel;
14
15
/**
16
 * Handles commands related to products
17
 */
18
class Product extends Base
19
{
20
21
    /**
22
     * Product model instance
23
     * @var \gplcart\core\models\Product $product
24
     */
25
    protected $product;
26
27
    /**
28
     * Currency model instance
29
     * @var \gplcart\core\models\Currency $currency
30
     */
31
    protected $currency;
32
33
    /**
34
     * @param ProductModel $product
35
     * @param CurrencyModel $currency
36
     */
37
    public function __construct(ProductModel $product, CurrencyModel $currency)
38
    {
39
        parent::__construct();
40
41
        $this->product = $product;
42
        $this->currency = $currency;
43
    }
44
45
    /**
46
     * Callback for "product-get" command
47
     */
48
    public function cmdGetProduct()
49
    {
50
        $result = $this->getListProduct();
51
        $this->outputFormat($result);
52
        $this->outputFormatTableProduct($result);
53
        $this->output();
54
    }
55
56
    /**
57
     * Callback for "product-delete" command
58
     */
59
    public function cmdDeleteProduct()
60
    {
61
        $id = $this->getParam(0);
62
        $all = $this->getParam('all');
63
64
        if (!isset($id) && empty($all)) {
65
            $this->errorAndExit($this->text('Invalid command'));
66
        }
67
68
        if (isset($id) && !is_numeric($id)) {
69
            $this->errorAndExit($this->text('Invalid argument'));
70
        }
71
72
        $options = null;
73
74
        if (isset($id)) {
75
76
            if ($this->getParam('user')) {
77
                $options = array('user_id' => $id);
78
            } else if ($this->getParam('store')) {
79
                $options = array('store_id' => $id);
80
            } else if ($this->getParam('category')) {
81
                $options = array('category_id' => $id);
82
            } else if ($this->getParam('class')) {
83
                $options = array('product_class_id' => $id);
84
            } else if ($this->getParam('brand')) {
85
                $options = array('brand_category_id' => $id);
86
            }
87
88
        } else if ($all) {
89
            $options = array();
90
        }
91
92
        if (isset($options)) {
93
94
            $deleted = $count = 0;
95
            foreach ($this->product->getList($options) as $item) {
96
                $count++;
97
                $deleted += (int) $this->product->delete($item['product_id']);
98
            }
99
100
            $result = $count && $count == $deleted;
101
102
        } else if (!empty($id)) {
103
            $result = $this->product->delete($id);
104
        }
105
106
        if (empty($result)) {
107
            $this->errorAndExit($this->text('Unexpected result'));
108
        }
109
110
        $this->output();
111
    }
112
113
    /**
114
     * Callback for "product-add" command
115
     */
116
    public function cmdAddProduct()
117
    {
118
        if ($this->getParam()) {
119
            $this->submitAddProduct();
120
        } else {
121
            $this->wizardAddProduct();
122
        }
123
124
        $this->output();
125
    }
126
127
    /**
128
     * Callback for "product-update" command
129
     */
130
    public function cmdUpdateProduct()
131
    {
132
        $params = $this->getParam();
133
134
        if (empty($params[0]) || count($params) < 2) {
135
            $this->errorAndExit($this->text('Invalid command'));
136
        }
137
138
        if (!is_numeric($params[0])) {
139
            $this->errorAndExit($this->text('Invalid argument'));
140
        }
141
142
        $this->setSubmitted(null, $params);
143
        $this->setSubmitted('update', $params[0]);
144
        $this->setSubmitted('form', true); // Internal flag. Specifies that the product submitted manually
145
146
        $this->validateComponent('product');
147
        $this->updateProduct($params[0]);
148
        $this->output();
149
    }
150
151
    /**
152
     * Returns an array of products
153
     * @return array
154
     */
155
    protected function getListProduct()
156
    {
157
        $id = $this->getParam(0);
158
159
        if (!isset($id)) {
160
            return $this->product->getList(array('limit' => $this->getLimit()));
161
        }
162
163
        if (!is_numeric($id)) {
164
            $this->errorAndExit($this->text('Invalid argument'));
165
        }
166
167
        if ($this->getParam('user')) {
168
            return $this->product->getList(array('user_id' => $id, 'limit' => $this->getLimit()));
169
        }
170
171
        if ($this->getParam('category')) {
172
            return $this->product->getList(array('category_id' => $id, 'limit' => $this->getLimit()));
173
        }
174
175
        if ($this->getParam('brand')) {
176
            return $this->product->getList(array('brand_category_id' => $id, 'limit' => $this->getLimit()));
177
        }
178
179
        if ($this->getParam('class')) {
180
            return $this->product->getList(array('product_class_id' => $id, 'limit' => $this->getLimit()));
181
        }
182
183
        if ($this->getParam('store')) {
184
            return $this->product->getList(array('store_id' => $id, 'limit' => $this->getLimit()));
185
        }
186
187
        $result = $this->product->get($id);
188
189
        if (empty($result)) {
190
            $this->errorAndExit($this->text('Unexpected result'));
191
        }
192
193
        return array($result);
194
    }
195
196
    /**
197
     * Output table format
198
     * @param array $items
199
     */
200
    protected function outputFormatTableProduct(array $items)
201
    {
202
        $header = array(
203
            $this->text('ID'),
204
            $this->text('Title'),
205
            $this->text('Category'),
206
            $this->text('Brand'),
207
            $this->text('Product class'),
208
            $this->text('Price'),
209
            $this->text('Currency'),
210
            $this->text('Store'),
211
            $this->text('User'),
212
            $this->text('Enabled')
213
        );
214
215
        $rows = array();
216
217
        foreach ($items as $item) {
218
            $rows[] = array(
219
                $item['product_id'],
220
                $this->truncate($item['title'], 50),
221
                $item['category_id'],
222
                $item['brand_category_id'],
223
                $item['product_class_id'],
224
                $item['price'],
225
                $item['currency'],
226
                $item['store_id'],
227
                $item['user_id'],
228
                empty($item['status']) ? $this->text('No') : $this->text('Yes')
229
            );
230
        }
231
232
        $this->outputFormatTable($rows, $header);
233
    }
234
235
    /**
236
     * Add a new product
237
     */
238
    protected function addProduct()
239
    {
240
        if (!$this->isError()) {
241
            $id = $this->product->add($this->getSubmitted());
242
            if (empty($id)) {
243
                $this->errorAndExit($this->text('Unexpected result'));
244
            }
245
            $this->line($id);
246
        }
247
    }
248
249
    /**
250
     * Updates a product
251
     * @param string $product_id
252
     */
253
    protected function updateProduct($product_id)
254
    {
255
        if (!$this->isError() && !$this->product->update($product_id, $this->getSubmitted())) {
256
            $this->errorAndExit($this->text('Unexpected result'));
257
        }
258
    }
259
260
    /**
261
     * Add a new product at once
262
     */
263
    protected function submitAddProduct()
264
    {
265
        $this->setSubmitted(null, $this->getParam());
266
        $this->setSubmitted('form', true); // Internal flag. Specifies that the product submitted manually
267
        $this->validateComponent('product');
268
        $this->addProduct();
269
    }
270
271
    /**
272
     * Add a new product step by step
273
     */
274
    protected function wizardAddProduct()
275
    {
276
        // Required
277
        $this->validatePrompt('title', $this->text('Title'), 'product');
278
279
        // Optional
280
        $this->validatePrompt('description', $this->text('Description'), 'product');
281
        $this->validatePrompt('store_id', $this->text('Store ID'), 'product', 0);
282
        $this->validatePrompt('sku', $this->text('SKU'), 'product', '');
283
        $this->validatePrompt('stock', $this->text('Stock'), 'product', 0);
284
        $this->validatePrompt('price', $this->text('Price'), 'product', 0);
285
        $this->validatePrompt('currency', $this->text('Currency code'), 'product', $this->currency->getDefault());
286
        $this->validatePrompt('product_class_id', $this->text('Product class ID'), 'product', 0);
287
        $this->validatePrompt('user_id', $this->text('User ID'), 'product', 0);
288
        $this->validatePrompt('meta_title', $this->text('Meta title'), 'product', '');
289
        $this->validatePrompt('meta_description', $this->text('Meta description'), 'product', '');
290
        $this->validatePrompt('category_id', $this->text('Category ID'), 'product', 0);
291
        $this->validatePrompt('brand_category_id', $this->text('Brand category ID'), 'product', 0);
292
293
        $size_units = $this->product->getSizeUnits();
294
        $this->validateMenu('size_unit', $this->text('Size unit'), 'product', $size_units, key($size_units));
295
296
        $this->validatePrompt('length', $this->text('Length'), 'product', 0);
297
        $this->validatePrompt('width', $this->text('Width'), 'product', 0);
298
        $this->validatePrompt('height', $this->text('Height'), 'product', 0);
299
300
        $weight_units = $this->product->getWeightUnits();
301
        $this->validateMenu('weight_unit', $this->text('Weight unit'), 'product', $weight_units, key($weight_units));
302
        $this->validatePrompt('weight', $this->text('Weight'), 'product', 0);
303
304
        $this->validatePrompt('subtract', $this->text('Subtract'), 'page', 0);
305
        $this->validatePrompt('status', $this->text('Status'), 'page', 0);
306
307
        $this->setSubmitted('form', true); // Internal flag. Specifies that the product submitted manually
308
        $this->validateComponent('product');
309
        $this->addProduct();
310
    }
311
312
}
313