Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
field.php
1<?
2
4
8
9Loc::loadMessages(__FILE__);
10
15class Field
16{
25 public static function string($name, $defaultValue = "", $label = "", $placeholder = "")
26 {
27 $field = array(
28 "ID" => "field_".$name,
29 "TYPE" => Type::STRING,
30 "NAME" => $name,
31 "VALUE" => $defaultValue,
32 "LABEL" => $label,
33 "PLACEHOLDER" => $placeholder
34 );
35
36 return $field;
37 }
38
46 public static function textarea($name, $defaultValue = "", $label = "", $placeholder = "")
47 {
48 $field = array(
49 "ID" => "field_".$name,
50 "TYPE" => Type::TEXTAREA,
51 "NAME" => $name,
52 "VALUE" => $defaultValue,
53 "LABEL" => $label,
54 "PLACEHOLDER" => $placeholder
55 );
56
57 return $field;
58 }
59
60
70 public static function custom($name, $value, $label = "", $placeholder = "", $style = false)
71 {
72 $field = array(
73 "ID" => "field_".$name,
74 "TYPE" => Type::CUSTOM,
75 "NAME" => $name,
76 "VALUE" => HtmlFilter::encode($value),
77 "PLACEHOLDER" => $placeholder,
78 "LABEL" => $label,
79 "ENABLE_STYLE" => $style
80 );
81
82 return $field;
83 }
84
85
94 public static function customEntity($name, $label = "", $placeholder = "", $multiple = false)
95 {
96 $field = array(
97 "ID" => "field_".$name,
98 "TYPE" => Type::CUSTOM_ENTITY,
99 "NAME" => $name,
100 "LABEL" => $label,
101 "VALUES" => array(
102 "_label" => "",
103 "_value" => ""
104 ),
105 "MULTIPLE" => $multiple,
106 "PLACEHOLDER" => $placeholder
107 );
108
109 return $field;
110 }
111
112
127 public static function date(
128 $name,
129 $type = DateType::NONE,
130 $values = [],
131 $label = "",
132 $placeholder = "",
133 $enableTime = false,
134 $exclude = [],
135 $include = [],
136 $allowYearsSwithcer = false,
137 $messages = []
138 )
139 {
140 if (!is_bool($enableTime))
141 {
142 $enableTime = false;
143 }
144
145 if (!is_array($exclude))
146 {
147 $exclude = array();
148 }
149
150 $selectParams = array("isMulti" => false);
151
152 if (empty($values))
153 {
154 $values = array(
155 "_allow_year" => "",
156 "_from" => "",
157 "_to" => "",
158 "_days" => "",
159 "_month" => "",
160 "_quarter" => "",
161 "_year" => ""
162 );
163 }
164
165 $sourceMonths = range(1, 12);
166 $date = new Date();
167 $currentMonthNumber = $date->format("n");
168 $months = array();
169 $currentMonthType = array();
170
171 foreach($sourceMonths as $key => $month)
172 {
173 $month = (string)$month;
174 $months[] = array(
175 "VALUE" => $month,
176 "NAME" => static::getMessage($messages, "MAIN_UI_FILTER_FIELD_MONTH_".$month)
177 );
178
179 if ($currentMonthNumber == $month)
180 {
181 $currentMonthType = array(
182 "VALUE" => $month,
183 "NAME" => static::getMessage($messages, "MAIN_UI_FILTER_FIELD_MONTH_".$month)
184 );
185 }
186 }
187
188
189 $sourceQuarters = range(1, 4);
190 $quarters = array();
191 $quarterNumber = Quarter::getCurrent();
192 $currentQuarterType = array();
193
194 foreach($sourceQuarters as $key => $quarter)
195 {
196 $quarter = (string)$quarter;
197
198 $quarters[] = array(
199 "VALUE" => $quarter,
200 "NAME" => static::getMessage($messages, "MAIN_UI_FILTER_FIELD_QUARTER_".$quarter)
201 );
202
203 if ($quarterNumber == $quarter)
204 {
205 $currentQuarterType = array(
206 "VALUE" => $quarter,
207 "NAME" => static::getMessage($messages, "MAIN_UI_FILTER_FIELD_QUARTER_".$quarter)
208 );
209 }
210 }
211
212 $sourceSubtypes = DateType::getList();
213 $subtypes = array();
214 $subtypeType = array();
215
216 foreach ($sourceSubtypes as $key => $subtype)
217 {
218 if (!in_array($subtype, $exclude))
219 {
220 $subtypes[] = array(
221 "NAME" => static::getMessage($messages, "MAIN_UI_FILTER_FIELD_SUBTYPE_".$subtype),
222 "VALUE" => $subtype
223 );
224
225 if ($subtype == $type)
226 {
227 $subtypeType = array(
228 "NAME" => static::getMessage($messages, "MAIN_UI_FILTER_FIELD_SUBTYPE_".$subtype),
229 "VALUE" => $subtype
230 );
231 }
232 }
233 }
234
235 if (is_array($include))
236 {
237 foreach ($include as $key => $item)
238 {
239 if ($item === AdditionalDateType::CUSTOM_DATE)
240 {
241 $subtypes[] = array(
242 "NAME" => static::getMessage($messages, "MAIN_UI_FILTER_FIELD_SUBTYPE_".$item),
243 "VALUE" => AdditionalDateType::CUSTOM_DATE,
244 "DECL" => static::customDate(array("id" => $name, "name" => $label))
245 );
246 }
247
248 if ($item === AdditionalDateType::NEXT_DAY ||
249 $item === AdditionalDateType::PREV_DAY ||
250 $item === AdditionalDateType::MORE_THAN_DAYS_AGO ||
251 $item === AdditionalDateType::AFTER_DAYS)
252 {
253 $subtypes[] = array(
254 "NAME" => static::getMessage($messages, "MAIN_UI_FILTER_FIELD_SUBTYPE_".$item),
255 "VALUE" => $item
256 );
257 }
258 }
259 }
260
261 $currentYear = (int) $date->format("Y");
262 $sourceYears = range($currentYear+5, $currentYear-20);
263 $years = array();
264 $currentYearType = array();
265
266 foreach ($sourceYears as $key => $year)
267 {
268 $year = (string)$year;
269
270 $years[] = array(
271 "NAME" => $year,
272 "VALUE" => $year
273 );
274
275 if ($year == $currentYear)
276 {
277 $currentYearType = array(
278 "NAME" => $year,
279 "VALUE" => $year
280 );
281 }
282 }
283
284 $yearsSwitcher = static::select(
285 $name."_allow_year",
286 array(
287 array(
288 "NAME" => Loc::getMessage("MAIN_UI_FILTER_FIELD_SUBTYPE_CUSTOM_DATE_YEARS_SWITCHER_YES"),
289 "VALUE" => '1'
290 ),
291 array(
292 "NAME" => Loc::getMessage("MAIN_UI_FILTER_FIELD_SUBTYPE_CUSTOM_DATE_YEARS_SWITCHER_NO"),
293 "VALUE" => '0'
294 )
295 ),
296 array()
297 );
298
299 $field = array(
300 "ID" => "field_".$name,
301 "TYPE" => Type::DATE,
302 "NAME" => $name,
303 "SUB_TYPE" => $subtypeType,
304 "SUB_TYPES" => $subtypes,
305 "MONTH" => $currentMonthType,
306 "MONTHS" => $months,
307 "QUARTER" => $currentQuarterType,
308 "QUARTERS" => $quarters,
309 "YEAR" => $currentYearType,
310 "YEARS" => $years,
311 "VALUES" => $values,
312 "PLACEHOLDER" => $placeholder,
313 "LABEL" => $label,
314 "ENABLE_TIME" => $enableTime,
315 "SELECT_PARAMS" => $selectParams,
316 "YEARS_SWITCHER" => $allowYearsSwithcer ? $yearsSwitcher : null
317 );
318
319 return $field;
320 }
321
322
332 public static function number(
333 $name,
334 $type = NumberType::SINGLE,
335 $values = [],
336 $label = "",
337 $placeholder = "",
338 $exclude = [],
339 $include = [],
340 $messages = []
341 )
342 {
343 $selectParams = array("isMulti" => false);
344
345 if (empty($values))
346 {
347 $values = array(
348 "_from" => "",
349 "_to" => ""
350 );
351 }
352
353 $subtypes = [];
354 $sourceSubtypes = NumberType::getList();
355
356 foreach ($sourceSubtypes as $key => $subtype)
357 {
358 if (!is_array($exclude) || !in_array($subtype, $exclude))
359 {
360 $subtypes[] = [
361 "NAME" => static::getMessage($messages, "MAIN_UI_FILTER__NUMBER_".$key),
362 "VALUE" => $subtype,
363 ];
364
365 if ($subtype == $type)
366 {
367 $subtypeType = [
368 "NAME" => static::getMessage($messages, "MAIN_UI_FILTER__NUMBER_".$key),
369 "VALUE" => $subtype,
370 ];
371 }
372 }
373 }
374
375 if (is_array($include))
376 {
377 $additionalSubtypes = AdditionalNumberType::getList();
378 foreach ($additionalSubtypes as $key => $subtype)
379 {
380 if (in_array($subtype, $include))
381 {
382 $subtypes[] = [
383 "NAME" => static::getMessage($messages, "MAIN_UI_FILTER__NUMBER_".$key),
384 "VALUE" => $subtype,
385 ];
386 }
387 }
388 }
389
390 return [
391 "ID" => "field_".$name,
392 "TYPE" => Type::NUMBER,
393 "NAME" => $name,
394 "SUB_TYPE" => $subtypeType,
395 "SUB_TYPES" => $subtypes,
396 "VALUES" => $values,
397 "LABEL" => $label,
398 "PLACEHOLDER" => $placeholder,
399 "SELECT_PARAMS" => $selectParams
400 ];
401 }
402
403
413 public static function select($name, $items, Array $defaultValue = array(), $label = "", $placeholder = "")
414 {
415 if (empty($defaultValue) && count($items))
416 {
417 $defaultValue["NAME"] = $items[0]["NAME"];
418 $defaultValue["VALUE"] = $items[0]["VALUE"] ?? null;
419 }
420
421 $field = array(
422 "ID" => "field_".$name,
423 "TYPE" => Type::SELECT,
424 "NAME" => $name,
425 "VALUE" => $defaultValue,
426 "PLACEHOLDER" => $placeholder,
427 "LABEL" => $label,
428 "ITEMS" => $items,
429 "PARAMS" => array("isMulti" => false)
430 );
431
432 return $field;
433 }
434
435
445 public static function multiSelect($name, $items, $defaultValues = array(), $label = "", $placeholder = "")
446 {
447 $field = array(
448 "ID" => "field_".$name,
449 "TYPE" => Type::MULTI_SELECT,
450 "NAME" => $name,
451 "VALUE" => $defaultValues,
452 "PLACEHOLDER" => $placeholder,
453 "LABEL" => $label,
454 "ITEMS" => $items,
455 "PARAMS" => array("isMulti" => true)
456 );
457
458 return $field;
459 }
460
461
462 public static function customDate($options = array())
463 {
464 $defaultValues = array(
465 "days" => array(),
466 "months" => array(),
467 "years" => array()
468 );
469
470 $days = static::getDaysList();
471 $daysDate = new Date();
472 $today = (int) $daysDate->format("d");
473 $yesterday = (int) $daysDate->add("-1 days")->format("d");
474 $tomorrow = (int) $daysDate->add("2 days")->format("d");
475 $additionalDays = array(
476 array(
477 "NAME" => Loc::getMessage("MAIN_UI_FILTER_FIELD_SUBTYPE_CUSTOM_DATE_TODAY"),
478 "VALUE" => $today
479 ),
480 array(
481 "NAME" => Loc::getMessage("MAIN_UI_FILTER_FIELD_SUBTYPE_CUSTOM_DATE_YESTERDAY"),
482 "VALUE" => $yesterday
483 ),
484 array(
485 "NAME" => Loc::getMessage("MAIN_UI_FILTER_FIELD_SUBTYPE_CUSTOM_DATE_TOMORROW"),
486 "VALUE" => $tomorrow
487 ),
488 array(
489 "SEPARATOR" => true
490 )
491 );
492 $days = array_merge($additionalDays, $days);
493
494 $months = static::getMonthsList();
495 $monthsDate = new Date();
496 $currentMonth = (int) $monthsDate->format("n");
497 $lastMonth = (int) $monthsDate->add("-1 month")->format("n");
498 $nextMonth = (int) $monthsDate->add("2 month")->format("n");
499 $additionalMonths = array(
500 array(
501 "NAME" => Loc::getMessage("MAIN_UI_FILTER_FIELD_SUBTYPE_CUSTOM_DATE_CURRENT_MONTH"),
502 "VALUE" => $currentMonth
503 ),
504 array(
505 "NAME" => Loc::getMessage("MAIN_UI_FILTER_FIELD_SUBTYPE_CUSTOM_DATE_LAST_MONTH"),
506 "VALUE" => $lastMonth
507 ),
508 array(
509 "NAME" => Loc::getMessage("MAIN_UI_FILTER_FIELD_SUBTYPE_CUSTOM_DATE_NEXT_MONTH"),
510 "VALUE" => $nextMonth
511 ),
512 array(
513 "SEPARATOR" => true
514 )
515 );
516 $months = array_merge($additionalMonths, $months);
517
518 $years = static::getYearsList();
519 $yearsDate = new Date();
520 $currentYear = (int) $yearsDate->format("Y");
521 $lastYear = (int) $yearsDate->add("-1 year")->format("Y");
522 $nextYear = (int) $yearsDate->add("2 year")->format("Y");
523 $additionalYears = array(
524 array(
525 "NAME" => Loc::getMessage("MAIN_UI_FILTER_FIELD_SUBTYPE_CUSTOM_DATE_CURRENT_YEAR"),
526 "VALUE" => $currentYear
527 ),
528 array(
529 "NAME" => Loc::getMessage("MAIN_UI_FILTER_FIELD_SUBTYPE_CUSTOM_DATE_LAST_YEAR"),
530 "VALUE" => $lastYear
531 ),
532 array(
533 "NAME" => Loc::getMessage("MAIN_UI_FILTER_FIELD_SUBTYPE_CUSTOM_DATE_NEXT_YEAR"),
534 "VALUE" => $nextYear
535 ),
536 array(
537 "SEPARATOR" => true
538 )
539 );
540 $years = array_merge($additionalYears, $years);
541
542 return array(
543 "ID" => "field_".$options["id"],
544 "TYPE" => Type::CUSTOM_DATE,
545 "NAME" => $options["id"],
546 "VALUE" => $defaultValues,
547 "LABEL" => $options["name"],
548 "DAYS" => $days,
549 "MONTHS" => $months,
550 "YEARS" => $years,
551 "DAYS_PLACEHOLDER" => Loc::getMessage("MAIN_UI_FILTER_FIELD_DAYS"),
552 "MONTHS_PLACEHOLDER" => Loc::getMessage("MAIN_UI_FILTER_FIELD_MONTHS"),
553 "YEARS_PLACEHOLDER" => Loc::getMessage("MAIN_UI_FILTER_FIELD_YEARS")
554 );
555 }
556
557
562 protected static function getMonthsList()
563 {
564 $months = array();
565
566 foreach(range(1, 12) as $key => $month)
567 {
568 $months[] = array(
569 "VALUE" => $month,
570 "NAME" => (string) Loc::getMessage("MAIN_UI_FILTER_FIELD_MONTH_".$month)
571 );
572 }
573
574 return $months;
575 }
576
577
583 protected static function getYearsList()
584 {
585 $date = new Date();
586 $currentYear = (int) $date->format("Y");
587 $sourceYears = range(($currentYear+5), ($currentYear-95));
588 $years = array();
589
590 foreach ($sourceYears as $key => $year)
591 {
592 $years[] = array(
593 "NAME" => (string) $year,
594 "VALUE" => $year
595 );
596 }
597
598 return $years;
599 }
600
601
606 protected static function getDaysList()
607 {
608 $days = array();
609
610 foreach(range(1, 31) as $key => $day)
611 {
612 $days[] = array(
613 "VALUE" => $day,
614 "NAME" => (string) $day
615 );
616 }
617
618 return $days;
619 }
620
631 public static function destSelector($name, $label = "", $placeholder = "", $multiple = false, $params = array(), $lightweight = false, $filterName = '')
632 {
633 \CJSCore::init(array('socnetlogdest'));
634
635 global $APPLICATION;
636
637 $field = array(
638 "ID" => "field_".$name.($filterName <> '' ? '_'.$filterName : ''),
639 "TYPE" => Type::DEST_SELECTOR,
640 "NAME" => $name,
641 "LABEL" => $label,
642 "VALUES" => array(
643 "_label" => "",
644 "_value" => ""
645 ),
646 "MULTIPLE" => $multiple,
647 "PLACEHOLDER" => $placeholder
648 );
649
650 if (!$lightweight)
651 {
652 ob_start();
653 $optionsList = array(
654 'multiple' => ($multiple ? 'Y' : 'N'),
655 'eventInit' => 'BX.Filter.DestinationSelector:openInit',
656 'eventOpen' => 'BX.Filter.DestinationSelector:open',
657 'context' => ($params['context'] ?? 'FILTER_'.$name),
658 'popupAutoHide' => 'N',
659 'useSearch' => 'N',
660 'userNameTemplate' => \CUtil::jSEscape(\CSite::getNameFormat()),
661 'useClientDatabase' => (isset($params['useClientDatabase']) && $params['useClientDatabase'] == 'N' ? 'N' : 'Y'),
662 'enableLast' => 'Y',
663 'enableUsers' => (!isset($params['enableUsers']) || $params['enableUsers'] != 'N' ? 'Y' : 'N'),
664 'enableDepartments' => (!isset($params['enableDepartments']) || $params['enableDepartments'] != 'N' ? 'Y' : 'N'),
665 'allowAddUser' => 'N',
666 'allowAddCrmContact' => 'N',
667 'allowAddSocNetGroup' => 'N',
668 'allowSearchCrmEmailUsers' => 'N',
669 'allowSearchNetworkUsers' => 'N',
670 'useNewCallback' => 'Y',
671 'focusInputOnSelectItem' => 'N',
672 'focusInputOnSwitchTab' => 'N',
673 'landing' => (isset($params['landing']) && $params['landing'] == 'Y' ? 'Y' : 'N'),
674 );
675
676 if (!empty($params['contextCode']))
677 {
678 $optionsList['contextCode'] = $params['contextCode'];
679 unset($params['contextCode']);
680 }
681
682 if (isset($params['context']))
683 {
684 unset($params['context']);
685 }
686 if (isset($params['enableUsers']))
687 {
688 unset($params['enableUsers']);
689 }
690 if (isset($params['enableDepartments']))
691 {
692 unset($params['enableDepartments']);
693 }
694
695 $optionsList = array_merge($optionsList, $params);
696
697 $APPLICATION->includeComponent(
698 "bitrix:main.ui.selector",
699 ".default",
700 array(
701 'API_VERSION' => (!empty($params['apiVersion']) && intval($params['apiVersion']) >= 2 ? intval($params['apiVersion']) : 2),
702 'ID' => $name,
703 'ITEMS_SELECTED' => array(),
704 'CALLBACK' => array(
705 'select' => 'BX.Filter.DestinationSelectorManager.onSelect.bind(null, \''.(isset($params['isNumeric']) && $params['isNumeric'] == 'Y' ? 'Y' : 'N').'\', \''.($params['prefix'] ?? '').'\')',
706 'unSelect' => '',
707 'openDialog' => 'BX.Filter.DestinationSelectorManager.onDialogOpen',
708 'closeDialog' => 'BX.Filter.DestinationSelectorManager.onDialogClose',
709 'openSearch' => '',
710 'closeSearch' => 'BX.Filter.DestinationSelectorManager.onDialogClose',
711 ),
712 'OPTIONS' => $optionsList,
713 'LOAD_JS' => true
714 ),
715 false,
716 array("HIDE_ICONS" => "Y")
717 );
718
719 $field["HTML"] = ob_get_clean();
720 }
721
722 return $field;
723 }
724
725 public static function entitySelector(
726 string $name,
727 string $label = '',
728 string $placeholder = '',
729 array $params = [],
730 string $filterName = ''
731 )
732 {
733 $multiple = $params['multiple'] ?? false;
734 $addEntityIdToResult = $params['addEntityIdToResult'] ?? false;
735 $showDialogOnEmptyInput = $params['showDialogOnEmptyInput'] ?? true;
736 $dialogOptions = $params['dialogOptions'] ?? [];
737 $field = [
738 'ID' => 'field_' . $name . ($filterName != '' ? '_' . $filterName : ''),
739 'TYPE' => Type::ENTITY_SELECTOR,
740 'NAME' => $name,
741 'LABEL' => $label,
742 'VALUES' => [
743 '_label' => '',
744 '_value' => '',
745 ],
746 'MULTIPLE' => $multiple,
747 'PLACEHOLDER' => $placeholder,
748 'DIALOG_OPTIONS' => $dialogOptions,
749 'ADD_ENTITY_ID_TO_RESULT' => $addEntityIdToResult,
750 'SHOW_DIALOG_ON_EMPTY_INPUT' => $showDialogOnEmptyInput,
751 ];
752
753 return $field;
754 }
755
756 protected static function getMessage($messages, $messageId)
757 {
758 if (is_array($messages) && array_key_exists($messageId, $messages))
759 {
760 return $messages[$messageId];
761 }
762
763 return Loc::getMessage($messageId);
764 }
765}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static encode($string, $flags=ENT_COMPAT, $doubleEncode=true)
static select($name, $items, Array $defaultValue=array(), $label="", $placeholder="")
Definition field.php:413
static date( $name, $type=DateType::NONE, $values=[], $label="", $placeholder="", $enableTime=false, $exclude=[], $include=[], $allowYearsSwithcer=false, $messages=[])
Definition field.php:127
static multiSelect($name, $items, $defaultValues=array(), $label="", $placeholder="")
Definition field.php:445
static customEntity($name, $label="", $placeholder="", $multiple=false)
Definition field.php:94
static destSelector($name, $label="", $placeholder="", $multiple=false, $params=array(), $lightweight=false, $filterName='')
Definition field.php:631
static custom($name, $value, $label="", $placeholder="", $style=false)
Definition field.php:70
static textarea($name, $defaultValue="", $label="", $placeholder="")
Definition field.php:46
static number( $name, $type=NumberType::SINGLE, $values=[], $label="", $placeholder="", $exclude=[], $include=[], $messages=[])
Definition field.php:332
static string($name, $defaultValue="", $label="", $placeholder="")
Definition field.php:25
static customDate($options=array())
Definition field.php:462