From 280c7924a9df5e8b14f5e5cdc85170fc2d676983 Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Fri, 7 Jan 2022 09:38:40 +0100 Subject: [PATCH] refactor(condition): simplifcation --- ajax/condition.php | 14 ++++++++++- inc/condition.class.php | 25 +++---------------- templates/components/form/condition.html.twig | 8 +++--- 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/ajax/condition.php b/ajax/condition.php index f4c34ded1..dfe6e95c2 100644 --- a/ajax/condition.php +++ b/ajax/condition.php @@ -32,6 +32,18 @@ include ('../../../inc/includes.php'); Session::checkRight('entity', UPDATE); +// integrity check +if (!isset($_POST['itemtype']) || !isset($_POST['items_id'])) { + http_response_code(400); + die(); +} +if (!is_subclass_of($_POST['itemtype'], PluginFormcreatorConditionnableInterface::class)) { + http_response_code(400); + die(); +} + // get an empty condition HTML table row $condition = new PluginFormcreatorCondition(); -echo $condition->getConditionHtml($_POST); +$condition->fields['itemtype'] = $_POST['itemtype']; +$condition->fields['items_id'] = $_POST['items_id']; +echo $condition->getConditionHtml(); diff --git a/inc/condition.class.php b/inc/condition.class.php index f790e1124..268d0af91 100644 --- a/inc/condition.class.php +++ b/inc/condition.class.php @@ -290,7 +290,7 @@ public function showConditionsForItem(PluginFormcreatorConditionnableInterface $ $conditions = self::getConditionsFromItem($item); foreach ($conditions as $condition) { echo ''; - echo $condition->getConditionHtml($item->fields); + echo $condition->getConditionHtml(); echo ''; } } @@ -347,36 +347,17 @@ public static function getQuestionsExclusion(PluginFormcreatorConditionnableInte /** * return HTML to show a condition line for a question * - * @param array $input data of the item the condition applies to - * * @return string HTML to insert in a rendered web page */ - public function getConditionHtml(array $input): string { - if ($this->isNewItem()) { - $this->getEmpty(); - $itemtype = $input['itemtype']; - $questionId = ''; - } else { - $itemtype = $this->fields['itemtype']; - $questionId = $this->fields['plugin_formcreator_questions_id']; - } + public function getConditionHtml(): string { + $itemtype = $this->fields['itemtype']; if (!is_subclass_of($itemtype, PluginFormcreatorConditionnableInterface::class)) { // security check throw new RuntimeException("$itemtype is not a " . PluginFormcreatorConditionnableInterface::class); } - $item = new $itemtype(); - if (!isset($input['id']) || !$item->getFromDB($input['id'])) { - $item->getEmpty(); - $parentFk = $item::$items_id; - $item->fields[$parentFk] = $input[$parentFk]; - } $out = TemplateRenderer::getInstance()->render('@formcreator/components/form/condition.html.twig', [ 'condition' => $this, - 'item' => $item, - 'params' => [ - 'questionId' => $questionId, - ], ]); return $out; diff --git a/templates/components/form/condition.html.twig b/templates/components/form/condition.html.twig index c27011bc1..dd60cd547 100644 --- a/templates/components/form/condition.html.twig +++ b/templates/components/form/condition.html.twig @@ -55,11 +55,11 @@ 'rand': random(), 'width': '100%', } %} - {% set excludeQuestions = params.excludeQuestions ?? [] %} - {% set excludeQuestions = call('PluginFormcreatorCondition::getQuestionsExclusion', [item]) %} - {% set form = call('PluginFormcreatorForm::getByItem', [item]) %} + {% set parent = get_item(condition.fields['itemtype'], condition.fields['items_id']) %} + {% set excludeQuestions = call('PluginFormcreatorCondition::getQuestionsExclusion', [parent]) %} + {% set form = call('PluginFormcreatorForm::getByItem', [parent]) %} {% set field %} - {% do call('PluginFormcreatorQuestion::dropdownForForm', [form, excludeQuestions, '_conditions[plugin_formcreator_questions_id][]', params.questionId]) %} + {% do call('PluginFormcreatorQuestion::dropdownForForm', [form, excludeQuestions, '_conditions[plugin_formcreator_questions_id][]', condition.fields['plugin_formcreator_questions_id']]) %} {% endset %} {{ fields.field(name, field, label, options|merge({'id': 'dropdown_' ~ name ~ '_' ~ options.rand})) }}