2008-09-29 05:18:23 +02:00
|
|
|
<?php
|
2015-08-11 22:57:19 +02:00
|
|
|
|
2017-08-11 02:36:28 +02:00
|
|
|
namespace SilverStripe\UserForms\Model;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\CMS\Controllers\CMSMain;
|
|
|
|
use SilverStripe\CMS\Controllers\CMSPageEditController;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Core\ClassInfo;
|
|
|
|
use SilverStripe\Core\Config\Config;
|
2017-08-13 23:26:53 +02:00
|
|
|
use SilverStripe\Core\Manifest\ModuleLoader;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\Forms\CheckboxField;
|
|
|
|
use SilverStripe\Forms\DropdownField;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Forms\FieldList;
|
2018-03-20 23:57:30 +01:00
|
|
|
use SilverStripe\Forms\FormField;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridFieldButtonRow;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
|
|
|
|
use SilverStripe\Forms\LiteralField;
|
|
|
|
use SilverStripe\Forms\ReadonlyField;
|
|
|
|
use SilverStripe\Forms\SegmentField;
|
|
|
|
use SilverStripe\Forms\TabSet;
|
|
|
|
use SilverStripe\Forms\TextField;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2017-10-09 04:05:41 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
2018-09-03 13:37:21 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBVarchar;
|
2021-02-26 04:13:23 +01:00
|
|
|
use SilverStripe\ORM\HasManyList;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\ORM\ValidationException;
|
|
|
|
use SilverStripe\UserForms\Extension\UserFormFieldEditorExtension;
|
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroup;
|
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroupEnd;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableFormStep;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\UserForms\Model\Submission\SubmittedFormField;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\UserForms\Modifier\DisambiguationSegmentFieldModifier;
|
|
|
|
use SilverStripe\UserForms\Modifier\UnderscoreSegmentFieldModifier;
|
|
|
|
use SilverStripe\Versioned\Versioned;
|
|
|
|
use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton;
|
|
|
|
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2008-09-29 05:18:23 +02:00
|
|
|
/**
|
2015-08-11 22:57:19 +02:00
|
|
|
* Represents the base class of a editable form field
|
|
|
|
* object like {@link EditableTextField}.
|
2009-04-17 04:26:40 +02:00
|
|
|
*
|
|
|
|
* @package userforms
|
2015-08-11 22:57:19 +02:00
|
|
|
*
|
2015-12-11 05:38:31 +01:00
|
|
|
* @property string $CustomErrorMessage
|
2021-02-26 04:13:23 +01:00
|
|
|
* @property string $Default
|
2017-04-28 00:22:15 +02:00
|
|
|
* @property string $DisplayRulesConjunction
|
2021-02-26 04:13:23 +01:00
|
|
|
* @property string $ExtraClass
|
|
|
|
* @property string $Name
|
|
|
|
* @property int $ParentID
|
|
|
|
* @property string $Placeholder
|
|
|
|
* @property string $RightTitle
|
|
|
|
* @property bool $Required
|
|
|
|
* @property int $ShowInSummary
|
|
|
|
* @property int $ShowOnLoad
|
|
|
|
* @property int $Sort
|
2017-03-10 05:48:47 +01:00
|
|
|
* @mixin Versioned
|
2023-12-15 05:04:35 +01:00
|
|
|
* @method HasManyList<EditableCustomRule> DisplayRules()
|
|
|
|
* @method DataObject Parent()
|
2008-09-29 05:18:23 +02:00
|
|
|
*/
|
2016-07-21 07:53:59 +02:00
|
|
|
class EditableFormField extends DataObject
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Set to true to hide from class selector
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $hidden = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define this field as abstract (not inherited)
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $abstract = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag this field type as non-data (e.g. literal, header, html)
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $literal = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default sort order
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $default_sort = '"Sort"';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A list of CSS classes that can be added
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-08-11 01:33:06 +02:00
|
|
|
public static $allowed_css = [];
|
2016-07-21 07:53:59 +02:00
|
|
|
|
2017-04-28 10:31:51 +02:00
|
|
|
/**
|
|
|
|
* Set this to true to enable placeholder field for any given class
|
|
|
|
* @config
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $has_placeholder = false;
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $summary_fields = [
|
2016-07-21 07:53:59 +02:00
|
|
|
'Title'
|
2017-08-11 01:33:06 +02:00
|
|
|
];
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $db = [
|
|
|
|
'Name' => 'Varchar',
|
|
|
|
'Title' => 'Varchar(255)',
|
|
|
|
'Default' => 'Varchar(255)',
|
|
|
|
'Sort' => 'Int',
|
|
|
|
'Required' => 'Boolean',
|
|
|
|
'CustomErrorMessage' => 'Varchar(255)',
|
2017-08-24 05:15:36 +02:00
|
|
|
'ExtraClass' => 'Text',
|
|
|
|
'RightTitle' => 'Varchar(255)',
|
|
|
|
'ShowOnLoad' => 'Boolean(1)',
|
2017-08-11 01:33:06 +02:00
|
|
|
'ShowInSummary' => 'Boolean',
|
|
|
|
'Placeholder' => 'Varchar(255)',
|
2017-04-28 00:22:15 +02:00
|
|
|
'DisplayRulesConjunction' => 'Enum("And,Or","Or")',
|
2017-08-11 01:33:06 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
private static $table_name = 'EditableFormField';
|
2017-01-24 21:35:50 +01:00
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $defaults = [
|
2016-07-21 07:53:59 +02:00
|
|
|
'ShowOnLoad' => true,
|
2017-08-11 01:33:06 +02:00
|
|
|
];
|
2016-07-21 07:53:59 +02:00
|
|
|
|
2022-06-13 09:36:35 +02:00
|
|
|
private static $indexes = [
|
|
|
|
'Name' => 'Name',
|
|
|
|
];
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $has_one = [
|
2017-10-09 04:05:41 +02:00
|
|
|
'Parent' => DataObject::class,
|
2017-08-11 01:33:06 +02:00
|
|
|
];
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Built in extensions required
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $extensions = [
|
|
|
|
Versioned::class . "('Stage', 'Live')"
|
|
|
|
];
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $has_many = [
|
|
|
|
'DisplayRules' => EditableCustomRule::class . '.Parent'
|
|
|
|
];
|
2016-07-21 07:53:59 +02:00
|
|
|
|
2017-08-15 05:30:59 +02:00
|
|
|
private static $owns = [
|
|
|
|
'DisplayRules',
|
|
|
|
];
|
|
|
|
|
|
|
|
private static $cascade_deletes = [
|
|
|
|
'DisplayRules',
|
|
|
|
];
|
|
|
|
|
2018-03-05 02:31:33 +01:00
|
|
|
private static $cascade_duplicates = false;
|
|
|
|
|
2023-05-31 07:33:35 +02:00
|
|
|
/**
|
|
|
|
* This is protected rather that private so that it's unit testable
|
|
|
|
*/
|
|
|
|
protected static $isDisplayedRecursionProtection = [];
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $readonly;
|
|
|
|
|
2017-04-28 00:22:15 +02:00
|
|
|
/**
|
|
|
|
* Property holds the JS event which gets fired for this type of element
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $jsEventHandler = 'change';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the jsEventHandler property for the current object. Bearing in mind it could've been overridden.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getJsEventHandler()
|
|
|
|
{
|
|
|
|
return $this->jsEventHandler;
|
|
|
|
}
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
|
|
|
* Set the visibility of an individual form field
|
|
|
|
*
|
|
|
|
* @param bool
|
2017-08-15 05:30:59 +02:00
|
|
|
* @return $this
|
2016-07-21 07:53:59 +02:00
|
|
|
*/
|
|
|
|
public function setReadonly($readonly = true)
|
|
|
|
{
|
|
|
|
$this->readonly = $readonly;
|
2017-08-15 05:30:59 +02:00
|
|
|
return $this;
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether this field is readonly
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function isReadonly()
|
|
|
|
{
|
|
|
|
return $this->readonly;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
|
|
|
public function getCMSFields()
|
|
|
|
{
|
2017-08-11 01:33:06 +02:00
|
|
|
$fields = FieldList::create(TabSet::create('Root'));
|
2016-07-21 07:53:59 +02:00
|
|
|
|
2019-07-08 14:26:58 +02:00
|
|
|
// If created with (+) button
|
|
|
|
if ($this->ClassName === EditableFormField::class) {
|
|
|
|
$fieldClasses = $this->getEditableFieldClasses();
|
|
|
|
$fields->addFieldsToTab('Root.Main', [
|
|
|
|
DropdownField::create('ClassName', _t(__CLASS__.'.TYPE', 'Type'), $fieldClasses)
|
2020-12-01 22:01:50 +01:00
|
|
|
->setEmptyString(_t(__CLASS__ . '.TYPE_EMPTY', 'Select field type'))
|
2019-07-08 14:26:58 +02:00
|
|
|
]);
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// Main tab
|
|
|
|
$fields->addFieldsToTab(
|
|
|
|
'Root.Main',
|
2017-08-11 01:33:06 +02:00
|
|
|
[
|
2016-07-21 07:53:59 +02:00
|
|
|
ReadonlyField::create(
|
|
|
|
'Type',
|
2017-08-11 01:33:06 +02:00
|
|
|
_t(__CLASS__.'.TYPE', 'Type'),
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->i18n_singular_name()
|
|
|
|
),
|
2017-08-11 01:33:06 +02:00
|
|
|
CheckboxField::create('ShowInSummary', _t(__CLASS__.'.SHOWINSUMMARY', 'Show in summary gridfield')),
|
2016-07-21 07:53:59 +02:00
|
|
|
LiteralField::create(
|
|
|
|
'MergeField',
|
2017-09-20 06:31:40 +02:00
|
|
|
'<div class="form-group field readonly">' .
|
|
|
|
'<label class="left form__field-label" for="Form_ItemEditForm_MergeField">'
|
|
|
|
. _t(__CLASS__.'.MERGEFIELDNAME', 'Merge field')
|
|
|
|
. '</label>'
|
|
|
|
. '<div class="form__field-holder">'
|
|
|
|
. '<span class="readonly" id="Form_ItemEditForm_MergeField">$' . $this->Name . '</span>'
|
|
|
|
. '</div>'
|
|
|
|
. '</div>'
|
2016-07-21 07:53:59 +02:00
|
|
|
),
|
2017-08-11 01:33:06 +02:00
|
|
|
TextField::create('Title', _t(__CLASS__.'.TITLE', 'Title')),
|
|
|
|
TextField::create('Default', _t(__CLASS__.'.DEFAULT', 'Default value')),
|
|
|
|
TextField::create('RightTitle', _t(__CLASS__.'.RIGHTTITLE', 'Right title')),
|
|
|
|
SegmentField::create('Name', _t(__CLASS__.'.NAME', 'Name'))->setModifiers([
|
2016-07-21 07:53:59 +02:00
|
|
|
UnderscoreSegmentFieldModifier::create()->setDefault('FieldName'),
|
|
|
|
DisambiguationSegmentFieldModifier::create(),
|
2017-08-11 01:33:06 +02:00
|
|
|
])->setPreview($this->Name)
|
|
|
|
]
|
2016-07-21 07:53:59 +02:00
|
|
|
);
|
2017-08-11 01:33:06 +02:00
|
|
|
$fields->fieldByName('Root.Main')->setTitle(_t('SilverStripe\\CMS\\Model\\SiteTree.TABMAIN', 'Main'));
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
// Custom settings
|
|
|
|
if (!empty(self::$allowed_css)) {
|
2017-08-11 01:33:06 +02:00
|
|
|
$cssList = [];
|
2016-07-21 07:53:59 +02:00
|
|
|
foreach (self::$allowed_css as $k => $v) {
|
|
|
|
if (!is_array($v)) {
|
|
|
|
$cssList[$k]=$v;
|
|
|
|
} elseif ($k === $this->ClassName) {
|
|
|
|
$cssList = array_merge($cssList, $v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 02:37:03 +02:00
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Main',
|
2016-07-21 07:53:59 +02:00
|
|
|
DropdownField::create(
|
|
|
|
'ExtraClass',
|
2017-08-11 01:33:06 +02:00
|
|
|
_t(__CLASS__.'.EXTRACLASS_TITLE', 'Extra Styling/Layout'),
|
2016-07-21 07:53:59 +02:00
|
|
|
$cssList
|
|
|
|
)->setDescription(_t(
|
2017-08-11 01:33:06 +02:00
|
|
|
__CLASS__.'.EXTRACLASS_SELECT',
|
2016-07-21 07:53:59 +02:00
|
|
|
'Select from the list of allowed styles'
|
|
|
|
))
|
|
|
|
);
|
|
|
|
} else {
|
2017-08-11 02:37:03 +02:00
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Main',
|
2016-07-21 07:53:59 +02:00
|
|
|
TextField::create(
|
|
|
|
'ExtraClass',
|
2017-08-11 01:33:06 +02:00
|
|
|
_t(__CLASS__.'.EXTRACLASS_Title', 'Extra CSS classes')
|
2016-07-21 07:53:59 +02:00
|
|
|
)->setDescription(_t(
|
2017-08-11 01:33:06 +02:00
|
|
|
__CLASS__.'.EXTRACLASS_MULTIPLE',
|
2016-07-21 07:53:59 +02:00
|
|
|
'Separate each CSS class with a single space'
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validation
|
|
|
|
$validationFields = $this->getFieldValidationOptions();
|
|
|
|
if ($validationFields && $validationFields->count()) {
|
2024-05-13 09:22:24 +02:00
|
|
|
$fields->addFieldsToTab('Root.Validation', $validationFields->toArray());
|
2017-08-11 01:33:06 +02:00
|
|
|
$fields->fieldByName('Root.Validation')->setTitle(_t(__CLASS__.'.VALIDATION', 'Validation'));
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add display rule fields
|
|
|
|
$displayFields = $this->getDisplayRuleFields();
|
|
|
|
if ($displayFields && $displayFields->count()) {
|
2024-05-13 09:22:24 +02:00
|
|
|
$fields->addFieldsToTab('Root.DisplayRules', $displayFields->toArray());
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
2017-04-28 10:31:51 +02:00
|
|
|
// Placeholder
|
|
|
|
if ($this->config()->has_placeholder) {
|
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Main',
|
|
|
|
TextField::create(
|
|
|
|
'Placeholder',
|
2017-08-11 01:33:06 +02:00
|
|
|
_t(__CLASS__.'.PLACEHOLDER', 'Placeholder')
|
2017-04-28 10:31:51 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->extend('updateCMSFields', $fields);
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
2017-10-09 04:05:41 +02:00
|
|
|
|
|
|
|
public function requireDefaultRecords()
|
|
|
|
{
|
|
|
|
parent::requireDefaultRecords();
|
|
|
|
|
|
|
|
// make sure to migrate the class across (prior to v5.x)
|
2018-08-21 05:04:51 +02:00
|
|
|
DB::query("UPDATE \"EditableFormField\" SET \"ParentClass\" = 'Page' WHERE \"ParentClass\" IS NULL");
|
2021-06-20 23:58:35 +02:00
|
|
|
if (self::has_extension(Versioned::class)) {
|
|
|
|
DB::query("UPDATE \"EditableFormField_Live\" SET \"ParentClass\" = 'Page' WHERE \"ParentClass\" IS NULL");
|
|
|
|
DB::query("UPDATE \"EditableFormField_Versions\" SET \"ParentClass\" = 'Page' WHERE \"ParentClass\" IS NULL");
|
|
|
|
}
|
2017-10-09 04:05:41 +02:00
|
|
|
}
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
|
|
|
* Return fields to display on the 'Display Rules' tab
|
|
|
|
*
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
|
|
|
protected function getDisplayRuleFields()
|
|
|
|
{
|
2022-04-13 03:52:56 +02:00
|
|
|
$allowedClasses = array_keys($this->getEditableFieldClasses(false) ?? []);
|
2016-07-21 07:53:59 +02:00
|
|
|
$editableColumns = new GridFieldEditableColumns();
|
2017-08-11 01:33:06 +02:00
|
|
|
$editableColumns->setDisplayFields([
|
|
|
|
'ConditionFieldID' => function ($record, $column, $grid) use ($allowedClasses) {
|
|
|
|
return DropdownField::create($column, '', EditableFormField::get()->filter([
|
|
|
|
'ParentID' => $this->ParentID,
|
2017-04-28 00:22:15 +02:00
|
|
|
'ClassName' => $allowedClasses,
|
2017-08-11 01:33:06 +02:00
|
|
|
])->exclude([
|
|
|
|
'ID' => $this->ID,
|
|
|
|
])->map('ID', 'Title'));
|
2016-07-21 07:53:59 +02:00
|
|
|
},
|
|
|
|
'ConditionOption' => function ($record, $column, $grid) {
|
2017-08-09 01:55:09 +02:00
|
|
|
$options = Config::inst()->get(EditableCustomRule::class, 'condition_options');
|
2017-05-01 07:44:39 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
return DropdownField::create($column, '', $options);
|
|
|
|
},
|
|
|
|
'FieldValue' => function ($record, $column, $grid) {
|
|
|
|
return TextField::create($column);
|
2017-05-24 03:46:54 +02:00
|
|
|
}
|
2017-08-11 01:33:06 +02:00
|
|
|
]);
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
// Custom rules
|
|
|
|
$customRulesConfig = GridFieldConfig::create()
|
|
|
|
->addComponents(
|
|
|
|
$editableColumns,
|
|
|
|
new GridFieldButtonRow(),
|
|
|
|
new GridFieldToolbarHeader(),
|
|
|
|
new GridFieldAddNewInlineButton(),
|
|
|
|
new GridFieldDeleteAction()
|
|
|
|
);
|
|
|
|
|
|
|
|
return new FieldList(
|
2017-08-11 02:37:03 +02:00
|
|
|
DropdownField::create(
|
|
|
|
'ShowOnLoad',
|
2017-08-11 01:33:06 +02:00
|
|
|
_t(__CLASS__.'.INITIALVISIBILITY', 'Initial visibility'),
|
|
|
|
[
|
2017-04-28 00:22:15 +02:00
|
|
|
1 => 'Show',
|
|
|
|
0 => 'Hide',
|
2017-08-11 01:33:06 +02:00
|
|
|
]
|
2017-04-28 00:22:15 +02:00
|
|
|
),
|
2017-08-11 02:37:03 +02:00
|
|
|
DropdownField::create(
|
|
|
|
'DisplayRulesConjunction',
|
2017-08-11 01:33:06 +02:00
|
|
|
_t(__CLASS__.'.DISPLAYIF', 'Toggle visibility when'),
|
|
|
|
[
|
2017-08-11 01:39:58 +02:00
|
|
|
'Or' => _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.SENDIFOR', 'Any conditions are true'),
|
|
|
|
'And' => _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.SENDIFAND', 'All conditions are true'),
|
2017-08-11 01:33:06 +02:00
|
|
|
]
|
2017-04-10 22:15:29 +02:00
|
|
|
),
|
2016-07-21 07:53:59 +02:00
|
|
|
GridField::create(
|
|
|
|
'DisplayRules',
|
2017-08-11 01:33:06 +02:00
|
|
|
_t(__CLASS__.'.CUSTOMRULES', 'Custom Rules'),
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->DisplayRules(),
|
|
|
|
$customRulesConfig
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onBeforeWrite()
|
|
|
|
{
|
|
|
|
parent::onBeforeWrite();
|
|
|
|
|
2021-01-17 03:55:47 +01:00
|
|
|
$formField = $this->getFormField();
|
|
|
|
if ($formField && !$formField->hasData()) {
|
|
|
|
$this->Required = false;
|
|
|
|
}
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// Set a field name.
|
|
|
|
if (!$this->Name) {
|
|
|
|
// New random name
|
|
|
|
$this->Name = $this->generateName();
|
|
|
|
} elseif ($this->Name === 'Field') {
|
|
|
|
throw new ValidationException('Field name cannot be "Field"');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->Sort && $this->ParentID) {
|
|
|
|
$parentID = $this->ParentID;
|
|
|
|
$this->Sort = EditableFormField::get()
|
|
|
|
->filter('ParentID', $parentID)
|
|
|
|
->max('Sort') + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a new non-conflicting Name value
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function generateName()
|
|
|
|
{
|
|
|
|
do {
|
2017-08-13 23:26:53 +02:00
|
|
|
// Generate a new random name after this class (handles namespaces)
|
2017-08-15 00:49:13 +02:00
|
|
|
$classNamePieces = explode('\\', static::class);
|
2017-08-13 23:26:53 +02:00
|
|
|
$class = array_pop($classNamePieces);
|
2016-07-21 07:53:59 +02:00
|
|
|
$entropy = substr(sha1(uniqid()), 0, 5);
|
|
|
|
$name = "{$class}_{$entropy}";
|
|
|
|
|
|
|
|
// Check if it conflicts
|
|
|
|
$exists = EditableFormField::get()->filter('Name', $name)->count() > 0;
|
|
|
|
} while ($exists);
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag indicating that this field will set its own error message via data-msg='' attributes
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getSetsOwnError()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether a user can delete this form field
|
|
|
|
* based on whether they can edit the page
|
|
|
|
*
|
2015-12-22 23:14:36 +01:00
|
|
|
* @param Member $member
|
2016-07-21 07:53:59 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canDelete($member = null)
|
|
|
|
{
|
|
|
|
return $this->canEdit($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether a user can edit this form field
|
|
|
|
* based on whether they can edit the page
|
|
|
|
*
|
2015-12-22 23:14:36 +01:00
|
|
|
* @param Member $member
|
2016-07-21 07:53:59 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canEdit($member = null)
|
|
|
|
{
|
2015-12-22 23:14:36 +01:00
|
|
|
$parent = $this->Parent();
|
2016-07-21 07:53:59 +02:00
|
|
|
if ($parent && $parent->exists()) {
|
|
|
|
return $parent->canEdit($member) && !$this->isReadonly();
|
|
|
|
} elseif (!$this->exists() && Controller::has_curr()) {
|
2017-02-03 15:39:36 +01:00
|
|
|
// This is for GridFieldOrderableRows support as it checks edit permissions on
|
|
|
|
// singleton of the class. Allows editing of User Defined Form pages by
|
2016-07-21 07:53:59 +02:00
|
|
|
// 'Content Authors' and those with permission to edit the UDF page. (ie. CanEditType/EditorGroups)
|
|
|
|
// This is to restore User Forms 2.x backwards compatibility.
|
|
|
|
$controller = Controller::curr();
|
|
|
|
if ($controller && $controller instanceof CMSPageEditController) {
|
|
|
|
$parent = $controller->getRecord($controller->currentPageID());
|
|
|
|
// Only allow this behaviour on pages using UserFormFieldEditorExtension, such
|
|
|
|
// as UserDefinedForm page type.
|
2017-08-09 01:55:09 +02:00
|
|
|
if ($parent && $parent->hasExtension(UserFormFieldEditorExtension::class)) {
|
2016-07-21 07:53:59 +02:00
|
|
|
return $parent->canEdit($member);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-22 23:14:36 +01:00
|
|
|
|
|
|
|
// Fallback to secure admin permissions
|
2016-07-21 07:53:59 +02:00
|
|
|
return parent::canEdit($member);
|
|
|
|
}
|
2015-12-22 23:14:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether a user can view this form field
|
|
|
|
* based on whether they can view the page, regardless of the ReadOnly status of the field
|
|
|
|
*
|
|
|
|
* @param Member $member
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-07-21 07:53:59 +02:00
|
|
|
public function canView($member = null)
|
|
|
|
{
|
|
|
|
$parent = $this->Parent();
|
|
|
|
if ($parent && $parent->exists()) {
|
|
|
|
return $parent->canView($member);
|
|
|
|
}
|
2015-07-24 04:37:48 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-08-11 22:57:19 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
|
|
|
* Return whether a user can create an object of this type
|
|
|
|
*
|
2015-12-22 23:14:36 +01:00
|
|
|
* @param Member $member
|
|
|
|
* @param array $context Virtual parameter to allow context to be passed in to check
|
2016-07-21 07:53:59 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-08-11 02:36:28 +02:00
|
|
|
public function canCreate($member = null, $context = [])
|
2016-07-21 07:53:59 +02:00
|
|
|
{
|
|
|
|
// Check parent page
|
2015-12-22 23:14:36 +01:00
|
|
|
$parent = $this->getCanCreateContext(func_get_args());
|
2016-07-21 07:53:59 +02:00
|
|
|
if ($parent) {
|
2015-12-22 23:14:36 +01:00
|
|
|
return $parent->canEdit($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to secure admin permissions
|
|
|
|
return parent::canCreate($member);
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
2015-08-11 22:57:19 +02:00
|
|
|
|
2015-12-22 23:14:36 +01:00
|
|
|
/**
|
|
|
|
* Helper method to check the parent for this object
|
|
|
|
*
|
|
|
|
* @param array $args List of arguments passed to canCreate
|
|
|
|
* @return SiteTree Parent page instance
|
|
|
|
*/
|
2016-07-21 07:53:59 +02:00
|
|
|
protected function getCanCreateContext($args)
|
|
|
|
{
|
2015-12-22 23:14:36 +01:00
|
|
|
// Inspect second parameter to canCreate for a 'Parent' context
|
2016-07-21 07:53:59 +02:00
|
|
|
if (isset($args[1]['Parent'])) {
|
2015-12-22 23:14:36 +01:00
|
|
|
return $args[1]['Parent'];
|
|
|
|
}
|
|
|
|
// Hack in currently edited page if context is missing
|
2016-07-21 07:53:59 +02:00
|
|
|
if (Controller::has_curr() && Controller::curr() instanceof CMSMain) {
|
2015-12-22 23:14:36 +01:00
|
|
|
return Controller::curr()->currentPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
// No page being edited
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
2017-03-10 05:48:47 +01:00
|
|
|
* checks whether record is new, copied from SiteTree
|
2016-07-21 07:53:59 +02:00
|
|
|
*/
|
|
|
|
public function isNew()
|
|
|
|
{
|
|
|
|
if (empty($this->ID)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_numeric($this->ID)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-13 03:52:56 +02:00
|
|
|
return stripos($this->ID ?? '', 'new') === 0;
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the allowed css classes for the extraClass custom setting
|
|
|
|
*
|
2017-03-10 05:48:47 +01:00
|
|
|
* @param array $allowed The permissible CSS classes to add
|
2016-07-21 07:53:59 +02:00
|
|
|
*/
|
|
|
|
public function setAllowedCss(array $allowed)
|
|
|
|
{
|
|
|
|
if (is_array($allowed)) {
|
|
|
|
foreach ($allowed as $k => $v) {
|
|
|
|
self::$allowed_css[$k] = (!is_null($v)) ? $v : $k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the path to the icon for this field type, relative to the site root.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getIcon()
|
|
|
|
{
|
2017-11-02 22:44:45 +01:00
|
|
|
$classNamespaces = explode("\\", static::class);
|
|
|
|
$shortClass = end($classNamespaces);
|
|
|
|
|
|
|
|
$resource = ModuleLoader::getModule('silverstripe/userforms')
|
2022-04-13 03:52:56 +02:00
|
|
|
->getResource('images/' . strtolower($shortClass ?? '') . '.png');
|
2017-11-02 22:44:45 +01:00
|
|
|
|
|
|
|
if (!$resource->exists()) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $resource->getURL();
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether or not this field has addable options
|
|
|
|
* such as a dropdown field or radio set
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getHasAddableOptions()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether or not this field needs to show the extra
|
|
|
|
* options dropdown list
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function showExtraOptions()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the numeric indicator (1.1.2) that represents it's nesting value
|
|
|
|
*
|
|
|
|
* Only useful for fields attached to a current page, and that contain other fields such as pages
|
|
|
|
* or groups
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFieldNumber()
|
|
|
|
{
|
|
|
|
// Check if exists
|
|
|
|
if (!$this->exists()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// Check parent
|
|
|
|
$form = $this->Parent();
|
|
|
|
if (!$form || !$form->exists() || !($fields = $form->Fields())) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$prior = 0; // Number of prior group at this level
|
2017-08-11 01:33:06 +02:00
|
|
|
$stack = []; // Current stack of nested groups, where the top level = the page
|
2016-07-21 07:53:59 +02:00
|
|
|
foreach ($fields->map('ID', 'ClassName') as $id => $className) {
|
2017-08-09 01:55:09 +02:00
|
|
|
if ($className === EditableFormStep::class) {
|
2016-07-21 07:53:59 +02:00
|
|
|
$priorPage = empty($stack) ? $prior : $stack[0];
|
|
|
|
$stack = array($priorPage + 1);
|
|
|
|
$prior = 0;
|
2017-08-09 01:55:09 +02:00
|
|
|
} elseif ($className === EditableFieldGroup::class) {
|
2016-07-21 07:53:59 +02:00
|
|
|
$stack[] = $prior + 1;
|
|
|
|
$prior = 0;
|
2017-08-09 01:55:09 +02:00
|
|
|
} elseif ($className === EditableFieldGroupEnd::class) {
|
2016-07-21 07:53:59 +02:00
|
|
|
$prior = array_pop($stack);
|
|
|
|
}
|
|
|
|
if ($id == $this->ID) {
|
|
|
|
return implode('.', $stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCMSTitle()
|
|
|
|
{
|
|
|
|
return $this->i18n_singular_name() . ' (' . $this->Title . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append custom validation fields to the default 'Validation'
|
|
|
|
* section in the editable options view
|
|
|
|
*
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
|
|
|
public function getFieldValidationOptions()
|
|
|
|
{
|
|
|
|
$fields = new FieldList(
|
2017-08-11 01:33:06 +02:00
|
|
|
CheckboxField::create('Required', _t(__CLASS__.'.REQUIRED', 'Is this field Required?'))
|
|
|
|
->setDescription(_t(__CLASS__.'.REQUIRED_DESCRIPTION', 'Please note that conditional fields can\'t be required')),
|
|
|
|
TextField::create('CustomErrorMessage', _t(__CLASS__.'.CUSTOMERROR', 'Custom Error Message'))
|
2016-07-21 07:53:59 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->extend('updateFieldValidationOptions', $fields);
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a FormField to appear on the front end. Implement on
|
|
|
|
* your subclass.
|
|
|
|
*
|
|
|
|
* @return FormField
|
|
|
|
*/
|
|
|
|
public function getFormField()
|
|
|
|
{
|
|
|
|
user_error("Please implement a getFormField() on your EditableFormClass ". $this->ClassName, E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates a formfield with extensions
|
|
|
|
*
|
|
|
|
* @param FormField $field
|
|
|
|
*/
|
|
|
|
public function doUpdateFormField($field)
|
|
|
|
{
|
|
|
|
$this->extend('beforeUpdateFormField', $field);
|
|
|
|
$this->updateFormField($field);
|
|
|
|
$this->extend('afterUpdateFormField', $field);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates a formfield with the additional metadata specified by this field
|
|
|
|
*
|
|
|
|
* @param FormField $field
|
|
|
|
*/
|
|
|
|
protected function updateFormField($field)
|
|
|
|
{
|
|
|
|
// set the error / formatting messages
|
|
|
|
$field->setCustomValidationMessage($this->getErrorMessage()->RAW());
|
|
|
|
|
|
|
|
// set the right title on this field
|
|
|
|
if ($this->RightTitle) {
|
2018-03-20 23:57:30 +01:00
|
|
|
$field->setRightTitle($this->RightTitle);
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// if this field is required add some
|
|
|
|
if ($this->Required) {
|
|
|
|
// Required validation can conflict so add the Required validation messages as input attributes
|
|
|
|
$errorMessage = $this->getErrorMessage()->HTML();
|
|
|
|
$field->addExtraClass('requiredField');
|
|
|
|
$field->setAttribute('data-rule-required', 'true');
|
|
|
|
$field->setAttribute('data-msg-required', $errorMessage);
|
|
|
|
|
|
|
|
if ($identifier = UserDefinedForm::config()->required_identifier) {
|
|
|
|
$title = $field->Title() . " <span class='required-identifier'>". $identifier . "</span>";
|
2019-01-10 10:40:53 +01:00
|
|
|
$field->setTitle(DBField::create_field('HTMLText', $title));
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if this field has an extra class
|
|
|
|
if ($this->ExtraClass) {
|
|
|
|
$field->addExtraClass($this->ExtraClass);
|
|
|
|
}
|
2017-04-28 10:31:51 +02:00
|
|
|
|
2017-05-07 23:08:31 +02:00
|
|
|
// if ShowOnLoad is false hide the field
|
|
|
|
if (!$this->ShowOnLoad) {
|
|
|
|
$field->addExtraClass($this->ShowOnLoadNice());
|
|
|
|
}
|
|
|
|
|
2017-04-28 10:31:51 +02:00
|
|
|
// if this field has a placeholder
|
2022-04-13 03:52:56 +02:00
|
|
|
if (strlen($this->Placeholder ?? '') >= 0) {
|
2017-04-28 10:31:51 +02:00
|
|
|
$field->setAttribute('placeholder', $this->Placeholder);
|
|
|
|
}
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the instance of the submission field class
|
|
|
|
*
|
|
|
|
* @return SubmittedFormField
|
|
|
|
*/
|
|
|
|
public function getSubmittedFormField()
|
|
|
|
{
|
2017-08-11 01:33:06 +02:00
|
|
|
return SubmittedFormField::create();
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show this form field (and its related value) in the reports and in emails.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function showInReports()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the error message for this field. Either uses the custom
|
|
|
|
* one (if provided) or the default SilverStripe message
|
|
|
|
*
|
2018-09-03 13:37:21 +02:00
|
|
|
* @return DBVarchar
|
2016-07-21 07:53:59 +02:00
|
|
|
*/
|
|
|
|
public function getErrorMessage()
|
|
|
|
{
|
|
|
|
$title = strip_tags("'". ($this->Title ? $this->Title : $this->Name) . "'");
|
2018-09-03 13:37:21 +02:00
|
|
|
$standard = _t(__CLASS__ . '.FIELDISREQUIRED', '{name} is required', ['name' => $title]);
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
// only use CustomErrorMessage if it has a non empty value
|
|
|
|
$errorMessage = (!empty($this->CustomErrorMessage)) ? $this->CustomErrorMessage : $standard;
|
|
|
|
|
|
|
|
return DBField::create_field('Varchar', $errorMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the formfield to use when editing this inline in gridfield
|
|
|
|
*
|
|
|
|
* @param string $column name of column
|
|
|
|
* @param array $fieldClasses List of allowed classnames if this formfield has a selectable class
|
|
|
|
* @return FormField
|
|
|
|
*/
|
|
|
|
public function getInlineClassnameField($column, $fieldClasses)
|
|
|
|
{
|
|
|
|
return DropdownField::create($column, false, $fieldClasses);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the formfield to use when editing the title inline
|
|
|
|
*
|
|
|
|
* @param string $column
|
|
|
|
* @return FormField
|
|
|
|
*/
|
|
|
|
public function getInlineTitleField($column)
|
|
|
|
{
|
|
|
|
return TextField::create($column, false)
|
2017-08-11 01:33:06 +02:00
|
|
|
->setAttribute('placeholder', _t(__CLASS__.'.TITLE', 'Title'))
|
|
|
|
->setAttribute('data-placeholder', _t(__CLASS__.'.TITLE', 'Title'));
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the JS expression for selecting the holder for this field
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSelectorHolder()
|
|
|
|
{
|
2017-04-28 00:22:15 +02:00
|
|
|
return sprintf('$("%s")', $this->getSelectorOnly());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns only the JS identifier of a string, less the $(), which can be inserted elsewhere, for example when you
|
|
|
|
* want to perform selections on multiple selectors
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSelectorOnly()
|
|
|
|
{
|
|
|
|
return "#{$this->Name}";
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the JS expression for selecting the value for this field
|
|
|
|
*
|
|
|
|
* @param EditableCustomRule $rule Custom rule this selector will be used with
|
|
|
|
* @param bool $forOnLoad Set to true if this will be invoked on load
|
2017-04-28 00:22:15 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2016-07-21 07:53:59 +02:00
|
|
|
*/
|
|
|
|
public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false)
|
|
|
|
{
|
2017-04-28 00:22:15 +02:00
|
|
|
return sprintf("$(%s)", $this->getSelectorFieldOnly());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSelectorFieldOnly()
|
|
|
|
{
|
|
|
|
return "[name='{$this->Name}']";
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the list of classes that can be selected and used as data-values
|
|
|
|
*
|
|
|
|
* @param $includeLiterals Set to false to exclude non-data fields
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getEditableFieldClasses($includeLiterals = true)
|
|
|
|
{
|
2017-08-09 01:55:09 +02:00
|
|
|
$classes = ClassInfo::getValidSubClasses(EditableFormField::class);
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
// Remove classes we don't want to display in the dropdown.
|
2017-08-11 01:33:06 +02:00
|
|
|
$editableFieldClasses = [];
|
2016-07-21 07:53:59 +02:00
|
|
|
foreach ($classes as $class) {
|
|
|
|
// Skip abstract / hidden classes
|
2017-08-15 00:49:13 +02:00
|
|
|
if (Config::inst()->get($class, 'abstract', Config::UNINHERITED)
|
|
|
|
|| Config::inst()->get($class, 'hidden')
|
2016-07-21 07:53:59 +02:00
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$includeLiterals && Config::inst()->get($class, 'literal')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$singleton = singleton($class);
|
|
|
|
if (!$singleton->canCreate()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$editableFieldClasses[$class] = $singleton->i18n_singular_name();
|
|
|
|
}
|
|
|
|
|
|
|
|
asort($editableFieldClasses);
|
|
|
|
return $editableFieldClasses;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-24 05:15:36 +02:00
|
|
|
* @return EditableFormField\Validator
|
2016-07-21 07:53:59 +02:00
|
|
|
*/
|
|
|
|
public function getCMSValidator()
|
|
|
|
{
|
2017-08-24 05:15:36 +02:00
|
|
|
return EditableFormField\Validator::create()
|
2016-07-21 07:53:59 +02:00
|
|
|
->setRecord($this);
|
|
|
|
}
|
|
|
|
|
2017-04-28 00:22:15 +02:00
|
|
|
/**
|
|
|
|
* Extracts info from DisplayRules into array so UserDefinedForm->buildWatchJS can run through it.
|
|
|
|
* @return array|null
|
|
|
|
*/
|
|
|
|
public function formatDisplayRules()
|
|
|
|
{
|
|
|
|
$holderSelector = $this->getSelectorOnly();
|
2017-08-11 01:33:06 +02:00
|
|
|
$result = [
|
2017-04-28 00:22:15 +02:00
|
|
|
'targetFieldID' => $holderSelector,
|
|
|
|
'conjunction' => $this->DisplayRulesConjunctionNice(),
|
2017-08-11 01:33:06 +02:00
|
|
|
'selectors' => [],
|
|
|
|
'events' => [],
|
|
|
|
'operations' => [],
|
2017-04-28 00:22:15 +02:00
|
|
|
'initialState' => $this->ShowOnLoadNice(),
|
2017-08-11 01:33:06 +02:00
|
|
|
'view' => [],
|
|
|
|
'opposite' => [],
|
|
|
|
];
|
|
|
|
|
2017-04-28 00:22:15 +02:00
|
|
|
// Check for field dependencies / default
|
2018-05-08 07:02:01 +02:00
|
|
|
foreach ($this->DisplayRules() as $rule) {
|
2017-04-28 00:22:15 +02:00
|
|
|
// Get the field which is effected
|
2017-08-09 01:55:09 +02:00
|
|
|
$formFieldWatch = DataObject::get_by_id(EditableFormField::class, $rule->ConditionFieldID);
|
2017-04-28 00:22:15 +02:00
|
|
|
// Skip deleted fields
|
2019-03-25 03:09:38 +01:00
|
|
|
if (!$formFieldWatch) {
|
2017-04-28 00:22:15 +02:00
|
|
|
continue;
|
|
|
|
}
|
2019-03-25 03:09:38 +01:00
|
|
|
|
2017-04-28 00:22:15 +02:00
|
|
|
$fieldToWatch = $formFieldWatch->getSelectorFieldOnly();
|
|
|
|
|
|
|
|
$expression = $rule->buildExpression();
|
2022-04-13 03:52:56 +02:00
|
|
|
if (!in_array($fieldToWatch, $result['selectors'] ?? [])) {
|
2017-04-28 00:22:15 +02:00
|
|
|
$result['selectors'][] = $fieldToWatch;
|
|
|
|
}
|
2022-04-13 03:52:56 +02:00
|
|
|
if (!in_array($expression['event'], $result['events'] ?? [])) {
|
2017-04-28 00:22:15 +02:00
|
|
|
$result['events'][] = $expression['event'];
|
|
|
|
}
|
|
|
|
$result['operations'][] = $expression['operation'];
|
|
|
|
|
2017-05-22 05:54:28 +02:00
|
|
|
// View/Show should read
|
2017-04-28 00:22:15 +02:00
|
|
|
$result['view'] = $rule->toggleDisplayText($result['initialState']);
|
2019-03-25 03:09:38 +01:00
|
|
|
$result['opposite'] = $rule->toggleDisplayText($result['initialState'], true);
|
|
|
|
$result['holder'] = $this->getSelectorHolder();
|
|
|
|
$result['holder_event'] = $rule->toggleDisplayEvent($result['initialState']);
|
|
|
|
$result['holder_event_opposite'] = $rule->toggleDisplayEvent($result['initialState'], true);
|
2017-04-28 00:22:15 +02:00
|
|
|
}
|
|
|
|
|
2022-04-13 03:52:56 +02:00
|
|
|
return (count($result['selectors'] ?? [])) ? $result : null;
|
2017-04-28 00:22:15 +02:00
|
|
|
}
|
|
|
|
|
2023-05-31 07:33:35 +02:00
|
|
|
/**
|
|
|
|
* Used to prevent infinite recursion when checking a CMS user has setup two or more fields to have
|
|
|
|
* their display rules dependent on one another
|
|
|
|
*
|
|
|
|
* There will be several thousand calls to isDisplayed before memory is likely to be hit, so 100
|
|
|
|
* calls is a reasonable limit that ensures that this doesn't prevent legit use cases from being
|
|
|
|
* identified as recursion
|
|
|
|
*/
|
|
|
|
private function checkIsDisplayedRecursionProtection(): bool
|
|
|
|
{
|
|
|
|
$count = count(array_filter(static::$isDisplayedRecursionProtection, fn($id) => $id === $this->ID));
|
|
|
|
return $count < 100;
|
|
|
|
}
|
|
|
|
|
2020-01-10 05:36:04 +01:00
|
|
|
/**
|
|
|
|
* Check if this EditableFormField is displayed based on its DisplayRules and the provided data.
|
|
|
|
* @param array $data
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isDisplayed(array $data)
|
|
|
|
{
|
2023-05-31 07:33:35 +02:00
|
|
|
static::$isDisplayedRecursionProtection[] = $this->ID;
|
2020-01-10 05:36:04 +01:00
|
|
|
$displayRules = $this->DisplayRules();
|
|
|
|
|
|
|
|
if ($displayRules->count() === 0) {
|
|
|
|
// If no display rule have been defined, isDisplayed equals the ShowOnLoad property
|
|
|
|
return $this->ShowOnLoad;
|
|
|
|
}
|
|
|
|
|
|
|
|
$conjunction = $this->DisplayRulesConjunctionNice();
|
|
|
|
|
|
|
|
// && start with true and find and condition that doesn't satisfy
|
|
|
|
// || start with false and find and condition that satisfies
|
|
|
|
$conditionsSatisfied = ($conjunction === '&&');
|
|
|
|
|
|
|
|
foreach ($displayRules as $rule) {
|
|
|
|
$controllingField = $rule->ConditionField();
|
|
|
|
|
|
|
|
// recursively check - if any of the dependant fields are hidden, assume the rule can not be satisfied
|
2023-05-31 07:33:35 +02:00
|
|
|
$ruleSatisfied = $this->checkIsDisplayedRecursionProtection()
|
|
|
|
&& $controllingField->isDisplayed($data)
|
|
|
|
&& $rule->validateAgainstFormData($data);
|
2020-01-10 05:36:04 +01:00
|
|
|
|
|
|
|
if ($conjunction === '||' && $ruleSatisfied) {
|
|
|
|
$conditionsSatisfied = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ($conjunction === '&&' && !$ruleSatisfied) {
|
|
|
|
$conditionsSatisfied = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// initially displayed - condition fails || initially hidden, condition passes
|
|
|
|
$startDisplayed = $this->ShowOnLoad;
|
|
|
|
return ($startDisplayed xor $conditionsSatisfied);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-28 00:22:15 +02:00
|
|
|
/**
|
|
|
|
* Replaces the set DisplayRulesConjunction with their JS logical operators
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function DisplayRulesConjunctionNice()
|
|
|
|
{
|
2022-04-13 03:52:56 +02:00
|
|
|
return (strtolower($this->DisplayRulesConjunction ?? '') === 'or') ? '||' : '&&';
|
2017-04-28 00:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces boolean ShowOnLoad with its JS string equivalent
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function ShowOnLoadNice()
|
|
|
|
{
|
|
|
|
return ($this->ShowOnLoad) ? 'show' : 'hide';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether this is of type EditableCheckBoxField
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isCheckBoxField()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether this is of type EditableRadioField
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isRadioField()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determined is this is of type EditableCheckboxGroupField
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isCheckBoxGroupField()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2010-11-02 21:14:28 +01:00
|
|
|
}
|