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
|
|
|
|
2013-04-03 03:34:43 +02:00
|
|
|
private static $singular_name = 'Date Field';
|
2009-04-21 05:44:13 +02:00
|
|
|
|
2013-04-03 03:34:43 +02:00
|
|
|
private static $plural_name = 'Date Fields';
|
2008-09-29 05:18:23 +02:00
|
|
|
|
2012-05-04 03:39:08 +02:00
|
|
|
public 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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-05-04 03:39:08 +02:00
|
|
|
public 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
|
|
|
/**
|
2012-05-10 07:44:41 +02:00
|
|
|
* Return the form field
|
2009-04-21 05:44:13 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function getFormField() {
|
2012-05-10 07:44:41 +02:00
|
|
|
$defaultValue = ($this->getSetting('DefaultToToday')) ? date('Y-m-d') : $this->Default;
|
2014-12-22 02:49:33 +01:00
|
|
|
$field = EditableDateField_FormField::create( $this->Name, $this->Title, $defaultValue);
|
2012-05-10 07:44:41 +02:00
|
|
|
$field->setConfig('showcalendar', true);
|
2014-08-11 01:09:31 +02:00
|
|
|
|
2014-12-22 02:49:33 +01:00
|
|
|
if ($this->Required) {
|
|
|
|
// Required validation can conflict so add the Required validation messages
|
|
|
|
// as input attributes
|
|
|
|
$errorMessage = $this->getErrorMessage()->HTML();
|
|
|
|
$field->setAttribute('data-rule-required', 'true');
|
|
|
|
$field->setAttribute('data-msg-required', $errorMessage);
|
|
|
|
}
|
|
|
|
|
2012-05-10 07:44:41 +02:00
|
|
|
return $field;
|
2009-04-21 05:44:13 +02:00
|
|
|
}
|
2012-07-17 06:09:31 +02:00
|
|
|
}
|
2014-08-11 01:09:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package userforms
|
|
|
|
*/
|
|
|
|
class EditableDateField_FormField extends DateField {
|
|
|
|
|
|
|
|
public function Type() {
|
|
|
|
return "date-alt text";
|
|
|
|
}
|
|
|
|
}
|