2008-09-29 05:18:23 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2009-04-17 04:26:40 +02:00
|
|
|
* Represents the base class of a editable form field
|
|
|
|
* object like {@link EditableTextField}.
|
|
|
|
*
|
|
|
|
* @package userforms
|
2015-07-24 04:37:48 +02:00
|
|
|
* @method DataList DisplayRules() List of EditableCustomRule objects
|
2008-09-29 05:18:23 +02:00
|
|
|
*/
|
|
|
|
class EditableFormField extends DataObject {
|
2015-07-24 04:37:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default sort order
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $default_sort = '"Sort"';
|
2008-09-29 05:18:23 +02:00
|
|
|
|
2012-04-14 13:03:43 +02:00
|
|
|
/**
|
|
|
|
* A list of CSS classes that can be added
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public static $allowed_css = array();
|
2015-07-24 04:37:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $summary_fields = array(
|
|
|
|
'Title'
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-04-03 03:34:43 +02:00
|
|
|
private static $db = array(
|
2008-09-29 05:18:23 +02:00
|
|
|
"Name" => "Varchar",
|
|
|
|
"Title" => "Varchar(255)",
|
2015-07-24 04:37:48 +02:00
|
|
|
"Default" => "Varchar(255)",
|
2008-09-29 05:18:23 +02:00
|
|
|
"Sort" => "Int",
|
|
|
|
"Required" => "Boolean",
|
2009-04-24 00:52:08 +02:00
|
|
|
"CustomErrorMessage" => "Varchar(255)",
|
2015-07-24 04:37:48 +02:00
|
|
|
|
|
|
|
"CustomRules" => "Text", // @deprecated from 2.0
|
|
|
|
"CustomSettings" => "Text", // @deprecated from 2.0
|
|
|
|
"Migrated" => "Boolean", // set to true when migrated
|
|
|
|
|
|
|
|
"ExtraClass" => "Text", // from CustomSettings
|
|
|
|
"RightTitle" => "Varchar(255)", // from CustomSettings
|
|
|
|
"ShowOnLoad" => "Boolean(1)", // from CustomSettings
|
2008-09-29 05:18:23 +02:00
|
|
|
);
|
2012-07-17 06:09:31 +02:00
|
|
|
|
2015-07-24 04:37:48 +02:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-04-03 03:34:43 +02:00
|
|
|
private static $has_one = array(
|
2010-03-12 04:09:36 +01:00
|
|
|
"Parent" => "UserDefinedForm",
|
2008-09-29 05:18:23 +02:00
|
|
|
);
|
2015-07-24 04:37:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Built in extensions required
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-04-03 03:34:43 +02:00
|
|
|
private static $extensions = array(
|
2009-09-23 00:36:53 +02:00
|
|
|
"Versioned('Stage', 'Live')"
|
|
|
|
);
|
2015-07-24 04:37:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $has_many = array(
|
|
|
|
"DisplayRules" => "EditableCustomRule.Parent" // from CustomRules
|
|
|
|
);
|
|
|
|
|
2009-06-08 06:42:17 +02:00
|
|
|
/**
|
2010-09-03 07:06:13 +02:00
|
|
|
* @var bool
|
2009-06-08 06:42:17 +02:00
|
|
|
*/
|
2010-09-03 07:06:13 +02:00
|
|
|
protected $readonly;
|
2015-07-24 04:37:48 +02:00
|
|
|
|
2010-09-03 07:06:13 +02:00
|
|
|
/**
|
|
|
|
* Set the visibility of an individual form field
|
|
|
|
*
|
|
|
|
* @param bool
|
|
|
|
*/
|
|
|
|
public function setReadonly($readonly = true) {
|
|
|
|
$this->readonly = $readonly;
|
2009-06-08 06:42:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-09-03 07:06:13 +02:00
|
|
|
* Returns whether this field is readonly
|
|
|
|
*
|
2009-06-08 06:42:17 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2010-09-03 07:06:13 +02:00
|
|
|
private function isReadonly() {
|
2009-06-08 06:42:17 +02:00
|
|
|
return $this->readonly;
|
|
|
|
}
|
2015-07-24 04:37:48 +02:00
|
|
|
|
2010-09-03 07:06:13 +02:00
|
|
|
/**
|
2015-07-24 04:37:48 +02:00
|
|
|
* @return FieldList
|
|
|
|
*/
|
|
|
|
public function getCMSFields() {
|
|
|
|
$fields = new FieldList(new TabSet('Root'));
|
|
|
|
|
|
|
|
// Main tab
|
|
|
|
$fields->addFieldsToTab(
|
|
|
|
'Root.Main',
|
|
|
|
array(
|
|
|
|
ReadonlyField::create(
|
|
|
|
'Type',
|
|
|
|
_t('EditableFormField.TYPE', 'Type'),
|
|
|
|
$this->i18n_singular_name()
|
|
|
|
),
|
|
|
|
LiteralField::create(
|
|
|
|
'MergeField',
|
|
|
|
_t(
|
|
|
|
'EditableFormField.MERGEFIELDNAME',
|
|
|
|
'<div class="field readonly">' .
|
|
|
|
'<label class="left">Merge field</label>' .
|
|
|
|
'<div class="middleColumn">' .
|
|
|
|
'<span class="readonly">$' . $this->Name . '</span>' .
|
|
|
|
'</div>' .
|
|
|
|
'</div>'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
TextField::create('Title'),
|
|
|
|
TextField::create('Default', _t('EditableFormField.DEFAULT', 'Default value')),
|
|
|
|
TextField::create('RightTitle', _t('EditableFormField.RIGHTTITLE', 'Right title'))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Custom settings
|
|
|
|
if (!empty(self::$allowed_css)) {
|
|
|
|
$cssList = array();
|
|
|
|
foreach(self::$allowed_css as $k => $v) {
|
|
|
|
if (!is_array($v)) {
|
|
|
|
$cssList[$k]=$v;
|
|
|
|
} elseif ($k === $this->ClassName) {
|
|
|
|
$cssList = array_merge($cssList, $v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields->addFieldToTab('Root.Main',
|
|
|
|
DropdownField::create(
|
|
|
|
'ExtraClass',
|
|
|
|
_t('EditableFormField.EXTRACLASS_TITLE', 'Extra Styling/Layout'),
|
|
|
|
$cssList
|
|
|
|
)->setDescription(_t(
|
|
|
|
'EditableFormField.EXTRACLASS_SELECT',
|
|
|
|
'Select from the list of allowed styles'
|
|
|
|
))
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$fields->addFieldToTab('Root.Main',
|
|
|
|
TextField::create(
|
|
|
|
'ExtraClass',
|
|
|
|
_t('EditableFormField.EXTRACLASS_Title', 'Extra CSS Classes')
|
|
|
|
)->setDescription(_t(
|
|
|
|
'EditableFormField.EXTRACLASS_MULTIPLE',
|
|
|
|
'Separate each CSS class with a single space'
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validation
|
|
|
|
$fields->addFieldsToTab(
|
|
|
|
'Root.Validation',
|
|
|
|
$this->getFieldValidationOptions()
|
|
|
|
);
|
|
|
|
|
|
|
|
$editableColumns = new GridFieldEditableColumns();
|
|
|
|
$editableColumns->setDisplayFields(array(
|
|
|
|
'Display' => '',
|
|
|
|
'ConditionFieldID' => function($record, $column, $grid) {
|
|
|
|
return DropdownField::create(
|
|
|
|
$column,
|
|
|
|
'',
|
|
|
|
EditableFormField::get()
|
|
|
|
->filter(array(
|
|
|
|
'ParentID' => $this->ParentID
|
|
|
|
))
|
|
|
|
->exclude(array(
|
|
|
|
'ID' => $this->ID
|
|
|
|
))
|
|
|
|
->map('ID', 'Title')
|
|
|
|
);
|
|
|
|
},
|
|
|
|
'ConditionOption' => function($record, $column, $grid) {
|
|
|
|
$options = Config::inst()->get('EditableCustomRule', 'condition_options');
|
|
|
|
return DropdownField::create($column, '', $options);
|
|
|
|
},
|
|
|
|
'FieldValue' => function($record, $column, $grid) {
|
|
|
|
return TextField::create($column);
|
|
|
|
},
|
|
|
|
'ParentID' => function($record, $column, $grid) {
|
|
|
|
return HiddenField::create($column, '', $this->ID);
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
// Custom rules
|
|
|
|
$customRulesConfig = GridFieldConfig::create()
|
|
|
|
->addComponents(
|
|
|
|
$editableColumns,
|
|
|
|
new GridFieldButtonRow(),
|
|
|
|
new GridFieldToolbarHeader(),
|
|
|
|
new GridFieldAddNewInlineButton(),
|
|
|
|
new GridFieldDeleteAction(),
|
|
|
|
new GridState_Component()
|
|
|
|
);
|
|
|
|
|
|
|
|
$fields->addFieldsToTab('Root.DisplayRules', array(
|
|
|
|
CheckboxField::create('ShowOnLoad')
|
|
|
|
->setDescription(_t(
|
|
|
|
'EditableFormField.SHOWONLOAD',
|
|
|
|
'Initial visibility before processing these rules'
|
|
|
|
)),
|
|
|
|
GridField::create(
|
|
|
|
'DisplayRules',
|
|
|
|
_t('EditableFormField.CUSTOMRULES', 'Custom Rules'),
|
|
|
|
$this->DisplayRules(),
|
|
|
|
$customRulesConfig
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->extend('updateCMSFields', $fields);
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
2010-09-03 07:06:13 +02:00
|
|
|
*/
|
2015-07-24 04:37:48 +02:00
|
|
|
public function onBeforeWrite() {
|
|
|
|
parent::onBeforeWrite();
|
|
|
|
|
|
|
|
if(!$this->Sort && $this->ParentID) {
|
|
|
|
$parentID = $this->ParentID;
|
|
|
|
$this->Sort = EditableFormField::get()
|
|
|
|
->filter('ParentID', $parentID)
|
|
|
|
->max('Sort') + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function onAfterWrite() {
|
|
|
|
parent::onAfterWrite();
|
|
|
|
|
|
|
|
// Set a field name.
|
|
|
|
if(!$this->Name) {
|
|
|
|
$this->Name = $this->RecordClassName . $this->ID;
|
|
|
|
$this->write();
|
|
|
|
}
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
|
|
|
|
2014-04-16 01:48:10 +02:00
|
|
|
/**
|
|
|
|
* Flag indicating that this field will set its own error message via data-msg='' attributes
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getSetsOwnError() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-05-14 23:40:03 +02:00
|
|
|
/**
|
|
|
|
* Return whether a user can delete this form field
|
|
|
|
* based on whether they can edit the page
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-05-04 03:59:10 +02:00
|
|
|
public function canDelete($member = null) {
|
2015-07-24 04:37:48 +02:00
|
|
|
if($this->Parent()) {
|
|
|
|
return $this->Parent()->canEdit($member) && !$this->isReadonly();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2009-05-14 23:40:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether a user can edit this form field
|
|
|
|
* based on whether they can edit the page
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-05-04 03:59:10 +02:00
|
|
|
public function canEdit($member = null) {
|
2015-07-24 04:37:48 +02:00
|
|
|
if($this->Parent()) {
|
|
|
|
return $this->Parent()->canEdit($member) && !$this->isReadonly();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2009-05-14 23:40:03 +02:00
|
|
|
}
|
|
|
|
|
2009-12-12 09:59:20 +01:00
|
|
|
/**
|
|
|
|
* Publish this Form Field to the live site
|
|
|
|
*
|
|
|
|
* Wrapper for the {@link Versioned} publish function
|
|
|
|
*/
|
|
|
|
public function doPublish($fromStage, $toStage, $createNewVersion = false) {
|
|
|
|
$this->publish($fromStage, $toStage, $createNewVersion);
|
2015-07-24 04:37:48 +02:00
|
|
|
|
|
|
|
// Don't forget to publish the related custom rules...
|
|
|
|
foreach ($this->DisplayRules() as $rule) {
|
|
|
|
$rule->doPublish($fromStage, $toStage, $createNewVersion);
|
|
|
|
}
|
2009-12-12 09:59:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete this form from a given stage
|
|
|
|
*
|
|
|
|
* Wrapper for the {@link Versioned} deleteFromStage function
|
|
|
|
*/
|
|
|
|
public function doDeleteFromStage($stage) {
|
|
|
|
$this->deleteFromStage($stage);
|
2015-07-24 04:37:48 +02:00
|
|
|
|
|
|
|
// Don't forget to delete the related custom rules...
|
|
|
|
foreach ($this->DisplayRules() as $rule) {
|
|
|
|
$rule->deleteFromStage($stage);
|
|
|
|
}
|
2009-12-12 09:59:20 +01:00
|
|
|
}
|
|
|
|
|
2012-10-18 09:01:56 +02:00
|
|
|
/**
|
|
|
|
* checks wether record is new, copied from Sitetree
|
|
|
|
*/
|
|
|
|
function isNew() {
|
|
|
|
if(empty($this->ID)) return true;
|
|
|
|
|
|
|
|
if(is_numeric($this->ID)) return false;
|
|
|
|
|
|
|
|
return stripos($this->ID, 'new') === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* checks if records is changed on stage
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function getIsModifiedOnStage() {
|
|
|
|
// new unsaved fields could be never be published
|
|
|
|
if($this->isNew()) return false;
|
|
|
|
|
|
|
|
$stageVersion = Versioned::get_versionnumber_by_stage('EditableFormField', 'Stage', $this->ID);
|
2015-07-24 04:37:48 +02:00
|
|
|
$liveVersion = Versioned::get_versionnumber_by_stage('EditableFormField', 'Live', $this->ID);
|
2012-10-18 09:01:56 +02:00
|
|
|
|
|
|
|
return ($stageVersion && $stageVersion != $liveVersion);
|
|
|
|
}
|
2009-12-12 09:59:20 +01:00
|
|
|
|
2009-05-14 23:40:03 +02:00
|
|
|
/**
|
2015-07-24 04:37:48 +02:00
|
|
|
* @deprecated since version 4.0
|
2009-04-25 03:14:32 +02:00
|
|
|
*/
|
2010-09-08 00:44:43 +02:00
|
|
|
public function getSettings() {
|
2015-07-24 04:37:48 +02:00
|
|
|
Deprecation::notice('4.0', 'getSettings is deprecated');
|
2009-06-15 11:35:34 +02:00
|
|
|
return (!empty($this->CustomSettings)) ? unserialize($this->CustomSettings) : array();
|
2009-04-25 03:14:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-24 04:37:48 +02:00
|
|
|
* @deprecated since version 4.0
|
2009-04-25 03:14:32 +02:00
|
|
|
*/
|
2010-09-08 00:44:43 +02:00
|
|
|
public function setSettings($settings = array()) {
|
2015-07-24 04:37:48 +02:00
|
|
|
Deprecation::notice('4.0', 'setSettings is deprecated');
|
2009-04-25 03:14:32 +02:00
|
|
|
$this->CustomSettings = serialize($settings);
|
|
|
|
}
|
|
|
|
|
2010-09-03 07:06:13 +02:00
|
|
|
/**
|
2015-07-24 04:37:48 +02:00
|
|
|
* @deprecated since version 4.0
|
2010-09-03 07:06:13 +02:00
|
|
|
*/
|
2010-09-08 00:44:43 +02:00
|
|
|
public function setSetting($key, $value) {
|
2015-07-24 04:37:48 +02:00
|
|
|
Deprecation::notice('4.0', "setSetting({$key}) is deprecated");
|
2010-09-08 00:44:43 +02:00
|
|
|
$settings = $this->getSettings();
|
2010-09-03 07:06:13 +02:00
|
|
|
$settings[$key] = $value;
|
|
|
|
|
2010-09-08 00:44:43 +02:00
|
|
|
$this->setSettings($settings);
|
2010-09-03 07:06:13 +02:00
|
|
|
}
|
2012-04-14 13:03:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the allowed css classes for the extraClass custom setting
|
|
|
|
*
|
|
|
|
* @param array The permissible CSS classes to add
|
|
|
|
*/
|
|
|
|
public function setAllowedCss(array $allowed) {
|
2014-07-06 07:50:21 +02:00
|
|
|
if (is_array($allowed)) {
|
|
|
|
foreach ($allowed as $k => $v) {
|
2012-04-14 13:03:43 +02:00
|
|
|
self::$allowed_css[$k] = (!is_null($v)) ? $v : $k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-25 03:14:32 +02:00
|
|
|
/**
|
2015-07-24 04:37:48 +02:00
|
|
|
* @deprecated since version 4.0
|
2009-04-25 03:14:32 +02:00
|
|
|
*/
|
2009-04-27 04:43:35 +02:00
|
|
|
public function getSetting($setting) {
|
2015-07-24 04:37:48 +02:00
|
|
|
Deprecation::notice("4.0", "getSetting({$setting}) is deprecated");
|
|
|
|
|
2010-09-08 00:44:43 +02:00
|
|
|
$settings = $this->getSettings();
|
2009-04-25 03:14:32 +02:00
|
|
|
if(isset($settings) && count($settings) > 0) {
|
|
|
|
if(isset($settings[$setting])) {
|
|
|
|
return $settings[$setting];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2009-04-20 05:07:57 +02:00
|
|
|
/**
|
|
|
|
* Get the path to the icon for this field type, relative to the site root.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-09-03 07:06:13 +02:00
|
|
|
public function getIcon() {
|
2014-04-16 01:48:10 +02:00
|
|
|
return USERFORMS_DIR . '/images/' . strtolower($this->class) . '.png';
|
2009-04-20 05:07:57 +02:00
|
|
|
}
|
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
/**
|
|
|
|
* Return whether or not this field has addable options
|
|
|
|
* such as a dropdown field or radio set
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2009-05-06 05:34:40 +02:00
|
|
|
public function getHasAddableOptions() {
|
2009-04-17 04:26:40 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether or not this field needs to show the extra
|
|
|
|
* options dropdown list
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function showExtraOptions() {
|
|
|
|
return true;
|
|
|
|
}
|
2009-05-14 23:40:03 +02:00
|
|
|
|
2010-09-03 07:06:13 +02:00
|
|
|
/**
|
|
|
|
* Title field of the field in the backend of the page
|
|
|
|
*
|
|
|
|
* @return TextField
|
|
|
|
*/
|
2012-05-04 03:39:08 +02:00
|
|
|
public function TitleField() {
|
2012-04-14 08:36:50 +02:00
|
|
|
$label = _t('EditableFormField.ENTERQUESTION', 'Enter Question');
|
|
|
|
|
|
|
|
$field = new TextField('Title', $label, $this->getField('Title'));
|
2010-09-03 07:06:13 +02:00
|
|
|
$field->setName($this->getFieldName('Title'));
|
2011-03-03 04:08:09 +01:00
|
|
|
|
2010-09-08 05:20:28 +02:00
|
|
|
if(!$this->canEdit()) {
|
|
|
|
return $field->performReadonlyTransformation();
|
|
|
|
}
|
2011-03-03 04:08:09 +01:00
|
|
|
|
2010-09-03 07:06:13 +02:00
|
|
|
return $field;
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
2009-05-06 05:34:40 +02:00
|
|
|
|
2015-07-24 04:37:48 +02:00
|
|
|
/**
|
|
|
|
* Returns the Title for rendering in the front-end (with XML values escaped)
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-05-04 03:39:08 +02:00
|
|
|
public function getTitle() {
|
2011-03-03 04:08:09 +01:00
|
|
|
return Convert::raw2att($this->getField('Title'));
|
|
|
|
}
|
|
|
|
|
2009-04-24 00:52:08 +02:00
|
|
|
/**
|
2015-07-24 04:37:48 +02:00
|
|
|
* @deprecated since version 4.0
|
2009-04-24 00:52:08 +02:00
|
|
|
*/
|
2010-07-07 11:17:37 +02:00
|
|
|
public function getFieldName($field = false) {
|
2015-07-24 04:37:48 +02:00
|
|
|
Deprecation::notice('4.0', "getFieldName({$field}) is deprecated");
|
2010-07-07 11:17:37 +02:00
|
|
|
return ($field) ? "Fields[".$this->ID."][".$field."]" : "Fields[".$this->ID."]";
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
|
|
|
|
2009-04-24 00:52:08 +02:00
|
|
|
/**
|
2015-07-24 04:37:48 +02:00
|
|
|
* @deprecated since version 4.0
|
2009-04-24 00:52:08 +02:00
|
|
|
*/
|
2012-04-14 08:36:50 +02:00
|
|
|
public function getSettingName($field) {
|
2015-07-24 04:37:48 +02:00
|
|
|
Deprecation::notice('4.0', "getSettingName({$field}) is deprecated");
|
2010-07-07 11:17:37 +02:00
|
|
|
$name = $this->getFieldName('CustomSettings');
|
|
|
|
|
|
|
|
return $name . '[' . $field .']';
|
2009-04-24 00:52:08 +02:00
|
|
|
}
|
|
|
|
|
2009-05-06 05:34:40 +02:00
|
|
|
/**
|
|
|
|
* Append custom validation fields to the default 'Validation'
|
|
|
|
* section in the editable options view
|
|
|
|
*
|
2015-07-24 04:37:48 +02:00
|
|
|
* @return FieldList
|
2009-05-06 05:34:40 +02:00
|
|
|
*/
|
|
|
|
public function getFieldValidationOptions() {
|
2012-04-22 21:17:42 +02:00
|
|
|
$fields = new FieldList(
|
2015-07-24 04:37:48 +02:00
|
|
|
CheckboxField::create('Required', _t('EditableFormField.REQUIRED', 'Is this field Required?')),
|
|
|
|
TextField::create('CustomErrorMessage', _t('EditableFormField.CUSTOMERROR','Custom Error Message'))
|
2009-05-06 05:34:40 +02:00
|
|
|
);
|
2013-12-04 19:06:34 +01:00
|
|
|
|
|
|
|
$this->extend('updateFieldValidationOptions', $fields);
|
2010-07-07 11:17:37 +02:00
|
|
|
|
2009-05-26 07:40:47 +02:00
|
|
|
return $fields;
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-04-21 05:44:13 +02:00
|
|
|
* Return a FormField to appear on the front end. Implement on
|
|
|
|
* your subclass
|
|
|
|
*
|
|
|
|
* @return FormField
|
2008-09-29 05:18:23 +02:00
|
|
|
*/
|
2009-04-21 05:44:13 +02:00
|
|
|
public function getFormField() {
|
2009-04-27 05:58:52 +02:00
|
|
|
user_error("Please implement a getFormField() on your EditableFormClass ". $this->ClassName, E_USER_ERROR);
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
|
|
|
|
2010-02-14 08:53:16 +01:00
|
|
|
/**
|
|
|
|
* Return the instance of the submission field class
|
|
|
|
*
|
|
|
|
* @return SubmittedFormField
|
|
|
|
*/
|
|
|
|
public function getSubmittedFormField() {
|
|
|
|
return new SubmittedFormField();
|
|
|
|
}
|
2010-07-07 11:17:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show this form field (and its related value) in the reports and in emails.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-05-04 03:39:08 +02:00
|
|
|
public function showInReports() {
|
2008-09-29 05:18:23 +02:00
|
|
|
return true;
|
|
|
|
}
|
2012-07-17 06:09:31 +02:00
|
|
|
|
2009-04-21 05:44:13 +02:00
|
|
|
/**
|
|
|
|
* Return the validation information related to this field. This is
|
|
|
|
* interrupted as a JSON object for validate plugin and used in the
|
|
|
|
* PHP.
|
|
|
|
*
|
|
|
|
* @see http://docs.jquery.com/Plugins/Validation/Methods
|
|
|
|
* @return Array
|
|
|
|
*/
|
|
|
|
public function getValidation() {
|
2014-04-16 01:48:10 +02:00
|
|
|
return $this->Required
|
|
|
|
? array('required' => true)
|
|
|
|
: array();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getValidationJSON() {
|
|
|
|
return Convert::raw2json($this->getValidation());
|
2009-04-21 05:44:13 +02:00
|
|
|
}
|
2010-12-19 02:19:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the error message for this field. Either uses the custom
|
|
|
|
* one (if provided) or the default SilverStripe message
|
|
|
|
*
|
|
|
|
* @return Varchar
|
|
|
|
*/
|
|
|
|
public function getErrorMessage() {
|
|
|
|
$title = strip_tags("'". ($this->Title ? $this->Title : $this->Name) . "'");
|
|
|
|
$standard = sprintf(_t('Form.FIELDISREQUIRED', '%s is required').'.', $title);
|
|
|
|
|
2014-02-12 00:14:42 +01:00
|
|
|
// only use CustomErrorMessage if it has a non empty value
|
|
|
|
$errorMessage = (!empty($this->CustomErrorMessage)) ? $this->CustomErrorMessage : $standard;
|
2010-12-19 02:19:19 +01:00
|
|
|
|
2012-04-22 21:25:26 +02:00
|
|
|
return DBField::create_field('Varchar', $errorMessage);
|
2010-12-19 02:19:19 +01:00
|
|
|
}
|
2015-07-24 04:37:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the field taking into account its custom rules.
|
|
|
|
*
|
|
|
|
* @param Array $data
|
|
|
|
* @param UserForm $form
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function validateField($data, $form) {
|
|
|
|
if($this->Required && $this->DisplayRules()->Count() == 0) {
|
|
|
|
$formField = $this->getFormField();
|
|
|
|
|
|
|
|
if(isset($data[$this->Name])) {
|
|
|
|
$formField->setValue($data[$this->Name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(
|
|
|
|
!isset($data[$this->Name]) ||
|
|
|
|
!$data[$this->Name] ||
|
|
|
|
!$formField->validate($form->getValidator())
|
|
|
|
) {
|
|
|
|
$form->addErrorMessage($this->Name, $this->getErrorMessage(), 'bad');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoked by UserFormUpgradeService to migrate settings specific to this field from CustomSettings
|
|
|
|
* to the field proper
|
|
|
|
*
|
|
|
|
* @param array $data Unserialised data
|
|
|
|
*/
|
|
|
|
public function migrateSettings($data) {
|
|
|
|
// Map 'Show' / 'Hide' to boolean
|
|
|
|
if(isset($data['ShowOnLoad'])) {
|
|
|
|
$this->ShowOnLoad = $data['ShowOnLoad'] === '' || ($data['ShowOnLoad'] && $data['ShowOnLoad'] !== 'Hide');
|
|
|
|
unset($data['ShowOnLoad']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate all other settings
|
|
|
|
foreach($data as $key => $value) {
|
|
|
|
if($this->hasField($key)) {
|
|
|
|
$this->setField($key, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-11-02 21:14:28 +01:00
|
|
|
}
|