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