Skip to content

Commit

Permalink
fix(question,section): duplication failure
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Feb 5, 2020
1 parent 58e0101 commit 08e934b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
21 changes: 16 additions & 5 deletions inc/condition.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con
throw new ImportFailureException('UUID or ID is mandatory');
}

//$itemtypeFk = $input['itemtype']::getForeignKeyField();
//$questionFk = PluginFormcreatorQuestion::getForeignKeyField();
$input['items_id'] = $containerId;

$item = new self();
Expand All @@ -119,10 +117,16 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con
}

// set ID for linked objects
$linked = $linker->getObject($input['plugin_formcreator_questions_id'], $input['itemtype']);
$linked = $linker->getObject($input['plugin_formcreator_questions_id'], PluginFormcreatorQuestion::class);
if ($linked === false) {
$linker->postpone($input[$idKey], $item->getType(), $input, $containerId);
return false;
$linked = new PluginFormcreatorQuestion();
$linked->getFromDBByCrit([
$idKey => $input['plugin_formcreator_questions_id']
]);
if ($linked->isNewItem()) {
$linker->postpone($input[$idKey], $item->getType(), $input, $containerId);
return false;
}
}
$input['plugin_formcreator_questions_id'] = $linked->getID();

Expand Down Expand Up @@ -170,6 +174,13 @@ public function export($remove_uuid = false) {
$question = new PluginFormcreatorQuestion();
$question->getFromDB($condition['plugin_formcreator_questions_id']);
$condition['plugin_formcreator_questions_id'] = $question->fields['uuid'];
$containerType = $input['itemtype'];
if (!class_exists($containerType) || !is_subclass_of($containerType, PluginFormcreatorConditionnableInterface::class)) {
return false;
}
$container = new $containerType();
$container->getFromDB($condition['items_id']);
$condition['items_id'] = $container->fields['uuid'];
}
unset($condition[$idToRemove]);

Expand Down
10 changes: 8 additions & 2 deletions inc/questiondependency.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,14 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con
// set ID for linked objects
$linked = $linker->getObject($input['plugin_formcreator_questions_id_2'], PluginFormcreatorQuestion::class);
if ($linked === false) {
$linker->postpone($input[$idKey], $item->getType(), $input, $containerId);
return false;
$linked = new PluginFormcreatorQuestion();
$linked->getFromDBByCrit([
$idKey => $input['plugin_formcreator_questions_id']
]);
if ($linked->isNewItem()) {
$linker->postpone($input[$idKey], $item->getType(), $input, $containerId);
return false;
}
}
$input['plugin_formcreator_questions_id_2'] = $linked->getID();

Expand Down
18 changes: 17 additions & 1 deletion inc/section.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con
// add the section to the linker
$linker->addObject($originalId, $item);

// Import the questions
if (isset($input['_questions'])) {
// sort questions by order
usort($input['_questions'], function ($a, $b) {
Expand All @@ -274,6 +275,13 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con
}
}

// Import conditions
if (isset($input['_conditions'])) {
foreach ($input['_conditions'] as $condition) {
PluginFormcreatorCondition::import($linker, $condition, $itemId);
}
}

return $itemId;
}

Expand All @@ -284,7 +292,7 @@ public function export($remove_uuid = false) {
return false;
}

$section = $this->fields;
$section = $this->fields;

// remove key and fk
$formFk = PluginFormcreatorForm::getForeignKeyField();
Expand All @@ -306,6 +314,14 @@ public function export($remove_uuid = false) {
}
}

// get question conditions
$section['_conditions'] = [];
$condition = new PluginFormcreatorCondition();
$all_conditions = $condition->getConditionsFromItem($this);
foreach ($all_conditions as $condition) {
$section['_conditions'][] = $condition->export($remove_uuid);
}

// remove ID or UUID
$idToRemove = 'id';
if ($remove_uuid) {
Expand Down
1 change: 1 addition & 0 deletions tests/suite-unit/PluginFormcreatorSection.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public function testExport() {
];
$extraFields = [
'_questions',
'_conditions',
];
$this->array($output)
->hasKeys($fieldsWithoutID + $extraFields + ['uuid'])
Expand Down

0 comments on commit 08e934b

Please sign in to comment.