Skip to content

Commit

Permalink
fix(formanswer): show/hide questions in various cases
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Nov 27, 2018
1 parent 0c75a69 commit 87cc394
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion inc/fields.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ public static function updateVisibility($input) {
$fields[$id] = PluginFormcreatorFields::getFieldInstance(
$question->fields['fieldtype'],
$question
);
);
$fields[$id]->parseAnswerValues($input);
}

Expand Down
3 changes: 3 additions & 0 deletions inc/fields/textfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ public static function getJSFields() {

public function parseAnswerValues($input) {
$key = 'formcreator_field_' . $this->fields['id'];
if (!isset($input[$key])) {
return false;
}
if (!is_string($input[$key])) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions inc/form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ public function displayUserForm() {

// Display form
$formName = 'formcreator_form' . $this->getID();
echo "<form name='" . $formName . "' method='post' role='form' enctype='multipart/form-data'
echo "<form name='form' method='post' role='form' enctype='multipart/form-data'
action='". $CFG_GLPI['root_doc'] . "/plugins/formcreator/front/form.form.php'
class='formcreator_form form_horizontal'>";
echo "<h1 class='form-title'>";
Expand Down Expand Up @@ -966,7 +966,7 @@ class='formcreator_form form_horizontal'>";
}
}
echo Html::scriptBlock('$(function() {
formcreatorShowFields($("form[name=\'' . $formName . '\']"));
formcreatorShowFields($("form[name=\'form\']"));
})');

// Show validator selector
Expand Down
28 changes: 18 additions & 10 deletions inc/formanswer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,18 @@ public function showForm($ID, $options = []) {
$questions = $DB->request($request);
$last_section = '';
$questionsCount = $questions->count();
$fields = [];
while ($question_line = $questions->next()) {
$question = new PluginFormcreatorQuestion();
$question->getFromDB($question_line['id']);
$fields[$question_line['id']] = PluginFormcreatorFields::getFieldInstance(
$question_line['fieldtype'],
$question
);
$fields[$question_line['id']]->deserializeValue($question_line['answer']);
}
$questions->rewind();
while ($question_line = $questions->current()) {
// Get and display current section if needed
if ($last_section != $question_line['section_name']) {
echo '<h2>'.$question_line['section_name'].'</h2>';
Expand All @@ -548,19 +559,14 @@ public function showForm($ID, $options = []) {
|| ($question_line['fieldtype'] != "description"
&& $question_line['fieldtype'] != "hidden")
) {
$question = new PluginFormcreatorQuestion();
$question->getFromDB($question_line['id']);
$field = PluginFormcreatorFields::getFieldInstance(
$question_line['fieldtype'],
$question
);
$field->deserializeValue($question_line['answer']);
$field->show($canEdit);
// if (PluginFormcreatorFields::isVisible($question_line['id'], $fields)) {
// }
$fields[$question_line['id']]->show($canEdit);
}
$questions->next();
}
$formName = 'formcreator_form' . $formId;
echo Html::scriptBlock('$(function() {
formcreatorShowFields($("form[name=\'' . $formName . '\']"));
formcreatorShowFields($("form[name=\'form\']"));
})');

//add requester info
Expand Down Expand Up @@ -599,6 +605,8 @@ public function showForm($ID, $options = []) {
echo '<input type="submit" name="accept_formanswer" class="submit_button" value="' . __('Accept', 'formcreator') . '" />';
echo '</div>';
echo '</div>';
$options['canedit'] = true;
$options['candel'] = false;
}
}

Expand Down
2 changes: 1 addition & 1 deletion js/scripts.js.php
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ function deleteTarget(items_id, token, target_id) {

function formcreatorShowFields(form) {
$.ajax({
url: '../ajax/showfields.php',
url: rootDoc + '/plugins/formcreator/ajax/showfields.php',
type: "POST",
data: form.serializeArray()
}).done(function(response){
Expand Down

0 comments on commit 87cc394

Please sign in to comment.