2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2009-04-02 19:17:04 +02:00
|
|
|
* @todo Add localization support, see http://open.silverstripe.com/ticket/2931
|
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-datetime
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class TimeField extends TextField {
|
2009-06-16 08:39:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string $timeformat Time description compatible with date() syntax.
|
|
|
|
*/
|
|
|
|
protected $timeformat = "g:ia";
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor saves the format difference. Timefields shouldn't
|
|
|
|
* have a problem with length as times can only be represented in on way.
|
2009-06-16 08:39:57 +02:00
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @param $name string The name of the field
|
|
|
|
* @param $title string The Title of the field
|
|
|
|
* @param $value string the value for the field
|
|
|
|
* @param $timeformat string The Time format in date php format e.g. G:ia
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-06-16 08:39:57 +02:00
|
|
|
function __construct($name, $title = null, $value = "",$timeformat = null){
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::__construct($name,$title,$value);
|
2009-06-16 08:39:57 +02:00
|
|
|
|
|
|
|
if($timeformat) $this->timeformat = $timeformat;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2009-06-16 08:39:57 +02:00
|
|
|
function dataValue() {
|
2009-06-16 09:03:17 +02:00
|
|
|
if($this->value) {
|
|
|
|
return date($this->timeformat,strtotime($this->value));
|
|
|
|
} else {
|
|
|
|
return $this->value;
|
|
|
|
}
|
2009-06-16 08:39:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function setValue($val) {
|
2009-06-16 09:03:17 +02:00
|
|
|
if($val) {
|
|
|
|
$this->value = date($this->timeformat,strtotime($val));
|
|
|
|
} else {
|
|
|
|
$this->value = $val;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new readonly field specified below
|
|
|
|
*/
|
|
|
|
function performReadonlyTransformation() {
|
|
|
|
return new TimeField_Readonly( $this->name, $this->title, $this->dataValue(),$this->timeformat);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-01-10 01:34:18 +01:00
|
|
|
* The readonly class for our {@link TimeField}.
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-datetime
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class TimeField_Readonly extends TimeField {
|
2007-11-06 21:43:19 +01:00
|
|
|
|
2008-08-12 04:58:48 +02:00
|
|
|
protected $readonly = true;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function Field() {
|
2007-11-06 21:43:19 +01:00
|
|
|
if( $this->value )
|
|
|
|
$val = $this->attrValue();
|
|
|
|
else
|
|
|
|
$val = '<i>(not set)</i>';
|
|
|
|
|
|
|
|
return "<span class=\"readonly\" id=\"" . $this->id() . "\">$val</span>";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|