1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
date.php
См. документацию.
1<?php
2namespace Bitrix\Bizproc\BaseType;
3
4use Bitrix\Main;
5use Bitrix\Main\Type;
6use Bitrix\Main\Localization\Loc;
7use Bitrix\Bizproc\FieldType;
8
9Loc::loadMessages(__FILE__);
10
15class Date extends Base
16{
20 public static function getType()
21 {
22 return FieldType::DATE;
23 }
24
32 public static function toSingleValue(FieldType $fieldType, $value)
33 {
34 if (is_array($value))
35 {
36 reset($value);
37 $value = current($value);
38 }
39 return $value;
40 }
41
48 public static function convertTo(FieldType $fieldType, $value, $toTypeClass)
49 {
51 $type = $toTypeClass::getType();
52 switch ($type)
53 {
55 case FieldType::INT:
56 $value = $value? (int)strtotime($value) : 0;
57 break;
58 case FieldType::DATE:
59 if ($value instanceof Value\Date)
60 {
61 //cut time converting to main date
62 $dateTs = Type\Date::createFromTimestamp($value->toSystemObject()->getTimestamp());
63 $value = new Value\Date($dateTs->getTimestamp() - $value->getOffset(), $value->getOffset());
64
65 break;
66 }
68 if ($value instanceof Value\Date)
69 {
70 $value = new Value\DateTime($value->getTimestamp(), $value->getOffset());
71
72 break;
73 }
74 case FieldType::STRING:
75 case FieldType::TEXT:
76 $value = (string) $value;
77 if ($value)
78 {
79 if ($type == FieldType::DATE)
80 $format = \FORMAT_DATE;
81 elseif ($type == FieldType::DATETIME)
82 $format = \FORMAT_DATETIME;
83 else
84 $format = static::getType() == FieldType::DATE ? \FORMAT_DATE : \FORMAT_DATETIME;
85
86 if (\CheckDateTime($value, $format))
87 {
88 $value = date(Type\Date::convertFormatToPhp($format), \MakeTimeStamp($value, $format));
89 }
90 else
91 {
92 $value = date(Type\Date::convertFormatToPhp($format), strtotime($value));
93 }
94 }
95 break;
96 default:
97 $value = null;
98 }
99
100 return $value;
101 }
102
107 public static function getConversionMap()
108 {
109 return array(
110 array(
117 )
118 );
119 }
120
129 protected static function renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
130 {
131 $name = static::generateControlName($field);
132 $value = static::internalizeValue($fieldType, 'Renderer', $value);
133 $offset = ($value instanceof Value\Date) ? $value->getOffset() : 0;
134
135 $className = static::generateControlClassName($fieldType, $field);
136 $renderResult = '';
137 $isPublicControl = $renderMode & FieldType::RENDER_MODE_PUBLIC;
138
139 if ($isPublicControl && $allowSelection)
140 {
141 $selectorAttributes = sprintf(
142 'data-role="inline-selector-target" data-selector-type="%s" data-property="%s" ',
143 htmlspecialcharsbx($fieldType->getType()),
144 htmlspecialcharsbx(Main\Web\Json::encode($fieldType->getProperty()))
145 );
146
147 $renderResult = sprintf(
148 '<input name="%s" type="text" class="%s" value="%s" placeholder="%s" %s/>',
150 htmlspecialcharsbx($className),
152 htmlspecialcharsbx($fieldType->getDescription()),
153 $selectorAttributes
154 );
155 }
157 {
158 $renderResult = '<div><input type="hidden" value="'
159 .htmlspecialcharsbx($value).'" data-type="'
160 .htmlspecialcharsbx(static::getType()).'" name="'.htmlspecialcharsbx($name).'"/>'
161 .'<a href="#" onclick="return BX.BizProcMobile.showDatePicker(this, event);">'
162 .($value? htmlspecialcharsbx($value) : Loc::getMessage('BPDT_DATE_MOBILE_SELECT')).'</a></div>';
163 }
164 else
165 {
166 \CJSCore::Init(['popup', 'date']);
167 $renderResult = sprintf(
168 '<input type="text" name="%s" value="%s" class="%s"/>'
169 . '<img src="/bitrix/js/main/core/images/calendar-icon.gif" alt="calendar" class="calendar-icon" '
170 . 'onclick="BX.calendar({node:this, field: this.previousSibling, bTime: %s, bHideTime: %s});" '
171 . 'onmouseover="BX.addClass(this, \'calendar-icon-hover\');" '
172 . 'onmouseout="BX.removeClass(this, \'calendar-icon-hover\');" border="0"/>',
175 $isPublicControl ? htmlspecialcharsbx($className) : '',
176 static::getType() == FieldType::DATETIME ? 'true' : 'false',
177 static::getType() == FieldType::DATETIME ? 'false' : 'true'
178 );
179
180 $tzName = 'tz_'.$name;
181 $zones = self::getZones();
182
183 if (!$offset && $renderMode & FieldType::RENDER_MODE_PUBLIC)
184 {
185 $offset = 'current';
186 }
187
188 $tzClassName = 'bizproc-type-control-date-lc';
189 if ($fieldType->isMultiple())
190 {
191 $tzClassName .= ' bizproc-type-control-date-lc-multiple';
192 }
193 if (!$isPublicControl)
194 {
195 $tzClassName = '';
196 }
197
198 $renderResult .= '<select name="'.htmlspecialcharsbx($tzName).'" class="'.$tzClassName.'">';
199 foreach ($zones as $zone)
200 {
201 $selected = ($offset && $offset === $zone['offset']) ? 'selected' : '';
202 $renderResult .= '<option value="'.htmlspecialcharsbx($zone['value']).'" '.$selected.'>'
203 .htmlspecialcharsbx($zone['text']).'</option>';
204 }
205 $renderResult .= '</select>';
206
207 if ($fieldType->isMultiple())
208 {
209 $settings = $fieldType->getSettings();
210 $settings['timezones'] = $zones;
211 $fieldType->setSettings($settings);
212 }
213 }
214
215 return $renderResult;
216 }
217
226 public static function renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
227 {
228 $allowSelectionOrig = $allowSelection;
229 if ($renderMode & FieldType::RENDER_MODE_PUBLIC)
230 {
231 $allowSelection = false;
232 }
233
234 $value = static::toSingleValue($fieldType, $value);
235 $selectorValue = null;
236
237 if ($allowSelection && \CBPActivity::isExpression($value))
238 {
239 $selectorValue = $value;
240 $value = null;
241 }
242
243 $renderResult = static::renderControl($fieldType, $field, $value, $allowSelectionOrig, $renderMode);
244
245 if ($allowSelection)
246 {
247 $renderResult .= static::renderControlSelector($field, $selectorValue, true, '', $fieldType);
248 }
249
250 return $renderResult;
251 }
252
253 public static function renderControlMultiple(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
254 {
255 $allowSelectionOrig = $allowSelection;
256 if ($renderMode & FieldType::RENDER_MODE_PUBLIC)
257 {
258 $allowSelection = false;
259 }
260
261 if (!is_array($value) || is_array($value) && \CBPHelper::isAssociativeArray($value))
262 {
264 }
265
266 $selectorValue = null;
267 if ($allowSelection)
268 {
269 foreach ($value as $k => $v)
270 {
272 {
273 $selectorValue = $v;
274 unset($value[$k]);
275 }
276 }
277 $value = array_values($value);
278 }
279
280 if (empty($value))
281 {
282 $value[] = null;
283 }
284
285 $controls = [];
286
287 foreach ($value as $k => $v)
288 {
289 $singleField = $field;
290 $singleField['Index'] = $k;
291 $controls[] = static::renderControl(
292 $fieldType,
293 $singleField,
294 $v,
295 $allowSelectionOrig,
296 $renderMode
297 );
298 }
299
300 if ($renderMode & FieldType::RENDER_MODE_PUBLIC)
301 {
302 $renderResult = static::renderPublicMultipleWrapper($fieldType, $field, $controls);
303 }
304 else
305 {
306 $renderResult = static::wrapCloneableControls($controls, static::generateControlName($field));
307 }
308
309 if ($allowSelection)
310 {
311 $renderResult .= static::renderControlSelector($field, $selectorValue, true, '', $fieldType);
312 }
313
314 return $renderResult;
315 }
316
317
322 public static function canRenderControl($renderMode)
323 {
324 return true;
325 }
326
333 protected static function extractValue(FieldType $fieldType, array $field, array $request)
334 {
335 $value = parent::extractValue($fieldType, $field, $request);
336
337 if ($value !== null && is_string($value) && $value <> '')
338 {
340 return $value;
341
342 $format = static::getType() == FieldType::DATETIME ? \FORMAT_DATETIME : \FORMAT_DATE;
343 if(!\CheckDateTime($value, $format))
344 {
345 $value = null;
346 static::addError(array(
347 'code' => 'ErrorValue',
348 'message' => Loc::getMessage('BPDT_DATE_INVALID'),
349 'parameter' => static::generateControlName($field),
350 ));
351 }
352 else
353 {
354 $tzOffset = self::extractOffset($field, $request);
355 $value = (static::getType() == FieldType::DATETIME) ?
356 new Value\DateTime($value, $tzOffset) : new Value\Date($value, $tzOffset);
357
358 //have to serialize in design time.
359 $value = $value->serialize();
360 }
361 }
362 else
363 {
364 $value = null;
365 }
366
367 return $value;
368 }
369
370 private static function extractOffset(array $field, array $request)
371 {
372 $tzName = 'tz_'.$field['Field'];
373 $tz = isset($request[$tzName]) ? $request[$tzName] : null;
374 if (is_array($tz))
375 {
376 $tz = isset($field['Index']) ? $tz[$field['Index']] : $tz[0];
377 }
378
379 if ($tz === 'current')
380 {
381 return \CTimeZone::GetOffset();
382 }
383 elseif ($tz)
384 {
385 $localTime = new \DateTime();
386 $localOffset = $localTime->getOffset();
387
388 $userTime = new \DateTime(null, new \DateTimeZone($tz));
389 $userOffset = $userTime->getOffset();
390
391 return $userOffset - $localOffset;
392 }
393
394 return 0;
395 }
396
401 public static function getFormats()
402 {
403 $formats = parent::getFormats();
404 $formats['server'] = [
405 'callable' => 'formatValueServer',
406 'separator' => ', ',
407 ];
408
409 $formats['author'] = $formats['responsible'] = [
410 'callable' => 'formatValueAuthor',
411 'separator' => ', ',
412 ];
413
414 return $formats;
415 }
416
422 protected static function formatValueServer(FieldType $fieldType, $value)
423 {
424 if ($value instanceof Value\Date)
425 {
426 return date($value->getFormat(), $value->getTimestamp());
427 }
428
429 return $value;
430 }
431
437 protected static function formatValueAuthor(FieldType $fieldType, $value)
438 {
439 if ($value instanceof Value\Date)
440 {
441 $documentId = $fieldType->getDocumentId();
442
443 if ($documentId)
444 {
445 $userId = \CBPHelper::ExtractUsers(['author', 'responsible'], $documentId, true);
446 $offset = $userId ? \CTimeZone::GetOffset($userId, true) : 0;
447
448 $value = new Value\DateTime($value->getTimestamp(), $offset);
449 }
450
451 return (string) $value;
452 }
453
454 return $value;
455 }
456
457 public static function internalizeValue(FieldType $fieldType, $context, $value)
458 {
459 if ($value && is_string($value))
460 {
461 $offset = \CTimeZone::GetOffset();
462 try
463 {
464 $obj = (static::getType() === FieldType::DATE)
465 ? new Value\Date($value, $offset)
466 : new Value\DateTime($value, $offset);
467 //set value if everything is ok
468 if ($obj->getTimestamp() !== null)
469 {
470 $value = $obj;
471 }
472 }
473 catch(Main\ObjectException $e)
474 {
475 }
476 }
477 else if ($value instanceof Type\Date)
478 {
479 return (static::getType() === FieldType::DATE)
481 : Value\DateTime::fromSystemObject($value);
482 }
483
484 return $value;
485 }
486
487 public static function externalizeValue(FieldType $fieldType, $context, $value)
488 {
490 {
491 return \CBPHelper::makeTimestamp($value) ?: null;
492 }
493
494 //serialized date string
495 if (is_string($value) && preg_match('#(.+)\s\[([0-9\-]+)\]#', $value))
496 {
497 $value = static::internalizeValue($fieldType, $context, $value);
498 }
499
500 if ($value instanceof Value\Date)
501 {
502 return $context === FieldType::VALUE_CONTEXT_REST ? $value->toSystemObject()->format('c') : (string) $value->toSystemObject();
503 }
504
505 if (is_string($value) && $context === FieldType::VALUE_CONTEXT_REST)
506 {
507 return date('c', strtotime($value));
508 }
509
510 return $value;
511 }
512
513 private static function getZones()
514 {
515 $serverOffset = (new \DateTime())->getOffset();
516
517 $timezones = [];
518 $exclude = ["Etc/", "GMT", "UTC", "UCT", "HST", "PST", "MST", "CST", "EST", "CET", "MET", "WET", "EET", "PRC", "ROC", "ROK", "W-SU"];
519 foreach (\DateTimeZone::listIdentifiers() as $tz)
520 {
521 foreach ($exclude as $ex)
522 if (mb_strpos($tz, $ex) === 0)
523 continue 2;
524 try
525 {
526 $dateTimeZone = new \DateTimeZone($tz);
527 $timezones[$tz] = ['timezone_id' => $tz, 'offset' => $dateTimeZone->getOffset(new \DateTime("now", $dateTimeZone))];
528 } catch (\Exception $e)
529 {
530 }
531 }
532
533 uasort($timezones, function ($a, $b)
534 {
535 if ($a['offset'] == $b['offset'])
536 return strcmp($a['timezone_id'], $b['timezone_id']);
537
538 return ($a['offset'] < $b['offset'] ? -1 : 1);
539 });
540
541 $result = [
542 ['value' => '', 'text' => Loc::getMessage('BPDT_DATE_SERVER_TZ'), 'offset' => 0],
543 ['value' => 'current', 'text' => Loc::getMessage('BPDT_DATE_CURRENT_TZ'), 'offset' => 'current']
544 ];
545 foreach ($timezones as $z)
546 {
547 $result[] = [
548 'value' => $z['timezone_id'],
549 'text' => '(UTC'.($z['offset'] <> 0 ? ' '.($z['offset'] < 0 ? '-' : '+').sprintf("%02d", ($h = floor(abs($z['offset']) / 3600))).':'.sprintf("%02d", abs($z['offset']) / 60 - $h * 60) : '').') '.$z['timezone_id'],
550 'offset' => $z['offset'] - $serverOffset
551 ];
552 }
553
554 return $result;
555 }
556
557 public static function compareValues($valueA, $valueB)
558 {
559 $valueA = \CBPHelper::makeTimestamp($valueA, true);
560 $valueB = \CBPHelper::makeTimestamp($valueB, true);
561
562 //cut time
563 $valueA = Type\Date::createFromTimestamp($valueA)->getTimestamp();
564 $valueB = Type\Date::createFromTimestamp($valueB)->getTimestamp();
565
566 return parent::compareValues($valueA, $valueB);
567 }
568}
$type
Определения options.php:106
if(!Loader::includeModule('catalog')) if(!AccessController::getCurrent() ->check(ActionDictionary::ACTION_PRICE_EDIT)) if(!check_bitrix_sessid()) $request
Определения catalog_reindex.php:36
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static $formats
Определения base.php:33
static formatValueAuthor(FieldType $fieldType, $value)
Определения date.php:437
static renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Определения date.php:129
static getType()
Определения date.php:20
static getFormats()
Определения date.php:401
static extractValue(FieldType $fieldType, array $field, array $request)
Определения date.php:333
static externalizeValue(FieldType $fieldType, $context, $value)
Определения date.php:487
static renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Определения date.php:226
static internalizeValue(FieldType $fieldType, $context, $value)
Определения date.php:457
static toSingleValue(FieldType $fieldType, $value)
Определения date.php:32
static renderControlMultiple(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Определения date.php:253
static getConversionMap()
Определения date.php:107
static compareValues($valueA, $valueB)
Определения date.php:557
static canRenderControl($renderMode)
Определения date.php:322
static formatValueServer(FieldType $fieldType, $value)
Определения date.php:422
static fromSystemObject(Main\Type\Date $date)
Определения date.php:72
const DATE
Определения fieldtype.php:22
getDescription()
Определения fieldtype.php:295
getDocumentId()
Определения fieldtype.php:202
const RENDER_MODE_MOBILE
Определения fieldtype.php:87
const STRING
Определения fieldtype.php:57
const VALUE_CONTEXT_REST
Определения fieldtype.php:100
const TEXT
Определения fieldtype.php:62
const RENDER_MODE_PUBLIC
Определения fieldtype.php:92
const DATETIME
Определения fieldtype.php:27
setSettings(array $settings)
Определения fieldtype.php:278
getProperty()
Определения fieldtype.php:133
isMultiple()
Определения fieldtype.php:220
const VALUE_CONTEXT_JN_MOBILE
Определения fieldtype.php:101
const INT
Определения fieldtype.php:42
getSettings()
Определения fieldtype.php:266
const DOUBLE
Определения fieldtype.php:32
Определения date.php:9
static createFromTimestamp($timestamp)
Определения date.php:402
$value
Определения date.php:11
static convertFormatToPhp($format)
Определения date.php:309
static isExpression($text)
Определения activity.php:1718
static Init($arExt=array(), $bReturn=false)
Определения jscore.php:66
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$context
Определения csv_new_setup.php:223
const FORMAT_DATETIME
Определения include.php:64
const FORMAT_DATE
Определения include.php:63
$z
Определения options.php:31
htmlspecialcharsbx($string, $flags=ENT_COMPAT, $doubleEncode=true)
Определения tools.php:2701
CheckDateTime($datetime, $format=false)
Определения tools.php:398
MakeTimeStamp($datetime, $format=false)
Определения tools.php:538
$name
Определения menu_edit.php:35
Определения collection.php:2
Определения cookie.php:3
$settings
Определения product_settings.php:43
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
else $a
Определения template.php:137
$k
Определения template_pdf.php:567