2008-09-29 05:18:23 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Allows CMS user to create forms dynamically.
|
2009-04-15 06:23:43 +02:00
|
|
|
*
|
|
|
|
* @package userforms
|
2008-09-29 05:18:23 +02:00
|
|
|
*/
|
|
|
|
class FieldEditor extends FormField {
|
|
|
|
|
2009-04-18 04:49:46 +02:00
|
|
|
protected $haveFormOptions = true;
|
|
|
|
|
2008-09-29 05:18:23 +02:00
|
|
|
protected $readonly = false;
|
|
|
|
|
|
|
|
function isReadonly() {
|
|
|
|
return $this->readonly;
|
|
|
|
}
|
|
|
|
|
|
|
|
function performReadonlyTransformation() {
|
2009-02-24 03:56:38 +01:00
|
|
|
$clone = clone $this;
|
|
|
|
$clone->setReadonly(true);
|
|
|
|
return $clone;
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeReadonly() {
|
|
|
|
return $this->performReadonlyTransformation();
|
|
|
|
}
|
|
|
|
|
|
|
|
function FieldHolder() {
|
|
|
|
return $this->renderWith("FieldEditor");
|
|
|
|
}
|
|
|
|
|
|
|
|
function Fields() {
|
2009-04-17 04:26:40 +02:00
|
|
|
Requirements::css("userforms/css/FieldEditor.css");
|
2009-04-18 04:49:46 +02:00
|
|
|
Requirements::javascript("jsparty/jquery/ui/ui.core.js");
|
|
|
|
Requirements::javascript("jsparty/jquery/ui/ui.sortable.js");
|
2009-04-17 04:26:40 +02:00
|
|
|
Requirements::javascript("userforms/javascript/UserForm.js");
|
2008-09-29 05:18:23 +02:00
|
|
|
|
|
|
|
$relationName = $this->name;
|
|
|
|
|
|
|
|
$fields = $this->form->getRecord()->$relationName();
|
2009-04-20 01:22:22 +02:00
|
|
|
|
2009-04-19 23:20:52 +02:00
|
|
|
if($this->readonly) {
|
2008-09-29 05:18:23 +02:00
|
|
|
$readonlyFields = new DataObjectSet();
|
|
|
|
|
2009-04-19 23:20:52 +02:00
|
|
|
foreach($fields as $field) {
|
|
|
|
$field->setEditor($this);
|
|
|
|
$readonlyFields->push($field->makeReadonly());
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$fields = $readonlyFields;
|
|
|
|
}
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
2009-03-25 03:06:28 +01:00
|
|
|
/**
|
|
|
|
* Return a DataObjectSet of all the addable fields to populate
|
|
|
|
* the add field menu
|
|
|
|
*
|
|
|
|
* @return DataObjectSet
|
|
|
|
*/
|
|
|
|
function CreatableFields() {
|
|
|
|
$fields = ClassInfo::subclassesFor('EditableFormField');
|
|
|
|
|
|
|
|
if($fields) {
|
|
|
|
array_shift($fields); // get rid of subclass 0
|
|
|
|
$output = new DataObjectSet();
|
|
|
|
foreach($fields as $field => $title) {
|
|
|
|
// get the nice title and strip out field
|
|
|
|
$niceTitle = trim(str_ireplace("Field", "", eval("return $title::\$singular_name;")));
|
|
|
|
$title = trim(str_ireplace("Editable", "", $title));
|
|
|
|
|
|
|
|
$output->push(new ArrayData(array(
|
|
|
|
'ClassName' => $title,
|
|
|
|
'Title' => "$niceTitle"
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
/**
|
|
|
|
* Handles saving the page. Needs to keep an eye on fields
|
|
|
|
* and options which have been removed / added
|
|
|
|
*
|
|
|
|
* @param DataObject Record to Save it In
|
|
|
|
*/
|
2008-09-29 05:18:23 +02:00
|
|
|
function saveInto(DataObject $record) {
|
|
|
|
|
|
|
|
$name = $this->name;
|
2009-04-17 04:26:40 +02:00
|
|
|
|
2008-09-29 05:18:23 +02:00
|
|
|
$fieldSet = $record->$name();
|
|
|
|
|
2009-04-15 00:59:46 +02:00
|
|
|
// @todo shouldn't we deal with customFormActions on that object?
|
2008-09-29 05:18:23 +02:00
|
|
|
$record->EmailOnSubmit = isset( $_REQUEST[$name]['EmailOnSubmit'] ) ? "1" : "0";
|
2009-04-19 09:34:30 +02:00
|
|
|
$record->SubmitButtonText = isset($_REQUEST[$name]['SubmitButtonText']) ? $_REQUEST[$name]['SubmitButtonText'] : "";
|
2009-04-15 00:59:46 +02:00
|
|
|
$record->ShowClearButton = isset($_REQUEST[$name]['ShowClearButton']) ? "1" : "0";
|
2008-09-29 05:18:23 +02:00
|
|
|
|
|
|
|
// store the field IDs and delete the missing fields
|
|
|
|
// alternatively, we could delete all the fields and re add them
|
|
|
|
$missingFields = array();
|
2009-04-17 04:26:40 +02:00
|
|
|
|
|
|
|
foreach($fieldSet as $existingField){
|
2008-09-29 05:18:23 +02:00
|
|
|
$missingFields[$existingField->ID] = $existingField;
|
|
|
|
}
|
2009-04-17 04:26:40 +02:00
|
|
|
|
2008-09-29 05:18:23 +02:00
|
|
|
if($_REQUEST[$name]){
|
|
|
|
foreach( array_keys( $_REQUEST[$name] ) as $newEditableID ) {
|
|
|
|
$newEditableData = $_REQUEST[$name][$newEditableID];
|
2009-04-17 04:26:40 +02:00
|
|
|
|
|
|
|
$editable = DataObject::get_one('EditableFormField', "(`ParentID`='{$record->ID}' OR `ParentID`=0) AND `EditableFormField`.`ID`='$newEditableID'" );
|
|
|
|
|
|
|
|
// check if we are updating an existing field. One odd thing is a 'deleted' field
|
|
|
|
// still exists in the post data (ID) so we need to check for type.
|
|
|
|
if($editable && isset($missingFields[$editable->ID]) && isset($newEditableData['Type'])) {
|
|
|
|
// check if it has been labelled as deleted
|
|
|
|
if(isset($newEditableData['Title']) && $newEditableData['Title'] != 'field-node-deleted') {
|
|
|
|
unset($missingFields[$editable->ID]);
|
|
|
|
}
|
|
|
|
}
|
2008-09-29 05:18:23 +02:00
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
if($editable) {
|
|
|
|
if($editable->ParentID == 0) {
|
|
|
|
$editable->ParentID = $record->ID;
|
|
|
|
}
|
|
|
|
$editable->populateFromPostData($newEditableData);
|
|
|
|
}
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
|
|
|
}
|
2009-04-17 04:26:40 +02:00
|
|
|
|
2008-09-29 05:18:23 +02:00
|
|
|
// remove the fields not saved
|
|
|
|
foreach($missingFields as $removedField) {
|
|
|
|
if(is_numeric($removedField->ID)) $removedField->delete();
|
|
|
|
}
|
2009-04-17 04:26:40 +02:00
|
|
|
|
2008-09-29 05:18:23 +02:00
|
|
|
if($record->hasMethod('customFormSave')) {
|
|
|
|
$record->customFormSave( $_REQUEST[$name], $record );
|
|
|
|
}
|
|
|
|
|
|
|
|
if($record->hasMethod( 'processNewFormFields')) {
|
|
|
|
$record->processNewFormFields();
|
|
|
|
}
|
|
|
|
}
|
2009-04-17 04:26:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a field to the field editor. Called via a ajax get request
|
|
|
|
* from the userdefinedform javascript
|
|
|
|
*
|
|
|
|
* @return bool|html
|
|
|
|
*/
|
|
|
|
public function addfield() {
|
2008-09-29 05:18:23 +02:00
|
|
|
// get the last field in this form editor
|
|
|
|
$parentID = $this->form->getRecord()->ID;
|
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
if($parentID) {
|
|
|
|
$parentID = Convert::raw2sql($parentID); // who knows what could happen
|
|
|
|
$highestSort = DB::query("SELECT MAX(Sort) FROM EditableFormField WHERE ParentID = '$parentID'");
|
|
|
|
$sort = $highestSort->value() + 1;
|
|
|
|
|
|
|
|
$className = "Editable" . ucfirst($_REQUEST['Type']);
|
|
|
|
$name = $this->name;
|
|
|
|
if(is_subclass_of($className, "EditableFormField")) {
|
|
|
|
$e = new $className();
|
|
|
|
$e->write();
|
2008-09-29 05:18:23 +02:00
|
|
|
$e->ParentID = $this->form->getRecord()->ID;
|
2009-04-17 04:26:40 +02:00
|
|
|
$e->Name = $e->class . $e->ID;
|
|
|
|
$e->write();
|
|
|
|
return $e->EditSegment();
|
|
|
|
}
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
2009-04-17 04:26:40 +02:00
|
|
|
return false;
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
/**
|
|
|
|
* Return the html for a field option such as a
|
|
|
|
* dropdown field or a radio check box field
|
|
|
|
*
|
|
|
|
* @return bool|html
|
|
|
|
*/
|
|
|
|
public function addoptionfield() {
|
|
|
|
// passed via the ajax
|
|
|
|
$parent = (isset($_REQUEST['Parent'])) ? $_REQUEST['Parent'] : false;
|
|
|
|
$text = (isset($_REQUEST['Text'])) ? $_REQUEST['Text'] : "";
|
|
|
|
|
|
|
|
// work out the sort by getting the sort of the last field in the form +1
|
|
|
|
if($parent) {
|
|
|
|
$sql_parent = Convert::raw2sql($parent);
|
|
|
|
$highestSort = DB::query("SELECT MAX(Sort) FROM EditableOption WHERE ParentID = '$sql_parent'");
|
|
|
|
$sort = $highestSort->value() + 1;
|
|
|
|
|
|
|
|
if($parent) {
|
|
|
|
$object = new EditableOption();
|
|
|
|
$object->write();
|
|
|
|
$object->ParentID = $parent;
|
|
|
|
$object->Sort = $sort;
|
|
|
|
$object->Name = 'option' . $object->ID;
|
|
|
|
$object->Title = $text;
|
|
|
|
$object->write();
|
|
|
|
return $object->EditSegment();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function setHaveFormOptions($bool){
|
|
|
|
$this->haveFormOptions = $bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getHaveFormOptions(){
|
|
|
|
return $this->haveFormOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
function FormOptions() {
|
|
|
|
if($this->haveFormOptions){
|
2009-04-14 04:06:18 +02:00
|
|
|
if($this->form->getRecord()->hasMethod('customFormActions')) {
|
|
|
|
$newFields = $this->form->getRecord()->customFormActions($this->readonly);
|
2008-09-29 05:18:23 +02:00
|
|
|
|
|
|
|
foreach( $newFields as $newField ) {
|
|
|
|
$newField->setName( "{$this->name}[{$newField->Name()}]" );
|
|
|
|
}
|
2009-04-14 04:06:18 +02:00
|
|
|
if($this->readonly) {
|
|
|
|
$newFields = $newFields->makeReadonly();
|
|
|
|
}
|
|
|
|
return $newFields;
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|