ORIENT/modules/backend/formwidgets/DatePicker.php

188 lines
4.2 KiB
PHP
Raw Normal View History

2014-05-14 13:24:20 +00:00
<?php namespace Backend\FormWidgets;
2015-10-16 21:54:40 +00:00
use Carbon\Carbon;
use Backend\Classes\FormField;
2014-05-14 13:24:20 +00:00
use Backend\Classes\FormWidgetBase;
use System\Helpers\DateTime as DateTimeHelper;
2014-05-14 13:24:20 +00:00
/**
* Date picker
* Renders a date picker field.
2014-05-14 13:24:20 +00:00
*
* @package october\backend
* @author Alexey Bobkov, Samuel Georges
*/
class DatePicker extends FormWidgetBase
2014-05-14 13:24:20 +00:00
{
//
// Configurable properties
//
2014-05-14 13:24:20 +00:00
/**
* @var bool Display mode: datetime, date, time.
*/
public $mode = 'datetime';
/**
* @var string Provide an explicit date display format.
*/
2018-08-15 16:33:24 +00:00
public $format;
2014-05-14 13:24:20 +00:00
/**
* @var string the minimum/earliest date that can be selected.
* eg: 2000-01-01
2014-05-14 13:24:20 +00:00
*/
2018-08-15 16:33:24 +00:00
public $minDate;
2014-05-14 13:24:20 +00:00
/**
* @var string the maximum/latest date that can be selected.
* eg: 2020-12-31
2014-05-14 13:24:20 +00:00
*/
2018-08-15 16:33:24 +00:00
public $maxDate;
2014-05-14 13:24:20 +00:00
/**
* @var string number of years either side or array of upper/lower range
* eg: 10 or [1900,1999]
*/
2018-08-15 16:33:24 +00:00
public $yearRange;
/**
* @var int first day of the week
* eg: 0 (Sunday), 1 (Monday), 2 (Tuesday), etc.
*/
public $firstDay = 0;
/**
* @var bool show week numbers at head of row
*/
public $showWeekNumber = false;
/**
* @var bool change datetime exactly as is in database
*/
public $ignoreTimezone = false;
//
// Object properties
//
/**
2017-03-15 19:26:14 +00:00
* @inheritDoc
*/
protected $defaultAlias = 'datepicker';
2014-05-14 13:24:20 +00:00
/**
2017-03-15 19:26:14 +00:00
* @inheritDoc
2014-05-14 13:24:20 +00:00
*/
public function init()
{
2015-10-16 21:54:40 +00:00
$this->fillFromConfig([
'format',
2015-10-16 21:54:40 +00:00
'mode',
'minDate',
'maxDate',
'yearRange',
'firstDay',
'showWeekNumber',
'ignoreTimezone',
2015-10-16 21:54:40 +00:00
]);
2014-05-14 13:24:20 +00:00
2015-10-16 21:54:40 +00:00
$this->mode = strtolower($this->mode);
if ($this->minDate !== null) {
2018-08-15 16:51:25 +00:00
$this->minDate = is_int($this->minDate)
? Carbon::createFromTimestamp($this->minDate)
: Carbon::parse($this->minDate);
}
if ($this->maxDate !== null) {
2018-08-15 16:51:25 +00:00
$this->maxDate = is_int($this->maxDate)
? Carbon::createFromTimestamp($this->maxDate)
: Carbon::parse($this->maxDate);
}
}
2014-05-14 13:24:20 +00:00
/**
2017-03-15 19:26:14 +00:00
* @inheritDoc
2014-05-14 13:24:20 +00:00
*/
public function render()
{
$this->prepareVars();
return $this->makePartial('datepicker');
}
/**
* Prepares the list data
*/
public function prepareVars()
{
2015-01-04 22:45:04 +00:00
if ($value = $this->getLoadValue()) {
$value = DateTimeHelper::makeCarbon($value, false);
$value = $value instanceof Carbon ? $value->toDateTimeString() : $value;
2014-05-14 13:24:20 +00:00
}
$this->vars['name'] = $this->getFieldName();
2014-05-14 13:24:20 +00:00
$this->vars['value'] = $value ?: '';
2015-08-04 10:04:25 +00:00
$this->vars['field'] = $this->formField;
$this->vars['mode'] = $this->mode;
2014-05-14 13:24:20 +00:00
$this->vars['minDate'] = $this->minDate;
$this->vars['maxDate'] = $this->maxDate;
$this->vars['yearRange'] = $this->yearRange;
$this->vars['firstDay'] = $this->firstDay;
$this->vars['showWeekNumber'] = $this->showWeekNumber;
$this->vars['ignoreTimezone'] = $this->ignoreTimezone;
$this->vars['format'] = $this->format;
$this->vars['formatMoment'] = $this->getDateFormatMoment();
$this->vars['formatAlias'] = $this->getDateFormatAlias();
2014-05-14 13:24:20 +00:00
}
/**
2017-03-15 19:26:14 +00:00
* @inheritDoc
2014-05-14 13:24:20 +00:00
*/
2015-01-04 22:43:39 +00:00
public function getSaveValue($value)
2014-05-14 13:24:20 +00:00
{
if ($this->formField->disabled || $this->formField->hidden) {
return FormField::NO_SAVE_DATA;
}
if (!strlen($value)) {
return null;
}
return $value;
2014-05-14 13:24:20 +00:00
}
/**
* Convert PHP format to JS format
*/
protected function getDateFormatMoment()
{
if ($this->format) {
return DateTimeHelper::momentFormat($this->format);
}
}
/*
* Display alias, used by preview mode
*/
protected function getDateFormatAlias()
{
if ($this->format) {
return null;
}
if ($this->mode == 'time') {
return 'time';
}
elseif ($this->mode == 'date') {
return 'dateLong';
}
else {
return 'dateTimeLong';
}
}
2014-10-10 21:50:05 +00:00
}