silverstripe-framework/forms/DateField.php
Normann Lou 26b8f7dffe switch externals to trunk.
Inform-merge: from the changeset:
r32477: Merge 2.0-inform from trunk previously
r32478: Merge 2.0-inform from trunk previously
r32481: merge 2.0infom with lastes chunk 
r32483: merge 2.0infom with lastes chunk 
r33526: Final styling of all forms and combined communication form add/alter javascript for height adjustment of First / Second block 
r33580: styling for combined form communication in myinfom pages 
r33706: styling of combined form (communication) in Email 
r33881: made compatible to $extraClass 
r33885: added defaultVal
r33887: fixed typo 
r34728: modified SmallFieldHolder?()
r34729: added "validationError"-class 
r34914: WIP3866: Factfinder: Hide "self emplyed" block 
r34964: Change current plan upto TraumaInsurance? 
r35038: disabled friggin field focus 
r35230: #1032 Fixed hash-link insertion 
r35887: conditionally setting parameters in sourceID() - to avoid empty overrides
r35892: Saving value in SQL-compatible format (YYYY-MM-DD instead of DD/MM/YYYY), with fallback for non-sql values (just passed through without conversion) 
r35928: Removed "create a" from PageType?-dropdown, sorting alphabetically, falling back to $singular_name
r35990: branched off for membertablefield
r35994: fix for membertablefield
r36024: added array-condition needed for DMYDateField 
r36083: fix bug for compositeField -> dropDatalessField
r36394: removed debug code 
r36826: change wrong indent format 
r36828: WIP 4262: Logging out of My Inform goes to blank page
r36858: Fixed error caused in r12472 while merging to Session-class
r37132: Merged partial changesets from branches/2.0-nzct, only adding childID to detailform when not in add-mode 
r40815: add an unsubscribe record when a member subscribe a newslettertype 
r41113: fix the bug described in http://support.silverstripe.com/info/ticket/31: CRM not showing search results
r43226: fixed search (partial merge from trunk) 
r43268: merged createNewPassword() from trunk, was referencing a non-existinent global function randomString()


git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@45473 467b73ca-7a2a-4603-9d3b-597d59a354a9
2007-11-23 01:10:19 +00:00

123 lines
2.9 KiB
PHP
Executable File

<?php
/**
* Date field.
* Default Value represented in the format
*/
class DateField extends TextField {
function setValue($val) {
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;
}
}
function dataValue() {
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;
}
}
function performReadonlyTransformation() {
$field = new DateField_Disabled($this->name, $this->title, $this->value);
$field->setForm($this->form);
return $field;
}
function jsValidation($formID = null)
{
if(!$formID)$formID = $this->form->FormName();
$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');
// 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;
}
function validate($validator)
{
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;
}
}
/**
* 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;
}
}
?>