From a88df90288214a7964341e8bba056890e15a5afc Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Fri, 5 Jul 2019 19:23:33 +0200 Subject: [PATCH] fix(target_actor): export / import issue --- inc/section.class.php | 2 +- inc/target_actor.class.php | 79 +++++++++++++++++++++----------------- 2 files changed, 44 insertions(+), 37 deletions(-) diff --git a/inc/section.class.php b/inc/section.class.php index 34f9a98c2..55417ce99 100644 --- a/inc/section.class.php +++ b/inc/section.class.php @@ -293,7 +293,7 @@ public function export($remove_uuid = false) { 'SELECT' => ['id'], 'FROM' => $form_question::getTable(), 'WHERE' => [ - 'plugin_formcreator_sections_id' => $this->getID() + self::getForeignKeyField() => $this->getID() ] ]); foreach ($all_questions as $question) { diff --git a/inc/target_actor.class.php b/inc/target_actor.class.php index a3473686e..c57108ba3 100644 --- a/inc/target_actor.class.php +++ b/inc/target_actor.class.php @@ -105,34 +105,44 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con } // set ID for linked objects - if (isset($input['_question'])) { - $linked = $linker->getObject($input['_question'], PluginFormcreatorQuestion::class); - if ($linked === false) { - $linker->postpone($input[$idKey], $item->getType(), $input, $containerId); - return false; - } - $input['actor_value'] = $linked->getID(); - } else if (isset($input['_user'])) { - $user = new User; - $users_id = plugin_formcreator_getFromDBByField($user, 'name', $input['_user']); - if ($users_id === false) { - throw new ImportFailureException('failed to find a user'); - } - $input['actor_value'] = $users_id; - } else if (isset($input['_group'])) { - $group = new Group; - $groups_id = plugin_formcreator_getFromDBByField($group, 'completename', $input['_group']); - if ($groups_id === false) { - throw new ImportFailureException('failed to find a group'); - } - $input['actor_value'] = $groups_id; - } else if (isset($input['_supplier'])) { - $supplier = new Supplier; - $suppliers_id = plugin_formcreator_getFromDBByField($supplier, 'name', $input['_supplier']); - if ($suppliers_id === false) { - throw new ImportFailureException('failed to find a supplier'); - } - $input['actor_value'] = $suppliers_id; + switch ($input['actor_type']) { + case self::ACTOR_TYPE_QUESTION_PERSON : + case self::ACTOR_TYPE_QUESTION_GROUP : + case self::ACTOR_TYPE_QUESTION_SUPPLIER : + $question = $linker->getObject($input['actor_value'], PluginFormcreatorQuestion::class); + if ($question === false) { + $linker->postpone($input[$idKey], $item->getType(), $input, $containerId); + return false; + } + $input['actor_value'] = $question->getID(); + break; + + case self::ACTOR_TYPE_PERSON: + $user = new User; + $users_id = plugin_formcreator_getFromDBByField($user, 'name', $input['_user']); + if ($users_id === false) { + throw new ImportFailureException('failed to find a user'); + } + $input['actor_value'] = $users_id; + break; + + case self::ACTOR_TYPE_GROUP: + $group = new Group; + $groups_id = plugin_formcreator_getFromDBByField($group, 'completename', $input['_group']); + if ($groups_id === false) { + throw new ImportFailureException('failed to find a group'); + } + $input['actor_value'] = $groups_id; + break; + + case self::ACTOR_TYPE_SUPPLIER: + $supplier = new Supplier; + $suppliers_id = plugin_formcreator_getFromDBByField($supplier, 'name', $input['_supplier']); + if ($suppliers_id === false) { + throw new ImportFailureException('failed to find a supplier'); + } + $input['actor_value'] = $suppliers_id; + break; } $originalId = $input[$idKey]; @@ -166,6 +176,7 @@ public function export($remove_uuid = false) { $target_actor = $this->fields; + // remove key and fk unset($target_actor[static::$items_id]); // remove ID or UUID @@ -181,29 +192,25 @@ public function export($remove_uuid = false) { case self::ACTOR_TYPE_QUESTION_ACTORS: $question = new PluginFormcreatorQuestion; if ($question->getFromDB($target_actor['actor_value'])) { - $target_actor['_question'] = $question->fields['uuid']; - unset($target_actor['actor_value']); + $target_actor['actor_value'] = $question->fields['uuid']; } break; case self::ACTOR_TYPE_PERSON: $user = new User; if ($user->getFromDB($target_actor['actor_value'])) { - $target_actor['_user'] = $user->fields['name']; - unset($target_actor['actor_value']); + $target_actor['actor_value'] = $user->fields['name']; } break; case self::ACTOR_TYPE_GROUP: $group = new Group; if ($group->getFromDB($target_actor['actor_value'])) { - $target_actor['_group'] = $group->fields['completename']; - unset($target_actor['actor_value']); + $target_actor['actor_value'] = $group->fields['completename']; } break; case self::ACTOR_TYPE_SUPPLIER: $supplier = new Supplier; if ($supplier->getFromDB($target_actor['actor_value'])) { - $target_actor['_supplier'] = $supplier->fields['name']; - unset($target_actor['actor_value']); + $target_actor['actor_value'] = $supplier->fields['name']; } break; }