Backend\Core\Engine\Form::addDate PHP Method

addDate() public method

Adds a datefield to the form
public addDate ( string $name, mixed $value = null, string $type = null, integer $date = null, integer $date2 = null, string $class = null, string $classError = null ) : FormDate
$name string Name of the element.
$value mixed The value for the element.
$type string The type (from, till, range) of the datepicker.
$date integer The date to use.
$date2 integer The second date for a rangepicker.
$class string Class(es) that have to be applied on the element.
$classError string Class(es) that have to be applied when an error occurs on the element.
return FormDate
    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);
    }

Usage Example

Example #1
0
 /**
  * Load the form
  */
 private function loadForm()
 {
     $startDate = '';
     $endDate = '';
     if (isset($this->filter['start_date']) && $this->filter['start_date'] != '') {
         $chunks = explode('/', $this->filter['start_date']);
         $startDate = (int) mktime(0, 0, 0, (int) $chunks[1], (int) $chunks[0], (int) $chunks[2]);
         if ($startDate == 0) {
             $startDate = '';
         }
     }
     if (isset($this->filter['end_date']) && $this->filter['end_date'] != '') {
         $chunks = explode('/', $this->filter['end_date']);
         $endDate = (int) mktime(0, 0, 0, (int) $chunks[1], (int) $chunks[0], (int) $chunks[2]);
         if ($endDate == 0) {
             $endDate = '';
         }
     }
     $this->frm = new BackendForm('filter', BackendModel::createURLForAction() . '&id=' . $this->id, 'get');
     $this->frm->addDate('start_date', $startDate);
     $this->frm->addDate('end_date', $endDate);
     // manually parse fields
     $this->frm->parse($this->tpl);
 }
All Usage Examples Of Backend\Core\Engine\Form::addDate