Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
time.php
1<?php
2
4
8
9class Time extends Base
10{
11 public static function getType()
12 {
13 return FieldType::TIME;
14 }
15
16 public static function internalizeValue(FieldType $fieldType, $context, $value)
17 {
18 if (\CBPActivity::isExpression($value))
19 {
20 return $value;
21 }
22
23 if (is_string($value) && Value\Time::isCorrect($value))
24 {
25 return new Value\Time($value, \CTimeZone::GetOffset());
26 }
27
28 return null;
29 }
30
31 public static function getFormats()
32 {
33 $formats = [
34 'server' => [
35 'callable' => 'formatValueServer',
36 'separator' => ', ',
37 ],
38 'responsible' => [
39 'callable' => 'formatValueResponsible',
40 'separator' => ', ',
41 ],
42 ];
43 $formats['author'] = $formats['responsible'];
44
45 return array_merge(parent::getFormats(), $formats);
46 }
47
48 protected static function formatValueServer(FieldType $fieldType, $value)
49 {
50 if ($value instanceof Value\Time)
51 {
52 return $value->toServerTime()->format(Value\Time::getFormat());
53 }
54
55 return $value;
56 }
57
58 protected static function formatValueResponsible(FieldType $fieldType, $value)
59 {
60 if ($value instanceof Value\Time)
61 {
62 $offset = static::getResponsibleOffset($fieldType);
63
64 return $value->toUserTime($offset)->format(Value\Time::getFormat());
65 }
66
67 return $value;
68 }
69
70 protected static function extractValue(FieldType $fieldType, array $field, array $request)
71 {
72 $value = parent::extractValue($fieldType, $field, $request);
73 if (is_string($value) && !\CBPHelper::isEmptyValue($value))
74 {
75 if (\CBPActivity::isExpression($value))
76 {
77 return $value;
78 }
79
80 if (Value\Time::isCorrect($value))
81 {
82 $value = new Value\Time($value, \CTimeZone::GetOffset());
83
84 return $value->serialize();
85 }
86
87 static::addError([
88 'code' => 'ErrorValue',
89 'message' => Loc::getMessage('BPDT_TIME_INVALID'),
90 ]);
91 }
92
93 return null;
94 }
95
96 public static function compareValues($valueA, $valueB)
97 {
98 $offset = \CTimeZone::GetOffset();
99 $timestampA = (new Value\Time((string)$valueA, $offset))->getTimestamp();
100 $timestampB = (new Value\Time((string)$valueB, $offset))->getTimestamp();
101
102 return parent::compareValues($timestampA, $timestampB);
103 }
104
105 protected static function renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
106 {
107 $name = static::generateControlName($field);
108 $value = static::internalizeValue($fieldType, 'Renderer', $value);
109 if ($value instanceof Value\Time)
110 {
111 $value = $value->toSystemObject()->format(Value\Time::getRenderFormat());
112 }
113 $className = static::generateControlClassName($fieldType, $field);
114
115 $renderResult = '<select name="' . htmlspecialcharsbx($name) . '" class="' . htmlspecialcharsbx($className) . '">';
116 $renderResult .= '<option value="">' . Loc::getMessage('BPDT_TIME_NOT_SET') . '</option>';
117 $format = Value\Time::getFormat();
118 for ($hour = 0; $hour < 24; $hour++)
119 {
120 $time = (new \Bitrix\Main\Type\DateTime())->setTime($hour, 0);
121
122 $selected =
123 ($value === $time->format(Value\Time::getRenderFormat()))
124 ? ' selected'
125 : ''
126 ;
127 $timeValue = htmlspecialcharsbx($time->format(Value\Time::getRenderFormat()));
128 $timeText = htmlspecialcharsbx($time->format($format));
129 $renderResult .= '<option value="' . $timeValue . '"' . $selected . '>' . $timeText . '</option>';
130
131 $time = (new \Bitrix\Main\Type\DateTime())->setTime($hour, 30);
132 $selected =
133 ($value === $time->format(Value\Time::getRenderFormat()))
134 ? ' selected'
135 : ''
136 ;
137 $timeValue = htmlspecialcharsbx($time->format(Value\Time::getRenderFormat()));
138 $timeText = htmlspecialcharsbx($time->format($format));
139 $renderResult .= '<option value="' . $timeValue . '"' . $selected . '>' . $timeText . '</option>';
140
141 }
142 $renderResult .= '</select>';
143
144 return $renderResult;
145 }
146
147 protected static function getResponsibleOffset(FieldType $fieldType): int
148 {
149 $documentId = $fieldType->getDocumentId();
150 $userId = $documentId ? \CBPHelper::extractFirstUser(['author', 'responsible'], $documentId) : null;
151
152 return $userId ? \CTimeZone::GetOffset($userId, true) : 0;
153 }
154
155 public static function convertTo(FieldType $fieldType, $value, $toTypeClass)
156 {
157 $isCorrectValue = (
158 $value instanceof Value\Time
159 || Value\Time::isCorrect((string)$value)
160 );
161
163 $type = $toTypeClass::getType();
164
165 switch ($type)
166 {
167 case FieldType::BOOL:
168 $result = $isCorrectValue;
169 break;
170 case FieldType::DATE:
172 $culture = Application::getInstance()->getContext()->getCulture();
173 $dateFormat =
174 \Bitrix\Main\Type\DateTime::convertFormatToPhp($culture?->getDateFormat() ?? 'DD.MM.YYYY')
175 ;
176 $dateTimeFormat =
177 \Bitrix\Main\Type\DateTime::convertFormatToPhp($culture?->getDateTimeFormat() ?? 'DD.MM.YYYY HH:MI:SS')
178 ;
179
180 $currentDate = new \Bitrix\Main\Type\DateTime();
181 $date =
182 $isCorrectValue
183 ? (new Value\Time((string)$value, \CTimeZone::GetOffset()))->toSystemObject()
184 : new \Bitrix\Main\Type\DateTime()
185 ;
186 $date->setDate($currentDate->format('Y'), $currentDate->format('m'), $currentDate->format('d'));
187
188 $result = $date->format($type === FieldType::DATE ? $dateFormat : $dateTimeFormat);
189
190 break;
192 case FieldType::INT:
193 $result =
194 $isCorrectValue
195 ? (new \Bitrix\Bizproc\BaseType\Value\Time((string)$value, \CTimeZone::GetOffset()))->getTimestamp()
196 : 0
197 ;
198 break;
200 case FieldType::TEXT:
201 case FieldType::TIME:
202 $result =
203 $isCorrectValue
204 ? (string)(new \Bitrix\Bizproc\BaseType\Value\Time((string)$value, \CTimeZone::GetOffset()))
205 : ''
206 ;
207
208 break;
209 default:
210 $result = null;
211 }
212
213 return $result;
214 }
215}
static convertTo(FieldType $fieldType, $value, $toTypeClass)
Definition base.php:209
static renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Definition time.php:105
static extractValue(FieldType $fieldType, array $field, array $request)
Definition time.php:70
static internalizeValue(FieldType $fieldType, $context, $value)
Definition time.php:16
static getResponsibleOffset(FieldType $fieldType)
Definition time.php:147
static formatValueResponsible(FieldType $fieldType, $value)
Definition time.php:58
static compareValues($valueA, $valueB)
Definition time.php:96
static formatValueServer(FieldType $fieldType, $value)
Definition time.php:48
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
setDate($year, $month, $day)
Definition date.php:166