Skip to content

Commit

Permalink
fix(section,question): modals malfunctions
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Jan 31, 2022
1 parent c676cb5 commit cf22eb0
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 71 deletions.
6 changes: 5 additions & 1 deletion ajax/condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@
http_response_code(400);
die();
}
/** @var CommonDBTM $parent */
$parent = new $_POST['itemtype'];
$parent->getEmpty();
$parent->fields = array_intersect_key($_POST, $parent->fields);

// get an empty condition HTML table row
$condition = new PluginFormcreatorCondition();
$condition->fields['itemtype'] = $_POST['itemtype'];
$condition->fields['items_id'] = $_POST['items_id'];
echo $condition->getConditionHtml();
echo $condition->getConditionHtml($parent);
2 changes: 1 addition & 1 deletion ajax/question_add.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@
'height' => '1',
'html' => $question->getDesignHtml(),
];
echo json_encode($json);
echo json_encode($json, JSON_UNESCAPED_UNICODE);
20 changes: 10 additions & 10 deletions ajax/question_update.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,28 @@
Session::checkRight('entity', UPDATE);

if (!isset($_REQUEST['id'])) {
echo __('Bad request', 'formcreator');
Session::addMessageAfterRedirect(__('Bad request', 'formcreator'), false, ERROR);
http_response_code(400);
exit();
}
$questionId = (int) $_REQUEST['id'];

$question = new PluginFormcreatorQuestion();
if (!$question->getFromDB($questionId)) {
http_response_code(404);
echo __('Question not found', 'formcreator');
exit;
http_response_code(404);
Session::addMessageAfterRedirect(__('Question not found', 'formcreator'), false, ERROR);
exit;
}

if (!$question->canUpdate()) {
http_response_code(403);
echo __('You don\'t have right for this action', 'formcreator');
exit;
http_response_code(403);
Session::addMessageAfterRedirect(__('You don\'t have right for this action', 'formcreator'), false, ERROR);
exit;
}

$success = $question->update($_REQUEST);
if (!$success) {
http_response_code(500);
exit();
http_response_code(500);
exit();
}
echo $question->fields['name'];
echo json_encode(['name' => $question->fields['name']], JSON_UNESCAPED_UNICODE);
6 changes: 3 additions & 3 deletions ajax/section_add.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
$section = new PluginFormcreatorSection();
if (!$section->canCreate()) {
http_response_code(403);
echo __('You don\'t have right for this action', 'formcreator');
Session::addMessageAfterRedirect(__('You don\'t have right for this action', 'formcreator'), false, ERROR);
exit;
}

if (!$section->add($_REQUEST)) {
http_response_code(500);
echo __('Could not add the section', 'formcreator');
Session::addMessageAfterRedirect(__('Could not add the section', 'formcreator'), false, ERROR);
exit;
}
echo $section->getDesignHtml();
echo json_encode(['id' => $section->getID(), 'html' => $section->getDesignHtml()], JSON_UNESCAPED_UNICODE);
7 changes: 4 additions & 3 deletions ajax/section_update.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,20 @@

if (!isset($_REQUEST['id'])) {
http_response_code(400);
Session::addMessageAfterRedirect(__('Bad request', 'formcreator'), false, ERROR);
exit;
}

$section = new PluginFormcreatorSection();
if (!$section->canUpdate()) {
http_response_code(403);
echo __('You don\'t have right for this action', 'formcreator');
Session::addMessageAfterRedirect(__('You don\'t have right for this action', 'formcreator'), false, ERROR);
exit;
}

if (!$section->update($_REQUEST)) {
http_response_code(500);
echo __('Could not update the section', 'formcreator');
Session::addMessageAfterRedirect(__('Could not update the section', 'formcreator'), false, ERROR);
exit;
}
echo $section->fields['name'];
echo json_encode(['id' => $section->getID(), 'name' => $section->fields['name']], JSON_UNESCAPED_UNICODE);
13 changes: 9 additions & 4 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();
echo $condition->getConditionHtml($item);
echo '</td></tr>';
}
}
Expand All @@ -301,7 +301,11 @@ public function showConditionsForItem(PluginFormcreatorConditionnableInterface $
* @param PluginFormcreatorConditionnableInterface $item
* @return void
*/
public static function getQuestionsExclusion(PluginFormcreatorConditionnableInterface $item) {
public static function getQuestionsExclusion(?PluginFormcreatorConditionnableInterface $item) {
if ($item === null) {
return [];
}

/** @var CommonDBTM $item */
$itemtype = $item->getType();
switch ($itemtype) {
Expand Down Expand Up @@ -349,15 +353,16 @@ public static function getQuestionsExclusion(PluginFormcreatorConditionnableInte
*
* @return string HTML to insert in a rendered web page
*/
public function getConditionHtml(): string {
$itemtype = $this->fields['itemtype'];
public function getConditionHtml(CommonDBTM $parent): string {
$itemtype = $parent->getType();
if (!is_subclass_of($itemtype, PluginFormcreatorConditionnableInterface::class)) {
// security check
throw new RuntimeException("$itemtype is not a " . PluginFormcreatorConditionnableInterface::class);
}

$out = TemplateRenderer::getInstance()->render('@formcreator/components/form/condition.html.twig', [
'condition' => $this,
'parent' => $parent
]);

return $out;
Expand Down
6 changes: 3 additions & 3 deletions inc/conditionnabletrait.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,18 @@ public function checkConditionSettings(array $input): bool {
}

public function updateConditions($input) : bool {
$input = $input['_conditions'];

$itemtype = $this->getType();
$itemId = $this->getID();

// Delete all existing conditions for the question
$this->deleteConditions();
if ($input['show_rule'] == PluginFormcreatorCondition::SHOW_RULE_ALWAYS) {
if ($this->fields['show_rule'] == PluginFormcreatorCondition::SHOW_RULE_ALWAYS) {
// No condition ? Exit now !
return true;
}

$input = $input['_conditions'];

// Arrays all have the same count and have at least one item
$questionFk = PluginFormcreatorQuestion::getForeignKeyField();
$order = 0;
Expand Down
7 changes: 5 additions & 2 deletions inc/question.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -739,8 +739,11 @@ public function post_purgeItem() {
}

public function showForm($ID, $options = []) {
// $options['candel'] = false;
// $options['formoptions'] = sprintf('data-itemtype="%s"', self::getType());
// TemplateRenderer::getInstance()->display('@formcreator/pages/question.html.twig', [
// 'item' => $this,
// 'params' => $options,
// ]);
// return true;

Expand All @@ -753,9 +756,9 @@ public function showForm($ID, $options = []) {
}

$rand = mt_rand();
echo '<form name="form"'
echo '<form name="asset_form"'
. ' method="post"'
. ' action="javascript:' . $action . '"'
. ' action="javascript:;"'
. ' data-itemtype="' . self::class . '"'
. '>';
echo '<table class="tab_cadre_fixe">';
Expand Down
1 change: 1 addition & 0 deletions inc/section.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ public function showForm($ID, $options = []) {
$this->initForm($ID, $options);
$options['candel'] = false;
$options['formoptions'] = sprintf('data-itemtype="%s"', self::getType());
$options['target'] = "javascript:;";
TemplateRenderer::getInstance()->display('@formcreator/pages/section.html.twig', [
'item' => $this,
'params' => $options,
Expand Down
Loading

0 comments on commit cf22eb0

Please sign in to comment.