2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Displays a date field with day, month and year boxes, with a calendar to select
|
2009-04-02 19:17:04 +02:00
|
|
|
* the date.
|
|
|
|
*
|
|
|
|
* @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 DMYCalendarDateField extends CalendarDateField {
|
|
|
|
|
|
|
|
function Field() {
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
Requirements::javascript(THIRDPARTY_DIR . "/calendar/calendar.js");
|
|
|
|
Requirements::javascript(THIRDPARTY_DIR . "/calendar/lang/calendar-en.js");
|
|
|
|
Requirements::javascript(THIRDPARTY_DIR . "/calendar/calendar-setup.js");
|
|
|
|
Requirements::css(SAPPHIRE_DIR . "/css/CalendarDateField.css");
|
|
|
|
Requirements::css(THIRDPARTY_DIR . "/calendar/calendar-win2k-1.css");
|
2009-07-17 01:52:35 +02:00
|
|
|
Requirements::javascript(SAPPHIRE_DIR . "/javascript/NumericField.js");
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
Requirements::javascript(SAPPHIRE_DIR . "/javascript/CalendarDateField.js");
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
$field = DateField::Field();
|
|
|
|
|
|
|
|
$id = $this->id();
|
|
|
|
$val = $this->attrValue();
|
|
|
|
|
2008-09-23 03:44:40 +02:00
|
|
|
$day = $month = $year = null;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if( preg_match( '/^\d{2}\/\d{2}\/\d{4}$/', $val ) ) {
|
|
|
|
$dateArray = explode( '/', $val );
|
|
|
|
$val = $dateArray[2] . '-' . $dateArray[1] . '-' . $dateArray[0];
|
|
|
|
}
|
|
|
|
|
2008-09-23 03:44:40 +02:00
|
|
|
if($val) {
|
|
|
|
$dateArray = explode( '-', $val );
|
|
|
|
$day = $dateArray[2];
|
|
|
|
$month = $dateArray[1];
|
|
|
|
$year = $dateArray[0];
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-09-23 03:44:40 +02:00
|
|
|
if(preg_match('/(.*)[(.+)]$/', $this->name, $fieldNameParts)) {
|
|
|
|
$fieldNamePrefix = $fieldNameParts[1];
|
|
|
|
$fieldName = $fieldNameParts[2];
|
|
|
|
} else {
|
|
|
|
$fieldNamePrefix = $this->name;
|
|
|
|
$fieldName = $this->name;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
return <<<HTML
|
|
|
|
<div class="dmycalendardate">
|
|
|
|
<input type="hidden" id="$id" name="{$this->name}" value="$val" />
|
2008-09-23 03:44:40 +02:00
|
|
|
<input type="text" id="$id-day" class="day numeric" name="{$fieldNamePrefix}[Day]" value="$day" maxlength="2" />/
|
|
|
|
<input type="text" id="$id-month" class="month numeric" name="{$fieldNamePrefix}[Month]" value="$month" maxlength="2" />/
|
|
|
|
<input type="text" id="$id-year" class="year numeric" name="{$fieldNamePrefix}[Year]" value="$year" maxlength="4" />
|
2007-07-19 12:40:28 +02:00
|
|
|
<div class="calendarpopup" id="{$id}-calendar"></div>
|
|
|
|
</div>
|
|
|
|
HTML;
|
|
|
|
}
|
2010-01-28 06:46:48 +01:00
|
|
|
|
|
|
|
function jsValidation() {
|
2010-01-29 02:44:56 +01:00
|
|
|
if(Validator::get_javascript_validator_handler() == 'none') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2010-01-28 06:46:48 +01:00
|
|
|
$formID = $this->form->FormName();
|
|
|
|
$error = _t('DateField.VALIDATIONJS', 'Please enter a valid date format (DD/MM/YYYY).');
|
|
|
|
$error = 'Please enter a valid date format (DD/MM/YYYY) from dmy.';
|
|
|
|
$jsFunc =<<<JS
|
|
|
|
Behaviour.register({
|
|
|
|
"#$formID": {
|
|
|
|
validateDMYDate: function(fieldName) {
|
|
|
|
var day_value = \$F(_CURRENT_FORM.elements[fieldName+'[Day]']);
|
|
|
|
var month_value = \$F(_CURRENT_FORM.elements[fieldName+'[Month]']);
|
|
|
|
var year_value = \$F(_CURRENT_FORM.elements[fieldName+'[Year]']);
|
|
|
|
|
2010-01-29 02:44:56 +01:00
|
|
|
var value = day_value + '/' + month_value + '/' + year_value;
|
|
|
|
|
|
|
|
if(value && value.length > 0 && !value.match(/^[0-9]{1,2}\/[0-9]{1,2}\/([0-9][0-9]){1,2}\$/)) {
|
|
|
|
validationError(_CURRENT_FORM.elements[fieldName+'[Day]'],"$error","validation",false);
|
|
|
|
return false;
|
2010-01-28 06:46:48 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
JS;
|
|
|
|
Requirements :: customScript($jsFunc, 'func_validateDMYDate_'.$formID);
|
|
|
|
|
|
|
|
// return "\$('$formID').validateDate('$this->name');";
|
|
|
|
return <<<JS
|
|
|
|
if(\$('$formID')){
|
|
|
|
if(typeof fromAnOnBlur != 'undefined'){
|
|
|
|
if(fromAnOnBlur.name == '$this->name')
|
|
|
|
\$('$formID').validateDMYDate('$this->name');
|
|
|
|
}else{
|
|
|
|
\$('$formID').validateDMYDate('$this->name');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
JS;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
?>
|