1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Helper for module methods |
5
|
|
|
* |
6
|
|
|
* @author Alexey Krupskiy <[email protected]> |
7
|
|
|
* @link http://inji.ru/ |
8
|
|
|
* @copyright 2016 Alexey Krupskiy |
9
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ecommerce; |
13
|
|
|
|
14
|
|
|
class OptionsParser extends \Object { |
15
|
|
|
|
16
|
|
|
public static function parse($options = []) { |
17
|
|
|
$selectOptions = self::getDefault($options); |
18
|
|
|
|
19
|
|
|
//parse order options |
20
|
|
|
self::order($options, $selectOptions); |
21
|
|
|
|
22
|
|
|
//joined offers |
23
|
|
|
$selectOptions['join'][] = [Item\Offer::table(), Item::index() . ' = ' . Item\Offer::colPrefix() . Item::index(), 'inner']; |
24
|
|
|
|
25
|
|
|
//joined prices, check price configs |
26
|
|
|
$selectOptions['join'][] = [Item\Offer\Price::table(), |
27
|
|
|
Item\Offer::index() . ' = ' . Item\Offer\Price::colPrefix() . Item\Offer::index() . |
28
|
|
|
(empty(\App::$cur->Ecommerce->config['show_zero_price']) ? ' and ' . Item\Offer\Price::colPrefix() . 'price>0' : ''), |
29
|
|
|
empty(\App::$cur->Ecommerce->config['show_without_price']) ? 'inner' : 'left']; |
30
|
|
|
|
31
|
|
|
//join item types |
32
|
|
|
$selectOptions['join'][] = [ |
33
|
|
|
Item\Offer\Price\Type::table(), Item\Offer\Price::colPrefix() . Item\Offer\Price\Type::index() . ' = ' . Item\Offer\Price\Type::index() |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
//add filter for item type user roles by current user role |
37
|
|
|
$selectOptions['where'][] = [ |
38
|
|
|
[Item\Offer\Price\Type::index(), NULL, 'is'], |
39
|
|
|
[ |
40
|
|
|
[Item\Offer\Price\Type::colPrefix() . 'roles', '', '=', 'OR'], |
41
|
|
|
[Item\Offer\Price\Type::colPrefix() . 'roles', '%|' . \Users\User::$cur->role_id . '|%', 'LIKE', 'OR'], |
42
|
|
|
], |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
//add custom preset filters from config on where |
46
|
|
|
self::presetFilters($options, $selectOptions); |
47
|
|
|
|
48
|
|
|
//parse filters |
49
|
|
|
self::filters($options, $selectOptions); |
50
|
|
|
|
51
|
|
|
//categories paths |
52
|
|
|
self::categories($options, $selectOptions); |
53
|
|
|
|
54
|
|
|
//search |
55
|
|
|
self::search($options, $selectOptions); |
56
|
|
|
|
57
|
|
|
self::warehouse($options, $selectOptions); |
58
|
|
|
|
59
|
|
|
$selectOptions['group'] = Item::index(); |
60
|
|
|
|
61
|
|
|
return $selectOptions; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public static function getDefault(&$options) { |
65
|
|
|
$selectOptions = [ |
66
|
|
|
'where' => !empty($options['where']) ? $options['where'] : [], |
67
|
|
|
'distinct' => false, |
68
|
|
|
'join' => [], |
69
|
|
|
'order' => [], |
70
|
|
|
'start' => isset($options['start']) ? (int) $options['start'] : 0, |
71
|
|
|
'key' => isset($options['key']) ? $options['key'] : null, |
72
|
|
|
'limit' => !empty($options['count']) ? (int) $options['count'] : 0, |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
//only not deleted items |
76
|
|
|
$selectOptions['where'][] = ['deleted', 0]; |
77
|
|
|
|
78
|
|
|
//check config of view items without images |
79
|
|
|
if (empty(\App::$cur->Ecommerce->config['view_empty_image'])) { |
80
|
|
|
$selectOptions['where'][] = ['image_file_id', 0, '!=']; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $selectOptions; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public static function order(&$options, &$selectOptions) { |
87
|
|
|
if (empty($options['sort']) || !is_array($options['sort'])) { |
88
|
|
|
return; |
89
|
|
|
} |
90
|
|
|
foreach ($options['sort'] as $col => $direction) { |
91
|
|
|
$direction = strtolower($direction) == 'desc' ? 'desc' : 'asc'; |
92
|
|
|
switch ($col) { |
93
|
|
|
case 'price': |
94
|
|
|
$selectOptions['order'][] = [Item\Offer\Price::colPrefix() . 'price', $direction]; |
95
|
|
|
break; |
96
|
|
|
case 'new': |
97
|
|
|
$selectOptions['order'][] = ['date_create', $direction]; |
98
|
|
|
break; |
99
|
|
|
case 'name': |
100
|
|
|
case 'sales': |
101
|
|
|
case 'weight': |
102
|
|
|
$selectOptions['order'][] = [$col, $direction]; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public static function presetFilters(&$options, &$selectOptions) { |
|
|
|
|
108
|
|
|
if (empty(\App::$cur->Ecommerce->config['view_filter'])) { |
109
|
|
|
return; |
110
|
|
|
} |
111
|
|
|
if (!empty(\App::$cur->Ecommerce->config['view_filter']['options'])) { |
112
|
|
|
foreach (\App::$cur->Ecommerce->config['view_filter']['options'] as $optionId => $optionValue) { |
113
|
|
|
$selectOptions['join'][] = [Item\Param::table(), Item::index() . ' = ' . 'option' . $optionId . '.' . Item\Param::colPrefix() . Item::index() . ' AND ' . |
114
|
|
|
'option' . $optionId . '.' . Item\Param::colPrefix() . Item\Option::index() . ' = "' . (int) $optionId . '" AND ' . |
115
|
|
|
'option' . $optionId . '.' . Item\Param::colPrefix() . 'value = "' . (int) $optionValue . '"', |
116
|
|
|
'inner', 'option' . $optionId]; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public static function filters(&$options, &$selectOptions) { |
122
|
|
|
if (empty($options['filters'])) { |
123
|
|
|
return; |
124
|
|
|
} |
125
|
|
|
foreach ($options['filters'] as $col => $filter) { |
126
|
|
|
switch ($col) { |
127
|
|
|
case 'price': |
128
|
|
|
$colName = Item\Offer\Price::colPrefix() . 'price'; |
129
|
|
View Code Duplication |
if (!empty($filter['min'])) { |
|
|
|
|
130
|
|
|
$selectOptions['where'][] = [$colName, (float) $filter['min'], '>=']; |
131
|
|
|
} |
132
|
|
View Code Duplication |
if (!empty($filter['max'])) { |
|
|
|
|
133
|
|
|
$selectOptions['where'][] = [$colName, (float) $filter['max'], '<=']; |
134
|
|
|
} |
135
|
|
|
break; |
136
|
|
|
case 'options': |
137
|
|
|
case 'offerOptions': |
138
|
|
|
$paramPrefix = $col == 'options' ? Item\Param::colPrefix() : Item\Offer\Param::colPrefix(); |
139
|
|
|
$itemIndex = $col == 'options' ? Item::index() : Item\Offer::index(); |
140
|
|
|
$optionIndex = $col == 'options' ? Item\Option::index() : Item\Offer\Option::index(); |
141
|
|
|
$table = $col == 'options' ? Item\Param::table() : Item\Offer\Param::table(); |
142
|
|
|
foreach ($filter as $optionId => $optionValue) { |
143
|
|
|
$optionId = (int) $optionId; |
144
|
|
|
if (is_array($optionValue)) { |
145
|
|
|
$optionValueArr = []; |
146
|
|
|
foreach ($optionValue as $val) { |
147
|
|
|
$optionValueArr[] = \App::$cur->db->connection->pdo->quote($val); |
148
|
|
|
} |
149
|
|
|
$qstr = 'IN (' . implode(',', $optionValueArr) . ')'; |
150
|
|
|
} else { |
151
|
|
|
$qstr = '= ' . \App::$cur->db->connection->pdo->quote($optionValue); |
152
|
|
|
} |
153
|
|
|
$selectOptions['join'][] = [$table, $itemIndex . ' = ' . 'option' . $optionId . '.' . $paramPrefix . $itemIndex . ' AND ' . |
154
|
|
|
'option' . $optionId . '.' . $paramPrefix . $optionIndex . ' = "' . (int) $optionId . '" AND ' . |
155
|
|
|
'option' . $optionId . '.' . $paramPrefix . 'value ' . $qstr . '', |
156
|
|
|
'inner', 'option' . $optionId]; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public static function categories(&$options, &$selectOptions) { |
163
|
|
|
if (empty($options['parent'])) { |
164
|
|
|
return; |
165
|
|
|
} |
166
|
|
|
if (strpos($options['parent'], ',') !== false) { |
167
|
|
|
$first = true; |
168
|
|
|
$where = []; |
169
|
|
|
foreach (explode(',', $options['parent']) as $categoryId) { |
170
|
|
|
if (!$categoryId) { |
171
|
|
|
continue; |
172
|
|
|
} |
173
|
|
|
$category = Category::get($categoryId); |
174
|
|
|
$where[] = ['tree_path', $category->tree_path . (int) $categoryId . '/%', 'LIKE', $first ? 'AND' : 'OR']; |
175
|
|
|
$first = false; |
176
|
|
|
} |
177
|
|
|
$selectOptions['where'][] = $where; |
178
|
|
|
} else { |
179
|
|
|
$category = Category::get($options['parent']); |
180
|
|
|
$selectOptions['where'][] = ['tree_path', $category->tree_path . (int) $options['parent'] . '/%', 'LIKE']; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public static function search(&$options, &$selectOptions) { |
185
|
|
|
if (empty($options['search'])) { |
186
|
|
|
return; |
187
|
|
|
} |
188
|
|
|
$searchStr = preg_replace('![^A-zА-я0-9 ]!iSu', ' ', $options['search']); |
189
|
|
|
$searchArr = []; |
190
|
|
|
foreach (explode(' ', $searchStr) as $part) { |
191
|
|
|
$part = trim($part); |
192
|
|
|
if ($part && strlen($part) > 2) { |
193
|
|
|
$searchArr[] = ['search_index', '%' . $part . '%', 'LIKE']; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
if (!empty($searchArr)) { |
197
|
|
|
$selectOptions['where'][] = $searchArr; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public static function warehouse(&$options, &$selectOptions) { |
|
|
|
|
202
|
|
|
if (!empty(\App::$cur->Ecommerce->config['view_empty_warehouse'])) { |
203
|
|
|
return; |
204
|
|
|
} |
205
|
|
|
$warehouseIds = []; |
206
|
|
View Code Duplication |
if (\App::$cur->geography && \Geography\City::$cur) { |
|
|
|
|
207
|
|
|
$warehouses = \Geography\City\Data::get([['code', 'warehouses'], ['city_id', \Geography\City::$cur->id]]); |
208
|
|
|
if ($warehouses && $warehouses->data) { |
209
|
|
|
foreach (explode(',', $warehouses->data) as $id) { |
210
|
|
|
$warehouseIds[$id] = $id; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
$selectOptions['where'][] = [ |
215
|
|
|
'( |
216
|
|
|
(SELECT COALESCE(sum(`' . Item\Offer\Warehouse::colPrefix() . 'count`),0) |
217
|
|
|
FROM ' . \App::$cur->db->table_prefix . Item\Offer\Warehouse::table() . ' iciw |
218
|
|
|
WHERE iciw.' . Item\Offer\Warehouse::colPrefix() . Item\Offer::index() . ' = ' . Item\Offer::index() . ' |
219
|
|
|
' . ($warehouseIds ? ' AND iciw.' . Item\Offer\Warehouse::colPrefix() . Warehouse::index() . ' IN(' . implode(',', $warehouseIds) . ')' : '') . ' |
220
|
|
|
) |
221
|
|
|
- |
222
|
|
|
(SELECT COALESCE(sum(' . Warehouse\Block::colPrefix() . 'count) ,0) |
223
|
|
|
FROM ' . \App::$cur->db->table_prefix . Warehouse\Block::table() . ' iewb |
224
|
|
|
inner JOIN ' . \App::$cur->db->table_prefix . Cart::table() . ' icc ON icc.' . Cart::index() . ' = iewb.' . Warehouse\Block::colPrefix() . Cart::index() . ' AND ( |
225
|
|
|
(`' . Cart::colPrefix() . 'warehouse_block` = 1 and `' . Cart::colPrefix() . 'cart_status_id` in(2,3,6)) || |
226
|
|
|
(`' . Cart::colPrefix() . Cart\Status::index() . '` in(0,1) and `' . Cart::colPrefix() . 'date_last_activ` >=subdate(now(),INTERVAL 30 MINUTE)) |
227
|
|
|
) |
228
|
|
|
WHERE iewb.' . Warehouse\Block::colPrefix() . Item\Offer::index() . ' = ' . Item\Offer::index() . ') |
229
|
|
|
)', |
230
|
|
|
0, |
231
|
|
|
'>' |
232
|
|
|
]; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
} |
236
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.