Skip to content

Commit 29ed099

Browse files
authored
Not imported template (#3449)
fix(targetticket,targetchange,targetproblem): not imported template
1 parent cb9ce0f commit 29ed099

6 files changed

Lines changed: 47 additions & 0 deletions

inc/abstracttarget.class.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ abstract public static function getTargetItemtypeName(): string;
7979
*/
8080
abstract protected function getTaggableFields();
8181

82+
abstract protected function getTemplateItemtypeName();
83+
8284
const DESTINATION_ENTITY_CURRENT = 1;
8385
const DESTINATION_ENTITY_REQUESTER = 2;
8486
const DESTINATION_ENTITY_REQUESTER_DYN_FIRST = 3;
@@ -561,4 +563,18 @@ public function prepareInputForClone($input) {
561563
unset($input['uuid']);
562564
return $input;
563565
}
566+
567+
protected static function getTemplateByName(string $name): int {
568+
$targetTemplateType = (new static())->getTemplateItemtypeName();
569+
$targetTemplate = new $targetTemplateType();
570+
$targetTemplate->getFromDBByCrit([
571+
'name' => $name,
572+
]);
573+
574+
if ($targetTemplate->isNewItem()) {
575+
return 0;
576+
}
577+
578+
return $targetTemplate->getID();
579+
}
564580
}

inc/targetchange.class.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,16 @@ public static function import(PluginFormcreatorLinker $linker, array $input = []
288288
$input[$fieldSetting['field']] = $question->getID();
289289
}
290290

291+
// Find template by name
292+
$input['changetemplates_id'] = 0;
293+
if (is_string($input['_changetemplate']) && strlen($input['_changetemplate']) > 0) {
294+
$input['changetemplates_id'] = self::getTemplateByName($input['_changetemplate']);
295+
if ($input['changetemplates_id'] === 0) {
296+
$typeName = strtolower(self::getTypeName());
297+
throw new ImportFailureException(sprintf(__('Failed to add or update the %1$s %2$s: It uses a non existent template', 'formceator'), $typeName, $input['name']));
298+
}
299+
}
300+
291301
// Add or update
292302
$originalId = $input[$idKey];
293303
$item->skipChecks = true;

inc/targetproblem.class.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,15 @@ public static function import(PluginFormcreatorLinker $linker, array $input = []
525525
$input[$fieldSetting['field']] = $question->getID();
526526
}
527527

528+
// Find template by name
529+
$input['problemtemplates_id'] = 0;
530+
if (is_string($input['_problemtemplate']) && strlen($input['_problemtemplate']) > 0) {
531+
$input['problemtemplates_id'] = self::getTemplateByName($input['_problemtemplate']);
532+
if ($input['problemtemplates_id'] === 0) {
533+
$typeName = strtolower(self::getTypeName());
534+
throw new ImportFailureException(sprintf(__('Failed to add or update the %1$s %2$s: It uses a non existent template', 'formceator'), $typeName, $input['name']));
535+
}
536+
}
528537
// Add or update
529538
$originalId = $input[$idKey];
530539
$item->skipChecks = true;

inc/targetticket.class.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,16 @@ public static function import(PluginFormcreatorLinker $linker, array $input = []
14041404
$input[$fieldSetting['field']] = $question->getID();
14051405
}
14061406

1407+
// Find template by name
1408+
$input['tickettemplates_id'] = 0;
1409+
if (is_string($input['_tickettemplate']) && strlen($input['_tickettemplate']) > 0) {
1410+
$input['tickettemplates_id'] = self::getTemplateByName($input['_tickettemplate'] ?? '');
1411+
if ($input['tickettemplates_id'] === 0) {
1412+
$typeName = strtolower(self::getTypeName());
1413+
throw new ImportFailureException(sprintf(__('Failed to add or update the %1$s %2$s: It uses a non existent template', 'formceator'), $typeName, $input['name']));
1414+
}
1415+
}
1416+
14071417
// Add or update
14081418
$originalId = $input[$idKey];
14091419
$item->skipChecks = true;

tests/3-unit/PluginFormcreatorTargetChange.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ public function testImport() {
505505
'tag_specifics' => '',
506506
'category_rule' => \PluginFormcreatorTargetChange::CATEGORY_RULE_NONE,
507507
'category_question' => '0',
508+
'_changetemplate' => '',
508509
'uuid' => $uuid,
509510
];
510511

tests/3-unit/PluginFormcreatorTargetTicket.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,7 @@ public function testImport() {
782782
'source_question' => 0,
783783
'type_rule' => 1,
784784
'type_question' => 0,
785+
'_tickettemplate' => '',
785786
'uuid' => $uuid,
786787
];
787788

0 commit comments

Comments
 (0)