2008-09-29 05:18:23 +02:00
|
|
|
<?php
|
2017-08-09 01:55:09 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\UserForms\Model\EditableFormField;
|
|
|
|
|
|
|
|
use SilverStripe\Forms\CheckboxField;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Forms\FieldList;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField;
|
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableDateField\FormField;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2008-09-29 05:18:23 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
2016-07-21 07:53:59 +02:00
|
|
|
class EditableDateField extends EditableFormField
|
|
|
|
{
|
|
|
|
private static $singular_name = 'Date Field';
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
private static $plural_name = 'Date Fields';
|
2015-07-24 04:37:48 +02:00
|
|
|
|
2017-04-28 10:31:51 +02:00
|
|
|
private static $has_placeholder = true;
|
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $db = [
|
2016-07-21 07:53:59 +02:00
|
|
|
'DefaultToToday' => 'Boolean' // From customsettings
|
2017-08-11 01:33:06 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
private static $table_name = 'EditableDateField';
|
2015-07-24 04:37:48 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
|
|
|
public function getCMSFields()
|
|
|
|
{
|
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) {
|
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Main',
|
|
|
|
CheckboxField::create(
|
|
|
|
'DefaultToToday',
|
2017-08-11 01:33:06 +02:00
|
|
|
_t('SilverStripe\\UserForms\\Model\\EditableFormField.DEFAULTTOTODAY', 'Default to Today?')
|
2016-07-21 07:53:59 +02:00
|
|
|
),
|
|
|
|
'RightTitle'
|
|
|
|
);
|
|
|
|
});
|
2015-07-24 04:37:48 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
return parent::getCMSFields();
|
|
|
|
}
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
|
|
|
* Return the form field
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function getFormField()
|
|
|
|
{
|
|
|
|
$defaultValue = $this->DefaultToToday
|
2018-09-21 05:58:19 +02:00
|
|
|
? DBDatetime::now()->Format('yyyy-MM-dd')
|
2016-07-21 07:53:59 +02:00
|
|
|
: $this->Default;
|
2015-08-21 05:21:33 +02:00
|
|
|
|
2018-03-23 04:23:51 +01:00
|
|
|
$field = FormField::create($this->Name, $this->Title ?: false, $defaultValue)
|
2018-03-21 02:56:16 +01:00
|
|
|
->setFieldHolderTemplate(EditableFormField::class . '_holder')
|
2017-08-14 02:29:57 +02:00
|
|
|
->setTemplate(EditableFormField::class);
|
2015-08-21 05:21:33 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->doUpdateFormField($field);
|
2015-08-21 05:21:33 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
return $field;
|
|
|
|
}
|
2012-07-17 06:09:31 +02:00
|
|
|
}
|