diff --git a/hook.php b/hook.php index 39692b866..6f6326d5e 100644 --- a/hook.php +++ b/hook.php @@ -610,71 +610,6 @@ function plugin_formcreator_dynamicReport($params) { return false; } -function plugin_formcreator_redefine_menus($menus) { - if (Session::getCurrentInterface() != "helpdesk") { - return $menus; - } - - if (plugin_formcreator_replaceHelpdesk() !== false) { - $newMenu = []; - $newMenu['seek_assistance'] = [ - 'default' => Plugin::getWebDir('formcreator', false) . '/front/wizard.php', - 'title' => __('Seek assistance', 'formcreator'), - 'icon' => 'fa fa-paper-plane', - ]; - $newMenu['my_assistance_requests'] = [ - 'default' => PluginFormcreatorIssue::getSearchURL(false), - 'title' => __('My requests for assistance', 'formcreator'), - 'icon' => 'fa fa-list', - ]; - if (PluginFormcreatorEntityConfig::getUsedConfig('is_kb_separated', Session::getActiveEntity()) == PluginFormcreatorEntityConfig::CONFIG_KB_DISTINCT - && Session::haveRight('knowbase', KnowbaseItem::READFAQ) - ) { - $newMenu['faq'] = $menus['faq']; - $newMenu['faq']['default'] = Plugin::getWebDir('formcreator', false) . '/front/knowbaseitem.php'; - } - if (Session::haveRight("reservation", ReservationItem::RESERVEANITEM)) { - $newMenu['reservation'] = $menus['reservation']; - } - if (RSSFeed::canView()) { - $newMenu['feeds'] = [ - 'default' => Plugin::getWebDir('formcreator', false) . '/front/wizardfeeds.php', - 'title' => __('Consult feeds', 'formcreator'), - 'icon' => 'fa fa-rss', - ]; - } - return $newMenu; - } - - // Using GLPI's helpdesk interface; then just modify the menu - $newMenus = []; - foreach ($menus as $key => $menu) { - switch ($key) { - case 'create_ticket': - $newMenus['forms'] = [ - 'default' => '/' . Plugin::getWebDir('formcreator', false) . '/front/formlist.php', - 'title' => _n('Form', 'Forms', 2, 'formcreator'), - 'icon' => 'fas fa-edit', - ]; - break; - - case 'tickets': - $newMenus['tickets'] = [ - 'default' => PluginFormcreatorIssue::getSearchURL(false), - 'title' => PluginFormcreatorIssue::getTypeName(Session::getPluralNumber()), - 'icon' => 'fas fa-exclamation-circle', - ]; - break; - - default: - $newMenus[$key] = $menu; - } - } - $menus = $newMenus; - - return $menus; -} - function plugin_formcreator_hook_update_plugin(CommonDBTM $item) { if ($item->fields['directory'] != 'formcreator') { return; diff --git a/inc/common.class.php b/inc/common.class.php index d191189f0..c31c442cb 100644 --- a/inc/common.class.php +++ b/inc/common.class.php @@ -945,4 +945,71 @@ public static function hookPostShowTab(array $options) { $_SESSION["glpiactiveprofile"]["helpdesk_item_type"] = $_SESSION['plugin_formcreator']['helpdesk_item_type_backup']; } + + public static function hookRedefineMenu($menus) { + if (Session::getCurrentInterface() != 'helpdesk') { + return $menus; + } + + if (plugin_formcreator_replaceHelpdesk() !== false) { + $newMenu = []; + $newMenu['seek_assistance'] = [ + 'default' => Plugin::getWebDir('formcreator', false) . '/front/wizard.php', + 'title' => __('Seek assistance', 'formcreator'), + 'icon' => 'fa fa-paper-plane', + ]; + $newMenu['my_assistance_requests'] = [ + 'default' => PluginFormcreatorIssue::getSearchURL(false), + 'title' => __('My requests for assistance', 'formcreator'), + 'icon' => 'fa fa-list', + ]; + if (PluginFormcreatorEntityConfig::getUsedConfig('is_kb_separated', Session::getActiveEntity()) == PluginFormcreatorEntityConfig::CONFIG_KB_DISTINCT + && Session::haveRight('knowbase', KnowbaseItem::READFAQ) + ) { + $newMenu['faq'] = $menus['faq']; + $newMenu['faq']['default'] = Plugin::getWebDir('formcreator', false) . '/front/knowbaseitem.php'; + } + if (Session::haveRight("reservation", ReservationItem::RESERVEANITEM)) { + if (isset($menus['reservation'])) { + $newMenu['reservation'] = $menus['reservation']; + } + } + if (RSSFeed::canView()) { + $newMenu['feeds'] = [ + 'default' => Plugin::getWebDir('formcreator', false) . '/front/wizardfeeds.php', + 'title' => __('Consult feeds', 'formcreator'), + 'icon' => 'fa fa-rss', + ]; + } + return $newMenu; + } + + // Using GLPI's helpdesk interface; then just modify the menu + $newMenus = []; + foreach ($menus as $key => $menu) { + switch ($key) { + case 'create_ticket': + $newMenus['forms'] = [ + 'default' => '/' . Plugin::getWebDir('formcreator', false) . '/front/formlist.php', + 'title' => _n('Form', 'Forms', 2, 'formcreator'), + 'icon' => 'fas fa-edit', + ]; + break; + + case 'tickets': + $newMenus['tickets'] = [ + 'default' => PluginFormcreatorIssue::getSearchURL(false), + 'title' => PluginFormcreatorIssue::getTypeName(Session::getPluralNumber()), + 'icon' => 'fas fa-exclamation-circle', + ]; + break; + + default: + $newMenus[$key] = $menu; + } + } + $menus = $newMenus; + + return $menus; + } } diff --git a/setup.php b/setup.php index e5926e744..3830116a5 100644 --- a/setup.php +++ b/setup.php @@ -365,7 +365,7 @@ function plugin_formcreator_hook() { // Add a link in the main menu plugins for technician and admin panel $PLUGIN_HOOKS['menu_entry']['formcreator'] = 'front/formlist.php'; - $PLUGIN_HOOKS['redefine_menus']['formcreator'] = "plugin_formcreator_redefine_menus"; + $PLUGIN_HOOKS['redefine_menus']['formcreator'] = [PluginFormcreatorCommon::class, 'hookRedefineMenu']; // Config page $links = []; diff --git a/tests/3-unit/PluginFormcreatorCommon.php b/tests/3-unit/PluginFormcreatorCommon.php index 347ae59e0..97425aead 100644 --- a/tests/3-unit/PluginFormcreatorCommon.php +++ b/tests/3-unit/PluginFormcreatorCommon.php @@ -30,11 +30,15 @@ */ namespace tests\units; use GlpiPlugin\Formcreator\Tests\CommonTestCase; +use ITILFollowup; +use KnowbaseItem; +use ReservationItem; +use Plugin; +use Session; +use Ticket; class PluginFormcreatorCommon extends CommonTestCase { public function beforeTestMethod($method) { - global $CFG_GLPI; - switch ($method) { case 'testGetTicketStatusForIssue': $this->login('glpi', 'glpi'); @@ -449,4 +453,69 @@ public function testGetInterface() { $output = \PluginFormcreatorCommon::getInterface(); $this->string($output)->isEqualTo('servicecatalog'); } + + public function testHookRedefineMenu() { + // Create an entity + $this->login('glpi', 'glpi'); + $entity = new \Entity(); + $entityId = $entity->import([ + 'entities_id' => '0', + 'name' => __FUNCTION__ . $this->getUniqueString(), + ]); + // Force creation of the entity config + \PluginFormcreatorEntityConfig::getUsedConfig('replace_helpdesk', $entityId); + + // Use an not-self-service account + $this->login('glpi', 'glpi'); + + // Check the menu is left as is + $menus = \Html::generateMenuSession(true); + $output = \PluginFormcreatorCommon::hookRedefineMenu($menus); + $this->array($output)->isIdenticalTo($menus); + + // Check taht service catalog enabled does not impacts the menu for Central users + $entityConfig = new \PluginFormcreatorEntityConfig(); + $entityConfig->getFromDbByCrit(['entities_id' => $entityId]); + $this->boolean($entityConfig->isNewItem())->isFalse(); + $entityConfig->update([ + 'id' => $entityConfig->getID(), + 'replace_helpdesk' => '1', + ]); + $this->login('glpi', 'glpi'); + + $menus = \Html::generateHelpMenu(); + $output = \PluginFormcreatorCommon::hookRedefineMenu($menus); + $this->array($output)->isIdenticalTo($menus); + + $this->login('post-only', 'postonly'); + $menus = \Html::generateHelpMenu(true); + $expected = [ + 'seek_assistance' => + [ + 'default' => 'plugins/formcreator/front/wizard.php', + 'title' => 'Seek assistance', + 'icon' => 'fa fa-paper-plane', + ], + 'my_assistance_requests' => + [ + 'default' => '/plugins/formcreator/front/issue.php', + 'title' => 'My requests for assistance', + 'icon' => 'fa fa-list', + ], + 'reservation' => + [ + 'default' => '/front/reservationitem.php', + 'title' => 'Reservations', + 'icon' => 'ti ti-calendar-event', + ], + 'feeds' => + [ + 'default' => 'plugins/formcreator/front/wizardfeeds.php', + 'title' => 'Consult feeds', + 'icon' => 'fa fa-rss', + ], + ]; + $output = \PluginFormcreatorCommon::hookRedefineMenu($menus); + $this->array($output)->isIdenticalTo($expected); + } }