2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Date field.
|
|
|
|
* Default Value represented in the format
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-datetime
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class DateField extends TextField {
|
2007-11-23 02:10:19 +01:00
|
|
|
|
2009-03-10 03:32:42 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function setValue($val) {
|
2008-09-23 03:44:40 +02:00
|
|
|
if(is_string($val) && preg_match('/^([\d]{2,4})-([\d]{1,2})-([\d]{1,2})/', $val)) {
|
2007-11-23 02:10:19 +01:00
|
|
|
$this->value = preg_replace('/^([\d]{2,4})-([\d]{1,2})-([\d]{1,2})/','\\3/\\2/\\1', $val);
|
|
|
|
} else {
|
|
|
|
$this->value = $val;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-11-23 02:10:19 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function dataValue() {
|
2007-11-23 02:10:19 +01:00
|
|
|
if(is_array($this->value)) {
|
2008-09-23 03:44:40 +02:00
|
|
|
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);
|
|
|
|
}
|
2007-11-23 02:10:19 +01:00
|
|
|
} 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;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-11-23 02:10:19 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function performReadonlyTransformation() {
|
|
|
|
$field = new DateField_Disabled($this->name, $this->title, $this->value);
|
|
|
|
$field->setForm($this->form);
|
2008-08-12 04:58:48 +02:00
|
|
|
$field->readonly = true;
|
2007-07-19 12:40:28 +02:00
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
2008-10-08 04:00:12 +02:00
|
|
|
function jsValidation() {
|
|
|
|
$formID = $this->form->FormName();
|
2008-01-10 04:28:13 +01:00
|
|
|
$error = _t('DateField.VALIDATIONJS', 'Please enter a valid date format (DD/MM/YYYY).');
|
2007-07-19 12:40:28 +02:00
|
|
|
$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}\$/)) {
|
2008-01-10 04:28:13 +01:00
|
|
|
validationError(el,"$error","validation",false);
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
JS;
|
2008-10-30 02:02:57 +01:00
|
|
|
Requirements :: customScript($jsFunc, 'func_validateDate_'.$formID);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2007-08-23 07:47:54 +02:00
|
|
|
// return "\$('$formID').validateDate('$this->name');";
|
|
|
|
return <<<JS
|
2008-10-30 02:02:57 +01:00
|
|
|
if(\$('$formID')){
|
|
|
|
if(typeof fromAnOnBlur != 'undefined'){
|
|
|
|
if(fromAnOnBlur.name == '$this->name')
|
|
|
|
\$('$formID').validateDate('$this->name');
|
|
|
|
}else{
|
|
|
|
\$('$formID').validateDate('$this->name');
|
|
|
|
}
|
2007-08-23 07:47:54 +02:00
|
|
|
}
|
|
|
|
JS;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2009-03-10 03:32:42 +01:00
|
|
|
function validate($validator) {
|
|
|
|
if(!self::$validation_enabled) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!empty ($this->value) && !preg_match('/^[0-9]{1,2}\/[0-9]{1,2}\/[0-90-9]{2,4}$/', $this->value))
|
|
|
|
{
|
2007-10-25 04:47:45 +02:00
|
|
|
$validator->validationError(
|
|
|
|
$this->name,
|
2008-10-08 04:00:12 +02:00
|
|
|
_t('DateField.VALIDDATEFORMAT', "Please enter a valid date format (DD/MM/YYYY)."),
|
2007-10-25 04:47:45 +02:00
|
|
|
"validation",
|
|
|
|
false
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-01-10 01:34:18 +01:00
|
|
|
* 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
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class DateField_Disabled extends DateField {
|
|
|
|
|
2008-08-12 04:58:48 +02:00
|
|
|
protected $disabled = true;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function setValue($val) {
|
2008-12-04 23:38:32 +01:00
|
|
|
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;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function Field() {
|
|
|
|
if($this->value) {
|
|
|
|
$df = new Date($this->name);
|
|
|
|
$df->setValue($this->dataValue());
|
|
|
|
|
|
|
|
if(date('Y-m-d', time()) == $this->dataValue()) {
|
2009-02-02 00:49:53 +01:00
|
|
|
$val = Convert::raw2xml($this->value . ' ('._t('DateField.TODAY','today').')');
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
|
|
|
$val = Convert::raw2xml($this->value . ', ' . $df->Ago());
|
|
|
|
}
|
|
|
|
} else {
|
2009-02-02 00:49:53 +01:00
|
|
|
$val = '<i>('._t('DateField.NOTSET', 'not set').')</i>';
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
return "<span class=\"readonly\" id=\"" . $this->id() . "\">$val</span>
|
|
|
|
<input type=\"hidden\" value=\"{$this->value}\" name=\"$this->name\" />";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function Type() {
|
|
|
|
return "date_disabled readonly";
|
|
|
|
}
|
|
|
|
|
|
|
|
function jsValidation() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function php() {
|
|
|
|
return true;
|
|
|
|
}
|
2008-10-08 04:00:12 +02:00
|
|
|
|
|
|
|
function validate($validator) {
|
|
|
|
return true;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
?>
|