2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Date field.
|
|
|
|
* Default Value represented in the format
|
|
|
|
*/
|
|
|
|
class DateField extends TextField {
|
2007-11-23 02:10:19 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function setValue($val) {
|
2007-11-23 02:10:19 +01:00
|
|
|
if($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
|
|
|
}
|
|
|
|
}
|
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)) {
|
|
|
|
return $this->value['Year'] . '-' . $this->value['Month'] . '-' . $this->value['Day'];
|
|
|
|
} 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);
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
2007-08-23 07:47:54 +02:00
|
|
|
function jsValidation($formID = null)
|
2007-07-19 12:40:28 +02:00
|
|
|
{
|
2007-08-23 07:47:54 +02:00
|
|
|
if(!$formID)$formID = $this->form->FormName();
|
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}\$/)) {
|
|
|
|
validationError(el,"Please enter a valid date format (DD/MM/YYYY).","validation",false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
JS;
|
|
|
|
Requirements :: customScript($jsFunc, 'func_validateDate');
|
|
|
|
|
2007-08-23 07:47:54 +02:00
|
|
|
// return "\$('$formID').validateDate('$this->name');";
|
|
|
|
return <<<JS
|
|
|
|
if(typeof fromAnOnBlur != 'undefined'){
|
|
|
|
if(fromAnOnBlur.name == '$this->name')
|
|
|
|
$('$formID').validateDate('$this->name');
|
|
|
|
}else{
|
|
|
|
$('$formID').validateDate('$this->name');
|
|
|
|
}
|
|
|
|
JS;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-08-23 07:47:54 +02:00
|
|
|
function validate($validator)
|
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,
|
|
|
|
_t('DateField.VALIDDATEFORMAT', "Please enter a valid date format (DD/MM/YYYY)."),
|
|
|
|
"validation",
|
|
|
|
false
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows dates to be represented in a form, by
|
|
|
|
* showing in a user friendly format, eg, dd/mm/yyyy.
|
|
|
|
*/
|
|
|
|
class DateField_Disabled extends DateField {
|
|
|
|
|
|
|
|
function setValue($val) {
|
|
|
|
if($val && $val != "0000-00-00") $this->value = date('d/m/Y', strtotime($val));
|
|
|
|
else $this->value = "(No date set)";
|
|
|
|
}
|
|
|
|
|
|
|
|
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 . ' (today)');
|
|
|
|
} else {
|
|
|
|
$val = Convert::raw2xml($this->value . ', ' . $df->Ago());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$val = '<i>(not set)</i>';
|
|
|
|
}
|
|
|
|
|
|
|
|
return "<span class=\"readonly\" id=\"" . $this->id() . "\">$val</span>";
|
|
|
|
}
|
|
|
|
|
|
|
|
function Type() {
|
|
|
|
return "date_disabled readonly";
|
|
|
|
}
|
|
|
|
|
|
|
|
function jsValidation() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function php() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|