Skip to content

Commit

Permalink
fix(target_actor): export / import issue
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Aug 22, 2019
1 parent 0592792 commit a88df90
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 37 deletions.
2 changes: 1 addition & 1 deletion inc/section.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
79 changes: 43 additions & 36 deletions inc/target_actor.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand Down

0 comments on commit a88df90

Please sign in to comment.