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
|
|
|
*/
|
|
|
|
class EditableFormField extends DataObject {
|
|
|
|
|
|
|
|
static $default_sort = "Sort";
|
|
|
|
|
|
|
|
static $db = array(
|
|
|
|
"Name" => "Varchar",
|
|
|
|
"Title" => "Varchar(255)",
|
|
|
|
"Default" => "Varchar",
|
|
|
|
"Sort" => "Int",
|
|
|
|
"Required" => "Boolean",
|
2009-04-20 05:07:57 +02:00
|
|
|
"CanDelete" => "Boolean",
|
2009-04-24 00:52:08 +02:00
|
|
|
"CustomErrorMessage" => "Varchar(255)",
|
|
|
|
"CustomRules" => "Text",
|
2009-08-11 06:42:50 +02:00
|
|
|
"CustomSettings" => "Text",
|
|
|
|
"ShowOnLoad" => "Boolean"
|
2008-09-29 05:18:23 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
static $defaults = array(
|
2009-04-27 01:39:40 +02:00
|
|
|
"CanDelete" => "1",
|
|
|
|
"ShowOnLoad" => "1"
|
2008-09-29 05:18:23 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
static $has_one = array(
|
|
|
|
"Parent" => "SiteTree",
|
|
|
|
);
|
2009-05-14 23:40:03 +02:00
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
/**
|
|
|
|
* @var FieldEditor The current editor
|
|
|
|
*/
|
2008-09-29 05:18:23 +02:00
|
|
|
protected $editor;
|
2009-04-17 04:26:40 +02:00
|
|
|
|
2009-06-08 06:42:17 +02:00
|
|
|
protected $readonly;
|
2009-08-11 06:42:50 +02:00
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
/**
|
|
|
|
* Construct a new EditableFormField Object.
|
|
|
|
*
|
|
|
|
* @param array|null $record This will be null for a new database record.
|
|
|
|
* @param boolean $isSingleton This this to true if this is a singleton() object, a stub for calling methods.
|
|
|
|
*/
|
|
|
|
public function __construct($record = null, $isSingleton = false) {
|
2009-05-14 23:40:03 +02:00
|
|
|
parent::__construct($record, $isSingleton);
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
/**
|
|
|
|
* Set the FieldEditor object for this field.
|
|
|
|
*
|
|
|
|
* @param FieldEditor The Editor window you wish to use
|
|
|
|
*/
|
2009-05-14 23:40:03 +02:00
|
|
|
public function setEditor($editor) {
|
2009-04-17 04:26:40 +02:00
|
|
|
$this->editor = $editor;
|
|
|
|
}
|
|
|
|
|
2009-06-08 06:42:17 +02:00
|
|
|
/**
|
|
|
|
* Set this formfield to readonly
|
|
|
|
*/
|
|
|
|
public function setReadonly() {
|
|
|
|
$this->readonly = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is this multipleoption field readonly to the user
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isReadonly() {
|
|
|
|
return $this->readonly;
|
|
|
|
}
|
|
|
|
|
2008-09-29 05:18:23 +02:00
|
|
|
function EditSegment() {
|
|
|
|
return $this->renderWith('EditableFormField');
|
|
|
|
}
|
|
|
|
|
|
|
|
function ClassName() {
|
|
|
|
return $this->class;
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
return $this->Parent()->canEdit();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether a user can edit this form field
|
|
|
|
* based on whether they can edit the page
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canEdit() {
|
|
|
|
return $this->Parent()->canEdit();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show this form on load or not
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2009-05-06 05:34:40 +02:00
|
|
|
function ShowOnLoad() {
|
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
|
|
|
|
*/
|
|
|
|
protected function getFieldSettings() {
|
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
|
|
|
|
*/
|
|
|
|
protected function setFieldSettings($settings = array()) {
|
|
|
|
$this->CustomSettings = serialize($settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2009-04-25 03:14:32 +02:00
|
|
|
$settings = $this->getFieldSettings();
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function Icon() {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Return the custom validation fields for the field
|
|
|
|
*
|
|
|
|
* @return DataObjectSet
|
|
|
|
*/
|
|
|
|
public function CustomRules() {
|
|
|
|
$output = new DataObjectSet();
|
|
|
|
$fields = $this->Parent()->Fields();
|
|
|
|
|
|
|
|
// check for existing ones
|
|
|
|
if($this->CustomRules) {
|
|
|
|
$rules = unserialize($this->CustomRules);
|
2009-05-01 06:11:31 +02:00
|
|
|
|
2009-04-24 00:52:08 +02:00
|
|
|
if($rules) {
|
|
|
|
foreach($rules as $rule => $data) {
|
|
|
|
// recreate all the field object to prevent caching
|
|
|
|
$outputFields = new DataObjectSet();
|
|
|
|
foreach($fields as $field) {
|
|
|
|
$new = clone $field;
|
|
|
|
$new->isSelected = ($new->Name == $data['ConditionField']) ? true : false;
|
|
|
|
$outputFields->push($new);
|
|
|
|
}
|
|
|
|
$output->push(new ArrayData(array(
|
2009-05-22 01:31:46 +02:00
|
|
|
'FieldName' => $this->FieldName(),
|
2009-04-24 00:52:08 +02:00
|
|
|
'Display' => $data['Display'],
|
|
|
|
'Fields' => $outputFields,
|
|
|
|
'ConditionField' => $data['ConditionField'],
|
|
|
|
'ConditionOption' => $data['ConditionOption'],
|
|
|
|
'Value' => $data['Value']
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
2009-05-14 23:40:03 +02:00
|
|
|
|
2008-09-29 05:18:23 +02:00
|
|
|
function TitleField() {
|
|
|
|
$titleAttr = Convert::raw2att($this->Title);
|
2009-05-14 23:40:03 +02:00
|
|
|
$readOnlyAttr = (!$this->canEdit()) ? ' disabled="disabled"' : '';
|
2008-09-29 05:18:23 +02:00
|
|
|
|
|
|
|
return "<input type=\"text\" class=\"text\" title=\"("._t('EditableFormField.ENTERQUESTION', 'Enter Question').")\" value=\"$titleAttr\" name=\"Fields[{$this->ID}][Title]\"$readOnlyAttr />";
|
|
|
|
}
|
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
|
|
|
|
* form builder
|
|
|
|
*
|
|
|
|
* @return String
|
|
|
|
*/
|
2009-05-22 01:31:46 +02:00
|
|
|
public function FieldName() {
|
2008-09-29 05:18:23 +02:00
|
|
|
return "Fields[".$this->ID."]";
|
|
|
|
}
|
|
|
|
|
2009-04-24 00:52:08 +02:00
|
|
|
/**
|
|
|
|
* @todo Fix this - shouldn't name be returning name?!?
|
|
|
|
*/
|
|
|
|
public function BaseName() {
|
|
|
|
return $this->Name;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
$this->Title = (isset($data['Title'])) ? $data['Title']: "";
|
|
|
|
$this->Default = (isset($data['Default'])) ? $data['Default'] : "";
|
2009-05-22 01:31:46 +02:00
|
|
|
$this->Sort = (isset($data['Sort'])) ? $data['Sort'] : null;
|
2008-09-29 07:42:46 +02:00
|
|
|
$this->Required = !empty($data['Required']) ? 1 : 0;
|
|
|
|
$this->CanDelete = (isset($data['CanDelete']) && !$data['CanDelete']) ? 0 : 1;
|
2008-09-29 05:18:23 +02:00
|
|
|
$this->Name = $this->class.$this->ID;
|
2009-04-20 05:07:57 +02:00
|
|
|
$this->CustomErrorMessage = (isset($data['CustomErrorMessage'])) ? $data['CustomErrorMessage'] : "";
|
2009-04-24 00:52:08 +02:00
|
|
|
$this->CustomRules = "";
|
2009-04-25 03:14:32 +02:00
|
|
|
$this->CustomSettings = "";
|
2009-04-24 00:52:08 +02:00
|
|
|
$this->ShowOnLoad = (isset($data['ShowOnLoad']) && $data['ShowOnLoad'] == "Show") ? 1 : 0;
|
2009-04-25 03:14:32 +02:00
|
|
|
// custom settings
|
|
|
|
if(isset($data['CustomSettings'])) {
|
|
|
|
$this->setFieldSettings($data['CustomSettings']);
|
|
|
|
}
|
|
|
|
|
2009-04-24 00:52:08 +02:00
|
|
|
// custom validation
|
|
|
|
if(isset($data['CustomRules'])) {
|
|
|
|
$rules = array();
|
|
|
|
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'] : '';
|
|
|
|
if(isset($value['ConditionOption']) && $value['ConditionOption'] == "Blank" || $value['ConditionOption'] == "NotBlank") {
|
|
|
|
$fieldValue = "";
|
|
|
|
}
|
|
|
|
$rules[] = array(
|
|
|
|
'Display' => (isset($value['Display'])) ? $value['Display'] : "",
|
|
|
|
'ConditionField' => (isset($value['ConditionField'])) ? $value['ConditionField'] : "",
|
|
|
|
'ConditionOption' => (isset($value['ConditionOption'])) ? $value['ConditionOption'] : "",
|
|
|
|
'Value' => $fieldValue
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->CustomRules = serialize($rules);
|
|
|
|
}
|
2008-09-29 05:18:23 +02:00
|
|
|
$this->write();
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
return new FieldSet();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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(
|
2009-05-06 05:34:40 +02:00
|
|
|
new CheckboxField("Fields[$this->ID][Required]", _t('EditableFormField.REQUIRED', 'Is this field Required?'), $this->Required),
|
|
|
|
new TextField("Fields[$this->ID][CustomErrorMessage]", _t('EditableFormField.CUSTOMERROR','Custom Error Message'), $this->CustomErrorMessage)
|
|
|
|
);
|
2009-05-14 23:40:03 +02:00
|
|
|
|
|
|
|
if(!$this->canEdit()) {
|
|
|
|
foreach($fields as $field) {
|
|
|
|
$fields->performReadonlyTransformation();
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
function showInReports() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-05-06 05:34:40 +02:00
|
|
|
function prepopulate($value) {
|
|
|
|
$this->prepopulateFromMap($this->parsePrepopulateValue($value));
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
|
|
|
|
2009-05-06 05:34:40 +02:00
|
|
|
protected function parsePrepopulateValue($value) {
|
|
|
|
$paramList = explode(',', $value);
|
2008-09-29 05:18:23 +02:00
|
|
|
$paramMap = array();
|
|
|
|
|
2009-05-06 05:34:40 +02:00
|
|
|
foreach($paramList as $param) {
|
2008-09-29 05:18:23 +02:00
|
|
|
|
2009-05-06 05:34:40 +02:00
|
|
|
if(preg_match( '/([^=]+)=(.+)/', $param, $match)) {
|
|
|
|
if(isset($paramMap[$match[1]]) && is_array($paramMap[$match[1]])) {
|
2008-09-29 05:18:23 +02:00
|
|
|
$paramMap[$match[1]][] = $match[2];
|
2009-05-06 05:34:40 +02:00
|
|
|
} else if(isset( $paramMap[$match[1]])) {
|
|
|
|
$paramMap[$match[1]] = array($paramMap[$match[1]]);
|
2008-09-29 05:18:23 +02:00
|
|
|
$paramMap[$match[1]][] = $match[2];
|
|
|
|
} else {
|
|
|
|
$paramMap[$match[1]] = $match[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $paramMap;
|
|
|
|
}
|
|
|
|
|
2009-05-06 05:34:40 +02:00
|
|
|
protected function prepopulateFromMap($paramMap) {
|
2009-04-17 04:26:40 +02:00
|
|
|
foreach($paramMap as $field => $fieldValue) {
|
|
|
|
if(!is_array($fieldValue)) {
|
2008-09-29 05:18:23 +02:00
|
|
|
$this->$field = $fieldValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-03-25 03:06:28 +01:00
|
|
|
|
2008-09-29 05:18:23 +02:00
|
|
|
function Type() {
|
|
|
|
return $this->class;
|
|
|
|
}
|
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();
|
|
|
|
}
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
|
|
|
?>
|