Skip to content

Commit

Permalink
refactor: improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Feb 6, 2024
1 parent ed7e704 commit 0438c55
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
8 changes: 6 additions & 2 deletions inc/abstracttarget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -564,13 +564,17 @@ public function prepareInputForClone($input) {
return $input;
}

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

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

return $targetTemplate->getID();
}
}
7 changes: 3 additions & 4 deletions inc/targetchange.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,12 @@ public static function import(PluginFormcreatorLinker $linker, array $input = []

// Find template by name
$input['changetemplates_id'] = 0;
if ($input['_changetemplate'] !== '') {
$targetTemplate = self::getTemplateByName($input['_changetemplate'] ?? '');
if ($targetTemplate->isNewItem()) {
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']));
}
$input['changetemplates_id'] = $targetTemplate->getID();
}

// Add or update
Expand Down
10 changes: 4 additions & 6 deletions inc/targetproblem.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,15 +527,13 @@ public static function import(PluginFormcreatorLinker $linker, array $input = []

// Find template by name
$input['problemtemplates_id'] = 0;
if ($input['_problemtemplate'] !== '') {
$targetTemplate = self::getTemplateByName($input['_problemtemplate'] ?? '');
if ($targetTemplate->isNewItem()) {
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']));
throw new ImportFailureException(sprintf(__('Failed to add or update the %1$s %2$s: It uses a non existent template', 'formceator'), $typeName, $input['name']));
}
$input['problemtemplates_id'] = $targetTemplate->getID();
}

// Add or update
$originalId = $input[$idKey];
$item->skipChecks = true;
Expand Down
7 changes: 3 additions & 4 deletions inc/targetticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1406,13 +1406,12 @@ public static function import(PluginFormcreatorLinker $linker, array $input = []

// Find template by name
$input['tickettemplates_id'] = 0;
if ($input['_tickettemplate'] !== '') {
$targetTemplate = self::getTemplateByName($input['_tickettemplate'] ?? '');
if ($targetTemplate->isNewItem()) {
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']));
}
$input['tickettemplates_id'] = $targetTemplate->getID();
}

// Add or update
Expand Down

0 comments on commit 0438c55

Please sign in to comment.