Skip to content

Commit c977b1c

Browse files
committed
fix(issue): issue not deleted when tichet goes to trash bin
1 parent c3baebb commit c977b1c

File tree

2 files changed

+32
-36
lines changed

2 files changed

+32
-36
lines changed

hook.php

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -469,30 +469,19 @@ function plugin_formcreator_hook_delete_ticket(CommonDBTM $item) {
469469

470470
$id = $item->getID();
471471

472-
// find a formanswer linked to the ticket
472+
// Update the formanswer's status (for cases where a form answer has several tickets)
473473
$formAnswer = new PluginFormcreatorFormAnswer();
474-
$formAnswer->getFromDBByCrit([
475-
'id' => new QuerySubQuery([
476-
'SELECT' => 'items_id',
477-
'FROM' => Item_Ticket::getTable(),
478-
'WHERE' => [
479-
'itemtype' => PluginFormcreatorFormAnswer::getType(),
480-
'tickets_id' => $id,
481-
]
482-
])
483-
]);
484-
if (!$formAnswer->isNewItem()) {
474+
if ($formAnswer->getFromDbByTicket($id)) {
485475
$minimalStatus = $formAnswer->getAggregatedStatus();
486476
if ($minimalStatus === null) {
487477
// There is no more ticket in the form anwer
488478
$formAnswer->updateStatus(CommonITILObject::CLOSED);
489479
} else {
490480
$formAnswer->updateStatus($minimalStatus);
491481
}
492-
return;
493482
}
494483

495-
// delete issue
484+
// Delete the issue
496485
// TODO: add is_deleted column to issue ?
497486
$issue = new PluginFormcreatorIssue();
498487
$issue->deleteByCriteria([
@@ -503,17 +492,7 @@ function plugin_formcreator_hook_delete_ticket(CommonDBTM $item) {
503492

504493
function plugin_formcreator_hook_restore_ticket(CommonDBTM $item) {
505494
$formAnswer = new PluginFormcreatorFormAnswer();
506-
$formAnswer->getFromDBByCrit([
507-
'id' => new QuerySubQuery([
508-
'SELECT' => 'items_id',
509-
'FROM' => Item_Ticket::getTable(),
510-
'WHERE' => [
511-
'itemtype' => PluginFormcreatorFormAnswer::getType(),
512-
'tickets_id' => $item->getID(),
513-
]
514-
])
515-
]);
516-
if ($formAnswer->isNewItem()) {
495+
if (! $formAnswer->getFromDbByTicket($item)) {
517496
plugin_formcreator_hook_add_ticket($item);
518497
return;
519498
}
@@ -533,17 +512,7 @@ function plugin_formcreator_hook_purge_ticket(CommonDBTM $item) {
533512

534513
// Update status of form answer, if any
535514
$formAnswer = new PluginFormcreatorFormAnswer();
536-
$formAnswer->getFromDBByCrit([
537-
'id' => new QuerySubQuery([
538-
'SELECT' => 'items_id',
539-
'FROM' => Item_Ticket::getTable(),
540-
'WHERE' => [
541-
'itemtype' => PluginFormcreatorFormAnswer::getType(),
542-
'tickets_id' => $id,
543-
]
544-
])
545-
]);
546-
if (!$formAnswer->isNewItem()) {
515+
if ($formAnswer->getFromDbByTicket($id)) {
547516
$minimalStatus = $formAnswer->getAggregatedStatus();
548517
if ($minimalStatus === null) {
549518
// There is no more ticket in the form anwer

inc/formanswer.class.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,4 +2047,31 @@ public static function getDefaultSearchRequest(): array {
20472047
'order' => 'DESC'
20482048
];
20492049
}
2050+
2051+
/**
2052+
* Get a formanswer from a generated ticket
2053+
*
2054+
* @param Ticket|int $item
2055+
* @return bool
2056+
*/
2057+
public function getFromDbByTicket($item) {
2058+
if (($item instanceof Ticket)) {
2059+
$id = $item->getID();
2060+
} else if (is_integer($item)) {
2061+
$id = $item;
2062+
} else {
2063+
throw new InvalidArgumentException("$item must be an integer or a " . Ticket::class);
2064+
}
2065+
2066+
return $this->getFromDBByCrit([
2067+
'id' => new QuerySubQuery([
2068+
'SELECT' => 'items_id',
2069+
'FROM' => Item_Ticket::getTable(),
2070+
'WHERE' => [
2071+
'itemtype' => PluginFormcreatorFormAnswer::getType(),
2072+
'tickets_id' => $id,
2073+
]
2074+
])
2075+
]);
2076+
}
20502077
}

0 commit comments

Comments
 (0)