Skip to content

Commit f9a23b6

Browse files
feat(question): new hooks for other plugins interaction (#3093)
1 parent 810c854 commit f9a23b6

File tree

5 files changed

+157
-64
lines changed

5 files changed

+157
-64
lines changed

inc/abstractfield.class.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,17 @@ public function show(string $domain, bool $canEdit = true): string {
9696
continue;
9797
}
9898
}
99+
$description = Plugin::doHookFunction('formcreator_question_description', [
100+
'description' => $description,
101+
'question' => $this->getQuestion()
102+
])['description'];
99103
$html .= '<div class="help-block">' . html_entity_decode(__($description, $domain)) . '</div>';
100104
}
101105
$html .= '<div class="form_field">';
106+
$this->value = Plugin::doHookFunction('formcreator_question_default_value', [
107+
'value' => $this->value,
108+
'question' => $this->getQuestion()
109+
])['value'];
102110
$html .= $this->getRenderedHtml($domain, $canEdit);
103111
$html .= '</div>';
104112

inc/abstractitiltarget.class.php

Lines changed: 83 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ protected function setTargetUrgency($data, $formanswer) {
437437
* find all actors and prepare data for the ticket being created
438438
*/
439439
protected function prepareActors(PluginFormcreatorForm $form, PluginFormcreatorFormAnswer $formanswer) {
440-
global $DB;
440+
global $DB, $PLUGIN_HOOKS;
441441

442442
$rows = $DB->request([
443443
'FROM' => PluginFormcreatorTarget_Actor::getTable(),
@@ -586,6 +586,26 @@ protected function prepareActors(PluginFormcreatorForm $form, PluginFormcreatorF
586586
$this->addActor(PluginFormcreatorTarget_Actor::ACTOR_ROLE_SUPPLIER, $userId, $notify);
587587
}
588588
break;
589+
default:
590+
foreach (($PLUGIN_HOOKS['formcreator_actors_type'] ?? []) as $plugin => $classes) {
591+
foreach ($classes as $plugin_target) {
592+
if (!is_a($plugin_target, PluginFormcreatorPluginTargetInterface::class, true)) {
593+
continue;
594+
}
595+
if ($actor['actor_type']== $plugin_target::getId()) {
596+
$value = $plugin_target::getActorId($formanswer, $actor['actor_value']);
597+
if ($value) {
598+
if ($plugin_target::getActorType() == PluginFormcreatorPluginTargetInterface::ACTOR_TYPE_USER) {
599+
$this->addActor($actor['actor_role'], $value, $notify);
600+
} else if (PluginFormcreatorPluginTargetInterface::ACTOR_TYPE_GROUP) {
601+
$this->addGroupActor($actor['actor_role'], $value);
602+
}
603+
}
604+
break 2;
605+
}
606+
}
607+
}
608+
break;
589609
}
590610
}
591611
}
@@ -1760,7 +1780,7 @@ protected function showActorSettingsHeader($type) {
17601780
* @return void
17611781
*/
17621782
protected function showActorSettingsForType($actorType, array $actors) {
1763-
global $DB;
1783+
global $DB, $PLUGIN_HOOKS;
17641784

17651785
$itemActor = new PluginFormcreatorTarget_Actor();
17661786
$dropdownItems = ['' => Dropdown::EMPTY_VALUE] + $itemActor::getEnumActorType();
@@ -1799,21 +1819,21 @@ protected function showActorSettingsForType($actorType, array $actors) {
17991819
]
18001820
);
18011821

1802-
echo '<div id="block_' . $type . '_user" style="display:none">';
1822+
echo '<div style="display:none" data-actor-type="' . $type . "_" . PluginFormcreatorTarget_Actor::ACTOR_TYPE_PERSON . '">';
18031823
User::dropdown([
18041824
'name' => 'actor_value_' . PluginFormcreatorTarget_Actor::ACTOR_TYPE_PERSON,
18051825
'right' => 'all',
18061826
'all' => 0,
18071827
]);
18081828
echo '</div>';
18091829

1810-
echo '<div id="block_' . $type . '_group" style="display:none">';
1830+
echo '<div style="display:none" data-actor-type="' . $type . "_" . PluginFormcreatorTarget_Actor::ACTOR_TYPE_GROUP . '">';
18111831
Group::dropdown([
18121832
'name' => 'actor_value_' . PluginFormcreatorTarget_Actor::ACTOR_TYPE_GROUP,
18131833
]);
18141834
echo '</div>';
18151835

1816-
echo '<div id="block_' . $type . '_question_user" style="display:none">';
1836+
echo '<div style="display:none" data-actor-type="' . $type . "_" . PluginFormcreatorTarget_Actor::ACTOR_TYPE_QUESTION_PERSON . '">';
18171837
// find already used items
18181838
$request = $DB->request([
18191839
'FROM' => PluginFormcreatorTarget_Actor::getTable(),
@@ -1850,7 +1870,7 @@ protected function showActorSettingsForType($actorType, array $actors) {
18501870
);
18511871
echo '</div>';
18521872

1853-
echo '<div id="block_' . $type . '_question_group" style="display:none">';
1873+
echo '<div style="display:none" data-actor-type="' . $type . "_" . PluginFormcreatorTarget_Actor::ACTOR_TYPE_QUESTION_GROUP . '">';
18541874
// find already used items
18551875
$request = $DB->request([
18561876
'FROM' => PluginFormcreatorTarget_Actor::getTable(),
@@ -1880,7 +1900,7 @@ protected function showActorSettingsForType($actorType, array $actors) {
18801900
);
18811901
echo '</div>';
18821902

1883-
echo '<div id="block_' . $type . '_group_from_object" style="display:none">';
1903+
echo '<div style="display:none" data-actor-type="' . $type . "_" . PluginFormcreatorTarget_Actor::ACTOR_TYPE_GROUP_FROM_OBJECT . '">';
18841904
// find already used items
18851905
$request = $DB->request([
18861906
'FROM' => PluginFormcreatorTarget_Actor::getTable(),
@@ -1909,7 +1929,7 @@ protected function showActorSettingsForType($actorType, array $actors) {
19091929
);
19101930
echo '</div>';
19111931

1912-
echo '<div id="block_' . $type . '_tech_group_from_object" style="display:none">';
1932+
echo '<div style="display:none" data-actor-type="' . $type . "_" . PluginFormcreatorTarget_Actor::ACTOR_TYPE_TECH_GROUP_FROM_OBJECT . '">';
19131933
// find already used items
19141934
$request = $DB->request([
19151935
'FROM' => PluginFormcreatorTarget_Actor::getTable(),
@@ -1938,7 +1958,7 @@ protected function showActorSettingsForType($actorType, array $actors) {
19381958
);
19391959
echo '</div>';
19401960

1941-
echo '<div id="block_' . $type . '_question_actors" style="display:none">';
1961+
echo '<div style="display:none" data-actor-type="' . $type . "_" . PluginFormcreatorTarget_Actor::ACTOR_TYPE_QUESTION_ACTORS . '">';
19421962
// find already used items
19431963
$request = $DB->request([
19441964
'FROM' => PluginFormcreatorTarget_Actor::getTable(),
@@ -1968,7 +1988,7 @@ protected function showActorSettingsForType($actorType, array $actors) {
19681988
echo '</div>';
19691989

19701990
if ($actorType == CommonITILActor::ASSIGN) {
1971-
echo '<div id="block_' . $type . '_supplier" style="display:none">';
1991+
echo '<div style="display:none" data-actor-type="' . $type . "_" . PluginFormcreatorTarget_Actor::ACTOR_TYPE_SUPPLIER . '">';
19721992
// find already used items
19731993
$request = $DB->request([
19741994
'FROM' => PluginFormcreatorTarget_Actor::getTable(),
@@ -1990,7 +2010,7 @@ protected function showActorSettingsForType($actorType, array $actors) {
19902010
]);
19912011
echo '</div>';
19922012

1993-
echo '<div id="block_' . $type . '_question_supplier" style="display:none">';
2013+
echo '<div style="display:none" data-actor-type="' . $type . "_" . PluginFormcreatorTarget_Actor::ACTOR_TYPE_QUESTION_SUPPLIER . '">';
19942014
// find already used items
19952015
$request = $DB->request([
19962016
'FROM' => PluginFormcreatorTarget_Actor::getTable(),
@@ -2021,6 +2041,19 @@ protected function showActorSettingsForType($actorType, array $actors) {
20212041
echo '</div>';
20222042
}
20232043

2044+
foreach (($PLUGIN_HOOKS['formcreator_actors_type'] ?? []) as $plugin => $classes) {
2045+
foreach ($classes as $plugin_target) {
2046+
if (!is_a($plugin_target, PluginFormcreatorPluginTargetInterface::class, true)) {
2047+
continue;
2048+
}
2049+
2050+
// Show custom form
2051+
echo '<div style="display:none" data-actor-type="' . $type . "_" . $plugin_target::getId() . '">';
2052+
echo $plugin_target::getForm($this->getForm());
2053+
echo '</div>';
2054+
}
2055+
}
2056+
20242057
echo '<div>';
20252058
echo __('Email followup');
20262059
Dropdown::showYesNo('use_notification', 1);
@@ -2035,11 +2068,11 @@ protected function showActorSettingsForType($actorType, array $actors) {
20352068

20362069
Html::closeForm();
20372070

2038-
$img_user = '<i class="fas fa-user" alt="' . __('User') . '" title="' . __('User') . '" width="20"></i>';
2039-
$img_group = '<i class="fas fa-users" alt="' . __('Group') . '" title="' . __('Group') . '" width="20"></i>';
2040-
$img_supplier = '<i class="fas fa-suitcase" alt="' . __('Supplier') . '" title="' . __('Supplier') . '" width="20"></i>';
2041-
$img_mail = '<i class="fas fa-envelope pointer" title="' . __('Email followup') . ' ' . __('Yes') . '" width="20"></i>';
2042-
$img_nomail = '<i class="fas fa-envelope pointer" title="' . __('Email followup') . ' ' . __('No') . '" width="20"></i>';
2071+
$img_user = static::getUserImage();
2072+
$img_group = static::getGroupImage();
2073+
$img_supplier = static::getSupplierImage();
2074+
$img_mail = static::getMailImage();
2075+
$img_nomail = static::getNoMailImage();
20432076

20442077
foreach ($actors[$actorRole] as $id => $values) {
20452078
echo '<div data-itemtype="PluginFormcreatorTarget_Actor" data-id="' . $id . '">';
@@ -2104,6 +2137,20 @@ protected function showActorSettingsForType($actorType, array $actors) {
21042137
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_AUTHORS_SUPERVISOR :
21052138
echo $img_user . ' <b>' . __('Form author\'s supervisor', 'formcreator') . '</b>';
21062139
break;
2140+
default:
2141+
foreach (($PLUGIN_HOOKS['formcreator_actors_type'] ?? []) as $plugin => $classes) {
2142+
foreach ($classes as $plugin_target) {
2143+
if (!is_a($plugin_target, PluginFormcreatorPluginTargetInterface::class, true)) {
2144+
continue;
2145+
}
2146+
2147+
if ($values['actor_type'] == $plugin_target::getId()) {
2148+
echo $plugin_target::getDisplayedValue($values['actor_value']);
2149+
break 2;
2150+
}
2151+
}
2152+
}
2153+
break;
21072154
}
21082155
echo $values['use_notification'] ? ' ' . $img_mail . ' ' : ' ' . $img_nomail . ' ';
21092156
echo $this->getDeleteImage();
@@ -2417,4 +2464,24 @@ public static function findForFormAnswer(PluginFormcreatorFormAnswer $formAnswer
24172464

24182465
return $targets;
24192466
}
2467+
2468+
public static function getUserImage() {
2469+
return '<i class="fas fa-user" alt="' . __('User') . '" title="' . __('User') . '" width="20"></i>';
2470+
}
2471+
2472+
public static function getGroupImage() {
2473+
return '<i class="fas fa-users" alt="' . __('Group') . '" title="' . __('Group') . '" width="20"></i>';
2474+
}
2475+
2476+
public static function getSupplierImage() {
2477+
return '<i class="fas fa-suitcase" alt="' . __('Supplier') . '" title="' . __('Supplier') . '" width="20"></i>';
2478+
}
2479+
2480+
public static function getMailImage() {
2481+
return '<i class="fas fa-envelope pointer" title="' . __('Email followup') . ' ' . __('Yes') . '" width="20"></i>';
2482+
}
2483+
2484+
public static function getNoMailImage() {
2485+
return '<i class="fas fa-envelope pointer" title="' . __('Email followup') . ' ' . __('No') . '" width="20"></i>';
2486+
}
24202487
}

inc/plugintargetinterface.class.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* ---------------------------------------------------------------------
4+
* Formcreator is a plugin which allows creation of custom forms of
5+
* easy access.
6+
* ---------------------------------------------------------------------
7+
* LICENSE
8+
*
9+
* This file is part of Formcreator.
10+
*
11+
* Formcreator is free software; you can redistribute it and/or modify
12+
* it under the terms of the GNU General Public License as published by
13+
* the Free Software Foundation; either version 2 of the License, or
14+
* (at your option) any later version.
15+
*
16+
* Formcreator is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU General Public License
22+
* along with Formcreator. If not, see <http://www.gnu.org/licenses/>.
23+
* ---------------------------------------------------------------------
24+
* @copyright Copyright © 2011 - 2021 Teclib'
25+
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+
26+
* @link https://github.com/pluginsGLPI/formcreator/
27+
* @link https://pluginsglpi.github.io/formcreator/
28+
* @link http://plugins.glpi-project.org/#/plugin/formcreator
29+
* ---------------------------------------------------------------------
30+
*/
31+
32+
if (!defined('GLPI_ROOT')) {
33+
die("Sorry. You can't access this file directly");
34+
}
35+
36+
interface PluginFormcreatorPluginTargetInterface
37+
{
38+
const ACTOR_TYPE_USER = 1;
39+
const ACTOR_TYPE_GROUP = 2;
40+
41+
public static function getId(): int;
42+
public static function getLabel(): string;
43+
public static function getForm(PluginFormcreatorForm $form): string;
44+
public static function getDisplayedValue($value): string;
45+
public static function getActorType(): int;
46+
public static function getActorId(PluginFormcreatorFormAnswer $formanswer, int $value): int;
47+
}

inc/target_actor.class.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ class PluginFormcreatorTarget_Actor extends CommonDBChild implements PluginFormc
6262
const ACTOR_ROLE_SUPPLIER = 4;
6363

6464
static function getEnumActorType() {
65-
return [
65+
global $PLUGIN_HOOKS;
66+
67+
$types = [
6668
self::ACTOR_TYPE_AUTHOR => __('Form author', 'formcreator'),
6769
self::ACTOR_TYPE_VALIDATOR => __('Form validator', 'formcreator'),
6870
self::ACTOR_TYPE_PERSON => __('Specific person', 'formcreator'),
@@ -76,6 +78,20 @@ static function getEnumActorType() {
7678
self::ACTOR_TYPE_QUESTION_ACTORS => __('Actors from the question', 'formcreator'),
7779
self::ACTOR_TYPE_AUTHORS_SUPERVISOR => __('Form author\'s supervisor', 'formcreator'),
7880
];
81+
82+
// Add extra plugin types
83+
foreach (($PLUGIN_HOOKS['formcreator_actors_type'] ?? []) as $plugin => $classes) {
84+
foreach ($classes as $plugin_target) {
85+
if (!is_a($plugin_target, PluginFormcreatorPluginTargetInterface::class, true)) {
86+
continue;
87+
}
88+
89+
$types[$plugin_target::getId()] = $plugin_target::getLabel();
90+
}
91+
}
92+
93+
asort($types);
94+
return $types;
7995
}
8096

8197
static function getEnumRole() {

js/scripts.js

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,28 +1222,8 @@ var plugin_formcreator = new function() {
12221222
}
12231223

12241224
this.changeActor = function(type, value) {
1225-
$('#block_' + type + '_user').hide();
1226-
$('#block_' + type + '_question_user').hide();
1227-
$('#block_' + type + '_group').hide();
1228-
$('#block_' + type + '_question_group').hide();
1229-
$('#block_' + type + '_group_from_object').hide();
1230-
$('#block_' + type + '_tech_group_from_object').hide();
1231-
$('#block_' + type + '_question_actors').hide();
1232-
$('#block_' + type + '_supplier').hide();
1233-
$('#block_' + type + '_question_supplier').hide();
1234-
1235-
// The numbers match PluginFormcreatorTarget_Actor::ACTOR_TYPE_* constants
1236-
switch (value) {
1237-
case '3' : $('#block_' + type + '_user').show(); break;
1238-
case '4' : $('#block_' + type + '_question_user').show(); break;
1239-
case '5' : $('#block_' + type + '_group').show(); break;
1240-
case '6' : $('#block_' + type + '_question_group').show(); break;
1241-
case '9' : $('#block_' + type + '_question_actors').show(); break;
1242-
case '7' : $('#block_' + type + '_supplier').show(); break;
1243-
case '8' : $('#block_' + type + '_question_supplier').show(); break;
1244-
case '10': $('#block_' + type + '_group_from_object').show(); break;
1245-
case '11': $('#block_' + type + '_tech_group_from_object').show(); break;
1246-
}
1225+
$('div[data-actor-type^=' + type + ']').hide();
1226+
$('div[data-actor-type=' + type + '_' + value + ']').show();
12471227
}
12481228

12491229
this.updateWizardFormsView = function (item) {
@@ -1295,31 +1275,6 @@ var plugin_formcreator = new function() {
12951275
);
12961276
}
12971277

1298-
this.changeActor = function(type, value) {
1299-
$('#block_' + type + '_user').hide();
1300-
$('#block_' + type + '_question_user').hide();
1301-
$('#block_' + type + '_group').hide();
1302-
$('#block_' + type + '_question_group').hide();
1303-
$('#block_' + type + '_group_from_object').hide();
1304-
$('#block_' + type + '_tech_group_from_object').hide();
1305-
$('#block_' + type + '_question_actors').hide();
1306-
$('#block_' + type + '_supplier').hide();
1307-
$('#block_' + type + '_question_supplier').hide();
1308-
1309-
// The numbers match PluginFormcreatorTarget_Actor::ACTOR_TYPE_* constants
1310-
switch (value) {
1311-
case '3' : $('#block_' + type + '_user').show(); break;
1312-
case '4' : $('#block_' + type + '_question_user').show(); break;
1313-
case '5' : $('#block_' + type + '_group').show(); break;
1314-
case '6' : $('#block_' + type + '_question_group').show(); break;
1315-
case '9' : $('#block_' + type + '_question_actors').show(); break;
1316-
case '7' : $('#block_' + type + '_supplier').show(); break;
1317-
case '8' : $('#block_' + type + '_question_supplier').show(); break;
1318-
case '10': $('#block_' + type + '_group_from_object').show(); break;
1319-
case '11': $('#block_' + type + '_tech_group_from_object').show(); break;
1320-
}
1321-
}
1322-
13231278
this.deleteActor = function (item) {
13241279
var item = item.closest('div[data-itemtype="PluginFormcreatorTarget_Actor"][data-id]');
13251280
var id = item.getAttribute('data-id');

0 commit comments

Comments
 (0)