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))
61 Option::delete(self::$moduleId, array(
"name" =>
"listFilterToConvert"));
62 $GLOBALS[
"CACHE_MANAGER"]->cleanDir(
"user_option");
67 $sqlHelper = $connection->getSqlHelper();
69 foreach ($listFilter as $filterId => $filter)
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())
77 $listNewPresetName = [];
78 $oldFields = unserialize($oldFilter[
"FIELDS"]);
79 if (is_array($oldFields))
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)
89 $listNewPresetName[$presetId] = $oldFilter[
"NAME"];
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"]).
"'"
103 if ($optionCurrentFilter = $queryOptionCurrentFilter->fetch())
105 $optionCurrentFilterValue = unserialize($optionCurrentFilter[
"VALUE"]);
106 if (is_array($optionCurrentFilterValue))
108 if (!empty($optionCurrentFilterValue[
"filters"]))
111 foreach ($optionCurrentFilterValue[
"filters"] as $currentFilter)
113 $name = (!empty($currentFilter[
"name"]) ? $currentFilter[
"name"] :
"");
114 $listNewPresetName = array_filter($listNewPresetName,
function($oldName) use ($name) {
115 return ($oldName !== $name);
119 $filters = array_intersect_key($filters, $listNewPresetName);
121 $optionCurrentFilterValue[
"filters"] = array_merge(
122 $optionCurrentFilterValue[
"filters"], $filters);
123 $optionCurrentFilterValue[
"update_default_presets"] =
true;
126 "UPDATE `b_user_option` SET
127 `VALUE` = '" . $sqlHelper->forSql(serialize($optionCurrentFilterValue)) .
"' WHERE
128 `ID` = '" . $sqlHelper->forSql($optionCurrentFilter[
"ID"]) .
"'"
135 $optionNewFilter = array();
136 $optionNewFilter[
"filters"] = $filters;
137 $optionNewFilter[
"update_default_presets"] =
true;
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"]).
"')"
149 if ($selectedRowsCount < $this->limit)
151 unset($listFilter[$filterId]);
155 $listFilter[$filterId][
"offset"] = $listFilter[$filterId][
"offset"] + $selectedRowsCount;
159 $GLOBALS[
"CACHE_MANAGER"]->cleanDir(
"user_option");
161 if (!empty($listFilter))
163 Option::set(self::$moduleId,
"listFilterToConvert", serialize($listFilter));
168 Option::delete(self::$moduleId, array(
"name" =>
"listFilterToConvert"));
177 foreach ($oldFields as $fieldId => $field)
179 if ($field[
"hidden"] !==
"false" || (array_key_exists($fieldId, $ratioFields) &&
180 array_key_exists($ratioFields[$fieldId], $newFields)))
183 if (preg_match(
"/_FILTER_PERIOD/", $fieldId, $matches, PREG_OFFSET_CAPTURE))
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"] :
"";
193 if (substr($dateFieldId, -2) ==
"_1")
196 $fieldId = substr($dateFieldId, 0, strlen($dateFieldId)-2);
200 $fieldId = $dateFieldId;
204 if ((substr($fieldId, -5) ==
"_from"))
206 $fieldId = substr($fieldId, 0, strlen($fieldId)-5);
208 elseif ((substr($fieldId, -3) ==
"_to"))
210 $fieldId = substr($fieldId, 0, strlen($fieldId)-3);
216 if ($newDateType == DateType::EXACT || $newDateType == DateType::RANGE)
218 if (array_key_exists($fieldId.
"_1", $oldFields))
220 $from = $oldFields[$fieldId.
"_1"][
"value"];
221 $to = $oldFields[$fieldId.
"_2"][
"value"];
223 elseif (array_key_exists($fieldId.
"_from", $oldFields))
225 $from = $oldFields[$fieldId.
"_from"][
"value"];
226 $to = $oldFields[$fieldId.
"_to"][
"value"];
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"] =
"";
237 elseif (substr($fieldId, -2) ===
"_1")
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))
243 $newFields[$ratioFields[$fieldId].
"_numsel"] =
"range";
244 $newFields[$ratioFields[$fieldId].
"_from"] = $field[
"value"];
245 $newFields[$ratioFields[$fieldId].
"_to"] = $oldFields[$fieldId.
"_2"][
"value"];
248 elseif (substr($fieldId, -6) ===
"_start")
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))
254 $newFields[$ratioFields[$fieldId].
"_numsel"] =
"range";
255 $newFields[$ratioFields[$fieldId].
"_start"] = $field[
"value"];
256 $newFields[$ratioFields[$fieldId].
"_end"] = $oldFields[$fieldId.
"_end"][
"value"];
259 elseif (substr($fieldId, -5) ==
"_from" && !array_key_exists($fieldId.
"_FILTER_DIRECTION", $oldFields))
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"];
266 elseif (substr($fieldId, -3) ==
"_to")
268 $fieldId = substr($fieldId, 0, strlen($fieldId)-3);
269 if (!array_key_exists($fieldId.
"_from".
"_FILTER_DIRECTION", $oldFields))
271 $rangeType = (($oldFields[$fieldId.
"_from"] === $oldFields[$fieldId.
"_to"]) ?
"exact" :
"range");
272 $newFields[$ratioFields[$fieldId].
"_numsel"] = $rangeType;
273 $newFields[$ratioFields[$fieldId].
"_to"] = $field[
"value"];
276 elseif (array_key_exists($fieldId, $ratioFields))
278 $newFields[$ratioFields[$fieldId]] = $field[
"value"];
281 if (!in_array($ratioFields[$fieldId], $newRows) && $ratioFields[$fieldId])
283 $newRows[] = $ratioFields[$fieldId];
287 return array($newFields, $newRows);
292 $newDateType = DateType::EXACT;
294 switch ($oldDateType)
297 switch ($oldDateValue)
300 $newDateType = DateType::YESTERDAY;
303 $newDateType = DateType::CURRENT_DAY;
306 $newDateType = DateType::TOMORROW;
311 switch ($oldDateValue)
314 $newDateType = DateType::LAST_WEEK;
317 $newDateType = DateType::CURRENT_WEEK;
320 $newDateType = DateType::NEXT_WEEK;
325 switch ($oldDateValue)
328 $newDateType = DateType::LAST_MONTH;
331 $newDateType = DateType::CURRENT_MONTH;
334 $newDateType = DateType::NEXT_MONTH;
339 switch ($oldDateValue)
342 $newDateType = DateType::CURRENT_QUARTER;
346 $newDateType = DateType::RANGE;
351 $newDateType = DateType::RANGE;
354 $newDateType = DateType::EXACT;
359 $newDateType = DateType::RANGE;