Skip to content

Commit c9e3d1e

Browse files
committed
feat(targetticket): associate assets to tickets
Signed-off-by: Thierry Bugier <[email protected]>
1 parent c1dc721 commit c9e3d1e

File tree

3 files changed

+199
-1
lines changed

3 files changed

+199
-1
lines changed

inc/targetticket.class.php

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ protected function getCategoryFilter() {
7676
return "`is_request` = '1' OR `is_incident` = '1'";
7777
}
7878

79+
static function getEnumAssociateRule() {
80+
return [
81+
'none' => __('none', 'formcreator'),
82+
'specific' => __('Specific asset', 'formcreator'),
83+
'answer' => __('Equals to the answer to the question', 'formcreator'),
84+
];
85+
}
86+
7987
/**
8088
* Show the Form for the adminsitrator to edit in the config page
8189
*
@@ -142,6 +150,11 @@ public function showForm($options = []) {
142150
$this->showDueDateSettings($form, $rand);
143151
echo '</tr>';
144152

153+
// -------------------------------------------------------------------------------------------
154+
// associated elements of the target
155+
// -------------------------------------------------------------------------------------------
156+
$this->showAssociateSettings($rand);
157+
145158
// -------------------------------------------------------------------------------------------
146159
// category of the target
147160
// -------------------------------------------------------------------------------------------
@@ -958,6 +971,10 @@ public function prepareInputForUpdate($input) {
958971
$input = $this->saveLinkedItem($input);
959972
}
960973

974+
if (isset($input['items_id'])) {
975+
$input = $this->saveAssociatedItems($input);
976+
}
977+
961978
return parent::prepareInputForUpdate($input);
962979
}
963980

@@ -1180,6 +1197,7 @@ public function save(PluginFormcreatorFormAnswer $formanswer) {
11801197
$data = $this->setTargetUrgency($data, $formanswer);
11811198
$data = $this->setTargetCategory($data, $formanswer);
11821199
$data = $this->setTargetLocation($data, $formanswer);
1200+
$data = $this->setTargetAssociatedItem($data, $formanswer);
11831201

11841202
// There is always at least one requester
11851203
$data = $this->requesters + $data;
@@ -1364,6 +1382,130 @@ protected function setTargetLocation($data, $formanswer) {
13641382
return $data;
13651383
}
13661384

1385+
protected function showAssociateSettings($rand) {
1386+
global $DB, $CFG_GLPI;
1387+
1388+
echo '<tr class="line0">';
1389+
echo '<td width="15%">' . __('Associated elements') . '</td>';
1390+
echo '<td width="45%">';
1391+
Dropdown::showFromArray('associate_rule', static::getEnumAssociateRule(), [
1392+
'value' => $this->fields['associate_rule'],
1393+
'on_change' => 'change_associate()',
1394+
'rand' => $rand
1395+
]);
1396+
$script = <<<JAVASCRIPT
1397+
function change_associate() {
1398+
$('#associate_specific_title').hide();
1399+
$('#associate_specific_value').hide();
1400+
$('#associate_question_title').hide();
1401+
$('#associate_question_value').hide();
1402+
1403+
switch($('#dropdown_associate_rule$rand').val()) {
1404+
case 'answer' :
1405+
$('#associate_question_title').show();
1406+
$('#associate_question_value').show();
1407+
break;
1408+
case 'specific':
1409+
$('#associate_specific_title').show();
1410+
$('#associate_specific_value').show();
1411+
break;
1412+
}
1413+
}
1414+
change_associate();
1415+
JAVASCRIPT;
1416+
echo Html::scriptBlock($script);
1417+
echo '</td>';
1418+
echo '<td width="15%">';
1419+
echo '<span id="associate_question_title" style="display: none">' . __('Question', 'formcreator') . '</span>';
1420+
echo '<span id="associate_specific_title" style="display: none">' . __('Item ', 'formcreator') . '</span>';
1421+
echo '</td>';
1422+
echo '<td width="25%">';
1423+
1424+
echo '<div id="associate_specific_value" style="display: none">';
1425+
$options = json_decode($this->fields['associate_question'], true);
1426+
if (!is_array($options)) {
1427+
$options = [];
1428+
}
1429+
$options['_canupdate'] = true;
1430+
$targetTicketFk = self::getForeignKeyField();
1431+
$itemTargetTicket = new PluginFormcreatorItem_TargetTicket();
1432+
$targetTicketId = $this->getID();
1433+
$exclude = implode("', '", [PluginFormcreatorTargetTicket::class, Ticket::class]);
1434+
$rows = $itemTargetTicket->find("`$targetTicketFk` = '$targetTicketId'
1435+
AND `itemtype` NOT IN ('$exclude')");
1436+
foreach ($rows as $row) {
1437+
$options['items_id'][$row['itemtype']][$row['id']] = $row['id'];
1438+
}
1439+
Item_Ticket::itemAddForm(new Ticket(), $options);
1440+
echo '</div>';
1441+
echo '<div id="associate_question_value" style="display: none">';
1442+
// select all user questions (GLPI Object)
1443+
$ticketTypes = implode("' ,'", $CFG_GLPI["ticket_types"]);
1444+
$query2 = "SELECT q.id, q.name, q.values
1445+
FROM glpi_plugin_formcreator_questions q
1446+
INNER JOIN glpi_plugin_formcreator_sections s
1447+
ON s.id = q.plugin_formcreator_sections_id
1448+
INNER JOIN glpi_plugin_formcreator_targets t
1449+
ON s.plugin_formcreator_forms_id = t.plugin_formcreator_forms_id
1450+
WHERE t.items_id = ".$this->getID()."
1451+
AND q.fieldtype = 'glpiselect'
1452+
AND q.values IN ('$ticketTypes')";
1453+
$result2 = $DB->query($query2);
1454+
$users_questions = [];
1455+
while ($question = $DB->fetch_array($result2)) {
1456+
$users_questions[$question['id']] = $question['name'];
1457+
}
1458+
Dropdown::showFromArray('_associate_question', $users_questions, [
1459+
'value' => $this->fields['associate_question'],
1460+
]);
1461+
echo '</div>';
1462+
echo '</td>';
1463+
echo '</tr>';
1464+
}
1465+
1466+
protected function setTargetAssociatedItem($data, $formanswer) {
1467+
switch ($this->fields['associate_rule']) {
1468+
case 'answer':
1469+
// find the itemtype of the associated item
1470+
$associateQuestion = $this->fields['associate_question'];
1471+
$question = new PluginFormcreatorQuestion();
1472+
$question->getFromDB($associateQuestion);
1473+
$itemtype = $question->fields['values'];
1474+
1475+
// find the id of the associated item
1476+
$answer = new PluginFormcreatorAnswer();
1477+
$formAnswerId = $formanswer->fields['id'];
1478+
$found = $answer->find("`plugin_formcreator_forms_answers_id` = '$formAnswerId'
1479+
AND `plugin_formcreator_questions_id` = '$associateQuestion'");
1480+
$associate = array_shift($found);
1481+
$itemId = $associate['answer'];
1482+
1483+
// associate the item if it exists
1484+
if (!class_exists($itemtype)) {
1485+
return $data;
1486+
}
1487+
$item = new $itemtype();
1488+
if ($item->getFromDB($itemId)) {
1489+
$data['items_id'] = [$itemtype => [$itemId => $itemId]];
1490+
}
1491+
break;
1492+
1493+
case 'specific':
1494+
$targetTicketFk = self::getForeignKeyField();
1495+
$itemTargetTicket = new PluginFormcreatorItem_TargetTicket();
1496+
$targetTicketId = $this->getID();
1497+
$exclude = implode("', '", [PluginFormcreatorTargetTicket::class, Ticket::class]);
1498+
$rows = $itemTargetTicket->find("`$targetTicketFk` = '$targetTicketId'
1499+
AND `itemtype` NOT IN ('$exclude')");
1500+
foreach ($rows as $row) {
1501+
$data['items_id'] = [$row['itemtype'] => [$row['items_id'] => $row['items_id']]];
1502+
}
1503+
break;
1504+
}
1505+
1506+
return $data;
1507+
}
1508+
13671509
private static function getDeleteImage($id) {
13681510
global $CFG_GLPI;
13691511

@@ -1532,4 +1674,35 @@ public function export($remove_uuid = false) {
15321674
unset($target_data['name']);
15331675
return $target_data;
15341676
}
1677+
1678+
private function saveAssociatedItems($input) {
1679+
switch ($input['associate_rule']) {
1680+
case 'answer':
1681+
$input['associate_question'] = $input['_associate_question'];
1682+
break;
1683+
1684+
case 'specific':
1685+
$itemTargetTicket = new PluginFormcreatorItem_TargetTicket();
1686+
$itemTargetTicket->deleteByCriteria([
1687+
'NOT' => ['itemtype' => [
1688+
PluginFormcreatorTargetTicket::class,
1689+
Ticket::class,
1690+
]]
1691+
]);
1692+
$targetTicketFk = self::getForeignKeyField();
1693+
foreach ($input['items_id'] as $itemtype => $items) {
1694+
foreach ($items as $id) {
1695+
$itemTargetTicket = new PluginFormcreatorItem_TargetTicket();
1696+
$itemTargetTicket->add([
1697+
'itemtype' => $itemtype,
1698+
'items_id' => $id,
1699+
$targetTicketFk => $this->getID(),
1700+
]);
1701+
}
1702+
}
1703+
break;
1704+
}
1705+
unset($input['items_id']);
1706+
return $input;
1707+
}
15351708
}

install/mysql/plugin_formcreator_empty.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_targettickets` (
210210
`tag_specifics` varchar(255) NOT NULL,
211211
`category_rule` enum('none','specific','answer') NOT NULL DEFAULT 'none',
212212
`category_question` int(11) NOT NULL DEFAULT '0',
213+
`associate_rule` enum('none','specific','answer') NOT NULL DEFAULT 'none',
214+
`associate_question` int(11) NOT NULL DEFAULT '0',
213215
PRIMARY KEY (`id`),
214216
INDEX `tickettemplates_id` (`tickettemplates_id`)
215217
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

install/upgrade_to_2.8.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
* You should have received a copy of the GNU General Public License
2222
* along with Formcreator. If not, see <http://www.gnu.org/licenses/>.
2323
* ---------------------------------------------------------------------
24-
*
2524
* @copyright Copyright © 2011 - 2019 Teclib'
2625
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+
2726
* @link https://github.com/pluginsGLPI/formcreator/
@@ -50,5 +49,29 @@ public function upgrade(Migration $migration) {
5049
'actor_type',
5150
"ENUM('creator','validator','person','question_person','group','question_group','supplier','question_supplier','question_actors') NOT NULL"
5251
);
52+
53+
// add item association rule
54+
$table = 'glpi_plugin_formcreator_targettickets';
55+
$enum_associate_rule = "'".implode("', '", array_keys(PluginFormcreatorTargetTicket::getEnumAssociateRule()))."'";
56+
if (!$DB->fieldExists($table, 'associate_rule', false)) {
57+
$migration->addField(
58+
$table,
59+
'associate_rule',
60+
"ENUM($enum_associate_rule) NOT NULL DEFAULT 'none'",
61+
['after' => 'category_question']
62+
);
63+
} else {
64+
$current_enum_associate_rule = PluginFormcreatorCommon::getEnumValues($table, 'associate_rule');
65+
if (count($current_enum_associate_rule) != count($enum_associate_rule)) {
66+
$migration->changeField(
67+
$table,
68+
'location_rule',
69+
'location_rule',
70+
"ENUM($enum_associate_rule) NOT NULL DEFAULT 'none'",
71+
['after' => 'category_question']
72+
);
73+
}
74+
}
75+
$migration->addField($table, 'associate_question', 'integer', ['after' => 'associate_rule']);
5376
}
5477
}

0 commit comments

Comments
 (0)