diff --git a/ajax/condition.php b/ajax/condition.php index 8ff705860..f4e581907 100644 --- a/ajax/condition.php +++ b/ajax/condition.php @@ -44,18 +44,39 @@ } $itemtype = $_REQUEST['itemtype']; -if (!isset($_REQUEST['plugin_formcreator_sections_id'])) { +if (!class_exists($itemtype)) { http_response_code(400); exit; } -$sectionId = (int) $_REQUEST['plugin_formcreator_sections_id']; -if (!class_exists($itemtype)) { +if (!is_subclass_of($itemtype, CommonDBTM::class)) { http_response_code(400); exit; } -if (!is_subclass_of($itemtype, CommonDBTM::class)) { +$form = new PluginFormcreatorForm(); +switch ($itemtype) { + case PluginFormcreatorQuestion::class: + if (!isset($_REQUEST['plugin_formcreator_sections_id'])) { + http_response_code(400); + exit; + } + $sectionId = (int) $_REQUEST['plugin_formcreator_sections_id']; + $section = new PluginFormcreatorSection(); + $section->getFromDB($sectionId); + $form->getFromDBBySection($section); + break; + case PluginFormcreatorSection::class: + if (!isset($_REQUEST['plugin_formcreator_forms_id'])) { + http_response_code(400); + exit; + } + $formId = (int) $_REQUEST['plugin_formcreator_forms_id']; + $form->getFromDB($formId); + break; +} + +if ($form->isNewItem()) { http_response_code(400); exit; } @@ -63,9 +84,5 @@ // get an empty condition HTML table row $item = new $itemtype(); $item->getFromDB($itemId); -$section = new PluginFormcreatorSection(); -$section->getFromDB($sectionId); -$form = new PluginFormcreatorForm(); -$form->getFromDBBySection($section); $condition = new PluginFormcreatorCondition(); echo $condition->getConditionHtml($form, $itemtype, $itemId); diff --git a/css/styles.css b/css/styles.css index 593dcc942..9d4219a4a 100644 --- a/css/styles.css +++ b/css/styles.css @@ -211,7 +211,7 @@ form.formcreator_form { margin: 0 0 10px; } -.formcreator_form > .form_section > h2 { +.formcreator_form .form_section > h2 { background: #1B2F62; /*#E1D39E;*/ color: #FFF; margin: 0 0 10px; diff --git a/inc/condition.class.php b/inc/condition.class.php index 1789301db..e01df5c53 100644 --- a/inc/condition.class.php +++ b/inc/condition.class.php @@ -79,6 +79,14 @@ public static function getEnumShowCondition() { ]; } + public function getEnumShowRule() { + return [ + self::SHOW_RULE_ALWAYS => __('Always displayed', 'formcreator'), + self::SHOW_RULE_HIDDEN => __('Hidden unless', 'formcreator'), + self::SHOW_RULE_SHOWN => __('Displayed unless', 'formcreator'), + ]; + } + public static function import(PluginFormcreatorLinker $linker, $input = [], $containerId = 0) { global $DB; @@ -167,34 +175,6 @@ public function export($remove_uuid = false) { return $condition; } - /** - * get show / hide conditions for a question - * - * @param int $questionId - * @return array - */ - public function getConditionsFromQuestion($questionId) { - global $DB; - - $conditions = []; - $rows = $DB->request([ - 'SELECT' => ['id'], - 'FROM' => self::getTable(), - 'WHERE' => [ - 'itemtype' => PluginFormcreatorQuestion::class, - 'items_id' => $questionId - ], - 'ORDER' => 'order ASC' - ]); - foreach ($rows as $row) { - $condition = new static(); - $condition->getFromDB($row['id']); - $conditions[] = $condition; - } - - return $conditions; - } - /** * get conditions applied to an item * @@ -235,10 +215,6 @@ public function getConditionsFromItem(PluginFormcreatorConditionnableInterface $ * @return void */ public function showConditionsForItem($form, PluginFormcreatorConditionnableInterface $item) { - $ID = 0; - if (!$item->isNewItem()) { - $ID = $item->getID(); - } $rand = mt_rand(); echo ''; echo ''; @@ -248,6 +224,7 @@ public function showConditionsForItem($form, PluginFormcreatorConditionnableInte echo ''; echo ''; + // Get conditionsexisting conditions for the item $conditions = $this->getConditionsFromItem($item); reset($conditions); $condition = array_shift($conditions); @@ -260,15 +237,15 @@ public function showConditionsForItem($form, PluginFormcreatorConditionnableInte echo ''; echo ''; - Dropdown::showFromArray('show_rule', [ - self::SHOW_RULE_ALWAYS => __('Always displayed', 'formcreator'), - self::SHOW_RULE_HIDDEN => __('Hidden unless', 'formcreator'), - self::SHOW_RULE_SHOWN => __('Displayed unless', 'formcreator'), - ], [ - 'value' => $item->fields['show_rule'], - 'on_change' => 'plugin_formcreator_toggleCondition(this);', - 'rand' => $rand, - ]); + Dropdown::showFromArray( + 'show_rule', + $this->getEnumShowRule(), + [ + 'value' => $item->fields['show_rule'], + 'on_change' => 'plugin_formcreator_toggleCondition(this, "' . get_class($item) . '");', + 'rand' => $rand, + ] + ); echo ''; echo ''; } @@ -300,15 +277,15 @@ public function getConditionHtml($form, $itemtype, $itemId = 0, $isFirst = false } $rand = mt_rand(); + // Get list of question in the form of the item if (!is_subclass_of($itemtype, PluginFormcreatorConditionnableInterface::class)) { throw new Exception("$itemtype is not a " . PluginFormcreatorConditionnableInterface::class); } - $item = new $itemtype(); $questionListCondition = []; if ($itemtype == PluginFormcreatorQuestion::class) { $questionListCondition = [PluginFormcreatorQuestion::getTable() . '.id' => ['<>', $itemId]]; } - $questionsInForm = $item->getQuestionsFromForm($form->getID(), $questionListCondition); + $questionsInForm = (new PluginFormcreatorQuestion)->getQuestionsFromForm($form->getID(), $questionListCondition); $questions_tab = []; foreach ($questionsInForm as $question) { if (strlen($question->fields['name']) > 30) { diff --git a/inc/conditionnableinterface.class.php b/inc/conditionnableinterface.class.php index 9c61e8b62..2b59dfbfc 100644 --- a/inc/conditionnableinterface.class.php +++ b/inc/conditionnableinterface.class.php @@ -31,9 +31,10 @@ interface PluginFormcreatorConditionnableInterface { - /** - * get the form of the item - * @return boolean|PluginFormcreatorForm the form or false if not found + /** + * Updates the conditions of the question + * @param array $input + * @return boolean true if success, false otherwise */ - public function getForm(); + public function updateConditions($input); } diff --git a/inc/fields.class.php b/inc/fields.class.php index c04ad1762..fc146e699 100644 --- a/inc/fields.class.php +++ b/inc/fields.class.php @@ -104,39 +104,59 @@ public static function getNames() { } /** - * Check if a question should be shown or not + * Check if an item should be shown or not * - * @param integer $question Question tested for visibility + * @param integer $item Item tested for visibility * @param array $fields Array of fields instances (question id => instance) * @return boolean If true the question should be visible */ - public static function isVisible($question, $fields) { - $questionId = $question->getID(); + public static function isVisible(PluginFormcreatorConditionnableInterface $item, $fields) { /** * Keep track of questions results and computation status - * null = is beinc computed + * null = is being avaluated * true or false = result of a previous evaluation - * not set = not evaluated yet and not being evaluated + * not set = not evaluated yet AND not being evaluated */ - static $evalQuestion = []; - if (!isset($evalQuestion[$questionId])) { - $evalQuestion[$questionId] = null; - } else if ($evalQuestion[$questionId] !== null) { - return $evalQuestion[$questionId]; + static $evalItem = []; + + $itemtype = get_class($item); + $itemId = $item->getID(); + if (!isset($evalItem[$itemtype][$itemId])) { + $evalItem[$itemtype][$itemId] = null; + } else if ($evalItem[$itemtype][$itemId] !== null) { + return $evalItem[$itemtype][$itemId]; } else { throw new Exception("Infinite loop in show conditions evaluation"); } + /** + * Get inherit visibility from parent item. + * @return boolean + */ + $getParentVisibility = function() use ($item, $itemtype, &$evalItem, $itemId, $fields) { + // Check if item has a condtionnable visibility parent + if ($item instanceof CommonDBChild) { + if (is_subclass_of($item::$itemtype, PluginFormcreatorConditionnableInterface::class)) { + if ($parent = $item->getItem(true, false)) { + // Use visibility of the parent item + $evalItem[$itemtype][$itemId] = self::isVisible($parent, $fields); + return $evalItem[$itemtype][$itemId]; + } + } + } + $evalItem[$itemtype][$itemId] = true; + return $evalItem[$itemtype][$itemId]; + }; + // If the field is always shown - if ($question->getField('show_rule') == PluginFormcreatorCondition::SHOW_RULE_ALWAYS) { - $evalQuestion[$questionId] = true; - return true; + if ($item->fields['show_rule'] == PluginFormcreatorCondition::SHOW_RULE_ALWAYS) { + return $getParentVisibility(); } - // Get conditions to show or hide field + // Get conditions to show or hide the item $conditions = []; $condition = new PluginFormcreatorCondition(); - foreach ($condition->getConditionsFromQuestion($questionId) as $condition) { + foreach ($condition->getConditionsFromItem($item) as $condition) { $conditions[] = [ 'logic' => $condition->fields['show_logic'], 'field' => $condition->fields['plugin_formcreator_questions_id'], @@ -144,11 +164,11 @@ public static function isVisible($question, $fields) { 'value' => $condition->fields['show_value'] ]; } - if (count($conditions) < 1) { - // No condition defined, then always show the question - $evalQuestion[$questionId] = true; - return true; + if ($getParentVisibility() === false || count($conditions) < 1) { + // No condition defined or parent hidden + return $evalItem[$itemtype][$itemId]; } + $evalItem[$itemtype][$itemId] = null; // Force the first logic operator to OR $conditions[0]['logic'] = 'OR'; @@ -168,76 +188,84 @@ public static function isVisible($question, $fields) { // TODO: find the best behavior if the question does not exists $conditionField = $fields[$condition['field']]; - switch ($condition['operator']) { - case PluginFormcreatorCondition::SHOW_CONDITION_NE : - if (!$conditionField->isPrerequisites()) { - return true; - } - try { - $value = self::isVisible($conditionField->getQuestion(), $fields) && $conditionField->notEquals($condition['value']); - } catch (PluginFormcreatorComparisonException $e) { - $value = false; - } - break; + $value = false; + if (self::isVisible($conditionField->getQuestion(), $fields)) { + switch ($condition['operator']) { + case PluginFormcreatorCondition::SHOW_CONDITION_NE : + if (!$conditionField->isPrerequisites()) { + $evalItem[$itemtype][$itemId] = true; + return $evalItem[$itemtype][$itemId]; + } + try { + $value = $conditionField->notEquals($condition['value']); + } catch (PluginFormcreatorComparisonException $e) { + $value = false; + } + break; - case PluginFormcreatorCondition::SHOW_CONDITION_EQ : - if (!$conditionField->isPrerequisites()) { - return false; - } - try { - $value = self::isVisible($conditionField->getQuestion(), $fields) && $conditionField->equals($condition['value']); - } catch (PluginFormcreatorComparisonException $e) { - $value = false; - } - break; + case PluginFormcreatorCondition::SHOW_CONDITION_EQ : + if (!$conditionField->isPrerequisites()) { + $evalItem[$itemtype][$itemId] = false; + return $evalItem[$itemtype][$itemId]; + } + try { + $value = $conditionField->equals($condition['value']); + } catch (PluginFormcreatorComparisonException $e) { + $value = false; + } + break; - case PluginFormcreatorCondition::SHOW_CONDITION_GT: - if (!$conditionField->isPrerequisites()) { - return false; - } - try { - $value = self::isVisible($conditionField->getQuestion(), $fields) && $conditionField->greaterThan($condition['value']); - } catch (PluginFormcreatorComparisonException $e) { - $value = false; - } - break; + case PluginFormcreatorCondition::SHOW_CONDITION_GT: + if (!$conditionField->isPrerequisites()) { + $evalItem[$itemtype][$itemId] = false; + return $evalItem[$itemtype][$itemId]; + } + try { + $value = $conditionField->greaterThan($condition['value']); + } catch (PluginFormcreatorComparisonException $e) { + $value = false; + } + break; - case PluginFormcreatorCondition::SHOW_CONDITION_LT: - if (!$conditionField->isPrerequisites()) { - return false; - } - try { - $value = self::isVisible($conditionField->getQuestion(), $fields) && $conditionField->lessThan($condition['value']); - } catch (PluginFormcreatorComparisonException $e) { - $value = false; - } - break; + case PluginFormcreatorCondition::SHOW_CONDITION_LT: + if (!$conditionField->isPrerequisites()) { + $evalItem[$itemtype][$itemId] = false; + return $evalItem[$itemtype][$itemId]; + } + try { + $value = $conditionField->lessThan($condition['value']); + } catch (PluginFormcreatorComparisonException $e) { + $value = false; + } + break; - case PluginFormcreatorCondition::SHOW_CONDITION_GE: - if (!$conditionField->isPrerequisites()) { - return false; - } - try { - $value = self::isVisible($conditionField->getQuestion(), $fields) && ($conditionField->greaterThan($condition['value']) - || $conditionField->equals($condition['value'])); - } catch (PluginFormcreatorComparisonException $e) { - $value = false; - } - break; + case PluginFormcreatorCondition::SHOW_CONDITION_GE: + if (!$conditionField->isPrerequisites()) { + $evalItem[$itemtype][$itemId] = false; + return $evalItem[$itemtype][$itemId]; + } + try { + $value = ($conditionField->greaterThan($condition['value']) + || $conditionField->equals($condition['value'])); + } catch (PluginFormcreatorComparisonException $e) { + $value = false; + } + break; - case PluginFormcreatorCondition::SHOW_CONDITION_LE: - if (!$conditionField->isPrerequisites()) { - return false; - } - try { - $value = self::isVisible($conditionField->getQuestion(), $fields) && ($conditionField->lessThan($condition['value']) - || $conditionField->equals($condition['value'])); - } catch (PluginFormcreatorComparisonException $e) { - $value = false; - } - break; + case PluginFormcreatorCondition::SHOW_CONDITION_LE: + if (!$conditionField->isPrerequisites()) { + $evalItem[$itemtype][$itemId] = false; + return $evalItem[$itemtype][$itemId]; + } + try { + $value = ($conditionField->lessThan($condition['value']) + || $conditionField->equals($condition['value'])); + } catch (PluginFormcreatorComparisonException $e) { + $value = false; + } + break; + } } - // Combine all condition with respect of operator precedence // AND has precedence over OR and XOR if ($currentLogic != PluginFormcreatorCondition::SHOW_LOGIC_AND && $nextLogic == PluginFormcreatorCondition::SHOW_LOGIC_AND) { @@ -277,15 +305,15 @@ public static function isVisible($question, $fields) { $return = ($return xor $lowPrecedenceReturnPart); } - if ($question->fields['show_rule'] == PluginFormcreatorCondition::SHOW_RULE_HIDDEN) { + if ($item->fields['show_rule'] == PluginFormcreatorCondition::SHOW_RULE_HIDDEN) { // If the field is hidden by default, show it if condition is true - $evalQuestion[$questionId] = $return; + $evalItem[$itemtype][$itemId] = $return; } else { // else show it if condition is false - $evalQuestion[$questionId] = !$return; + $evalItem[$itemtype][$itemId] = !$return; } - return $evalQuestion[$questionId]; + return $evalItem[$itemtype][$itemId]; } /** @@ -303,12 +331,23 @@ public static function updateVisibility($input) { $fields[$id]->parseAnswerValues($input, true); } + // Get the visibility result of questions $questionToShow = []; - foreach ($fields as $id => $value) { + foreach ($fields as $id => $field) { $questionToShow[$id] = PluginFormcreatorFields::isVisible($field->getQuestion(), $fields); } - return $questionToShow; + // Get the visibility result of sections + $sectionToShow = []; + $sections = (new PluginFormcreatorSection)->getSectionsFromForm($form->getID()); + foreach($sections as $section) { + $sectionToShow[$section->getID()] = PluginFormcreatorFields::isVisible($section, $fields);; + } + + return [ + PluginFormcreatorQuestion::class => $questionToShow, + PluginFormcreatorSection::class => $sectionToShow, + ]; } /** diff --git a/inc/form.class.php b/inc/form.class.php index 2b397cbfb..c30ae7cd1 100644 --- a/inc/form.class.php +++ b/inc/form.class.php @@ -1216,7 +1216,8 @@ public function displayUserForm() { echo Html::css("plugins/formcreator/css/print_form.css", ['media' => 'print']); // Display form - echo "
"; echo "

"; @@ -1233,10 +1234,9 @@ class='formcreator_form form_horizontal'>"; // Get and display sections of the form $sections = (new PluginFormcreatorSection)->getSectionsFromForm($this->getID()); - echo '
'; foreach ($sections as $section) { - echo '

' . $section->getField('name') . '

'; - + echo '
'; + echo '

' . $section->fields['name'] . '

'; // Display all fields of the section $questions = (new PluginFormcreatorQuestion())->getQuestionsFromSection($section->getID()); foreach ($questions as $question) { @@ -1254,9 +1254,10 @@ class='formcreator_form form_horizontal'>"; } $field->show(); } + echo '
'; } echo Html::scriptBlock('$(function() { - formcreatorShowFields($("form[name=\'form\']")); + formcreatorShowFields($("form[name=\'' . $formName . '\']")); })'); // Show validator selector @@ -1293,8 +1294,6 @@ class='formcreator_form form_horizontal'>"; } } - echo '
'; - // Display submit button echo '
'; echo Html::submit(__('Send'), ['name' => 'submit_formcreator']); diff --git a/inc/question.class.php b/inc/question.class.php index ff981561e..5b119fbdf 100644 --- a/inc/question.class.php +++ b/inc/question.class.php @@ -759,13 +759,11 @@ public function showForm($ID, $options = []) { $section->getFromDB($this->fields[$sectionFk]); $form = new PluginFormcreatorForm(); $form->getFromDBBySection($section); - $formId = $form->getID(); $rand = mt_rand(); - $action = static::getFormURL(); - echo ''; - + echo ''; echo ''; + echo ''; echo '
'; echo ($ID == 0) ? __('Add a question', 'formcreator') : __('Edit a question', 'formcreator'); @@ -783,7 +781,6 @@ public function showForm($ID, $options = []) { echo ''; echo ''; - echo ' 'name', 'autofocus' => '', @@ -802,7 +799,7 @@ public function showForm($ID, $options = []) { echo ''; $sections = []; - foreach ((new PluginFormcreatorSection())->getSectionsFromForm($formId) as $section) { + foreach ((new PluginFormcreatorSection())->getSectionsFromForm($form->getID()) as $section) { $sections[$section->getID()] = $section->getField('name'); } $currentSectionId = ($this->fields['plugin_formcreator_sections_id']) @@ -1039,41 +1036,6 @@ public function export($remove_uuid = false) { return $question; } - /** - * get the form belonging to the question - * - * @return boolean|PluginFormcreatorForm the form or false if not found - */ - public function getForm() { - global $DB; - - $form = new PluginFormcreatorForm(); - $iterator = $DB->request([ - 'SELECT' => $form::getForeignKeyField(), - 'FROM' => PluginFormcreatorSection::getTable(), - 'INNER JOIN' => [ - $this::getTable() => [ - 'FKEY' => [ - PluginFormcreatorSection::getTable() => PluginFormcreatorSection::getIndexName(), - $this::getTable() => PluginFormcreatorSection::getForeignKeyField() - ] - ] - ], - 'WHERE' => [ - $this::getTable() . '.' . $this::getIndexName() => $this->getID() - ] - ]); - if ($iterator->count() !== 1) { - return false; - } - $form->getFromDB($iterator->next()[$form::getForeignKeyField()]); - if ($form->isNewItem()) { - return false; - } - - return $form; - } - /** * return array of question objects belonging to a form * @param integer $formId diff --git a/inc/section.class.php b/inc/section.class.php index e8ca5b315..0f5cde520 100644 --- a/inc/section.class.php +++ b/inc/section.class.php @@ -37,7 +37,8 @@ class PluginFormcreatorSection extends CommonDBChild implements PluginFormcreatorExportableInterface, -PluginFormcreatorDuplicatableInterface +PluginFormcreatorDuplicatableInterface, +PluginFormcreatorConditionnableInterface { static public $itemtype = PluginFormcreatorForm::class; static public $items_id = 'plugin_formcreator_forms_id'; @@ -346,7 +347,7 @@ public function showForm($ID, $options = []) { } else { $title = __('Edit a section', 'formcreator'); } - echo ''; + echo ''; echo ''; echo ''; @@ -362,11 +363,17 @@ public function showForm($ID, $options = []) { echo ''; echo ''; + $form = new PluginFormcreatorForm(); + $form->getFromDBBySection($this); + $condition = new PluginFormcreatorCondition(); + $condition->showConditionsForItem($form, $this); + echo ''; echo ''; echo ''; @@ -424,4 +431,64 @@ public function post_getFromDB() { $this->fields += self::getFullData(null, $this->fields['id']); } } + + /** + * Updates the conditions of the question + * @param array $input + * @return boolean true if success, false otherwise + */ + public function updateConditions($input) { + if (!isset($input['plugin_formcreator_questions_id']) || !isset($input['show_condition']) + || !isset($input['show_value']) || !isset($input['show_logic'])) { + return false; + } + + if (!is_array($input['plugin_formcreator_questions_id']) || !is_array($input['show_condition']) + || !is_array($input['show_value']) || !is_array($input['show_logic'])) { + return false; + } + + // All arrays of condition exists + if ($input['show_rule'] == PluginFormcreatorCondition::SHOW_RULE_ALWAYS) { + return false; + } + + if (!(count($input['plugin_formcreator_questions_id']) == count($input['show_condition']) + && count($input['show_value']) == count($input['show_logic']) + && count($input['plugin_formcreator_questions_id']) == count($input['show_value']))) { + return false; + } + + // Delete all existing conditions for the question + $condition = new PluginFormcreatorCondition(); + $condition->deleteByCriteria([ + 'itemtype' => static::class, + 'items_id' => $input['id'], + ]); + + // Arrays all have the same count and have at least one item + $order = 0; + while (count($input['plugin_formcreator_questions_id']) > 0) { + $order++; + $value = array_shift($input['show_value']); + $questionID = (int) array_shift($input['plugin_formcreator_questions_id']); + $showCondition = html_entity_decode(array_shift($input['show_condition'])); + $showLogic = array_shift($input['show_logic']); + $condition = new PluginFormcreatorCondition(); + $condition->add([ + 'itemtype' => static::class, + 'items_id' => $input['id'], + 'plugin_formcreator_questions_id' => $questionID, + 'show_condition' => $showCondition, + 'show_value' => $value, + 'show_logic' => $showLogic, + 'order' => $order, + ]); + if ($condition->isNewItem()) { + return false; + } + } + + return true; + } } diff --git a/install/mysql/plugin_formcreator_empty.sql b/install/mysql/plugin_formcreator_empty.sql index 6846de93d..dbeb5a944 100644 --- a/install/mysql/plugin_formcreator_empty.sql +++ b/install/mysql/plugin_formcreator_empty.sql @@ -137,6 +137,7 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_sections` ( `name` varchar(255) NOT NULL DEFAULT '', `plugin_formcreator_forms_id` int(11) NOT NULL, `order` int(11) NOT NULL DEFAULT '0', + `show_rule` int(11) NOT NULL DEFAULT '1', `uuid` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`), INDEX `plugin_formcreator_forms_id` (`plugin_formcreator_forms_id`) diff --git a/install/upgrade_to_2.9.php b/install/upgrade_to_2.9.php index b8e2bc2ff..bbcec39c4 100644 --- a/install/upgrade_to_2.9.php +++ b/install/upgrade_to_2.9.php @@ -328,6 +328,10 @@ public function upgrade(Migration $migration) { $migration->backupTables('glpi_plugin_formcreator_questions_conditions'); } } + + // make sections hideable by condition + $table = 'glpi_plugin_formcreator_sections'; + $migration->addField($table, 'show_rule', 'integer', ['value' => '1', 'after' => 'order']); } /** diff --git a/js/scripts.js.php b/js/scripts.js.php index cc7926909..86c89208d 100644 --- a/js/scripts.js.php +++ b/js/scripts.js.php @@ -610,16 +610,29 @@ function plugin_formcreator_deleteTarget(itemtype, target_id, token) { var formcreatorQuestions = new Object(); function formcreatorShowFields(form) { + debugger; $.ajax({ url: rootDoc + '/plugins/formcreator/ajax/showfields.php', type: "POST", data: form.serializeArray() }).done(function(response){ try { - var questionToShow = JSON.parse(response); + var itemToShow = JSON.parse(response); + var questionToShow = itemToShow['PluginFormcreatorQuestion']; + var sectionToShow = itemToShow['PluginFormcreatorSection']; } catch (e) { // Do nothing for now } + for (var sectionKey in sectionToShow) { + var sectionId = parseInt(sectionKey); + if (!isNaN(sectionId)) { + if (sectionToShow[sectionId]) { + $('div[data-section-id="' + sectionId+ '"]').show(); + } else { + $('div[data-section-id="' + sectionId+ '"]').hide(); + } + } + } var i = 0; for (var questionKey in questionToShow) { var questionId = questionKey; @@ -757,15 +770,30 @@ function plugin_formcreator_ChangeActorAssigned(value) { // === FIELDS EDITION === -function plugin_formcreator_addEmptyCondition(target) { - var questionId = $('form[name="form_question"] input[name="id"]').val(); +function plugin_formcreator_addEmptyCondition(target, itemtype) { + var itemId = $('form[name="plugin_formcreator_form"] input[name="id"]').val(); + var parentKey; + var parentId; + var data; + + data = { + itemtype: itemtype, + items_id: itemId, + }; + switch (itemtype) { + case 'PluginFormcreatorQuestion': + parentId = $('form[name="plugin_formcreator_form"] [name="plugin_formcreator_sections_id"]').val(); + data.plugin_formcreator_sections_id = parentId; + break; + + case 'PluginFormcreatorSection': + parentId = $('form[name="plugin_formcreator_form"] [name="plugin_formcreator_forms_id"]').val(); + data.plugin_formcreator_forms_id = parentId; + break; + } $.ajax({ url: rootDoc + '/plugins/formcreator/ajax/condition.php', - data: { - itemtype: 'PluginFormcreatorQuestion', - items_id: questionId, - plugin_formcreator_sections_id: sectionId, - } + data: data }).done(function (data) { $(target).parents('tr').after(data); $('.plugin_formcreator_logicRow .div_show_condition_logic').first().hide(); @@ -778,8 +806,8 @@ function plugin_formcreator_removeNextCondition(target) { } function plugin_formcreator_changeDropdownItemtype(rand) { - dropdown_type = $('[name="form_question"] [name="dropdown_values"]').val(); - dropdown_id = $('[name="form_question"] [name="id"]').val(); + dropdown_type = $('[name="plugin_formcreator_form"] [name="dropdown_values"]').val(); + dropdown_id = $('[name="plugin_formcreator_form"] [name="id"]').val(); $.ajax({ url: rootDoc + '/plugins/formcreator/ajax/dropdown_values.php', @@ -815,8 +843,8 @@ function plugin_formcreator_changeDropdownItemtype(rand) { } function plugin_formcreator_changeGlpiObjectItemType() { - glpi_object = $('[name="form_question"] [name="glpi_objects"]').val(); - glpi_object_id = $('[name="form_question"] [name="id"]').val(); + glpi_object = $('[name="plugin_formcreator_form"] [name="glpi_objects"]').val(); + glpi_object_id = $('[name="plugin_formcreator_form"] [name="id"]').val(); $.ajax({ url: rootDoc + '/plugins/formcreator/ajax/dropdown_values.php', @@ -830,25 +858,17 @@ function plugin_formcreator_changeGlpiObjectItemType() { }); } -function plugin_formcreator_toggleCondition(field) { +function plugin_formcreator_toggleCondition(field, itemtype) { if (field.value == '1') { $('.plugin_formcreator_logicRow').hide(); } else { if ($('.plugin_formcreator_logicRow').length < 1) { - plugin_formcreator_addEmptyCondition(field); + plugin_formcreator_addEmptyCondition(field, itemtype); } $('.plugin_formcreator_logicRow').show(); } } -function toggleLogic(field) { - if (field.value == '0') { - $('#'+field.id).parents('tr').next().remove(); - } else { - plugin_formcreator_addEmptyCondition(field); - } -} - // === FIELDS === /** @@ -1034,8 +1054,8 @@ function pluginFormcreatorInitializeUrgency(fieldName, rand) { } function plugin_formcreator_changeQuestionType(rand) { - var questionId = $('form[name="form_question"] input[name="id"]').val(); - var questionType = $ ('form[name="form_question"] [name="fieldtype"]').val(); + var questionId = $('form[name="plugin_formcreator_form"] input[name="id"]').val(); + var questionType = $ ('form[name="plugin_formcreator_form"] [name="fieldtype"]').val(); $.ajax({ url: rootDoc + '/plugins/formcreator/ajax/question_design.php',
'; + $formFk = PluginFormcreatorForm::getForeignKeyField(); echo Html::hidden('id', ['value' => $ID]); echo Html::hidden('uuid', ['value' => $this->fields['uuid']]); - echo Html::hidden('plugin_formcreator_forms_id', ['value' => (int) $_REQUEST['plugin_formcreator_forms_id']]); + echo Html::hidden($formFk, ['value' => $this->fields[$formFk]]); echo '