Skip to content

Commit e301159

Browse files
authored
fix(issue): sync issues problem when a ticket has several validators (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
1 parent e6986b0 commit e301159

File tree

5 files changed

+227
-76
lines changed

5 files changed

+227
-76
lines changed

composer.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
"php": "7.2"
1010
},
1111
"optimize-autoloader": true,
12-
"apcu-autoloader": true
12+
"apcu-autoloader": true,
13+
"allow-plugins": {
14+
"dealerdirect/phpcodesniffer-composer-installer": true
15+
}
1316
},
1417
"require": {
1518
"php": ">= 7.2",

hook.php

+51-8
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,42 @@ function plugin_formcreator_addDefaultJoin($itemtype, $ref_table, &$already_link
8585
case PluginFormcreatorIssue::class:
8686
// Get default joins for tickets
8787
$join = Search::addDefaultJoin(Ticket::getType(), Ticket::getTable(), $already_link_tables);
88-
$join .= Search::addLeftJoin($itemtype, $ref_table, $already_link_tables, Group::getTable(), 'groups_id_validator');
88+
// $join .= Search::addLeftJoin($itemtype, $ref_table, $already_link_tables, Group::getTable(), 'groups_id_validator');
8989
// but we want to join in issues
9090
$join = str_replace('`glpi_tickets`.`id`', '`glpi_plugin_formcreator_issues`.`itemtype` = "Ticket" AND `glpi_plugin_formcreator_issues`.`items_id`', $join);
9191
$join = str_replace('`glpi_tickets`', '`glpi_plugin_formcreator_issues`', $join);
9292
$join = str_replace('`users_id_recipient`', '`requester_id`', $join);
93+
$issueSo = Search::getOptions($itemtype);
94+
$join .= Search::addLeftJoin(
95+
$itemtype,
96+
$ref_table,
97+
$already_link_tables,
98+
$issueSo[9]['table'],
99+
'users_id_validator',
100+
0,
101+
0,
102+
$issueSo[9]['joinparams']
103+
);
104+
$join .= Search::addLeftJoin(
105+
$itemtype,
106+
$ref_table,
107+
$already_link_tables,
108+
$issueSo[11]['table'],
109+
'users_id_validate',
110+
0,
111+
0,
112+
$issueSo[11]['joinparams']
113+
);
114+
$join .= Search::addLeftJoin(
115+
$itemtype,
116+
$ref_table,
117+
$already_link_tables,
118+
$issueSo[16]['table'],
119+
'groups_id_validator',
120+
0,
121+
0,
122+
$issueSo[16]['joinparams']
123+
);
93124
}
94125
return $join;
95126
}
@@ -153,18 +184,29 @@ function plugin_formcreator_addDefaultWhere($itemtype) {
153184
$condition = str_replace('`users_id_recipient`', '`requester_id`', $condition);
154185
$condition .= ' OR ';
155186
}
156-
// condition where current user is 1st validator of the issue
157-
$condition .= " `glpi_plugin_formcreator_issues`.`users_id_validator` = '$currentUser'";
158-
// condition where current user is a member of 1st validator group of the issue
187+
// condition where current user is a validator of the issue
188+
// Search optin ID 9 is either from Formcreator, either from AdvForms
189+
$issueSearchOptions = Search::getOptions($itemtype);
190+
$complexJoinId = Search::computeComplexJoinID($issueSearchOptions[9]['joinparams']);
191+
$colname = $issueSearchOptions[9]['linkfield'];
192+
$condition .= "`glpi_users_{$colname}_$complexJoinId`.`id` = '$currentUser'";
193+
194+
// condition where current user is a member of a validator group of the issue
159195
$groupList = [];
160196
foreach (Group_User::getUserGroups($currentUser) as $group) {
161197
$groupList[] = $group['id'];
162198
}
163199
if (count($groupList) > 0) {
164200
$groupList = implode("', '", $groupList);
165-
$condition .= " OR `glpi_groups_groups_id_validator`.`id` IN ('$groupList')";
201+
// Search option ID 16 is either from Formcreator, either from AdvForms
202+
$complexJoinId = Search::computeComplexJoinID($issueSearchOptions[16]['joinparams']);
203+
$colname = $issueSearchOptions[16]['linkfield'];
204+
$condition .= " OR `glpi_groups_{$colname}_$complexJoinId`.`id` IN ('$groupList')";
166205
}
167206

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

@@ -343,9 +385,10 @@ function plugin_formcreator_hook_add_ticket(CommonDBTM $item) {
343385
$issueName = $item->fields['name'] != '' ? addslashes($item->fields['name']) : '(' . $item->getID() . ')';
344386
$issue = new PluginFormcreatorIssue();
345387
$issue->add([
346-
'items_id' => $item->getID(),
347-
'itemtype' => 'Ticket',
348388
'name' => $issueName,
389+
'display_id' => 't_' . $item->getID(),
390+
'items_id' => $item->getID(),
391+
'itemtype' => Ticket::class,
349392
'status' => $validationStatus['status'],
350393
'date_creation' => $item->fields['date'],
351394
'date_mod' => $item->fields['date_mod'],
@@ -525,7 +568,7 @@ function plugin_formcreator_dynamicReport($params) {
525568
case PluginFormcreatorFormAnswer::class;
526569
if ($url = parse_url($_SERVER['HTTP_REFERER'])) {
527570
if (strpos($url['path'],
528-
Toolbox::getItemTypeFormURL("PluginFormcreatorForm")) !== false) {
571+
Toolbox::getItemTypeFormURL(PluginFormcreatorForm::class)) !== false) {
529572
parse_str($url['query'], $query);
530573
if (isset($query['id'])) {
531574
$item = new PluginFormcreatorForm;

inc/formanswer.class.php

-4
Original file line numberDiff line numberDiff line change
@@ -1253,8 +1253,6 @@ private function createIssue() {
12531253
'entities_id' => $this->fields['entities_id'],
12541254
'is_recursive' => $this->fields['is_recursive'],
12551255
'requester_id' => $this->fields['requester_id'],
1256-
'users_id_validator' => $this->fields['users_id_validator'],
1257-
'groups_id_validator'=> $this->fields['groups_id_validator'],
12581256
'comment' => '',
12591257
]);
12601258

@@ -1350,8 +1348,6 @@ private function updateIssue() {
13501348
'entities_id' => $this->fields['entities_id'],
13511349
'is_recursive' => $this->fields['is_recursive'],
13521350
'requester_id' => $this->fields['requester_id'],
1353-
'users_id_validator' => $this->fields['users_id_validator'],
1354-
'groups_id_validator'=> $this->fields['groups_id_validator'],
13551351
'comment' => '',
13561352
]);
13571353

inc/issue.class.php

+59-40
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,16 @@ public static function getSyncIssuesRequest() : AbstractQuery {
9595
new QueryExpression("CONCAT('f_', `$formAnswerTable`.`id`) as `display_id`"),
9696
"$formAnswerTable.id as items_id",
9797
new QueryExpression("'" . PluginFormcreatorFormAnswer::getType() . "' as `itemtype`"),
98-
$formAnswerTable => [
99-
'status as status',
100-
'request_date as date_creation',
101-
'request_date as date_mod',
102-
'entities_id as entities_d',
103-
'is_recursive as is_recursive',
104-
'requester_id as requester_id',
105-
'users_id_validator as users_id_validator',
106-
'groups_id_validator as groups_id_validator',
107-
'comment as comment',
108-
'requester_id as users_id_recipient'
109-
],
98+
$formAnswerTable . '.status as status',
99+
$formAnswerTable . '.request_date as date_creation',
100+
$formAnswerTable . '.request_date as date_mod',
101+
$formAnswerTable . '.entities_id as entities_d',
102+
$formAnswerTable . '.is_recursive as is_recursive',
103+
$formAnswerTable . '.requester_id as requester_id',
104+
new QueryExpression('0 as users_id_validator'),
105+
new QueryExpression('0 as groups_id_validator'),
106+
$formAnswerTable . '.comment as comment',
107+
$formAnswerTable . '.requester_id as users_id_recipient',
110108
],
111109
'DISTINCT' => true,
112110
'FROM' => $formAnswerTable,
@@ -142,16 +140,13 @@ public static function getSyncIssuesRequest() : AbstractQuery {
142140
new QueryExpression("CONCAT('t_', `$ticketTable`.`id`) as `display_id`"),
143141
"$ticketTable.id as items_id",
144142
new QueryExpression("'" . Ticket::getType() . "' as `itemtype`"),
145-
new QueryExpression("IF(`$ticketValidationTable`.`status` IS NULL,
143+
new QueryExpression("IF(`$ticketTable`.`global_validation` IN ('" . CommonITILValidation::NONE . "', '" . CommonITILValidation::ACCEPTED . "'),
146144
`$ticketTable`.`status`,
147-
IF(`$ticketTable`.`global_validation` IN ('" . CommonITILValidation::NONE . "', '" . CommonITILValidation::ACCEPTED . "'),
145+
IF(`$ticketTable`.`status` IN ('" . CommonITILObject::SOLVED . "', '" . CommonITILObject::CLOSED . "'),
148146
`$ticketTable`.`status`,
149-
IF(`$ticketTable`.`status` IN ('" . CommonITILObject::SOLVED . "', '" . CommonITILObject::CLOSED . "'),
150-
`$ticketTable`.`status`,
151-
IF(`$ticketTable`.`global_validation` = '" . CommonITILValidation::WAITING . "',
152-
'" . PluginFormcreatorFormAnswer::STATUS_WAITING . "',
153-
'" . PluginFormcreatorFormAnswer::STATUS_REFUSED . "'
154-
)
147+
IF(`$ticketTable`.`global_validation` = '" . CommonITILValidation::WAITING . "',
148+
'" . PluginFormcreatorFormAnswer::STATUS_WAITING . "',
149+
'" . PluginFormcreatorFormAnswer::STATUS_REFUSED . "'
155150
)
156151
)
157152
) AS `status`"),
@@ -161,9 +156,9 @@ public static function getSyncIssuesRequest() : AbstractQuery {
161156
'entities_id as entities_id'
162157
],
163158
new QueryExpression('0 as is_recursive'),
164-
"$ticketUserTable.users_id as requester_id",
165-
new QueryExpression("IF(`$ticketValidationTable`.`users_id_validate` IS NULL, 0, `$ticketValidationTable`.`users_id_validate`) as users_id_validator"),
166-
new QueryExpression('0 as groups_id_validator'),
159+
new QueryExpression("COALESCE(`$ticketUserTable`.`users_id`, 0) as `requester_id`"),
160+
new QueryExpression('0 as users_id_validator'),
161+
new QueryExpression('0 as groups_id_validator'),
167162
"$ticketTable.content as comment",
168163
'users_id_recipient as users_id_recipient'
169164
],
@@ -195,12 +190,12 @@ public static function getSyncIssuesRequest() : AbstractQuery {
195190
$ticketUserTable => $ticketFk,
196191
],
197192
],
198-
$ticketValidationTable => [
199-
'FKEY' => [
200-
$ticketTable => 'id',
201-
$ticketValidationTable => $ticketFk,
202-
],
203-
],
193+
// $ticketValidationTable => [
194+
// 'FKEY' => [
195+
// $ticketTable => 'id',
196+
// $ticketValidationTable => $ticketFk,
197+
// ],
198+
// ],
204199
],
205200
'WHERE' => [
206201
"$ticketTable.is_deleted" => 0,
@@ -216,7 +211,7 @@ public static function getSyncIssuesRequest() : AbstractQuery {
216211
'INNER JOIN' => [$itemTicketTable => $query2['LEFT JOIN'][$itemTicketTable]],
217212
'LEFT JOIN' => [
218213
$query2['LEFT JOIN'][0], // This is the TABLE => [...] subquery
219-
$ticketValidationTable => $query2['LEFT JOIN'][$ticketValidationTable],
214+
// $ticketValidationTable => $query2['LEFT JOIN'][$ticketValidationTable],
220215
],
221216
'WHERE' => $query2['WHERE'],
222217
'GROUPBY' => ["$itemTicketTable.items_id"],
@@ -499,7 +494,6 @@ public function rawSearchOptions() {
499494
'name' => __('Name'),
500495
'datatype' => 'itemlink',
501496
'massiveaction' => false,
502-
'forcegroupby' => true,
503497
'additionalfields' => [
504498
'0' => 'display_id'
505499
]
@@ -583,12 +577,21 @@ public function rawSearchOptions() {
583577

584578
$newtab = [
585579
'id' => '9',
586-
'table' => 'glpi_users',
580+
'table' => User::getTable(),
587581
'field' => 'name',
588582
'linkfield' => 'users_id_validator',
589583
'name' => __('Form approver', 'formcreator'),
590584
'datatype' => 'dropdown',
591-
'massiveaction' => false
585+
'massiveaction' => false,
586+
'joinparams' => [
587+
'beforejoin' => [
588+
'table' => PluginFormcreatorFormAnswer::getTable(),
589+
'joinparams' => [
590+
'jointype' => 'itemtype_item_revert',
591+
'specific_itemtype' => PluginFormcreatorFormAnswer::class,
592+
]
593+
],
594+
],
592595
];
593596
if (!Session::isCron() // no filter for cron
594597
&& Session::getCurrentInterface() == 'helpdesk') {
@@ -613,12 +616,12 @@ public function rawSearchOptions() {
613616
'field' => 'name',
614617
'linkfield' => 'users_id_validate',
615618
'name' => __('Ticket approver', 'formcreator'),
616-
'datatype' => 'dropdown',
619+
'datatype' => 'itemlink',
617620
'right' => [
618621
'0' => 'validate_request',
619622
'1' => 'validate_incident'
620623
],
621-
'forcegroupby' => false,
624+
'forcegroupby' => true,
622625
'massiveaction' => false,
623626
'joinparams' => [
624627
'beforejoin' => [
@@ -636,6 +639,12 @@ public function rawSearchOptions() {
636639
]
637640
]
638641
];
642+
if (version_compare(GLPI_VERSION, '10.1') >= 0) {
643+
$newtab['linkfield'] = 'items_id_target';
644+
$newtab['condition'] = [
645+
'REFTABLE.itemtype_target' => User::class,
646+
];
647+
}
639648
if (!Session::isCron() // no filter for cron
640649
&& Session::getCurrentInterface() == 'helpdesk') {
641650
$newtab['right'] = 'id';
@@ -648,7 +657,6 @@ public function rawSearchOptions() {
648657
'field' => 'name',
649658
'name' => __('Technician'),
650659
'datatype' => 'dropdown',
651-
'forcegroupby' => true,
652660
'massiveaction' => false,
653661
'nodisplay' => $hide_technician,
654662
'nosearch' => $hide_technician,
@@ -685,12 +693,14 @@ public function rawSearchOptions() {
685693
'massiveaction' => false,
686694
'nodisplay' => $hide_technician,
687695
'nosearch' => $hide_technician,
696+
'condition' => ['is_assign' => 1],
688697
'joinparams' => [
689-
'temoin' => true,
690698
'beforejoin' => [
691699
'table' => Group_Ticket::getTable(),
692700
'joinparams' => [
693-
'condition' => "AND NEWTABLE.`type` = '2'", // Assign
701+
'condition' => [
702+
'NEWTABLE.type' => CommonITILActor::ASSIGN,
703+
],
694704
'jointype' => 'child',
695705
'beforejoin' => [
696706
'table' => Ticket::getTable(),
@@ -706,12 +716,21 @@ public function rawSearchOptions() {
706716

707717
$tab[] = [
708718
'id' => '16',
709-
'table' => 'glpi_groups',
719+
'table' => Group::getTable(),
710720
'field' => 'completename',
721+
'linkfield' => 'groups_id_validator',
711722
'name' => __('Form approver group', 'formcreator'),
712723
'datatype' => 'itemlink',
713724
'massiveaction' => false,
714-
'linkfield' => 'groups_id_validator',
725+
'joinparams' => [
726+
'beforejoin' => [
727+
'table' => PluginFormcreatorFormAnswer::getTable(),
728+
'joinparams' => [
729+
'jointype' => 'itemtype_item_revert',
730+
'specific_itemtype' => PluginFormcreatorFormAnswer::class,
731+
]
732+
],
733+
],
715734
];
716735

717736
return $tab;

0 commit comments

Comments
 (0)