Test Failed
Push — master ( 25dee5...fc0a17 )
by Alexey
08:54 queued 03:59
created

Item::inFav()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 4
Ratio 44.44 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 0
dl 4
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Item
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Ecommerce;
13
14
class Item extends \Model {
15
16
    public static $categoryModel = 'Ecommerce\Category';
17
    public static $objectName = 'Товар';
18
    public static $labels = [
19
        'name' => 'Название',
20
        'alias' => 'Алиас',
21
        'item_badge_id' => 'Наклейка',
22
        'category_id' => 'Раздел',
23
        'description' => 'Описание',
24
        'item_type_id' => 'Тип товара',
25
        'image_file_id' => 'Изображение',
26
        'best' => 'Лучшее предложение',
27
        'options' => 'Параметры',
28
        'offers' => 'Торговые предложения',
29
        'widget' => 'Виджет для отображения в каталоге',
30
        'view' => 'Шаблон для отображения полной информации',
31
        'deleted' => 'Удален',
32
        'imgs' => 'Фото',
33
        'date_create' => 'Дата создания'
34
    ];
35
    public static $cols = [
36
        //Основные параметры
37
        'category_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'category'],
38
        'image_file_id' => ['type' => 'image'],
39
        'name' => ['type' => 'text'],
40
        'alias' => ['type' => 'text'],
41
        'description' => ['type' => 'html'],
42
        'item_type_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'type'],
43
        'best' => ['type' => 'bool'],
44
        'item_badge_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'badge'],
45
        'deleted' => ['type' => 'bool'],
46
        //Системные
47
        'user_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'user'],
48
        'weight' => ['type' => 'number'],
49
        'sales' => ['type' => 'number'],
50
        'imported' => ['type' => 'text'],
51
        'tree_path' => ['type' => 'text'],
52
        'search_index' => ['type' => 'text', 'logging' => false],
53
        'date_create' => ['type' => 'dateTime'],
54
        'widget' => ['type' => 'text'],
55
        'view' => ['type' => 'text'],
56
        //Менеджеры
57
        'options' => ['type' => 'dataManager', 'relation' => 'options'],
58
        'offers' => ['type' => 'dataManager', 'relation' => 'offers'],
59
        'imgs' => ['type' => 'dataManager', 'relation' => 'images'],
60
    ];
61
62
    public static function simpleItemHandler($request) {
63
        if ($request) {
64
            $item = new Item();
65
            $item->name = $request['name'];
66
            $item->description = $request['description'];
67
            $item->category_id = $request['category'];
68
            $item->save();
69
            if (!empty($_FILES['ActiveForm_simpleItem']['tmp_name']['Ecommerce\Item']['image'])) {
70
                $file_id = \App::$primary->files->upload([
71
                    'tmp_name' => $_FILES['ActiveForm_simpleItem']['tmp_name']['Ecommerce\Item']['image'],
72
                    'name' => $_FILES['ActiveForm_simpleItem']['name']['Ecommerce\Item']['image'],
73
                    'type' => $_FILES['ActiveForm_simpleItem']['type']['Ecommerce\Item']['image'],
74
                    'size' => $_FILES['ActiveForm_simpleItem']['size']['Ecommerce\Item']['image'],
75
                    'error' => $_FILES['ActiveForm_simpleItem']['error']['Ecommerce\Item']['image'],
76
                ], [
77
                    'upload_code' => 'activeForm:' . 'Ecommerce\Item' . ':' . $item->pk(),
78
                    'accept_group' => 'image'
79
                ]);
80
                if ($file_id) {
81
                    $item->image_file_id = $file_id;
82
                    $item->save();
83
                }
84
            }
85 View Code Duplication
            if (!empty($request['options']['option'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
                foreach ($request['options']['option'] as $key => $option_id) {
87
                    $param = new Item\Param();
88
                    $param->item_id = $item->id;
89
                    $param->value = $request['options']['value'][$key];
90
                    $param->item_option_id = $option_id;
91
                    $param->save();
92
                }
93
            }
94
            $offer = new Item\Offer();
95
            $offer->item_id = $item->id;
96
            $offer->save();
97 View Code Duplication
            if (!empty($request['offerOptions']['option'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
                foreach ($request['offerOptions']['option'] as $key => $option_id) {
99
                    $param = new Item\Offer\Param();
100
                    $param->item_offer_id = $offer->id;
101
                    $param->value = $request['offerOptions']['value'][$key];
102
                    $param->item_offer_option_id = $option_id;
103
                    $param->save();
104
                }
105
            }
106
            $price = new Item\Offer\Price();
107
            $price->price = $request['price'];
108
            $price->item_offer_id = $offer->id;
109
            $price->currency_id = $request['currency'];
110
            $price->save();
111
        }
112
    }
113
114
    public static $dataManagers = [
115
        'manager' => [
116
            'name' => 'Товары',
117
            'cols' => [
118
                'name',
119
                'imgs',
120
                'category_id',
121
                'item_type_id',
122
                'best',
123
                'deleted',
124
                'options',
125
                'offers',
126
                'date_create'
127
            ],
128
            'categorys' => [
129
                'model' => 'Ecommerce\Category',
130
            ],
131
            'sortable' => ['date_create'],
132
            'preSort' => [
133
                'date_create' => 'desc'
134
            ],
135
            'filters' => [
136
                'name', 'best', 'deleted', 'date_create'
137
            ],
138
            'sortMode' => true
139
        ]
140
    ];
141
    public static $forms = [
142
        'manager' => [
143
            'map' => [
144
                ['name', 'alias'],
145
                ['category_id', 'item_type_id', 'deleted'],
146
                ['widget', 'view'],
147
                ['best', 'item_badge_id', 'image_file_id'],
148
                ['description'],
149
                ['imgs'],
150
                ['options'],
151
                ['offers'],
152
            ]
153
        ],
154
        'simpleItem' => [
155
            'options' => [
156
                'access' => [
157
                    'groups' => [
158
                        3
159
                    ]
160
                ],
161
            ],
162
            'name' => 'Простой товар с ценой',
163
            'inputs' => [
164
                'name' => ['type' => 'text'],
165
                'description' => ['type' => 'html'],
166
                'category' => ['type' => 'select', 'source' => 'model', 'model' => 'Ecommerce\Category', 'label' => 'Категория'],
167
                'image' => ['type' => 'image', 'label' => 'Изображение'],
168
                'price' => ['type' => 'text', 'label' => 'Цена'],
169
                'currency' => ['type' => 'select', 'source' => 'model', 'model' => 'Money\Currency', 'label' => 'Валюта'],
170
                'options' => ['type' => 'dynamicList', 'source' => 'options', 'options' => [
171
                    'inputs' => [
172
                        'option' => ['type' => 'select', 'source' => 'model', 'model' => 'Ecommerce\Item\Option', 'label' => 'Свойство'],
173
                        'value' => ['type' => 'dynamicType', 'typeSource' => 'selfMethod', 'selfMethod' => 'realType', 'label' => 'Значение'],
174
                    ]
175
                ]
176
                ],
177
                'offerOptions' => ['type' => 'dynamicList', 'source' => 'options', 'options' => [
178
                    'inputs' => [
179
                        'option' => ['type' => 'select', 'source' => 'model', 'model' => 'Ecommerce\Item\Offer\Option', 'label' => 'Свойство предложения'],
180
                        'value' => ['type' => 'dynamicType', 'typeSource' => 'selfMethod', 'selfMethod' => 'realType', 'label' => 'Значение'],
181
                    ]
182
                ], 'label' => 'Параметры предложения'
183
                ]
184
            ],
185
            'map' => [
186
                ['name', 'category'],
187
                ['description'],
188
                ['image'],
189
                ['price', 'currency'],
190
                ['options'],
191
                ['offerOptions'],
192
            ],
193
            'handler' => 'simpleItemHandler'
194
        ]
195
    ];
196
197
    public function realType() {
198
        if ($this->option && $this->option->type) {
199
            $type = $this->option->type;
200
201
            if ($type == 'select') {
202
                return [
203
                    'type' => 'select',
204
                    'source' => 'relation',
205
                    'relation' => 'option:items',
206
                ];
207
            }
208
            return $type;
209
        }
210
        return 'text';
211
    }
212
213
    public static function indexes() {
214
        return [
215
            'ecommerce_item_item_category_id' => [
216
                'type' => 'INDEX',
217
                'cols' => [
218
                    'item_category_id'
219
                ]
220
            ],
221
            'inji_ecommerce_item_item_tree_path' => [
222
                'type' => 'INDEX',
223
                'cols' => [
224
                    'item_tree_path(255)'
225
                ]
226
            ],
227
            'ecommerce_item_item_search_index' => [
228
                'type' => 'INDEX',
229
                'cols' => [
230
                    'item_search_index(255)'
231
                ]
232
            ],
233
        ];
234
    }
235
236
    public function beforeSave() {
237
238
        if ($this->id) {
239
            $this->search_index = $this->name . ' ';
240
            if ($this->category) {
241
                $this->search_index .= $this->category->name . ' ';
242
            }
243 View Code Duplication
            if ($this->options) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
244
                foreach ($this->options as $option) {
245
                    if ($option->item_option_searchable && $option->value) {
246
                        if ($option->item_option_type != 'select') {
247
                            $this->search_index .= $option->value . ' ';
248
                        } elseif (!empty($option->option->items[$option->value])) {
249
                            $option->option->items[$option->value]->value . ' ';
250
                        }
251
                    }
252
                }
253
            }
254
            if ($this->offers) {
255
                foreach ($this->offers as $offer) {
256 View Code Duplication
                    if ($offer->options) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
257
                        foreach ($offer->options as $option) {
258
                            if ($option->item_offer_option_searchable && $option->value) {
259
                                if ($option->item_offer_option_type != 'select') {
260
                                    $this->search_index .= $option->value . ' ';
261
                                } elseif (!empty($option->option->items[$option->value])) {
262
                                    $option->option->items[$option->value]->value . ' ';
263
                                }
264
                            }
265
                        }
266
                    }
267
                }
268
            }
269
        }
270
    }
271
272
    public static function relations() {
273
274
        return [
275
            'badge' => [
276
                'model' => 'Ecommerce\Item\Badge',
277
                'col' => 'item_badge_id'
278
            ],
279
            'category' => [
280
                'model' => 'Ecommerce\Category',
281
                'col' => 'category_id'
282
            ],
283
            'options' => [
284
                'type' => 'many',
285
                'model' => 'Ecommerce\Item\Param',
286
                'col' => 'item_id',
287
                'resultKey' => 'item_option_id',
288
                'join' => [Item\Option::table(), Item\Option::index() . ' = ' . Item\Param::colPrefix() . Item\Option::index()]
289
            ],
290
            'offers' => [
291
                'type' => 'many',
292
                'model' => 'Ecommerce\Item\Offer',
293
                'col' => 'item_id',
294
            ],
295
            'type' => [
296
                'model' => 'Ecommerce\Item\Type',
297
                'col' => 'item_type_id',
298
            ],
299
            'image' => [
300
                'model' => 'Files\File',
301
                'col' => 'image_file_id'
302
            ],
303
            'images' => [
304
                'type' => 'many',
305
                'model' => 'Ecommerce\Item\Image',
306
                'col' => 'item_id'
307
            ],
308
            'user' => [
309
                'model' => 'Users\User',
310
                'col' => 'user_id'
311
            ]
312
        ];
313
    }
314
315
    public function getPrice() {
316
        $offers = $this->offers(['key' => false]);
317
        $curPrice = null;
318
319
        foreach ($offers[0]->prices as $price) {
320
            if (!$price->type) {
321
                $curPrice = $price;
322
            } elseif (
323
                (!$price->type->roles && !$curPrice) ||
324
                ($price->type->roles && !$curPrice && strpos($price->type->roles, "|" . \Users\User::$cur->role_id . "|") !== false)
325
            ) {
326
                $curPrice = $price;
327
            }
328
        }
329
        return $curPrice;
330
    }
331
332
    public function name() {
333
        if (!empty(\App::$primary->ecommerce->config['item_option_as_name'])) {
334
            $param = Item\Param::get([['item_id', $this->id], ['item_option_id', \App::$primary->ecommerce->config['item_option_as_name']]]);
335
            if ($param && $param->value) {
336
                return $param->value;
337
            }
338
        }
339
        return $this->name;
340
    }
341
342
    public function beforeDelete() {
343
        if ($this->id) {
344
            if ($this->options) {
345
                foreach ($this->options as $option) {
346
                    $option->delete();
347
                }
348
            }
349
            if ($this->offers) {
350
                foreach ($this->offers as $offer) {
351
                    $offer->delete();
352
                }
353
            }
354
            if ($this->image) {
355
                $this->image->delete();
356
            }
357
        }
358
    }
359
360
    public function getHref() {
361
        return "/ecommerce/view/{$this->pk()}";
362
    }
363
364
    public function inFav() {
365
        if (\Users\User::$cur->id) {
366
            $fav = \Ecommerce\Favorite::get([['user_id', \Users\User::$cur->id], ['item_id', $this->id]]);
367
            return (bool)$fav;
368 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
369
            $favs = !empty($_COOKIE['ecommerce_favitems']) ? json_decode($_COOKIE['ecommerce_favitems'], true) : [];
370
            return in_array($this->id, $favs);
371
        }
372
    }
373
}