Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
datefield.php
1<?php
9
12use Bitrix\Main\UI\Filter\Options as FilterOptions;
15
21{
28 public function fetchFieldValue($filterFields)
29 {
31 $this->getId() . '_datesel',
32 $filterFields
33 );
34
35 $allowYearName = $this->getAllowYearName();
36 if (!isset($result[$allowYearName]) && isset($filterFields[$allowYearName]))
37 {
38 $result[$allowYearName] = $filterFields[$allowYearName];
39 }
40
41 if (!empty($result))
42 {
43 return $result;
44 }
45
46 if (!$this->isCustomDate())
47 {
48 return [];
49 }
50
51 $result = [];
52 foreach(['days', 'month', 'year'] as $key)
53 {
54 $fieldKey = $this->getId() . '_' . $key;
55 if (empty($filterFields[$fieldKey]))
56 {
57 continue;
58 }
59
60 $result[$fieldKey] = $filterFields[$fieldKey];
61 }
62
63 return $result;
64 }
65
72 public function applyFilter(array &$filter = array())
73 {
74 $filterKey = $this->getFilterKey();
75 $from = $this->isMoreThanDaysAgo() ? null : $this->getFrom();
76 $to = $this->isAfterDays() ? null : $this->getTo();
77
78 if ($from)
79 {
80 if ($this->isAllowYears())
81 {
82 $filter[">=$filterKey"] = $from;
83 }
84 else
85 {
86 $filter[] = $this->getFilterYearLess('FROM', $from, ">=");
87 }
88 }
89 if ($to)
90 {
91 if ($this->isAllowYears())
92 {
93 $filter["<=$filterKey"] = $to;
94 }
95 else
96 {
97 $filter[] = $this->getFilterYearLess('TO', $to, "<=", $from);
98 }
99 }
100 if ($this->getDays())
101 {
102 $fieldId = $this->getId();
103 $filterKey = $this->getId() . '_EXPR_DAYS';
104 $filter[] = (new RuntimeFilter())
105 ->setFilter(
106 '=' . $filterKey,
107 $this->getDays()
108 )
109 ->addRuntime([
110 'name' => $filterKey,
111 'expression' => "EXTRACT(DAY from %s)",
112 'buildFrom' => [$fieldId],
113 'parameters' => []
114 ]);
115 }
116 if ($this->getMonths())
117 {
118 $fieldId = $this->getId();
119 $filterKey = $this->getId() . '_EXPR_MONTHS';
120 $filter[] = (new RuntimeFilter())
121 ->setFilter(
122 '=' . $filterKey,
123 $this->getMonths()
124 )
125 ->addRuntime([
126 'name' => $filterKey,
127 'expression' => "EXTRACT(MONTH from %s)",
128 'buildFrom' => [$fieldId],
129 'parameters' => []
130 ]);
131 }
132 if ($this->getYears())
133 {
134 $fieldId = $this->getId();
135 $filterKey = $this->getId() . '_EXPR_YEARS';
136 $filter[] = (new RuntimeFilter())
137 ->setFilter(
138 '=' . $filterKey,
139 $this->getYears()
140 )
141 ->addRuntime([
142 'name' => $filterKey,
143 'expression' => "EXTRACT(YEAR from %s)",
144 'buildFrom' => [$fieldId],
145 'parameters' => []
146 ]);
147 }
148 }
149
156 public function getFrom($defaultValue = null)
157 {
158 return $this->getDate($defaultValue, true);
159 }
160
167 public function getTo($defaultValue = null)
168 {
169 return $this->getDate($defaultValue, false);
170 }
171
177 public function getDays()
178 {
179 return $this->getCustomDateData('days');
180 }
181
187 public function getMonths()
188 {
189 return $this->getCustomDateData('month');
190 }
191
197 public function getYears()
198 {
199 return $this->getCustomDateData('year');
200 }
201
202 private function getCustomDateData($key)
203 {
204 $value = $this->getDateDataByKey($key);
205 if (!$value || !is_array($value))
206 {
207 return [];
208 }
209
210 return array_map(
211 function ($item)
212 {
213 return (int) $item;
214 },
215 $value
216 );
217 }
218
219 private function isMoreThanDaysAgo()
220 {
221 return $this->getDateDataByKey('datesel') === AdditionalDateType::MORE_THAN_DAYS_AGO;
222 }
223
224 private function isAfterDays()
225 {
226 return $this->getDateDataByKey('datesel') === AdditionalDateType::AFTER_DAYS;
227 }
228
229 private function getDateDataByKey($key)
230 {
231 $key = $this->getId() . '_' . $key;
232 $value = $this->getValue();
233 if (!is_array($value) || count($value) === 0)
234 {
235 return null;
236 }
237
238 if (empty($value[$key]))
239 {
240 return null;
241 }
242
243 return $value[$key];
244 }
245
246 private function getDate($defaultValue = null, $isFrom = true)
247 {
248 $name = $this->getId();
249 $value = $this->getValue();
250 if (!is_array($value) || count($value) === 0)
251 {
252 return $defaultValue;
253 }
254
255 $calcData = array();
256 FilterOptions::calcDates($name, $value, $calcData);
257
258 if ($isFrom)
259 {
260 return isset($calcData[$name . '_from']) ? $calcData[$name . '_from'] : $defaultValue;
261 }
262 else
263 {
264 return isset($calcData[$name . '_to']) ? $calcData[$name . '_to'] : $defaultValue;
265 }
266 }
267
268 private function isCustomDate()
269 {
270 return isset($this->data['include']) && in_array(AdditionalDateType::CUSTOM_DATE, $this->data['include']);
271 }
272
273 private function isAllowYears()
274 {
275 if (!isset($this->data['allow_years_switcher']) || !$this->data['allow_years_switcher'])
276 {
277 return true;
278 }
279
280 return !empty($this->data['value'][$this->getAllowYearName()]);
281 }
282
283 private function getAllowYearName()
284 {
285 return $this->getId() . '_allow_year';
286 }
287
288 private function getFilterYearLess($tag, $value, $operation = "=", $fromValue = null)
289 {
290 $addOneYear = '';
291 $date = new Date($value);
292 if ($fromValue)
293 {
294 $dateFrom = new Date($fromValue);
295 if ($dateFrom->getTimestamp() > $date->getTimestamp())
296 {
297 $addOneYear = '+ 1';
298 }
299 }
300
301 $fieldId = $this->getId();
302 $expressionFieldName = $fieldId . "_YEAR_LESS_" . $tag;
303 $filterKey = $this->getFilterKey();
304
305 // hack for multiple user field of date type.
306 $uf = explode('.', $filterKey);
307 foreach ($uf as $item)
308 {
309 if (mb_strpos($item, 'UF_') !== 0)
310 {
311 continue;
312 }
313
314 $userField = UserFieldTable::getRow([
315 'select' => ['USER_TYPE_ID', 'MULTIPLE'],
316 'filter' => ['=FIELD_NAME' => $item]
317 ]);
318 if (!$userField || $userField['USER_TYPE_ID'] != 'date')
319 {
320 continue;
321 }
322 if ($userField['MULTIPLE'] != 'Y')
323 {
324 continue;
325 }
326
327 $filterKey .= '_SINGLE'; // Magic ORM postfix
328 }
329 // end hack
330
331 $sqlHelper = Application::getConnection()->getSqlHelper();
332
333 $charDate = $sqlHelper->getConcatFunction(
334 "(EXTRACT(YEAR from %s) $addOneYear)",
335 "'-{$date->format('m')}-{$date->format('d')}'"
336 );
337 $sqlDate = $sqlHelper->getDatetimeToDateFunction($charDate); // char -> date conversion
338
339 return (new RuntimeFilter())
340 ->setFilter(
341 "=$expressionFieldName",
342 1
343 )
344 ->addRuntime([
345 'name' => $expressionFieldName,
346 'expression' => "case when %s $operation $sqlDate then 1 else 0 end",
347 'buildFrom' => [
348 $filterKey,
349 $filterKey
350 ],
351 'parameters' => []
352 ]);
353 }
354}
static getConnection($name="")
static fetchDateFieldValue($key="", $filterFields=array())
Definition options.php:643
static calcDates($fieldId, $source, &$result)
Definition options.php:1265
applyFilter(array &$filter=array())
Definition datefield.php:72