ORIENT/modules/backend/formwidgets/DatePicker.php

158 lines
3.4 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.
*/
public $format = null;
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
*/
public $minDate = null;
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
*/
public $maxDate = null;
2014-05-14 13:24:20 +00:00
//
// Object properties
//
/**
* {@inheritDoc}
*/
protected $defaultAlias = 'datepicker';
2014-05-14 13:24:20 +00:00
/**
* {@inheritDoc}
*/
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',
]);
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) {
$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);
}
}
2014-05-14 13:24:20 +00:00
/**
* {@inheritDoc}
*/
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->formField->getName();
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['format'] = $this->format;
$this->vars['formatMoment'] = $this->getDateFormatMoment();
$this->vars['formatAlias'] = $this->getDateFormatAlias();
2014-05-14 13:24:20 +00:00
}
/**
* {@inheritDoc}
*/
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
}