17 public static function decode($text, $limit = 0)
26 $date = new \Bitrix\Main\Type\DateTime();
28 if ($limit > 0 && mb_strlen($text) > $limit)
30 $text = mb_substr($text, 0, $limit);
33 $originalText = $text;
34 $text = mb_strtolower($text);
36 $workTimeStart = explode(
'.', \
Bitrix\Main\
Config\Option::get(
'calendar',
'work_time_start',
'9'));
37 $timeOfStartDate = str_pad(intval($workTimeStart[0] ?? 0), 2,
"0", STR_PAD_LEFT).
':'.str_pad(intval($workTimeStart[1] ?? 0), 2,
"0", STR_PAD_LEFT);
39 $workTimeEnd = explode(
'.', \
Bitrix\Main\
Config\Option::get(
'calendar',
'work_time_end',
'18'));
40 $timeOfEndDate = str_pad(intval($workTimeEnd[0] ?? 0), 2,
"0", STR_PAD_LEFT).
':'.str_pad(intval($workTimeEnd[1] ?? 0), 2,
"0", STR_PAD_LEFT);
42 $defaultPregExceptionSearch = Array(
'.',
'/',
'-',
':');
43 $defaultPregExceptionReplace = Array(
'\.',
'\/',
'\-',
'\:');
47 for ($i = 1; $i <= 11; $i++)
50 $pattern[$i] = $pattern[$i] <>
''? $pattern[$i]:
'bxt2dmetricskip';
51 $pattern[$i] = str_replace($defaultPregExceptionSearch, $defaultPregExceptionReplace, $pattern[$i]);
53 if (preg_match_all(
"/(".implode(
'|', $pattern).
")/", $text, $match, PREG_OFFSET_CAPTURE))
55 foreach ($match[0] as $matchPattern)
58 switch($matchPattern[0])
61 $matchType =
'DAYAFTERTOMORROW';
64 $matchType =
'TOMORROW';
70 $matchType =
'YESTERDAY';
73 $matchType =
'DAYBEFOREYESTERDAY';
79 $matchType =
'WEEKEND';
87 $position = defined(
"BX_UTF")? mb_strlen(substr($text, 0, $matchPattern[1])) : $matchPattern[1];
88 $matchWord = self::getMatchWord($originalText, $position);
90 $metrics[1][] = Array(
92 'COUNT' => mb_strlen($matchWord),
93 'POSITION' => $position,
102 for ($i = 1; $i <= 10; $i++)
104 $pattern[$i] = mb_strtolower(
Loc::getMessage(
"MAIN_TDC_METRIC_2_".$i));
105 $pattern[$i] = $pattern[$i] <>
''? $pattern[$i]:
'bxt2dmetricskip';
106 $pattern[$i] = str_replace($defaultPregExceptionSearch, $defaultPregExceptionReplace, $pattern[$i]);
108 if (preg_match_all(
"/(".implode(
'|', $pattern).
")/", $text, $match, PREG_OFFSET_CAPTURE))
110 foreach ($match[0] as $matchPattern)
113 switch($matchPattern[0])
116 $matchType =
'MONDAY';
117 if (is_array($metrics[1]))
119 foreach ($metrics[1] as $key => $metric)
121 if ($metric[
'TYPE'] ==
'WEEK')
123 $position = defined(
"BX_UTF")? mb_strlen(substr($text, 0, $matchPattern[1])) : $matchPattern[1];
124 $matchWord = self::getMatchWord($originalText, $position);
126 if ($matchPattern[1] < $metric[
'POSITION'] && $matchPattern[1] + mb_strlen($matchWord) >= $metric[
'POSITION'] + $metric[
'COUNT'])
128 unset($metrics[1][$key]);
129 if (empty($metrics[1]))
139 $matchType =
'TUESDAY';
144 $matchType =
'WEDNESDAY';
147 $matchType =
'THURSDAY';
150 $matchType =
'FRIDAY';
153 $matchType =
'SATURDAY';
157 $matchType =
'SUNDAY';
161 if ($matchType <>
'')
163 $position = defined(
"BX_UTF")? mb_strlen(substr($text, 0, $matchPattern[1])) : $matchPattern[1];
164 $matchWord = self::getMatchWord($originalText, $position);
166 $metrics[2][] = Array(
167 'TYPE' => $matchType,
168 'COUNT' => mb_strlen($matchWord),
169 'POSITION' => $position,
170 'MATCH' => $matchWord
178 for ($i = 1; $i <= 9; $i++)
180 $pattern[$i] = mb_strtolower(
Loc::getMessage(
"MAIN_TDC_METRIC_3_".$i));
181 $pattern[$i] = $pattern[$i] <>
''? $pattern[$i]:
'bxt2dmetricskip';
182 $pattern[$i] = str_replace($defaultPregExceptionSearch, $defaultPregExceptionReplace, $pattern[$i]);
184 if (preg_match_all(
"/(".implode(
'|', $pattern).
")/", $text, $match, PREG_OFFSET_CAPTURE))
186 foreach ($match[0] as $matchPattern)
189 switch($matchPattern[0])
192 $matchType =
'BEFORE';
195 $matchType =
'AFTER';
201 $matchType =
'PREVIOUS';
205 $matchType =
'ENDOF';
208 $matchType =
'MIDDLEOF';
211 $matchType =
'STARTOF';
219 if (in_array($matchType, Array(
'BEFORE',
'AFTER')) && isset($metrics[1]))
221 foreach ($metrics[1] as $key => $metric)
223 if (in_array($metric[
'TYPE'], Array(
'DAYAFTERTOMORROW',
'DAYBEFOREYESTERDAY')))
225 if ($metric[
'POSITION'] == $matchPattern[1])
230 else if (in_array($metric[
'TYPE'], Array(
'TOMORROW',
'YESTERDAY')))
232 if ($metric[
'POSITION'] == $matchPattern[1] + mb_strlen($matchPattern[0])+1)
234 if ($metric[
'TYPE'] ==
'TOMORROW')
236 $metrics[1][$key][
'TYPE'] = $matchType ==
'AFTER'?
'DAYAFTERTOMORROW':
'TODAY';
240 $metrics[1][$key][
'TYPE'] = $matchType ==
'AFTER'?
'TODAY':
'DAYBEFOREYESTERDAY';
248 if ($matchType <>
'')
250 $position = defined(
"BX_UTF")? mb_strlen(substr($text, 0, $matchPattern[1])) : $matchPattern[1];
251 $matchWord = self::getMatchWord($originalText, $position);
253 $metrics[3][] = Array(
254 'TYPE' => $matchType,
255 'COUNT' => mb_strlen($matchWord),
256 'POSITION' => $position,
257 'MATCH' => $matchWord
262 if (preg_match_all(
"/([0-2][0-9]:[0-5][0-9])/", $text, $match, PREG_OFFSET_CAPTURE))
264 foreach ($match[0] as $matchPattern)
266 $position = defined(
"BX_UTF")? mb_strlen(substr($text, 0, $matchPattern[1])) : $matchPattern[1];
267 $matchWord = self::getMatchWord($originalText, $position);
269 $metrics[3][] = Array(
271 'VALUE' => $matchPattern[0],
272 'COUNT' => mb_strlen($matchPattern[0]),
273 'POSITION' => $position,
274 'MATCH' => $matchWord
281 $patternOriginal = Array();
282 $patternLength = Array();
283 for ($i = 1; $i <= 3; $i++)
285 $patternOriginal[$i] = mb_strtolower(
Loc::getMessage(
"MAIN_TDC_METRIC_4_".$i));
286 $patternOriginal[$i] = $patternOriginal[$i] <>
''? $patternOriginal[$i]:
'bxt2dmetricskip';
287 $patternLength[$i] = mb_strlen($patternOriginal[$i]);
288 if ($patternOriginal[$i] !=
'bxt2dmetricskip')
290 $pattern[$i] = str_replace($defaultPregExceptionSearch, $defaultPregExceptionReplace, $patternOriginal[$i]);
291 $pattern[$i] = str_replace(
292 Array(
'yyyy',
'yy',
'mm',
'dd'),
293 Array(
'[1-2][0-9][0-9][0-9]',
'[0-9][0-9]',
'[0-1][0-9]',
'[0-3][0-9]'),
296 $pattern[$i] =
'^(?>[\s|\t]?)('.$pattern[$i].
')|[\s|^|\t]('.$pattern[$i].
')(?=[\s|$|\r|\t])';
299 if (preg_match_all(
"/".implode(
'|', $pattern).
"/", $text, $match, PREG_OFFSET_CAPTURE))
301 $count = count($match)-1;
302 for ($i = 1; $i <= $count; $i++)
304 foreach ($match[$i] as $matchPattern)
306 if (!$matchPattern[0])
313 switch(mb_strlen($matchPattern[0]))
315 case $patternLength[1]:
316 $matchType =
'DDMMYYYY';
317 $matchYear = mb_substr($matchPattern[0], mb_strpos($patternOriginal[1],
'yyyy'), 4);
318 $matchMonth = mb_substr($matchPattern[0], mb_strpos($patternOriginal[1],
'mm'), 2);
319 $matchDay = mb_substr($matchPattern[0], mb_strpos($patternOriginal[1],
'dd'), 2);
320 $matchPattern[0] = $matchYear.
'-'.$matchMonth.
'-'.$matchDay;
321 $matchLength = $patternLength[1];
323 case $patternLength[2]:
324 $matchType =
'DDMMYY';
325 $matchYear = mb_substr($matchPattern[0], mb_strpos($patternOriginal[2],
'yy'), 2);
326 $matchMonth = mb_substr($matchPattern[0], mb_strpos($patternOriginal[2],
'mm'), 2);
327 $matchDay = mb_substr($matchPattern[0], mb_strpos($patternOriginal[2],
'dd'), 2);
328 $matchPattern[0] =
'20'.$matchYear.
'-'.$matchMonth.
'-'.$matchDay;
329 $matchLength = $patternLength[2];
331 case $patternLength[3]:
333 $matchMonth = mb_substr($matchPattern[0], mb_strpos($patternOriginal[3],
'mm'), 2);
334 $matchDay = mb_substr($matchPattern[0], mb_strpos($patternOriginal[3],
'dd'), 2);
335 $matchPattern[0] = $date->format(
'Y').
'-'.$matchMonth.
'-'.$matchDay;
336 $matchLength = $patternLength[3];
340 $position = defined(
"BX_UTF")? mb_strlen(substr($text, 0, $matchPattern[1])) : $matchPattern[1];
341 $matchWord = mb_substr($originalText, $position, $matchLength);
343 $metrics[4][] = Array(
344 'TYPE' => $matchType,
345 'VALUE' => $matchPattern[0],
346 'COUNT' => $matchLength,
347 'POSITION' => $position,
348 'MATCH' => $matchWord
356 for ($i = 1; $i <= 5; $i++)
358 $pattern[$i] = mb_strtolower(
Loc::getMessage(
"MAIN_TDC_METRIC_5_".$i));
359 $pattern[$i] = $pattern[$i] <>
''? $pattern[$i]:
'bxt2dmetricskip';
360 $pattern[$i] = str_replace($defaultPregExceptionSearch, $defaultPregExceptionReplace, $pattern[$i]);
362 if (preg_match_all(
"/(".implode(
'|', $pattern).
")/", $text, $match, PREG_OFFSET_CAPTURE))
364 foreach ($match[0] as $matchPattern)
368 switch($matchPattern[0])
371 $matchType =
'MORNING';
372 $matchValue = $timeOfStartDate;
375 $matchType =
'LUNCH';
376 $matchValue =
'14:00';
381 $matchType =
'WORKDAY';
382 $matchValue = $timeOfEndDate;
384 if (isset($metrics[1]))
386 foreach ($metrics[1] as $key => $metric)
388 if ($metric[
'TYPE'] ==
'TODAY')
390 if ($metric[
'POSITION'] == $matchPattern[1] + mb_strlen($matchPattern[3]))
399 $matchType =
'EVENING';
400 $matchValue =
'20:00';
403 if ($matchType <>
'')
405 $position = defined(
"BX_UTF")? mb_strlen(substr($text, 0, $matchPattern[1])) : $matchPattern[1];
406 $matchWord = self::getMatchWord($originalText, $position);
408 $metrics[5][] = Array(
409 'TYPE' => $matchType,
410 'VALUE' => $matchValue,
411 'COUNT' => mb_strlen($matchWord),
412 'POSITION' => $position,
413 'MATCH' => $matchWord
420 foreach ($metrics as $values)
429 if ($countOfMetrics == 1 && (isset($metrics[3]) || isset($metrics[5])))
431 else if ($countOfMetrics == 1)
435 else if ($countOfMetrics == 2 && isset($metrics[3]))
437 $useDefault = !isset($metrics[5]);
439 else if ($countOfMetrics == 2 && isset($metrics[5]) && (isset($metrics[1]) || isset($metrics[2]) || isset($metrics[4])))
441 if (isset($metrics[1]))
443 foreach ($metrics[1] as $metric)
445 $progressDate = self::createDateUsingMetrics(1, $metric, $metrics[5]);
446 $matchParams = self::getTextForReplace($originalText, $metric, $metrics[5]);
450 else if (isset($metrics[2]))
452 foreach ($metrics[2] as $metric)
454 $progressDate = self::createDateUsingMetrics(2, $metric, $metrics[5]);
455 $matchParams = self::getTextForReplace($originalText, $metric, $metrics[5]);
459 else if (isset($metrics[4]))
461 foreach ($metrics[4] as $metric)
463 $progressDate = self::createDateUsingMetrics(4, $metric, $metrics[5]);
464 $matchParams = self::getTextForReplace($originalText, $metric, $metrics[5]);
469 else if ($countOfMetrics == 2 && isset($metrics[1]) && (isset($metrics[2]) || isset($metrics[4])))
471 if (isset($metrics[2]))
473 foreach ($metrics[2] as $metric)
476 $progressDate = self::createDateUsingMetrics(2, $metric, $array);
477 $matchParams = self::getTextForReplace($originalText, $metric, array());
481 elseif (isset($metrics[4]))
483 foreach ($metrics[4] as $metric)
486 $progressDate = self::createDateUsingMetrics(4, $metric, $array);
487 $matchParams = self::getTextForReplace($originalText, $metric, array());
499 $modificators = $metrics[3] ?? array();
500 if (isset($metrics[4]) && count($metrics[4]))
502 foreach ($metrics[4] as $metric)
504 $progressDate = self::createDateUsingMetrics(4, $metric, $modificators);
505 $matchParams = self::getTextForReplace($originalText, $metric, $modificators);
509 else if (isset($metrics[2]) && count($metrics[2]))
511 foreach ($metrics[2] as $metric)
513 $progressDate = self::createDateUsingMetrics(2, $metric, $modificators);
514 $matchParams = self::getTextForReplace($originalText, $metric, $modificators);
518 else if (isset($metrics[1]) && count($metrics[1]))
520 foreach ($metrics[1] as $metric)
522 $progressDate = self::createDateUsingMetrics(1, $metric, $modificators);
523 $matchParams = self::getTextForReplace($originalText, $metric, $modificators);
527 else if (isset($metrics[5]) && count($metrics[5]))
529 foreach ($metrics[5] as $metric)
531 $progressDate = self::createDateUsingMetrics(5, $metric, $modificators);
532 $matchParams = self::getTextForReplace($originalText, $metric, $modificators);
536 else if (isset($metrics[3]) && count($metrics[3]))
538 foreach ($metrics[3] as $metric)
541 $progressDate = self::createDateUsingMetrics(3, $metric, $array);
542 $matchParams = self::getTextForReplace($originalText, $metric, $modificators);