Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
datetimetype.php
1<?php
2
4
7use CLang;
8use CUserTypeManager;
12
13Loc::loadMessages(__FILE__);
14
20{
21 public const
22 USER_TYPE_ID = 'datetime',
23 RENDER_COMPONENT = 'bitrix:main.field.datetime';
24
28 public static function getDescription(): array
29 {
30 return [
31 'DESCRIPTION' => Loc::GetMessage('USER_TYPE_DT_DESCRIPTION'),
32 'BASE_TYPE' => CUserTypeManager::BASE_TYPE_DATETIME,
33 ];
34 }
35
39 public static function getDbColumnType(): string
40 {
41 $connection = \Bitrix\Main\Application::getConnection();
42 $helper = $connection->getSqlHelper();
43 return $helper->getColumnTypeByField(new \Bitrix\Main\ORM\Fields\DatetimeField('x'));
44 }
45
50 public static function prepareSettings(array $userField): array
51 {
52 $def = $userField['SETTINGS']['DEFAULT_VALUE'] ?? '';
53 $value = '';
54
55 if(!is_array($def))
56 {
57 $def = ['TYPE' => static::TYPE_NONE, 'VALUE' => $value];
58 }
59 elseif($def['TYPE'] === static::TYPE_FIXED)
60 {
61 $def['VALUE'] = \CDatabase::FormatDate(
62 $def['VALUE'],
63 CLang::GetDateFormat(static::FORMAT_TYPE_FULL),
64 'YYYY-MM-DD HH:MI:SS'
65 );
66 }
67 elseif($def['TYPE'] === static::TYPE_NOW)
68 {
69 $def['VALUE'] = $value;
70 }
71 else
72 {
73 $def = ['TYPE' => static::TYPE_NONE, 'VALUE' => $value];
74 }
75
76 return [
77 'DEFAULT_VALUE' => $def,
78 'USE_SECOND' => (($userField['SETTINGS']['USE_SECOND'] ?? 'Y') === 'N' ? 'N' : 'Y'),
79 'USE_TIMEZONE' => (($userField['SETTINGS']['USE_TIMEZONE'] ?? 'N') === 'Y' ? 'Y' : 'N'),
80 ];
81 }
82
88 public static function getFilterData(?array $userField, array $additionalParameters): array
89 {
90 return [
91 'id' => $additionalParameters['ID'],
92 'name' => $additionalParameters['NAME'],
93 'type' => 'date',
94 'time' => true
95 ];
96 }
97
103 public static function checkFields(array $userField, $value): array
104 {
105 $msg = [];
106 $value = (string)$value;
107 if($value !== '')
108 {
109 try
110 {
111 Type\DateTime::createFromUserTime($value);
112 } catch(Main\ObjectException $e)
113 {
114 $msg[] = [
115 'id' => $userField['FIELD_NAME'],
116 'text' => Loc::GetMessage('USER_TYPE_DT_ERROR',
117 [
118 '#FIELD_NAME#' => HtmlFilter::encode(
119 $userField['EDIT_FORM_LABEL'] <> ''
120 ? $userField['EDIT_FORM_LABEL'] : $userField['FIELD_NAME']
121 ),
122 ]
123 ),
124 ];
125 }
126 }
127 return $msg;
128 }
129
136 public static function onAfterFetch(array $userField, array $fetched): string
137 {
138 $value = $fetched['VALUE'];
139
140 if($userField['MULTIPLE'] === 'Y' && !($value instanceof Type\DateTime))
141 {
142 //Invalid value
143 if(mb_strlen($value) <= 1)
144 {
145 //will be ignored by the caller
146 return '';
147 }
148
149 try
150 {
151 //try new independent datetime format
152 $value = new Type\DateTime(
153 $value,
155 );
156 } catch(Main\ObjectException $e)
157 {
158 //try site format
159 try
160 {
161 $value = new Type\DateTime($value);
162 } catch(Main\ObjectException $e)
163 {
164 //try short format
165 $value = Type\DateTime::createFromUserTime($value);
166 }
167 }
168 }
169
170 $isFieldWithoutTimeZone = static::isFieldWithoutTimeZone($userField);
171
172 if ($isFieldWithoutTimeZone)
173 {
174 \CTimeZone::Disable();
175 }
176
177 $value = (string)$value;
178
179 if ($isFieldWithoutTimeZone)
180 {
181 \CTimeZone::Enable();
182 }
183
184 return $value;
185 }
186
187
194 public static function onBeforeSave(?array $userField, $value)
195 {
196 if($value != '' && !($value instanceof Type\DateTime))
197 {
198 $isFieldWithoutTimeZone = static::isFieldWithoutTimeZone($userField);
199
200 if ($isFieldWithoutTimeZone)
201 {
202 \CTimeZone::Disable();
203 }
204
205 $value = Type\DateTime::createFromUserTime($value);
206
207 if ($isFieldWithoutTimeZone)
208 {
209 \CTimeZone::Enable();
210 }
211 }
212
213 return $value;
214 }
215
221 public static function getFormat(string $value, array $userField): string
222 {
223 $format = CLang::GetDateFormat(static::FORMAT_TYPE_FULL);
224
225 if($userField['SETTINGS']['USE_SECOND'] === 'N' && MakeTimeStamp($value) % 60 <= 0)
226 {
227 $format = str_replace(':SS', '', $format);
228 }
229
230 return $format;
231 }
232
238 public static function formatField(?array $userField, string $fieldName): string
239 {
240 $isFieldWithoutTimeZone = static::isFieldWithoutTimeZone($userField);
241
242 if ($isFieldWithoutTimeZone)
243 {
244 \CTimeZone::Disable();
245 }
246
247 global $DB;
248 $date = $DB->dateToCharFunction($fieldName, static::FORMAT_TYPE_FULL);
249
250 if ($isFieldWithoutTimeZone)
251 {
252 \CTimeZone::Enable();
253 }
254
255 return $date;
256 }
257
262 protected static function isFieldWithoutTimeZone(?array $userField): bool
263 {
264 return (
265 isset($userField['SETTINGS']['USE_TIMEZONE'])
266 &&
267 $userField['SETTINGS']['USE_TIMEZONE']==='N'
268 &&
269 \CTimeZone::Enabled()
270 );
271 }
272
278 public static function charToDate(array $userField, Type\DateTime $dateTime): string
279 {
280 $isFieldWithoutTimeZone = static::isFieldWithoutTimeZone($userField);
281
282 if ($isFieldWithoutTimeZone)
283 {
284 \CTimeZone::Disable();
285 }
286
287 global $DB;
288 $value = $DB->CharToDateFunction($dateTime);
289
290 if ($isFieldWithoutTimeZone)
291 {
292 \CTimeZone::Enable();
293 }
294
295 return (string) $value;
296 }
297
298 public static function getFieldValue(array $userField, array $additionalParameters = [])
299 {
300 $bVarsFromForm = ($additionalParameters['bVarsFromForm'] ?? false);
301 if(!$bVarsFromForm)
302 {
303 if(
304 isset($userField['ENTITY_VALUE_ID'])
305 &&
306 $userField['ENTITY_VALUE_ID'] <= 0
307 )
308 {
309 if($userField['SETTINGS']['DEFAULT_VALUE']['TYPE'] === self::TYPE_NOW)
310 {
311 $value = \ConvertTimeStamp(
312 time() + \CTimeZone::getOffset(),
313 self::FORMAT_TYPE_FULL
314 );
315 }
316 else
317 {
318 $value = str_replace(
319 ' 00:00:00',
320 '',
321 \CDatabase::formatDate(
322 $userField['SETTINGS']['DEFAULT_VALUE']['VALUE'],
323 'YYYY-MM-DD HH:MI:SS',
324 \CLang::getDateFormat(self::FORMAT_TYPE_FULL)
325 )
326 );
327 }
328 }
329 else
330 {
331 $value = $userField['VALUE'] ?? null;
332 }
333 }
334 elseif(isset($additionalParameters['VALUE']))
335 {
336 $value = $additionalParameters['VALUE'];
337 }
338 else
339 {
340 $value = Context::getCurrent()->getRequest()->get($userField['FIELD_NAME']);
341 }
342
343 return $value;
344 }
345}
static getCurrent()
Definition context.php:241
static loadMessages($file)
Definition loc.php:64
static encode($string, $flags=ENT_COMPAT, $doubleEncode=true)
static charToDate(array $userField, Type\DateTime $dateTime)
static getFormat(string $value, array $userField)
static getFilterData(?array $userField, array $additionalParameters)
static onBeforeSave(?array $userField, $value)
static onAfterFetch(array $userField, array $fetched)
static isFieldWithoutTimeZone(?array $userField)
static getFieldValue(array $userField, array $additionalParameters=[])
static checkFields(array $userField, $value)
static formatField(?array $userField, string $fieldName)
static prepareSettings(array $userField)