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
  • DateField_Disabled
  • DatePickerField
  • DatetimeField_Readonly
  • TimeField
  • TimeField_Readonly
  1 <?php
  2 require_once 'Zend/Date.php';
  3 
  4 /**
  5  * Form field to display editable time values in an <input type="text">
  6  * field. Can optionally display a dropdown with predefined time ranges
  7  * through `setConfig('showdropdown', true)`.
  8  * 
  9  * # Configuration
 10  * 
 11  * - 'timeformat' (string): Time format compatible with Zend_Date.
 12  *    Usually set to default format for {@link locale}
 13  *    through {@link Zend_Locale_Format::getTimeFormat()}.
 14  * - 'use_strtotime' (boolean): Accept values in PHP's built-in strtotime() notation, in addition
 15  *    to the format specified in `timeformat`. Example inputs: 'now', '11pm', '23:59:59'.
 16  * - 'showdropdown': Show a dropdown with suggested date values.
 17  *    CAUTION: The dropdown does not support localization.
 18  * 
 19  * # Localization
 20  * 
 21  * See {@link DateField}
 22  * 
 23  * @todo Timezone support
 24  * @todo 'showdropdown' localization
 25  *
 26  * @package forms
 27  * @subpackage fields-datetime
 28  */
 29 class TimeField extends TextField {
 30 
 31     protected $config = array(
 32         'showdropdown' => false,
 33         'timeformat' => 'HH:mm:ss',
 34         'use_strtotime' => true,
 35         'datavalueformat' => 'HH:mm:ss'
 36     );
 37 
 38     /**
 39      * @var String
 40      */
 41     protected $locale = null;
 42     
 43     /**
 44      * @var Zend_Date Just set if the date is valid.
 45      * {@link $value} will always be set to aid validation,
 46      * and might contain invalid values.
 47      */
 48     protected $valueObj = null;
 49         
 50     function __construct($name, $title = null, $value = ""){
 51         if(!$this->locale) {
 52             $this->locale = i18n::get_locale();
 53         }
 54         
 55         if(!$this->getConfig('timeformat')) {
 56             $this->setConfig('timeformat', Zend_Locale_Format::getDateFormat($this->locale));
 57         }
 58         $this->setHTML5Attribute('type', 'time');
 59         
 60         parent::__construct($name,$title,$value);
 61     }
 62     
 63     function Field() {
 64         $html = parent::Field();
 65         
 66         $html = $this->FieldDriver($html);
 67         
 68         return $html;
 69     }
 70     
 71     /**
 72      * Internal volatile API.
 73      * 
 74      * @see DateField->FieldDriver()
 75      */
 76     protected function FieldDriver($html) {
 77         if($this->getConfig('showdropdown')) {
 78             Requirements::javascript(SAPPHIRE_DIR . '/javascript/TimeField_dropdown.js');
 79             Requirements::css(SAPPHIRE_DIR . '/css/TimeField_dropdown.css');
 80             
 81             $html .= sprintf('<img src="sapphire/images/clock-icon.gif" id="%s-icon"/>', $this->id());
 82             $html .= sprintf('<div class="dropdownpopup" id="%s-dropdowntime"></div>', $this->id());
 83             $html = '<div class="dropdowntime">' . $html . '</div>';
 84         }
 85         
 86         return $html;
 87     }
 88     
 89     /**
 90      * Sets the internal value to ISO date format.
 91      * 
 92      * @param String|Array $val
 93      */
 94     function setValue($val) {
 95         // Fuzzy matching through strtotime() to support a wider range of times,
 96         // e.g. 11am. This means that validate() might not fire.
 97         // Note: Time formats are assumed to be less ambiguous than dates across locales.
 98         if($this->getConfig('use_strtotime') && !empty($val)) {
 99             if($parsedTimestamp = strtotime($val)) {
100                 $parsedObj = new Zend_Date($parsedTimestamp, Zend_Date::TIMESTAMP);
101                 $val = $parsedObj->get($this->getConfig('timeformat'));
102                 unset($parsedObj);
103             }
104         }
105 
106         if(empty($val)) {
107             $this->value = null;
108             $this->valueObj = null;
109         }
110         // load ISO time from database (usually through Form->loadDataForm())
111         else if(Zend_Date::isDate($val, $this->getConfig('datavalueformat'))) {
112             $this->valueObj = new Zend_Date($val, $this->getConfig('datavalueformat'));
113             $this->value = $this->valueObj->get($this->getConfig('timeformat'));
114         }
115         // Set in current locale (as string)
116         else if(Zend_Date::isDate($val, $this->getConfig('timeformat'), $this->locale)) {
117             $this->valueObj = new Zend_Date($val, $this->getConfig('timeformat'), $this->locale);
118             $this->value = $this->valueObj->get($this->getConfig('timeformat'));
119         }
120         // Fallback: Set incorrect value so validate() can pick it up
121         else {
122             $this->value = $val;
123             $this->valueObj = null;
124         } 
125     }
126     
127     /**
128      * @return String ISO 8601 date, suitable for insertion into database
129      */
130     function dataValue() {
131         if($this->valueObj) {
132             return $this->valueObj->toString($this->getConfig('datavalueformat'));
133         } else {
134             return null;
135         }
136     }
137 
138     /**
139      * @return Boolean
140      */
141     function validate($validator) {
142         $valid = true;
143         
144         // Don't validate empty fields
145         if(empty($this->value)) return true;
146         
147         if(!Zend_Date::isDate($this->value, $this->getConfig('timeformat'), $this->locale)) {
148             $validator->validationError(
149                 $this->name, 
150                 _t('DateField.VALIDDATEFORMAT', "Please enter a valid time format."), 
151                 "validation", 
152                 false
153             );
154             return false;
155         }
156         return true;
157     }
158     
159     /**
160      * @return string
161      */
162     function getLocale() {
163         return $this->locale;
164     }
165     
166     /**
167      * @param String $locale
168      */
169     function setLocale($locale) {
170         $this->locale = $locale;
171     }
172     
173     /**
174      * @param string $name
175      * @param mixed $val
176      */
177     function setConfig($name, $val) {       
178         $this->config[$name] = $val;
179     }
180     
181     /**
182      * @param String $name
183      * @return mixed
184      */
185     function getConfig($name) {
186         return $this->config[$name];
187     }
188         
189     /**
190      * Creates a new readonly field specified below
191      */
192     function performReadonlyTransformation() {
193         return new TimeField_Readonly($this->name, $this->title, $this->dataValue(), $this->getConfig('timeformat'));
194     }
195     
196 }
197 
198 /**
199  * The readonly class for our {@link TimeField}.
200  * 
201  * @package forms
202  * @subpackage fields-datetime
203  */
204 class TimeField_Readonly extends TimeField {
205     
206     protected $readonly = true;
207     
208     function Field() {
209         if($this->valueObj) {
210             $val = Convert::raw2xml($this->valueObj->toString($this->getConfig('timeformat')));
211         } else {
212             // TODO Localization
213             $val = '<i>(not set)</i>';
214         }
215         
216         return "<span class=\"readonly\" id=\"" . $this->id() . "\">$val</span>";
217     }
218     
219     function jsValidation() {
220         return null;
221     }
222     
223     function validate($validator) {
224         return true;    
225     }
226 }
227 ?>
[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