1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
datetype.php
См. документацию.
1<?php
2
3namespace Bitrix\Main\UserField\Types;
4
5use Bitrix\Main;
6use Bitrix\Main\Context;
7use Bitrix\Main\Localization\Loc;
8use Bitrix\Main\Text\HtmlFilter;
9use Bitrix\Main\Type;
10use CDatabase;
11use CLang;
12use CUserTypeManager;
13
14Loc::loadMessages(__FILE__);
15
20class DateType extends BaseType
21{
22 public const
23 USER_TYPE_ID = 'date',
24 RENDER_COMPONENT = 'bitrix:main.field.date';
25
26 public const
27 TYPE_NONE = 'NONE',
28 TYPE_FIXED = 'FIXED',
29 TYPE_NOW = 'NOW';
30
31 public const
34
38 public static function getDescription(): array
39 {
40 return [
41 'DESCRIPTION' => Loc::getMessage('USER_TYPE_D_DESCRIPTION'),
42 'BASE_TYPE' => CUserTypeManager::BASE_TYPE_DATETIME,
43 ];
44 }
45
49 public static function getDbColumnType(): string
50 {
52 $helper = $connection->getSqlHelper();
53 return $helper->getColumnTypeByField(new \Bitrix\Main\ORM\Fields\DateField('x'));
54 }
55
60 public static function prepareSettings(array $userField): array
61 {
62 $def = ($userField['SETTINGS']['DEFAULT_VALUE'] ?? null);
63 $value = '';
64
65 if(!is_array($def))
66 {
67 $def = ['TYPE' => static::TYPE_NONE, 'VALUE' => $value];
68 }
69 elseif(isset($def['TYPE']) && $def['TYPE'] === static::TYPE_FIXED)
70 {
71 $dateObject = \DateTime::createFromFormat('Y-m-d', $def['VALUE']);
72 if (!$dateObject || $dateObject->format('Y-m-d') !== $def['VALUE'])
73 {
74 $def['VALUE'] = CDatabase::FormatDate(
75 $def['VALUE'],
76 CLang::GetDateFormat(static::FORMAT_TYPE_SHORT),
77 'YYYY-MM-DD'
78 );
79 }
80 }
81 elseif(isset($def['TYPE']) && $def['TYPE'] === static::TYPE_NOW)
82 {
83 $def['VALUE'] = $value;
84 }
85 else
86 {
87 $def = ['TYPE' => static::TYPE_NONE, 'VALUE' => $value];
88 }
89
90 return [
91 'DEFAULT_VALUE' => $def,
92 ];
93 }
94
100 public static function getFilterData(?array $userField, array $additionalParameters): array
101 {
102 return [
103 'id' => $additionalParameters['ID'],
104 'name' => $additionalParameters['NAME'],
105 'type' => 'date'
106 ];
107 }
108
114 public static function checkFields(array $userField, $value): array
115 {
116 $msg = [];
117 if(is_string($value) && $value !== '' && !Type\Date::isCorrect($value))
118 {
119 $msg[] = [
120 'id' => $userField['FIELD_NAME'],
121 'text' => Loc::getMessage('USER_TYPE_D_ERROR',
122 [
123 '#FIELD_NAME#' => HtmlFilter::encode(
124 $userField['EDIT_FORM_LABEL'] !== ''
125 ? $userField['EDIT_FORM_LABEL']
126 : $userField['FIELD_NAME']
127 ),
128 ]
129 ),
130 ];
131 }
132 return $msg;
133 }
134
141 public static function onAfterFetch(array $userField, array $fetched): string
142 {
143 $value = $fetched['VALUE'];
144
145 if($userField['MULTIPLE'] === 'Y' && !($value instanceof Type\Date))
146 {
147 try
148 {
149 //try new independent date format
150 $value = new Type\Date(
151 $value,
153 );
154 } catch(Main\ObjectException $e)
155 {
156 // try site format (sometimes it can be full site format)
157 try
158 {
159 $value = new Type\Date($value);
160 } catch(Main\ObjectException $e)
161 {
162 $value = new Type\Date($value, Type\DateTime::getFormat());
163 }
164 }
165 }
166
167 return (string)$value;
168 }
169
176 public static function onBeforeSave(?array $userField, $value)
177 {
178 if($value != '' && !($value instanceof Type\Date))
179 {
180 // try both site's format - short and full
181 try
182 {
183 $value = new Type\Date($value);
184 } catch(Main\ObjectException $e)
185 {
186 $value = new Type\Date($value, Type\DateTime::getFormat());
187 }
188 }
189
190 return $value;
191 }
192
198 public static function formatField(?array $userField, string $fieldName): string
199 {
200 global $DB;
201
202 return $fieldName . ', ' . $DB->DateToCharFunction($fieldName, static::FORMAT_TYPE_SHORT);
203 }
204
210 public static function getDefaultValue(array $userField, array $additionalParameters = [])
211 {
212 $userField['ENTITY_VALUE_ID'] = 0;
213 $value = static::getFieldValue($userField, $additionalParameters);
214 return ($userField['MULTIPLE'] === 'Y' ? [$value] : $value);
215 }
216
222 public static function getFieldValue(array $userField, array $additionalParameters = [])
223 {
224 $bVarsFromForm = ($additionalParameters['bVarsFromForm'] ?? false);
225 if(!$bVarsFromForm)
226 {
227 if(
228 isset($userField['ENTITY_VALUE_ID'])
229 && $userField['ENTITY_VALUE_ID'] <= 0
230 )
231 {
232 if($userField['SETTINGS']['DEFAULT_VALUE']['TYPE'] === self::TYPE_NOW)
233 {
234 $value = \ConvertTimeStamp(time(), self::FORMAT_TYPE_SHORT);
235 }
236 else
237 {
238 $value = \CDatabase::formatDate(
239 $userField['SETTINGS']['DEFAULT_VALUE']['VALUE'],
240 'YYYY-MM-DD',
241 \CLang::getDateFormat(self::FORMAT_TYPE_SHORT)
242 );
243 }
244 } else {
245 $value = $userField['VALUE'] ?? null;
246 }
247 }
248 elseif(isset($additionalParameters['VALUE']))
249 {
250 $value = $additionalParameters['VALUE'];
251 }
252 else
253 {
254 $value = Context::getCurrent()->getRequest()->get($userField['FIELD_NAME']);
255 }
256
257 return $value;
258 }
259}
$connection
Определения actionsdefinitions.php:38
static getConnection($name="")
Определения application.php:638
Определения date.php:9
static getDefaultValue(array $userField, array $additionalParameters=[])
Определения datetype.php:210
static getFilterData(?array $userField, array $additionalParameters)
Определения datetype.php:100
static onBeforeSave(?array $userField, $value)
Определения datetype.php:176
static onAfterFetch(array $userField, array $fetched)
Определения datetype.php:141
static getDescription()
Определения datetype.php:38
static getDbColumnType()
Определения datetype.php:49
static getFieldValue(array $userField, array $additionalParameters=[])
Определения datetype.php:222
static checkFields(array $userField, $value)
Определения datetype.php:114
const FORMAT_TYPE_SHORT
Определения datetype.php:33
static formatField(?array $userField, string $fieldName)
Определения datetype.php:198
static prepareSettings(array $userField)
Определения datetype.php:60
const MULTIPLE_DATE_FORMAT
Определения userfield.php:35
$bVarsFromForm
Определения file_edit.php:44
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
global $DB
Определения cron_frame.php:29
Определения collection.php:2
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
foreach($arTemplatesList as $templ) if(mb_strpos($templ["NAME"] $def
Определения template_copy.php:264