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))
62 Option::delete(self::$moduleId, array(
"name" =>
"listFilterToConvert"));
63 $GLOBALS[
"CACHE_MANAGER"]->cleanDir(
"user_option");
68 $sqlHelper = $connection->getSqlHelper();
70 foreach ($listFilter as $filterId => $filter)
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())
78 $listNewPresetName = [];
79 $oldFields = unserialize($oldFilter[
"FIELDS"], [
'allowed_classes' =>
false]);
80 if (is_array($oldFields))
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)
90 $listNewPresetName[$presetId] = $oldFilter[
"NAME"];
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"]).
"'"
104 if ($optionCurrentFilter = $queryOptionCurrentFilter->fetch())
106 $optionCurrentFilterValue = unserialize($optionCurrentFilter[
"VALUE"], [
'allowed_classes' =>
false]);
107 if (is_array($optionCurrentFilterValue))
109 if (!empty($optionCurrentFilterValue[
"filters"]))
112 foreach ($optionCurrentFilterValue[
"filters"] as $currentFilter)
114 $name = (!empty($currentFilter[
"name"]) ? $currentFilter[
"name"] :
"");
115 $listNewPresetName = array_filter($listNewPresetName,
function($oldName) use ($name) {
116 return ($oldName !== $name);
120 $filters = array_intersect_key($filters, $listNewPresetName);
122 $optionCurrentFilterValue[
"filters"] = array_merge(
123 $optionCurrentFilterValue[
"filters"], $filters);
124 $optionCurrentFilterValue[
"update_default_presets"] =
true;
127 "UPDATE b_user_option SET
128 VALUE = '" . $sqlHelper->forSql(serialize($optionCurrentFilterValue)) .
"' WHERE
129 ID = '" . $sqlHelper->forSql($optionCurrentFilter[
"ID"]) .
"'"
136 $optionNewFilter = array();
137 $optionNewFilter[
"filters"] = $filters;
138 $optionNewFilter[
"update_default_presets"] =
true;
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"]).
"')"
150 if ($selectedRowsCount < $this->limit)
152 unset($listFilter[$filterId]);
156 $listFilter[$filterId][
"offset"] = $listFilter[$filterId][
"offset"] + $selectedRowsCount;
160 $GLOBALS[
"CACHE_MANAGER"]->cleanDir(
"user_option");
162 if (!empty($listFilter))
164 Option::set(self::$moduleId,
"listFilterToConvert", serialize($listFilter));
169 Option::delete(self::$moduleId, array(
"name" =>
"listFilterToConvert"));
178 foreach ($oldFields as $fieldId => $field)
180 if ($field[
"hidden"] !==
"false" || (array_key_exists($fieldId, $ratioFields) &&
181 array_key_exists($ratioFields[$fieldId], $newFields)))
184 if (preg_match(
"/_FILTER_PERIOD/", $fieldId, $matches, PREG_OFFSET_CAPTURE))
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"] :
"";
194 if (mb_substr($dateFieldId, -2) ==
"_1")
197 $fieldId = mb_substr($dateFieldId, 0, mb_strlen($dateFieldId) - 2);
201 $fieldId = $dateFieldId;
206 if ((mb_substr($fieldId, -5) ==
"_from"))
208 $fieldId = mb_substr($fieldId, 0, mb_strlen($fieldId) - 5);
210 elseif ((mb_substr($fieldId, -3) ==
"_to"))
212 $fieldId = mb_substr($fieldId, 0, mb_strlen($fieldId) - 3);
218 if ($newDateType == DateType::EXACT || $newDateType == DateType::RANGE)
220 if (array_key_exists($fieldId.
"_1", $oldFields))
222 $from = $oldFields[$fieldId.
"_1"][
"value"];
223 $to = $oldFields[$fieldId.
"_2"][
"value"];
225 elseif (array_key_exists($fieldId.
"_from", $oldFields))
227 $from = $oldFields[$fieldId.
"_from"][
"value"];
228 $to = $oldFields[$fieldId.
"_to"][
"value"];
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"] =
"";
240 elseif (mb_substr($fieldId, -2) ===
"_1")
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))
246 $newFields[$ratioFields[$fieldId].
"_numsel"] =
"range";
247 $newFields[$ratioFields[$fieldId].
"_from"] = $field[
"value"];
248 $newFields[$ratioFields[$fieldId].
"_to"] = $oldFields[$fieldId.
"_2"][
"value"];
251 elseif (isset($ratioFields[$fieldId.
"_integer"]))
253 $fieldId = $fieldId.
"_integer";
254 $newFields[$ratioFields[$fieldId].
"_numsel"] =
"exact";
255 $newFields[$ratioFields[$fieldId].
"_from"] = $field[
"value"];
256 $newFields[$ratioFields[$fieldId].
"_to"] = $field[
"value"];
258 elseif (mb_substr($fieldId, -6) ===
"_start")
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))
264 $newFields[$ratioFields[$fieldId].
"_numsel"] =
"range";
265 $newFields[$ratioFields[$fieldId].
"_start"] = $field[
"value"];
266 $newFields[$ratioFields[$fieldId].
"_end"] = $oldFields[$fieldId.
"_end"][
"value"];
269 elseif ((
bool)strtotime($field[
"value"]))
271 if ((mb_substr($fieldId, -5) ==
"_from"))
273 $fieldId = mb_substr($fieldId, 0, mb_strlen($fieldId) - 5);
275 elseif ((mb_substr($fieldId, -3) ==
"_to"))
277 $fieldId = mb_substr($fieldId, 0, mb_strlen($fieldId) - 3);
281 if (array_key_exists($fieldId.
"_from", $oldFields))
283 $from = $oldFields[$fieldId.
"_from"][
"value"];
284 $to = $oldFields[$fieldId.
"_to"][
"value"];
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"] =
"";
297 elseif (mb_substr($fieldId, -5) ==
"_from" && !array_key_exists($fieldId.
"_FILTER_DIRECTION", $oldFields))
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"];
304 elseif (mb_substr($fieldId, -3) ==
"_to")
306 $fieldId = mb_substr($fieldId, 0, mb_strlen($fieldId) - 3);
307 if (!array_key_exists($fieldId.
"_from".
"_FILTER_DIRECTION", $oldFields))
309 $rangeType = (($oldFields[$fieldId.
"_from"] === $oldFields[$fieldId.
"_to"]) ?
"exact" :
"range");
310 $newFields[$ratioFields[$fieldId].
"_numsel"] = $rangeType;
311 $newFields[$ratioFields[$fieldId].
"_to"] = $field[
"value"];
314 elseif (array_key_exists($fieldId, $ratioFields))
316 $newFields[$ratioFields[$fieldId]] = $field[
"value"];
319 if (!in_array($ratioFields[$fieldId], $newRows) && $ratioFields[$fieldId])
321 $newRows[] = $ratioFields[$fieldId];
325 return array($newFields, $newRows);