Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
filteroption.php
1<?
2namespace Bitrix\Sale\Update;
3
8
14class FilterOption extends Stepper
15{
16 protected static $moduleId = "sale";
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 $integer = false;
193 if (substr($dateFieldId, -2) == "_1")
194 {
195 $integer = true;
196 $fieldId = substr($dateFieldId, 0, strlen($dateFieldId)-2);
197 }
198 else
199 {
200 $fieldId = $dateFieldId;
201 }
202 if (!$integer)
203 {
204 if ((substr($fieldId, -5) == "_from"))
205 {
206 $fieldId = substr($fieldId, 0, strlen($fieldId)-5);
207 }
208 elseif ((substr($fieldId, -3) == "_to"))
209 {
210 $fieldId = substr($fieldId, 0, strlen($fieldId)-3);
211 }
212 }
213
214 $from = "";
215 $to = "";
216 if ($newDateType == DateType::EXACT || $newDateType == DateType::RANGE)
217 {
218 if (array_key_exists($fieldId."_1", $oldFields))
219 {
220 $from = $oldFields[$fieldId."_1"]["value"];
221 $to = $oldFields[$fieldId."_2"]["value"];
222 }
223 elseif (array_key_exists($fieldId."_from", $oldFields))
224 {
225 $from = $oldFields[$fieldId."_from"]["value"];
226 $to = $oldFields[$fieldId."_to"]["value"];
227 }
228 }
229 $newFields[$ratioFields[$fieldId]."_datesel"] = $newDateType;
230 $newFields[$ratioFields[$fieldId]."_from"] = $from;
231 $newFields[$ratioFields[$fieldId]."_to"] = $to;
232 $newFields[$ratioFields[$fieldId]."_days"] = "";
233 $newFields[$ratioFields[$fieldId]."_month"] = "";
234 $newFields[$ratioFields[$fieldId]."_quarter"] = "";
235 $newFields[$ratioFields[$fieldId]."_year"] = "";
236 }
237 elseif (substr($fieldId, -2) === "_1")
238 {
239 $fieldId = substr($fieldId, 0, strlen($fieldId)-2);
240 if (array_key_exists($fieldId, $ratioFields) && array_key_exists($fieldId."_2", $oldFields) &&
241 !array_key_exists($fieldId."_FILTER_PERIOD", $oldFields))
242 {
243 $newFields[$ratioFields[$fieldId]."_numsel"] = "range";
244 $newFields[$ratioFields[$fieldId]."_from"] = $field["value"];
245 $newFields[$ratioFields[$fieldId]."_to"] = $oldFields[$fieldId."_2"]["value"];
246 }
247 }
248 elseif (substr($fieldId, -6) === "_start")
249 {
250 $fieldId = substr($fieldId, 0, strlen($fieldId)-6);
251 if (array_key_exists($fieldId, $ratioFields) && array_key_exists($fieldId."_end", $oldFields) &&
252 !array_key_exists($fieldId."_FILTER_PERIOD", $oldFields))
253 {
254 $newFields[$ratioFields[$fieldId]."_numsel"] = "range";
255 $newFields[$ratioFields[$fieldId]."_start"] = $field["value"];
256 $newFields[$ratioFields[$fieldId]."_end"] = $oldFields[$fieldId."_end"]["value"];
257 }
258 }
259 elseif (substr($fieldId, -5) == "_from" && !array_key_exists($fieldId."_FILTER_DIRECTION", $oldFields))
260 {
261 $fieldId = substr($fieldId, 0, strlen($fieldId)-5);
262 $rangeType = (($oldFields[$fieldId."_from"] === $oldFields[$fieldId."_to"]) ? "exact" : "range");
263 $newFields[$ratioFields[$fieldId]."_numsel"] = $rangeType;
264 $newFields[$ratioFields[$fieldId]."_from"] = $field["value"];
265 }
266 elseif (substr($fieldId, -3) == "_to")
267 {
268 $fieldId = substr($fieldId, 0, strlen($fieldId)-3);
269 if (!array_key_exists($fieldId."_from"."_FILTER_DIRECTION", $oldFields))
270 {
271 $rangeType = (($oldFields[$fieldId."_from"] === $oldFields[$fieldId."_to"]) ? "exact" : "range");
272 $newFields[$ratioFields[$fieldId]."_numsel"] = $rangeType;
273 $newFields[$ratioFields[$fieldId]."_to"] = $field["value"];
274 }
275 }
276 elseif (array_key_exists($fieldId, $ratioFields))
277 {
278 $newFields[$ratioFields[$fieldId]] = $field["value"];
279 }
280
281 if (!in_array($ratioFields[$fieldId], $newRows) && $ratioFields[$fieldId])
282 {
283 $newRows[] = $ratioFields[$fieldId];
284 }
285 }
286
287 return array($newFields, $newRows);
288 }
289
290 protected function getNewDateType($oldDateType, $oldDateValue)
291 {
292 $newDateType = DateType::EXACT;
293
294 switch ($oldDateType)
295 {
296 case "day":
297 switch ($oldDateValue)
298 {
299 case "previous":
300 $newDateType = DateType::YESTERDAY;
301 break;
302 case "current":
303 $newDateType = DateType::CURRENT_DAY;
304 break;
305 case "next":
306 $newDateType = DateType::TOMORROW;
307 break;
308 }
309 break;
310 case "week":
311 switch ($oldDateValue)
312 {
313 case "previous":
314 $newDateType = DateType::LAST_WEEK;
315 break;
316 case "current":
317 $newDateType = DateType::CURRENT_WEEK;
318 break;
319 case "next":
320 $newDateType = DateType::NEXT_WEEK;
321 break;
322 }
323 break;
324 case "month":
325 switch ($oldDateValue)
326 {
327 case "previous":
328 $newDateType = DateType::LAST_MONTH;
329 break;
330 case "current":
331 $newDateType = DateType::CURRENT_MONTH;
332 break;
333 case "next":
334 $newDateType = DateType::NEXT_MONTH;
335 break;
336 }
337 break;
338 case "quarter":
339 switch ($oldDateValue)
340 {
341 case "current":
342 $newDateType = DateType::CURRENT_QUARTER;
343 break;
344 case "previous":
345 case "next":
346 $newDateType = DateType::RANGE;
347 break;
348 }
349 break;
350 case "year":
351 $newDateType = DateType::RANGE;
352 break;
353 case "exact":
354 $newDateType = DateType::EXACT;
355 break;
356 case "before":
357 case "after":
358 case "interval":
359 $newDateType = DateType::RANGE;
360 break;
361 }
362
363 return $newDateType;
364 }
365}
getNewDateType($oldDateType, $oldDateValue)
static setFilterToConvert($filterId, $tableId, array $ratioFields)
convertOldFieldsToNewFields(array $oldFields, array $ratioFields)
$GLOBALS['____1444769544']
Definition license.php:1