Version: current

      1 <?php
      2 /**
      3  * EditableDateTimeField
      4  *
      5  * Allow users to define a validating datetime field for a UserDefinedForm
      6  *
      7  * @package userforms
      8  */
      9
     10 class EditableDateTimeField extends EditableFormField {
     11
     12     static $singular_name = 'DateTime Field';
     13
     14     static $plural_name = 'DateTime Fields';
     15
     16     function getFieldConfiguration() {
     17         $fields = parent::getFieldConfiguration();
     18
     19         $default = ($this->getSetting('DefaultToToday')) ? $this->getSetting('DefaultToToday') : false;
     20         $fields->push(new CheckboxField($this->getSettingName("DefaultToToday"), _t('EditableFormField.DEFAULTTOTODAY', 'Default to Today?'), $default));
     21
     22         $minDate = ($this->getSetting('MinDate')) ? $this->getSetting('MinDate') : '';
     23         $minDateOffset = ($this->getSetting('MinDateOffset')) ? $this->getSetting('MinDateOffset') : '';
     24         $minDateField = ($this->getSetting('MinDateField')) ? $this->getSetting('MinDateField') : '';
     25
     26         $minDateType = ($this->getSetting('MinDateType')) ? $this->getSetting('MinDateType') : '';
     27         $maxDateType = ($this->getSetting('MaxDateType')) ? $this->getSetting('MaxDateType') : '';
     28
     29         $fields->push(new LiteralField('MinDateTitle', _t('EditableDateField.MINDATETITLE')));
     30         $options = array(
     31             '0//' . _t('EditableDateField.NOMINDATE', 'No Min Date') => new LiteralField('NOMINDATE', ''),
     32             '1//' . _t('EditableDateField.MINDATE', 'Min Date') => new DateField($this->getSettingName("MinDate"), '', $minDate),
     33             '2//' . _t('EditableDateField.CurrentDateOffset', 'Min Date Offset') => new NumericField($this->getSettingName("MinDateOffset"), '', $minDateOffset),
     34         );
     35         $t = new SelectionGroup($this->getSettingName("MinDateType"), $options);
     36         $t->setValue($minDateType);
     37         $fields->push($t);
     38
     39
     40         $minTime = ($this->getSetting('MinTime')) ? $this->getSetting('MinTime') : '';
     41         $minTimeOffset = ($this->getSetting('MinTimeOffset')) ? $this->getSetting('MinTimeOffset') : '';
     42         $minTimeField = ($this->getSetting('MinTimeField')) ? $this->getSetting('MinTimeField') : '';
     43
     44         $minTimeType = ($this->getSetting('MinTimeType')) ? $this->getSetting('MinTimeType') : '';
     45         $maxTimeType = ($this->getSetting('MaxTimeType')) ? $this->getSetting('MaxTimeType') : '';
     46
     47         $fields->push(new LiteralField('MinTimeTitle', _t('EditableTimeField.MINTIMETITLE')));
     48         $options = array(
     49             '0//' . _t('EditableTimeField.NOMINTIME', 'No Min Time') => new LiteralField('NOMINTIME', ''),
     50             '1//' . _t('EditableTimeField.MINTIME', 'Min Time') => new TimeField($this->getSettingName("MinTime"), '', $minTime),
     51             '2//' . _t('EditableTimeField.CurrentTimeOffset', 'Min Time Offset') => new NumericField($this->getSettingName("MinTimeOffset"), '', $minTimeOffset),
     52         );
     53         $t = new SelectionGroup($this->getSettingName("MinTimeType"), $options);
     54         $t->setValue($minTimeType);
     55         $fields->push($t);
     56
     57
     58         $maxDate = ($this->getSetting('MaxDate')) ? $this->getSetting('MaxDate') : '';
     59         $maxDateOffset = ($this->getSetting('MaxDateOffset')) ? $this->getSetting('MaxDateOffset') : '';
     60         $maxDateField = ($this->getSetting('MaxDateField')) ? $this->getSetting('MaxDateField') : '';
     61         $fields->push(new LiteralField('MaxDateTitle', _t('EditableDateField.MAXDATETITLE')));
     62         $options = array(
     63             '0//' . _t('EditableDateField.NOMAXDATE', 'No Max Date') => new LiteralField('NOMAXDATE', ''),
     64             '1//' . _t('EditableDateField.MAXDATE', 'Max Date') => new DateField($this->getSettingName("MaxDate"), '', $maxDate),
     65             '2//' . _t('EditableDateField.CurrentDateOffset', 'Max Date Offset') => new NumericField($this->getSettingName("MaxDateOffset"), '', $maxDateOffset),
     66         );
     67         $t = new SelectionGroup($this->getSettingName("MaxDateType"), $options);
     68         $t->setValue($maxDateType);
     69         $fields->push($t);
     70
     71         $maxTime = ($this->getSetting('MaxTime')) ? $this->getSetting('MaxTime') : '';
     72         $maxTimeOffset = ($this->getSetting('MaxTimeOffset')) ? $this->getSetting('MaxTimeOffset') : '';
     73         $maxTimeField = ($this->getSetting('MaxTimeField')) ? $this->getSetting('MaxTimeField') : '';
     74         $fields->push(new LiteralField('MaxTimeTitle', _t('EditableTimeField.MAXTIMETITLE')));
     75         $options = array(
     76             '0//' . _t('EditableTimeField.NOMAXTIME', 'No Max Time') => new LiteralField('NOMAXTIME', ''),
     77             '1//' . _t('EditableTimeField.MAXTIME', 'Max Time') => new TimeField($this->getSettingName("MaxTime"), '', $maxTime),
     78             '2//' . _t('EditableTimeField.CurrentTimeOffset', 'Max Time Offset') => new NumericField($this->getSettingName("MaxTimeOffset"), '', $maxTimeOffset),
     79         );
     80         $t = new SelectionGroup($this->getSettingName("MaxTimeType"), $options);
     81         $t->setValue($maxTimeType);
     82         $fields->push($t);
     83
     84         return $fields;
     85     }
     86
     87     /**
     88      * Return the form field.
     89      *
     90      * @todo Make a jQuery safe form field. The current CalendarDropDown
     91      *          breaks on the front end.
     92      */
     93     public function getFormField() {
     94         $default = ($this->getSetting('DefaultToToday')) ? date('Y-m-d H:i') : $this->Default;
     95
     96         $field = new DateTimeField( $this->Name, $this->Title);
     97         $field->setValue($default);
     98
     99         $dateField = $field->getDateField();
    100         if ($this->getSetting('MinDateType')) {
    101             if (($this->getSetting('MinDateType') == 1) && $this->getSetting('MinDate')) {
    102                 $dateField->setHTML5Attribute('min', $this->getSetting('MinDate'));
    103             } elseif (($this->getSetting('MinDateType') == 2) && ($offset = $this->getSetting('MinDateOffset'))) {
    104                 $dateField->setHTML5Attribute('min', date('Y-m-d', strtotime((($offset > 0) ? '+' : '') . "{$offset} day")));
    105             }
    106         }
    107         if ($this->getSetting('MaxDateType')) {
    108             if (($this->getSetting('MaxDateType') == 1) && $this->getSetting('MaxDate')) {
    109                 $dateField->setHTML5Attribute('max', $this->getSetting('MaxDate'));
    110             } elseif (($this->getSetting('MaxDateType') == 2) && ($offset = $this->getSetting('MaxDateOffset'))) {
    111                 $dateField->setHTML5Attribute('max', date('Y-m-d', strtotime((($offset > 0) ? '+' : '') . "{$offset} day")));
    112             }
    113         }
    114
    115         $timeField = $field->getTimeField();
    116         if ($this->getSetting('MinTimeType')) {
    117             if (($this->getSetting('MinTimeType') == 1) && $this->getSetting('MinTime')) {
    118                 $timeField->setHTML5Attribute('min', $this->getSetting('MinTime'));
    119             } elseif (($this->getSetting('MinTimeType') == 2) && ($offset = $this->getSetting('MinTimeOffset'))) {
    120                 $timeField->setHTML5Attribute('min', date('H:i', strtotime((($offset > 0) ? '+' : '') . "{$offset} minutes")));
    121             }
    122         }
    123         if ($this->getSetting('MaxTimeType')) {
    124             if (($this->getSetting('MaxTimeType') == 1) && $this->getSetting('MaxTime')) {
    125                 $timeField->setHTML5Attribute('max', $this->getSetting('MaxTime'));
    126             } elseif (($this->getSetting('MaxTimeType') == 2) && ($offset = $this->getSetting('MaxTimeOffset'))) {
    127                 $timeField->setHTML5Attribute('max', date('H:i', strtotime((($offset > 0) ? '+' : '') . "{$offset} minutes")));
    128             }
    129         }
    130         return $field;
    131     }
    132
    133     /*
    134
    135     function getFormField() {
    136         return new DateTimeField($this->Name, $this->Title);
    137     }
    138     */
    139
    140     public function getValueFromData($data) {
    141         if (isset($data[$this->Name]) && isset($data[$this->Name]['date'])) {
    142             $date = $data[$this->Name]['date'];
    143             if (isset($data[$this->Name]['time'])) {
    144                 $date .= ' ' . $data[$this->Name]['time'];
    145             }
    146             return $date;
    147         }
    148         return '';
    149         return (isset($data[$this->Name])) ? PhoneField::cleanup_phone($data[$this->Name]) : '';
    150     }
    151 }

[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. -