yf_form2_daterange class| File | plugins/form2/classes/form2/yf_form2_daterange.class.php |
|---|---|
| Use | _class('form2_daterange', 'classes/form2/') |
A site sets them in its config with $PROJECT_CONF['form2_daterange']['NAME'] = ...;
| Property | Default | Description |
|---|---|---|
$SHOW_VALUEbool |
false |
Render the selected date in the field (format `format_php`, default Y-m-d). Off keeps the old output, where the value was always empty because the format variable was never set (yfix/yf c41e75bd4). Default flips in 4.0. |
public function daterange_select($name = '', $desc = '', $extra = [], $replace = [], $form = null)
Daterange picker http://www.daterangepicker.com/#examples.
public function daterange_select($name = '', $desc = '', $extra = [], $replace = [], $form = null)
{
if (is_array($desc)) {
$extra += $desc;
$desc = '';
}
if ( ! is_array($extra)) {
$extra = [];
}
$extra['name'] = ($extra['name'] ?? null) ?: ($name ?: 'date');
$extra['desc'] = $form->_prepare_desc($extra, $desc);
$extra['placeholder'] = ($extra['placeholder'] ?? null) ?: ($this->SHOW_VALUE ? '' : null);
if (($extra['min_date'] ?? null) && strlen($extra['min_date']) == 10) {
$time = time();
$extra += [
'min_date' => date('Y-m-d', (int) (($extra['min_date'] ?? null) ?: ($time - 86400 * 30))),
'max_date' => date('Y-m-d', $time + 86400),
'autocomplete' => 'off',
];
}
// Compatibility with filter
if ( ! strlen($extra['value'] ?? '')) {
if (isset($extra['selected'])) {
$value = $extra['selected'];
} elseif (isset($form->_params['selected'])) {
$value = $form->_params['selected'][$extra['name']];
} elseif (isset($form->_replace[$extra['name']])) {
$value = $form->_replace[$extra['name']];
}
$extra['value'] = empty($value) || $value == '0000-00-00 00:00:00' ? null : strtotime($value);
}
if ($this->SHOW_VALUE) {
$time = $extra['value'];
if ( ! empty($time) && ! is_numeric($time)) {
$time = $time === '0000-00-00 00:00:00' ? null : strtotime($time);
}
$extra['value'] = empty($time) ? '' : date(($extra['format_php'] ?? null) ?: 'Y-m-d', (int) $time);
} else {
// A passed date string used to reach date() as its timestamp and throw a TypeError
$extra['value'] = '';
}
asset('bootstrap-daterangepicker');
jquery('$("input#' . $extra['name'] . '").daterangepicker({
format: "' . (($extra['format'] ?? null) ?: 'YYYY-MM-DD') . '",
minDate: "' . ($extra['min_date'] ?? null) . '",
maxDate: "' . ($extra['max_date'] ?? null) . '",
startDate: "' . (($extra['start_date'] ?? null) ?: ($extra['min_date'] ?? null)) . '",
endDate: "' . (($extra['end_date'] ?? null) ?: ($extra['max_date'] ?? null)) . '",
buttonClasses: "btn btn-xs",
ranges: {
"' . t('Today') . '": [moment(), moment()],
"' . t('Yesterday') . '": [moment().subtract(1, "days"), moment().subtract(1, "days")],
"' . t('Last 7 Days') . '": [moment().subtract(6, "days"), moment()],
"' . t('Last 30 Days') . '": [moment().subtract(29, "days"), moment()],
"' . t('Last 90 Days') . '": [moment().subtract(89, "days"), moment()],
"' . t('This Month') . '": [moment().startOf("month"), moment().endOf("month")],
"' . t('Last Month') . '": [moment().subtract(1, "month").startOf("month"), moment().subtract(1, "month").endOf("month")]
}
});');
if (MAIN_TYPE_ADMIN) {
css('.daterangepicker .ranges li { padding: 0px 12px; margin-bottom: 1px; }');
}
$extra['type'] = 'text';
$extra['class_add'] = 'input-medium'; // daterangepicker';
$extra['prepend'] = isset($extra['prepend']) ? $extra['prepend'] : '<i class="' . $form->CLASS_ICON_CALENDAR . '"></i>';
return $form->input($extra['name'], $extra['desc'], $extra, $replace);
}