@@ -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
383413function 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
417449function 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
421472function 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
433508function plugin_formcreator_hook_pre_purge_targetTicket (CommonDBTM $ item ) {
0 commit comments