silverstripe-userforms/code/model/editableformfields/EditableCheckbox.php

65 lines
1.5 KiB
PHP
Raw Normal View History

<?php
/**
* EditableCheckbox
*
* A user modifiable checkbox on a UserDefinedForm
2015-09-11 00:20:06 +02:00
*
* @package userforms
*/
2016-07-21 07:53:59 +02:00
class EditableCheckbox extends EditableFormField
{
2015-09-11 00:20:06 +02:00
2016-07-21 07:53:59 +02:00
private static $singular_name = 'Checkbox Field';
2015-09-11 00:20:06 +02:00
2016-07-21 07:53:59 +02:00
private static $plural_name = 'Checkboxes';
2015-07-24 04:37:48 +02:00
2016-07-21 07:53:59 +02:00
private static $db = array(
'CheckedDefault' => 'Boolean' // from CustomSettings
);
2015-07-24 04:37:48 +02:00
2016-07-21 07:53:59 +02:00
/**
* @return FieldList
*/
public function getCMSFields()
{
$fields = parent::getCMSFields();
2015-07-24 04:37:48 +02:00
2016-07-21 07:53:59 +02:00
$fields->replaceField('Default', CheckboxField::create(
"CheckedDefault",
_t('EditableFormField.CHECKEDBYDEFAULT', 'Checked by Default?')
));
2015-07-24 04:37:48 +02:00
2016-07-21 07:53:59 +02:00
return $fields;
}
2015-07-24 04:37:48 +02:00
2016-07-21 07:53:59 +02:00
public function getFormField()
{
$field = CheckboxField::create($this->Name, $this->EscapedTitle, $this->CheckedDefault)
->setFieldHolderTemplate('UserFormsCheckboxField_holder')
->setTemplate('UserFormsCheckboxField');
2016-07-21 07:53:59 +02:00
$this->doUpdateFormField($field);
2016-07-21 07:53:59 +02:00
return $field;
}
2015-09-11 00:20:06 +02:00
2016-07-21 07:53:59 +02:00
public function getValueFromData($data)
{
$value = (isset($data[$this->Name])) ? $data[$this->Name] : false;
2015-09-11 00:20:06 +02:00
2016-07-21 07:53:59 +02:00
return ($value) ? _t('EditableFormField.YES', 'Yes') : _t('EditableFormField.NO', 'No');
}
2015-07-24 04:37:48 +02:00
2016-07-21 07:53:59 +02:00
public function migrateSettings($data)
{
// Migrate 'Default' setting to 'CheckedDefault'
if (isset($data['Default'])) {
$this->CheckedDefault = (bool)$data['Default'];
unset($data['Default']);
}
2015-09-11 00:20:06 +02:00
2016-07-21 07:53:59 +02:00
parent::migrateSettings($data);
}
2015-07-24 04:37:48 +02:00
}