2014-05-14 13:24:20 +00:00
|
|
|
<?php namespace Backend\FormWidgets;
|
|
|
|
|
|
|
|
|
|
use Backend\Classes\FormWidgetBase;
|
|
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
{
|
2014-11-26 08:02:06 +00:00
|
|
|
const TIME_PREFIX = '___time_';
|
|
|
|
|
|
2014-05-14 13:24:20 +00:00
|
|
|
/**
|
|
|
|
|
* {@inheritDoc}
|
|
|
|
|
*/
|
|
|
|
|
public $defaultAlias = 'datepicker';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var bool Display mode: datetime, date, time.
|
|
|
|
|
*/
|
|
|
|
|
public $mode = 'datetime';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string the minimum/earliest date that can be selected.
|
|
|
|
|
*/
|
|
|
|
|
public $minDate = '2000-01-01';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string the maximum/latest date that can be selected.
|
|
|
|
|
*/
|
|
|
|
|
public $maxDate = '2020-12-31';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* {@inheritDoc}
|
|
|
|
|
*/
|
|
|
|
|
public function init()
|
|
|
|
|
{
|
2014-11-26 06:57:59 +00:00
|
|
|
$this->mode = strtolower($this->getConfig('mode', $this->mode));
|
2014-05-14 13:24:20 +00:00
|
|
|
$this->minDate = $this->getConfig('minDate', $this->minDate);
|
|
|
|
|
$this->maxDate = $this->getConfig('maxDate', $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();
|
|
|
|
|
|
2014-11-26 08:02:06 +00:00
|
|
|
$this->vars['timeName'] = self::TIME_PREFIX.$this->formField->getName(false);
|
2014-11-27 08:02:23 +00:00
|
|
|
$this->vars['timeValue'] = null;
|
2014-11-26 08:02:06 +00:00
|
|
|
|
2014-11-27 06:44:27 +00:00
|
|
|
if ($value = $this->getLoadData()) {
|
2014-05-14 13:24:20 +00:00
|
|
|
|
2014-11-27 06:44:27 +00:00
|
|
|
/*
|
|
|
|
|
* Date / Time
|
|
|
|
|
*/
|
|
|
|
|
if ($this->mode == 'datetime') {
|
2014-11-27 08:02:23 +00:00
|
|
|
if (is_object($value)) {
|
2014-11-27 06:44:27 +00:00
|
|
|
$value = $value->toDateTimeString();
|
|
|
|
|
}
|
2014-11-27 08:02:23 +00:00
|
|
|
|
|
|
|
|
$dateTime = explode(' ', $value);
|
2014-11-27 06:44:27 +00:00
|
|
|
$value = $dateTime[0];
|
|
|
|
|
$this->vars['timeValue'] = isset($dateTime[1]) ? substr($dateTime[1], 0, 5) : '';
|
2014-11-04 06:41:48 +00:00
|
|
|
}
|
2014-11-27 06:44:27 +00:00
|
|
|
/*
|
|
|
|
|
* Date
|
|
|
|
|
*/
|
|
|
|
|
elseif ($this->mode == 'date') {
|
|
|
|
|
if (is_string($value)) {
|
|
|
|
|
$value = substr($value, 0, 10);
|
|
|
|
|
}
|
|
|
|
|
elseif (is_object($value)) {
|
|
|
|
|
$value = $value->toDateString();
|
|
|
|
|
}
|
2014-10-10 21:50:05 +00:00
|
|
|
}
|
2014-11-27 06:44:27 +00:00
|
|
|
elseif ($this->mode == 'time') {
|
|
|
|
|
if (is_object($value)) {
|
|
|
|
|
$value = $value->toTimeString();
|
|
|
|
|
}
|
2014-11-26 10:57:25 +00:00
|
|
|
}
|
2014-11-27 06:44:27 +00:00
|
|
|
|
2014-05-14 13:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->vars['value'] = $value ?: '';
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* {@inheritDoc}
|
|
|
|
|
*/
|
|
|
|
|
public function loadAssets()
|
|
|
|
|
{
|
2014-05-24 06:57:38 +00:00
|
|
|
$this->addCss('vendor/pikaday/css/pikaday.css', 'core');
|
2014-11-26 06:57:59 +00:00
|
|
|
$this->addCss('vendor/clockpicker/css/jquery-clockpicker.css', 'core');
|
2014-05-24 06:57:38 +00:00
|
|
|
$this->addCss('css/datepicker.css', 'core');
|
2014-07-26 06:39:03 +00:00
|
|
|
$this->addJs('vendor/moment/moment.js', 'core');
|
2014-05-24 06:57:38 +00:00
|
|
|
$this->addJs('vendor/pikaday/js/pikaday.js', 'core');
|
|
|
|
|
$this->addJs('vendor/pikaday/js/pikaday.jquery.js', 'core');
|
2014-11-26 06:57:59 +00:00
|
|
|
$this->addJs('vendor/clockpicker/js/jquery-clockpicker.js', 'core');
|
2014-05-24 06:57:38 +00:00
|
|
|
$this->addJs('js/datepicker.js', 'core');
|
2014-11-26 06:57:59 +00:00
|
|
|
$this->addJs('js/timepicker.js', 'core');
|
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
|
|
|
{
|
2014-11-26 08:02:06 +00:00
|
|
|
if (!strlen($value)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-03 04:48:07 +00:00
|
|
|
$timeValue = post(self::TIME_PREFIX . $this->formField->getName(false));
|
|
|
|
|
if ($this->mode == 'datetime' && $timeValue) {
|
2014-12-27 22:33:27 +00:00
|
|
|
$value .= ' ' . $timeValue . ':00';
|
2014-11-26 08:02:06 +00:00
|
|
|
}
|
|
|
|
|
elseif ($this->mode == 'time') {
|
|
|
|
|
$value .= ':00';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $value;
|
2014-05-14 13:24:20 +00:00
|
|
|
}
|
2014-10-10 21:50:05 +00:00
|
|
|
}
|