diff --git a/inc/formanswer.class.php b/inc/formanswer.class.php index 9791c3ede..b299a7fa9 100644 --- a/inc/formanswer.class.php +++ b/inc/formanswer.class.php @@ -933,6 +933,19 @@ public function generateTarget() { $generatedTargets = new PluginFormcreatorComposite(new PluginFormcreatorItem_TargetTicket(), new Ticket_Ticket()); foreach ($all_targets as $targetType => $targets) { foreach ($targets as $targetObject) { + // Check the condition of the target + $this->questionFields = $form->getFields(); + $answers_values = $this->getAnswers($this->getID()); + foreach ($this->questionFields as $id => $field) { + $this->questionFields[$id]->deserializeValue($answers_values['formcreator_field_' . $id]); + } + + if (!PluginFormcreatorFields::isVisible($targetObject, $this->questionFields)) { + // The target shall not be generated + continue; + } + + // Generate the target $generatedTarget = $targetObject->save($this); if ($generatedTarget === null) { $success = false; diff --git a/inc/targetbase.class.php b/inc/targetbase.class.php index def613890..e15256dd1 100644 --- a/inc/targetbase.class.php +++ b/inc/targetbase.class.php @@ -35,8 +35,11 @@ abstract class PluginFormcreatorTargetBase extends CommonDBChild implements PluginFormcreatorExportableInterface, -PluginFormcreatorTargetInterface +PluginFormcreatorTargetInterface, +PluginFormcreatorConditionnableInterface { + use PluginFormcreatorConditionnable; + static public $itemtype = PluginFormcreatorForm::class; static public $items_id = 'plugin_formcreator_forms_id'; @@ -1478,6 +1481,14 @@ protected function convertTags($input) { return $input; } + protected function showConditionsSettings($rand) { + $formFk = PluginFormcreatorForm::getForeignKeyField(); + $form = new PluginFormcreatorForm(); + $form->getFromDB($this->fields[$formFk]); + $condition = new PluginFormcreatorCondition(); + $condition->showConditionsForItem($this); + } + /** * Show header for actors edition * diff --git a/inc/targetchange.class.php b/inc/targetchange.class.php index 2430186b2..4ff487d91 100644 --- a/inc/targetchange.class.php +++ b/inc/targetchange.class.php @@ -133,6 +133,14 @@ public function export($remove_uuid = false) { } } + // get conditions + $target_data['_conditions'] = []; + $condition = new PluginFormcreatorCondition(); + $all_conditions = $condition->getConditionsFromItem($this); + foreach ($all_conditions as $condition) { + $target_data['_conditions'][] = $condition->export($remove_uuid); + } + // remove ID or UUID $idToRemove = 'id'; if ($remove_uuid) { @@ -225,6 +233,13 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con } } + // Import conditions + if (isset($input['_conditions'])) { + foreach ($input['_conditions'] as $condition) { + PluginFormcreatorCondition::import($linker, $condition, $itemId); + } + } + return $itemId; } @@ -241,7 +256,7 @@ public function showForm($options = []) { $form = $this->getForm(); echo '
'; - echo '
'; + echo ''; // General information: target_name echo ''; @@ -351,6 +366,17 @@ public function showForm($options = []) { // ------------------------------------------------------------------------------------------- $this->showPluginTagsSettings($form, $rand); + // ------------------------------------------------------------------------------------------- + // Conditions to generate the target + // ------------------------------------------------------------------------------------------- + echo ''; + echo ''; + echo ''; + $this->showConditionsSettings($rand); + echo '
'; + echo __('Condition to show the target', 'formcreator'); + echo ''; + echo '
'; // Buttons @@ -361,6 +387,8 @@ public function showForm($options = []) { echo '   '; echo ''; + $formFk = PluginFormcreatorForm::getForeignKeyField(); + echo Html::hidden($formFk, ['value' => $this->fields[$formFk]]); echo ''; echo ''; echo ''; @@ -475,9 +503,27 @@ public function pre_purgeItem() { return false; } + // delete conditions + if (! (new PluginFormcreatorCondition())->deleteByCriteria([ + 'itemtype' => self::class, + 'items_id' => $this->getID(), + ])) { + return false; + } + return true; } + public function post_addItem() { + parent::post_addItem(); + $this->updateConditions($this->input); + } + + public function post_updateItem($history = 1) { + parent::post_updateItem(); + $this->updateConditions($this->input); + } + /** * Save form data to the target * diff --git a/inc/targetticket.class.php b/inc/targetticket.class.php index 881516dc1..bc0696ea7 100644 --- a/inc/targetticket.class.php +++ b/inc/targetticket.class.php @@ -110,7 +110,7 @@ public function showForm($ID, $options = []) { $form = $this->getForm(); echo '
'; - echo ''; + echo ''; // General information: target_name echo ''; @@ -200,6 +200,17 @@ public function showForm($ID, $options = []) { echo ''; } + // ------------------------------------------------------------------------------------------- + // Conditions to generate the target + // ------------------------------------------------------------------------------------------- + echo ''; + echo ''; + echo ''; + $this->showConditionsSettings($rand); + echo '
'; + echo __('Condition to show the target', 'formcreator'); + echo ''; + echo '
'; // Buttons @@ -210,6 +221,8 @@ public function showForm($ID, $options = []) { echo '   '; echo ''; + $formFk = PluginFormcreatorForm::getForeignKeyField(); + echo Html::hidden($formFk, ['value' => $this->fields[$formFk]]); echo ''; echo ''; echo ''; @@ -452,6 +465,14 @@ public function pre_purgeItem() { return false; } + // delete conditions + if (! (new PluginFormcreatorCondition())->deleteByCriteria([ + 'itemtype' => self::class, + 'items_id' => $this->getID(), + ])) { + return false; + } + // delete targets linked to this instance $myFk = static::getForeignKeyField(); $item_targetTicket = new PluginFormcreatorItem_TargetTicket(); @@ -463,6 +484,16 @@ public function pre_purgeItem() { return true; } + public function post_addItem() { + parent::post_addItem(); + $this->updateConditions($this->input); + } + + public function post_updateItem($history = 1) { + parent::post_updateItem(); + $this->updateConditions($this->input); + } + /** * Save links to other items for composite tickets * @param array $input form data @@ -1022,6 +1053,13 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con } } + // Import conditions + if (isset($input['_conditions'])) { + foreach ($input['_conditions'] as $condition) { + PluginFormcreatorCondition::import($linker, $condition, $itemId); + } + } + return $itemId; } @@ -1092,6 +1130,14 @@ public function export($remove_uuid = false) { $target_data['_ticket_relations'][] = $target_ticketLink->export($remove_uuid); } + // get conditions + $target_data['_conditions'] = []; + $condition = new PluginFormcreatorCondition(); + $all_conditions = $condition->getConditionsFromItem($this); + foreach ($all_conditions as $condition) { + $target_data['_conditions'][] = $condition->export($remove_uuid); + } + // remove ID or UUID $idToRemove = 'id'; if ($remove_uuid) { diff --git a/install/mysql/plugin_formcreator_empty.sql b/install/mysql/plugin_formcreator_empty.sql index 9ed38cb80..fbf5d4dc6 100644 --- a/install/mysql/plugin_formcreator_empty.sql +++ b/install/mysql/plugin_formcreator_empty.sql @@ -169,6 +169,7 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_targetchanges` ( `tag_specifics` varchar(255) NOT NULL, `category_rule` int(11) NOT NULL DEFAULT '1', `category_question` int(11) NOT NULL DEFAULT '0', + `show_rule` int(11) NOT NULL DEFAULT '1', `uuid` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; @@ -210,6 +211,7 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_targettickets` ( `category_question` int(11) NOT NULL DEFAULT '0', `associate_rule` int(11) NOT NULL DEFAULT '1', `associate_question` int(11) NOT NULL DEFAULT '0', + `show_rule` int(11) NOT NULL DEFAULT '1', `uuid` varchar(255) DEFAULT NULL, `location_rule` INT(11) NOT NULL DEFAULT '1', `location_question` int(11) NOT NULL DEFAULT '0', diff --git a/install/upgrade_to_2.10.php b/install/upgrade_to_2.10.php index d3e3ba03b..a19c2f9d6 100644 --- a/install/upgrade_to_2.10.php +++ b/install/upgrade_to_2.10.php @@ -56,5 +56,11 @@ public function upgrade(Migration $migration) { $migration->changeField($table, 'type', 'type_question', 'integer', ['after' => 'target_name', 'value' => '0']); $migration->migrationOneTable($table); $migration->addField($table, 'type_rule', 'integer', ['after' => 'target_name', 'value' => '1']); + + // conditions on targets + $table = 'glpi_plugin_formcreator_targetchanges'; + $migration->addField($table, 'show_rule', 'integer', ['value' => '1', 'after' => 'category_question']); + $table = 'glpi_plugin_formcreator_targettickets'; + $migration->addField($table, 'show_rule', 'integer', ['value' => '1', 'after' => 'location_question']); } }