/** * 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; }
/** * 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; }