Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
filteroption.php
1<?
3
8
14class FilterOption extends Stepper
15{
16 protected static $moduleId = "iblock";
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 (isset($ratioFields[$fieldId."_integer"]))
251 {
252 $fieldId = $fieldId."_integer";
253 $newFields[$ratioFields[$fieldId]."_numsel"] = "exact";
254 $newFields[$ratioFields[$fieldId]."_from"] = $field["value"];
255 $newFields[$ratioFields[$fieldId]."_to"] = $field["value"];
256 }
257 elseif (substr($fieldId, -6) === "_start")
258 {
259 $fieldId = substr($fieldId, 0, strlen($fieldId)-6);
260 if (array_key_exists($fieldId, $ratioFields) && array_key_exists($fieldId."_end", $oldFields) &&
261 !array_key_exists($fieldId."_FILTER_PERIOD", $oldFields))
262 {
263 $newFields[$ratioFields[$fieldId]."_numsel"] = "range";
264 $newFields[$ratioFields[$fieldId]."_start"] = $field["value"];
265 $newFields[$ratioFields[$fieldId]."_end"] = $oldFields[$fieldId."_end"]["value"];
266 }
267 }
268 elseif ((bool)strtotime($field["value"]))
269 {
270 if ((substr($fieldId, -5) == "_from"))
271 {
272 $fieldId = substr($fieldId, 0, strlen($fieldId)-5);
273 }
274 elseif ((substr($fieldId, -3) == "_to"))
275 {
276 $fieldId = substr($fieldId, 0, strlen($fieldId)-3);
277 }
278 $from = "";
279 $to = "";
280 if (array_key_exists($fieldId."_from", $oldFields))
281 {
282 $from = $oldFields[$fieldId."_from"]["value"];
283 $to = $oldFields[$fieldId."_to"]["value"];
284 }
285 if ($from || $to)
286 {
287 $newFields[$ratioFields[$fieldId]."_datesel"] = DateType::RANGE;
288 $newFields[$ratioFields[$fieldId]."_from"] = $from;
289 $newFields[$ratioFields[$fieldId]."_to"] = $to;
290 $newFields[$ratioFields[$fieldId]."_days"] = "";
291 $newFields[$ratioFields[$fieldId]."_month"] = "";
292 $newFields[$ratioFields[$fieldId]."_quarter"] = "";
293 $newFields[$ratioFields[$fieldId]."_year"] = "";
294 }
295 }
296 elseif (substr($fieldId, -5) == "_from" && !array_key_exists($fieldId."_FILTER_DIRECTION", $oldFields))
297 {
298 $fieldId = substr($fieldId, 0, strlen($fieldId)-5);
299 $rangeType = (($oldFields[$fieldId."_from"] === $oldFields[$fieldId."_to"]) ? "exact" : "range");
300 $newFields[$ratioFields[$fieldId]."_numsel"] = $rangeType;
301 $newFields[$ratioFields[$fieldId]."_from"] = $field["value"];
302 }
303 elseif (substr($fieldId, -3) == "_to")
304 {
305 $fieldId = substr($fieldId, 0, strlen($fieldId)-3);
306 if (!array_key_exists($fieldId."_from"."_FILTER_DIRECTION", $oldFields))
307 {
308 $rangeType = (($oldFields[$fieldId."_from"] === $oldFields[$fieldId."_to"]) ? "exact" : "range");
309 $newFields[$ratioFields[$fieldId]."_numsel"] = $rangeType;
310 $newFields[$ratioFields[$fieldId]."_to"] = $field["value"];
311 }
312 }
313 elseif (array_key_exists($fieldId, $ratioFields))
314 {
315 $newFields[$ratioFields[$fieldId]] = $field["value"];
316 }
317
318 if (!in_array($ratioFields[$fieldId], $newRows) && $ratioFields[$fieldId])
319 {
320 $newRows[] = $ratioFields[$fieldId];
321 }
322 }
323
324 return array($newFields, $newRows);
325 }
326
327 protected function getNewDateType($oldDateType, $oldDateValue)
328 {
329 $newDateType = DateType::EXACT;
330
331 switch ($oldDateType)
332 {
333 case "day":
334 switch ($oldDateValue)
335 {
336 case "previous":
337 $newDateType = DateType::YESTERDAY;
338 break;
339 case "current":
340 $newDateType = DateType::CURRENT_DAY;
341 break;
342 case "next":
343 $newDateType = DateType::TOMORROW;
344 break;
345 }
346 break;
347 case "week":
348 switch ($oldDateValue)
349 {
350 case "previous":
351 $newDateType = DateType::LAST_WEEK;
352 break;
353 case "current":
354 $newDateType = DateType::CURRENT_WEEK;
355 break;
356 case "next":
357 $newDateType = DateType::NEXT_WEEK;
358 break;
359 }
360 break;
361 case "month":
362 switch ($oldDateValue)
363 {
364 case "previous":
365 $newDateType = DateType::LAST_MONTH;
366 break;
367 case "current":
368 $newDateType = DateType::CURRENT_MONTH;
369 break;
370 case "next":
371 $newDateType = DateType::NEXT_MONTH;
372 break;
373 }
374 break;
375 case "quarter":
376 switch ($oldDateValue)
377 {
378 case "current":
379 $newDateType = DateType::CURRENT_QUARTER;
380 break;
381 case "previous":
382 case "next":
383 $newDateType = DateType::RANGE;
384 break;
385 }
386 break;
387 case "year":
388 $newDateType = DateType::RANGE;
389 break;
390 case "exact":
391 $newDateType = DateType::EXACT;
392 break;
393 case "before":
394 case "after":
395 case "interval":
396 $newDateType = DateType::RANGE;
397 break;
398 }
399
400 return $newDateType;
401 }
402}
getNewDateType($oldDateType, $oldDateValue)
static setFilterToConvert($filterId, $tableId, array $ratioFields)
convertOldFieldsToNewFields(array $oldFields, array $ratioFields)
$GLOBALS['____1444769544']
Definition license.php:1