Version: current

      1 <?php
      2 /**
      3  * EditableTimeField
      4  *
      5  * Allow users to define a validating time field for a UserDefinedForm
      6  *
      7  * @package userforms
      8  */
      9
     10 class EditableTimeField extends EditableFormField {
     11
     12     static $singular_name = 'Time Field';
     13
     14     static $plural_name = 'Time Fields';
     15
     16     function getFieldConfiguration() {
     17         $fields = parent::getFieldConfiguration();
     18
     19         $min = ($this->getSetting('MinTime')) ? $this->getSetting('MinTime') : '';
     20         $minOffset = ($this->getSetting('MinTimeOffset')) ? $this->getSetting('MinTimeOffset') : '';
     21         $minTimeField = ($this->getSetting('MinTimeField')) ? $this->getSetting('MinTimeField') : '';
     22         $someFieldMinOffset = ($this->getSetting('SomeFieldMinOffset')) ? $this->getSetting('SomeFieldMinOffset') : '';
     23
     24         $max = ($this->getSetting('MaxTime')) ? $this->getSetting('MaxTime') : '';
     25         $maxOffset = ($this->getSetting('MaxTimeOffset')) ? $this->getSetting('MaxTimeOffset') : '';
     26         $maxTimeField = ($this->getSetting('MaxTimeField')) ? $this->getSetting('MaxTimeField') : '';
     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         $minTimeType = ($this->getSetting('MinTimeType')) ? $this->getSetting('MinTimeType') : '';
     33         $maxTimeType = ($this->getSetting('MaxTimeType')) ? $this->getSetting('MaxTimeType') : '';
     34
     35         $fields->push(new CheckboxField($this->getSettingName("DefaultToToday"), $label, $default));
     36
     37         $TimeFields = new DataObjectSet();
     38         if ($allFields = $this->Parent()->Fields()) {
     39             foreach($allFields as $field) {
     40                 if (($field->ClassName == 'EditableTimeField') && ($field->Name != $this->Name)) {
     41                     $TimeFields->push($field);
     42                 }
     43             }
     44         }
     45
     46         $fields->push(new LiteralField('MinTimeTitle', _t('EditableTimeField.MINTIMETITLE')));
     47         $options = array(
     48             '0//' . _t('EditableTimeField.NOMINTIME', 'No Min Time') => new LiteralField('NOMINTIME', ''),
     49             '1//' . _t('EditableTimeField.MINTIME', 'Min Time') => new TimeField($this->getSettingName("MinTime"), '', $min),
     50             '2//' . _t('EditableTimeField.CurrentTimeOffset', 'Min Time Offset') => new NumericField($this->getSettingName("MinTimeOffset"), '', $minOffset),
     51         );
     52         if ($TimeFields->Count()) {
     53             $options['3//' . _t('EditableTimeField.SomeField', 'Some Field')] = new CompositeField(new FieldSet(new DropdownField($this->getSettingName("MinTimeField"), '', $TimeFields->map(), $minTimeField), new NumericField($this->getSettingName("SomeFieldMinOffset"), _t('EditableTimeField.SomeFieldOffset', 'Some Field Offset Title'), $someFieldMinOffset)));
     54         }
     55         $t = new SelectionGroup($this->getSettingName("MinTimeType"), $options);
     56         $t->setValue($minTimeType);
     57         $fields->push($t);
     58
     59         $fields->push(new LiteralField('MaxTimeTitle', _t('EditableTimeField.MAXTIMETITLE')));
     60         $options = array(
     61             '0//' . _t('EditableTimeField.NOMAXTIME', 'No Max Time') => new LiteralField('NOMAXTIME', ''),
     62             '1//' . _t('EditableTimeField.MAXTIME', 'Max Time') => new TimeField($this->getSettingName("MaxTime"), '', $max),
     63             '2//' . _t('EditableTimeField.CurrentTimeOffset', 'Max Time Offset') => new NumericField($this->getSettingName("MaxTimeOffset"), '', $maxOffset),
     64         );
     65         if ($TimeFields->Count()) {
     66             $options['3//' . _t('EditableTimeField.SomeField', 'Some Field')] = new CompositeField(new FieldSet(new DropdownField($this->getSettingName("MaxTimeField"), '', $TimeFields->map(), $maxTimeField), new NumericField($this->getSettingName("SomeFieldMaxOffset"),  _t('EditableTimeField.SomeFieldOffset', 'Some Field Offset Title'), $someFieldMaxOffset)));
     67         }
     68         $t = new SelectionGroup($this->getSettingName("MaxTimeType"), $options);
     69         $t->setValue($maxTimeType);
     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('H:i') : $this->Default;
     83         $field = new TimeField( $this->Name, $this->Title, $default);
     84         if ($this->getSetting('MinTimeType')) {
     85             if (($this->getSetting('MinTimeType') == 1) && $this->getSetting('MinTime')) {
     86                 $field->setHTML5Attribute('min', $this->getSetting('MinTime'));
     87             } elseif (($this->getSetting('MinTimeType') == 2) && ($offset = $this->getSetting('MinTimeOffset'))) {
     88                 $field->setHTML5Attribute('min', date('H:i', strtotime((($offset > 0) ? '+' : '') . "{$offset} minutes")));
     89             } elseif (($this->getSetting('MinTimeType') == 3) && ($minFieldID = $this->getSetting('MinTimeField')) && ($minFieldOffset = $this->getSetting('SomeFieldMinOffset'))) {
     90                 if (($fields = $this->Parent()->Fields()) && ($minField = $fields->find('ID', $minFieldID))) {
     91                     $field->addExtraAttribute('min_field', $minField);
     92                     $field->addExtraAttribute('min_field_offset', $minFieldOffset);
     93                 }
     94             }
     95         }
     96         if ($this->getSetting('MaxTimeType')) {
     97             if (($this->getSetting('MaxTimeType') == 1) && $this->getSetting('MaxTime')) {
     98                 $field->setHTML5Attribute('max', $this->getSetting('MaxTime'));
     99             } elseif (($this->getSetting('MaxTimeType') == 2) && ($offset = $this->getSetting('MaxTimeOffset'))) {
    100                 $field->setHTML5Attribute('max', date('H:i', strtotime((($offset > 0) ? '+' : '') . "{$offset} minutes")));
    101             } elseif (($this->getSetting('MaxTimeType') == 3) && ($maxFieldID = $this->getSetting('MaxTimeField')) && ($maxFieldOffset = $this->getSetting('SomeFieldMaxOffset'))) {
    102                 if (($fields = $this->Parent()->Fields()) && ($maxField = $fields->find('ID', $maxFieldID))) {
    103                     $field->addExtraAttribute('max_field', $maxField);
    104                     $field->addExtraAttribute('max_field_offset', $maxFieldOffset);
    105                 }
    106             }
    107         }
    108         return $field;
    109     }
    110 }

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