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",
|
|
|
|
"CanDelete" => "Boolean",
|
2009-04-17 04:26:40 +02:00
|
|
|
"CustomParameter" => "Varchar",
|
|
|
|
"OptionallyDisplay" => "Boolean"
|
2008-09-29 05:18:23 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
static $defaults = array(
|
|
|
|
"CanDelete" => "1"
|
|
|
|
);
|
|
|
|
|
|
|
|
static $has_one = array(
|
|
|
|
"Parent" => "SiteTree",
|
|
|
|
);
|
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
/**
|
|
|
|
* @var bool Is this field readonly to the user
|
|
|
|
*/
|
2008-09-29 05:18:23 +02:00
|
|
|
protected $readonly;
|
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2008-09-29 05:18:23 +02:00
|
|
|
$this->setField('Default', -1);
|
|
|
|
parent::__construct( $record, $isSingleton );
|
|
|
|
}
|
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
/**
|
|
|
|
* Set the FieldEditor object for this field.
|
|
|
|
*
|
|
|
|
* @param FieldEditor The Editor window you wish to use
|
|
|
|
*/
|
|
|
|
protected function setEditor($editor) {
|
|
|
|
$this->editor = $editor;
|
|
|
|
}
|
|
|
|
|
2008-09-29 05:18:23 +02:00
|
|
|
function EditSegment() {
|
|
|
|
return $this->renderWith('EditableFormField');
|
|
|
|
}
|
|
|
|
|
|
|
|
function isReadonly() {
|
|
|
|
return $this->readonly;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ClassName() {
|
|
|
|
return $this->class;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function hasAddableOptions() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether or not this field needs to show the extra
|
|
|
|
* options dropdown list
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function showExtraOptions() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-09-29 05:18:23 +02:00
|
|
|
function makeReadonly() {
|
|
|
|
$this->readonly = true;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ReadonlyEditSegment() {
|
|
|
|
$this->readonly = true;
|
|
|
|
return $this->EditSegment();
|
|
|
|
}
|
|
|
|
|
|
|
|
function TitleField() {
|
|
|
|
$titleAttr = Convert::raw2att($this->Title);
|
2009-04-17 04:26:40 +02:00
|
|
|
$readOnlyAttr = ($this->readonly) ? ' 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 />";
|
|
|
|
}
|
|
|
|
|
|
|
|
function Name() {
|
|
|
|
return "Fields[".$this->ID."]";
|
|
|
|
}
|
|
|
|
|
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'] : "";
|
2008-09-29 05:18:23 +02:00
|
|
|
$this->Sort = isset($data['Sort']) ? $data['Sort'] : null;
|
2008-09-29 07:42:46 +02:00
|
|
|
$this->CustomParameter = isset($data['CustomParameter']) ? $data['CustomParameter'] : null;
|
|
|
|
$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;
|
|
|
|
$this->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
function ExtraOptions() {
|
|
|
|
|
|
|
|
$baseName = "Fields[$this->ID]";
|
|
|
|
$extraOptions = new FieldSet();
|
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
if(!$this->Parent()->hasMethod('hideExtraOption')){
|
|
|
|
$extraOptions->push(new CheckboxField($baseName . "[Required]", _t('EditableFormField.REQUIRED', 'Required?'), $this->Required));
|
|
|
|
}
|
|
|
|
elseif(!$this->Parent()->hideExtraOption('Required')){
|
|
|
|
$extraOptions->push(new CheckboxField($baseName . "[Required]", _t('EditableFormField.REQUIRED', 'Required?'), $this->Required));
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
if($this->Parent()->hasMethod('getExtraOptionsForField')) {
|
|
|
|
$extraFields = $this->Parent()->getExtraOptionsForField($this);
|
2008-09-29 05:18:23 +02:00
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
foreach($extraFields as $extraField) {
|
|
|
|
$extraOptions->push($extraField);
|
|
|
|
}
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
if($this->readonly) {
|
2008-09-29 05:18:23 +02:00
|
|
|
$extraOptions = $extraOptions->makeReadonly();
|
2009-04-17 04:26:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// support for optionally display field
|
|
|
|
// $extraOptions->push(new CheckboxField($baseName ."[OptionallyDisplay]", _t('EditableFormField.OPTIONALLYDISPLAY', 'Optionally Display Field'), $this->OptionallyDisplay));
|
|
|
|
|
2008-09-29 05:18:23 +02:00
|
|
|
return $extraOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a FormField to appear on the front end
|
|
|
|
*/
|
|
|
|
function getFormField() {
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFilterField() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an evaluation appropriate for a filter clause
|
|
|
|
* @todo: escape the string
|
|
|
|
*/
|
|
|
|
function filterClause( $value ) {
|
|
|
|
// Not filtering on this field
|
|
|
|
|
|
|
|
if( $value == '-1' )
|
|
|
|
return "";
|
|
|
|
else
|
|
|
|
return "`{$this->name}` = '$value'";
|
|
|
|
}
|
|
|
|
|
|
|
|
function showInReports() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepopulate( $value ) {
|
|
|
|
$this->prepopulateFromMap( $this->parsePrepopulateValue( $value ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function parsePrepopulateValue( $value ) {
|
|
|
|
$paramList = explode( ',', $value );
|
|
|
|
$paramMap = array();
|
|
|
|
|
|
|
|
foreach( $paramList as $param ) {
|
|
|
|
|
|
|
|
if( preg_match( '/([^=]+)=(.+)/', $param, $match ) ) {
|
|
|
|
if( isset( $paramMap[$match[1]] ) && is_array( $paramMap[$match[1]] ) ) {
|
|
|
|
$paramMap[$match[1]][] = $match[2];
|
|
|
|
} else if( isset( $paramMap[$match[1]] ) ) {
|
|
|
|
$paramMap[$match[1]] = array( $paramMap[$match[1]] );
|
|
|
|
$paramMap[$match[1]][] = $match[2];
|
|
|
|
} else {
|
|
|
|
$paramMap[$match[1]] = $match[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $paramMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
function CustomParameter() {
|
|
|
|
return $this->CustomParameter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|