2008-09-29 05:18:23 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* EditableDateField
|
2009-04-17 04:26:40 +02:00
|
|
|
*
|
2012-04-14 08:36:50 +02:00
|
|
|
* Allows a user to add a date field.
|
2010-05-31 09:57:00 +02:00
|
|
|
*
|
2009-04-17 04:26:40 +02:00
|
|
|
* @package userforms
|
2008-09-29 05:18:23 +02:00
|
|
|
*/
|
2009-12-07 03:04:20 +01:00
|
|
|
|
2008-09-29 05:18:23 +02:00
|
|
|
class EditableDateField extends EditableFormField {
|
2009-04-21 05:44:13 +02:00
|
|
|
|
2009-05-06 05:34:40 +02:00
|
|
|
static $singular_name = 'Date Field';
|
2009-04-21 05:44:13 +02:00
|
|
|
|
2009-05-06 05:34:40 +02:00
|
|
|
static $plural_name = 'Date Fields';
|
2008-09-29 05:18:23 +02:00
|
|
|
|
2010-05-31 11:43:25 +02:00
|
|
|
function getFieldConfiguration() {
|
2012-04-14 08:36:50 +02:00
|
|
|
$default = ($this->getSetting('DefaultToToday')) ? $this->getSetting('DefaultToToday') : false;
|
|
|
|
$label = _t('EditableFormField.DEFAULTTOTODAY', 'Default to Today?');
|
2010-11-16 00:23:11 +01:00
|
|
|
|
2012-04-22 21:17:42 +02:00
|
|
|
return new FieldList(
|
2012-04-14 08:36:50 +02:00
|
|
|
new CheckboxField($this->getSettingName("DefaultToToday"), $label, $default)
|
2010-05-31 11:43:25 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
function populateFromPostData($data) {
|
2008-09-29 05:18:23 +02:00
|
|
|
$fieldPrefix = 'Default-';
|
|
|
|
|
2009-05-06 05:34:40 +02:00
|
|
|
if(empty($data['Default']) && !empty($data[$fieldPrefix.'Year']) && !empty($data[$fieldPrefix.'Month']) && !empty($data[$fieldPrefix.'Day'])) {
|
|
|
|
$data['Default'] = $data['Year'] . '-' . $data['Month'] . '-' . $data['Day'];
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::populateFromPostData($data);
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
|
|
|
|
2009-04-21 05:44:13 +02:00
|
|
|
/**
|
|
|
|
* Return the form field.
|
|
|
|
*
|
|
|
|
* @todo Make a jQuery safe form field. The current CalendarDropDown
|
|
|
|
* breaks on the front end.
|
|
|
|
*/
|
|
|
|
public function getFormField() {
|
2010-05-31 09:57:00 +02:00
|
|
|
// scripts for jquery date picker
|
|
|
|
Requirements::javascript(THIRDPARTY_DIR .'/jquery-ui/jquery.ui.core.js');
|
|
|
|
Requirements::javascript(THIRDPARTY_DIR .'/jquery-ui/jquery.ui.datepicker.js');
|
2010-11-12 04:28:42 +01:00
|
|
|
|
2010-11-16 00:23:11 +01:00
|
|
|
$dateFormat = DateField_View_JQuery::convert_iso_to_jquery_format(i18n::get_date_format());
|
|
|
|
|
2010-05-31 09:57:00 +02:00
|
|
|
Requirements::customScript(<<<JS
|
|
|
|
(function(jQuery) {
|
|
|
|
$(document).ready(function() {
|
2010-11-16 00:23:11 +01:00
|
|
|
$('input[name^=EditableDateField]').attr('autocomplete', 'off').datepicker({ dateFormat: '$dateFormat' });
|
2010-05-31 09:57:00 +02:00
|
|
|
});
|
|
|
|
})(jQuery);
|
|
|
|
JS
|
|
|
|
, 'UserFormsDate');
|
|
|
|
|
|
|
|
// css for jquery date picker
|
|
|
|
Requirements::css(THIRDPARTY_DIR .'/jquery-ui-themes/smoothness/jquery-ui-1.8rc3.custom.css');
|
|
|
|
|
2010-05-31 11:43:25 +02:00
|
|
|
$default = ($this->getSetting('DefaultToToday')) ? date('d/m/Y') : $this->Default;
|
|
|
|
|
|
|
|
return new DateField( $this->Name, $this->Title, $default);
|
2009-04-21 05:44:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the validation information related to this field. This is
|
|
|
|
* interrupted as a JSON object for validate plugin and used in the
|
|
|
|
* PHP.
|
|
|
|
*
|
|
|
|
* @see http://docs.jquery.com/Plugins/Validation/Methods
|
|
|
|
* @return Array
|
|
|
|
*/
|
|
|
|
public function getValidation() {
|
|
|
|
return array(
|
|
|
|
'date' => true
|
|
|
|
);
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
2009-12-07 03:04:20 +01:00
|
|
|
}
|