@@ -43,6 +43,100 @@ public function upgrade(Migration $migration) {
4343 // Versioin 2.10.2 contains fixes on counters requiring repopulation of issues table
4444 $ table = 'glpi_plugin_formcreator_issues ' ;
4545 $ DB ->query ("TRUNCATE ` $ table` " );
46- PluginFormcreatorIssue::cronSyncIssues (new CronTask ());
46+ $ this ->syncIssues (new CronTask ());
47+ }
48+
49+ /**
50+ * This is a copy of PluginFormcreatorIssue::cronSyncIssues as it is in 2.10.2
51+ *
52+ * @param CronTask $task
53+ * @return void
54+ */
55+ public function syncIssues (CronTask $ task ) {
56+ global $ DB ;
57+
58+ $ task ->log ("Sync issues from forms answers and tickets " );
59+ $ volume = 0 ;
60+
61+ // Request which merges tickets and formanswers
62+ // 1 ticket not linked to a formanswer => 1 issue which is the ticket sub_itemtype
63+ // 1 form_answer not linked to a ticket => 1 issue which is the formanswer sub_itemtype
64+ // 1 ticket linked to 1 form_answer => 1 issue which is the ticket sub_itemtype
65+ // several tickets linked to the same form_answer => 1 issue which is the form_answer sub_itemtype
66+ $ query = "SELECT DISTINCT
67+ NULL AS `id`,
68+ `f`.`name` AS `name`,
69+ CONCAT('f_',`fanswer`.`id`) AS `display_id`,
70+ `fanswer`.`id` AS `original_id`,
71+ 'PluginFormcreatorFormAnswer' AS `sub_itemtype`,
72+ `fanswer`.`status` AS `status`,
73+ `fanswer`.`request_date` AS `date_creation`,
74+ `fanswer`.`request_date` AS `date_mod`,
75+ `fanswer`.`entities_id` AS `entities_id`,
76+ `fanswer`.`is_recursive` AS `is_recursive`,
77+ `fanswer`.`requester_id` AS `requester_id`,
78+ `fanswer`.`users_id_validator` AS `users_id_validator`,
79+ `fanswer`.`groups_id_validator` AS `groups_id_validator`,
80+ `fanswer`.`comment` AS `comment`
81+ FROM `glpi_plugin_formcreator_formanswers` AS `fanswer`
82+ LEFT JOIN `glpi_plugin_formcreator_forms` AS `f`
83+ ON`f`.`id` = `fanswer`.`plugin_formcreator_forms_id`
84+ LEFT JOIN `glpi_items_tickets` AS `itic`
85+ ON `itic`.`items_id` = `fanswer`.`id`
86+ AND `itic`.`itemtype` = 'PluginFormcreatorFormAnswer'
87+ GROUP BY `original_id`
88+ HAVING COUNT(`itic`.`tickets_id`) != 1
89+ UNION
90+ SELECT DISTINCT
91+ NULL AS `id`,
92+ `tic`.`name` AS `name`,
93+ CONCAT('t_',`tic`.`id`) AS `display_id`,
94+ `tic`.`id` AS `original_id`,
95+ 'Ticket' AS `sub_itemtype`,
96+ if(`tv`.`status` IS NULL,`tic`.`status`, if(`tv`.`status` = 2, 101, if(`tv`.`status` = 3, `tic`.`status`, 102))) AS `status`,
97+ `tic`.`date` AS `date_creation`,
98+ `tic`.`date_mod` AS `date_mod`,
99+ `tic`.`entities_id` AS `entities_id`,
100+ 0 AS `is_recursive`,
101+ `tu`.`users_id` AS `requester_id`,
102+ `tv`.`users_id_validate` AS `users_id_validator`,
103+ 0 AS `groups_id_validator`,
104+ `tic`.`content` AS `comment`
105+ FROM `glpi_tickets` AS `tic`
106+ LEFT JOIN `glpi_items_tickets` AS `itic`
107+ ON `itic`.`tickets_id` = `tic`.`id`
108+ AND `itic`.`itemtype` = 'PluginFormcreatorFormAnswer'
109+ LEFT JOIN (
110+ SELECT DISTINCT `users_id`, `tickets_id`
111+ FROM `glpi_tickets_users` AS `tu`
112+ WHERE `tu`.`type` = ' " . CommonITILActor::REQUESTER . "'
113+ ORDER BY `id` ASC
114+ ) AS `tu` ON (`tic`.`id` = `tu`.`tickets_id`)
115+ LEFT JOIN `glpi_ticketvalidations` as `tv`
116+ ON (`tic`.`id` = `tv`.`tickets_id`)
117+ WHERE `tic`.`is_deleted` = 0
118+ GROUP BY `original_id`
119+ HAVING COUNT(`itic`.`items_id`) <= 1 " ;
120+
121+ $ countQuery = "SELECT COUNT(*) AS `cpt` FROM ( $ query) AS `issues` " ;
122+ $ result = $ DB ->query ($ countQuery );
123+ if ($ result !== false ) {
124+ if (version_compare (GLPI_VERSION , '9.5 ' ) < 0 ) {
125+ $ fa = 'fetch_assoc ' ;
126+ } else {
127+ $ fa = 'fetchAssoc ' ;
128+ }
129+ $ count = $ DB ->$ fa ($ result );
130+ $ table = 'glpi_plugin_formcreator_issues ' ;
131+ if (countElementsInTable ($ table ) != $ count ['cpt ' ]) {
132+ if ($ DB ->query ("TRUNCATE ` $ table` " )) {
133+ $ DB ->query ("INSERT INTO ` $ table` SELECT * FROM ( $ query) as `dt` " );
134+ $ volume = 1 ;
135+ }
136+ }
137+ }
138+ $ task ->setVolume ($ volume );
139+
140+ return 1 ;
47141 }
48142}
0 commit comments