diff --git a/code/editor/EditableFormField.php b/code/editor/EditableFormField.php index 33bf923..ff3b469 100755 --- a/code/editor/EditableFormField.php +++ b/code/editor/EditableFormField.php @@ -35,6 +35,8 @@ class EditableFormField extends DataObject { */ protected $editor; + protected $readonly; + /** * Construct a new EditableFormField Object. * @@ -54,6 +56,22 @@ class EditableFormField extends DataObject { $this->editor = $editor; } + /** + * 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; + } + function EditSegment() { return $this->renderWith('EditableFormField'); } @@ -156,6 +174,7 @@ class EditableFormField extends DataObject { public function showExtraOptions() { return true; } + /** * Return the Custom Validation fields for this * field for the CMS diff --git a/code/editor/EditableMultipleOptionField.php b/code/editor/EditableMultipleOptionField.php index f69f2bd..fc4cbc1 100644 --- a/code/editor/EditableMultipleOptionField.php +++ b/code/editor/EditableMultipleOptionField.php @@ -22,8 +22,6 @@ class EditableMultipleOptionField extends EditableFormField { "Options" => "EditableOption" ); - protected $readonly; - /** * Deletes all the options attached to this field before * deleting the field. Keeps stray options from floating @@ -94,22 +92,6 @@ class EditableMultipleOptionField extends EditableFormField { return true; } - /** - * Set this multipleoptionfield to readonly - */ - protected function ReadonlyOption() { - $this->readonly = true; - } - - /** - * Is this multipleoption field readonly to the user - * - * @return bool - */ - public function isReadonly() { - return $this->readonly; - } - /** * Return the form field for this object in the front * end form view diff --git a/code/editor/FieldEditor.php b/code/editor/FieldEditor.php index b07e60d..bddbfa6 100755 --- a/code/editor/FieldEditor.php +++ b/code/editor/FieldEditor.php @@ -13,14 +13,33 @@ class FieldEditor extends FormField { } /** - * Can a user edit this field - * - * @return bool + * Can a user edit this field? + * @return boolean */ public function canEdit() { + if($this->readonly) return false; return $this->form->getRecord()->canEdit(); } + /** + * Can a user delete this field? + * @return boolean + */ + public function canDelete() { + if($this->readonly) return false; + return $this->form->getRecord()->canDelete(); + } + + function performReadonlyTransformation() { + $this->readonly = true; + $fields = $this->Fields(); + if($fields) foreach($fields as $field) { + $field->setReadonly(); + } + + return $this->customise(array('Fields' => $fields)); + } + /** * Return the Form Fields for the user forms * @@ -33,19 +52,20 @@ class FieldEditor extends FormField { Requirements::javascript("userforms/javascript/UserForm.js"); $relationName = $this->name; - $fields = $this->form->getRecord()->$relationName(); - foreach($fields as $field) { - if(!$this->canEdit()) { - if(is_a($field, 'FormField')) { - $fields->remove($field); - $fields->push($field->performReadonlyTransformation()); + if($fields) { + foreach($fields as $field) { + if(!$this->canEdit()) { + if(is_a($field, 'FormField')) { + $fields->remove($field); + $fields->push($field->performReadonlyTransformation()); + } } + $field->setEditor($this); } - - $field->setEditor($this); } + return $fields; } diff --git a/templates/EditableFormField.ss b/templates/EditableFormField.ss index 0c84ba4..fce98d3 100755 --- a/templates/EditableFormField.ss +++ b/templates/EditableFormField.ss @@ -18,10 +18,8 @@ <% end_if %> <% if canDelete %> - - <% _t('DELETE', 'Delete') %> - - <% end_if %> + <% _t('DELETE', 'Delete') %> + <% end_if %> <% if showExtraOptions %> diff --git a/templates/FieldEditor.ss b/templates/FieldEditor.ss index 0f0ae3b..12ec1c2 100755 --- a/templates/FieldEditor.ss +++ b/templates/FieldEditor.ss @@ -5,11 +5,7 @@