BUGFIX: fixed permission system for the field editor view

This commit is contained in:
Will Rossiter 2009-05-14 21:40:03 +00:00
parent 603764de01
commit 86bc91d1f2
4 changed files with 68 additions and 64 deletions

View File

@ -30,11 +30,6 @@ class EditableFormField extends DataObject {
"Parent" => "SiteTree", "Parent" => "SiteTree",
); );
/**
* @var bool Is this field readonly to the user
*/
protected $readonly;
/** /**
* @var FieldEditor The current editor * @var FieldEditor The current editor
*/ */
@ -47,8 +42,7 @@ class EditableFormField extends DataObject {
* @param boolean $isSingleton This this to true if this is a singleton() object, a stub for calling methods. * @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) { public function __construct($record = null, $isSingleton = false) {
$this->setField('Default', -1); parent::__construct($record, $isSingleton);
parent::__construct( $record, $isSingleton );
} }
/** /**
@ -56,7 +50,7 @@ class EditableFormField extends DataObject {
* *
* @param FieldEditor The Editor window you wish to use * @param FieldEditor The Editor window you wish to use
*/ */
protected function setEditor($editor) { public function setEditor($editor) {
$this->editor = $editor; $this->editor = $editor;
} }
@ -64,14 +58,35 @@ class EditableFormField extends DataObject {
return $this->renderWith('EditableFormField'); return $this->renderWith('EditableFormField');
} }
function isReadonly() {
return $this->readonly;
}
function ClassName() { function ClassName() {
return $this->class; return $this->class;
} }
/**
* 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
*/
function ShowOnLoad() { function ShowOnLoad() {
return ($this->getSetting('ShowOnLoad') == "Show" || $this->getSetting('ShowOnLoad') == '') ? true : false; return ($this->getSetting('ShowOnLoad') == "Show" || $this->getSetting('ShowOnLoad') == '') ? true : false;
} }
@ -186,19 +201,9 @@ class EditableFormField extends DataObject {
return $output; return $output;
} }
function makeReadonly() {
$this->readonly = true;
return $this;
}
function ReadonlyEditSegment() {
$this->readonly = true;
return $this->EditSegment();
}
function TitleField() { function TitleField() {
$titleAttr = Convert::raw2att($this->Title); $titleAttr = Convert::raw2att($this->Title);
$readOnlyAttr = ($this->readonly) ? ' disabled="disabled"' : ''; $readOnlyAttr = (!$this->canEdit()) ? ' disabled="disabled"' : '';
return "<input type=\"text\" class=\"text\" title=\"("._t('EditableFormField.ENTERQUESTION', 'Enter Question').")\" value=\"$titleAttr\" name=\"Fields[{$this->ID}][Title]\"$readOnlyAttr />"; return "<input type=\"text\" class=\"text\" title=\"("._t('EditableFormField.ENTERQUESTION', 'Enter Question').")\" value=\"$titleAttr\" name=\"Fields[{$this->ID}][Title]\"$readOnlyAttr />";
} }
@ -288,10 +293,16 @@ class EditableFormField extends DataObject {
* @return FieldSet * @return FieldSet
*/ */
public function getFieldValidationOptions() { public function getFieldValidationOptions() {
return new FieldSet( $fields = new FieldSet(
new CheckboxField("Fields[$this->ID][Required]", _t('EditableFormField.REQUIRED', 'Is this field Required?'), $this->Required), 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) new TextField("Fields[$this->ID][CustomErrorMessage]", _t('EditableFormField.CUSTOMERROR','Custom Error Message'), $this->CustomErrorMessage)
); );
if(!$this->canEdit()) {
foreach($fields as $field) {
$fields->performReadonlyTransformation();
}
}
} }
/** /**

View File

@ -8,26 +8,18 @@ class FieldEditor extends FormField {
protected $haveFormOptions = true; protected $haveFormOptions = true;
protected $readonly = false;
function isReadonly() {
return $this->readonly;
}
function performReadonlyTransformation() {
$clone = clone $this;
$clone->setReadonly(true);
return $clone;
}
function makeReadonly() {
return $this->performReadonlyTransformation();
}
function FieldHolder() { function FieldHolder() {
return $this->renderWith("FieldEditor"); return $this->renderWith("FieldEditor");
} }
/**
* Can a user edit this field
*
* @return bool
*/
public function canEdit() {
return $this->form->getRecord()->canEdit();
}
function Fields() { function Fields() {
Requirements::css("userforms/css/FieldEditor.css"); Requirements::css("userforms/css/FieldEditor.css");
Requirements::javascript("jsparty/jquery/ui/ui.core.js"); Requirements::javascript("jsparty/jquery/ui/ui.core.js");
@ -38,15 +30,14 @@ class FieldEditor extends FormField {
$fields = $this->form->getRecord()->$relationName(); $fields = $this->form->getRecord()->$relationName();
if($this->readonly) { foreach($fields as $field) {
$readonlyFields = new DataObjectSet(); if(!$this->canEdit()) {
if(is_a($field, 'FormField')) {
foreach($fields as $field) { $readonlyFields->push($field->performReadonlyTransformation());
$field->setEditor($this); }
$readonlyFields->push($field->makeReadonly());
} }
$fields = $readonlyFields; $field->setEditor($this);
} }
return $fields; return $fields;
} }

View File

@ -17,7 +17,7 @@
<a class="moreOptions" href="#" title="<% _t('SHOWOPTIONS', 'Show Options') %>"><% _t('SHOWOPTIONS','Show Options') %></a> <a class="moreOptions" href="#" title="<% _t('SHOWOPTIONS', 'Show Options') %>"><% _t('SHOWOPTIONS','Show Options') %></a>
<% end_if %> <% end_if %>
<% if CanDelete %> <% if canDelete %>
<a class="delete" href="#" title="<% _t('DELETE', 'Delete') %>"> <a class="delete" href="#" title="<% _t('DELETE', 'Delete') %>">
<% _t('DELETE', 'Delete') %> <% _t('DELETE', 'Delete') %>
</a> </a>
@ -31,7 +31,7 @@
<legend><% _t('OPTIONS', 'Options') %></legend> <legend><% _t('OPTIONS', 'Options') %></legend>
<ul class="editableOptions" id="$Name.Attr-list"> <ul class="editableOptions" id="$Name.Attr-list">
<% if isReadonly %> <% if canEdit %>
<% control Options %> <% control Options %>
$ReadonlyOption $ReadonlyOption
<% end_control %> <% end_control %>

View File

@ -1,12 +1,14 @@
<div class="MenuHolder"> <% if canEdit %>
<h2><% _t('ADD', 'Add') %></h2> <div class="MenuHolder">
<select name="AddUserFormField" id="AddUserFormField"> <h2><% _t('ADD', 'Add') %></h2>
<option value=""><% _t('SELECTAFIELD', 'Select a Field') %></option> <select name="AddUserFormField" id="AddUserFormField">
<option value=""><% _t('SELECTAFIELD', 'Select a Field') %></option>
<% control CreatableFields %> <% control CreatableFields %>
<option value="$ClassName">$Title</option> <option value="$ClassName">$Title</option>
<% end_control %> <% end_control %>
</select> </select>
<input type="submit" class="action" value="<% _t('ADD', 'Add') %>" /> <input type="submit" class="action" value="<% _t('ADD', 'Add') %>" />
</div> </div>
<% end_if %>