@@ -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}
0 commit comments