Skip to content

Commit 61cf134

Browse files
committed
feat(targetticket): better template support for request source
1 parent 032c4c1 commit 61cf134

File tree

6 files changed

+182
-3
lines changed

6 files changed

+182
-3
lines changed

inc/targetticket.class.php

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class PluginFormcreatorTargetTicket extends PluginFormcreatorAbstractItilTarget
4949
const REQUESTTYPE_SPECIFIC = 1;
5050
const REQUESTTYPE_ANSWER = 2;
5151

52+
const REQUESTSOURCE_NONE = 0;
53+
const REQUESTSOURCE_SPECIFIC = 1;
5254

5355
public static function getTypeName($nb = 1) {
5456
return _n('Target ticket', 'Target tickets', $nb, 'formcreator');
@@ -100,6 +102,13 @@ public static function getEnumAssociateRule() {
100102
];
101103
}
102104

105+
public static function getEnumRequestSourceRule(): array {
106+
return [
107+
self::REQUESTSOURCE_NONE => __('Source from template or user default or GLPI default', 'formcreator'),
108+
self::REQUESTSOURCE_SPECIFIC => __('Formcreator', 'formcreator'),
109+
];
110+
}
111+
103112
public static function getEnumRequestTypeRule() {
104113
return [
105114
self::REQUESTTYPE_NONE => __('Default or from a template', 'formcreator'),
@@ -239,6 +248,7 @@ public static function showProperties(self $item) {
239248
$item->showSLASettings();
240249
$item->showOLASettings();
241250

251+
$item->showSourceSettings($rand);
242252
$item->showTypeSettings($rand);
243253
// -------------------------------------------------------------------------------------------
244254
// associated elements of the target
@@ -528,6 +538,13 @@ public function prepareInputForAdd($input) {
528538
}
529539
}
530540

541+
if (!isset($input['source_rule'])) {
542+
$input['source_rule'] = self::REQUESTSOURCE_SPECIFIC;
543+
}
544+
$input['source_question'] = 0;
545+
if ($input['source_rule'] == self::REQUESTTYPE_SPECIFIC) {
546+
$input['source_question'] = PluginFormcreatorCommon::getFormcreatorRequestTypeId();
547+
}
531548
return $input;
532549
}
533550

@@ -613,6 +630,18 @@ public function prepareInputForUpdate($input) {
613630
}
614631
}
615632

633+
if (isset($input['source_rule'])) {
634+
$input['source_question'] = '0';
635+
switch ($input['source_rule']) {
636+
case self::REQUESTSOURCE_NONE:
637+
$input['source_question'] = 0;
638+
break;
639+
case self::REQUESTSOURCE_SPECIFIC:
640+
$input['source_question'] = PluginFormcreatorCommon::getFormcreatorRequestTypeId();
641+
break;
642+
}
643+
}
644+
616645
if (isset($input['category_rule'])) {
617646
switch ($input['category_rule']) {
618647
case self::CATEGORY_RULE_ANSWER:
@@ -799,7 +828,6 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
799828
$ticket = new Ticket();
800829
$form = $formanswer->getForm();
801830
$data = $this->getDefaultData($formanswer);
802-
$data['requesttypes_id'] = $data['requesttypes_id'] ?? PluginFormcreatorCommon::getFormcreatorRequestTypeId();
803831

804832
// Parse data
805833
// TODO: generate instances of all answers of the form and use them for the fullform computation
@@ -852,6 +880,7 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
852880
$data['users_id_lastupdater'] = $lastUpdater != '' ? $lastUpdater : 0;
853881

854882
$data = $this->setTargetType($data, $formanswer);
883+
$data = $this->setTargetSource($data, $formanswer);
855884
$data = $this->setTargetEntity($data, $formanswer, $requesters_id);
856885
$data = $this->setTargetDueDate($data, $formanswer);
857886
$data = $this->setSLA($data, $formanswer);
@@ -1033,6 +1062,16 @@ protected function setTargetLocation($data, $formanswer) {
10331062
return $data;
10341063
}
10351064

1065+
protected function setTargetSource(array $data, PluginFormcreatorFormAnswer $formanswer): array {
1066+
switch ($this->fields['source_rule']) {
1067+
case self::REQUESTSOURCE_SPECIFIC:
1068+
$data['requesttypes_id'] = $this->fields['source_question'];
1069+
break;
1070+
}
1071+
1072+
return $data;
1073+
}
1074+
10361075
protected function setTargetType(array $data, PluginFormcreatorFormAnswer $formanswer) {
10371076
global $DB;
10381077

@@ -1062,6 +1101,19 @@ protected function setTargetType(array $data, PluginFormcreatorFormAnswer $forma
10621101
return $data;
10631102
}
10641103

1104+
protected function showSourceSettings($rand): void {
1105+
echo '<tr>';
1106+
echo '<td width="15%">' . __('Request source') . '</td>';
1107+
echo '<td width="25%">';
1108+
Dropdown::showFromArray('source_rule', static::getEnumRequestSourceRule(), [
1109+
'value' => $this->fields['source_rule'],
1110+
'rand' => $rand,
1111+
]);
1112+
echo '<td></td><td></td>';
1113+
echo '</td>';
1114+
echo '</tr>';
1115+
}
1116+
10651117
protected function showTypeSettings($rand) {
10661118
echo '<tr>';
10671119
echo '<td width="15%">' . __('Request type') . '</td>';

install/mysql/plugin_formcreator_empty.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_targettickets` (
219219
`name` varchar(255) NOT NULL DEFAULT '',
220220
`plugin_formcreator_forms_id` int unsigned NOT NULL DEFAULT '0',
221221
`target_name` varchar(255) NOT NULL DEFAULT '',
222+
`source_rule` int(11) NOT NULL DEFAULT '0',
223+
`source_question` int(11) NOT NULL DEFAULT '0',
222224
`type_rule` int(11) NOT NULL DEFAULT '0',
223225
`type_question` int unsigned NOT NULL DEFAULT '0',
224226
`tickettemplates_id` int unsigned NOT NULL DEFAULT '0',

install/upgrade_to_2.12.5.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@ class PluginFormcreatorUpgradeTo2_12_5 {
3636
* @param Migration $migration
3737
*/
3838
public function upgrade(Migration $migration) {
39-
global $DB;
40-
4139
$this->migration = $migration;
4240

41+
$this->addUserRecipient();
42+
}
43+
44+
public function addUserRecipient(): void {
45+
global $DB;
46+
4347
// Add users_id_recipient
4448
$table = 'glpi_plugin_formcreator_issues';
4549
$this->migration->addField($table, 'users_id_recipient', 'integer');

install/upgrade_to_2.13.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function upgrade(Migration $migration) {
4646
$this->addTargetValidationSetting();
4747
$this->addFormVisibility();
4848
$this->addDashboardVisibility();
49+
$this->addRequestSourceSeting();
4950
}
5051

5152
public function addFormAnswerTitle() {
@@ -274,4 +275,18 @@ protected function migrateFkToUnsignedInt() {
274275
}
275276
$this->migration->changeField($table, 'id', 'id', 'int ' . DBConnection::getDefaultPrimaryKeySignOption() . ' not null auto_increment');
276277
}
278+
279+
public function addRequestSourceSeting(): void {
280+
global $DB;
281+
282+
$table = 'glpi_plugin_formcreator_targettickets';
283+
284+
if (!$DB->fieldExists($table, 'source_rule')) {
285+
$this->migration->addField($table, 'source_rule', 'integer', ['after' => 'target_name']);
286+
$this->migration->addField($table, 'source_question', 'integer', ['after' => 'source_rule']);
287+
$this->migration->migrationOneTable($table);
288+
$formcreatorSourceId = PluginFormcreatorCommon::getFormcreatorRequestTypeId();
289+
$DB->queryOrDie("UPDATE `$table` SET `source_rule` = '1', `source_question` = '$formcreatorSourceId'");
290+
}
291+
}
277292
}

tests/3-unit/PluginFormcreatorTargetTicket.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function beforeTestMethod($method) {
4545
case 'testDeleteLinkedTickets':
4646
case 'testSetTargetAssociatedItem':
4747
case 'testSetTargetLocation':
48+
case 'testSetRequestSource':
4849
$this->boolean($this->login('glpi', 'glpi'))->isTrue();
4950
break;
5051
}
@@ -77,6 +78,23 @@ public function testGetTypeName($number, $expected) {
7778
$this->string($output)->isEqualTo($expected);
7879
}
7980

81+
public function testGetEnumRequestTypeRule(): void {
82+
$output = \PluginFormcreatorTargetTicket::getEnumRequestTypeRule();
83+
$this->array($output)->isEqualTo([
84+
\PluginFormcreatorTargetTicket::REQUESTTYPE_NONE => 'Default or from a template',
85+
\PluginFormcreatorTargetTicket::REQUESTTYPE_SPECIFIC => "Specific type",
86+
\PluginFormcreatorTargetTicket::REQUESTTYPE_ANSWER => "Equals to the answer to the question",
87+
]);
88+
}
89+
90+
public function testGetEnumRequestSourceRule(): void {
91+
$output = \PluginFormcreatorTargetTicket::getEnumRequestSourceRule();
92+
$this->array($output)->isEqualTo([
93+
\PluginFormcreatorTargetTicket::REQUESTTYPE_NONE => 'Source from template or user default or GLPI default',
94+
\PluginFormcreatorTargetTicket::REQUESTTYPE_SPECIFIC => "Formcreator",
95+
]);
96+
}
97+
8098
public function testGetEnumDestinationEntity() {
8199
$output = \PluginFormcreatorTargetTicket::getEnumDestinationEntity();
82100
$this->array($output)->isEqualTo([
@@ -638,6 +656,8 @@ public function testExport() {
638656
$fieldsWithoutID = [
639657
'name',
640658
'target_name',
659+
'source_rule',
660+
'source_question',
641661
'type_rule',
642662
'type_question',
643663
'content',
@@ -712,6 +732,8 @@ public function testImport() {
712732
'category_question' => '0',
713733
'associate_rule' => \PluginFormcreatorTargetTicket::ASSOCIATE_RULE_NONE,
714734
'associate_question' => '0',
735+
'source_rule' => 0,
736+
'source_question' => 0,
715737
'type_rule' => 1,
716738
'type_question' => 0,
717739
'uuid' => $uuid,
@@ -1266,6 +1288,7 @@ public function providerPrepareInputForAdd() {
12661288
$formFk = \PluginFormcreatorForm::getForeignKeyField();
12671289
$form = $this->getForm();
12681290
$name = $this->getUniqueString();
1291+
$sourceId = \PluginFormcreatorCommon::getFormcreatorRequestTypeId();
12691292
return [
12701293
'name is mandatory' => [
12711294
'input' => [
@@ -1287,6 +1310,8 @@ public function providerPrepareInputForAdd() {
12871310
'type_rule' => \PluginFormcreatorTargetTicket::REQUESTTYPE_SPECIFIC,
12881311
'type_question' => \Ticket::INCIDENT_TYPE,
12891312
'show_rule' => \PluginFormcreatorCondition::SHOW_RULE_ALWAYS,
1313+
'source_rule' => \PluginFormcreatorTargetTicket::REQUESTSOURCE_SPECIFIC,
1314+
'source_question' => $sourceId,
12901315
],
12911316
'message' => null,
12921317
],
@@ -1296,6 +1321,7 @@ public function providerPrepareInputForAdd() {
12961321
'name' => $name,
12971322
'type_rule' => \PluginFormcreatorTargetTicket::REQUESTTYPE_SPECIFIC,
12981323
'type_question' => \Ticket::DEMAND_TYPE,
1324+
'source_rule' => \PluginFormcreatorTargetTicket::REQUESTSOURCE_NONE,
12991325
],
13001326
'expected' => [
13011327
$formFk => $form->getID(),
@@ -1305,6 +1331,8 @@ public function providerPrepareInputForAdd() {
13051331
'type_rule' => \PluginFormcreatorTargetTicket::REQUESTTYPE_SPECIFIC,
13061332
'type_question' => \Ticket::DEMAND_TYPE,
13071333
'show_rule' => \PluginFormcreatorCondition::SHOW_RULE_ALWAYS,
1334+
'source_rule' => \PluginFormcreatorTargetTicket::REQUESTSOURCE_NONE,
1335+
'source_question' => 0,
13081336
],
13091337
'message' => null,
13101338
],
@@ -1437,4 +1465,78 @@ public function testSetTargetLocation($instance, $formanswer, $expected) {
14371465
$this->array($output)->notHasKey('locations_id');
14381466
}
14391467
}
1468+
1469+
public function providerSetRequestSource_none(): array {
1470+
$form = $this->getForm();
1471+
$formanswer = new \PluginFormcreatorFormanswer();
1472+
$formanswer->add([
1473+
'plugin_formcreator_forms_id' => $form->getID(),
1474+
]);
1475+
$this->boolean($formanswer->isNewItem())->isFalse();
1476+
$targetTicket = new \PluginFormcreatorTargetTicket();
1477+
$targetTicket->add([
1478+
'name' => 'target ticket',
1479+
'target_name' => 'target ticket',
1480+
'plugin_formcreator_forms_id' => $form->getID(),
1481+
'source_rule' => \PluginFormcreatorTargetTicket::REQUESTSOURCE_NONE,
1482+
]);
1483+
$this->boolean($targetTicket->isNewItem())->isFalse();
1484+
1485+
return [
1486+
[
1487+
'instance' => $targetTicket,
1488+
'formanswer' => $formanswer,
1489+
'expected' => 0
1490+
],
1491+
];
1492+
}
1493+
1494+
public function providerSetRequestSource_specific(): array {
1495+
$form = $this->getForm();
1496+
$formanswer = new \PluginFormcreatorFormanswer();
1497+
$formanswer->add([
1498+
'plugin_formcreator_forms_id' => $form->getID(),
1499+
]);
1500+
$this->boolean($formanswer->isNewItem())->isFalse();
1501+
$targetTicket = new \PluginFormcreatorTargetTicket();
1502+
$targetTicket->add([
1503+
'name' => 'target ticket',
1504+
'target_name' => 'target ticket',
1505+
'plugin_formcreator_forms_id' => $form->getID(),
1506+
'source_rule' => \PluginFormcreatorTargetTicket::REQUESTSOURCE_SPECIFIC,
1507+
'source_question' => \PluginFormcreatorCommon::getFormcreatorRequestTypeId(),
1508+
]);
1509+
$this->boolean($targetTicket->isNewItem())->isFalse();
1510+
1511+
return [
1512+
[
1513+
'instance' => $targetTicket,
1514+
'formanswer' => $formanswer,
1515+
'expected' => 0
1516+
],
1517+
];
1518+
}
1519+
1520+
public function providerSetRequestSource(): array {
1521+
return array_merge(
1522+
$this->providerSetRequestSource_none(),
1523+
$this->providerSetRequestSource_specific()
1524+
);
1525+
}
1526+
1527+
/**
1528+
* @dataProvider providerSetRequestSource
1529+
*/
1530+
public function testSetRequestSource($instance, $formanswer, $expected): void {
1531+
// Substitute a dummy class to access protected / private methods
1532+
$dummyItemtype = 'GlpiPlugin\Formcreator\Tests\\' . $this->getTestedClassName() . 'Dummy';
1533+
$dummyInstance = new $dummyItemtype();
1534+
/**@var \GlpiPlugin\Formcreator\Tests\PluginFormcreatorTargetTicketDummy */
1535+
$instance->getFromDB($instance->getID());
1536+
$dummyInstance->fields = $instance->fields;
1537+
1538+
$data = $dummyInstance->publicGetDefaultData($formanswer);
1539+
$output = $dummyInstance->publicSetTargetCategory($data, $formanswer);
1540+
$this->integer((int) $output['itilcategories_id'])->isEqualTo($expected);
1541+
}
14401542
}

tests/src/PluginFormcreatorTargetTicketDummy.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,8 @@ public function publicGetDefaultData($formanswer) {
9696
public function publicSetTargetLocation($data, $formanswer) {
9797
return $this->setTargetLocation($data, $formanswer);
9898
}
99+
100+
public function publicSetTargetSource($data, $formanswer): array {
101+
return $this->setTargetSource($data, $formanswer);
102+
}
99103
}

0 commit comments

Comments
 (0)