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;
205 if ((substr($fieldId, -5) ==
"_from"))
207 $fieldId = substr($fieldId, 0, strlen($fieldId)-5);
209 elseif ((substr($fieldId, -3) ==
"_to"))
211 $fieldId = substr($fieldId, 0, strlen($fieldId)-3);
217 if ($newDateType == DateType::EXACT || $newDateType == DateType::RANGE)
219 if (array_key_exists($fieldId.
"_1", $oldFields))
221 $from = $oldFields[$fieldId.
"_1"][
"value"];
222 $to = $oldFields[$fieldId.
"_2"][
"value"];
224 elseif (array_key_exists($fieldId.
"_from", $oldFields))
226 $from = $oldFields[$fieldId.
"_from"][
"value"];
227 $to = $oldFields[$fieldId.
"_to"][
"value"];
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"] =
"";
239 elseif (substr($fieldId, -2) ===
"_1")
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))
245 $newFields[$ratioFields[$fieldId].
"_numsel"] =
"range";
246 $newFields[$ratioFields[$fieldId].
"_from"] = $field[
"value"];
247 $newFields[$ratioFields[$fieldId].
"_to"] = $oldFields[$fieldId.
"_2"][
"value"];
250 elseif (substr($fieldId, -6) ===
"_start")
252 $fieldId = substr($fieldId, 0, strlen($fieldId)-6);
253 if (array_key_exists($fieldId, $ratioFields) && array_key_exists($fieldId.
"_end", $oldFields) &&
254 !array_key_exists($fieldId.
"_FILTER_PERIOD", $oldFields))
256 $newFields[$ratioFields[$fieldId].
"_numsel"] =
"range";
257 $newFields[$ratioFields[$fieldId].
"_start"] = $field[
"value"];
258 $newFields[$ratioFields[$fieldId].
"_end"] = $oldFields[$fieldId.
"_end"][
"value"];
261 elseif ((
bool)strtotime($field[
"value"]) && !array_key_exists($fieldId.
"_FILTER_DIRECTION", $oldFields))
263 if ((substr($fieldId, -5) ==
"_from"))
265 $fieldId = substr($fieldId, 0, strlen($fieldId)-5);
267 elseif ((substr($fieldId, -3) ==
"_to"))
269 $fieldId = substr($fieldId, 0, strlen($fieldId)-3);
273 if (array_key_exists($fieldId.
"_from", $oldFields))
275 $from = $oldFields[$fieldId.
"_from"][
"value"];
276 $to = $oldFields[$fieldId.
"_to"][
"value"];
278 $newFields[$ratioFields[$fieldId].
"_datesel"] = DateType::RANGE;
279 $newFields[$ratioFields[$fieldId].
"_from"] = $from;
280 $newFields[$ratioFields[$fieldId].
"_to"] = $to;
281 $newFields[$ratioFields[$fieldId].
"_days"] =
"";
282 $newFields[$ratioFields[$fieldId].
"_month"] =
"";
283 $newFields[$ratioFields[$fieldId].
"_quarter"] =
"";
284 $newFields[$ratioFields[$fieldId].
"_year"] =
"";
286 elseif (substr($fieldId, -5) ==
"_from" && !array_key_exists($fieldId.
"_FILTER_DIRECTION", $oldFields))
288 $fieldId = substr($fieldId, 0, strlen($fieldId)-5);
289 $rangeType = (($oldFields[$fieldId.
"_from"] === $oldFields[$fieldId.
"_to"]) ?
"exact" :
"range");
290 $newFields[$ratioFields[$fieldId].
"_numsel"] = $rangeType;
291 $newFields[$ratioFields[$fieldId].
"_from"] = $field[
"value"];
293 elseif (substr($fieldId, -3) ==
"_to")
295 $fieldId = substr($fieldId, 0, strlen($fieldId)-3);
296 if (!array_key_exists($fieldId.
"_from".
"_FILTER_DIRECTION", $oldFields))
298 $rangeType = (($oldFields[$fieldId.
"_from"] === $oldFields[$fieldId.
"_to"]) ?
"exact" :
"range");
299 $newFields[$ratioFields[$fieldId].
"_numsel"] = $rangeType;
300 $newFields[$ratioFields[$fieldId].
"_to"] = $field[
"value"];
303 elseif (array_key_exists($fieldId, $ratioFields))
305 $newFields[$ratioFields[$fieldId]] = $field[
"value"];
308 if (!in_array($ratioFields[$fieldId], $newRows) && $ratioFields[$fieldId])
310 $newRows[] = $ratioFields[$fieldId];
314 return array($newFields, $newRows);
319 $newDateType = DateType::EXACT;
321 switch ($oldDateType)
324 switch ($oldDateValue)
327 $newDateType = DateType::YESTERDAY;
330 $newDateType = DateType::CURRENT_DAY;
333 $newDateType = DateType::TOMORROW;
338 switch ($oldDateValue)
341 $newDateType = DateType::LAST_WEEK;
344 $newDateType = DateType::CURRENT_WEEK;
347 $newDateType = DateType::NEXT_WEEK;
352 switch ($oldDateValue)
355 $newDateType = DateType::LAST_MONTH;
358 $newDateType = DateType::CURRENT_MONTH;
361 $newDateType = DateType::NEXT_MONTH;
366 switch ($oldDateValue)
369 $newDateType = DateType::CURRENT_QUARTER;
373 $newDateType = DateType::RANGE;
378 $newDateType = DateType::RANGE;
381 $newDateType = DateType::EXACT;
386 $newDateType = DateType::RANGE;