public function addDate($name, $value = null, $type = null, $date = null, $date2 = null, $class = null, $classError = null)
{
$name = (string) $name;
$value = $value !== null ? $value !== '' ? (int) $value : '' : null;
$type = \SpoonFilter::getValue($type, array('from', 'till', 'range'), 'none');
$date = $date !== null ? (int) $date : null;
$date2 = $date2 !== null ? (int) $date2 : null;
$class = $class !== null ? (string) $class : 'inputText inputDate';
$classError = $classError !== null ? (string) $classError : 'inputTextError inputDateError';
// validate
if ($type == 'from' && ($date == 0 || $date == null)) {
throw new Exception('A date field with type "from" should have a valid date-parameter.');
}
if ($type == 'till' && ($date == 0 || $date == null)) {
throw new Exception('A date field with type "till" should have a valid date-parameter.');
}
if ($type == 'range' && ($date == 0 || $date2 == 0 || $date == null || $date2 == null)) {
throw new Exception('A date field with type "range" should have 2 valid date-parameters.');
}
// set mask and firstday
$mask = Model::get('fork.settings')->get('Core', 'date_format_short');
$firstDay = 1;
// build attributes
$attributes['data-mask'] = str_replace(array('d', 'm', 'Y', 'j', 'n'), array('dd', 'mm', 'yy', 'd', 'm'), $mask);
$attributes['data-firstday'] = $firstDay;
$attributes['data-year'] = date('Y', $value);
// -1 because javascript starts at 0
$attributes['data-month'] = date('n', $value) - 1;
$attributes['data-day'] = date('j', $value);
// add extra classes based on type
switch ($type) {
// start date
case 'from':
$class .= ' inputDatefieldFrom inputText';
$classError .= ' inputDatefieldFrom';
$attributes['data-startdate'] = date('Y-m-d', $date);
break;
// end date
// end date
case 'till':
$class .= ' inputDatefieldTill inputText';
$classError .= ' inputDatefieldTill';
$attributes['data-enddate'] = date('Y-m-d', $date);
break;
// date range
// date range
case 'range':
$class .= ' inputDatefieldRange inputText';
$classError .= ' inputDatefieldRange';
$attributes['data-startdate'] = date('Y-m-d', $date);
$attributes['data-enddate'] = date('Y-m-d', $date2);
break;
// normal date field
// normal date field
default:
$class .= ' inputDatefieldNormal inputText';
$classError .= ' inputDatefieldNormal';
break;
}
// create a datefield
$this->add(new FrontendFormDate($name, $value, $mask, $class, $classError));
// set attributes
parent::getField($name)->setAttributes($attributes);
// return date field
return parent::getField($name);
}