Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
datetype.php
1<?php
2
4
6use CUserTypeManager;
9use CLang;
10use CDatabase;
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 {
51 $connection = \Bitrix\Main\Application::getConnection();
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'];
63 $value = '';
64
65 if(!is_array($def))
66 {
67 $def = ['TYPE' => static::TYPE_NONE, 'VALUE' => $value];
68 }
69 elseif($def['TYPE'] === static::TYPE_FIXED)
70 {
71 $def['VALUE'] = CDatabase::FormatDate(
72 $def['VALUE'],
73 CLang::GetDateFormat(static::FORMAT_TYPE_SHORT),
74 'YYYY-MM-DD'
75 );
76 }
77 elseif($def['TYPE'] === static::TYPE_NOW)
78 {
79 $def['VALUE'] = $value;
80 }
81 else
82 {
83 $def = ['TYPE' => static::TYPE_NONE, 'VALUE' => $value];
84 }
85
86 return [
87 'DEFAULT_VALUE' => $def,
88 ];
89 }
90
96 public static function getFilterData(?array $userField, array $additionalParameters): array
97 {
98 return [
99 'id' => $additionalParameters['ID'],
100 'name' => $additionalParameters['NAME'],
101 'type' => 'date'
102 ];
103 }
104
110 public static function checkFields(array $userField, $value): array
111 {
112 $msg = [];
113 if(is_string($value) && $value !== '' && !Type\Date::isCorrect($value))
114 {
115 $msg[] = [
116 'id' => $userField['FIELD_NAME'],
117 'text' => Loc::getMessage('USER_TYPE_D_ERROR',
118 [
119 '#FIELD_NAME#' => HtmlFilter::encode(
120 $userField['EDIT_FORM_LABEL'] !== ''
121 ? $userField['EDIT_FORM_LABEL']
122 : $userField['FIELD_NAME']
123 ),
124 ]
125 ),
126 ];
127 }
128 return $msg;
129 }
130
137 public static function onAfterFetch(array $userField, array $fetched): string
138 {
139 $value = $fetched['VALUE'];
140
141 if($userField['MULTIPLE'] === 'Y' && !($value instanceof Type\Date))
142 {
143 try
144 {
145 //try new independent date format
146 $value = new Type\Date(
147 $value,
149 );
150 } catch(Main\ObjectException $e)
151 {
152 // try site format (sometimes it can be full site format)
153 try
154 {
155 $value = new Type\Date($value);
156 } catch(Main\ObjectException $e)
157 {
158 $value = new Type\Date($value, Type\DateTime::getFormat());
159 }
160 }
161 }
162
163 return (string)$value;
164 }
165
172 public static function onBeforeSave(?array $userField, $value)
173 {
174 if($value != '' && !($value instanceof Type\Date))
175 {
176 // try both site's format - short and full
177 try
178 {
179 $value = new Type\Date($value);
180 } catch(Main\ObjectException $e)
181 {
182 $value = new Type\Date($value, Type\DateTime::getFormat());
183 }
184 }
185
186 return $value;
187 }
188
194 public static function formatField(?array $userField, string $fieldName): string
195 {
196 global $DB;
197 return $DB->DateToCharFunction($fieldName, static::FORMAT_TYPE_SHORT);
198 }
199
203 public static function getDefaultValue(array $userField, array $additionalParameters = [])
204 {
205 $userField['ENTITY_VALUE_ID'] = 0;
206 $value = static::getFieldValue($userField, $additionalParameters);
207 return ($userField['MULTIPLE'] === 'Y' ? [$value] : $value);
208 }
209
214 public static function getFieldValue(array $userField, array $additionalParameters = [])
215 {
216 $bVarsFromForm = ($additionalParameters['bVarsFromForm'] ?? false);
217 if(!$bVarsFromForm)
218 {
219 if(
220 isset($userField['ENTITY_VALUE_ID'])
221 && $userField['ENTITY_VALUE_ID'] <= 0
222 )
223 {
224 if($userField['SETTINGS']['DEFAULT_VALUE']['TYPE'] === self::TYPE_NOW)
225 {
226 $value = \ConvertTimeStamp(time(), self::FORMAT_TYPE_SHORT);
227 }
228 else
229 {
230 $value = \CDatabase::formatDate(
231 $userField['SETTINGS']['DEFAULT_VALUE']['VALUE'],
232 'YYYY-MM-DD',
233 \CLang::getDateFormat(self::FORMAT_TYPE_SHORT)
234 );
235 }
236 } else {
237 $value = $userField['VALUE'] ?? null;
238 }
239 }
240 elseif(isset($additionalParameters['VALUE']))
241 {
242 $value = $additionalParameters['VALUE'];
243 }
244 else
245 {
246 $value = Context::getCurrent()->getRequest()->get($userField['FIELD_NAME']);
247 }
248
249 return $value;
250 }
251}
static getCurrent()
Definition context.php:241
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 getFormat(Context\Culture $culture=null)
Definition date.php:261
static getDefaultValue(array $userField, array $additionalParameters=[])
Definition datetype.php:203
static getFilterData(?array $userField, array $additionalParameters)
Definition datetype.php:96
static onBeforeSave(?array $userField, $value)
Definition datetype.php:172
static onAfterFetch(array $userField, array $fetched)
Definition datetype.php:137
static getFieldValue(array $userField, array $additionalParameters=[])
Definition datetype.php:214
static checkFields(array $userField, $value)
Definition datetype.php:110
static formatField(?array $userField, string $fieldName)
Definition datetype.php:194
static prepareSettings(array $userField)
Definition datetype.php:60