Skip to content

Commit

Permalink
feat(targetticket): link to a ticket from a question
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Nov 3, 2021
1 parent 8a901f4 commit a563d11
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 46 deletions.
50 changes: 49 additions & 1 deletion inc/composite.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,38 @@

class PluginFormcreatorComposite
{
/**
* Undocumented variable
*
* @var PluginFormcreatorItem_TargetTicket
*/
private $item_targetTicket;

/**
* Undocumented variable
*
* @var PluginFormcreatorFormAnswer
*/
private $targets = [];

/**
* Undocumented variable
*
* @var Ticket_Ticket
*/
private $ticket_ticket;

public function __construct(PluginFormcreatorItem_TargetTicket $item_targetTicket, Ticket_Ticket $ticket_ticket) {
/**
* Undocumented variable
*
* @var PluginFormcreatorFormAnswer
*/
private $formAnswer;

public function __construct(PluginFormcreatorItem_TargetTicket $item_targetTicket, Ticket_Ticket $ticket_ticket, PluginFormcreatorFormAnswer $formAnswer) {
$this->item_targetTicket = $item_targetTicket;
$this->ticket_ticket = $ticket_ticket;
$this->formAnswer = $formAnswer;
}

/**
Expand Down Expand Up @@ -101,6 +124,31 @@ public function buildCompositeRelations() {
]);
}
break;

case PluginFormcreatorQuestion::class:
// Check the answer matches a question of type GLPI Object / Ticket
$question = PluginFormcreatorQuestion::getById($row['items_id']);
if (!($question instanceof PluginFormcreatorQuestion)) {
break;
}
/** @var PluginFormcreatorQuestion $question */
if (strpos($question->fields['values'], '"itemtype":"Ticket"') === false) {
break;
}
$answer = new PluginFormcreatorAnswer();
$answer->getFromDBByCrit([
PluginFormcreatorQuestion::getForeignKeyField() => $row['items_id'],
PluginFormcreatorFormAnswer::getForeignKeyField() => $this->formAnswer->getID(),
]);
if ($answer->isNewItem()) {
break;
}
$this->ticket_ticket->add([
'link' => $row['link'],
'tickets_id_1' => $generatedObject->getID(),
'tickets_id_2' => $answer->fields['answer'],
]);
break;
}
}
}
Expand Down
72 changes: 40 additions & 32 deletions inc/field/dropdownfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,42 +285,50 @@ public function buildParams($rand = null) {

case Ticket::class:
// Shall match logic in \Search::getDefaultWhere()
if (!Session::haveRight("ticket", Ticket::READALL)) {
$currentUser = Session::getLoginUserID();
if (Session::haveRight(Ticket::$rightname, Ticket::READMY)) {
$requestersObserversQuery = new QuerySubQuery([
'SELECT' => 'tickets_id',
'FROM' => Ticket_User::getTable(),
'WHERE' => [
'users_id' => $currentUser,
'type' => [CommonITILActor::REQUESTER, CommonITILActor::OBSERVER]
],
]);
$dparams_cond_crit['OR'] = [
if (Session::haveRight("ticket", Ticket::READALL)) {
break;
}
$currentUser = Session::getLoginUserID();
if (!Session::haveRight(Ticket::$rightname, Ticket::READMY) && !Session::haveRight(Ticket::$rightname, Ticket::READGROUP)) {
// No right to view any ticket, then force the dropdown to be empty
$dparams_cond_crit['OR'] = new \QueryExpression('0=1');
break;
}
if (Session::haveRight(Ticket::$rightname, Ticket::READMY)) {
$requestersObserversQuery = new QuerySubQuery([
'SELECT' => 'tickets_id',
'FROM' => Ticket_User::getTable(),
'WHERE' => [
'users_id' => $currentUser,
'type' => [CommonITILActor::REQUESTER, CommonITILActor::OBSERVER]
],
]);
$dparams_cond_crit['OR'] = [
[
'id' => $requestersObserversQuery,
'users_id_recipient' => $currentUser,
],
];
}
if (Session::haveRight(Ticket::$rightname, Ticket::READGROUP)) {
$requestersObserversGroupsQuery = new QuerySubQuery([
'SELECT' => 'tickets_id',
'FROM' => Group_Ticket::getTable(),
'WHERE' => [
'groups_id' => $_SESSION['glpigroups'],
'type' => [CommonITILActor::REQUESTER, CommonITILActor::OBSERVER]
],
]);
if (!isset($dparams_cond_crit['OR'])) {
$dparams_cond_crit['OR'] = [
'id' => $requestersObserversGroupsQuery,
];
} else {
$dparams_cond_crit['OR'][] = [
'id' => $requestersObserversGroupsQuery,
'users_id_recipient' => $currentUser,
];
}
if (Session::haveRight(Ticket::$rightname, Ticket::READGROUP)) {
$requestersObserversGroupsQuery = new QuerySubQuery([
'SELECT' => 'tickets_id',
'FROM' => Group_Ticket::getTable(),
'WHERE' => [
'groups_id' => $_SESSION['glpigroups'],
'type' => [CommonITILActor::REQUESTER, CommonITILActor::OBSERVER]
],
]);
if (!isset($dparams_cond_crit['OR']['id'])) {
$dparams_cond_crit['OR'] = [
'id' => $requestersObserversGroupsQuery,
];
} else {
$dparams_cond_crit['OR']['id'] = new QueryUnion([
$dparams_cond_crit['OR']['id'],
$requestersObserversGroupsQuery,
]);
}
}
}
break;

Expand Down
2 changes: 1 addition & 1 deletion inc/formanswer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ public function generateTarget(): bool {
$this->deserializeAnswers();

// Generate targets
$generatedTargets = new PluginFormcreatorComposite(new PluginFormcreatorItem_TargetTicket(), new Ticket_Ticket());
$generatedTargets = new PluginFormcreatorComposite(new PluginFormcreatorItem_TargetTicket(), new Ticket_Ticket(), $this);
foreach ($all_targets as $targets) {
foreach ($targets as $targetObject) {
// Check the condition of the target
Expand Down
38 changes: 33 additions & 5 deletions inc/targetticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,14 @@ protected function showCompositeTicketSettings($rand) {
echo '<div style="display: none" id="plugin_formcreator_linked_items' . $rand . '">';
Ticket_Ticket::dropdownLinks('_linktype');
$elements = [
'PluginFormcreatorTargetTicket' => __('An other destination of this form', 'formcreator'),
'Ticket' => __('An existing ticket', 'formcreator'),
PluginFormcreatorTargetTicket::class => __('An other destination of this form', 'formcreator'),
Ticket::class => __('An existing ticket', 'formcreator'),
PluginFormcreatorQuestion::class => __('A ticket from an answer to a question'),
];
Dropdown::showFromArray('_link_itemtype', $elements, [
'on_change' => "plugin_formcreator_updateCompositePeerType($rand)",
'on_change' => "plugin_formcreator_updateCompositePeerType(this)",
'rand' => $rand,
]);
echo Html::scriptBlock("plugin_formcreator_updateCompositePeerType($rand);");
// get already linked items
$targetTicketId = $this->getID();
$rows = $DB->request([
Expand All @@ -345,7 +345,7 @@ protected function showCompositeTicketSettings($rand) {
}
}

echo '<span id="plugin_formcreator_link_ticket">';
echo '<span id="plugin_formcreator_link_ticket" style="display: none">';
$linkparam = [
'name' => '_link_tickets_id',
'rand' => $rand,
Expand All @@ -370,6 +370,20 @@ protected function showCompositeTicketSettings($rand) {
'condition' => $condition,
]);
echo '</span>';

// dropdown of questions of type GLPI Object / Ticket
echo '<span id="plugin_formcreator_link_question" style="display: none">';
PluginFormcreatorQuestion::dropdownForForm(
$this->getForm()->getID(),
[
'fieldtype' => ['glpiselect'],
'values' => ['LIKE', '%"itemtype":"' . Ticket::class . '"%'],
],
'_link_plugin_formcreator_questions_id',
$this->fields['destination_entity_value']
);
echo '</span>';

echo '</div>';

// show already linked items
Expand Down Expand Up @@ -404,6 +418,16 @@ protected function showCompositeTicketSettings($rand) {
echo ' ';
echo $icons;
break;

case PluginFormcreatorQuestion::getType():
echo Ticket_Ticket::getLinkName($row['link']);
echo ' ';
echo $itemtype::getTypeName();
echo ' ';
echo '<span style="font-weight:bold">' . $item->getField('name') . '</span>';
echo ' ';
echo $icons;
break;
}
echo '<br>';
}
Expand Down Expand Up @@ -616,6 +640,10 @@ private function saveLinkedItem($input) {
$itemId = (int) $input['_link_targettickets_id'];
break;

case PluginFormcreatorQuestion::getType():
$itemId = (int) $input['_link_plugin_formcreator_questions_id'];
break;

default:
Session::addMessageAfterRedirect(__('Invalid linked item type', 'formcreator'), false, ERROR);
return [];
Expand Down
22 changes: 15 additions & 7 deletions js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -1788,13 +1788,21 @@ function plugin_formceator_showPictogram(id, preview) {
/**
* update composite ticket (links between tickets) in target ticket (design mode)
*/
function plugin_formcreator_updateCompositePeerType(rand) {
if ($('#dropdown__link_itemtype' + rand).val() == 'Ticket') {
$('#plugin_formcreator_link_ticket').show();
$('#plugin_formcreator_link_target').hide();
} else {
$('#plugin_formcreator_link_ticket').hide();
$('#plugin_formcreator_link_target').show();
function plugin_formcreator_updateCompositePeerType(type) {
$('#plugin_formcreator_link_ticket').hide();
$('#plugin_formcreator_link_target').hide();
$('#plugin_formcreator_link_question').hide();

switch ($(type).val()) {
case 'Ticket':
$('#plugin_formcreator_link_ticket').show();
break;
case 'PluginFormcreatorTargetTicket':
$('#plugin_formcreator_link_target').show();
break;
case 'PluginFormcreatorQuestion':
$('#plugin_formcreator_link_question').show();
break;
}
}

Expand Down

0 comments on commit a563d11

Please sign in to comment.