Merged [47039]: Introduces custom actions to TableListField and ComplexTableField. By default, the show, edit and delete actions are included.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@60433 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Hayden Smith 2008-08-11 23:24:54 +00:00
parent 4258361337
commit 1220f6d86d
5 changed files with 96 additions and 24 deletions

View File

@ -110,7 +110,24 @@ class ComplexTableField extends TableListField {
* @var boolean
*/
protected $relationAutoSetting = true;
public $actions = array(
'show' => array(
'label' => 'Show',
'icon' => 'cms/images/show.png',
'class' => 'popuplink showlink',
),
'edit' => array(
'label' => 'Edit',
'icon' => 'cms/images/edit.gif',
'class' => 'popuplink editlink',
),
'delete' => array(
'label' => 'Delete',
'icon' => 'cms/images/delete.gif',
'class' => 'popuplink deletelink',
),
);
static $url_handlers = array(
'item/$ID' => 'handleItem',
@ -793,6 +810,14 @@ class ComplexTableField_Item extends TableListField_Item {
function DeleteLink() {
return $this->Link() . "/delete";
}
/**
* @param String $action
* @return boolean
*/
function IsDefaultAction($action) {
return ($action == $this->parent->defaultAction);
}
}

View File

@ -79,6 +79,8 @@ class TableListField extends FormField {
*/
public $Markable;
public $MarkableTitle = null;
/**
* @var $readOnly boolean Deprecated, please use $permssions instead
*/
@ -86,6 +88,7 @@ class TableListField extends FormField {
/**
* @var $permissions array Influence output without having to subclass the template.
* See $actions for adding your custom actions/permissions.
*/
protected $permissions = array(
//"print",
@ -93,6 +96,29 @@ class TableListField extends FormField {
"delete"
);
/**
* @var $actions array Action that can be performed on a single row-entry.
* Has to correspond to a method in a TableListField-class (or subclass).
* Actions can be disabled through $permissions.
* Format (key is used for the methodname and CSS-class):
* array(
* 'delete' => array('label' => 'Delete', 'icon' => 'cms/images/delete.gif')
* )
*/
public $actions = array(
'delete' => array(
'label' => 'Delete',
'icon' => 'cms/images/delete.gif',
'class' => 'deletelink'
)
);
/**
* @var $defaultAction String Action being executed when clicking on table-row (defaults to "show").
* Mostly needed in ComplexTableField-subclass.
*/
public $defaultAction = '';
/**
* @var $customQuery Specify custom query, e.g. for complicated having/groupby-constructs.
* Caution: TableListField automatically selects the ID from the {@sourceClass}, because it relies
@ -297,6 +323,23 @@ JS
return (in_array($SQL_fieldName,$query->select) || stripos($sql,"AS {$SQL_fieldName}"));
}
/**
* Dummy function to get number of actions originally generated in
* TableListField_Item.
*
* @return DataObjectSet
*/
function Actions() {
$allowedActions = new DataObjectSet();
foreach($this->actions as $actionName => $actionSettings) {
if($this->Can($actionName)) {
$allowedActions->push(new ViewableData());
}
}
return $allowedActions;
}
/**
* Provide a custom query to compute sourceItems. This is the preferred way to using
* {@setSourceItems}, because we can still paginate.

View File

@ -13,12 +13,30 @@ ComplexTableField.prototype = {
rules['#'+this.id+' table.data a.popuplink'] = {onclick: this.openPopup.bind(this)};
rules['#'+this.id+' table.data tbody td'] = {onclick: this.openPopup.bind(this)};
// invoke row action-link based on default-action set in classname
if(defaultAction) {
rules['#'+this.id+' table.data tbody td'] = {
onclick: function(e) {
var link = $$('.'+defaultAction, Event.element(e).parentNode)[0].getAttribute('href');
this.openPopup(null, link);
return false;
}.bind(this)
};
}
Behaviour.register(rules);
// HACK If already in a popup, we can't allow add (doesn't save existing relation correctly)
if(window != top) $$('#'+this.id+' table.data a.addlink').each(function(el) {Element.hide(el);});
},
getDefaultAction: function() {
// try to get link class from <td class="action default"><a href="...
var links = $$('#'+this.id+' table.data tbody .default a');
// fall back to first given link
if(!links || !links[0]) links = $$('#'+this.id+' table.data tbody .action a');
return (links && links[0]) ? $A(Element.classNames(links[0])).last() : false;
},
/**
* @param href, table Optional dom object (use for external triggering without an event)
*/

View File

@ -26,9 +26,7 @@
<% end_if %>
</th>
<% end_control %>
<% if Can(show) %><th width="18">&nbsp;</th><% end_if %>
<% if Can(edit) %><th width="18">&nbsp;</th><% end_if %>
<% if Can(delete) %><th width="18">&nbsp;</th><% end_if %>
<% control Actions %><th width="18">&nbsp;</th><% end_control %>
</tr>
</thead>
<tfoot>
@ -39,9 +37,7 @@
<% control SummaryFields %>
<td<% if Function %> class="$Function"<% end_if %>>$SummaryValue</td>
<% end_control %>
<% if Can(show) %><td width="18">&nbsp;</td><% end_if %>
<% if Can(edit) %><td width="18">&nbsp;</td><% end_if %>
<% if Can(delete) %><td width="18">&nbsp;</td><% end_if %>
<% control Actions %><td width="18">&nbsp;</td><% end_control %>
</tr>
<% end_if %>
<% if Can(add) %>
@ -50,9 +46,7 @@
<td colspan="$ItemCount">
<a class="popuplink addlink" href="$AddLink" alt="add"><img src="cms/images/add.gif" alt="<% _t('ADDITEM', 'add') %>" /><% _t('ADDITEM', 'Add', PR_MEDIUM, 'Add [name]') %> $Title</a>
</td>
<% if Can(show) %><td width="18">&nbsp;</td><% end_if %>
<% if Can(edit) %><td width="18">&nbsp;</td><% end_if %>
<% if Can(delete) %><td width="18">&nbsp;</td><% end_if %>
<% control Actions %><td width="18">&nbsp;</td><% end_control %>
</tr>
<% end_if %>
</tfoot>
@ -64,14 +58,8 @@
<% control Fields %>
<td>$Value</td>
<% end_control %>
<% if Can(show) %>
<td width="18"><a class="popuplink showlink" href="$ShowLink" target="_blank"><img src="cms/images/show.png" alt="<% _t('SHOW', 'show') %>" /></a></td>
<% end_if %>
<% if Can(edit) %>
<td width="18"><a class="popuplink editlink" href="$EditLink" target="_blank"><img src="cms/images/edit.gif" alt="<% _t('EDIT', 'edit') %>" /></a></td>
<% end_if %>
<% if Can(delete) %>
<td width="18"><a class="deletelink" href="$DeleteLink" title="<% _t('DELETEROW', 'Delete this row') %>"><img src="cms/images/delete.gif" alt="<% _t('DELETE', 'delete') %>" /></a></td>
<% control Actions %>
<td width="16" class="action<% if Default %> default<% end_if %>"><a class="$Class" href="$Link"><% if Icon %><img src="$Icon" alt="$Label" /><% else %>$Label<% end_if %></a></td>
<% end_if %>
</tr>
<% end_control %>
@ -79,9 +67,7 @@
<tr class="notfound">
<% if Markable %><th width="18">&nbsp;</th><% end_if %>
<td colspan="$Headings.Count"><i><% _t('NOITEMSFOUND', 'No items found') %></i></td>
<% if Can(show) %><td width="18">&nbsp;</td><% end_if %>
<% if Can(edit) %><td width="18">&nbsp;</td><% end_if %>
<% if Can(delete) %><td width="18">&nbsp;</td><% end_if %>
<% control Actions %><td width="18">&nbsp;</td><% end_control %>
</tr>
<% end_if %>
</tbody>

View File

@ -3,7 +3,7 @@
<% control Fields %>
<td class="field-$Title.HTMLATT">$Value</td>
<% end_control %>
<% if Can(delete) %>
<td width="16"><a class="deletelink" href="$DeleteLink"><img src="cms/images/delete.gif" alt="<% _t('DELETE') %>" /></a></td>
<% end_if %>
<% control Actions %>
<td width="16"><a class="$Class" href="$Link"><% if Icon %><img src="$Icon" alt="$Label" /><% else %>$Label<% end_if %></a></td>
<% end_control %>
</tr>