Skip to content

Commit

Permalink
refactor(condition): simplifcation
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Jan 31, 2022
1 parent 6a76c43 commit 280c792
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
14 changes: 13 additions & 1 deletion ajax/condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
25 changes: 3 additions & 22 deletions inc/condition.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public function showConditionsForItem(PluginFormcreatorConditionnableInterface $
$conditions = self::getConditionsFromItem($item);
foreach ($conditions as $condition) {
echo '<tr><td colspan="4">';
echo $condition->getConditionHtml($item->fields);
echo $condition->getConditionHtml();
echo '</td></tr>';
}
}
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions templates/components/form/condition.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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})) }}

Expand Down

0 comments on commit 280c792

Please sign in to comment.