Skip to content

Commit cf22eb0

Browse files
committed
fix(section,question): modals malfunctions
1 parent c676cb5 commit cf22eb0

13 files changed

+138
-71
lines changed

ajax/condition.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,13 @@
4141
http_response_code(400);
4242
die();
4343
}
44+
/** @var CommonDBTM $parent */
45+
$parent = new $_POST['itemtype'];
46+
$parent->getEmpty();
47+
$parent->fields = array_intersect_key($_POST, $parent->fields);
4448

4549
// get an empty condition HTML table row
4650
$condition = new PluginFormcreatorCondition();
4751
$condition->fields['itemtype'] = $_POST['itemtype'];
4852
$condition->fields['items_id'] = $_POST['items_id'];
49-
echo $condition->getConditionHtml();
53+
echo $condition->getConditionHtml($parent);

ajax/question_add.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@
5151
'height' => '1',
5252
'html' => $question->getDesignHtml(),
5353
];
54-
echo json_encode($json);
54+
echo json_encode($json, JSON_UNESCAPED_UNICODE);

ajax/question_update.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,28 @@
3333
Session::checkRight('entity', UPDATE);
3434

3535
if (!isset($_REQUEST['id'])) {
36-
echo __('Bad request', 'formcreator');
36+
Session::addMessageAfterRedirect(__('Bad request', 'formcreator'), false, ERROR);
3737
http_response_code(400);
3838
exit();
3939
}
4040
$questionId = (int) $_REQUEST['id'];
4141

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

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

5555
$success = $question->update($_REQUEST);
5656
if (!$success) {
57-
http_response_code(500);
58-
exit();
57+
http_response_code(500);
58+
exit();
5959
}
60-
echo $question->fields['name'];
60+
echo json_encode(['name' => $question->fields['name']], JSON_UNESCAPED_UNICODE);

ajax/section_add.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@
3535
$section = new PluginFormcreatorSection();
3636
if (!$section->canCreate()) {
3737
http_response_code(403);
38-
echo __('You don\'t have right for this action', 'formcreator');
38+
Session::addMessageAfterRedirect(__('You don\'t have right for this action', 'formcreator'), false, ERROR);
3939
exit;
4040
}
4141

4242
if (!$section->add($_REQUEST)) {
4343
http_response_code(500);
44-
echo __('Could not add the section', 'formcreator');
44+
Session::addMessageAfterRedirect(__('Could not add the section', 'formcreator'), false, ERROR);
4545
exit;
4646
}
47-
echo $section->getDesignHtml();
47+
echo json_encode(['id' => $section->getID(), 'html' => $section->getDesignHtml()], JSON_UNESCAPED_UNICODE);

ajax/section_update.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,20 @@
3434

3535
if (!isset($_REQUEST['id'])) {
3636
http_response_code(400);
37+
Session::addMessageAfterRedirect(__('Bad request', 'formcreator'), false, ERROR);
3738
exit;
3839
}
3940

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

4748
if (!$section->update($_REQUEST)) {
4849
http_response_code(500);
49-
echo __('Could not update the section', 'formcreator');
50+
Session::addMessageAfterRedirect(__('Could not update the section', 'formcreator'), false, ERROR);
5051
exit;
5152
}
52-
echo $section->fields['name'];
53+
echo json_encode(['id' => $section->getID(), 'name' => $section->fields['name']], JSON_UNESCAPED_UNICODE);

inc/condition.class.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ public function showConditionsForItem(PluginFormcreatorConditionnableInterface $
290290
$conditions = self::getConditionsFromItem($item);
291291
foreach ($conditions as $condition) {
292292
echo '<tr><td colspan="4">';
293-
echo $condition->getConditionHtml();
293+
echo $condition->getConditionHtml($item);
294294
echo '</td></tr>';
295295
}
296296
}
@@ -301,7 +301,11 @@ public function showConditionsForItem(PluginFormcreatorConditionnableInterface $
301301
* @param PluginFormcreatorConditionnableInterface $item
302302
* @return void
303303
*/
304-
public static function getQuestionsExclusion(PluginFormcreatorConditionnableInterface $item) {
304+
public static function getQuestionsExclusion(?PluginFormcreatorConditionnableInterface $item) {
305+
if ($item === null) {
306+
return [];
307+
}
308+
305309
/** @var CommonDBTM $item */
306310
$itemtype = $item->getType();
307311
switch ($itemtype) {
@@ -349,15 +353,16 @@ public static function getQuestionsExclusion(PluginFormcreatorConditionnableInte
349353
*
350354
* @return string HTML to insert in a rendered web page
351355
*/
352-
public function getConditionHtml(): string {
353-
$itemtype = $this->fields['itemtype'];
356+
public function getConditionHtml(CommonDBTM $parent): string {
357+
$itemtype = $parent->getType();
354358
if (!is_subclass_of($itemtype, PluginFormcreatorConditionnableInterface::class)) {
355359
// security check
356360
throw new RuntimeException("$itemtype is not a " . PluginFormcreatorConditionnableInterface::class);
357361
}
358362

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

363368
return $out;

inc/conditionnabletrait.class.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,18 @@ public function checkConditionSettings(array $input): bool {
111111
}
112112

113113
public function updateConditions($input) : bool {
114-
$input = $input['_conditions'];
115-
116114
$itemtype = $this->getType();
117115
$itemId = $this->getID();
118116

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

124+
$input = $input['_conditions'];
125+
126126
// Arrays all have the same count and have at least one item
127127
$questionFk = PluginFormcreatorQuestion::getForeignKeyField();
128128
$order = 0;

inc/question.class.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,8 +739,11 @@ public function post_purgeItem() {
739739
}
740740

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

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

755758
$rand = mt_rand();
756-
echo '<form name="form"'
759+
echo '<form name="asset_form"'
757760
. ' method="post"'
758-
. ' action="javascript:' . $action . '"'
761+
. ' action="javascript:;"'
759762
. ' data-itemtype="' . self::class . '"'
760763
. '>';
761764
echo '<table class="tab_cadre_fixe">';

inc/section.class.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ public function showForm($ID, $options = []) {
456456
$this->initForm($ID, $options);
457457
$options['candel'] = false;
458458
$options['formoptions'] = sprintf('data-itemtype="%s"', self::getType());
459+
$options['target'] = "javascript:;";
459460
TemplateRenderer::getInstance()->display('@formcreator/pages/section.html.twig', [
460461
'item' => $this,
461462
'params' => $options,

0 commit comments

Comments
 (0)