Skip to content

Commit fa89fd7

Browse files
committed
feat(issue,formanswer): compose status from ticket
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
1 parent a47b1ca commit fa89fd7

File tree

8 files changed

+361
-61
lines changed

8 files changed

+361
-61
lines changed

hook.php

Lines changed: 114 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -364,70 +364,145 @@ function plugin_formcreator_hook_update_ticket(CommonDBTM $item) {
364364
'items_id' => $id
365365
]
366366
]);
367-
$issue->update([
368-
'id' => $issue->getID(),
369-
'items_id' => $id,
370-
'display_id' => "t_$id",
371-
'itemtype' => 'Ticket',
372-
'name' => $issueName,
373-
'status' => $validationStatus,
374-
'date_creation' => $item->fields['date'],
375-
'date_mod' => $item->fields['date_mod'],
376-
'entities_id' => $item->fields['entities_id'],
377-
'is_recursive' => '0',
378-
'requester_id' => $item->fields['users_id_recipient'],
379-
'comment' => addslashes($item->fields['content']),
367+
if (!$issue->isNewItem()) {
368+
$issue->update([
369+
'id' => $issue->getID(),
370+
'items_id' => $id,
371+
'display_id' => "t_$id",
372+
'itemtype' => 'Ticket',
373+
'name' => $issueName,
374+
'status' => $validationStatus,
375+
'date_creation' => $item->fields['date'],
376+
'date_mod' => $item->fields['date_mod'],
377+
'entities_id' => $item->fields['entities_id'],
378+
'is_recursive' => '0',
379+
'requester_id' => $item->fields['users_id_recipient'],
380+
'users_id_validator' => $validationStatus['user'],
381+
'comment' => addslashes($item->fields['content']),
382+
]);
383+
return;
384+
}
385+
386+
// No issue linked to the ticket,
387+
// then find the form answer linked to the ticket
388+
$formAnswer = new PluginFormcreatorFormAnswer();
389+
$formAnswer->getFromDBByCrit([
390+
'id' => new QuerySubQuery([
391+
'SELECT' => 'items_id',
392+
'FROM' => Item_Ticket::getTable(),
393+
'WHERE' => [
394+
'itemtype' => PluginFormcreatorFormAnswer::getType(),
395+
'tickets_id' => $id,
396+
]
397+
])
380398
]);
399+
if ($formAnswer->isNewItem()) {
400+
// Should not happen as one and only one form answer shall be linked to a ticket
401+
// If several formanswer found, the previous getFromDBByCrit() logs an error
402+
return;
403+
}
404+
405+
// set the minimal status to the form answer (which will forward the status to the issue)
406+
$minimalStatus = $formAnswer->getMinimalStatus();
407+
if ($minimalStatus === null) {
408+
return;
409+
}
410+
$formAnswer->updateStatus($minimalStatus);
381411
}
382412

383413
function plugin_formcreator_hook_delete_ticket(CommonDBTM $item) {
384-
global $DB;
385-
386414
if (!($item instanceof Ticket)) {
387415
return;
388416
}
389417

390418
$id = $item->getID();
391419

392-
// mark formanswers as deleted
393-
$iterator = $DB->request([
394-
'SELECT' => ['id'],
395-
'FROM' => Item_Ticket::getTable(),
396-
'WHERE' => [
397-
'itemtype' => PluginFormcreatorFormAnswer::class,
398-
'tickets_id' => $id,
399-
]
420+
// find a formanswer linked to the ticket
421+
$formAnswer = new PluginFormcreatorFormAnswer();
422+
$formAnswer->getFromDBByCrit([
423+
'id' => new QuerySubQuery([
424+
'SELECT' => 'items_id',
425+
'FROM' => Item_Ticket::getTable(),
426+
'WHERE' => [
427+
'itemtype' => PluginFormcreatorFormAnswer::getType(),
428+
'tickets_id' => $id,
429+
]
430+
])
400431
]);
401-
foreach ($iterator as $row) {
402-
$form_answer = PluginFormcreatorCommon::getFormAnswer();
403-
$form_answer->update([
404-
'id' => $row['id'],
405-
'is_deleted' => 1,
406-
]);
432+
if (!$formAnswer->isNewItem()) {
433+
$minimalStatus = $formAnswer->getMinimalStatus();
434+
if ($minimalStatus !== null) {
435+
$formAnswer->updateStatus($minimalStatus);
436+
}
437+
return;
407438
}
408439

409440
// delete issue
441+
// TODO: add is_deleted column to issue
410442
$issue = new PluginFormcreatorIssue();
411443
$issue->deleteByCriteria([
412-
'display_id' => "t_$id",
413-
'itemtype' => Ticket::class
444+
'items_id' => $id,
445+
'itemtype' => 'Ticket'
414446
], 1);
415447
}
416448

417449
function plugin_formcreator_hook_restore_ticket(CommonDBTM $item) {
418-
plugin_formcreator_hook_add_ticket($item);
450+
$formAnswer = new PluginFormcreatorFormAnswer();
451+
$formAnswer->getFromDBByCrit([
452+
'id' => new QuerySubQuery([
453+
'SELECT' => 'items_id',
454+
'FROM' => Item_Ticket::getTable(),
455+
'WHERE' => [
456+
'itemtype' => PluginFormcreatorFormAnswer::getType(),
457+
'tickets_id' => $item->getID(),
458+
]
459+
])
460+
]);
461+
if ($formAnswer->isNewItem()) {
462+
plugin_formcreator_hook_add_ticket($item);
463+
return;
464+
}
465+
466+
$minimalStatus = $formAnswer->getMinimalStatus();
467+
if ($minimalStatus !== null) {
468+
$formAnswer->updateStatus($minimalStatus);
469+
}
419470
}
420471

421472
function plugin_formcreator_hook_purge_ticket(CommonDBTM $item) {
422-
if ($item instanceof Ticket) {
423-
$id = $item->getID();
473+
if (!($item instanceof Ticket)) {
474+
return;
475+
}
424476

425-
$issue = new PluginFormcreatorIssue();
426-
$issue->deleteByCriteria([
427-
'display_id' => "t_$id",
428-
'itemtype' => Ticket::class
429-
], 1);
477+
$id = $item->getID();
478+
479+
// mark formanswers as deleted
480+
$formAnswer = new PluginFormcreatorFormAnswer();
481+
$formAnswer->getFromDBByCrit([
482+
'id' => new QuerySubQuery([
483+
'SELECT' => 'items_id',
484+
'FROM' => Item_Ticket::getTable(),
485+
'WHERE' => [
486+
'itemtype' => PluginFormcreatorFormAnswer::getType(),
487+
'tickets_id' => $id,
488+
]
489+
])
490+
]);
491+
if ($formAnswer->isNewItem() != false) {
492+
$minimalStatus = $formAnswer->getMinimalStatus();
493+
if ($minimalStatus !== null) {
494+
$formAnswer->updateStatus($minimalStatus);
495+
}
496+
return;
430497
}
498+
499+
// delete issue
500+
// TODO: add is_deleted column to issue
501+
$issue = new PluginFormcreatorIssue();
502+
$issue->deleteByCriteria([
503+
'items_id' => $id,
504+
'itemtype' => 'Ticket'
505+
], 1);
431506
}
432507

433508
function plugin_formcreator_hook_pre_purge_targetTicket(CommonDBTM $item) {

inc/abstracttarget.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ abstract protected function getItem_Supplier();
108108
*
109109
* @return CommonDBTM
110110
*/
111-
abstract protected function getItem_Item();
111+
abstract public function getItem_Item();
112112

113113
/**
114114
* Get the class name of the target itemtype's template class
@@ -129,7 +129,7 @@ abstract protected function getTemplatePredefinedFieldItemtype(): string;
129129
*
130130
* @return string
131131
*/
132-
abstract protected function getTargetItemtypeName(): string;
132+
abstract public function getTargetItemtypeName(): string;
133133

134134
/**
135135
* Get the query criterias to query the ITIL categories

0 commit comments

Comments
 (0)