Completed
Push — master ( b93a9c...e7c2e1 )
by Alexey
05:14
created

Item::name()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 0
dl 0
loc 10
rs 9.2
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
        'category_id' => 'Раздел',
22
        'description' => 'Описание',
23
        'item_type_id' => 'Тип товара',
24
        'image_file_id' => 'Изображение',
25
        'best' => 'Лучшее предложение',
26
        'options' => 'Параметры',
27
        'offers' => 'Торговые предложения',
28
        'widget' => 'Виджет для отображения в каталоге',
29
        'view' => 'Шаблон для отображения полной информации',
30
        'deleted' => 'Удален',
31
        'imgs' => 'Фото'
32
    ];
33
    public static $cols = [
34
        //Основные параметры
35
        'category_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'category'],
36
        'image_file_id' => ['type' => 'image'],
37
        'name' => ['type' => 'text'],
38
        'alias' => ['type' => 'text'],
39
        'description' => ['type' => 'html'],
40
        'item_type_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'type'],
41
        'best' => ['type' => 'bool'],
42
        'deleted' => ['type' => 'bool'],
43
        //Системные
44
        'user_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'user'],
45
        'weight' => ['type' => 'number'],
46
        'sales' => ['type' => 'number'],
47
        'imported' => ['type' => 'text'],
48
        'tree_path' => ['type' => 'text'],
49
        'search_index' => ['type' => 'text'],
50
        'date_create' => ['type' => 'dateTime'],
51
        'widget' => ['type' => 'text'],
52
        'view' => ['type' => 'text'],
53
        //Менеджеры
54
        'options' => ['type' => 'dataManager', 'relation' => 'options'],
55
        'offers' => ['type' => 'dataManager', 'relation' => 'offers'],
56
        'imgs' => ['type' => 'dataManager', 'relation' => 'images'],
57
    ];
58
    public static $dataManagers = [
59
        'manager' => [
60
            'name' => 'Товары',
61
            'cols' => [
62
                'name',
63
                'imgs',
64
                'category_id',
65
                'item_type_id',
66
                'best',
67
                'deleted',
68
                'options',
69
                'offers',
70
            ],
71
            'categorys' => [
72
                'model' => 'Ecommerce\Category',
73
            ],
74
            'sortMode' => true
75
        ]
76
    ];
77
    public static $forms = [
78
        'manager' => [
79
            'map' => [
80
                ['name', 'alias'],
81
                ['category_id', 'item_type_id', 'deleted'],
82
                ['widget', 'view'],
83
                ['best', 'image_file_id'],
84
                ['description'],
85
                ['imgs'],
86
                ['options'],
87
                ['offers'],
88
            ]
89
    ]];
90
91
    public static function indexes()
92
    {
93
        return [
94
            'ecommerce_item_item_category_id' => [
95
                'type' => 'INDEX',
96
                'cols' => [
97
                    'item_category_id'
98
                ]
99
            ],
100
            'inji_ecommerce_item_item_tree_path' => [
101
                'type' => 'INDEX',
102
                'cols' => [
103
                    'item_tree_path(255)'
104
                ]
105
            ],
106
            'ecommerce_item_item_search_index' => [
107
                'type' => 'INDEX',
108
                'cols' => [
109
                    'item_search_index(255)'
110
                ]
111
            ],
112
        ];
113
    }
114
115
    public function beforeSave()
116
    {
117
118
        if ($this->id) {
119
            $this->search_index = $this->name . ' ';
120
            if ($this->category) {
121
                $this->search_index .= $this->category->name . ' ';
122
            }
123
            if ($this->options) {
124
                foreach ($this->options as $option) {
125
                    if ($option->item_option_searchable && $option->value) {
126
                        if ($option->item_option_type != 'select') {
127
                            $this->search_index .= $option->value . ' ';
128
                        } elseif (!empty($option->option->items[$option->value])) {
129
                            $option->option->items[$option->value]->value . ' ';
130
                        }
131
                    }
132
                }
133
            }
134
        }
135
    }
136
137
    public static function relations()
138
    {
139
140
        return [
141
            'category' => [
142
                'model' => 'Ecommerce\Category',
143
                'col' => 'category_id'
144
            ],
145
            'options' => [
146
                'type' => 'many',
147
                'model' => 'Ecommerce\Item\Param',
148
                'col' => 'item_id',
149
                //'resultKey' => 'code',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
150
                'resultKey' => 'item_option_id',
151
                'join' => [Item\Option::table(), Item\Option::index() . ' = ' . Item\Param::colPrefix() . Item\Option::index()]
152
            ],
153
            'offers' => [
154
                'type' => 'many',
155
                'model' => 'Ecommerce\Item\Offer',
156
                'col' => 'item_id',
157
            ],
158
            'type' => [
159
                'model' => 'Ecommerce\Item\Type',
160
                'col' => 'item_type_id',
161
            ],
162
            'image' => [
163
                'model' => 'Files\File',
164
                'col' => 'image_file_id'
165
            ],
166
            'images' => [
167
                'type' => 'many',
168
                'model' => 'Ecommerce\Item\Image',
169
                'col' => 'item_id'
170
            ],
171
            'user' => [
172
                'model' => 'Users\User',
173
                'col' => 'user_id'
174
            ]
175
        ];
176
    }
177
178
    public function getPrice()
179
    {
180
        $offers = $this->offers(['key' => false]);
181
        $curPrice = null;
182
183
        foreach ($offers[0]->prices as $price) {
184
            if (!$price->type) {
185
                $curPrice = $price;
186
            } elseif (
187
                    (!$price->type->roles && !$curPrice) ||
188
                    ($price->type->roles && !$curPrice && strpos($price->type->roles, "|" . \Users\User::$cur->role_id . "|") !== false)
189
            ) {
190
                $curPrice = $price;
191
            }
192
        }
193
        return $curPrice;
194
    }
195
196
    public function name()
197
    {
198
        if (!empty(\App::$primary->ecommerce->config['item_option_as_name'])) {
199
            $param = Item\Param::get([['item_id', $this->id], ['item_option_id', \App::$primary->ecommerce->config['item_option_as_name']]]);
200
            if ($param && $param->value) {
201
                return $param->value;
202
            }
203
        }
204
        return $this->name;
205
    }
206
207
    public function beforeDelete()
208
    {
209
        if ($this->id) {
210
            if ($this->options) {
211
                foreach ($this->options as $option) {
212
                    $option->delete();
213
                }
214
            }
215
            if ($this->offers) {
216
                foreach ($this->offers as $offer) {
217
                    $offer->delete();
218
                }
219
            }
220
            if ($this->image) {
221
                $this->image->delete();
222
            }
223
        }
224
    }
225
226
    public function getHref()
227
    {
228
        return "/ecommerce/view/{$this->pk()}";
229
    }
230
231
}
232