diff --git a/front/issue.form.php b/front/issue.form.php index 5d46ace22..1ab605329 100644 --- a/front/issue.form.php +++ b/front/issue.form.php @@ -37,33 +37,29 @@ Html::displayNotFoundError(); } +// Show issue only if service catalog is enabled +if (!plugin_formcreator_replaceHelpdesk()) { + Html::redirect($CFG_GLPI['root_doc']."/front/helpdesk.public.php"); +} + // force layout of glpi -$layout = $_SESSION['glpilayout']; +PluginFormcreatorCommon::saveLayout(); $_SESSION['glpilayout'] = "lefttab"; -$issue = new PluginFormcreatorIssue(); -if (isset($_POST['save_formanswer'])) { - $_POST['plugin_formcreator_forms_id'] = intval($_POST['formcreator_form']); - $_POST['status'] = PluginFormcreatorFormAnswer::STATUS_WAITING; - $issue->saveAnswers($_POST); - Html::back(); -} else { - - if (plugin_formcreator_replaceHelpdesk()) { - PluginFormcreatorWizard::header(__('Service catalog', 'formcreator')); - } else { - Html::redirect($CFG_GLPI['root_doc']."/front/helpdesk.public.php"); - } +PluginFormcreatorWizard::header(__('Service catalog', 'formcreator')); - $issue->getFromDB((int) $_REQUEST['id']); - $issue->display($_REQUEST); +/** @var PluginFormcreatorIssue $issue */ +$issue = PluginFormcreatorIssue::getById((int) $_REQUEST['id']); +if ($issue === false) { + PluginFormcreatorCommon::restoreLayout(); + Html::displayNotFoundError(); +} +$issue->display($_REQUEST); - if (plugin_formcreator_replaceHelpdesk()) { - PluginFormcreatorWizard::footer(); - } else { - Html::footer(); - } +if (plugin_formcreator_replaceHelpdesk()) { + PluginFormcreatorWizard::footer(); +} else { + Html::footer(); } -// restore layout -$_SESSION['glpilayout'] = $layout; +PluginFormcreatorCommon::restoreLayout(); diff --git a/inc/common.class.php b/inc/common.class.php index dde3b81e7..ca996dc97 100644 --- a/inc/common.class.php +++ b/inc/common.class.php @@ -585,4 +585,12 @@ public static function checkRegex($regex) { return $isValid; } + + public static function saveLayout() { + $_SESSION['plugin_formcreator']['layout_backup'] = $_SESSION['glpilayout']; + } + + public static function restoreLayout() { + $_SESSION['glpilayout'] = $_SESSION['plugin_formcreator']['layout_backup'] ?? $_SESSION['glpilayout']; + } } diff --git a/inc/issue.class.php b/inc/issue.class.php index 3e7b5b49f..aa60edc06 100644 --- a/inc/issue.class.php +++ b/inc/issue.class.php @@ -262,8 +262,13 @@ public function display($options = []) { public function displayExtended($options = []) { $itemtype = $this->fields['itemtype']; + if (!class_exists($itemtype)) { + PluginFormcreatorCommon::restoreLayout(); + Html::displayNotFoundError(); + } $item = new $itemtype(); if (!$item->getFromDB($this->fields['items_id'])) { + PluginFormcreatorCommon::restoreLayout(); Html::displayNotFoundError(); } @@ -275,8 +280,26 @@ public function displayExtended($options = []) { unset($options['_item']); // Header if the item + link to the list of items + switch ($item::getType()) { + case self::getType(): + $_SESSION['glpilisturl'][self::getType()] = $this->getSearchURL(); + break; + + case PluginFormcreatorFormAnswer::getType(): + Session::initNavigateListItems(self::getType()); + $_SESSION['glpilisturl'][self::getType()] = $this->getSearchURL(); + break; + + case Ticket::getType(): + $_SESSION['glpilisturl'][self::getType()] = $this->getFormURLWithID($this->getID()); + } $this->showNavigationHeader($options); + if (!$item->canViewItem()) { + PluginFormcreatorCommon::restoreLayout(); + Html::displayNotFoundError(); + } + $item->showTabsContent($options); } @@ -287,8 +310,13 @@ public function displaySimplified($options = []) { global $CFG_GLPI; $itemtype = $this->fields['itemtype']; + if (!class_exists($itemtype)) { + PluginFormcreatorCommon::restoreLayout(); + Html::displayNotFoundError(); + } $item = new $itemtype(); if (!$item->getFromDB($this->fields['items_id'])) { + PluginFormcreatorCommon::restoreLayout(); Html::displayNotFoundError(); } @@ -315,7 +343,26 @@ public function displaySimplified($options = []) { $options['id'] = $item->getID(); } + $options['_item'] = $item; + if ($item Instanceof PluginFormcreatorFormAnswer) { + $item = $this->getTicketsForDisplay($options); + } + unset($options['_item']); + // Header if the item + link to the list of items + switch ($item::getType()) { + case self::getType(): + $_SESSION['glpilisturl'][self::getType()] = $this->getSearchURL(); + break; + + case PluginFormcreatorFormAnswer::getType(): + Session::initNavigateListItems(self::getType()); + $_SESSION['glpilisturl'][self::getType()] = $this->getSearchURL(); + break; + + case Ticket::getType(): + $_SESSION['glpilisturl'][self::getType()] = $this->getFormURLWithID($this->getID()); + } $this->showNavigationHeader($options); // retrieve associated tickets @@ -326,7 +373,7 @@ public function displaySimplified($options = []) { unset($options['_item']); // force recall of ticket in layout - $old_layout = $_SESSION['glpilayout']; + PluginFormcreatorCommon::saveLayout(); $_SESSION['glpilayout'] = "lefttab"; if ($item instanceof Ticket) { @@ -359,7 +406,7 @@ public function displaySimplified($options = []) { } // restore layout - $_SESSION['glpilayout'] = $old_layout; + PluginFormcreatorCommon::restoreLayout(); } /** @@ -371,12 +418,11 @@ public function getTicketsForDisplay($options) { global $DB; $item = $options['_item']; - $formanswerId = $options['id']; $rows = $DB->request([ 'FROM' => Item_Ticket::getTable(), 'WHERE' => [ 'itemtype' => 'PluginFormcreatorFormAnswer', - 'items_id' => $formanswerId + 'items_id' => $item->getID() // $item is a PluginFormcreatorFormAnswer ], 'ORDER' => 'tickets_id ASC' ]); @@ -386,8 +432,15 @@ public function getTicketsForDisplay($options) { $item = new Ticket; $item->getFromDB($ticket['tickets_id']); } else if (count($rows) > 1) { - // multiple tickets, force ticket tab in form anser - Session::setActiveTab(PluginFormcreatorFormAnswer::class, 'Ticket$1'); + if (isset($options['tickets_id'])) { + $ticket = Ticket::getById((int) $options['tickets_id']); + if ($ticket) { + $item = $ticket; + } + } else { + // multiple tickets, no specified ticket then force ticket tab in form anser + Session::setActiveTab(PluginFormcreatorFormAnswer::class, 'Ticket$1'); + } } return $item; diff --git a/setup.php b/setup.php index 6d4208548..5873190c7 100644 --- a/setup.php +++ b/setup.php @@ -193,17 +193,19 @@ function plugin_init_formcreator() { Session::setActiveTab(Ticket::class, $decodedUrl['forcetab']); } - // When an issue has a single target ticket + // When an ticket has a matching issue (it means that the ticket is the only generated ticket) $issue = new PluginFormcreatorIssue(); - $issue->getFromDBByCrit([ + $issues = $issue->find([ 'itemtype' => Ticket::class, 'items_id' => (int) $_GET['id'] ]); - if (!$issue->isNewItem()) { + if (count($issues) == 1) { + $issueId = $array_pop($issues)['id']; + $issue->getById($issueId); Html::redirect($issue->getFormURLWithID($issue->getID()) . $openItilFollowup); } - // When a target has several target tickets + // When no or several tickets matches an issue, rely use the Form Answer $itemTicket = new Item_Ticket(); $itemTicket->getFromDBByCrit([ 'itemtype' => PluginFormcreatorFormAnswer::class, @@ -213,6 +215,21 @@ function plugin_init_formcreator() { // No formanswer found Html::displayNotFoundError(); } + + $issue->getFromDBByCrit([ + 'itemtype' => PluginFormcreatorFormAnswer::class, + 'items_id' => $itemTicket->fields['items_id'] + ]); + if ($issue->isNewItem()) { + // No formanswer found + Html::displayNotFoundError(); + } + $ticket = Ticket::getById($itemTicket->fields['items_id']); + if ($ticket === false) { + Html::redirect($issue->getFormURLWithID($itemTicket->fields['items_id']) . $openItilFollowup); + } + + Html::redirect($issue->getFormURLWithID($issue->getID()) . '&tickets_id=' . $itemTicket->fields['tickets_id']); } }