Completed
Push — master ( 8717df...fdbe7f )
by Alexey
05:44
created

Item::indexes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 23
rs 9.0856
c 1
b 0
f 0
cc 1
eloc 14
nc 1
nop 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
    ];
29
    public static $cols = [
30
        //Основные параметры
31
        'category_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'category'],
32
        'image_file_id' => ['type' => 'image'],
33
        'name' => ['type' => 'text'],
34
        'alias' => ['type' => 'text'],
35
        'description' => ['type' => 'html'],
36
        'item_type_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'type'],
37
        'best' => ['type' => 'bool'],
38
        //Системные
39
        'user_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'user'],
40
        'weight' => ['type' => 'number'],
41
        'sales' => ['type' => 'number'],
42
        'imported' => ['type' => 'text'],
43
        'tree_path' => ['type' => 'text'],
44
        'search_index' => ['type' => 'text'],
45
        'date_create' => ['type' => 'dateTime'],
46
        //Менеджеры
47
        'options' => ['type' => 'dataManager', 'relation' => 'options'],
48
        'offers' => ['type' => 'dataManager', 'relation' => 'offers'],
49
    ];
50
    public static $dataManagers = [
51
        'manager' => [
52
            'name' => 'Товары',
53
            'cols' => [
54
                'name',
55
                'category_id',
56
                'item_type_id',
57
                'best',
58
                'options',
59
                'offers',
60
            ],
61
            'categorys' => [
62
                'model' => 'Ecommerce\Category',
63
            ],
64
            'sortMode' => true
65
        ]
66
    ];
67
    public static $forms = [
68
        'manager' => [
69
            'map' => [
70
                ['name', 'alias'],
71
                ['category_id', 'item_type_id'],
72
                ['best', 'image_file_id'],
73
                ['description'],
74
                ['options'],
75
                ['offers'],
76
            ]
77
    ]];
78
79
    public static function indexes()
80
    {
81
        return [
82
            'ecommerce_item_item_category_id' => [
83
                'type' => 'INDEX',
84
                'cols' => [
85
                    'item_category_id'
86
                ]
87
            ],
88
            'inji_ecommerce_item_item_tree_path' => [
89
                'type' => 'INDEX',
90
                'cols' => [
91
                    'item_tree_path(255)'
92
                ]
93
            ],
94
            'ecommerce_item_item_search_index' => [
95
                'type' => 'INDEX',
96
                'cols' => [
97
                    'item_search_index(255)'
98
                ]
99
            ],
100
        ];
101
    }
102
103
    public function beforeSave()
104
    {
105
106
        if ($this->id) {
107
            $this->search_index = $this->name . ' ';
108
            if ($this->category) {
109
                $this->search_index .= $this->category->name . ' ';
110
            }
111
            if ($this->options) {
112
                foreach ($this->options as $option) {
113
                    if ($option->item_option_searchable && $option->value) {
114
                        if ($option->item_option_type != 'select') {
115
                            $this->search_index .= $option->value . ' ';
116
                        } elseif (!empty($option->option->items[$option->value])) {
117
                            $option->option->items[$option->value]->value . ' ';
118
                        }
119
                    }
120
                }
121
            }
122
        }
123
    }
124
125
    public static function relations()
126
    {
127
128
        return [
129
            'category' => [
130
                'model' => 'Ecommerce\Category',
131
                'col' => 'category_id'
132
            ],
133
            'options' => [
134
                'type' => 'many',
135
                'model' => 'Ecommerce\Item\Param',
136
                'col' => 'item_id',
137
                //'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...
138
                'resultKey' => 'item_option_id',
139
                'join' => [Item\Option::table(), Item\Option::index() . ' = ' . Item\Param::colPrefix() . Item\Option::index()]
140
            ],
141
            'offers' => [
142
                'type' => 'many',
143
                'model' => 'Ecommerce\Item\Offer',
144
                'col' => 'item_id',
145
            ],
146
            'type' => [
147
                'model' => 'Ecommerce\Item\Type',
148
                'col' => 'item_type_id',
149
            ],
150
            'image' => [
151
                'model' => 'Files\File',
152
                'col' => 'image_file_id'
153
            ],
154
            'user' => [
155
                'model' => 'Users\User',
156
                'col' => 'user_id'
157
            ]
158
        ];
159
    }
160
161
    public function getPrice()
162
    {
163
        $offers = $this->offers(['key' => false]);
164
        $curPrice = null;
165
166
        foreach ($offers[0]->prices as $price) {
167
            if (!$price->type) {
168
                $curPrice = $price;
169
            } elseif (
170
                    (!$price->type->roles && !$curPrice) ||
171
                    ($price->type->roles && !$curPrice && strpos($price->type->roles, "|" . \Users\User::$cur->role_id . "|") !== false)
172
            ) {
173
                $curPrice = $price;
174
            }
175
        }
176
        return $curPrice;
177
    }
178
179
    public function name()
180
    {
181
        if (!empty(\App::$primary->ecommerce->config['item_option_as_name'])) {
182
            $param = Item\Param::get([['item_id', $this->id], ['item_option_id', \App::$primary->ecommerce->config['item_option_as_name']]]);
183
            if ($param && $param->value) {
184
                return $param->value;
185
            }
186
        }
187
        return $this->name;
188
    }
189
190
    public function beforeDelete()
191
    {
192
        if ($this->id) {
193
            if ($this->options) {
194
                foreach ($this->options as $option) {
195
                    $option->delete();
196
                }
197
            }
198
            if ($this->offers) {
199
                foreach ($this->offers as $offer) {
200
                    $offer->delete();
201
                }
202
            }
203
            if ($this->image) {
204
                $this->image->delete();
205
            }
206
        }
207
    }
208
209
}
210