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
|
2008-09-29 05:18:23 +02:00
|
|
|
*/
|
2009-12-07 03:04:20 +01:00
|
|
|
|
2008-09-29 05:18:23 +02:00
|
|
|
class EditableFormField extends DataObject {
|
|
|
|
|
|
|
|
static $default_sort = "Sort";
|
|
|
|
|
|
|
|
static $db = array(
|
|
|
|
"Name" => "Varchar",
|
|
|
|
"Title" => "Varchar(255)",
|
|
|
|
"Default" => "Varchar",
|
|
|
|
"Sort" => "Int",
|
|
|
|
"Required" => "Boolean",
|
2009-04-24 00:52:08 +02:00
|
|
|
"CustomErrorMessage" => "Varchar(255)",
|
|
|
|
"CustomRules" => "Text",
|
2010-05-31 10:20:04 +02:00
|
|
|
"CustomSettings" => "Text",
|
|
|
|
"CustomParameter" => "Varchar(200)"
|
2008-09-29 05:18:23 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
static $has_one = array(
|
2010-03-12 04:09:36 +01:00
|
|
|
"Parent" => "UserDefinedForm",
|
2008-09-29 05:18:23 +02:00
|
|
|
);
|
2009-09-23 00:36:53 +02:00
|
|
|
|
|
|
|
static $extensions = array(
|
|
|
|
"Versioned('Stage', 'Live')"
|
|
|
|
);
|
2010-05-31 10:20:04 +02:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2010-09-03 07:06:13 +02:00
|
|
|
/**
|
|
|
|
* Template to render the form field into
|
|
|
|
*
|
|
|
|
* @return String
|
|
|
|
*/
|
2008-09-29 05:18:23 +02:00
|
|
|
function EditSegment() {
|
|
|
|
return $this->renderWith('EditableFormField');
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function canDelete() {
|
2010-09-03 07:06:13 +02:00
|
|
|
return ($this->Parent()->canEdit() && !$this->isReadonly());
|
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
|
|
|
|
*/
|
|
|
|
public function canEdit() {
|
2010-09-03 07:06:13 +02:00
|
|
|
return ($this->Parent()->canEdit() && !$this->isReadonly());
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete this form from a given stage
|
|
|
|
*
|
|
|
|
* Wrapper for the {@link Versioned} deleteFromStage function
|
|
|
|
*/
|
|
|
|
public function doDeleteFromStage($stage) {
|
|
|
|
$this->deleteFromStage($stage);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-14 23:40:03 +02:00
|
|
|
/**
|
|
|
|
* Show this form on load or not
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2009-12-06 23:41:47 +01:00
|
|
|
function getShowOnLoad() {
|
2009-05-06 06:20:46 +02:00
|
|
|
return ($this->getSetting('ShowOnLoad') == "Show" || $this->getSetting('ShowOnLoad') == '') ? true : false;
|
2009-05-06 05:34:40 +02:00
|
|
|
}
|
|
|
|
|
2009-04-25 03:14:32 +02:00
|
|
|
/**
|
|
|
|
* To prevent having tables for each fields minor settings we store it as
|
|
|
|
* a serialized array in the database.
|
|
|
|
*
|
|
|
|
* @return Array Return all the Settings
|
|
|
|
*/
|
2010-09-08 00:44:43 +02:00
|
|
|
public function getSettings() {
|
2009-06-15 11:35:34 +02:00
|
|
|
return (!empty($this->CustomSettings)) ? unserialize($this->CustomSettings) : array();
|
2009-04-25 03:14:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the custom settings for this field as we store the minor details in
|
|
|
|
* a serialized array in the database
|
|
|
|
*
|
|
|
|
* @param Array the custom settings
|
|
|
|
*/
|
2010-09-08 00:44:43 +02:00
|
|
|
public function setSettings($settings = array()) {
|
2009-04-25 03:14:32 +02:00
|
|
|
$this->CustomSettings = serialize($settings);
|
|
|
|
}
|
|
|
|
|
2010-09-03 07:06:13 +02:00
|
|
|
/**
|
|
|
|
* Set a given field setting. Appends the option to the settings or overrides
|
|
|
|
* the existing value
|
|
|
|
*
|
|
|
|
* @param String key
|
|
|
|
* @param String value
|
|
|
|
*/
|
2010-09-08 00:44:43 +02:00
|
|
|
public function setSetting($key, $value) {
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
2009-04-25 03:14:32 +02:00
|
|
|
/**
|
|
|
|
* Return just one custom setting or empty string if it does
|
|
|
|
* not exist
|
|
|
|
*
|
|
|
|
* @param String Value to use as key
|
|
|
|
* @return String
|
|
|
|
*/
|
2009-04-27 04:43:35 +02:00
|
|
|
public function getSetting($setting) {
|
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() {
|
2009-04-20 05:07:57 +02:00
|
|
|
return 'userforms/images/' . strtolower($this->class) . '.png';
|
|
|
|
}
|
|
|
|
|
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-06-08 06:42:17 +02:00
|
|
|
|
2009-04-24 00:52:08 +02:00
|
|
|
/**
|
|
|
|
* Return the Custom Validation fields for this
|
|
|
|
* field for the CMS
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function Dependencies() {
|
|
|
|
return ($this->CustomRules) ? unserialize($this->CustomRules) : array();
|
|
|
|
}
|
2010-07-07 11:17:37 +02:00
|
|
|
|
2009-04-24 00:52:08 +02:00
|
|
|
/**
|
|
|
|
* Return the custom validation fields for the field
|
|
|
|
*
|
|
|
|
* @return DataObjectSet
|
|
|
|
*/
|
|
|
|
public function CustomRules() {
|
|
|
|
$output = new DataObjectSet();
|
|
|
|
$fields = $this->Parent()->Fields();
|
|
|
|
|
|
|
|
// check for existing ones
|
2010-09-08 05:20:28 +02:00
|
|
|
if($rules = $this->Dependencies()) {
|
|
|
|
foreach($rules as $rule => $data) {
|
|
|
|
// recreate all the field object to prevent caching
|
|
|
|
$outputFields = new DataObjectSet();
|
|
|
|
|
|
|
|
foreach($fields as $field) {
|
|
|
|
$new = clone $field;
|
2009-05-01 06:11:31 +02:00
|
|
|
|
2010-09-08 05:20:28 +02:00
|
|
|
$new->isSelected = ($new->Name == $data['ConditionField']) ? true : false;
|
|
|
|
$outputFields->push($new);
|
2009-04-24 00:52:08 +02:00
|
|
|
}
|
2010-09-08 05:20:28 +02:00
|
|
|
|
|
|
|
$output->push(new ArrayData(array(
|
|
|
|
'FieldName' => $this->getFieldName(),
|
|
|
|
'Display' => $data['Display'],
|
|
|
|
'Fields' => $outputFields,
|
|
|
|
'ConditionField' => $data['ConditionField'],
|
|
|
|
'ConditionOption' => $data['ConditionOption'],
|
|
|
|
'Value' => $data['Value']
|
|
|
|
)));
|
2009-04-24 00:52:08 +02:00
|
|
|
}
|
|
|
|
}
|
2010-09-08 05:20:28 +02:00
|
|
|
|
2009-04-24 00:52:08 +02:00
|
|
|
return $output;
|
|
|
|
}
|
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
|
|
|
|
*/
|
2008-09-29 05:18:23 +02:00
|
|
|
function TitleField() {
|
2010-09-08 05:20:28 +02:00
|
|
|
$field = new TextField('Title', _t('EditableFormField.ENTERQUESTION', 'Enter Question'), Convert::raw2att($this->Title));
|
2010-09-03 07:06:13 +02:00
|
|
|
$field->setName($this->getFieldName('Title'));
|
|
|
|
|
2010-09-08 05:20:28 +02:00
|
|
|
if(!$this->canEdit()) {
|
|
|
|
return $field->performReadonlyTransformation();
|
|
|
|
}
|
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
|
|
|
|
2009-04-24 00:52:08 +02:00
|
|
|
/**
|
|
|
|
* Return the base name for this form field in the
|
2010-07-07 11:17:37 +02:00
|
|
|
* form builder. Optionally returns the name with the given field
|
|
|
|
*
|
|
|
|
* @param String Field Name
|
2009-04-24 00:52:08 +02:00
|
|
|
*
|
|
|
|
* @return String
|
|
|
|
*/
|
2010-07-07 11:17:37 +02:00
|
|
|
public function getFieldName($field = false) {
|
|
|
|
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
|
|
|
/**
|
2010-07-07 11:17:37 +02:00
|
|
|
* Generate a name for the Setting field
|
|
|
|
*
|
|
|
|
* @param String name of the setting
|
|
|
|
* @return String
|
2009-04-24 00:52:08 +02:00
|
|
|
*/
|
2010-07-07 11:17:37 +02:00
|
|
|
public function getSettingFieldName($field) {
|
|
|
|
$name = $this->getFieldName('CustomSettings');
|
|
|
|
|
|
|
|
return $name . '[' . $field .']';
|
2009-04-24 00:52:08 +02:00
|
|
|
}
|
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
/**
|
|
|
|
* How to save the data submitted in this field into
|
|
|
|
* the database object which this field represents.
|
|
|
|
*
|
|
|
|
* Any class's which call this should also call
|
|
|
|
* {@link parent::populateFromPostData()} to ensure
|
|
|
|
* that this method is called
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public function populateFromPostData($data) {
|
2010-07-07 11:17:37 +02:00
|
|
|
$this->Title = (isset($data['Title'])) ? $data['Title']: "";
|
|
|
|
$this->Default = (isset($data['Default'])) ? $data['Default'] : "";
|
|
|
|
$this->Sort = (isset($data['Sort'])) ? $data['Sort'] : null;
|
|
|
|
$this->Required = !empty($data['Required']) ? 1 : 0;
|
|
|
|
$this->Name = $this->class.$this->ID;
|
|
|
|
$this->CustomRules = "";
|
2010-09-08 05:20:28 +02:00
|
|
|
$this->CustomErrorMessage = (isset($data['CustomErrorMessage'])) ? $data['CustomErrorMessage'] : "";
|
|
|
|
$this->CustomSettings = "";
|
2010-07-07 11:17:37 +02:00
|
|
|
|
2009-04-25 03:14:32 +02:00
|
|
|
// custom settings
|
|
|
|
if(isset($data['CustomSettings'])) {
|
2010-09-08 00:44:43 +02:00
|
|
|
$this->setSettings($data['CustomSettings']);
|
2009-04-25 03:14:32 +02:00
|
|
|
}
|
|
|
|
|
2009-04-24 00:52:08 +02:00
|
|
|
// custom validation
|
|
|
|
if(isset($data['CustomRules'])) {
|
|
|
|
$rules = array();
|
2010-09-08 05:20:28 +02:00
|
|
|
|
2009-04-24 00:52:08 +02:00
|
|
|
foreach($data['CustomRules'] as $key => $value) {
|
2009-05-01 06:11:31 +02:00
|
|
|
|
2009-04-24 00:52:08 +02:00
|
|
|
if(is_array($value)) {
|
|
|
|
$fieldValue = (isset($value['Value'])) ? $value['Value'] : '';
|
2010-09-08 00:44:43 +02:00
|
|
|
|
2009-04-24 00:52:08 +02:00
|
|
|
if(isset($value['ConditionOption']) && $value['ConditionOption'] == "Blank" || $value['ConditionOption'] == "NotBlank") {
|
|
|
|
$fieldValue = "";
|
|
|
|
}
|
2010-09-08 00:44:43 +02:00
|
|
|
|
2009-04-24 00:52:08 +02:00
|
|
|
$rules[] = array(
|
|
|
|
'Display' => (isset($value['Display'])) ? $value['Display'] : "",
|
|
|
|
'ConditionField' => (isset($value['ConditionField'])) ? $value['ConditionField'] : "",
|
|
|
|
'ConditionOption' => (isset($value['ConditionOption'])) ? $value['ConditionOption'] : "",
|
|
|
|
'Value' => $fieldValue
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2010-09-08 00:44:43 +02:00
|
|
|
|
2009-04-24 00:52:08 +02:00
|
|
|
$this->CustomRules = serialize($rules);
|
|
|
|
}
|
2010-09-08 05:20:28 +02:00
|
|
|
|
2008-09-29 05:18:23 +02:00
|
|
|
$this->write();
|
|
|
|
}
|
2010-09-08 05:20:28 +02:00
|
|
|
|
2009-05-06 05:34:40 +02:00
|
|
|
/**
|
|
|
|
* Implement custom field Configuration on this field. Includes such things as
|
|
|
|
* settings and options of a given editable form field
|
|
|
|
*
|
|
|
|
* @return FieldSet
|
|
|
|
*/
|
|
|
|
public function getFieldConfiguration() {
|
2010-07-07 11:17:37 +02:00
|
|
|
return new FieldSet(
|
|
|
|
new TextField(
|
|
|
|
$this->getSettingFieldName('RightTitle'),
|
|
|
|
_t('EditableFormField.RIGHTTITLE', 'Right Title'),
|
|
|
|
$this->getSetting('RightTitle')
|
|
|
|
)
|
|
|
|
);
|
2009-05-06 05:34:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append custom validation fields to the default 'Validation'
|
|
|
|
* section in the editable options view
|
|
|
|
*
|
|
|
|
* @return FieldSet
|
|
|
|
*/
|
|
|
|
public function getFieldValidationOptions() {
|
2009-05-14 23:40:03 +02:00
|
|
|
$fields = new FieldSet(
|
2010-07-07 11:17:37 +02:00
|
|
|
new CheckboxField($this->getFieldName('Required'), _t('EditableFormField.REQUIRED', 'Is this field Required?'), $this->Required),
|
|
|
|
new TextField($this->getFieldName('CustomErrorMessage'), _t('EditableFormField.CUSTOMERROR','Custom Error Message'), $this->CustomErrorMessage)
|
2009-05-06 05:34:40 +02:00
|
|
|
);
|
2009-05-14 23:40:03 +02:00
|
|
|
|
|
|
|
if(!$this->canEdit()) {
|
|
|
|
foreach($fields as $field) {
|
|
|
|
$fields->performReadonlyTransformation();
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
*/
|
2008-09-29 05:18:23 +02:00
|
|
|
function showInReports() {
|
|
|
|
return true;
|
|
|
|
}
|
2009-05-06 05:34:40 +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() {
|
|
|
|
return array();
|
|
|
|
}
|
2009-12-07 03:04:20 +01:00
|
|
|
}
|