Skip to content

Commit

Permalink
fix: redirect from ticket to formanswer if several target tickets
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Aug 10, 2021
1 parent ee8a6e2 commit dbfbb10
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 33 deletions.
42 changes: 19 additions & 23 deletions front/issue.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
8 changes: 8 additions & 0 deletions inc/common.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
}
65 changes: 59 additions & 6 deletions inc/issue.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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);
}

Expand All @@ -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();
}

Expand All @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -359,7 +406,7 @@ public function displaySimplified($options = []) {
}

// restore layout
$_SESSION['glpilayout'] = $old_layout;
PluginFormcreatorCommon::restoreLayout();
}

/**
Expand All @@ -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'
]);
Expand All @@ -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;
Expand Down
25 changes: 21 additions & 4 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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']);
}
}

Expand Down

0 comments on commit dbfbb10

Please sign in to comment.