Skip to content

Commit

Permalink
Not imported template (#3449)
Browse files Browse the repository at this point in the history
fix(targetticket,targetchange,targetproblem): not imported template
  • Loading branch information
btry authored Feb 29, 2024
1 parent cb9ce0f commit 29ed099
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 0 deletions.
16 changes: 16 additions & 0 deletions inc/abstracttarget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ abstract public static function getTargetItemtypeName(): string;
*/
abstract protected function getTaggableFields();

abstract protected function getTemplateItemtypeName();

const DESTINATION_ENTITY_CURRENT = 1;
const DESTINATION_ENTITY_REQUESTER = 2;
const DESTINATION_ENTITY_REQUESTER_DYN_FIRST = 3;
Expand Down Expand Up @@ -561,4 +563,18 @@ public function prepareInputForClone($input) {
unset($input['uuid']);
return $input;
}

protected static function getTemplateByName(string $name): int {
$targetTemplateType = (new static())->getTemplateItemtypeName();
$targetTemplate = new $targetTemplateType();
$targetTemplate->getFromDBByCrit([
'name' => $name,
]);

if ($targetTemplate->isNewItem()) {
return 0;
}

return $targetTemplate->getID();
}
}
10 changes: 10 additions & 0 deletions inc/targetchange.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,16 @@ public static function import(PluginFormcreatorLinker $linker, array $input = []
$input[$fieldSetting['field']] = $question->getID();
}

// Find template by name
$input['changetemplates_id'] = 0;
if (is_string($input['_changetemplate']) && strlen($input['_changetemplate']) > 0) {
$input['changetemplates_id'] = self::getTemplateByName($input['_changetemplate']);
if ($input['changetemplates_id'] === 0) {
$typeName = strtolower(self::getTypeName());
throw new ImportFailureException(sprintf(__('Failed to add or update the %1$s %2$s: It uses a non existent template', 'formceator'), $typeName, $input['name']));
}
}

// Add or update
$originalId = $input[$idKey];
$item->skipChecks = true;
Expand Down
9 changes: 9 additions & 0 deletions inc/targetproblem.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,15 @@ public static function import(PluginFormcreatorLinker $linker, array $input = []
$input[$fieldSetting['field']] = $question->getID();
}

// Find template by name
$input['problemtemplates_id'] = 0;
if (is_string($input['_problemtemplate']) && strlen($input['_problemtemplate']) > 0) {
$input['problemtemplates_id'] = self::getTemplateByName($input['_problemtemplate']);
if ($input['problemtemplates_id'] === 0) {
$typeName = strtolower(self::getTypeName());
throw new ImportFailureException(sprintf(__('Failed to add or update the %1$s %2$s: It uses a non existent template', 'formceator'), $typeName, $input['name']));
}
}
// Add or update
$originalId = $input[$idKey];
$item->skipChecks = true;
Expand Down
10 changes: 10 additions & 0 deletions inc/targetticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,16 @@ public static function import(PluginFormcreatorLinker $linker, array $input = []
$input[$fieldSetting['field']] = $question->getID();
}

// Find template by name
$input['tickettemplates_id'] = 0;
if (is_string($input['_tickettemplate']) && strlen($input['_tickettemplate']) > 0) {
$input['tickettemplates_id'] = self::getTemplateByName($input['_tickettemplate'] ?? '');
if ($input['tickettemplates_id'] === 0) {
$typeName = strtolower(self::getTypeName());
throw new ImportFailureException(sprintf(__('Failed to add or update the %1$s %2$s: It uses a non existent template', 'formceator'), $typeName, $input['name']));
}
}

// Add or update
$originalId = $input[$idKey];
$item->skipChecks = true;
Expand Down
1 change: 1 addition & 0 deletions tests/3-unit/PluginFormcreatorTargetChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ public function testImport() {
'tag_specifics' => '',
'category_rule' => \PluginFormcreatorTargetChange::CATEGORY_RULE_NONE,
'category_question' => '0',
'_changetemplate' => '',
'uuid' => $uuid,
];

Expand Down
1 change: 1 addition & 0 deletions tests/3-unit/PluginFormcreatorTargetTicket.php
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,7 @@ public function testImport() {
'source_question' => 0,
'type_rule' => 1,
'type_question' => 0,
'_tickettemplate' => '',
'uuid' => $uuid,
];

Expand Down

0 comments on commit 29ed099

Please sign in to comment.