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 : 'form-control fork-form-date inputDate';
$classError = $classError !== null ? (string) $classError : 'error';
// validate
if ($type == 'from' && ($date == 0 || $date == null)) {
throw new Exception('A datefield with type "from" should have a valid date-parameter.');
}
if ($type == 'till' && ($date == 0 || $date == null)) {
throw new Exception('A datefield with type "till" should have a valid date-parameter.');
}
if ($type == 'range' && ($date == 0 || $date2 == 0 || $date == null || $date2 == null)) {
throw new Exception('A datefield with type "range" should have 2 valid date-parameters.');
}
// @later get preferred mask & first day
$mask = 'd/m/Y';
$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;
// add extra classes based on type
switch ($type) {
// start date
case 'from':
$class .= ' fork-form-date-from inputDatefieldFrom';
$classError .= ' inputDatefieldFrom';
$attributes['data-startdate'] = date('Y-m-d', $date);
break;
// end date
// end date
case 'till':
$class .= ' fork-form-date-till inputDatefieldTill';
$classError .= ' inputDatefieldTill';
$attributes['data-enddate'] = date('Y-m-d', $date);
break;
// date range
// date range
case 'range':
$class .= ' fork-form-date-range inputDatefieldRange';
$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';
$classError .= ' inputDatefieldNormal';
break;
}
// create a datefield
$this->add(new FormDate($name, $value, $mask, $class, $classError));
// set attributes
parent::getField($name)->setAttributes($attributes);
// return datefield
return parent::getField($name);
}