Skip to content

Commit e56ffcd

Browse files
committed
remove raw sql
1 parent ab54a62 commit e56ffcd

File tree

4 files changed

+160
-82
lines changed

4 files changed

+160
-82
lines changed

hook.php

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,30 @@ function plugin_escalade_install() {
8686
) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;";
8787
$DB->query($query);
8888

89-
$query = "INSERT INTO glpi_plugin_escalade_configs
90-
VALUES (NULL, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, '".Ticket::WAITING."',0)";
91-
$DB->query($query);
89+
$DB->insert('glpi_plugin_escalade_configs', [
90+
'remove_group' => 1,
91+
'show_history' => 1,
92+
'task_history' => 1,
93+
'remove_tech' => 1,
94+
'solve_return_group' => 1,
95+
'reassign_group_from_cat' => 0,
96+
'reassign_tech_from_cat' => 0,
97+
'cloneandlink_ticket' => 1,
98+
'close_linkedtickets' => 1,
99+
'use_assign_user_group' => 0,
100+
'use_assign_user_group_creation' => 0,
101+
'use_assign_user_group_modification' => 0,
102+
'remove_delete_requester_user_btn' => 1,
103+
'remove_delete_watcher_user_btn' => 1,
104+
'remove_delete_assign_user_btn' => 0,
105+
'remove_delete_requester_group_btn' => 1,
106+
'remove_delete_watcher_group_btn' => 1,
107+
'remove_delete_assign_group_btn' => 0,
108+
'remove_delete_assign_supplier_btn' => 1,
109+
'use_filter_assign_group' => 1,
110+
'ticket_last_status' => Ticket::WAITING,
111+
'remove_requester' => 0
112+
]);
92113
}
93114

94115
// == Update to 1.2 ==
@@ -167,10 +188,11 @@ function plugin_escalade_install() {
167188
$status = -1;
168189
break;
169190
}
170-
$query = "UPDATE `glpi_plugin_escalade_configs`
171-
SET `ticket_last_status` = '".$status."'
172-
WHERE `id` = '".$data['id']."'";
173-
$DB->query($query);
191+
$DB->update('glpi_plugin_escalade_configs', [
192+
'ticket_last_status' => $status
193+
], [
194+
'id' => $data['id']
195+
]);
174196
}
175197

176198
$query = "ALTER TABLE `glpi_plugin_escalade_configs` MODIFY `ticket_last_status` INT;";
@@ -204,9 +226,10 @@ function plugin_escalade_install() {
204226

205227
$user = new User();
206228
foreach ($user->find() as $data) {
207-
$query = "INSERT INTO glpi_plugin_escalade_users (`users_id`, `use_filter_assign_group`)
208-
VALUES (".$data['id'].", $default_value)";
209-
$DB->query($query);
229+
$DB->insert('glpi_plugin_escalade_users', [
230+
'users_id' => $data['id'],
231+
'use_filter_assign_group' => $default_value
232+
]);
210233
}
211234
}
212235

@@ -343,7 +366,7 @@ function plugin_escalade_item_purge($item) {
343366
global $DB;
344367

345368
if ($item instanceof User) {
346-
$DB->query("DELETE FROM glpi_plugin_escalade_users WHERE users_id = ".$item->getID());
369+
$DB->delete('glpi_plugin_escalade_users', ['users_id' => $item->getID()]);
347370
}
348371

349372
if ($item instanceof Ticket) {
@@ -377,9 +400,10 @@ function plugin_escalade_item_add_user($item) {
377400
$config->getFromDB(1);
378401
$default_value = $config->fields["use_filter_assign_group"];
379402

380-
$query = "INSERT INTO glpi_plugin_escalade_users (`users_id`, `use_filter_assign_group`)
381-
VALUES (".$item->getID().", $default_value)";
382-
$DB->query($query);
403+
$DB->insert('glpi_plugin_escalade_users', [
404+
'users_id' => $item->getID(),
405+
'use_filter_assign_group' => $default_value
406+
]);
383407
}
384408

385409
if ($item instanceof Ticket_User) {

inc/history.class.php

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -187,57 +187,71 @@ static function showCentralSpecificList($type) {
187187
return false;
188188
}
189189

190-
$groups = implode("','", $_SESSION['glpigroups']);
191-
$numrows = 0;
192-
$is_deleted = " `glpi_tickets`.`is_deleted` = 0 ";
190+
$criteria = [
191+
'SELECT' => ['glpi_tickets.id'],
192+
'DISTINCT' => true,
193+
'FROM' => 'glpi_tickets',
194+
'LEFT JOIN' => [
195+
'glpi_tickets_users' => [
196+
'ON' => [
197+
'glpi_tickets' => 'id',
198+
'glpi_tickets_users' => 'tickets_id'
199+
]
200+
]
201+
],
202+
'WHERE' => [
203+
'glpi_tickets.is_deleted' => 0,
204+
],
205+
'ORDER' => ['glpi_tickets.date_mod DESC']
206+
];
193207

194208
if ($type == "notold") {
195209
$title = __("Tickets to follow (escalated)", "escalade");
196210
$status = CommonITILObject::INCOMING.", ".CommonITILObject::PLANNED.", ".
197211
CommonITILObject::ASSIGNED.", ".CommonITILObject::WAITING;
198212

199-
$search_assign = " `glpi_plugin_escalade_histories`.`groups_id` IN ('$groups')
200-
AND (`glpi_groups_tickets`.`groups_id` NOT IN ('$groups')
201-
OR `glpi_groups_tickets`.`groups_id` IS NULL)";
202-
203-
$query_join = "LEFT JOIN `glpi_plugin_escalade_histories`
204-
ON (`glpi_tickets`.`id` = `glpi_plugin_escalade_histories`.`tickets_id`)
205-
LEFT JOIN `glpi_groups_tickets`
206-
ON (`glpi_tickets`.`id` = `glpi_groups_tickets`.`tickets_id`
207-
AND `glpi_groups_tickets`.`type`=2)";
213+
$criteria['WHERE']['glpi_plugin_escalade_histories.groups_id'] = $_SESSION['glpigroups'];
214+
$criteria['WHERE'][] = [
215+
'OR' => [
216+
'NOT' => ['glpi_groups_tickets.groups_id' => $_SESSION['glpigroups']],
217+
'glpi_groups_tickets.groups_id' => null
218+
]
219+
];
220+
221+
$criteria['LEFT JOIN']['glpi_plugin_escalade_histories'] = [
222+
'ON' => [
223+
'glpi_tickets' => 'id',
224+
'glpi_plugin_escalade_histories' => 'tickets_id'
225+
]
226+
];
208227
} else {
209228
$title = __("Tickets to close (escalated)", "escalade");
210229
$status = CommonITILObject::SOLVED;
211230

212-
$search_assign = " (`glpi_groups_tickets`.`groups_id` IN ('$groups'))";
213-
214-
$query_join = "LEFT JOIN `glpi_groups_tickets`
215-
ON (`glpi_tickets`.`id` = `glpi_groups_tickets`.`tickets_id`
216-
AND `glpi_groups_tickets`.`type`=2)";
231+
$criteria['WHERE']['glpi_groups_tickets.groups_id'] = $_SESSION['glpigroups'];
217232
}
218-
219-
$query = "SELECT DISTINCT `glpi_tickets`.`id`
220-
FROM `glpi_tickets`
221-
LEFT JOIN `glpi_tickets_users`
222-
ON (`glpi_tickets`.`id` = `glpi_tickets_users`.`tickets_id`)";
223-
224-
$query .= $query_join;
225-
226-
$query .= "WHERE $is_deleted AND ( $search_assign )
227-
AND (`status` IN ($status))".
228-
getEntitiesRestrictRequest("AND", "glpi_tickets");
229-
230-
$query .= " ORDER BY glpi_tickets.date_mod DESC";
231-
232-
$result = $DB->query($query);
233-
$numrows = $DB->numrows($result);
233+
$criteria['LEFT JOIN']['glpi_groups_tickets'] = [
234+
'ON' => [
235+
'glpi_tickets' => 'id',
236+
'glpi_groups_tickets' => 'tickets_id',
237+
[
238+
'AND' => ['glpi_groups_tickets.type' => 2]
239+
]
240+
]
241+
];
242+
$criteria['WHERE']['status'] = $status;
243+
$criteria['WHERE'][] = getEntitiesRestrictCriteria('glpi_tickets');
244+
245+
$result = $DB->request($criteria);
246+
$numrows = count($result);
234247
if (!$numrows) {
235248
return;
236249
}
237250

238-
$query .= " LIMIT 0, 5";
239-
$result = $DB->query($query);
240-
$number = $DB->numrows($result);
251+
$criteria['START'] = 0;
252+
$criteria['LIMIT'] = 5;
253+
$result = $DB->request($criteria);
254+
$number = count($result);
241255

242256
//show central list
243257
if ($numrows > 0) {
@@ -287,9 +301,8 @@ static function showCentralSpecificList($type) {
287301
echo "<th>".__('Requester')."</th>";
288302
echo "<th>".__('Associated element')."</th>";
289303
echo "<th>".__('Description')."</th></tr></thead>";
290-
for ($i = 0; $i < $number; $i++) {
291-
$ID = $DB->result($result, $i, "id");
292-
Ticket::showVeryShort($ID, 'Ticket$2');
304+
foreach ($result as $data) {
305+
Ticket::showVeryShort($data['id'], 'Ticket$2');
293306
}
294307
}
295308
echo "</table>";

inc/ticket.class.php

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,14 @@ static function addHistoryOnAddGroup(CommonDBTM $item) {
257257
$history = new PluginEscaladeHistory();
258258

259259
$group_ticket = new Group_Ticket();
260-
$group_ticket->getFromDBByRequest(['ORDER' => 'id DESC',
261-
'LIMIT' => 1,
262-
'tickets_id' => $tickets_id,
263-
'type' => 2]);
260+
$group_ticket->getFromDBByRequest([
261+
'WHERE' => [
262+
'tickets_id' => $tickets_id,
263+
'type' => 2
264+
],
265+
'ORDER' => 'id DESC',
266+
'LIMIT' => 1
267+
]);
264268

265269
$previous_groups_id = 0;
266270
$counter = 0;
@@ -755,30 +759,56 @@ static function cloneAndLink($tickets_id) {
755759

756760
//add actors to the new ticket (without assign)
757761
//users
758-
$query_users = "INSERT INTO glpi_tickets_users
759-
SELECT '' AS id, $newID as tickets_id, users_id, type, use_notification, alternative_email
760-
FROM glpi_tickets_users
761-
WHERE tickets_id = $tickets_id AND type != 2";
762-
if (!$res = $DB->query($query_users)) {
762+
$res = $DB->insert('glpi_tickets_user', new QuerySubQuery([
763+
'SELECT' => [
764+
new QueryExpression("'' AS " . $DB::quoteName('id')),
765+
new QueryExpression($DB::quoteValue($newID) . " AS " . $DB::quoteName('tickets_id')),
766+
'users_id', 'type', 'use_notification', 'alternative_email'
767+
],
768+
'FROM' => 'glpi_tickets_users',
769+
'WHERE' => [
770+
'tickets_id' => $tickets_id,
771+
'type' => ['!=', 2]
772+
]
773+
]));
774+
if (!$res) {
763775
echo "{\"success\":false, \"message\":\"".__("Error : adding actors (user)", "escalade")."\"}";
764776
exit;
765777
}
766778
//groups
767-
$query_groups = "INSERT INTO glpi_groups_tickets
768-
SELECT '' AS id, $newID as tickets_id, groups_id, type
769-
FROM glpi_groups_tickets
770-
WHERE tickets_id = $tickets_id AND type != 2";
771-
if (!$res = $DB->query($query_groups)) {
779+
$res = $DB->insert('glpi_tickets_user', new QuerySubQuery([
780+
'SELECT' => [
781+
new QueryExpression("'' AS " . $DB::quoteName('id')),
782+
new QueryExpression($DB::quoteValue($newID) . " AS " . $DB::quoteName('tickets_id')),
783+
'groups_id', 'type'
784+
],
785+
'FROM' => 'glpi_groups_tickets',
786+
'WHERE' => [
787+
'tickets_id' => $tickets_id,
788+
'type' => ['!=', 2]
789+
]
790+
]));
791+
if (!$res) {
772792
echo "{\"success\":false, \"message\":\"".__("Error : adding actors (group)", "escalade")."\"}";
773793
exit;
774794
}
775795

776796
//add documents
777-
$query_docs = "INSERT INTO glpi_documents_items (documents_id, items_id, itemtype, entities_id, is_recursive, date_mod)
778-
SELECT documents_id, $newID, 'Ticket', entities_id, is_recursive, date_mod
779-
FROM glpi_documents_items
780-
WHERE items_id = $tickets_id AND itemtype = 'Ticket'";
781-
if (! $res = $DB->query($query_docs)) {
797+
$res = $DB->insert('glpi_documents_items', new QuerySubQuery([
798+
'SELECT' => [
799+
new QueryExpression("'' AS " . $DB::quoteName('id')),
800+
'documents_id',
801+
new QueryExpression($DB::quoteValue($newID) . " AS " . $DB::quoteName('items_id')),
802+
new QueryExpression($DB::quoteValue('Ticket') . " AS " . $DB::quoteName('itemtype')),
803+
'entities_id', 'is_recursive', 'date_mod'
804+
],
805+
'FROM' => 'glpi_documents_items',
806+
'WHERE' => [
807+
'items_id' => $tickets_id,
808+
'itemtype' => 'Ticket'
809+
]
810+
]));
811+
if (!$res) {
782812
echo "{\"success\":false, \"message\":\"".__("Error : adding documents", "escalade")."\"}";
783813
exit;
784814
}

inc/user.class.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,35 @@ static function processMassiveActionsForOneItemtype(MassiveAction $ma, CommonDBT
7575
static private function getUserGroup($entity, $userid, $filter = '', $first = true) {
7676
global $DB;
7777

78-
$query = "SELECT glpi_groups.id
79-
FROM glpi_groups_users
80-
INNER JOIN glpi_groups ON (glpi_groups.id = glpi_groups_users.groups_id)
81-
WHERE glpi_groups_users.users_id='$userid'".
82-
getEntitiesRestrictRequest(' AND ', 'glpi_groups', '', $entity, true, true);
78+
$criteria = [
79+
'SELECT' => ['glpi_groups.id'],
80+
'FROM' => 'glpi_groups_users',
81+
'INNER JOIN' => [
82+
'glpi_groups' => [
83+
'ON' => [
84+
'glpi_groups_users' => 'groups_id',
85+
'glpi_groups' => 'id'
86+
]
87+
]
88+
],
89+
'WHERE' => [
90+
'glpi_groups_users.users_id' => $userid,
91+
getEntitiesRestrictCriteria('glpi_groups', '', $entity, true, true)
92+
],
93+
'ORDER' => ['glpi_groups_users.id']
94+
];
8395

8496
if ($filter) {
85-
$query .= "AND ($filter)";
97+
$criteria['WHERE'][] = new QueryExpression($filter);
8698
}
8799

88-
$query.= " ORDER BY glpi_groups_users.id";
89-
100+
$it = $DB->request($criteria);
90101
$rep = [];
91-
foreach ($DB->request($query) as $data) {
102+
foreach ($it as $data) {
92103
if ($first) {
93104
return $data['id'];
94105
}
95-
$rep[]=$data['id'];
106+
$rep[] = $data['id'];
96107
}
97108
return ($first ? 0 : array_pop($rep));
98109
}

0 commit comments

Comments
 (0)