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
|
|
|
return [ |
93
|
|
|
'ecommerce_item_item_category_id' => [ |
94
|
|
|
'type' => 'INDEX', |
95
|
|
|
'cols' => [ |
96
|
|
|
'item_category_id' |
97
|
|
|
] |
98
|
|
|
], |
99
|
|
|
'inji_ecommerce_item_item_tree_path' => [ |
100
|
|
|
'type' => 'INDEX', |
101
|
|
|
'cols' => [ |
102
|
|
|
'item_tree_path(255)' |
103
|
|
|
] |
104
|
|
|
], |
105
|
|
|
'ecommerce_item_item_search_index' => [ |
106
|
|
|
'type' => 'INDEX', |
107
|
|
|
'cols' => [ |
108
|
|
|
'item_search_index(255)' |
109
|
|
|
] |
110
|
|
|
], |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function beforeSave() { |
115
|
|
|
|
116
|
|
|
if ($this->id) { |
117
|
|
|
$this->search_index = $this->name . ' '; |
118
|
|
|
if ($this->category) { |
119
|
|
|
$this->search_index .= $this->category->name . ' '; |
120
|
|
|
} |
121
|
|
View Code Duplication |
if ($this->options) { |
|
|
|
|
122
|
|
|
foreach ($this->options as $option) { |
123
|
|
|
if ($option->item_option_searchable && $option->value) { |
124
|
|
|
if ($option->item_option_type != 'select') { |
125
|
|
|
$this->search_index .= $option->value . ' '; |
126
|
|
|
} elseif (!empty($option->option->items[$option->value])) { |
127
|
|
|
$option->option->items[$option->value]->value . ' '; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
if ($this->offers) { |
133
|
|
|
foreach ($this->offers as $offer) { |
134
|
|
View Code Duplication |
if ($offer->options) { |
|
|
|
|
135
|
|
|
foreach ($offer->options as $option) { |
136
|
|
|
if ($option->item_offer_option_searchable && $option->value) { |
137
|
|
|
if ($option->item_offer_option_type != 'select') { |
138
|
|
|
$this->search_index .= $option->value . ' '; |
139
|
|
|
} elseif (!empty($option->option->items[$option->value])) { |
140
|
|
|
$option->option->items[$option->value]->value . ' '; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public static function relations() { |
151
|
|
|
|
152
|
|
|
return [ |
153
|
|
|
'category' => [ |
154
|
|
|
'model' => 'Ecommerce\Category', |
155
|
|
|
'col' => 'category_id' |
156
|
|
|
], |
157
|
|
|
'options' => [ |
158
|
|
|
'type' => 'many', |
159
|
|
|
'model' => 'Ecommerce\Item\Param', |
160
|
|
|
'col' => 'item_id', |
161
|
|
|
//'resultKey' => 'code', |
|
|
|
|
162
|
|
|
'resultKey' => 'item_option_id', |
163
|
|
|
'join' => [Item\Option::table(), Item\Option::index() . ' = ' . Item\Param::colPrefix() . Item\Option::index()] |
164
|
|
|
], |
165
|
|
|
'offers' => [ |
166
|
|
|
'type' => 'many', |
167
|
|
|
'model' => 'Ecommerce\Item\Offer', |
168
|
|
|
'col' => 'item_id', |
169
|
|
|
], |
170
|
|
|
'type' => [ |
171
|
|
|
'model' => 'Ecommerce\Item\Type', |
172
|
|
|
'col' => 'item_type_id', |
173
|
|
|
], |
174
|
|
|
'image' => [ |
175
|
|
|
'model' => 'Files\File', |
176
|
|
|
'col' => 'image_file_id' |
177
|
|
|
], |
178
|
|
|
'images' => [ |
179
|
|
|
'type' => 'many', |
180
|
|
|
'model' => 'Ecommerce\Item\Image', |
181
|
|
|
'col' => 'item_id' |
182
|
|
|
], |
183
|
|
|
'user' => [ |
184
|
|
|
'model' => 'Users\User', |
185
|
|
|
'col' => 'user_id' |
186
|
|
|
] |
187
|
|
|
]; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function getPrice() { |
191
|
|
|
$offers = $this->offers(['key' => false]); |
192
|
|
|
$curPrice = null; |
193
|
|
|
|
194
|
|
|
foreach ($offers[0]->prices as $price) { |
195
|
|
|
if (!$price->type) { |
196
|
|
|
$curPrice = $price; |
197
|
|
|
} elseif ( |
198
|
|
|
(!$price->type->roles && !$curPrice) || |
199
|
|
|
($price->type->roles && !$curPrice && strpos($price->type->roles, "|" . \Users\User::$cur->role_id . "|") !== false) |
200
|
|
|
) { |
201
|
|
|
$curPrice = $price; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
return $curPrice; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function name() { |
208
|
|
|
if (!empty(\App::$primary->ecommerce->config['item_option_as_name'])) { |
209
|
|
|
$param = Item\Param::get([['item_id', $this->id], ['item_option_id', \App::$primary->ecommerce->config['item_option_as_name']]]); |
210
|
|
|
if ($param && $param->value) { |
211
|
|
|
return $param->value; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
return $this->name; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function beforeDelete() { |
218
|
|
|
if ($this->id) { |
219
|
|
|
if ($this->options) { |
220
|
|
|
foreach ($this->options as $option) { |
221
|
|
|
$option->delete(); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
if ($this->offers) { |
225
|
|
|
foreach ($this->offers as $offer) { |
226
|
|
|
$offer->delete(); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
if ($this->image) { |
230
|
|
|
$this->image->delete(); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function getHref() { |
236
|
|
|
return "/ecommerce/view/{$this->pk()}"; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
} |
240
|
|
|
|
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.