2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-01-09 05:18:36 +01:00
|
|
|
/**
|
|
|
|
* Date field that provides 3 dropdowns for entering a date
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-datetime
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class CompositeDateField extends DateField {
|
|
|
|
|
|
|
|
function __construct($name, $title, $value = null, $yearRange = null){
|
2008-11-10 04:51:35 +01:00
|
|
|
$exploded = explode('-', $value);
|
|
|
|
$year = isset($exploded[0]) ? $exploded[0] : null;
|
|
|
|
$month = isset($exploded[1]) ? $exploded[1] : null;
|
|
|
|
$date = isset($exploded[2]) ? $exploded[2] : null;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
$this->dateDropdown = new DropdownField($name."[date]", "",
|
2008-01-10 04:28:13 +01:00
|
|
|
array('NotSet' => '('._t('CompositeDateField.DAY', 'Day').')',
|
2007-07-19 12:40:28 +02:00
|
|
|
'01'=>'01', '02'=>'02', '03'=>'03', '04'=>'04', '05'=>'05',
|
|
|
|
'06'=>'06', '07'=>'07', '08'=>'08', '09'=>'09', '10'=>'10',
|
|
|
|
'11'=>'11', '12'=>'12', '13'=>'13', '14'=>'14', '15'=>'15',
|
|
|
|
'16'=>'16', '17'=>'17', '18'=>'18', '19'=>'19', '20'=>'20',
|
|
|
|
'21'=>'21', '22'=>'22', '23'=>'23', '24'=>'24', '25'=>'25',
|
|
|
|
'26'=>'26', '27'=>'27', '28'=>'28', '29'=>'29', '30'=>'30',
|
|
|
|
'31'=>'31'
|
|
|
|
),
|
|
|
|
$date
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->monthDropdown = new DropdownField($name."[month]", "",
|
2008-01-10 04:28:13 +01:00
|
|
|
array( 'NotSet' => '('._t('CompositeDateField.MONTH', 'Month').')',
|
2007-07-19 12:40:28 +02:00
|
|
|
'01'=>'01', '02'=>'02', '03'=>'03', '04'=>'04', '05'=>'05',
|
|
|
|
'06'=>'06', '07'=>'07', '08'=>'08', '09'=>'09', '10'=>'10',
|
|
|
|
'11'=>'11', '12'=>'12'
|
|
|
|
),
|
|
|
|
$month
|
|
|
|
);
|
|
|
|
|
|
|
|
if($yearRange == null){
|
2008-09-23 03:44:40 +02:00
|
|
|
$this->customiseYearDropDown($name, "1995-2012", $year);
|
2007-07-19 12:40:28 +02:00
|
|
|
}else{
|
|
|
|
$this->customiseYearDropDown($name, $yearRange, $year);
|
|
|
|
}
|
|
|
|
parent::__construct($name, $title);
|
|
|
|
}
|
|
|
|
|
|
|
|
function Field() {
|
2008-04-06 06:08:45 +02:00
|
|
|
$this->dateDropdown->setTabIndex($this->getTabIndex());
|
|
|
|
$this->monthDropdown->setTabIndex($this->getTabIndex()+1);
|
|
|
|
$this->yearDropdown->setTabIndex($this->getTabIndex()+2);
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->dateDropdown->Field() . $this->monthDropdown->Field() . $this->yearDropdown->Field();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function performReadonlyTransformation() {
|
|
|
|
$field = new CompositeDateField_Disabled($this->name, $this->title, $this->value);
|
|
|
|
$field->setForm($this->form);
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
|
|
|
function customiseYearDropDown($name, $yearRange, $year){
|
|
|
|
list($from,$to) = explode('-', $yearRange);
|
|
|
|
$source['NotSet'] = '(Year)';
|
|
|
|
for($i = $to; $i >= $from; $i--){
|
|
|
|
$source[$i]=$i;
|
|
|
|
}
|
|
|
|
$this->yearDropdown = new DropdownField($name."[year]", "", $source, $year);
|
|
|
|
$this->yearDropdown->setValue($year);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setForm($form) {
|
|
|
|
$this->dateDropdown->setForm($form);
|
|
|
|
$this->monthDropdown->setForm($form);
|
|
|
|
$this->yearDropdown->setForm($form);
|
|
|
|
parent::setForm($form);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setValue($val) {
|
|
|
|
if($val) {
|
|
|
|
if(is_array($val)){
|
|
|
|
if($val['date'] == 'NotSet' || $val['month'] == 'NotSet' || $val['year'] == 'NotSet'){
|
|
|
|
$this->value = null; return;
|
|
|
|
}
|
|
|
|
$val = $val['year'] . '-' . $val['month'] . '-' . $val['date'];
|
|
|
|
}
|
|
|
|
$this->value = date('d/m/Y', strtotime($val));
|
|
|
|
list($year, $month, $date) = explode("-", $val);
|
|
|
|
$this->yearDropdown->setValue($year);
|
|
|
|
$this->monthDropdown->setValue($month);
|
|
|
|
$this->dateDropdown->setValue($date);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$this->value = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function jsValidation() {
|
|
|
|
$formID = $this->form->FormName();
|
2008-01-10 04:28:13 +01:00
|
|
|
$error1 = _t('CompositeDateField.VALIDATIONJS1', 'Please ensure you have set the');
|
|
|
|
$error2 = _t('CompositeDateField.VALIDATIONJS2', 'correctly.');
|
|
|
|
$day = _t('CompositeDateField.DAYJS', 'day');
|
|
|
|
$month = _t('CompositeDateField.MONTHJS', 'month');
|
|
|
|
$year = _t('CompositeDateField.YEARJS', 'year');
|
2007-07-19 12:40:28 +02:00
|
|
|
$jsFunc =<<<JS
|
|
|
|
Behaviour.register({
|
|
|
|
"#$formID": {
|
|
|
|
validateCompositeDateField: function(fieldName) {
|
|
|
|
var el = _CURRENT_FORM.elements[fieldName];
|
|
|
|
if(!el || !el.value) return true;
|
|
|
|
|
|
|
|
// Creditcards are split into multiple values, so get the inputs from the form.
|
|
|
|
dateParts = $(fieldName).getElementsByTagName('select');
|
|
|
|
|
|
|
|
// Concatenate the string values from the parts of the input.
|
|
|
|
for(i=0; i < dateParts.length ; i++ ){
|
|
|
|
// The default selected value is 'NotSet'
|
|
|
|
if(dateParts[i].value == 'NotSet'){
|
|
|
|
switch(i){
|
2008-01-10 04:28:13 +01:00
|
|
|
case 0: err = "$day"; break;
|
|
|
|
case 1: err = "$month"; break;
|
|
|
|
case 2: err = "$year"; break;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-01-10 04:28:13 +01:00
|
|
|
validationError(dateParts[i],"$error1 '" + err + "' $error2","validation");
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
JS;
|
|
|
|
|
|
|
|
Requirements::customScript($jsFunc, 'func_validateCompositeDateField');
|
|
|
|
|
|
|
|
return "\$('$formID').validateCompositeDateField('$this->name');";
|
|
|
|
}
|
|
|
|
|
|
|
|
function validate($validator) {
|
|
|
|
// TODO Implement server-side validation
|
|
|
|
if($this->value == null) {
|
2007-10-25 04:47:45 +02:00
|
|
|
$validator->validationError($this->name,_t('Form.VALIDATIONALLDATEVALUES',"Please ensure you have set all date values"),"validation");
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows dates to be represented in a form, by
|
|
|
|
* showing in a user friendly format, eg, dd/mm/yyyy.
|
2008-01-10 01:34:18 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-datetime
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class CompositeDateField_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) {
|
|
|
|
if($val && $val != "0000-00-00") $this->value = date('d/m/Y', strtotime($val));
|
2007-10-25 04:47:45 +02:00
|
|
|
else $this->value = _t('Form.DATENOTSET', "(No date set)");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-08-12 04:58:48 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function Field() {
|
|
|
|
if($this->value) {
|
|
|
|
$df = new Date($this->name);
|
|
|
|
$df->setValue($this->dataValue());
|
|
|
|
$val = Convert::raw2xml($this->value);
|
|
|
|
} else {
|
2007-10-25 04:47:45 +02:00
|
|
|
$val = '<i>' . _t('Form.NOTSET', '(not set)') . '</i>';
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return "<span class=\"readonly\" id=\"" . $this->id() . "\">$val</span>";
|
|
|
|
}
|
2008-08-12 04:58:48 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function Type() {
|
|
|
|
return "date_disabled readonly";
|
|
|
|
}
|
|
|
|
}
|
2008-11-10 04:51:35 +01:00
|
|
|
?>
|