You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
230 lines
7.3 KiB
230 lines
7.3 KiB
![]()
12 years ago
|
<?php
|
||
|
/* For licensing terms, see /license.txt */
|
||
![]()
11 years ago
|
|
||
![]()
12 years ago
|
/**
|
||
![]()
8 years ago
|
* Form element to select a range of dates (with popup datepicker).
|
||
![]()
12 years ago
|
*/
|
||
|
class DateRangePicker extends HTML_QuickForm_text
|
||
|
{
|
||
![]()
11 years ago
|
/**
|
||
![]()
7 years ago
|
* DateRangePicker constructor.
|
||
|
*
|
||
|
* @param string $elementName
|
||
|
* @param string|array $elementLabel
|
||
|
* @param array $attributes
|
||
![]()
9 years ago
|
*/
|
||
![]()
7 years ago
|
public function __construct($elementName, $elementLabel = null, $attributes = null)
|
||
![]()
11 years ago
|
{
|
||
![]()
12 years ago
|
if (!isset($attributes['id'])) {
|
||
|
$attributes['id'] = $elementName;
|
||
|
}
|
||
![]()
11 years ago
|
$attributes['class'] = 'form-control';
|
||
![]()
11 years ago
|
parent::__construct($elementName, $elementLabel, $attributes);
|
||
![]()
11 years ago
|
$this->_appendName = true;
|
||
|
$this->_type = 'date_range_picker';
|
||
|
}
|
||
|
|
||
|
/**
|
||
![]()
11 years ago
|
* @return string
|
||
![]()
11 years ago
|
*/
|
||
|
public function toHtml()
|
||
|
{
|
||
|
$js = $this->getElementJS();
|
||
|
|
||
![]()
10 years ago
|
$this->removeAttribute('format');
|
||
|
$this->removeAttribute('timepicker');
|
||
|
$this->removeAttribute('validate_format');
|
||
|
|
||
![]()
11 years ago
|
return $js.parent::toHtml();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param string $value
|
||
|
*/
|
||
|
public function setValue($value)
|
||
![]()
12 years ago
|
{
|
||
|
$this->updateAttributes(
|
||
![]()
8 years ago
|
[
|
||
![]()
8 years ago
|
'value' => $value,
|
||
![]()
8 years ago
|
]
|
||
![]()
12 years ago
|
);
|
||
|
}
|
||
|
|
||
![]()
11 years ago
|
/**
|
||
![]()
8 years ago
|
* @param array $dateRange
|
||
|
*
|
||
|
* @return array
|
||
|
*/
|
||
|
public function parseDateRange($dateRange)
|
||
|
{
|
||
![]()
7 years ago
|
$dateRange = Security::remove_XSS($dateRange);
|
||
![]()
8 years ago
|
$dates = explode('/', $dateRange);
|
||
|
$dates = array_map('trim', $dates);
|
||
|
$start = isset($dates[0]) ? $dates[0] : '';
|
||
|
$end = isset($dates[1]) ? $dates[1] : '';
|
||
|
|
||
![]()
6 years ago
|
$pattern = 'yyyy-MM-dd HH:mm';
|
||
|
|
||
|
if ('false' === $this->getAttribute('timePicker') &&
|
||
|
false === strpos($this->getAttribute('format'), 'HH:mm')) {
|
||
|
$pattern = 'yyyy-MM-dd';
|
||
|
}
|
||
|
|
||
|
$formatter = new IntlDateFormatter(
|
||
|
'en',
|
||
|
IntlDateFormatter::NONE,
|
||
|
IntlDateFormatter::NONE,
|
||
|
'UTC',
|
||
|
IntlDateFormatter::GREGORIAN,
|
||
|
$pattern
|
||
|
);
|
||
|
$resultStart = $formatter->format($formatter->parse($start));
|
||
|
$resultEnd = $formatter->format($formatter->parse($end));
|
||
|
|
||
![]()
8 years ago
|
return [
|
||
![]()
6 years ago
|
'start' => $resultStart,
|
||
|
'end' => $resultEnd,
|
||
![]()
8 years ago
|
];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param array $dates result of parseDateRange()
|
||
|
*
|
||
|
* @return bool
|
||
|
*/
|
||
|
public function validateDates($dates, $format = null)
|
||
|
{
|
||
|
if (empty($dates['start']) || empty($dates['end'])) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
$format = $format ? $format : 'Y-m-d H:i';
|
||
|
$d = DateTime::createFromFormat($format, $dates['start']);
|
||
|
$resultStart = $d && $d->format($format) == $dates['start'];
|
||
|
|
||
|
$d = DateTime::createFromFormat($format, $dates['end']);
|
||
|
$resultEnd = $d && $d->format($format) == $dates['end'];
|
||
|
|
||
![]()
7 years ago
|
if (!$resultStart || !$resultEnd) {
|
||
![]()
8 years ago
|
return false;
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param mixed $value
|
||
|
* @param array $submitValues
|
||
|
* @param array $errors
|
||
|
*
|
||
|
* @return string
|
||
|
*/
|
||
|
public function getSubmitValue($value, &$submitValues, &$errors)
|
||
|
{
|
||
|
/** @var DateRangePicker $element */
|
||
|
$elementName = $this->getName();
|
||
|
$parsedDates = $this->parseDateRange($value);
|
||
|
$validateFormat = $this->getAttribute('validate_format');
|
||
|
|
||
|
if (!$this->validateDates($parsedDates, $validateFormat)) {
|
||
![]()
6 years ago
|
$errors[$elementName] = get_lang('Validate dates');
|
||
![]()
8 years ago
|
}
|
||
|
$submitValues[$elementName.'_start'] = $parsedDates['start'];
|
||
|
$submitValues[$elementName.'_end'] = $parsedDates['end'];
|
||
|
|
||
|
return $value;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get the necessary javascript for this datepicker.
|
||
|
*
|
||
![]()
11 years ago
|
* @return string
|
||
|
*/
|
||
|
private function getElementJS()
|
||
|
{
|
||
![]()
12 years ago
|
$js = null;
|
||
|
$id = $this->getAttribute('id');
|
||
![]()
11 years ago
|
$dateRange = $this->getAttribute('value');
|
||
|
|
||
|
$defaultDates = null;
|
||
|
if (!empty($dateRange)) {
|
||
|
$dates = $this->parseDateRange($dateRange);
|
||
|
$defaultDates = "
|
||
|
startDate: '".$dates['start']."',
|
||
|
endDate: '".$dates['end']."', ";
|
||
|
}
|
||
|
|
||
![]()
10 years ago
|
$minDate = null;
|
||
![]()
7 years ago
|
$minDateValue = Security::remove_XSS($this->getAttribute('minDate'));
|
||
![]()
10 years ago
|
if (!empty($minDateValue)) {
|
||
![]()
10 years ago
|
$minDate = "
|
||
![]()
10 years ago
|
minDate: '{$minDateValue}',
|
||
![]()
10 years ago
|
";
|
||
|
}
|
||
|
|
||
|
$maxDate = null;
|
||
![]()
7 years ago
|
$maxDateValue = Security::remove_XSS($this->getAttribute('maxDate'));
|
||
![]()
10 years ago
|
if (!empty($maxDateValue)) {
|
||
![]()
10 years ago
|
$maxDate = "
|
||
![]()
10 years ago
|
maxDate: '{$maxDateValue}',
|
||
![]()
10 years ago
|
";
|
||
|
}
|
||
|
|
||
|
$format = 'YYYY-MM-DD HH:mm';
|
||
![]()
7 years ago
|
$formatValue = Security::remove_XSS($this->getAttribute('format'));
|
||
![]()
10 years ago
|
if (!empty($formatValue)) {
|
||
|
$format = $formatValue;
|
||
![]()
10 years ago
|
}
|
||
|
|
||
|
$timePicker = 'true';
|
||
![]()
7 years ago
|
$timePickerValue = Security::remove_XSS($this->getAttribute('timePicker'));
|
||
![]()
10 years ago
|
if (!empty($timePickerValue)) {
|
||
![]()
6 years ago
|
$timePicker = 'false';
|
||
![]()
10 years ago
|
}
|
||
|
|
||
![]()
10 years ago
|
// timeFormat: 'hh:mm'
|
||
![]()
12 years ago
|
$js .= "<script>
|
||
|
$(function() {
|
||
![]()
9 years ago
|
$('#$id').daterangepicker({
|
||
![]()
10 years ago
|
timePicker: $timePicker,
|
||
![]()
12 years ago
|
timePickerIncrement: 30,
|
||
|
timePicker12Hour: false,
|
||
![]()
11 years ago
|
$defaultDates
|
||
![]()
10 years ago
|
$maxDate
|
||
|
$minDate
|
||
![]()
12 years ago
|
ranges: {
|
||
![]()
12 years ago
|
'".addslashes(get_lang('Today'))."': [moment(), moment()],
|
||
![]()
7 years ago
|
'".addslashes(get_lang('Yesterday'))."': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
|
||
![]()
6 years ago
|
'".addslashes(get_lang('This month'))."': [moment().startOf('month'), moment().endOf('month')],
|
||
|
'".addslashes(get_lang('Last month'))."': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
|
||
|
'".addslashes(get_lang('This week'))."': [moment().weekday(1), moment().weekday(5)],
|
||
|
'".addslashes(get_lang('Next Week'))."': [moment().weekday(8), moment().weekday(12)]
|
||
![]()
12 years ago
|
},
|
||
|
//showDropdowns : true,
|
||
![]()
9 years ago
|
|
||
![]()
12 years ago
|
locale: {
|
||
![]()
9 years ago
|
separator: ' / ',
|
||
|
format: '$format',
|
||
![]()
6 years ago
|
applyLabel: '".addslashes(get_lang('Validate'))."',
|
||
![]()
12 years ago
|
cancelLabel: '".addslashes(get_lang('Cancel'))."',
|
||
|
fromLabel: '".addslashes(get_lang('From'))."',
|
||
|
toLabel: '".addslashes(get_lang('Until'))."',
|
||
![]()
6 years ago
|
customRangeLabel: '".addslashes(get_lang('Custom range'))."',
|
||
![]()
12 years ago
|
}
|
||
|
});
|
||
![]()
9 years ago
|
|
||
|
$('#$id').on('change', function() {
|
||
|
var myPickedDates = $('#$id').val().split('/');
|
||
|
var {$id}_start = myPickedDates[0].trim();
|
||
|
var {$id}_end = myPickedDates[1].trim();
|
||
|
|
||
|
$('input[name={$id}_start]').val({$id}_start);
|
||
|
$('input[name={$id}_end]').val({$id}_end);
|
||
|
});
|
||
![]()
12 years ago
|
});
|
||
|
</script>";
|
||
|
|
||
![]()
11 years ago
|
return $js;
|
||
|
}
|
||
![]()
12 years ago
|
}
|