Commit 535eedca authored by Côme Chilliet's avatar Côme Chilliet
Browse files

feat(management) Add Template actions to the menu

Also fixed some problems surrounding menu actions
parent 16c6196d
......@@ -49,7 +49,8 @@ class management
protected $title;
protected $icon;
protected $actions = array();
protected $actions = array();
protected $actionHandlers = array();
public $neededAttrs = array();
......@@ -99,6 +100,12 @@ class management
{
// Register default actions
$createMenu = array();
if (!static::$skipTemplates) {
$templateMenu = array();
$fromTemplateMenu = array();
}
foreach ($this->objectTypes as $type) {
$infos = objects::infos($type);
$icon = 'geticon.php?context=actions&icon=document-new&size=16';
......@@ -110,7 +117,38 @@ class management
'0', 'newEntry',
array('c')
);
if (!static::$skipTemplates) {
// TODO fix ACL
$templateMenu[] = new Action(
'new_template_'.$type, $infos['name'], $icon,
'0', 'newEntryTemplate',
array('c')
);
$fromTemplateMenu[] = new Action(
'template_apply_'.$type, $infos['name'], $icon,
'?', 'newEntryFromTemplate',
array('c')
);
}
}
if (!static::$skipTemplates) {
$createMenu =
array_merge(
array(
new ActionSubMenu(
'template', _('Template'), 'geticon.php?context=devices&icon=template&size=16',
$templateMenu
),
new ActionSubMenu(
'fromtemplate', _('From template'), 'geticon.php?context=actions&icon=document-new&size=16',
$fromTemplateMenu
),
),
$createMenu
);
}
$this->registerAction(
new ActionSubMenu(
'new', _('Create'), 'geticon.php?context=actions&icon=document-new&size=16',
......@@ -145,8 +183,9 @@ class management
*/
function registerAction(Action $action)
{
$this->actions[$action->getName()] = $action;
foreach ($action->listActions() as $actionName) {
$this->actions[$actionName] = $action;
$this->actionHandlers[$actionName] = $action;
}
}
......@@ -186,11 +225,13 @@ class management
function handleAction(array $action)
{
// Start action
if (isset($this->actions[$action['action']])) {
if (!isset($action['targets'])) {
$action['targets'] = array();
}
return $this->actions[$action['action']]->execute($this, $action);
if (!isset($action['targets'])) {
$action['targets'] = array();
}
if (isset($action['subaction']) && isset($this->actionHandlers[$action['action'].'_'.$action['subaction']])) {
return $this->actionHandlers[$action['action'].'_'.$action['subaction']]->execute($this, $action);
} elseif (isset($this->actionHandlers[$action['action']])) {
return $this->actionHandlers[$action['action']]->execute($this, $action);
}
}
......@@ -221,6 +262,7 @@ class management
// Handle actions (POSTs and GETs)
$action = $this->detectPostActions();
if (!empty($action)) {
@DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $action, 'Action');
$str = $this->handleAction($action);
if ($str) {
return $this->getHeader().$str;
......@@ -487,7 +529,7 @@ class management
*/
function newEntry(array $action)
{
$type = preg_replace('/^new_/', '', $action['action']);
$type = $action['subaction'];
$this->dn = 'new';
......@@ -498,6 +540,62 @@ class management
@DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $this->dn, 'Create entry initiated');
}
function newEntryTemplate(array $action)
{
if (static::$skipTemplates) {
return;
}
$action['subaction'] = preg_replace('/^template_/', '', $action['subaction']);
$this->newEntry($action);
$this->tabObject->setTemplateMode('template_cn');
}
function newEntryFromTemplate(array $action)
{
if (static::$skipTemplates) {
return;
}
if (isset($action['targets'][0])) {
$dn = $action['targets'][0];
} else {
$dn = NULL;
}
if ($action['subaction'] == 'apply') {
if ($dn === NULL) {
return;
}
$type = $this->listing->getEntry($dn)->getTemplatedType();
} else {
$type = preg_replace('/^apply_/', '', $action['subaction']);
}
$this->dialogObject = new templateDialog($this, $type, $dn);
}
function applyTemplateToEntry(array $action)
{
global $ui;
if (static::$skipTemplates) {
return;
}
if (empty($action['targets'])) {
return;
}
$type = preg_replace('/^template_apply_to_/', '', $action['action']);
$this->dns = $action['targets'];
// check locks
if ($locks = get_locks($this->dns)) {
return gen_locked_message($locks, $this->dns);
}
// Add locks
add_lock ($this->dns, $ui->dn);
$this->dn = array_shift($this->dns);
$this->dialogObject = new templateDialog($this, $type, NULL, $this->dn);
}
/*!
* \brief This method opens an existing object to be edited.
*
......
......@@ -27,7 +27,7 @@ class Action
protected $label;
protected $icon;
/* 0, 1, + or * */
/* 0, 1, ?, + or * */
protected $targets;
protected $acl;
......@@ -65,6 +65,10 @@ class Action
$this->minTargets = 1;
$this->maxTargets = 1;
break;
case '?':
$this->minTargets = 0;
$this->maxTargets = 1;
break;
case '+':
$this->minTargets = 1;
$this->maxTargets = FALSE;
......@@ -186,31 +190,33 @@ class Action
*/
class ActionSubMenu extends Action
{
protected $actions = array();
protected $actions = array();
protected $handlers = array();
function __construct($name, $label, $icon, array $actions, $inmenu = TRUE)
{
parent::__construct($name, $label, $icon, '0', FALSE, array(), $inmenu, FALSE);
foreach ($actions as $action) {
$this->actions = $actions;
foreach ($this->actions as $action) {
$names = $action->listActions();
foreach ($names as $name) {
$this->actions[$name] = $action;
$this->handlers[$name] = $action;
}
}
}
function listActions()
{
$actions = array();
foreach ($this->actions as $action) {
$actions = array_merge($actions, $action->listActions());
}
return $actions;
return array_keys($this->handlers);
}
function execute($management, $action)
{
return $this->actions[$action['action']]->execute($management, $action);
if (isset($action['subaction']) && isset($this->handlers[$action['action'].'_'.$action['subaction']])) {
return $this->handlers[$action['action'].'_'.$action['subaction']]->execute($management, $action);
} elseif (isset($this->handlers[$action['action']])) {
return $this->handlers[$action['action']]->execute($management, $action);
}
}
function renderMenuItems($level = 2)
......
......@@ -921,7 +921,7 @@ class managementListing
$this->scrollPosition = $_POST['position_'.$this->pid];
}
$result = array('targets' => array(), 'action' => '');
$result = array('targets' => array(), 'action' => '', 'subaction' => NULL);
// Filter GET with "act" attributes
if (isset($_GET['act'])) {
......
......@@ -125,10 +125,6 @@ class userManagement extends management
protected function configureActions()
{
parent::configureActions();
//~ $this->registerAction('new_user', 'newEntry');
//~ $this->registerAction('new_template_user', 'newEntryTemplate');
//~ $this->registerAction('template_apply_to_user', 'applyTemplateToEntry');
//~ $this->registerAction('template_apply_user', 'newEntryFromTemplate');
$this->registerAction(
new LockAction(
......
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