2014-05-14 13:24:20 +00:00
|
|
|
<?php namespace Backend\FormWidgets;
|
|
|
|
|
|
2015-10-16 21:54:40 +00:00
|
|
|
use Carbon\Carbon;
|
2015-12-08 21:19:53 +00:00
|
|
|
use Backend\Classes\FormField;
|
2014-05-14 13:24:20 +00:00
|
|
|
use Backend\Classes\FormWidgetBase;
|
2016-04-23 23:33:39 +00:00
|
|
|
use System\Helpers\DateTime as DateTimeHelper;
|
2014-05-14 13:24:20 +00:00
|
|
|
|
|
|
|
|
/**
|
2014-06-19 11:40:24 +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
|
|
|
|
|
*/
|
2014-10-03 08:01:37 +00:00
|
|
|
class DatePicker extends FormWidgetBase
|
2014-05-14 13:24:20 +00:00
|
|
|
{
|
2015-02-28 03:43:34 +00:00
|
|
|
//
|
|
|
|
|
// Configurable properties
|
|
|
|
|
//
|
2014-05-14 13:24:20 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var bool Display mode: datetime, date, time.
|
|
|
|
|
*/
|
|
|
|
|
public $mode = 'datetime';
|
|
|
|
|
|
2016-10-19 21:44:14 +00:00
|
|
|
/**
|
|
|
|
|
* @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.
|
2016-04-18 19:59:32 +00:00
|
|
|
* eg: 2000-01-01
|
2014-05-14 13:24:20 +00:00
|
|
|
*/
|
2016-04-18 19:59:32 +00:00
|
|
|
public $minDate = null;
|
2014-05-14 13:24:20 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string the maximum/latest date that can be selected.
|
2016-04-18 19:59:32 +00:00
|
|
|
* eg: 2020-12-31
|
2014-05-14 13:24:20 +00:00
|
|
|
*/
|
2016-04-18 19:59:32 +00:00
|
|
|
public $maxDate = null;
|
2014-05-14 13:24:20 +00:00
|
|
|
|
2015-02-28 03:43:34 +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([
|
2016-04-18 19:11:40 +00:00
|
|
|
'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);
|
2015-09-29 23:02:42 +00:00
|
|
|
|
2016-04-18 19:59:32 +00:00
|
|
|
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);
|
|
|
|
|
}
|
2015-09-29 23:02:42 +00:00
|
|
|
}
|
|
|
|
|
|
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()
|
|
|
|
|
{
|
2014-11-26 08:02:06 +00:00
|
|
|
|
2015-01-04 22:45:04 +00:00
|
|
|
if ($value = $this->getLoadValue()) {
|
2016-04-25 20:18:04 +00:00
|
|
|
|
2016-08-13 01:29:01 +00:00
|
|
|
$value = DateTimeHelper::makeCarbon($value, false);
|
|
|
|
|
|
|
|
|
|
$value = $value instanceof Carbon ? $value->toDateTimeString() : $value;
|
2014-05-14 13:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-19 21:44:14 +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;
|
2014-11-26 06:57:59 +00:00
|
|
|
$this->vars['mode'] = $this->mode;
|
2014-05-14 13:24:20 +00:00
|
|
|
$this->vars['minDate'] = $this->minDate;
|
|
|
|
|
$this->vars['maxDate'] = $this->maxDate;
|
2016-10-19 21:44:14 +00:00
|
|
|
$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
|
|
|
{
|
2016-05-14 20:47:28 +00:00
|
|
|
if ($this->formField->disabled || $this->formField->hidden) {
|
2015-12-08 21:19:53 +00:00
|
|
|
return FormField::NO_SAVE_DATA;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-26 08:02:06 +00:00
|
|
|
if (!strlen($value)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $value;
|
2014-05-14 13:24:20 +00:00
|
|
|
}
|
2016-10-19 21:44:14 +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
|
|
|
}
|