silverstripe-userforms/code/Model/EditableFormField/EditableDateField.php

70 lines
1.8 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\UserForms\Model\EditableFormField;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\FieldList;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\UserForms\Model\EditableFormField;
use SilverStripe\UserForms\Model\EditableFormField\EditableDateField\FormField;
/**
* EditableDateField
*
* Allows a user to add a date field.
*
* @package userforms
*/
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
private static $has_placeholder = true;
private static $db = [
2016-07-21 07:53:59 +02:00
'DefaultToToday' => 'Boolean' // From customsettings
];
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',
_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
? DBDatetime::now()->Format('yyyy-MM-dd')
2016-07-21 07:53:59 +02:00
: $this->Default;
$field = FormField::create($this->Name, $this->Title ?: false, $defaultValue)
->setFieldHolderTemplate(EditableFormField::class . '_holder')
->setTemplate(EditableFormField::class);
2016-07-21 07:53:59 +02:00
$this->doUpdateFormField($field);
2016-07-21 07:53:59 +02:00
return $field;
}
}