Skip to content

Commit ea80d69

Browse files
cconard96cedric-anne
authored andcommitted
Remove some raw SQL
1 parent a4a3cb2 commit ea80d69

3 files changed

Lines changed: 72 additions & 57 deletions

File tree

hook.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,17 @@ function plugin_treeview_install() {
7575

7676
$DB->query($query) or die($DB->error());
7777

78-
$query = "INSERT INTO `glpi_plugin_treeview_configs`
79-
(`id`, `target`, `folderLinks`, `useSelection`, `useLines`, `useIcons`,
80-
`closeSameLevel`, `itemName`, `locationName`)
81-
VALUES ('1','right','1','1','1','1','0', '3', '2');";
82-
83-
$DB->query($query) or die($DB->error());
78+
$DB->insertOrDie('glpi_plugin_treeview_configs', [
79+
'id' => 1,
80+
'target' => 'right',
81+
'folderLinks' => 1,
82+
'useSelection' => 1,
83+
'useLines' => 1,
84+
'useIcons' => 1,
85+
'closeSameLevel' => 0,
86+
'itemName' => 3,
87+
'locationName' => 2
88+
]);
8489

8590
$query = "CREATE TABLE IF NOT EXISTS `glpi_plugin_treeview_profiles` (
8691
`id` int {$default_key_sign} NOT NULL auto_increment,
@@ -272,4 +277,4 @@ function plugin_change_entity_Treeview() {
272277

273278
echo "<script type='text/javascript'>parent.left.location.reload(true);</script>";
274279
}
275-
}
280+
}

inc/config.class.php

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -314,16 +314,18 @@ function getNodesFromDb() {
314314
$dontLoad = 'false';
315315

316316
// Get the lowest level of the tree nodes and the highest primary key
317-
$query = " SELECT MAX(`id`) AS `max_id`,
318-
MAX(`level`) AS `max_level`
319-
FROM `glpi_locations` ";
320-
321-
$query.= getEntitiesRestrictRequest(" WHERE ", "glpi_locations", '', '', true);
322-
323-
$result = $DB->query($query);
324-
325-
$max_level = $DB->result($result, 0, "max_level");
326-
$tv_id = $max_id = $DB->result($result, 0, "max_id");
317+
$it = $DB->request([
318+
'SELECT' => [
319+
new QueryExpression('MAX(' . $DB::quoteName('id') . ') AS ' . $DB::quoteName('max_id')),
320+
new QueryExpression('MAX(' . $DB::quoteName('level') . ') AS ' . $DB::quoteName('max_level')),
321+
],
322+
'FROM' => 'glpi_locations',
323+
'WHERE' => getEntitiesRestrictCriteria('glpi_locations', '', '', true),
324+
]);
325+
$result = $it->current();
326+
327+
$max_level = $result['max_level'];
328+
$tv_id = $max_id = $result['max_id'];
327329
$tv_id++;
328330

329331
// Is this the first time we load the page?
@@ -347,19 +349,20 @@ function getNodesFromDb() {
347349
"\r" => " ",
348350
"\n" => " "];
349351

350-
for ($n=1; $n<=count($nodes); $n++) {
352+
$node_count = count($nodes);
353+
for ($n=1; $n <= $node_count; $n++) {
351354
if ($nodes[$n-1] <= $max_id && $n <= $max_level) {
352-
$query = "SELECT *
353-
FROM `glpi_locations`
354-
WHERE `level` = '$n'
355-
AND `locations_id` = '". $nodes[$n-1] ."'";
356-
357-
$query.= getEntitiesRestrictRequest(" AND ", "glpi_locations", '', '', true);
358-
$query.= "ORDER BY `completename` ASC";
359-
360-
$result = $DB->query($query);
361-
362-
while ($r = $DB->fetchAssoc($result)) {
355+
$it = $DB->request([
356+
'FROM' => 'glpi_locations',
357+
'WHERE' => [
358+
'level' => $n,
359+
'locations_id' => $nodes[$n-1],
360+
getEntitiesRestrictCriteria('glpi_locations', '', '', true)
361+
],
362+
'ORDER' => ['completename ASC']
363+
]);
364+
365+
foreach ($it as $r) {
363366
// Location's name schema
364367
if ($locationName == 0) {
365368
$l_name = $r['name'];
@@ -390,35 +393,38 @@ function getNodesFromDb() {
390393
$item = new $type();
391394
$itemtable = getTableForItemType($type);
392395

393-
$query = "SELECT *
394-
FROM `$itemtable`
395-
WHERE `locations_id` = '".$r['id']."'";
396+
$criteria = [
397+
'FROM' => $itemtable,
398+
'WHERE' => [
399+
'locations_id' => $r['id'],
400+
],
401+
'ORDER' => ["$itemtable.name"]
402+
];
396403

397404
if ($item->maybeTemplate()) {
398-
$query .= " AND `$itemtable`.`is_template` = '0'";
405+
$criteria['WHERE']['is_template'] = 0;
399406
}
400407
if ($item->maybeDeleted()) {
401-
$query .= " AND `$itemtable`.`is_deleted` = '0'";
408+
$criteria['WHERE']['is_deleted'] = 0;
402409
}
403410

404411
if ($this->isEntityAssign()) {
405-
$query .= " AND `$itemtable`.`entities_id` = '".$_SESSION["glpiactive_entity"]."'";
412+
$criteria['WHERE']['entities_id'] = $_SESSION["glpiactive_entity"];
406413
}
407414

408-
$query .= " ORDER BY `$itemtable`.`name`";
409-
410-
$result_1 = $DB->query($query);
411-
if ($DB->numrows($result_1)) {
415+
$result_1 = $DB->request($criteria);
416+
if (count($result_1)) {
412417
$pid = $tv_id;
413418
$field_num = 3;
414419

415-
$query_location = "SELECT `completename`
416-
FROM `glpi_locations`
417-
WHERE `id` = '". $r['id'] ."'";
418-
$result_location = $DB->query($query_location);
420+
$result_location = $DB->request([
421+
'SELECT' => ['completename'],
422+
'FROM' => 'glpi_locations',
423+
'WHERE' => ['id' => $r['id']]
424+
]);
419425

420-
while ($row = $DB->fetchAssoc($result_location)) {
421-
$name_location= $row['completename'];
426+
foreach ($result_location as $row) {
427+
$name_location = $row['completename'];
422428
}
423429
$value = $r['id'];
424430
$token = Session::getNewCSRFToken();
@@ -443,7 +449,7 @@ function getNodesFromDb() {
443449
$tv_id++;
444450
}
445451

446-
while ($r_1 = $DB->fetchAssoc($result_1)) {
452+
foreach ($result_1 as $r_1) {
447453
// Item's name schema
448454
if ($itemName == 0 || $type == 'Software') {
449455
$i_name = $r_1['name'];

inc/preference.class.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ function showFormUserPreference($target, $id) {
7878
function checkIfPreferenceExists($users_id) {
7979
global $DB;
8080

81-
$result = $DB->query("SELECT `id`
82-
FROM `glpi_plugin_treeview_preferences`
83-
WHERE `users_id` = '$users_id'");
84-
if ($DB->numrows($result) > 0) {
85-
return $DB->result($result, 0, "id");
81+
$result = $DB->request([
82+
'SELECT' => ['id'],
83+
'FROM' => 'glpi_plugin_treeview_preferences',
84+
'WHERE' => ['users_id' => $users_id]
85+
]);
86+
if (count($result) > 0) {
87+
return $result->current()['id'];
8688
}
8789
return 0;
8890
}
@@ -100,11 +102,13 @@ function addDefaultPreference($users_id) {
100102
function checkPreferenceValue($users_id) {
101103
global $DB;
102104

103-
$result = $DB->query("SELECT *
104-
FROM `glpi_plugin_treeview_preferences`
105-
WHERE `users_id` = '$users_id' ");
106-
if ($DB->numrows($result) > 0) {
107-
return $DB->result($result, 0, "show_on_load");
105+
$result = $DB->request([
106+
'SELECT' => ['show_on_load'],
107+
'FROM' => 'glpi_plugin_treeview_preferences',
108+
'WHERE' => ['users_id' => $users_id]
109+
]);
110+
if (count($result) > 0) {
111+
return $result->current()['show_on_load'];
108112
}
109113
return 0;
110114
}
@@ -131,4 +135,4 @@ static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtem
131135
}
132136
return true;
133137
}
134-
}
138+
}

0 commit comments

Comments
 (0)