Webylon 3.1 API Docs
  • Package
  • Class
  • Tree
  • Deprecated
  • Download
Version: current
  • 3.2
  • 3.1

Packages

  • auth
  • Booking
  • cart
    • shipping
    • steppedcheckout
  • Catalog
  • cms
    • assets
    • batchaction
    • batchactions
    • bulkloading
    • comments
    • content
    • core
    • export
    • newsletter
    • publishers
    • reports
    • security
    • tasks
  • Dashboard
  • DataObjectManager
  • event
  • faq
  • forms
    • actions
    • core
    • fields-basic
    • fields-dataless
    • fields-datetime
    • fields-files
    • fields-formatted
    • fields-formattedinput
    • fields-relational
    • fields-structural
    • transformations
    • validators
  • googlesitemaps
  • guestbook
  • installer
  • newsletter
  • None
  • photo
    • gallery
  • PHP
  • polls
  • recaptcha
  • sapphire
    • api
    • bulkloading
    • control
    • core
    • cron
    • dev
    • email
    • fields-formattedinput
    • filesystem
    • formatters
    • forms
    • i18n
    • integration
    • misc
    • model
    • parsers
    • search
    • security
    • tasks
    • testing
    • tools
    • validation
    • view
    • widgets
  • seo
    • open
      • graph
  • sfDateTimePlugin
  • spamprotection
  • stealth
    • captha
  • subsites
  • userform
    • pagetypes
  • userforms
  • webylon
  • widgets

Classes

  • DateField_View_JQuery
  • DatetimeField
  • FormScaffolder
  • NestedForm
  • RangeField
  1 <?php
  2 /**
  3  * A composite field for date and time entry,
  4  * based on {@link DateField} and {@link TimeField}.
  5  * Usually saves into a single {@link SS_Datetime} database column.
  6  * If you want to save into {@link Date} or {@link Time} columns,
  7  * please instanciate the fields separately.
  8  * 
  9  * # Configuration
 10  * 
 11  * All options in {@link setConfig()} are passed through to {@link DateField} and {@link TimeField}.
 12  * 
 13  * @package sapphire
 14  * @subpackage forms
 15  */
 16 class DatetimeField extends FormField {
 17     
 18     /**
 19      * @var DateField
 20      */
 21     protected $dateField = null;
 22     
 23     /**
 24      * @var TimeField
 25      */
 26     protected $timeField = null;
 27     
 28     /**
 29      * @var array
 30      */
 31     protected $config = array(
 32         'datavalueformat' => 'YYYY-MM-dd HH:mm:ss'
 33     );
 34         
 35     function __construct($name, $title = null, $value = ""){
 36         $this->dateField = new DateField($name . '[date]', false);
 37         $this->timeField = new TimeField($name . '[time]', false);
 38         
 39         parent::__construct($name, $title, $value);
 40     }
 41     
 42     function setForm($form) {
 43         parent::setForm($form);
 44         
 45         $this->dateField->setForm($form);
 46         $this->timeField->setForm($form);
 47     }
 48     
 49     function allowHTML5($val) {
 50         $this->dateField->allowHTML5($val);
 51         $this->timeField->allowHTML5($val);
 52     }
 53     
 54     function Field() {
 55         return $this->dateField->Field() . $this->timeField->Field();
 56     }
 57     
 58     /**
 59      * Sets the internal value to ISO date format.
 60      * 
 61      * @param string|array $val String expects an ISO date format. Array notation with 'date' and 'time'
 62      *  keys can contain localized strings. If the 'dmyfields' option is used for {@link DateField},
 63      *  the 'date' value may contain array notation was well (see {@link DateField->setValue()}).
 64      */
 65     function setValue($val) {
 66         if(empty($val)) {
 67             $this->dateField->setValue(null);
 68             $this->timeField->setValue(null);
 69         } else {
 70             // String setting is only possible from the database, so we don't allow anything but ISO format
 71             if(is_string($val) && Zend_Date::isDate($val, $this->getConfig('datavalueformat'), $this->locale)) {
 72                 // split up in date and time string values. 
 73                 $valueObj = new Zend_Date($val, $this->getConfig('datavalueformat'), $this->locale);
 74                 // set date either as array, or as string
 75                 if($this->dateField->getConfig('dmyfields')) {
 76                     $this->dateField->setValue($valueObj->toArray());
 77                 } else {
 78                     $this->dateField->setValue($valueObj->get($this->dateField->getConfig('dateformat')));
 79                 }
 80                 // set time
 81                 $this->timeField->setValue($valueObj->get($this->timeField->getConfig('timeformat')));
 82             }
 83             // Setting from form submission
 84             elseif(is_array($val) && array_key_exists('date', $val) && array_key_exists('time', $val)) {
 85                 $this->dateField->setValue($val['date']);
 86                 $this->timeField->setValue($val['time']);
 87             } else {
 88                 $this->dateField->setValue($val);
 89                 $this->timeField->setValue($val);
 90             }
 91         }
 92     }
 93     
 94     function dataValue() {
 95         $valDate = $this->dateField->dataValue();
 96         $valTime = $this->timeField->dataValue();
 97         
 98         if($valDate && $valTime) {
 99             return $valDate . ' ' . $valTime;
100         } else {
101             // TODO 
102             return null;
103         }
104     }
105     
106     /**
107      * @return DateField
108      */
109     function getDateField() {
110         return $this->dateField;
111     }
112     
113     /**
114      * @return TimeField
115      */
116     function getTimeField() {
117         return $this->timeField;
118     }
119     
120     function setLocale($locale) {
121         $this->dateField->setLocale($locale);
122         $this->timeField->setLocale($locale);
123     }
124     
125     function getLocale() {
126         return $this->dateField->getLocale();
127     }
128     
129     /**
130      * Note: Use {@link getDateField()} and {@link getTimeField()}
131      * to set field-specific config options.
132      * 
133      * @param string $name
134      * @param mixed $val
135      */
136     function setConfig($name, $val) {
137         $this->config[$name] = $val;
138     }
139     
140     /**
141      * Note: Use {@link getDateField()} and {@link getTimeField()}
142      * to get field-specific config options.
143      * 
144      * @param String $name
145      * @return mixed
146      */
147     function getConfig($name) {
148         return $this->config[$name];
149     }
150     
151     function validate($validator) {
152         $dateValid = $this->dateField->validate($validator);
153         $timeValid = $this->timeField->validate($validator);
154         
155         return ($dateValid && $timeValid);
156     }
157     
158     function jsValidation() {
159         return $this->dateField->jsValidation() . $this->timeField->jsValidation();
160     }
161     
162     function performReadonlyTransformation() {
163         $field = new DatetimeField_Readonly($this->name, $this->title, $this->dataValue());
164         $field->setForm($this->form);
165         
166         return $field;
167     }
168 }
169 
170 /**
171  * The readonly class for our {@link DatetimeField}.
172  * 
173  * @package forms
174  * @subpackage fields-datetime
175  */
176 class DatetimeField_Readonly extends DatetimeField {
177     
178     protected $readonly = true;
179         
180     function Field() {
181         $valDate = $this->dateField->dataValue();
182         $valTime = $this->timeField->dataValue();
183         if($valDate && $valTime) {
184             $format = $this->dateField->getConfig('dateformat') . ' ' . $this->timeField->getConfig('timeformat');
185             $valueObj = new Zend_Date(
186                 $valDate . ' ' . $valTime, 
187                 $this->getConfig('datavalueformat'), 
188                 $this->dateField->getLocale()
189             );
190             $val = $valueObj->toString($format);
191         } else {
192             // TODO Localization
193             $val = '<i>(not set)</i>';
194         }
195         
196         return "<span class=\"readonly\" id=\"" . $this->id() . "\">$val</span>";
197     }
198     
199     function jsValidation() {
200         return null;
201     }
202     
203     function validate($validator) {
204         return true;    
205     }
206 }
207 ?>
[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. -
Webylon 3.1 API Docs API documentation generated by ApiGen 2.8.0