Skip to content

Commit

Permalink
Merge branch 'feat/backport_conditions_on_targets' into support/2.10.0
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Mar 23, 2020
2 parents edaa13b + 7f905a0 commit a42ec4e
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 3 deletions.
13 changes: 13 additions & 0 deletions inc/formanswer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 12 additions & 1 deletion inc/targetbase.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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
*
Expand Down
48 changes: 47 additions & 1 deletion inc/targetchange.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand All @@ -241,7 +256,7 @@ public function showForm($options = []) {
$form = $this->getForm();

echo '<div class="center" style="width: 950px; margin: 0 auto;">';
echo '<form name="form_target" method="post" action="' . self::getFormURL() . '">';
echo '<form name="form_target" method="post" action="' . self::getFormURL() . '" data-itemtype="' . self::class . '">';

// General information: target_name
echo '<table class="tab_cadre_fixe">';
Expand Down Expand Up @@ -351,6 +366,17 @@ public function showForm($options = []) {
// -------------------------------------------------------------------------------------------
$this->showPluginTagsSettings($form, $rand);

// -------------------------------------------------------------------------------------------
// Conditions to generate the target
// -------------------------------------------------------------------------------------------
echo '<tr>';
echo '<th colspan="4">';
echo __('Condition to show the target', 'formcreator');
echo '</label>';
echo '</th>';
echo '</tr>';
$this->showConditionsSettings($rand);

echo '</table>';

// Buttons
Expand All @@ -361,6 +387,8 @@ public function showForm($options = []) {
echo '<input type="reset" name="reset" class="submit_button" value="' . __('Cancel', 'formcreator') . '"
onclick="document.location = \'form.form.php?id=' . $this->fields['plugin_formcreator_forms_id'] . '\'" /> &nbsp; ';
echo '<input type="hidden" name="id" value="' . $this->getID() . '" />';
$formFk = PluginFormcreatorForm::getForeignKeyField();
echo Html::hidden($formFk, ['value' => $this->fields[$formFk]]);
echo '<input type="submit" name="update" class="submit_button" value="' . __('Save') . '" />';
echo '</td>';
echo '</tr>';
Expand Down Expand Up @@ -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
*
Expand Down
48 changes: 47 additions & 1 deletion inc/targetticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function showForm($ID, $options = []) {
$form = $this->getForm();

echo '<div class="center" style="width: 950px; margin: 0 auto;">';
echo '<form name="form_target" method="post" action="' . self::getFormURL() . '">';
echo '<form name="form_target" method="post" action="' . self::getFormURL() . '" data-itemtype="' . self::class . '">';

// General information: target_name
echo '<table class="tab_cadre_fixe">';
Expand Down Expand Up @@ -200,6 +200,17 @@ public function showForm($ID, $options = []) {
echo '</tr>';
}

// -------------------------------------------------------------------------------------------
// Conditions to generate the target
// -------------------------------------------------------------------------------------------
echo '<tr>';
echo '<th colspan="4">';
echo __('Condition to show the target', 'formcreator');
echo '</label>';
echo '</th>';
echo '</tr>';
$this->showConditionsSettings($rand);

echo '</table>';

// Buttons
Expand All @@ -210,6 +221,8 @@ public function showForm($ID, $options = []) {
echo '<input type="reset" name="reset" class="submit_button" value="' . __('Cancel', 'formcreator') . '"
onclick="document.location = \'form.form.php?id=' . $this->fields['plugin_formcreator_forms_id'] . '\'" /> &nbsp; ';
echo '<input type="hidden" name="id" value="' . $this->getID() . '" />';
$formFk = PluginFormcreatorForm::getForeignKeyField();
echo Html::hidden($formFk, ['value' => $this->fields[$formFk]]);
echo '<input type="submit" name="update" class="submit_button" value="' . __('Save') . '" />';
echo '</td>';
echo '</tr>';
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions install/mysql/plugin_formcreator_empty.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -213,6 +214,7 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_targettickets` (
`uuid` varchar(255) DEFAULT NULL,
`location_rule` INT(11) NOT NULL DEFAULT '1',
`location_question` int(11) NOT NULL DEFAULT '0',
`show_rule` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
INDEX `tickettemplates_id` (`tickettemplates_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Expand Down
6 changes: 6 additions & 0 deletions install/upgrade_to_2.10.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
2 changes: 2 additions & 0 deletions tests/suite-unit/PluginFormcreatorTargetChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,11 @@ public function testExport() {
'tag_specifics',
'category_rule',
'category_question',
'show_rule',
];
$extraFields = [
'_actors',
'_conditions',
];

$this->array($output)
Expand Down
2 changes: 2 additions & 0 deletions tests/suite-unit/PluginFormcreatorTargetTicket.php
Original file line number Diff line number Diff line change
Expand Up @@ -661,11 +661,13 @@ public function testExport() {
'associate_question',
'location_rule',
'location_question',
'show_rule',
];
$extraFields = [
'_tickettemplate',
'_actors',
'_ticket_relations',
'_conditions',
];

$this->array($output)
Expand Down

0 comments on commit a42ec4e

Please sign in to comment.