Commit 40f6be8f authored by Côme Chilliet's avatar Côme Chilliet
Browse files

feat(management) Add enable/disable feature for actions

Paste is disabled until something is copied.
Restore is disabled if there are no snapshots.
parent bceec41a
......@@ -8,10 +8,14 @@
&nbsp;<img src="images/forward-arrow.png" alt="forward arrow"/>
</a>
{menu data=$entry.actions level=$level+1}
{else}
{elseif $entry.enabled}
<a href="#" onClick="document.getElementById('actionmenu').value='{$entry.name|escape}';document.getElementById('exec_act').click();">
<img src="{$entry.icon|escape}" alt="{$entry.name|escape}">&nbsp;{$entry.label|escape}
</a>
{else}
<a>
<img src="{$entry.icon|escape}&amp;disabled=1" alt="{$entry.name|escape}">&nbsp;{$entry.label|escape}
</a>
{/if}
</li>
{/foreach}
......
......@@ -37,6 +37,7 @@ class Action
protected $inline;
protected $callable;
protected $enabledCallable;
protected $minTargets;
protected $maxTargets;
......@@ -99,6 +100,11 @@ class Action
$this->separator = $bool;
}
function setEnableFunction(callable $callable)
{
$this->enabledCallable = $callable;
}
function listActions()
{
return array($this->name);
......@@ -127,10 +133,12 @@ class Action
if (!$this->inmenu) {
return;
}
$actions[] = array(
'name' => $this->name,
'icon' => $this->icon,
'label' => $this->label,
'enabled' => $this->isEnabledFor(),
'separator' => $this->separator,
);
}
......@@ -151,14 +159,24 @@ class Action
return '<img src="images/empty.png" alt=" " class="optional"/>';
}
// TODO? Skip entry if the pseudo filter does not fit
// TODO? Skip depending on type
if (!$this->isEnabledFor($entry)) {
return '<img src="'.htmlentities($this->icon.'&disabled=1', ENT_COMPAT, 'UTF-8').'"'.
' title="'.$this->label.'" alt="'.$this->label.'"/>';
}
// Render
return '<input type="image" src="'.htmlentities($this->icon, ENT_COMPAT, 'UTF-8').'"'.
' title="'.$this->label.'" alt="'.$this->label.'" name="listing_'.$this->name.'_'.$entry->row.'"/>';
}
function isEnabledFor(ListingEntry $entry = NULL)
{
if (isset($this->enabledCallable)) {
return call_user_func($this->enabledCallable, $this->name, $entry);
}
return TRUE;
}
function hasPermission($dn, $type, $template)
{
global $ui;
......
......@@ -223,7 +223,6 @@ class management
array('r')
)
);
// TODO: disable paste when there is nothing to paste
$this->registerAction(
new Action(
'paste', _('Paste'), 'geticon.php?context=actions&icon=edit-paste&size=16',
......@@ -231,6 +230,7 @@ class management
array('w')
)
);
$this->actions['paste']->setEnableFunction(array($this, 'enablePaste'));
}
$this->registerAction(
......@@ -256,6 +256,7 @@ class management
)
);
$this->actions['snapshot']->setSeparator(TRUE);
$this->actions['restore']->setEnableFunction(array($this, 'enableSnapshotRestore'));
}
/* Actions from footer are not in any menus and do not need a label */
......@@ -407,6 +408,11 @@ class management
// Update list
$this->listing->update();
// Init snapshot list for renderSnapshotActions
if (is_object($this->snapHandler)) {
$this->snapHandler->initSnapshotCache($this->listing->getBase());
}
// Display list
return $this->renderList();
}
......@@ -606,6 +612,15 @@ class management
}
}
function enablePaste($action, ListingEntry $entry = NULL)
{
if ($entry === NULL) {
return $this->cpHandler->entries_queued();
} else {
return FALSE;
}
}
/* Action handlers */
/*!
......@@ -986,7 +1001,7 @@ class management
if (empty($this->dn)) {
return;
}
$aclCategory = objects::infos($this->getEntry($this->dn)->getTemplatedType())['aclCategory'];
$aclCategory = objects::infos($this->listing->getEntry($this->dn)->getTemplatedType())['aclCategory'];
}
if ($ui->allow_snapshot_restore($this->dn, $aclCategory, empty($action['targets']))) {
......@@ -1053,6 +1068,20 @@ class management
return $this->snapHandler->getAvailableSnapsShots($dn);
}
/*
* \brief Whether snapshot restore action should be enabled for an entry
*/
function enableSnapshotRestore($action, ListingEntry $entry = NULL)
{
if ($entry !== NULL) {
/* For entries */
return $this->snapHandler->hasSnapshots($entry->dn);
} else {
/* For action menu */
return $this->snapHandler->hasDeletedSnapshots($this->getSnapshotBases());
}
}
/*!
* \brief Creates a new snapshot entry
*/
......
......@@ -55,6 +55,7 @@ class LockAction extends Action
'name' => $this->name.'_lock',
'icon' => $this->icon['lock'],
'label' => $this->label['lock'],
'enabled' => $this->isEnabledFor(),
'separator' => $this->separator,
);
......@@ -62,6 +63,7 @@ class LockAction extends Action
'name' => $this->name.'_unlock',
'icon' => $this->icon['unlock'],
'label' => $this->label['unlock'],
'enabled' => $this->isEnabledFor(),
'separator' => FALSE,
);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment