public function isValid($value) { // If the field is not visible, don't check it's value if (!PluginFormcreatorFields::isVisible($this->fields['id'], $this->fields['answer'])) { return true; } // If the field is required it can't be empty if ($this->isRequired() && (empty($_FILES['formcreator_field_' . $this->fields['id']]['tmp_name']) || !is_file($_FILES['formcreator_field_' . $this->fields['id']]['tmp_name']))) { Session::addMessageAfterRedirect(__('A required file is missing:', 'formcreator') . ' ' . $this->fields['name'], false, ERROR); return false; } // All is OK return true; }
public function isValid($value) { // If the field is not visible, don't check it's value if (!PluginFormcreatorFields::isVisible($this->fields['id'], $this->fields['answer'])) { return true; } // If the field is required it can't be empty if ($this->isRequired() && strtotime($value) == '') { Session::addMessageAfterRedirect(__('A required field is empty:', 'formcreator') . ' ' . $this->getLabel(), false, ERROR); return false; } // All is OK return true; }
/** * Get entire form to be inserted into a target content * * @return String Full form questions and answers to be print */ public function getFullForm() { $question_no = 0; $output = ''; if ($GLOBALS['CFG_GLPI']['use_rich_text']) { $output .= '<h1>' . __('Form data', 'formcreator') . '</h1>'; } else { $output .= __('Form data', 'formcreator') . PHP_EOL; $output .= '================='; $output .= PHP_EOL . PHP_EOL; } $section_class = new PluginFormcreatorSection(); $find_sections = $section_class->find('plugin_formcreator_forms_id = ' . $this->fields['plugin_formcreator_forms_id'], '`order` ASC'); $answer = new PluginFormcreatorAnswer(); $answers = $answer->find('`plugin_formcreator_formanwers_id` = ' . $this->getID()); $answers_values = array(); foreach ($answers as $found_answer) { $answers_values[$found_answer['plugin_formcreator_question_id']] = $found_answer['answer']; } foreach ($find_sections as $section_line) { if ($GLOBALS['CFG_GLPI']['use_rich_text']) { $output .= '<h2>' . $section_line['name'] . '</h2>'; } else { $output .= PHP_EOL . $section_line['name'] . PHP_EOL; $output .= '---------------------------------'; $output .= PHP_EOL; } // Display all fields of the section $question = new PluginFormcreatorQuestion(); $questions = $question->find('plugin_formcreator_sections_id = ' . $section_line['id'], '`order` ASC'); foreach ($questions as $question_line) { $id = $question_line['id']; $name = $question_line['name']; $found = $answer->find('`plugin_formcreator_formanwers_id` = ' . $this->getID() . ' AND `plugin_formcreator_question_id` = ' . $id); if (!PluginFormcreatorFields::isVisible($question_line['id'], $answers_values)) { continue; } if ($question_line['fieldtype'] != 'file' && $question_line['fieldtype'] != 'description') { $question_no++; if (count($found)) { $datas = array_shift($found); $value = $datas['answer']; } else { $value = ''; } $output_value = PluginFormcreatorFields::getValue($question_line, $value); if (in_array($question_line['fieldtype'], array('checkboxes', 'multiselect'))) { if (is_array($value)) { $output_value = PHP_EOL . " - " . implode(PHP_EOL . " - ", $value); } elseif (is_array(json_decode($value))) { $output_value = PHP_EOL . " - " . implode(PHP_EOL . " - ", json_decode($value)); } else { $output_value = $value; } } elseif ($question_line['fieldtype'] == 'textarea') { if ($GLOBALS['CFG_GLPI']['use_rich_text']) { $output_value = '<br /><blockquote>' . $value . '</blockquote>'; } else { $output_value = PHP_EOL . $value; } } if ($GLOBALS['CFG_GLPI']['use_rich_text']) { $output .= '<div>'; $output .= '<b>' . $question_no . ') ' . $question_line['name'] . ' : </b>'; $output .= $output_value; $output .= '</div>'; } else { $output .= $question_no . ') ' . $question_line['name'] . ' : '; $output .= $output_value . PHP_EOL . PHP_EOL; } } } } return $output; }
</td> </tr> </table> <script type="text/javascript"> function changeQuestionType() { var value = document.getElementById('dropdown_fieldtype<?php echo $rand; ?> ').value; if(value != "") { var tab_fields_fields = []; <?php PluginFormcreatorFields::printAllTabFieldsForJS(); ?> eval(tab_fields_fields[value]); } else { showFields(0, 0, 0, 0, 0, 0, 0, 0, 0, 0); } } changeQuestionType(); function showFields(required, default_values, values, range, show_empty, regex, show_type, dropdown_value, glpi_object, ldap_values) { if(required) { document.getElementById('dropdown_required<?php echo $rand; ?> ').style.display = 'inline';
/** * Display a list of all form sections and questions * * @param CommonGLPI $item Instance of a CommonGLPI Item (The Form Item) * @param integer $tabnum Number of the current tab * @param integer $withtemplate * * @see CommonDBTM::displayTabContentForItem * * @return null Nothing, just display the list */ public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) { echo '<table class="tab_cadre_fixe">'; // Get sections $section = new PluginFormcreatorSection(); $found_sections = $section->find('plugin_formcreator_forms_id = ' . $item->getId(), '`order`'); $section_number = count($found_sections); $tab_sections = array(); $tab_questions = array(); $token = Session::getNewCSRFToken(); foreach ($found_sections as $section) { $tab_sections[] = $section['id']; echo '<tr id="section_row_' . $section['id'] . '">'; echo '<th>' . $section['name'] . '</th>'; echo '<th align="center" width="32"> </th>'; echo '<th align="center" width="32">'; if ($section['order'] != 1) { echo '<img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/pics/up2.png" alt="*" title="' . __('Edit') . '" onclick="moveSection(\'' . $token . '\', ' . $section['id'] . ', \'up\');" align="absmiddle" style="cursor: pointer" /> '; } else { echo ' '; } echo '</th>'; echo '<th align="center" width="32">'; if ($section['order'] != $section_number) { echo '<img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/pics/down2.png" alt="*" title="' . __('Edit') . '" onclick="moveSection(\'' . $token . '\', ' . $section['id'] . ', \'down\');" align="absmiddle" style="cursor: pointer" /> '; } else { echo ' '; } echo '</th>'; echo '<th align="center" width="32">'; echo '<img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/pics/pencil.png" alt="*" title="' . __('Edit') . '" onclick="editSection(' . $item->getId() . ', \'' . $token . '\', ' . $section['id'] . ')" align="absmiddle" style="cursor: pointer" /> '; echo '</th>'; echo '<th align="center" width="32">'; echo '<img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/pics/delete.png" alt="*" title="' . __('Delete', 'formcreator') . '" onclick="deleteSection(' . $item->getId() . ', \'' . $token . '\', ' . $section['id'] . ', \'' . addslashes($section['name']) . '\')" align="absmiddle" style="cursor: pointer" /> '; echo '</th>'; echo '</tr>'; // Get questions $question = new PluginFormcreatorQuestion(); $found_questions = $question->find('plugin_formcreator_sections_id = ' . $section['id'], '`order`'); $question_number = count($found_questions); $i = 0; foreach ($found_questions as $question) { $i++; $tab_questions[] = $question['id']; echo '<tr class="line' . $i % 2 . '" id="question_row_' . $question['id'] . '">'; echo '<td onclick="editQuestion(' . $item->getId() . ', \'' . $token . '\', ' . $question['id'] . ', ' . $section['id'] . ')" style="cursor: pointer">'; echo '<img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/pics/ui-' . $question['fieldtype'] . '-field.png" alt="" title="" /> '; echo $question['name']; echo '</td>'; echo '<td align="center">'; $question_type = $question['fieldtype'] . 'Field'; $question_types = PluginFormcreatorFields::getTypes(); $classname = $question['fieldtype'] . 'Field'; $fields = $classname::getPrefs(); // avoid quote js error $question['name'] = htmlspecialchars_decode($question['name'], ENT_QUOTES); if ($fields['required'] == 0) { echo ' '; } elseif ($question['required']) { echo '<img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/pics/required.png" alt="*" title="' . __('Required', 'formcreator') . '" onclick="setRequired(\'' . $token . '\', ' . $question['id'] . ', 0)" align="absmiddle" style="cursor: pointer" /> '; } else { echo '<img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/pics/not-required.png" alt="*" title="' . __('Required', 'formcreator') . '" onclick="setRequired(\'' . $token . '\', ' . $question['id'] . ', 1)" align="absmiddle" style="cursor: pointer" /> '; } echo '</td>'; echo '<td align="center">'; if ($question['order'] != 1) { echo '<img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/pics/up.png" alt="*" title="' . __('Edit') . '" onclick="moveQuestion(\'' . $token . '\', ' . $question['id'] . ', \'up\');" align="absmiddle" style="cursor: pointer" /> '; } else { echo ' '; } echo '</td>'; echo '<td align="center">'; if ($question['order'] != $question_number) { echo '<img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/pics/down.png" alt="*" title="' . __('Edit') . '" onclick="moveQuestion(\'' . $token . '\', ' . $question['id'] . ', \'down\');" align="absmiddle" style="cursor: pointer" /> '; } else { echo ' '; } echo '</td>'; echo '<td align="center">'; echo '<img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/pics/pencil.png" alt="*" title="' . __('Edit') . '" onclick="editQuestion(' . $item->getId() . ', \'' . $token . '\', ' . $question['id'] . ', ' . $section['id'] . ')" align="absmiddle" style="cursor: pointer" /> '; echo '</td>'; echo '<td align="center">'; echo '<img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/pics/delete.png" alt="*" title="' . __('Delete', 'formcreator') . '" onclick="deleteQuestion(' . $item->getId() . ', \'' . $token . '\', ' . $question['id'] . ', \'' . addslashes($question['name']) . '\')" align="absmiddle" style="cursor: pointer" /> '; echo '</td>'; echo '</tr>'; } echo '<tr class="line' . ($i + 1) % 2 . '">'; echo '<td colspan="6" id="add_question_td_' . $section['id'] . '" class="add_question_tds">'; echo '<a href="javascript:addQuestion(' . $item->getId() . ', \'' . $token . '\', ' . $section['id'] . ');"> <img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/pics/menu_add.png" alt="+" align="absmiddle" /> ' . __('Add a question', 'formcreator') . ' </a>'; echo '</td>'; echo '</tr>'; } echo '<tr class="line1">'; echo '<th colspan="6" id="add_section_th">'; echo '<a href="javascript:addSection(' . $item->getId() . ', \'' . $token . '\');"> <img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/pics/menu_add.png" alt="+" align="absmiddle" /> ' . __('Add a section', 'formcreator') . ' </a>'; echo '</th>'; echo '</tr>'; echo "</table>"; $js_tab_sections = ""; $js_tab_questions = ""; $js_line_questions = ""; foreach ($tab_sections as $key) { $js_tab_sections .= "tab_sections[{$key}] = document.getElementById('section_row_{$key}').innerHTML;" . PHP_EOL; $js_tab_questions .= "tab_questions[{$key}] = document.getElementById('add_question_td_{$key}').innerHTML;" . PHP_EOL; } foreach ($tab_questions as $key) { $js_line_questions .= "line_questions[{$key}] = document.getElementById('question_row_{$key}').innerHTML;" . PHP_EOL; } }
public function isRequired() { $is_visible = PluginFormcreatorFields::isVisible($this->fields['id'], $this->fields['answer']); return $is_visible && $this->fields['required']; }
private function parseTags($content, $form, $input) { $content = str_replace('##FULLFORM##', $form->getFullForm($input), $content); $section = new PluginFormcreatorSection(); $founded = $section->find('plugin_formcreator_forms_id = ' . $form->getID(), '`order` ASC'); $tab_section = array(); foreach ($founded as $section_item) { $tab_section[] = $section_item['id']; } if (!empty($tab_section)) { $question = new PluginFormcreatorQuestion(); $founded = $question->find('plugin_formcreator_sections_id IN (' . implode(', ', $tab_section) . ')', '`order` ASC'); foreach ($founded as $question_line) { $id = $question_line['id']; $name = $question_line['name']; $value = isset($input['formcreator_field_' . $id]) ? $input['formcreator_field_' . $id] : ''; $value = PluginFormcreatorFields::getValue($question_line, $value); $content = str_replace('##question_' . $id . '##', $name, $content); $content = str_replace('##answer_' . $id . '##', $value, $content); } } return $content; }
/** * Display the Form end-user form to be filled * * @param CommonGLPI $item Instance of the Form to be displayed * * @return Null Nothing, just display the form */ public function displayUserForm(CommonGLPI $item) { if (isset($_SESSION['formcreator']['datas'])) { $datas = $_SESSION['formcreator']['datas']; unset($_SESSION['formcreator']['datas']); } else { $datas = null; } echo '<form name="formcreator_form' . $item->getID() . '" method="post" role="form" enctype="multipart/form-data" action="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/front/form.form.php" class="formcreator_form form_horizontal" onsubmit="return validateForm(this);">'; echo '<h1 class="form-title">' . $item->fields['name'] . '</h1>'; // Form Header if (!empty($item->fields['content'])) { echo '<div class="form_header">'; echo html_entity_decode($item->fields['content']); echo '</div>'; } // Get and display sections of the form $question = new PluginFormcreatorQuestion(); $questions = array(); $section_class = new PluginFormcreatorSection(); $find_sections = $section_class->find('plugin_formcreator_forms_id = ' . $item->getID(), '`order` ASC'); echo '<div class="form_section">'; foreach ($find_sections as $section_line) { echo '<h2>' . $section_line['name'] . '</h2>'; // Display all fields of the section $questions = $question->find('plugin_formcreator_sections_id = ' . $section_line['id'], '`order` ASC'); foreach ($questions as $question_line) { if (isset($datas[$question_line['id']])) { // multiple choice question are saved as JSON and needs to be decoded $answer = in_array($question_line['fieldtype'], array('checkboxes', 'multiselect')) ? json_decode($datas[$question_line['id']]) : $datas[$question_line['id']]; } else { $answer = null; } PluginFormcreatorFields::showField($question_line, $answer); } } echo '<script type="text/javascript">formcreatorShowFields();</script>'; // Show validator selector if ($item->fields['validation_required']) { $validators = array(); $tab_users = array(); $subquery = 'SELECT u.`id` FROM `glpi_users` u LEFT JOIN `glpi_plugin_formcreator_formvalidators` fv ON fv.`users_id` = u.`id` WHERE fv.`forms_id` = "' . $this->getID() . '"'; $result = $GLOBALS['DB']->query($subquery); if ($GLOBALS['DB']->numrows($result) == 0) { $subentities = getSonsOf('glpi_entities', $this->fields["entities_id"]); $subentities = implode(',', $subentities); $query = "SELECT u.`id`\n FROM `glpi_users` u\n INNER JOIN `glpi_profiles_users` pu ON u.`id` = pu.`users_id`\n INNER JOIN `glpi_profiles` p ON p.`id` = pu.`profiles_id`\n INNER JOIN `glpi_profilerights` pr ON p.`id` = pr.`profiles_id`\n WHERE pr.`name` = 'ticketvalidation'\n AND (pr.`rights` & " . TicketValidation::VALIDATEREQUEST . " = " . TicketValidation::VALIDATEREQUEST . "\n OR pr.`rights` & " . TicketValidation::VALIDATEINCIDENT . " = " . TicketValidation::VALIDATEINCIDENT . ")\n AND (pu.`entities_id` = {$this->fields["entities_id"]}\n OR (pu.`is_recursive` = 1 AND pu.entities_id IN ({$subentities})))\n GROUP BY u.`id`\n ORDER BY u.`name`"; $result = $GLOBALS['DB']->query($query); } echo '<div class="form-group required liste line' . (count($questions) + 1) % 2 . '" id="form-validator">'; echo '<label>' . __('Choose a validator', 'formcreator') . ' <span class="red">*</span></label>'; echo '<select name="formcreator_validator" id="formcreator_validator" class="required">'; echo '<option value="">---</option>'; while ($user = $GLOBALS['DB']->fetch_assoc($result)) { $userItem = new User(); $userItem->getFromDB($user['id']); echo '<option value="' . $user['id'] . '">' . $userItem->getname() . '</option>'; } echo '</select>'; echo '<script type="text/javascript" src="../scripts/combobox.js.php"></script>'; echo '</div>'; } echo '</div>'; // Display submit button echo '<div class="center">'; echo '<input type="submit" name="submit_formcreator" class="submit_button" value="' . __('Send') . '" />'; echo '</div>'; echo '<input type="hidden" name="formcreator_form" value="' . $item->getID() . '">'; echo '<input type="hidden" name="_glpi_csrf_token" value="' . Session::getNewCSRFToken() . '">'; echo '</form>'; }
/** * Parse target content to replace TAGS like ##FULLFORM## by the values * * @param String $content String to be parsed * @param PluginFormcreatorFormanswer $formanswer Formanswer object where answers are stored * @return String Parsed string with tags replaced by form values */ private function parseTags($content, PluginFormcreatorFormanswer $formanswer) { $content = str_replace('##FULLFORM##', $formanswer->getFullForm(), $content); $section = new PluginFormcreatorSection(); $found = $section->find('plugin_formcreator_forms_id = ' . (int) $formanswer->fields['plugin_formcreator_forms_id'], '`order` ASC'); $tab_section = array(); foreach ($found as $section_item) { $tab_section[] = $section_item['id']; } if (!empty($tab_section)) { $question = new PluginFormcreatorQuestion(); $found = $question->find('plugin_formcreator_sections_id IN (' . implode(', ', $tab_section) . ')', '`order` ASC'); foreach ($found as $question_line) { $id = $question_line['id']; $name = $question_line['name']; $answer = new PluginFormcreatorAnswer(); $found = $answer->find('`plugin_formcreator_formanwers_id` = ' . (int) $formanswer->getID() . ' AND `plugin_formcreator_question_id` = ' . (int) $id); if (count($found)) { $datas = array_shift($found); $value = $datas['answer']; } else { $value = ''; } $value = PluginFormcreatorFields::getValue($question_line, $value); if (is_array($value)) { if ($GLOBALS['CFG_GLPI']['use_rich_text']) { $value = '<br />' . implode('<br />', $value); } else { $value = "\r\n" . implode("\r\n", $value); } } $content = str_replace('##question_' . $id . '##', $name, $content); $content = str_replace('##answer_' . $id . '##', $value, $content); } } return $content; }
<?php include '../../../inc/includes.php'; $currentValues = json_decode(stripslashes($_POST['values']), true); foreach ($currentValues as &$value) { if (is_array($value)) { foreach ($value as &$sub_value) { $sub_value = plugin_formcreator_encode($sub_value); } } elseif (is_array(json_decode($value))) { $tab = json_decode($value); foreach ($tab as &$sub_value) { $sub_value = plugin_formcreator_encode($sub_value); } $value = json_encode($tab); } else { $value = plugin_formcreator_encode($value); } } $questionToShow = array(); foreach ($currentValues as $id => $value) { $questionToShow[$id] = PluginFormcreatorFields::isVisible($id, $currentValues); } echo json_encode($questionToShow);
/** * Display the Form end-user form to be filled * * @param CommonGLPI $item Instance of the Form to be displayed * * @return Null Nothing, just display the form */ public function displayUserForm(CommonGLPI $item) { if (isset($_SESSION['formcreator']['datas'])) { $datas = $_SESSION['formcreator']['datas']; unset($_SESSION['formcreator']['datas']); } else { $datas = null; } echo '<form name="formcreator_form' . $item->getID() . '" method="post" role="form" enctype="multipart/form-data" action="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/front/form.form.php" class="formcreator_form form_horizontal" onsubmit="return validateForm(this);">'; echo '<h1 class="form-title">' . $item->fields['name'] . '</h1>'; // Form Header if (!empty($item->fields['content'])) { echo '<div class="form_header">'; echo html_entity_decode($item->fields['content']); echo '</div>'; } // Get and display sections of the form $question = new PluginFormcreatorQuestion(); $questions = array(); $section_class = new PluginFormcreatorSection(); $find_sections = $section_class->find('plugin_formcreator_forms_id = ' . (int) $item->getID(), '`order` ASC'); echo '<div class="form_section">'; foreach ($find_sections as $section_line) { echo '<h2>' . $section_line['name'] . '</h2>'; // Display all fields of the section $questions = $question->find('plugin_formcreator_sections_id = ' . (int) $section_line['id'], '`order` ASC'); foreach ($questions as $question_line) { if (isset($datas[$question_line['id']])) { // multiple choice question are saved as JSON and needs to be decoded $answer = in_array($question_line['fieldtype'], array('checkboxes', 'multiselect')) ? json_decode($datas[$question_line['id']]) : $datas[$question_line['id']]; } else { $answer = null; } PluginFormcreatorFields::showField($question_line, $answer); } } echo '<script type="text/javascript">formcreatorShowFields();</script>'; // Show validator selector if ($item->fields['validation_required'] > 0) { $validators = array(0 => Dropdown::EMPTY_VALUE); // Groups if ($item->fields['validation_required'] == 2) { $query = 'SELECT g.`id`, g.`completename` FROM `glpi_groups` g LEFT JOIN `glpi_plugin_formcreator_formvalidators` fv ON fv.`users_id` = g.`id` WHERE fv.`forms_id` = ' . (int) $this->getID(); Toolbox::logDebug($query); $result = $GLOBALS['DB']->query($query); while ($validator = $GLOBALS['DB']->fetch_assoc($result)) { $validators[$validator['id']] = $validator['completename']; } // Users } else { $query = 'SELECT u.`id`, u.`name`, u.`realname`, u.`firstname` FROM `glpi_users` u LEFT JOIN `glpi_plugin_formcreator_formvalidators` fv ON fv.`users_id` = u.`id` WHERE fv.`forms_id` = ' . (int) $this->getID(); $result = $GLOBALS['DB']->query($query); while ($validator = $GLOBALS['DB']->fetch_assoc($result)) { $validators[$validator['id']] = formatUserName($validator['id'], $validator['name'], $validator['realname'], $validator['firstname']); } } echo '<div class="form-group required liste line' . (count($questions) + 1) % 2 . '" id="form-validator">'; echo '<label>' . __('Choose a validator', 'formcreator') . ' <span class="red">*</span></label>'; Dropdown::showFromArray('formcreator_validator', $validators); echo '</div>'; } echo '</div>'; // Display submit button echo '<div class="center">'; echo '<input type="submit" name="submit_formcreator" class="submit_button" value="' . __('Send') . '" />'; echo '</div>'; echo '<input type="hidden" name="formcreator_form" value="' . $item->getID() . '">'; echo '<input type="hidden" name="_glpi_csrf_token" value="' . Session::getNewCSRFToken() . '">'; echo '</form>'; }
public function getFullForm($datas) { $question_no = 0; $output = mb_strtoupper(__('Form data', 'formcreator'), 'UTF-8') . PHP_EOL; $output .= '================='; $output .= PHP_EOL . PHP_EOL; $section_class = new PluginFormcreatorSection(); $find_sections = $section_class->find('plugin_formcreator_forms_id = ' . $this->getID(), '`order` ASC'); foreach ($find_sections as $section_line) { $output .= $section_line['name'] . PHP_EOL; $output .= '---------------------------------'; $output .= PHP_EOL . PHP_EOL; // Display all fields of the section $question = new PluginFormcreatorQuestion(); $questions = $question->find('plugin_formcreator_sections_id = ' . $section_line['id'], '`order` ASC'); foreach ($questions as $question_line) { if ($question_line['fieldtype'] != 'file' && $question_line['fieldtype'] != 'description') { $question_no++; $id = $question_line['id']; $name = $question_line['name']; $value = isset($datas['formcreator_field_' . $id]) ? $datas['formcreator_field_' . $id] : ''; $value = PluginFormcreatorFields::getValue($question_line, $value); $output .= $question_no . ') ' . $question_line['name'] . ' : '; $output .= $value . PHP_EOL . PHP_EOL; } } } return $output; }
/** * Display a list of all form sections and questions * * @param CommonGLPI $item Instance of a CommonGLPI Item (The Form Item) * @param integer $tabnum Number of the current tab * @param integer $withtemplate * * @see CommonDBTM::displayTabContentForItem * * @return null Nothing, just display the list */ public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) { echo '<table class="tab_cadre_fixe">'; // Get sections $section = new PluginFormcreatorSection(); $founded_sections = $section->find('plugin_formcreator_forms_id = ' . $item->getId(), '`order`'); $section_number = count($founded_sections); $tab_sections = array(); $tab_questions = array(); foreach ($founded_sections as $section) { $tab_sections[] = $section['id']; echo '<tr id="section_row_' . $section['id'] . '">'; echo '<th>' . $section['name'] . '</th>'; echo '<th align="center" width="32"> </th>'; echo '<th align="center" width="32">'; if ($section['order'] != 1) { echo '<img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/pics/up2.png" alt="*" title="' . __('Edit') . '" onclick="moveSection(' . $section['id'] . ', \'up\');" align="absmiddle" style="cursor: pointer" /> '; } else { echo ' '; } echo '</th>'; echo '<th align="center" width="32">'; if ($section['order'] != $section_number) { echo '<img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/pics/down2.png" alt="*" title="' . __('Edit') . '" onclick="moveSection(' . $section['id'] . ', \'down\');" align="absmiddle" style="cursor: pointer" /> '; } else { echo ' '; } echo '</th>'; echo '<th align="center" width="32">'; echo '<img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/pics/pencil.png" alt="*" title="' . __('Edit') . '" onclick="editSection(' . $section['id'] . ')" align="absmiddle" style="cursor: pointer" /> '; echo '</th>'; echo '<th align="center" width="32">'; echo '<img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/pics/delete.png" alt="*" title="' . __('Delete', 'formcreator') . '" onclick="deleteSection(' . $section['id'] . ', \'' . addslashes($section['name']) . '\')" align="absmiddle" style="cursor: pointer" /> '; echo '</th>'; echo '</tr>'; // Get questions $question = new PluginFormcreatorQuestion(); $founded_questions = $question->find('plugin_formcreator_sections_id = ' . $section['id'], '`order`'); $question_number = count($founded_questions); $i = 0; foreach ($founded_questions as $question) { $i++; $tab_questions[] = $question['id']; echo '<tr class="line' . $i % 2 . '" id="question_row_' . $question['id'] . '">'; echo '<td onclick="editQuestion(' . $question['id'] . ', ' . $section['id'] . ')" style="cursor: pointer">'; echo '<img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/pics/ui-' . $question['fieldtype'] . '-field.png" alt="" title="" /> '; echo $question['name']; echo '</td>'; echo '<td align="center">'; $question_type = $question['fieldtype'] . 'Field'; $question_types = PluginFormcreatorFields::getTypes(); $classname = $question['fieldtype'] . 'Field'; $fields = $classname::getPrefs(); if ($fields['required'] == 0) { echo ' '; } elseif ($question['required']) { echo '<img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/pics/required.png" alt="*" title="' . __('Required', 'formcreator') . '" onclick="setRequired(' . $question['id'] . ', 0)" align="absmiddle" style="cursor: pointer" /> '; } else { echo '<img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/pics/not-required.png" alt="*" title="' . __('Required', 'formcreator') . '" onclick="setRequired(' . $question['id'] . ', 1)" align="absmiddle" style="cursor: pointer" /> '; } echo '</td>'; echo '<td align="center">'; if ($question['order'] != 1) { echo '<img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/pics/up.png" alt="*" title="' . __('Edit') . '" onclick="moveQuestion(' . $question['id'] . ', \'up\');" align="absmiddle" style="cursor: pointer" /> '; } else { echo ' '; } echo '</td>'; echo '<td align="center">'; if ($question['order'] != $question_number) { echo '<img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/pics/down.png" alt="*" title="' . __('Edit') . '" onclick="moveQuestion(' . $question['id'] . ', \'down\');" align="absmiddle" style="cursor: pointer" /> '; } else { echo ' '; } echo '</td>'; echo '<td align="center">'; echo '<img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/pics/pencil.png" alt="*" title="' . __('Edit') . '" onclick="editQuestion(' . $question['id'] . ', ' . $section['id'] . ')" align="absmiddle" style="cursor: pointer" /> '; echo '</td>'; echo '<td align="center">'; echo '<img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/pics/delete.png" alt="*" title="' . __('Delete', 'formcreator') . '" onclick="deleteQuestion(' . $question['id'] . ', \'' . addslashes($question['name']) . '\')" align="absmiddle" style="cursor: pointer" /> '; echo '</td>'; echo '</tr>'; } echo '<tr class="line' . ($i + 1) % 2 . '">'; echo '<td colspan="6" id="add_question_td_' . $section['id'] . '" class="add_question_tds">'; echo '<a href="javascript:addQuestion(' . $section['id'] . ');"> <img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/pics/menu_add.png" alt="+" align="absmiddle" /> ' . __('Add a question', 'formcreator') . ' </a>'; echo '</td>'; echo '</tr>'; } echo '<tr class="line1">'; echo '<th colspan="6" id="add_section_th">'; echo '<a href="javascript:addSection();" class="submit"> <img src="' . $GLOBALS['CFG_GLPI']['root_doc'] . '/pics/menu_add.png" alt="+" align="absmiddle" /> ' . __('Add a section', 'formcreator') . ' </a>'; echo '</th>'; echo '</tr>'; echo "</table>"; $js_tab_sections = ""; $js_tab_questions = ""; $js_line_questions = ""; foreach ($tab_sections as $key) { $js_tab_sections .= "tab_sections[{$key}] = document.getElementById('section_row_{$key}').innerHTML;" . PHP_EOL; $js_tab_questions .= "tab_questions[{$key}] = document.getElementById('add_question_td_{$key}').innerHTML;" . PHP_EOL; } foreach ($tab_questions as $key) { $js_line_questions .= "line_questions[{$key}] = document.getElementById('question_row_{$key}').innerHTML;" . PHP_EOL; } echo '<script type="text/javascript"> var modalWindow = new Ext.Window({ layout: "fit", width: "964", height: "600", closeAction: "hide", modal: "true", autoScroll: true, y: 500 }); // === QUESTIONS === var tab_questions = []; ' . $js_tab_questions . ' var line_questions = []; ' . $js_line_questions . ' function addQuestion(section) { modalWindow.show(); modalWindow.load({ url: "' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/ajax/question.php", params: { section_id: section, form_id: ' . $item->getId() . ', _glpi_csrf_token: "' . Session::getNewCSRFToken() . '" }, timeout: 30, scripts: true }); } function editQuestion(question, section) { modalWindow.show(); modalWindow.load({ url: "' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/ajax/question.php", params: { question_id: question, section_id: section, form_id: ' . $item->getId() . ', _glpi_csrf_token: "' . Session::getNewCSRFToken() . '" }, timeout: 30, scripts: true }); } function setRequired(question_id, value) { Ext.Ajax.request({ url: "' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/front/question.form.php", success: reloadTab, params: { set_required: 1, id: question_id, value: value, _glpi_csrf_token: "' . Session::getNewCSRFToken() . '" } }); } function moveQuestion(question_id, way) { Ext.Ajax.request({ url: "' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/front/question.form.php", success: reloadTab, params: { move: 1, id: question_id, way: way, _glpi_csrf_token: "' . Session::getNewCSRFToken() . '" } }); } function deleteQuestion(question_id, question_name) { if(confirm("' . __('Are you sure you want to delete this question:', 'formcreator') . ' " + question_name)) { Ext.Ajax.request({ url: "' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/front/question.form.php", success: reloadTab, params: { delete: 1, id: question_id, plugin_formcreator_forms_id: ' . $item->getId() . ', _glpi_csrf_token: "' . Session::getNewCSRFToken() . '" } }); } } // === SECTIONS === var add_section_link = document.getElementById("add_section_th").innerHTML; var tab_sections = []; ' . $js_tab_sections . ' function addSection() { Ext.get("add_section_th").load({ url: "' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/ajax/section.php", scripts: true, params: "form_id=' . $item->getId() . '" }); } function editSection(section) { document.getElementById("section_row_" + section).innerHTML = "<th colspan=\\"6\\"></th>"; Ext.get("section_row_" + section + "").child("th").load({ url: "' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/ajax/section.php", scripts: true, params: "section_id=" + section + "&form_id=' . $item->getId() . '" }); } function deleteSection(section_id, section_name) { if(confirm("' . __('Are you sure you want to delete this section:', 'formcreator') . ' " + section_name)) { Ext.Ajax.request({ url: "' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/front/section.form.php", success: reloadTab, params: { delete: 1, id: section_id, plugin_formcreator_forms_id: ' . $item->getId() . ', _glpi_csrf_token: "' . Session::getNewCSRFToken() . '" } }); } } function moveSection(section_id, way) { Ext.Ajax.request({ url: "' . $GLOBALS['CFG_GLPI']['root_doc'] . '/plugins/formcreator/front/section.form.php", success: reloadTab, params: { move: 1, id: section_id, way: way, _glpi_csrf_token: "' . Session::getNewCSRFToken() . '" } }); } function resetAll() { document.getElementById("add_section_th").innerHTML = add_section_link; for (section_id in tab_sections) { if(parseInt(section_id)) { document.getElementById("section_row_" + section_id).innerHTML = tab_sections[section_id]; document.getElementById("add_question_td_" + section_id).innerHTML = tab_questions[section_id]; } } for (question_id in line_questions) { if(parseInt(question_id)) { document.getElementById("question_row_" + question_id).innerHTML = line_questions[question_id]; } } } </script>'; }