Skip to content

Commit

Permalink
fix(issue): sync issues problem when a ticket has several validators …
Browse files Browse the repository at this point in the history
…(backport 2.12) (#2971)

the HAVING clause of the 3rd part of the UNION counts the number of validator rows
becasue of the JOIN with ticket validators
  • Loading branch information
btry authored Oct 24, 2022
1 parent e6986b0 commit e301159
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 76 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"php": "7.2"
},
"optimize-autoloader": true,
"apcu-autoloader": true
"apcu-autoloader": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"require": {
"php": ">= 7.2",
Expand Down
59 changes: 51 additions & 8 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,42 @@ function plugin_formcreator_addDefaultJoin($itemtype, $ref_table, &$already_link
case PluginFormcreatorIssue::class:
// Get default joins for tickets
$join = Search::addDefaultJoin(Ticket::getType(), Ticket::getTable(), $already_link_tables);
$join .= Search::addLeftJoin($itemtype, $ref_table, $already_link_tables, Group::getTable(), 'groups_id_validator');
// $join .= Search::addLeftJoin($itemtype, $ref_table, $already_link_tables, Group::getTable(), 'groups_id_validator');
// but we want to join in issues
$join = str_replace('`glpi_tickets`.`id`', '`glpi_plugin_formcreator_issues`.`itemtype` = "Ticket" AND `glpi_plugin_formcreator_issues`.`items_id`', $join);
$join = str_replace('`glpi_tickets`', '`glpi_plugin_formcreator_issues`', $join);
$join = str_replace('`users_id_recipient`', '`requester_id`', $join);
$issueSo = Search::getOptions($itemtype);
$join .= Search::addLeftJoin(
$itemtype,
$ref_table,
$already_link_tables,
$issueSo[9]['table'],
'users_id_validator',
0,
0,
$issueSo[9]['joinparams']
);
$join .= Search::addLeftJoin(
$itemtype,
$ref_table,
$already_link_tables,
$issueSo[11]['table'],
'users_id_validate',
0,
0,
$issueSo[11]['joinparams']
);
$join .= Search::addLeftJoin(
$itemtype,
$ref_table,
$already_link_tables,
$issueSo[16]['table'],
'groups_id_validator',
0,
0,
$issueSo[16]['joinparams']
);
}
return $join;
}
Expand Down Expand Up @@ -153,18 +184,29 @@ function plugin_formcreator_addDefaultWhere($itemtype) {
$condition = str_replace('`users_id_recipient`', '`requester_id`', $condition);
$condition .= ' OR ';
}
// condition where current user is 1st validator of the issue
$condition .= " `glpi_plugin_formcreator_issues`.`users_id_validator` = '$currentUser'";
// condition where current user is a member of 1st validator group of the issue
// condition where current user is a validator of the issue
// Search optin ID 9 is either from Formcreator, either from AdvForms
$issueSearchOptions = Search::getOptions($itemtype);
$complexJoinId = Search::computeComplexJoinID($issueSearchOptions[9]['joinparams']);
$colname = $issueSearchOptions[9]['linkfield'];
$condition .= "`glpi_users_{$colname}_$complexJoinId`.`id` = '$currentUser'";

// condition where current user is a member of a validator group of the issue
$groupList = [];
foreach (Group_User::getUserGroups($currentUser) as $group) {
$groupList[] = $group['id'];
}
if (count($groupList) > 0) {
$groupList = implode("', '", $groupList);
$condition .= " OR `glpi_groups_groups_id_validator`.`id` IN ('$groupList')";
// Search option ID 16 is either from Formcreator, either from AdvForms
$complexJoinId = Search::computeComplexJoinID($issueSearchOptions[16]['joinparams']);
$colname = $issueSearchOptions[16]['linkfield'];
$condition .= " OR `glpi_groups_{$colname}_$complexJoinId`.`id` IN ('$groupList')";
}

// condition where current user is a validator of a issue of type ticket
$complexJoinId = Search::computeComplexJoinID($issueSearchOptions[11]['joinparams']);
$condition .= " OR `glpi_users_users_id_validate_$complexJoinId`.`id` = '$currentUser'";
// Add users_id_recipient
$condition .= " OR `glpi_plugin_formcreator_issues`.`users_id_recipient` = $currentUser ";

Expand Down Expand Up @@ -343,9 +385,10 @@ function plugin_formcreator_hook_add_ticket(CommonDBTM $item) {
$issueName = $item->fields['name'] != '' ? addslashes($item->fields['name']) : '(' . $item->getID() . ')';
$issue = new PluginFormcreatorIssue();
$issue->add([
'items_id' => $item->getID(),
'itemtype' => 'Ticket',
'name' => $issueName,
'display_id' => 't_' . $item->getID(),
'items_id' => $item->getID(),
'itemtype' => Ticket::class,
'status' => $validationStatus['status'],
'date_creation' => $item->fields['date'],
'date_mod' => $item->fields['date_mod'],
Expand Down Expand Up @@ -525,7 +568,7 @@ function plugin_formcreator_dynamicReport($params) {
case PluginFormcreatorFormAnswer::class;
if ($url = parse_url($_SERVER['HTTP_REFERER'])) {
if (strpos($url['path'],
Toolbox::getItemTypeFormURL("PluginFormcreatorForm")) !== false) {
Toolbox::getItemTypeFormURL(PluginFormcreatorForm::class)) !== false) {
parse_str($url['query'], $query);
if (isset($query['id'])) {
$item = new PluginFormcreatorForm;
Expand Down
4 changes: 0 additions & 4 deletions inc/formanswer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1253,8 +1253,6 @@ private function createIssue() {
'entities_id' => $this->fields['entities_id'],
'is_recursive' => $this->fields['is_recursive'],
'requester_id' => $this->fields['requester_id'],
'users_id_validator' => $this->fields['users_id_validator'],
'groups_id_validator'=> $this->fields['groups_id_validator'],
'comment' => '',
]);

Expand Down Expand Up @@ -1350,8 +1348,6 @@ private function updateIssue() {
'entities_id' => $this->fields['entities_id'],
'is_recursive' => $this->fields['is_recursive'],
'requester_id' => $this->fields['requester_id'],
'users_id_validator' => $this->fields['users_id_validator'],
'groups_id_validator'=> $this->fields['groups_id_validator'],
'comment' => '',
]);

Expand Down
99 changes: 59 additions & 40 deletions inc/issue.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,16 @@ public static function getSyncIssuesRequest() : AbstractQuery {
new QueryExpression("CONCAT('f_', `$formAnswerTable`.`id`) as `display_id`"),
"$formAnswerTable.id as items_id",
new QueryExpression("'" . PluginFormcreatorFormAnswer::getType() . "' as `itemtype`"),
$formAnswerTable => [
'status as status',
'request_date as date_creation',
'request_date as date_mod',
'entities_id as entities_d',
'is_recursive as is_recursive',
'requester_id as requester_id',
'users_id_validator as users_id_validator',
'groups_id_validator as groups_id_validator',
'comment as comment',
'requester_id as users_id_recipient'
],
$formAnswerTable . '.status as status',
$formAnswerTable . '.request_date as date_creation',
$formAnswerTable . '.request_date as date_mod',
$formAnswerTable . '.entities_id as entities_d',
$formAnswerTable . '.is_recursive as is_recursive',
$formAnswerTable . '.requester_id as requester_id',
new QueryExpression('0 as users_id_validator'),
new QueryExpression('0 as groups_id_validator'),
$formAnswerTable . '.comment as comment',
$formAnswerTable . '.requester_id as users_id_recipient',
],
'DISTINCT' => true,
'FROM' => $formAnswerTable,
Expand Down Expand Up @@ -142,16 +140,13 @@ public static function getSyncIssuesRequest() : AbstractQuery {
new QueryExpression("CONCAT('t_', `$ticketTable`.`id`) as `display_id`"),
"$ticketTable.id as items_id",
new QueryExpression("'" . Ticket::getType() . "' as `itemtype`"),
new QueryExpression("IF(`$ticketValidationTable`.`status` IS NULL,
new QueryExpression("IF(`$ticketTable`.`global_validation` IN ('" . CommonITILValidation::NONE . "', '" . CommonITILValidation::ACCEPTED . "'),
`$ticketTable`.`status`,
IF(`$ticketTable`.`global_validation` IN ('" . CommonITILValidation::NONE . "', '" . CommonITILValidation::ACCEPTED . "'),
IF(`$ticketTable`.`status` IN ('" . CommonITILObject::SOLVED . "', '" . CommonITILObject::CLOSED . "'),
`$ticketTable`.`status`,
IF(`$ticketTable`.`status` IN ('" . CommonITILObject::SOLVED . "', '" . CommonITILObject::CLOSED . "'),
`$ticketTable`.`status`,
IF(`$ticketTable`.`global_validation` = '" . CommonITILValidation::WAITING . "',
'" . PluginFormcreatorFormAnswer::STATUS_WAITING . "',
'" . PluginFormcreatorFormAnswer::STATUS_REFUSED . "'
)
IF(`$ticketTable`.`global_validation` = '" . CommonITILValidation::WAITING . "',
'" . PluginFormcreatorFormAnswer::STATUS_WAITING . "',
'" . PluginFormcreatorFormAnswer::STATUS_REFUSED . "'
)
)
) AS `status`"),
Expand All @@ -161,9 +156,9 @@ public static function getSyncIssuesRequest() : AbstractQuery {
'entities_id as entities_id'
],
new QueryExpression('0 as is_recursive'),
"$ticketUserTable.users_id as requester_id",
new QueryExpression("IF(`$ticketValidationTable`.`users_id_validate` IS NULL, 0, `$ticketValidationTable`.`users_id_validate`) as users_id_validator"),
new QueryExpression('0 as groups_id_validator'),
new QueryExpression("COALESCE(`$ticketUserTable`.`users_id`, 0) as `requester_id`"),
new QueryExpression('0 as users_id_validator'),
new QueryExpression('0 as groups_id_validator'),
"$ticketTable.content as comment",
'users_id_recipient as users_id_recipient'
],
Expand Down Expand Up @@ -195,12 +190,12 @@ public static function getSyncIssuesRequest() : AbstractQuery {
$ticketUserTable => $ticketFk,
],
],
$ticketValidationTable => [
'FKEY' => [
$ticketTable => 'id',
$ticketValidationTable => $ticketFk,
],
],
// $ticketValidationTable => [
// 'FKEY' => [
// $ticketTable => 'id',
// $ticketValidationTable => $ticketFk,
// ],
// ],
],
'WHERE' => [
"$ticketTable.is_deleted" => 0,
Expand All @@ -216,7 +211,7 @@ public static function getSyncIssuesRequest() : AbstractQuery {
'INNER JOIN' => [$itemTicketTable => $query2['LEFT JOIN'][$itemTicketTable]],
'LEFT JOIN' => [
$query2['LEFT JOIN'][0], // This is the TABLE => [...] subquery
$ticketValidationTable => $query2['LEFT JOIN'][$ticketValidationTable],
// $ticketValidationTable => $query2['LEFT JOIN'][$ticketValidationTable],
],
'WHERE' => $query2['WHERE'],
'GROUPBY' => ["$itemTicketTable.items_id"],
Expand Down Expand Up @@ -499,7 +494,6 @@ public function rawSearchOptions() {
'name' => __('Name'),
'datatype' => 'itemlink',
'massiveaction' => false,
'forcegroupby' => true,
'additionalfields' => [
'0' => 'display_id'
]
Expand Down Expand Up @@ -583,12 +577,21 @@ public function rawSearchOptions() {

$newtab = [
'id' => '9',
'table' => 'glpi_users',
'table' => User::getTable(),
'field' => 'name',
'linkfield' => 'users_id_validator',
'name' => __('Form approver', 'formcreator'),
'datatype' => 'dropdown',
'massiveaction' => false
'massiveaction' => false,
'joinparams' => [
'beforejoin' => [
'table' => PluginFormcreatorFormAnswer::getTable(),
'joinparams' => [
'jointype' => 'itemtype_item_revert',
'specific_itemtype' => PluginFormcreatorFormAnswer::class,
]
],
],
];
if (!Session::isCron() // no filter for cron
&& Session::getCurrentInterface() == 'helpdesk') {
Expand All @@ -613,12 +616,12 @@ public function rawSearchOptions() {
'field' => 'name',
'linkfield' => 'users_id_validate',
'name' => __('Ticket approver', 'formcreator'),
'datatype' => 'dropdown',
'datatype' => 'itemlink',
'right' => [
'0' => 'validate_request',
'1' => 'validate_incident'
],
'forcegroupby' => false,
'forcegroupby' => true,
'massiveaction' => false,
'joinparams' => [
'beforejoin' => [
Expand All @@ -636,6 +639,12 @@ public function rawSearchOptions() {
]
]
];
if (version_compare(GLPI_VERSION, '10.1') >= 0) {
$newtab['linkfield'] = 'items_id_target';
$newtab['condition'] = [
'REFTABLE.itemtype_target' => User::class,
];
}
if (!Session::isCron() // no filter for cron
&& Session::getCurrentInterface() == 'helpdesk') {
$newtab['right'] = 'id';
Expand All @@ -648,7 +657,6 @@ public function rawSearchOptions() {
'field' => 'name',
'name' => __('Technician'),
'datatype' => 'dropdown',
'forcegroupby' => true,
'massiveaction' => false,
'nodisplay' => $hide_technician,
'nosearch' => $hide_technician,
Expand Down Expand Up @@ -685,12 +693,14 @@ public function rawSearchOptions() {
'massiveaction' => false,
'nodisplay' => $hide_technician,
'nosearch' => $hide_technician,
'condition' => ['is_assign' => 1],
'joinparams' => [
'temoin' => true,
'beforejoin' => [
'table' => Group_Ticket::getTable(),
'joinparams' => [
'condition' => "AND NEWTABLE.`type` = '2'", // Assign
'condition' => [
'NEWTABLE.type' => CommonITILActor::ASSIGN,
],
'jointype' => 'child',
'beforejoin' => [
'table' => Ticket::getTable(),
Expand All @@ -706,12 +716,21 @@ public function rawSearchOptions() {

$tab[] = [
'id' => '16',
'table' => 'glpi_groups',
'table' => Group::getTable(),
'field' => 'completename',
'linkfield' => 'groups_id_validator',
'name' => __('Form approver group', 'formcreator'),
'datatype' => 'itemlink',
'massiveaction' => false,
'linkfield' => 'groups_id_validator',
'joinparams' => [
'beforejoin' => [
'table' => PluginFormcreatorFormAnswer::getTable(),
'joinparams' => [
'jointype' => 'itemtype_item_revert',
'specific_itemtype' => PluginFormcreatorFormAnswer::class,
]
],
],
];

return $tab;
Expand Down
Loading

0 comments on commit e301159

Please sign in to comment.