silverstripe-framework/forms/DateField.php
Sean Harvey ee2771e99a BUGFIX Slice of extra "OO" if SQL server attempts to set value in DateField
BUGFIX Don't attempt to use JS validation on DateField is javascript handler is "none"


git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@78065 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-05-28 04:06:14 +00:00

177 lines
4.5 KiB
PHP
Executable File

<?php
/**
* Date field.
* Default Value represented in the format
*
* @todo Add localization support, see http://open.silverstripe.com/ticket/2931
*
* @package forms
* @subpackage fields-datetime
*/
class DateField extends TextField {
/**
* Enable DD/MM/YYYY field format validation
* in {@link DateField->validate()}. Set to
* FALSE to disable this validation.
*
* @var boolean
*/
public static $validation_enabled = true;
function setValue($val) {
if ($val) {
if (!is_array($val) ) { //&& strlen($val) == 26) {
$ampm = substr($val,strlen($val)-2,strlen($val));
if ($ampm == "PM") { //correct for pm offset when cutting off 12-hour clock format
$val = substr($val,0,strlen($val)-6)."PM";
} elseif ($ampm == "AM") {
$val = substr($val,0,strlen($val)-6);
}
}
}
if(is_string($val) && preg_match('/^([\d]{2,4})-([\d]{1,2})-([\d]{1,2})/', $val)) {
$this->value = preg_replace('/^([\d]{2,4})-([\d]{1,2})-([\d]{1,2})/','\\3/\\2/\\1', $val);
} else {
$this->value = $val;
}
}
function dataValue() {
if(is_array($this->value)) {
if(isset($this->value['Year']) && isset($this->value['Month']) && isset($this->value['Day'])) {
return $this->value['Year'] . '-' . $this->value['Month'] . '-' . $this->value['Day'];
} else {
user_error("Bad DateField value " . var_export($this->value,true), E_USER_WARNING);
}
} elseif(preg_match('/^([\d]{1,2})\/([\d]{1,2})\/([\d]{2,4})/', $this->value, $parts)) {
return "$parts[3]-$parts[2]-$parts[1]";
} elseif(!empty($this->value)) {
return date('Y-m-d', strtotime($this->value));
} else {
return null;
}
}
function performReadonlyTransformation() {
$field = new DateField_Disabled($this->name, $this->title, $this->value);
$field->setForm($this->form);
$field->readonly = true;
return $field;
}
function jsValidation() {
$formID = $this->form->FormName();
if(Validator::get_javascript_validator_handler() == 'none') {
return true;
}
$error = _t('DateField.VALIDATIONJS', 'Please enter a valid date format (DD/MM/YYYY).');
$jsFunc =<<<JS
Behaviour.register({
"#$formID": {
validateDate: function(fieldName) {
var el = _CURRENT_FORM.elements[fieldName];
var value = \$F(el);
if(value && value.length > 0 && !value.match(/^[0-9]{1,2}\/[0-9]{1,2}\/[0-90-9]{2,4}\$/)) {
validationError(el,"$error","validation",false);
return false;
}
return true;
}
}
});
JS;
Requirements :: customScript($jsFunc, 'func_validateDate_'.$formID);
// return "\$('$formID').validateDate('$this->name');";
return <<<JS
if(\$('$formID')){
if(typeof fromAnOnBlur != 'undefined'){
if(fromAnOnBlur.name == '$this->name')
\$('$formID').validateDate('$this->name');
}else{
\$('$formID').validateDate('$this->name');
}
}
JS;
}
function validate($validator) {
$validationHandler = Validator::get_javascript_validator_handler();
if(!self::$validation_enabled || $validationHandler == 'none') {
return true;
}
if(!empty ($this->value) && !preg_match('/^[0-9]{1,2}\/[0-9]{1,2}\/[0-90-9]{2,4}$/', $this->value))
{
$validator->validationError(
$this->name,
_t('DateField.VALIDDATEFORMAT', "Please enter a valid date format (DD/MM/YYYY)."),
"validation",
false
);
return false;
}
return true;
}
}
/**
* Disabled version of {@link DateField}.
* Allows dates to be represented in a form, by showing in a user friendly format, eg, dd/mm/yyyy.
* @package forms
* @subpackage fields-datetime
*/
class DateField_Disabled extends DateField {
protected $disabled = true;
function setValue($val) {
if(is_string($val) && preg_match('/^([\d]{2,4})-([\d]{1,2})-([\d]{1,2})/', $val)) {
$this->value = preg_replace('/^([\d]{2,4})-([\d]{1,2})-([\d]{1,2})/','\\3/\\2/\\1', $val);
} else {
$this->value = $val;
}
}
function Field() {
if($this->value) {
$df = new Date($this->name);
$df->setValue($this->dataValue());
if(date('Y-m-d', time()) == $this->dataValue()) {
$val = Convert::raw2xml($this->value . ' ('._t('DateField.TODAY','today').')');
} else {
$val = Convert::raw2xml($this->value . ', ' . $df->Ago());
}
} else {
$val = '<i>('._t('DateField.NOTSET', 'not set').')</i>';
}
return "<span class=\"readonly\" id=\"" . $this->id() . "\">$val</span>
<input type=\"hidden\" value=\"{$this->value}\" name=\"$this->name\" />";
}
function Type() {
return "date_disabled readonly";
}
function jsValidation() {
return null;
}
function php() {
return true;
}
function validate($validator) {
return true;
}
}
?>