Skip to content

Commit

Permalink
feat(question): new hooks for other plugins interaction (#3093)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienClairembault authored Nov 24, 2022
1 parent 810c854 commit f9a23b6
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 64 deletions.
8 changes: 8 additions & 0 deletions inc/abstractfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,17 @@ public function show(string $domain, bool $canEdit = true): string {
continue;
}
}
$description = Plugin::doHookFunction('formcreator_question_description', [
'description' => $description,
'question' => $this->getQuestion()
])['description'];
$html .= '<div class="help-block">' . html_entity_decode(__($description, $domain)) . '</div>';
}
$html .= '<div class="form_field">';
$this->value = Plugin::doHookFunction('formcreator_question_default_value', [
'value' => $this->value,
'question' => $this->getQuestion()
])['value'];
$html .= $this->getRenderedHtml($domain, $canEdit);
$html .= '</div>';

Expand Down
99 changes: 83 additions & 16 deletions inc/abstractitiltarget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ protected function setTargetUrgency($data, $formanswer) {
* find all actors and prepare data for the ticket being created
*/
protected function prepareActors(PluginFormcreatorForm $form, PluginFormcreatorFormAnswer $formanswer) {
global $DB;
global $DB, $PLUGIN_HOOKS;

$rows = $DB->request([
'FROM' => PluginFormcreatorTarget_Actor::getTable(),
Expand Down Expand Up @@ -586,6 +586,26 @@ protected function prepareActors(PluginFormcreatorForm $form, PluginFormcreatorF
$this->addActor(PluginFormcreatorTarget_Actor::ACTOR_ROLE_SUPPLIER, $userId, $notify);
}
break;
default:
foreach (($PLUGIN_HOOKS['formcreator_actors_type'] ?? []) as $plugin => $classes) {
foreach ($classes as $plugin_target) {
if (!is_a($plugin_target, PluginFormcreatorPluginTargetInterface::class, true)) {
continue;
}
if ($actor['actor_type']== $plugin_target::getId()) {
$value = $plugin_target::getActorId($formanswer, $actor['actor_value']);
if ($value) {
if ($plugin_target::getActorType() == PluginFormcreatorPluginTargetInterface::ACTOR_TYPE_USER) {
$this->addActor($actor['actor_role'], $value, $notify);
} else if (PluginFormcreatorPluginTargetInterface::ACTOR_TYPE_GROUP) {
$this->addGroupActor($actor['actor_role'], $value);
}
}
break 2;
}
}
}
break;
}
}
}
Expand Down Expand Up @@ -1760,7 +1780,7 @@ protected function showActorSettingsHeader($type) {
* @return void
*/
protected function showActorSettingsForType($actorType, array $actors) {
global $DB;
global $DB, $PLUGIN_HOOKS;

$itemActor = new PluginFormcreatorTarget_Actor();
$dropdownItems = ['' => Dropdown::EMPTY_VALUE] + $itemActor::getEnumActorType();
Expand Down Expand Up @@ -1799,21 +1819,21 @@ protected function showActorSettingsForType($actorType, array $actors) {
]
);

echo '<div id="block_' . $type . '_user" style="display:none">';
echo '<div style="display:none" data-actor-type="' . $type . "_" . PluginFormcreatorTarget_Actor::ACTOR_TYPE_PERSON . '">';
User::dropdown([
'name' => 'actor_value_' . PluginFormcreatorTarget_Actor::ACTOR_TYPE_PERSON,
'right' => 'all',
'all' => 0,
]);
echo '</div>';

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

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

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

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

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

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

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

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

foreach (($PLUGIN_HOOKS['formcreator_actors_type'] ?? []) as $plugin => $classes) {
foreach ($classes as $plugin_target) {
if (!is_a($plugin_target, PluginFormcreatorPluginTargetInterface::class, true)) {
continue;
}

// Show custom form
echo '<div style="display:none" data-actor-type="' . $type . "_" . $plugin_target::getId() . '">';
echo $plugin_target::getForm($this->getForm());
echo '</div>';
}
}

echo '<div>';
echo __('Email followup');
Dropdown::showYesNo('use_notification', 1);
Expand All @@ -2035,11 +2068,11 @@ protected function showActorSettingsForType($actorType, array $actors) {

Html::closeForm();

$img_user = '<i class="fas fa-user" alt="' . __('User') . '" title="' . __('User') . '" width="20"></i>';
$img_group = '<i class="fas fa-users" alt="' . __('Group') . '" title="' . __('Group') . '" width="20"></i>';
$img_supplier = '<i class="fas fa-suitcase" alt="' . __('Supplier') . '" title="' . __('Supplier') . '" width="20"></i>';
$img_mail = '<i class="fas fa-envelope pointer" title="' . __('Email followup') . ' ' . __('Yes') . '" width="20"></i>';
$img_nomail = '<i class="fas fa-envelope pointer" title="' . __('Email followup') . ' ' . __('No') . '" width="20"></i>';
$img_user = static::getUserImage();
$img_group = static::getGroupImage();
$img_supplier = static::getSupplierImage();
$img_mail = static::getMailImage();
$img_nomail = static::getNoMailImage();

foreach ($actors[$actorRole] as $id => $values) {
echo '<div data-itemtype="PluginFormcreatorTarget_Actor" data-id="' . $id . '">';
Expand Down Expand Up @@ -2104,6 +2137,20 @@ protected function showActorSettingsForType($actorType, array $actors) {
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_AUTHORS_SUPERVISOR :
echo $img_user . ' <b>' . __('Form author\'s supervisor', 'formcreator') . '</b>';
break;
default:
foreach (($PLUGIN_HOOKS['formcreator_actors_type'] ?? []) as $plugin => $classes) {
foreach ($classes as $plugin_target) {
if (!is_a($plugin_target, PluginFormcreatorPluginTargetInterface::class, true)) {
continue;
}

if ($values['actor_type'] == $plugin_target::getId()) {
echo $plugin_target::getDisplayedValue($values['actor_value']);
break 2;
}
}
}
break;
}
echo $values['use_notification'] ? ' ' . $img_mail . ' ' : ' ' . $img_nomail . ' ';
echo $this->getDeleteImage();
Expand Down Expand Up @@ -2417,4 +2464,24 @@ public static function findForFormAnswer(PluginFormcreatorFormAnswer $formAnswer

return $targets;
}

public static function getUserImage() {
return '<i class="fas fa-user" alt="' . __('User') . '" title="' . __('User') . '" width="20"></i>';
}

public static function getGroupImage() {
return '<i class="fas fa-users" alt="' . __('Group') . '" title="' . __('Group') . '" width="20"></i>';
}

public static function getSupplierImage() {
return '<i class="fas fa-suitcase" alt="' . __('Supplier') . '" title="' . __('Supplier') . '" width="20"></i>';
}

public static function getMailImage() {
return '<i class="fas fa-envelope pointer" title="' . __('Email followup') . ' ' . __('Yes') . '" width="20"></i>';
}

public static function getNoMailImage() {
return '<i class="fas fa-envelope pointer" title="' . __('Email followup') . ' ' . __('No') . '" width="20"></i>';
}
}
47 changes: 47 additions & 0 deletions inc/plugintargetinterface.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* ---------------------------------------------------------------------
* Formcreator is a plugin which allows creation of custom forms of
* easy access.
* ---------------------------------------------------------------------
* LICENSE
*
* This file is part of Formcreator.
*
* Formcreator is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Formcreator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Formcreator. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------
* @copyright Copyright © 2011 - 2021 Teclib'
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+
* @link https://github.com/pluginsGLPI/formcreator/
* @link https://pluginsglpi.github.io/formcreator/
* @link http://plugins.glpi-project.org/#/plugin/formcreator
* ---------------------------------------------------------------------
*/

if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}

interface PluginFormcreatorPluginTargetInterface
{
const ACTOR_TYPE_USER = 1;
const ACTOR_TYPE_GROUP = 2;

public static function getId(): int;
public static function getLabel(): string;
public static function getForm(PluginFormcreatorForm $form): string;
public static function getDisplayedValue($value): string;
public static function getActorType(): int;
public static function getActorId(PluginFormcreatorFormAnswer $formanswer, int $value): int;
}
18 changes: 17 additions & 1 deletion inc/target_actor.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ class PluginFormcreatorTarget_Actor extends CommonDBChild implements PluginFormc
const ACTOR_ROLE_SUPPLIER = 4;

static function getEnumActorType() {
return [
global $PLUGIN_HOOKS;

$types = [
self::ACTOR_TYPE_AUTHOR => __('Form author', 'formcreator'),
self::ACTOR_TYPE_VALIDATOR => __('Form validator', 'formcreator'),
self::ACTOR_TYPE_PERSON => __('Specific person', 'formcreator'),
Expand All @@ -76,6 +78,20 @@ static function getEnumActorType() {
self::ACTOR_TYPE_QUESTION_ACTORS => __('Actors from the question', 'formcreator'),
self::ACTOR_TYPE_AUTHORS_SUPERVISOR => __('Form author\'s supervisor', 'formcreator'),
];

// Add extra plugin types
foreach (($PLUGIN_HOOKS['formcreator_actors_type'] ?? []) as $plugin => $classes) {
foreach ($classes as $plugin_target) {
if (!is_a($plugin_target, PluginFormcreatorPluginTargetInterface::class, true)) {
continue;
}

$types[$plugin_target::getId()] = $plugin_target::getLabel();
}
}

asort($types);
return $types;
}

static function getEnumRole() {
Expand Down
49 changes: 2 additions & 47 deletions js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -1222,28 +1222,8 @@ var plugin_formcreator = new function() {
}

this.changeActor = function(type, value) {
$('#block_' + type + '_user').hide();
$('#block_' + type + '_question_user').hide();
$('#block_' + type + '_group').hide();
$('#block_' + type + '_question_group').hide();
$('#block_' + type + '_group_from_object').hide();
$('#block_' + type + '_tech_group_from_object').hide();
$('#block_' + type + '_question_actors').hide();
$('#block_' + type + '_supplier').hide();
$('#block_' + type + '_question_supplier').hide();

// The numbers match PluginFormcreatorTarget_Actor::ACTOR_TYPE_* constants
switch (value) {
case '3' : $('#block_' + type + '_user').show(); break;
case '4' : $('#block_' + type + '_question_user').show(); break;
case '5' : $('#block_' + type + '_group').show(); break;
case '6' : $('#block_' + type + '_question_group').show(); break;
case '9' : $('#block_' + type + '_question_actors').show(); break;
case '7' : $('#block_' + type + '_supplier').show(); break;
case '8' : $('#block_' + type + '_question_supplier').show(); break;
case '10': $('#block_' + type + '_group_from_object').show(); break;
case '11': $('#block_' + type + '_tech_group_from_object').show(); break;
}
$('div[data-actor-type^=' + type + ']').hide();
$('div[data-actor-type=' + type + '_' + value + ']').show();
}

this.updateWizardFormsView = function (item) {
Expand Down Expand Up @@ -1295,31 +1275,6 @@ var plugin_formcreator = new function() {
);
}

this.changeActor = function(type, value) {
$('#block_' + type + '_user').hide();
$('#block_' + type + '_question_user').hide();
$('#block_' + type + '_group').hide();
$('#block_' + type + '_question_group').hide();
$('#block_' + type + '_group_from_object').hide();
$('#block_' + type + '_tech_group_from_object').hide();
$('#block_' + type + '_question_actors').hide();
$('#block_' + type + '_supplier').hide();
$('#block_' + type + '_question_supplier').hide();

// The numbers match PluginFormcreatorTarget_Actor::ACTOR_TYPE_* constants
switch (value) {
case '3' : $('#block_' + type + '_user').show(); break;
case '4' : $('#block_' + type + '_question_user').show(); break;
case '5' : $('#block_' + type + '_group').show(); break;
case '6' : $('#block_' + type + '_question_group').show(); break;
case '9' : $('#block_' + type + '_question_actors').show(); break;
case '7' : $('#block_' + type + '_supplier').show(); break;
case '8' : $('#block_' + type + '_question_supplier').show(); break;
case '10': $('#block_' + type + '_group_from_object').show(); break;
case '11': $('#block_' + type + '_tech_group_from_object').show(); break;
}
}

this.deleteActor = function (item) {
var item = item.closest('div[data-itemtype="PluginFormcreatorTarget_Actor"][data-id]');
var id = item.getAttribute('data-id');
Expand Down

0 comments on commit f9a23b6

Please sign in to comment.