Skip to content

Commit 0d0834d

Browse files
committed
feat(abstracttarget): prepare to support non Itil targets
1 parent ac36272 commit 0d0834d

9 files changed

+2471
-2331
lines changed

inc/abstractitiltarget.class.php

Lines changed: 2207 additions & 0 deletions
Large diffs are not rendered by default.

inc/abstracttarget.class.php

Lines changed: 199 additions & 2314 deletions
Large diffs are not rendered by default.

inc/formanswer.class.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,7 @@ public function parseTags(string $content, PluginFormcreatorTargetInterface $tar
11801180
$content = str_replace('##question_' . $questionId . '##', Sanitizer::sanitize($name), $content);
11811181
if ($question->fields['fieldtype'] === 'file') {
11821182
if (strpos($content, '##answer_' . $questionId . '##') !== false) {
1183-
if ($target !== null) {
1183+
if ($target !== null && $target instanceof PluginFormcreatorAbstractItilTarget) {
11841184
foreach ($this->questionFields[$questionId]->getDocumentsForTarget() as $documentId) {
11851185
$target->addAttachedDocument($documentId);
11861186
}
@@ -1823,6 +1823,10 @@ public function getCurrentApprovers(): ?array {
18231823
* get all generated targets by the form answer
18241824
* populates the generated targets associated to the instance
18251825
*
1826+
* Assumes that the generated target has a relation with the form answer
1827+
*
1828+
* @param array $itemtypes Get only the targets of the given itemtypes
1829+
*
18261830
* @return array An array of target itemtypes to track
18271831
*/
18281832
public function getGeneratedTargets($itemtypes = []): array {
@@ -1844,6 +1848,9 @@ public function getGeneratedTargets($itemtypes = []): array {
18441848
$targetItem = new $targetType();
18451849
$generatedType = $targetItem->getTargetItemtypeName();
18461850
$relationType = $targetItem->getItem_Item();
1851+
if ($relationType === null) {
1852+
continue;
1853+
}
18471854
$relationTable = $relationType::getTable();
18481855
$generatedTypeFk = $generatedType::getForeignKeyField();
18491856
$generatedTypeTable = $generatedType::getTable();

inc/itiltargetinterface.class.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* ---------------------------------------------------------------------
4+
* Formcreator is a plugin which allows creation of custom forms of
5+
* easy access.
6+
* ---------------------------------------------------------------------
7+
* LICENSE
8+
*
9+
* This file is part of Formcreator.
10+
*
11+
* Formcreator is free software; you can redistribute it and/or modify
12+
* it under the terms of the GNU General Public License as published by
13+
* the Free Software Foundation; either version 2 of the License, or
14+
* (at your option) any later version.
15+
*
16+
* Formcreator is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU General Public License
22+
* along with Formcreator. If not, see <http://www.gnu.org/licenses/>.
23+
* ---------------------------------------------------------------------
24+
* @copyright Copyright © 2011 - 2021 Teclib'
25+
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+
26+
* @link https://github.com/pluginsGLPI/formcreator/
27+
* @link https://pluginsglpi.github.io/formcreator/
28+
* @link http://plugins.glpi-project.org/#/plugin/formcreator
29+
* ---------------------------------------------------------------------
30+
*/
31+
32+
if (!defined('GLPI_ROOT')) {
33+
die("Sorry. You can't access this file directly");
34+
}
35+
36+
interface PluginFormcreatorItilTargetInterface extends PluginFormcreatorTargetInterface
37+
{
38+
public function save(PluginFormcreatorFormAnswer $formanswer);
39+
40+
public function addAttachedDocument($documentId);
41+
42+
public function getTargetItemtypeName();
43+
44+
public function getItem_Item();
45+
}

inc/targetchange.class.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
die("Sorry. You can't access this file directly");
3838
}
3939

40-
class PluginFormcreatorTargetChange extends PluginFormcreatorAbstractTarget
40+
class PluginFormcreatorTargetChange extends PluginFormcreatorAbstractItilTarget
4141
{
4242
public static function getTypeName($nb = 1) {
4343
return _n('Target change', 'Target changes', $nb, 'formcreator');
@@ -560,11 +560,11 @@ public function prepareInputForUpdate($input) {
560560

561561
if (isset($input['sla_rule'])) {
562562
switch ($input['sla_rule']) {
563-
case PluginFormcreatorAbstractTarget::SLA_RULE_SPECIFIC:
563+
case self::SLA_RULE_SPECIFIC:
564564
$input['sla_question_tto'] = $input['_sla_specific_tto'];
565565
$input['sla_question_ttr'] = $input['_sla_specific_ttr'];
566566
break;
567-
case PluginFormcreatorAbstractTarget::SLA_RULE_FROM_ANWSER:
567+
case self::SLA_RULE_FROM_ANWSER:
568568
$input['sla_question_tto'] = $input['_sla_questions_tto'];
569569
$input['sla_question_ttr'] = $input['_sla_questions_ttr'];
570570
break;
@@ -573,11 +573,11 @@ public function prepareInputForUpdate($input) {
573573

574574
if (isset($input['ola_rule'])) {
575575
switch ($input['ola_rule']) {
576-
case PluginFormcreatorAbstractTarget::OLA_RULE_SPECIFIC:
576+
case self::OLA_RULE_SPECIFIC:
577577
$input['ola_question_tto'] = $input['_ola_specific_tto'];
578578
$input['ola_question_ttr'] = $input['_ola_specific_ttr'];
579579
break;
580-
case PluginFormcreatorAbstractTarget::OLA_RULE_FROM_ANWSER:
580+
case self::OLA_RULE_FROM_ANWSER:
581581
$input['ola_question_tto'] = $input['_ola_questions_tto'];
582582
$input['ola_question_ttr'] = $input['_ola_questions_ttr'];
583583
break;
@@ -659,7 +659,7 @@ public function getDefaultData(PluginFormcreatorFormAnswer $formanswer): array {
659659
*
660660
* @return Change|null generated change
661661
*/
662-
public function save(PluginFormcreatorFormAnswer $formanswer) {
662+
public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
663663
$data = [];
664664
$change = new Change();
665665
$form = $formanswer->getForm();

inc/targetinterface.class.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,5 @@ interface PluginFormcreatorTargetInterface
3737
{
3838
public function save(PluginFormcreatorFormAnswer $formanswer);
3939

40-
public function addAttachedDocument($documentId);
41-
4240
public function getTargetItemtypeName();
43-
44-
public function getItem_Item();
4541
}

inc/targetproblem.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
die("Sorry. You can't access this file directly");
3838
}
3939

40-
class PluginFormcreatorTargetProblem extends PluginFormcreatorAbstractTarget {
40+
class PluginFormcreatorTargetProblem extends PluginFormcreatorAbstractItilTarget {
4141
public static function getTypeName($nb = 1) {
4242
return _n('Target problem', 'Target problems', $nb, 'formcreator');
4343
}
@@ -162,7 +162,7 @@ public static function showActors(self $item) {
162162
*
163163
* @return Ticket|null Generated ticket if success, null otherwise
164164
*/
165-
public function save(PluginFormcreatorFormAnswer $formanswer) {
165+
public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
166166
$data = [];
167167
$problem = new Problem();
168168
$form = $formanswer->getForm();

inc/targetticket.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
die("Sorry. You can't access this file directly");
3939
}
4040

41-
class PluginFormcreatorTargetTicket extends PluginFormcreatorAbstractTarget
41+
class PluginFormcreatorTargetTicket extends PluginFormcreatorAbstractItilTarget
4242
{
4343
const ASSOCIATE_RULE_NONE = 1;
4444
const ASSOCIATE_RULE_SPECIFIC = 2;
@@ -795,7 +795,7 @@ public function getDefaultData(PluginFormcreatorFormAnswer $formanswer): array {
795795
*
796796
* @return Ticket|null Generated ticket if success, null otherwise
797797
*/
798-
public function save(PluginFormcreatorFormAnswer $formanswer) {
798+
public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
799799
$ticket = new Ticket();
800800
$form = $formanswer->getForm();
801801
$data = $this->getDefaultData($formanswer);

tests/2-integration/PluginFormcreatorTargetTicket.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function testUrgency() {
119119
'target_name' => 'urgency from answer',
120120
'content' => '##FULLFORM##',
121121
'itemtype' => \PluginFormcreatorTargetTicket::class,
122-
'urgency_rule' => \PluginFormcreatorAbstractTarget::URGENCY_RULE_ANSWER,
122+
'urgency_rule' => \PluginFormcreatorAbstractItilTarget::URGENCY_RULE_ANSWER,
123123
'urgency_question' => $question->getID(),
124124
]);
125125
$this->boolean($targetTicket1->isNewItem())->isFalse();
@@ -130,7 +130,7 @@ public function testUrgency() {
130130
'target_name' => 'default urgency',
131131
'content' => '##FULLFORM##',
132132
'itemtype' => \PluginFormcreatorTargetTicket::class,
133-
'urgency_rule' => \PluginFormcreatorAbstractTarget::URGENCY_RULE_NONE,
133+
'urgency_rule' => \PluginFormcreatorAbstractItilTarget::URGENCY_RULE_NONE,
134134
'urgency_question' => '0',
135135
]);
136136
$this->boolean($targetTicket2->isNewItem())->isFalse();

0 commit comments

Comments
 (0)