Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
filteroption.php
1<?
3
8
14class FilterOption extends Stepper
15{
16 protected static $moduleId = "catalog";
17 protected $deleteFile = true;
18
19 protected $limit = 100;
20
36 public static function setFilterToConvert($filterId, $tableId, array $ratioFields)
37 {
38 $listFilter = Option::get(self::$moduleId, "listFilterToConvert", "");
39 if ($listFilter !== "" )
40 $listFilter = unserialize($listFilter);
41 $listFilter = is_array($listFilter) ? $listFilter : array();
42 if (!array_key_exists($filterId, $listFilter))
43 {
44 $listFilter[$filterId] = array(
45 "offset" => 0,
46 "tableId"=> $tableId,
47 "ratioFields" => $ratioFields
48 );
49 Option::set(self::$moduleId, "listFilterToConvert", serialize($listFilter));
50 }
51 }
52
53 public function execute(array &$option)
54 {
55 $listFilter = Option::get(self::$moduleId, "listFilterToConvert", "");
56 if ($listFilter !== "" )
57 $listFilter = unserialize($listFilter);
58 $listFilter = is_array($listFilter) ? $listFilter : array();
59 if (empty($listFilter))
60 {
61 Option::delete(self::$moduleId, array("name" => "listFilterToConvert"));
62 $GLOBALS["CACHE_MANAGER"]->cleanDir("user_option");
63 return false;
64 }
65
66 $connection = Application::getInstance()->getConnection();
67 $sqlHelper = $connection->getSqlHelper();
68
69 foreach ($listFilter as $filterId => $filter)
70 {
71 $queryObject = $connection->query("SELECT * FROM `b_filters` WHERE `FILTER_ID` = '".$sqlHelper->forSql(
72 $filterId)."' ORDER BY ID ASC LIMIT ".$this->limit." OFFSET ".$filter["offset"]);
73 $selectedRowsCount = $queryObject->getSelectedRowsCount();
74 while ($oldFilter = $queryObject->fetch())
75 {
76 $filters = array();
77 $listNewPresetName = [];
78 $oldFields = unserialize($oldFilter["FIELDS"]);
79 if (is_array($oldFields))
80 {
81 list($newFields, $newRows) = $this->convertOldFieldsToNewFields(
82 $oldFields, $filter["ratioFields"]);
83 $presetId = "filter_".(time()+(int)$oldFilter["ID"]);
84 $filters[$presetId] = array(
85 "name" => $oldFilter["NAME"],
86 "fields" => $newFields,
87 "filter_rows" => implode(",", $newRows)
88 );
89 $listNewPresetName[$presetId] = $oldFilter["NAME"];
90 }
91
92 if (empty($filters))
93 {
94 continue;
95 }
96
97 $queryOptionCurrentFilter = $connection->query(
98 "SELECT * FROM `b_user_option` WHERE
99 `CATEGORY` = 'main.ui.filter' AND
100 `USER_ID` = '".$sqlHelper->forSql($oldFilter["USER_ID"])."' AND
101 `NAME` = '".$sqlHelper->forSql($filter["tableId"])."'"
102 );
103 if ($optionCurrentFilter = $queryOptionCurrentFilter->fetch())
104 {
105 $optionCurrentFilterValue = unserialize($optionCurrentFilter["VALUE"]);
106 if (is_array($optionCurrentFilterValue))
107 {
108 if (!empty($optionCurrentFilterValue["filters"]))
109 {
110 // This is a check whether presets exist with that name.
111 foreach ($optionCurrentFilterValue["filters"] as $currentFilter)
112 {
113 $name = (!empty($currentFilter["name"]) ? $currentFilter["name"] : "");
114 $listNewPresetName = array_filter($listNewPresetName, function($oldName) use ($name) {
115 return ($oldName !== $name);
116 });
117 }
118
119 $filters = array_intersect_key($filters, $listNewPresetName);
120
121 $optionCurrentFilterValue["filters"] = array_merge(
122 $optionCurrentFilterValue["filters"], $filters);
123 $optionCurrentFilterValue["update_default_presets"] = true;
124
125 $connection->query(
126 "UPDATE `b_user_option` SET
127 `VALUE` = '" . $sqlHelper->forSql(serialize($optionCurrentFilterValue)) . "' WHERE
128 `ID` = '" . $sqlHelper->forSql($optionCurrentFilter["ID"]) . "'"
129 );
130 }
131 }
132 }
133 else
134 {
135 $optionNewFilter = array();
136 $optionNewFilter["filters"] = $filters;
137 $optionNewFilter["update_default_presets"] = true;
138
139 $connection->query(
140 "INSERT INTO `b_user_option`
141 (`ID`, `USER_ID`, `CATEGORY`, `NAME`, `VALUE`, `COMMON`) VALUES
142 (NULL, '".$sqlHelper->forSql($oldFilter["USER_ID"])."', 'main.ui.filter', '".
143 $sqlHelper->forSql($filter["tableId"])."', '".$sqlHelper->forSql(serialize($optionNewFilter)).
144 "', '".$sqlHelper->forSql($oldFilter["COMMON"])."')"
145 );
146 }
147 }
148
149 if ($selectedRowsCount < $this->limit)
150 {
151 unset($listFilter[$filterId]);
152 }
153 else
154 {
155 $listFilter[$filterId]["offset"] = $listFilter[$filterId]["offset"] + $selectedRowsCount;
156 }
157 }
158
159 $GLOBALS["CACHE_MANAGER"]->cleanDir("user_option");
160
161 if (!empty($listFilter))
162 {
163 Option::set(self::$moduleId, "listFilterToConvert", serialize($listFilter));
164 return true;
165 }
166 else
167 {
168 Option::delete(self::$moduleId, array("name" => "listFilterToConvert"));
169 return false;
170 }
171 }
172
173 protected function convertOldFieldsToNewFields(array $oldFields, array $ratioFields)
174 {
175 $newFields = [];
176 $newRows = [];
177 foreach ($oldFields as $fieldId => $field)
178 {
179 if ($field["hidden"] !== "false" || (array_key_exists($fieldId, $ratioFields) &&
180 array_key_exists($ratioFields[$fieldId], $newFields)))
181 continue;
182
183 if (preg_match("/_FILTER_PERIOD/", $fieldId, $matches, PREG_OFFSET_CAPTURE))
184 {
185 $searchResult = current($matches);
186 $oldDateType = $field["value"];
187 $dateFieldId = substr($fieldId, 0, $searchResult[1]);
188 $dateValue = array_key_exists($dateFieldId."_FILTER_DIRECTION", $oldFields) ?
189 $oldFields[$dateFieldId."_FILTER_DIRECTION"]["value"] : "";
190 $newDateType = $this->getNewDateType($oldDateType, $dateValue);
191
192 $custom = false;
193 if (substr($dateFieldId, -2) == "_1")
194 {
195 $custom = true;
196 $fieldId = substr($dateFieldId, 0, strlen($dateFieldId)-2);
197 }
198 else
199 {
200 $fieldId = $dateFieldId;
201 }
202
203 if (!$custom)
204 {
205 if ((substr($fieldId, -5) == "_from"))
206 {
207 $fieldId = substr($fieldId, 0, strlen($fieldId)-5);
208 }
209 elseif ((substr($fieldId, -3) == "_to"))
210 {
211 $fieldId = substr($fieldId, 0, strlen($fieldId)-3);
212 }
213 }
214
215 $from = "";
216 $to = "";
217 if ($newDateType == DateType::EXACT || $newDateType == DateType::RANGE)
218 {
219 if (array_key_exists($fieldId."_1", $oldFields))
220 {
221 $from = $oldFields[$fieldId."_1"]["value"];
222 $to = $oldFields[$fieldId."_2"]["value"];
223 }
224 elseif (array_key_exists($fieldId."_from", $oldFields))
225 {
226 $from = $oldFields[$fieldId."_from"]["value"];
227 $to = $oldFields[$fieldId."_to"]["value"];
228 }
229 }
230
231 $newFields[$ratioFields[$fieldId]."_datesel"] = $newDateType;
232 $newFields[$ratioFields[$fieldId]."_from"] = $from;
233 $newFields[$ratioFields[$fieldId]."_to"] = $to;
234 $newFields[$ratioFields[$fieldId]."_days"] = "";
235 $newFields[$ratioFields[$fieldId]."_month"] = "";
236 $newFields[$ratioFields[$fieldId]."_quarter"] = "";
237 $newFields[$ratioFields[$fieldId]."_year"] = "";
238 }
239 elseif (substr($fieldId, -2) === "_1")
240 {
241 $fieldId = substr($fieldId, 0, strlen($fieldId)-2);
242 if (array_key_exists($fieldId, $ratioFields) && array_key_exists($fieldId."_2", $oldFields) &&
243 !array_key_exists($fieldId."_FILTER_PERIOD", $oldFields))
244 {
245 $newFields[$ratioFields[$fieldId]."_numsel"] = "range";
246 $newFields[$ratioFields[$fieldId]."_from"] = $field["value"];
247 $newFields[$ratioFields[$fieldId]."_to"] = $oldFields[$fieldId."_2"]["value"];
248 }
249 }
250 elseif (substr($fieldId, -6) === "_start")
251 {
252 $fieldId = substr($fieldId, 0, strlen($fieldId)-6);
253 if (array_key_exists($fieldId, $ratioFields) && array_key_exists($fieldId."_end", $oldFields) &&
254 !array_key_exists($fieldId."_FILTER_PERIOD", $oldFields))
255 {
256 $newFields[$ratioFields[$fieldId]."_numsel"] = "range";
257 $newFields[$ratioFields[$fieldId]."_start"] = $field["value"];
258 $newFields[$ratioFields[$fieldId]."_end"] = $oldFields[$fieldId."_end"]["value"];
259 }
260 }
261 elseif ((bool)strtotime($field["value"]) && !array_key_exists($fieldId."_FILTER_DIRECTION", $oldFields))
262 {
263 if ((substr($fieldId, -5) == "_from"))
264 {
265 $fieldId = substr($fieldId, 0, strlen($fieldId)-5);
266 }
267 elseif ((substr($fieldId, -3) == "_to"))
268 {
269 $fieldId = substr($fieldId, 0, strlen($fieldId)-3);
270 }
271 $from = "";
272 $to = "";
273 if (array_key_exists($fieldId."_from", $oldFields))
274 {
275 $from = $oldFields[$fieldId."_from"]["value"];
276 $to = $oldFields[$fieldId."_to"]["value"];
277 }
278 $newFields[$ratioFields[$fieldId]."_datesel"] = DateType::RANGE;
279 $newFields[$ratioFields[$fieldId]."_from"] = $from;
280 $newFields[$ratioFields[$fieldId]."_to"] = $to;
281 $newFields[$ratioFields[$fieldId]."_days"] = "";
282 $newFields[$ratioFields[$fieldId]."_month"] = "";
283 $newFields[$ratioFields[$fieldId]."_quarter"] = "";
284 $newFields[$ratioFields[$fieldId]."_year"] = "";
285 }
286 elseif (substr($fieldId, -5) == "_from" && !array_key_exists($fieldId."_FILTER_DIRECTION", $oldFields))
287 {
288 $fieldId = substr($fieldId, 0, strlen($fieldId)-5);
289 $rangeType = (($oldFields[$fieldId."_from"] === $oldFields[$fieldId."_to"]) ? "exact" : "range");
290 $newFields[$ratioFields[$fieldId]."_numsel"] = $rangeType;
291 $newFields[$ratioFields[$fieldId]."_from"] = $field["value"];
292 }
293 elseif (substr($fieldId, -3) == "_to")
294 {
295 $fieldId = substr($fieldId, 0, strlen($fieldId)-3);
296 if (!array_key_exists($fieldId."_from"."_FILTER_DIRECTION", $oldFields))
297 {
298 $rangeType = (($oldFields[$fieldId."_from"] === $oldFields[$fieldId."_to"]) ? "exact" : "range");
299 $newFields[$ratioFields[$fieldId]."_numsel"] = $rangeType;
300 $newFields[$ratioFields[$fieldId]."_to"] = $field["value"];
301 }
302 }
303 elseif (array_key_exists($fieldId, $ratioFields))
304 {
305 $newFields[$ratioFields[$fieldId]] = $field["value"];
306 }
307
308 if (!in_array($ratioFields[$fieldId], $newRows) && $ratioFields[$fieldId])
309 {
310 $newRows[] = $ratioFields[$fieldId];
311 }
312 }
313
314 return array($newFields, $newRows);
315 }
316
317 protected function getNewDateType($oldDateType, $oldDateValue)
318 {
319 $newDateType = DateType::EXACT;
320
321 switch ($oldDateType)
322 {
323 case "day":
324 switch ($oldDateValue)
325 {
326 case "previous":
327 $newDateType = DateType::YESTERDAY;
328 break;
329 case "current":
330 $newDateType = DateType::CURRENT_DAY;
331 break;
332 case "next":
333 $newDateType = DateType::TOMORROW;
334 break;
335 }
336 break;
337 case "week":
338 switch ($oldDateValue)
339 {
340 case "previous":
341 $newDateType = DateType::LAST_WEEK;
342 break;
343 case "current":
344 $newDateType = DateType::CURRENT_WEEK;
345 break;
346 case "next":
347 $newDateType = DateType::NEXT_WEEK;
348 break;
349 }
350 break;
351 case "month":
352 switch ($oldDateValue)
353 {
354 case "previous":
355 $newDateType = DateType::LAST_MONTH;
356 break;
357 case "current":
358 $newDateType = DateType::CURRENT_MONTH;
359 break;
360 case "next":
361 $newDateType = DateType::NEXT_MONTH;
362 break;
363 }
364 break;
365 case "quarter":
366 switch ($oldDateValue)
367 {
368 case "current":
369 $newDateType = DateType::CURRENT_QUARTER;
370 break;
371 case "previous":
372 case "next":
373 $newDateType = DateType::RANGE;
374 break;
375 }
376 break;
377 case "year":
378 $newDateType = DateType::RANGE;
379 break;
380 case "exact":
381 $newDateType = DateType::EXACT;
382 break;
383 case "before":
384 case "after":
385 case "interval":
386 $newDateType = DateType::RANGE;
387 break;
388 }
389
390 return $newDateType;
391 }
392}
getNewDateType($oldDateType, $oldDateValue)
static setFilterToConvert($filterId, $tableId, array $ratioFields)
convertOldFieldsToNewFields(array $oldFields, array $ratioFields)
$GLOBALS['____1444769544']
Definition license.php:1