Version: current

      1 <?php
      2 /**
      3  * EditableDateField
      4  *
      5  * Allows a user to add a date field.
      6  *
      7  * @package userforms
      8  */
      9
     10 class EditableDateField extends EditableFormField {
     11
     12     static $singular_name = 'Date Field';
     13
     14     static $plural_name = 'Date Fields';
     15
     16     function getFieldConfiguration() {
     17         $fields = parent::getFieldConfiguration();
     18
     19         $min = ($this->getSetting('MinDate')) ? $this->getSetting('MinDate') : '';
     20         $minOffset = ($this->getSetting('MinDateOffset')) ? $this->getSetting('MinDateOffset') : '';
     21         $minDateField = ($this->getSetting('MinDateField')) ? $this->getSetting('MinDateField') : '';
     22         $someFieldMinOffset = ($this->getSetting('SomeFieldMinOffset')) ? $this->getSetting('SomeFieldMinOffset') : '';
     23
     24         $max = ($this->getSetting('MaxDate')) ? $this->getSetting('MaxDate') : '';
     25         $maxOffset = ($this->getSetting('MaxDateOffset')) ? $this->getSetting('MaxDateOffset') : '';
     26         $maxDateField = ($this->getSetting('MaxDateField')) ? $this->getSetting('MaxDateField') : '';
     27         $someFieldMaxOffset = ($this->getSetting('SomeFieldMaxOffset')) ? $this->getSetting('SomeFieldMaxOffset') : '';
     28
     29         $default = ($this->getSetting('DefaultToToday')) ? $this->getSetting('DefaultToToday') : false;
     30         $label = _t('EditableFormField.DEFAULTTOTODAY', 'Default to Today?');
     31
     32         $minDateType = ($this->getSetting('MinDateType')) ? $this->getSetting('MinDateType') : '';
     33         $maxDateType = ($this->getSetting('MaxDateType')) ? $this->getSetting('MaxDateType') : '';
     34
     35         $fields->push(new CheckboxField($this->getSettingName("DefaultToToday"), $label, $default));
     36
     37         $dateFields = new DataObjectSet();
     38         if ($allFields = $this->Parent()->Fields()) {
     39             foreach($allFields as $field) {
     40                 if (($field->ClassName == 'EditableDateField') && ($field->Name != $this->Name)) {
     41                     $dateFields->push($field);
     42                 }
     43             }
     44         }
     45
     46         $fields->push(new LiteralField('MinDateTitle', _t('EditableDateField.MINDATETITLE')));
     47         $options = array(
     48             '0//' . _t('EditableDateField.NOMINDATE', 'No Min Date') => new LiteralField('NOMINDATE', ''),
     49             '1//' . _t('EditableDateField.MINDATE', 'Min Date') => new DateField($this->getSettingName("MinDate"), '', $min),
     50             '2//' . _t('EditableDateField.CurrentDateOffset', 'Min Date Offset') => new NumericField($this->getSettingName("MinDateOffset"), '', $minOffset),
     51         );
     52         if ($dateFields->Count()) {
     53             $options['3//' . _t('EditableDateField.SomeField', 'Some Field')] = new CompositeField(new FieldSet(new DropdownField($this->getSettingName("MinDateField"), '', $dateFields->map(), $minDateField), new NumericField($this->getSettingName("SomeFieldMinOffset"), _t('EditableDateField.SomeFieldOffset', 'Some Field Offset Title'), $someFieldMinOffset)));
     54         }
     55         $t = new SelectionGroup($this->getSettingName("MinDateType"), $options);
     56         $t->setValue($minDateType);
     57         $fields->push($t);
     58
     59         $fields->push(new LiteralField('MaxDateTitle', _t('EditableDateField.MAXDATETITLE')));
     60         $options = array(
     61             '0//' . _t('EditableDateField.NOMAXDATE', 'No Max Date') => new LiteralField('NOMAXDATE', ''),
     62             '1//' . _t('EditableDateField.MAXDATE', 'Max Date') => new DateField($this->getSettingName("MaxDate"), '', $max),
     63             '2//' . _t('EditableDateField.CurrentDateOffset', 'Max Date Offset') => new NumericField($this->getSettingName("MaxDateOffset"), '', $maxOffset),
     64         );
     65         if ($dateFields->Count()) {
     66             $options['3//' . _t('EditableDateField.SomeField', 'Some Field')] = new CompositeField(new FieldSet(new DropdownField($this->getSettingName("MaxDateField"), '', $dateFields->map(), $maxDateField), new NumericField($this->getSettingName("SomeFieldMaxOffset"), _t('EditableDateField.SomeFieldOffset', 'Some Field Offset Title'), $someFieldMaxOffset)));
     67         }
     68         $t = new SelectionGroup($this->getSettingName("MaxDateType"), $options);
     69         $t->setValue($maxDateType);
     70         $fields->push($t);
     71
     72         return $fields;
     73     }
     74
     75     /**
     76      * Return the form field.
     77      *
     78      * @todo Make a jQuery safe form field. The current CalendarDropDown
     79      *          breaks on the front end.
     80      */
     81     public function getFormField() {
     82         $default = ($this->getSetting('DefaultToToday')) ? date('Y-m-d') : $this->Default;
     83
     84         $field = new DateField( $this->Name, $this->Title, $default);
     85         if ($this->getSetting('MinDateType')) {
     86             if (($this->getSetting('MinDateType') == 1) && $this->getSetting('MinDate')) {
     87                 $field->setHTML5Attribute('min', $this->getSetting('MinDate'));
     88             } elseif (($this->getSetting('MinDateType') == 2) && ($offset = $this->getSetting('MinDateOffset'))) {
     89                 $field->setHTML5Attribute('min', date('Y-m-d', strtotime((($offset > 0) ? '+' : '') . "{$offset} day")));
     90             } elseif (($this->getSetting('MinDateType') == 3) && ($minFieldID = $this->getSetting('MinDateField')) && ($minFieldOffset = $this->getSetting('SomeFieldMinOffset'))) {
     91                 if (($fields = $this->Parent()->Fields()) && ($minField = $fields->find('ID', $minFieldID))) {
     92                     $field->addExtraAttribute('min_field', $minField);
     93                     $field->addExtraAttribute('min_field_offset', $minFieldOffset);
     94                 }
     95             }
     96         }
     97         if ($this->getSetting('MaxDateType')) {
     98             if (($this->getSetting('MaxDateType') == 1) && $this->getSetting('MaxDate')) {
     99                 $field->setHTML5Attribute('max', $this->getSetting('MaxDate'));
    100             } elseif (($this->getSetting('MaxDateType') == 2) && ($offset = $this->getSetting('MaxDateOffset'))) {
    101                 $field->setHTML5Attribute('max', date('Y-m-d', strtotime((($offset > 0) ? '+' : '') . "{$offset} day")));
    102             } elseif (($this->getSetting('MaxDateType') == 3) && ($maxFieldID = $this->getSetting('MaxDateField')) && ($maxFieldOffset = $this->getSetting('SomeFieldMaxOffset'))) {
    103                 if (($fields = $this->Parent()->Fields()) && ($maxField = $fields->find('ID', $maxFieldID))) {
    104                     $field->addExtraAttribute('max_field', $maxField);
    105                     $field->addExtraAttribute('max_field_offset', $maxFieldOffset);
    106                 }
    107             }
    108         }
    109         return $field;
    110     }
    111
    112     /**
    113      * Return the validation information related to this field. This is
    114      * interrupted as a JSON object for validate plugin and used in the
    115      * PHP.
    116      *
    117      * @see http://docs.jquery.com/Plugins/Validation/Methods
    118      * @return Array
    119      */
    120     public function getValidation() {
    121         return array_merge(
    122             parent::getValidation(),
    123              array(
    124                 'date' => true
    125             )
    126         );
    127     }
    128 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -