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

112 lines
2.8 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\UserForms\Model\EditableFormField;
use SilverStripe\Core\Convert;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\HeaderField;
use SilverStripe\UserForms\Model\EditableFormField;
/**
* Allows an editor to insert a generic heading into a field
*
* @package userforms
*/
2016-07-21 07:53:59 +02:00
class EditableFormHeading extends EditableFormField
{
private static $singular_name = 'Heading';
private static $plural_name = 'Headings';
private static $literal = true;
private static $db = [
2016-07-21 07:53:59 +02:00
'Level' => 'Int(3)', // From CustomSettings
'HideFromReports' => 'Boolean(0)' // from CustomSettings
];
2016-07-21 07:53:59 +02:00
private static $defaults = [
2016-07-21 07:53:59 +02:00
'Level' => 3,
'HideFromReports' => false
];
private static $table_name = 'EditableFormHeading';
2016-07-21 07:53:59 +02:00
/**
* @return FieldList
*/
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->removeByName(['Default', 'Validation', 'RightTitle']);
2016-07-21 07:53:59 +02:00
$levels = [
2016-07-21 07:53:59 +02:00
'1' => '1',
'2' => '2',
'3' => '3',
'4' => '4',
'5' => '5',
'6' => '6'
];
2016-07-21 07:53:59 +02:00
$fields->addFieldsToTab('Root.Main', [
2016-07-21 07:53:59 +02:00
DropdownField::create(
'Level',
_t(__CLASS__.'.LEVEL', 'Select Heading Level'),
2016-07-21 07:53:59 +02:00
$levels
),
CheckboxField::create(
'HideFromReports',
_t('SilverStripe\\UserForms\\Model\\EditableFormField\\EditableLiteralField.HIDEFROMREPORT', 'Hide from reports?')
2016-07-21 07:53:59 +02:00
)
]);
2016-07-21 07:53:59 +02:00
return $fields;
}
public function getFormField()
{
$labelField = HeaderField::create('userforms-header', $this->EscapedTitle)
->setHeadingLevel($this->Level);
2016-07-21 07:53:59 +02:00
$labelField->addExtraClass('FormHeading');
$labelField->setAttribute('data-id', $this->Name);
$this->doUpdateFormField($labelField);
return $labelField;
}
protected function updateFormField($field)
{
// set the right title on this field
if ($this->RightTitle) {
// Since this field expects raw html, safely escape the user data prior
$field->setRightTitle(Convert::raw2xml($this->RightTitle));
}
// if this field has an extra class
if ($this->ExtraClass) {
$field->addExtraClass($this->ExtraClass);
}
}
public function showInReports()
{
return !$this->HideFromReports;
}
public function getFieldValidationOptions()
{
return false;
}
public function getSelectorHolder()
{
return "$(\":header[data-id='{$this->Name}']\")";
}
public function getLevel()
{
return $this->getField('Level') ?: 3;
}
}