139 lines
3.0 KiB
PHP
139 lines
3.0 KiB
PHP
<?php namespace Backend\FormWidgets;
|
|
|
|
use Carbon\Carbon;
|
|
use Backend\Classes\FormField;
|
|
use Backend\Classes\FormWidgetBase;
|
|
use System\Helpers\DateTime as DateTimeHelper;
|
|
|
|
/**
|
|
* Date picker
|
|
* Renders a date picker field.
|
|
*
|
|
* @package october\backend
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
*/
|
|
class DatePicker extends FormWidgetBase
|
|
{
|
|
//
|
|
// Configurable properties
|
|
//
|
|
|
|
/**
|
|
* @var bool Display mode: datetime, date, time.
|
|
*/
|
|
public $mode = 'datetime';
|
|
|
|
/**
|
|
* @var string the minimum/earliest date that can be selected.
|
|
* eg: 2000-01-01
|
|
*/
|
|
public $minDate = null;
|
|
|
|
/**
|
|
* @var string the maximum/latest date that can be selected.
|
|
* eg: 2020-12-31
|
|
*/
|
|
public $maxDate = null;
|
|
|
|
//
|
|
// Object properties
|
|
//
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
protected $defaultAlias = 'datepicker';
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
public function init()
|
|
{
|
|
$this->fillFromConfig([
|
|
'format',
|
|
'mode',
|
|
'minDate',
|
|
'maxDate',
|
|
]);
|
|
|
|
$this->mode = strtolower($this->mode);
|
|
|
|
if ($this->minDate !== null) {
|
|
$this->minDate = is_integer($this->minDate)
|
|
? Carbon::createFromTimestamp($this->minDate)
|
|
: Carbon::parse($this->minDate);
|
|
}
|
|
|
|
if ($this->maxDate !== null) {
|
|
$this->maxDate = is_integer($this->maxDate)
|
|
? Carbon::createFromTimestamp($this->maxDate)
|
|
: Carbon::parse($this->maxDate);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
public function render()
|
|
{
|
|
$this->prepareVars();
|
|
return $this->makePartial('datepicker');
|
|
}
|
|
|
|
/**
|
|
* Prepares the list data
|
|
*/
|
|
public function prepareVars()
|
|
{
|
|
$this->vars['name'] = $this->formField->getName();
|
|
|
|
|
|
if ($value = $this->getLoadValue()) {
|
|
$value = $value instanceof Carbon ? $value->toDateTimeString() : $value;
|
|
|
|
/*
|
|
* Time
|
|
*/
|
|
if (strlen($value) <= 8) {
|
|
$value = Carbon::now()->toDateString() . ' ' . $value;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Display alias, used by preview mode
|
|
*/
|
|
if ($this->mode == 'time') {
|
|
$formatAlias = 'time';
|
|
}
|
|
elseif ($this->mode == 'date') {
|
|
$formatAlias = 'dateLong';
|
|
}
|
|
else {
|
|
$formatAlias = 'dateTimeLong';
|
|
}
|
|
|
|
$this->vars['formatAlias'] = $formatAlias;
|
|
$this->vars['value'] = $value ?: '';
|
|
$this->vars['field'] = $this->formField;
|
|
$this->vars['mode'] = $this->mode;
|
|
$this->vars['minDate'] = $this->minDate;
|
|
$this->vars['maxDate'] = $this->maxDate;
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
public function getSaveValue($value)
|
|
{
|
|
if ($this->formField->disabled) {
|
|
return FormField::NO_SAVE_DATA;
|
|
}
|
|
|
|
if (!strlen($value)) {
|
|
return null;
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
}
|