Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions inc/abstractitiltarget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ abstract protected function getTargetTemplate(array $data): int;
const LOCATION_RULE_ANSWER = 3;
const LOCATION_RULE_LAST_ANSWER = 4;

const CONTRACT_RULE_NONE = 1;
const CONTRACT_RULE_SPECIFIC = 2;
const CONTRACT_RULE_ANSWER = 3;
const CONTRACT_RULE_LAST_ANSWER = 4;

const COMMONITIL_VALIDATION_RULE_NONE = 1;
const COMMONITIL_VALIDATION_RULE_SPECIFIC_USER_OR_GROUP = 2;
const COMMONITIL_VALIDATION_RULE_ANSWER_USER = 3;
Expand Down Expand Up @@ -235,6 +240,15 @@ public static function getEnumLocationRule() {
];
}

public static function getEnumContractRule() {
return [
self::CONTRACT_RULE_NONE => __('Contract from template or none', 'formcreator'),
self::CONTRACT_RULE_SPECIFIC => __('Specific contract', 'formcreator'),
self::CONTRACT_RULE_ANSWER => __('Equals to the answer to the question', 'formcreator'),
self::CONTRACT_RULE_LAST_ANSWER => __('Last valid answer', 'formcreator'),
];
}

public static function getEnumValidationRule() {
return [
self::COMMONITIL_VALIDATION_RULE_NONE => __('No validation', 'formcreator'),
Expand Down Expand Up @@ -1263,6 +1277,48 @@ protected function showLocationSettings($rand) {
echo '</tr>';
}

protected function showContractSettings($rand) {
global $DB;

echo '<tr>';
echo '<td width="15%">' . __('Contract') . '</td>';
echo '<td width="45%">';
Dropdown::showFromArray('contract_rule', static::getEnumContractRule(), [
'value' => $this->fields['contract_rule'],
'on_change' => "plugin_formcreator_change_contract($rand)",
'rand' => $rand
]);

echo Html::scriptBlock("plugin_formcreator_change_contract($rand)");
echo '</td>';
echo '<td width="15%">';
echo '<span id="contract_question_title" style="display: none">' . __('Question', 'formcreator') . '</span>';
echo '<span id="contract_specific_title" style="display: none">' . __('Contract ', 'formcreator') . '</span>';
echo '</td>';
echo '<td width="25%">';

echo '<div id="contract_specific_value" style="display: none">';
Contract::dropdown([
'name' => '_contract_specific',
'value' => $this->fields["contract_question"],
]);
echo '</div>';
echo '<div id="contract_question_value" style="display: none">';
PluginFormcreatorQuestion::dropdownForForm(
$this->getForm(),
[
'fieldtype' => ['glpiselect'],
'itemtype' => Contract::class
],
'contract_question',
$this->fields['contract_question']
);

echo '</div>';
echo '</td>';
echo '</tr>';
}

protected function showValidationSettings($rand) {
echo '<tr>';

Expand Down
97 changes: 97 additions & 0 deletions inc/targetticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ public static function showProperties(self $item) {
// -------------------------------------------------------------------------------------------
$item->showLocationSettings($rand);

// -------------------------------------------------------------------------------------------
// Contracts
// -------------------------------------------------------------------------------------------
$item->showContractSettings($rand);

// -------------------------------------------------------------------------------------------
// Validation selection
// -------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -669,6 +674,20 @@ public function prepareInputForUpdate($input) {
}
}

if (isset($input['contract_rule'])) {
switch ($input['contract_rule']) {
case self::CONTRACT_RULE_ANSWER:
$input['contract_question'] = $input['_contract_question'];
break;
case self::CONTRACT_RULE_SPECIFIC:
$input['contract_question'] = $input['_contract_specific'];
break;
case self::CONTRACT_RULE_LAST_ANSWER:
default:
$input['contract_question'] = '0';
}
}

$plugin = new Plugin();
if ($plugin->isActivated('tag')) {
if (isset($input['tag_questions'])) {
Expand Down Expand Up @@ -888,6 +907,7 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
$data = $this->setOLA($data, $formanswer);
$data = $this->setTargetUrgency($data, $formanswer);
$data = $this->setTargetLocation($data, $formanswer);
$data = $this->setTargetContract($data, $formanswer);
$data = $this->setTargetAssociatedItem($data, $formanswer);
$data = $this->setTargetValidation($data, $formanswer);

Expand Down Expand Up @@ -1069,6 +1089,82 @@ protected function setTargetLocation($data, $formanswer) {
return $data;
}

protected function setTargetContract($data, $formanswer) {
global $DB;

$contract = null;
switch ($this->fields['contract_rule']) {
case self::CONTRACT_RULE_ANSWER:
$contract = $DB->request([
'SELECT' => ['answer'],
'FROM' => PluginFormcreatorAnswer::getTable(),
'WHERE' => [
'plugin_formcreator_formanswers_id' => $formanswer->fields['id'],
'plugin_formcreator_questions_id' => $this->fields['contract_question']
]
])->current();
if (isset($contract['answer']) && ctype_digit($contract['answer'])) {
$contract = $contract['answer'];
}
break;
case self::CONTRACT_RULE_SPECIFIC:
$contract = $this->fields['contract_question'];
break;
case self::CONTRACT_RULE_LAST_ANSWER:
$form_answer_id = $formanswer->fields['id'];

// Get all answers for dropdown questions of this form, ordered
// from last to first displayed
$answers = $DB->request([
'SELECT' => ['answer.plugin_formcreator_questions_id', 'answer.answer', 'question.values'],
'FROM' => PluginFormcreatorAnswer::getTable() . ' AS answer',
'JOIN' => [
PluginFormcreatorQuestion::getTable() . ' AS question' => [
'ON' => [
'answer' => 'plugin_formcreator_questions_id',
'question' => 'id',
]
]
],
'WHERE' => [
'answer.plugin_formcreator_formanswers_id' => $form_answer_id,
'question.fieldtype' => "glpiselect",
'question.itemtype' => Contract::class,
],
'ORDER' => [
'row DESC',
'col DESC',
]
]);

foreach ($answers as $answer) {
// Decode dropdown settings
$question = PluginFormcreatorQuestion::getById($answer[PluginFormcreatorQuestion::getForeignKeyField()]);
$itemtype = $question->fields['itemtype'];

// Skip if question was not answered
if (empty($answer['answer'])) {
continue;
}

// Skip if question is not visible
if (!$formanswer->isFieldVisible($answer['plugin_formcreator_questions_id'])) {
continue;
}

// Found a valid answer, stop here
$contract = $answer['answer'];
break;
}
break;
}
if (!is_null($contract)) {
$data['_contracts_id'] = $contract;
}

return $data;
}

protected function setTargetSource(array $data, PluginFormcreatorFormAnswer $formanswer): array {
switch ($this->fields['source_rule']) {
case self::REQUESTSOURCE_SPECIFIC:
Expand Down Expand Up @@ -1515,6 +1611,7 @@ public function export(bool $remove_uuid = false) : array {
'category_rule' => ['values' => self::CATEGORY_RULE_ANSWER, 'field' => 'category_question'],
'associate_rule' => ['values' => self::ASSOCIATE_RULE_ANSWER, 'field' => 'associate_question'],
'location_rule' => ['values' => self::LOCATION_RULE_ANSWER, 'field' => 'location_question'],
'contract_rule' => ['values' => self::CONTRACT_RULE_ANSWER, 'field' => 'contract_question'],
'destination_entity' => [
'values' => [
self::DESTINATION_ENTITY_ENTITY,
Expand Down
2 changes: 2 additions & 0 deletions install/mysql/plugin_formcreator_2.14.0_empty.sql
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_targettickets` (
`associate_question` int unsigned NOT NULL DEFAULT '0',
`location_rule` int(11) NOT NULL DEFAULT '1',
`location_question` int unsigned NOT NULL DEFAULT '0',
`contract_rule` int(11) NOT NULL DEFAULT '1',
`contract_question` int unsigned NOT NULL DEFAULT '0',
`commonitil_validation_rule` int(11) NOT NULL DEFAULT '1',
`commonitil_validation_question` varchar(255) DEFAULT NULL,
`show_rule` int(11) NOT NULL DEFAULT '1',
Expand Down
9 changes: 9 additions & 0 deletions install/upgrade_to_2.14.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function upgrade(Migration $migration) {
$this->addRights();
$this->addPropertiesToCategories();
$this->addTargetActorUnicity();
$this->addTargetContract();
}

public function addTtoToIssues() {
Expand Down Expand Up @@ -131,4 +132,12 @@ public function addTargetActorUnicity() {
// Set unicity
$this->migration->addKey($table, $unicity, 'unicity', 'UNIQUE');
}

public function addTargetContract() {
$table = 'glpi_plugin_formcreator_targettickets';
$unsignedIntType = "INT UNSIGNED NOT NULL DEFAULT '0'";

$this->migration->addField($table, 'contract_rule', 'integer', ['after' => 'location_question', 'value' => '1']);
$this->migration->addField($table, 'contract_question', $unsignedIntType, ['after' => 'contract_rule']);
}
}
18 changes: 18 additions & 0 deletions js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -1930,6 +1930,24 @@ function plugin_formcreator_change_location(rand) {
}
}

function plugin_formcreator_change_contract(rand) {
$('#contract_specific_title').hide();
$('#contract_specific_value').hide();
$('#contract_question_title').hide();
$('#contract_question_value').hide();

switch($('#dropdown_contract_rule' + rand).val()) {
case '3' : // PluginFormcreatorAbstractTarget::CONTRACT_RULE_ANSWER
$('#contract_question_title').show();
$('#contract_question_value').show();
break;
case '2' : // PluginFormcreatorAbstractTarget::CONTRACT_RULE_SPECIFIC
$('#contract_specific_title').show();
$('#contract_specific_value').show();
break;
}
}

function plugin_formcreator_change_validation(rand) {
switch($('#dropdown_commonitil_validation_rule' + rand).val()) {
case '1' : // PluginFormcreatorAbstractTarget::COMMONITIL_VALIDATION_RULE_NONE
Expand Down
Loading