Skip to content

Commit

Permalink
feat(targetticket,targetchange): set a group from an object from a qu…
Browse files Browse the repository at this point in the history
…estion

Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Jan 29, 2020
1 parent 2ca5498 commit 0461a91
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 8 deletions.
4 changes: 4 additions & 0 deletions inc/target_actor.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ abstract class PluginFormcreatorTarget_Actor extends CommonDBChild implements Pl
const ACTOR_TYPE_SUPPLIER = 7;
const ACTOR_TYPE_QUESTION_SUPPLIER = 8;
const ACTOR_TYPE_QUESTION_ACTORS = 9;
const ACTOR_TYPE_GROUP_FROM_OBJECT = 10;

const ACTOR_ROLE_REQUESTER = 1;
const ACTOR_ROLE_OBSERVER = 2;
Expand All @@ -60,6 +61,7 @@ static function getEnumActorType() {
self::ACTOR_TYPE_QUESTION_PERSON => __('Person from the question', 'formcreator'),
self::ACTOR_TYPE_GROUP => __('Specific group', 'formcreator'),
self::ACTOR_TYPE_QUESTION_GROUP => __('Group from the question', 'formcreator'),
self::ACTOR_TYPE_GROUP_FROM_OBJECT => __('Group from an object', 'formcreator'),
self::ACTOR_TYPE_SUPPLIER => __('Specific supplier', 'formcreator'),
self::ACTOR_TYPE_QUESTION_SUPPLIER => __('Supplier from the question', 'formcreator'),
self::ACTOR_TYPE_QUESTION_ACTORS => __('Actors from the question', 'formcreator'),
Expand Down Expand Up @@ -111,6 +113,7 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con
case self::ACTOR_TYPE_QUESTION_PERSON :
case self::ACTOR_TYPE_QUESTION_GROUP :
case self::ACTOR_TYPE_QUESTION_SUPPLIER :
case self::ACTOR_TYPE_GROUP_FROM_OBJECT :
$question = $linker->getObject($input['actor_value'], PluginFormcreatorQuestion::class);
if ($question === false) {
$linker->postpone($input[$idKey], $item->getType(), $input, $containerId);
Expand Down Expand Up @@ -193,6 +196,7 @@ public function export($remove_uuid = false) {
case self::ACTOR_TYPE_QUESTION_GROUP:
case self::ACTOR_TYPE_SUPPLIER:
case self::ACTOR_TYPE_QUESTION_ACTORS:
case self::ACTOR_TYPE_GROUP_FROM_OBJECT:
$question = new PluginFormcreatorQuestion;
if ($question->getFromDB($target_actor['actor_value'])) {
$target_actor['actor_value'] = $question->fields['uuid'];
Expand Down
114 changes: 106 additions & 8 deletions inc/targetbase.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,17 +442,14 @@ protected function prepareActors(PluginFormcreatorForm $form, PluginFormcreatorF
switch ($actor['actor_type']) {
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_CREATOR :
$userIds = [$formanswer->fields['requester_id']];
$notify = $actor['use_notification'];
break;
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_VALIDATOR :
$userIds = [$_SESSION['glpiID']];
$notify = $actor['use_notification'];
break;
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_PERSON :
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_GROUP :
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_SUPPLIER :
$userIds = [$actor['actor_value']];
$notify = $actor['use_notification'];
break;
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_QUESTION_PERSON :
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_QUESTION_GROUP :
Expand All @@ -462,7 +459,7 @@ protected function prepareActors(PluginFormcreatorForm $form, PluginFormcreatorF
$formanswerId = $formanswer->getID();
$answer->getFromDBByCrit([
'AND' => [
'plugin_formcreator_questions_id' => $actorValue,
'plugin_formcreator_questions_id' => $actorValue,
'plugin_formcreator_formanswers_id' => $formanswerId
]
]);
Expand All @@ -472,15 +469,14 @@ protected function prepareActors(PluginFormcreatorForm $form, PluginFormcreatorF
} else {
$userIds = [$answer->fields['answer']];
}
$notify = $actor['use_notification'];
break;
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_QUESTION_ACTORS:
$answer = new PluginFormcreatorAnswer();
$actorValue = $actor['actor_value'];
$formanswerId = $formanswer->getID();
$answer->getFromDBByCrit([
'AND' => [
'plugin_formcreator_questions_id' => $actorValue,
'plugin_formcreator_questions_id' => $actorValue,
'plugin_formcreator_formanswers_id' => $formanswerId
]
]);
Expand All @@ -490,9 +486,53 @@ protected function prepareActors(PluginFormcreatorForm $form, PluginFormcreatorF
} else {
$userIds = json_decode($answer->fields['answer'], JSON_OBJECT_AS_ARRAY);
}
$notify = $actor['use_notification'];
break;
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_GROUP_FROM_OBJECT:
// Get the object from the question
$answer = new PluginFormcreatorAnswer();
$actorValue = $actor['actor_value'];
$formanswerId = $formanswer->getID();
$answer->getFromDBByCrit([
'AND' => [
'plugin_formcreator_questions_id' => $actorValue,
'plugin_formcreator_formanswers_id' => $formanswerId
]
]);
if ($answer->isNewItem()) {
continue 2;
}
// Get the itemtype of the object
$question = new PluginFormcreatorQuestion();
$question->getFromDB($answer->fields[PluginFormcreatorQuestion::getForeignKeyField()]);
if ($question->isNewItem()) {
continue 2;
}
$itemtype = $question->fields['values'];
if (!is_subclass_of($itemtype, CommonDBTM::class)) {
continue 2;
}

// Check the object has a group FK
$groupFk = Group::getForeignKeyField();
$object = new $itemtype();
if (!$DB->fieldExists($object->getTable(), $groupFk)) {
continue 2;
}

// get the group
if (!$object->getFromDB($answer->fields['answer'])) {
continue 2;
}

// ignore invalid ID
if (Group::isNewId($object->fields[$groupFk])) {
continue 2;
}

$userIds = [$object->fields[$groupFk]];
break;
}
$notify = $actor['use_notification'];

switch ($actor['actor_type']) {
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_CREATOR :
Expand All @@ -506,7 +546,8 @@ protected function prepareActors(PluginFormcreatorForm $form, PluginFormcreatorF
break;
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_GROUP :
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_QUESTION_GROUP :
foreach ($userIds as $groupId) {
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_GROUP_FROM_OBJECT:
foreach ($userIds as $groupId) {
$this->addGroupActor($actor['actor_role'], $groupId);
}
break;
Expand Down Expand Up @@ -1104,6 +1145,19 @@ protected function showActorsSettings() {
);
echo '</div>';

echo '<div id="block_requester_group_from_object" style="display:none">';
PluginFormcreatorQuestion::dropdownForForm(
$this->getForm()->getID(),
[
'fieldtype' => ['glpiselect'],
],
'actor_value_' . PluginFormcreatorTarget_Actor::ACTOR_TYPE_GROUP_FROM_OBJECT,
[
'value' => 0
]
);
echo '</div>';

echo '<div id="block_requester_question_actors" style="display:none">';
PluginFormcreatorQuestion::dropdownForForm(
$this->getForm()->getID(),
Expand Down Expand Up @@ -1164,6 +1218,12 @@ protected function showActorsSettings() {
echo $img_group . ' <b>' . __('Group from the question', 'formcreator')
. '</b> "' . $question->getName() . '"';
break;
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_GROUP_FROM_OBJECT:
$question = new PluginFormcreatorQuestion();
$question->getFromDB($values['actor_value']);
echo $img_group . ' <b>' . __('Group from the object', 'formcreator')
. '</b> "' . $question->getName() . '"';
break;
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_QUESTION_ACTORS:
$question = new PluginFormcreatorQuestion();
$question->getFromDB($values['actor_value']);
Expand Down Expand Up @@ -1241,6 +1301,19 @@ protected function showActorsSettings() {
);
echo '</div>';

echo '<div id="block_watcher_group_from_object" style="display:none">';
PluginFormcreatorQuestion::dropdownForForm(
$this->getForm()->getID(),
[
'fieldtype' => ['glpiselect'],
],
'actor_value_' . PluginFormcreatorTarget_Actor::ACTOR_TYPE_GROUP_FROM_OBJECT,
[
'value' => 0
]
);
echo '</div>';

echo '<div id="block_watcher_question_actors" style="display:none">';
PluginFormcreatorQuestion::dropdownForForm(
$this->getForm()->getID(),
Expand Down Expand Up @@ -1301,6 +1374,12 @@ protected function showActorsSettings() {
echo $img_group . ' <b>' . __('Group from the question', 'formcreator')
. '</b> "' . $question->getName() . '"';
break;
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_GROUP_FROM_OBJECT:
$question = new PluginFormcreatorQuestion();
$question->getFromDB($values['actor_value']);
echo $img_group . ' <b>' . __('Group from the object', 'formcreator')
. '</b> "' . $question->getName() . '"';
break;
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_QUESTION_ACTORS :
$question = new PluginFormcreatorQuestion();
$question->getFromDB($values['actor_value']);
Expand Down Expand Up @@ -1385,6 +1464,19 @@ protected function showActorsSettings() {
);
echo '</div>';

echo '<div id="block_assigned_group_from_object" style="display:none">';
PluginFormcreatorQuestion::dropdownForForm(
$this->getForm()->getID(),
[
'fieldtype' => ['glpiselect'],
],
'actor_value_' . PluginFormcreatorTarget_Actor::ACTOR_TYPE_GROUP_FROM_OBJECT,
[
'value' => 0
]
);
echo '</div>';

echo '<div id="block_assigned_question_actors" style="display:none">';
PluginFormcreatorQuestion::dropdownForForm(
$this->getForm()->getID(),
Expand Down Expand Up @@ -1459,6 +1551,12 @@ protected function showActorsSettings() {
echo $img_group . ' <b>' . __('Group from the question', 'formcreator')
. '</b> "' . $question->getName() . '"';
break;
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_GROUP_FROM_OBJECT:
$question = new PluginFormcreatorQuestion();
$question->getFromDB($values['actor_value']);
echo $img_group . ' <b>' . __('Group from the object', 'formcreator')
. '</b> "' . $question->getName() . '"';
break;
case PluginFormcreatorTarget_Actor::ACTOR_TYPE_QUESTION_ACTORS :
$question = new PluginFormcreatorQuestion();
$question->getFromDB($values['actor_value']);
Expand Down
6 changes: 6 additions & 0 deletions js/scripts.js.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,7 @@ function plugin_formcreator_ChangeActorRequester(value) {
$('#block_requester_question_user').hide();
$('#block_requester_group').hide();
$('#block_requester_question_group').hide();
$('#block_requester_group_from_object').hide();
$('#block_requester_question_actors').hide();
$('#block_requester_supplier').hide();
$('#block_requester_question_supplier').hide();
Expand All @@ -723,6 +724,7 @@ function plugin_formcreator_ChangeActorRequester(value) {
case '4' : $('#block_requester_question_user').show(); break;
case '5' : $('#block_requester_group').show(); break;
case '6' : $('#block_requester_question_group').show(); break;
case '10': $('#block_requester_group_from_object').show(); break;
case '9' : $('#block_requester_question_actors').show(); break;
case '7' : $('#block_requester_supplier').show(); break;
case '8' : $('#block_requester_question_supplier').show(); break;
Expand All @@ -734,6 +736,7 @@ function plugin_formcreator_ChangeActorWatcher(value) {
$('#block_watcher_question_user').hide();
$('#block_watcher_group').hide();
$('#block_watcher_question_group').hide();
$('#block_watcher_group_from_object').hide();
$('#block_watcher_question_actors').hide();
$('#block_watcher_supplier').hide();
$('#block_watcher_question_supplier').hide();
Expand All @@ -744,6 +747,7 @@ function plugin_formcreator_ChangeActorWatcher(value) {
case '5' : $('#block_watcher_group').show(); break;
case '6' : $('#block_watcher_question_group').show(); break;
case '9' : $('#block_watcher_question_actors').show(); break;
case '10': $('#block_watcher_group_from_object').show(); break;
case '7' : $('#block_watcher_supplier').show(); break;
case '8' : $('#block_watcher_question_supplier').show(); break;
}
Expand All @@ -754,6 +758,7 @@ function plugin_formcreator_ChangeActorAssigned(value) {
$('#block_assigned_question_user').hide();
$('#block_assigned_group').hide();
$('#block_assigned_question_group').hide();
$('#block_assigned_group_from_object').hide();
$('#block_assigned_question_actors').hide();
$('#block_assigned_supplier').hide();
$('#block_assigned_question_supplier').hide();
Expand All @@ -765,6 +770,7 @@ function plugin_formcreator_ChangeActorAssigned(value) {
case '5' : $('#block_assigned_group').show(); break;
case '6' : $('#block_assigned_question_group').show(); break;
case '9' : $('#block_assigned_question_actors').show(); break;
case '10': $('#block_assigned_group_from_object').show(); break;
case '7' : $('#block_assigned_supplier').show(); break;
case '8' : $('#block_assigned_question_supplier').show(); break;
}
Expand Down

0 comments on commit 0461a91

Please sign in to comment.