Skip to content

Commit 31a0533

Browse files
authored
Fix: Use doQuery only when necessary (#74)
* Fix: Use doQuery only when necessary * typo * johan * fix * fix * lint
1 parent ec861c2 commit 31a0533

File tree

3 files changed

+98
-39
lines changed

3 files changed

+98
-39
lines changed

inc/category.class.php

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -149,24 +149,46 @@ public function multipleDropdownGroup($level)
149149
// find current values for this select
150150
$values = [];
151151
if (!$this->isNewItem()) {
152-
$res_val = $DB->doQuery("SELECT `groups_id`
153-
FROM glpi_plugin_itilcategorygroups_categories_groups
154-
WHERE (`itilcategories_id` = {$this->fields['itilcategories_id']}
155-
OR `plugin_itilcategorygroups_categories_id` = {$this->getID()}
156-
)
157-
AND level = $level");
158-
while ($data_val = $DB->fetchAssoc($res_val)) {
152+
$res_val = $DB->request([
153+
'SELECT' => 'groups_id',
154+
'FROM' => 'glpi_plugin_itilcategorygroups_categories_groups',
155+
'WHERE' => [
156+
'OR' => [
157+
'itilcategories_id' => $this->fields['itilcategories_id'],
158+
'plugin_itilcategorygroups_categories_id' => $this->getID(),
159+
],
160+
'level' => $level,
161+
],
162+
]);
163+
foreach ($res_val as $data_val) {
159164
$values[] = $data_val['groups_id'];
160165
}
161166
}
162167

163168
// find possible values for this select
164-
$res_gr = $DB->doQuery('SELECT gr.id, gr.name
165-
FROM glpi_groups gr
166-
INNER JOIN glpi_plugin_itilcategorygroups_groups_levels gr_lvl
167-
ON gr_lvl.groups_id = gr.id
168-
AND gr_lvl.lvl = ' . intval($level) .
169-
getEntitiesRestrictRequest(' AND', 'gr', '', $_SESSION['glpiactiveentities'], true));
169+
$res_gr = $DB->request([
170+
'SELECT' => ['glpi_groups.id', 'glpi_groups.name'],
171+
'FROM' => 'glpi_groups',
172+
'INNER JOIN' => [
173+
'glpi_plugin_itilcategorygroups_groups_levels' => [
174+
'ON' => [
175+
'glpi_plugin_itilcategorygroups_groups_levels' => 'groups_id',
176+
'glpi_groups' => 'id',
177+
[
178+
'AND' => [
179+
'glpi_plugin_itilcategorygroups_groups_levels.lvl' => intval($level),
180+
],
181+
],
182+
],
183+
],
184+
],
185+
'WHERE' => getEntitiesRestrictCriteria(
186+
'glpi_groups',
187+
'',
188+
$_SESSION['glpiactiveentities'],
189+
true,
190+
),
191+
]);
170192

171193
if ($this->fields["view_all_lvl$level"] == 1) {
172194
$checked = "checked='checked'";
@@ -178,7 +200,7 @@ public function multipleDropdownGroup($level)
178200

179201
echo "<span id='select_level_$level'>";
180202
echo "<select name='groups_id_level" . $level . "[]' id='groups_id_level" . $level . "[]' $disabled multiple='multiple' class='chzn-select' data-placeholder='-----' style='width:160px;'>";
181-
while ($data_gr = $DB->fetchAssoc($res_gr)) {
203+
foreach ($res_gr as $data_gr) {
182204
if (in_array($data_gr['id'], $values)) {
183205
$selected = 'selected';
184206
} else {
@@ -482,8 +504,15 @@ public static function canApplyFilter($itilcategories_id)
482504
$category = new ITILCategory();
483505
if ($category->getFromDB($itilcategories_id)) {
484506
$table = getTableForItemType(__CLASS__);
485-
$query = "SELECT is_active FROM `$table` WHERE itilcategories_id = $itilcategories_id AND is_active = '1' AND is_groups_restriction = '1'";
486-
$data = $DB->request($query);
507+
$data = $DB->request([
508+
'SELECT' => 'is_active',
509+
'FROM' => $table,
510+
'WHERE' => [
511+
'itilcategories_id' => $itilcategories_id,
512+
'is_active' => 1,
513+
'is_groups_restriction' => 1,
514+
],
515+
]);
487516
// A category rule exist for this ticket
488517
if (count($data)) {
489518
return true;
@@ -498,15 +527,30 @@ public static function getOthersGroupsID($level = 0)
498527
/** @var \DBmysql $DB */
499528
global $DB;
500529

501-
$res = $DB->doQuery("SELECT gr.id
502-
FROM glpi_groups gr
503-
LEFT JOIN glpi_plugin_itilcategorygroups_groups_levels gl
504-
ON gl.groups_id = gr.id
505-
WHERE gl.lvl != $level
506-
AND gr.is_assign
507-
OR gl.lvl IS NULL");
530+
$res = $DB->request([
531+
'SELECT' => 'glpi_groups.id',
532+
'FROM' => 'glpi_groups',
533+
'LEFT JOIN' => [
534+
'glpi_plugin_itilcategorygroups_groups_levels' => [
535+
'ON' => [
536+
'glpi_plugin_itilcategorygroups_groups_levels' => 'groups_id',
537+
'glpi_groups' => 'id',
538+
],
539+
],
540+
],
541+
'WHERE' => [
542+
'OR' => [
543+
[
544+
'NOT' => ['glpi_plugin_itilcategorygroups_groups_levels.lvl' => $level],
545+
'glpi_groups.is_assign' => 1,
546+
],
547+
'glpi_plugin_itilcategorygroups_groups_levels.lvl' => null,
548+
],
549+
],
550+
]);
551+
508552
$groups_id = [];
509-
while ($row = $DB->fetchAssoc($res)) {
553+
foreach ($res as $row) {
510554
$groups_id[$row['id']] = $row['id'];
511555
}
512556

inc/category_group.class.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,15 @@ public static function install(Migration $migration)
6868

6969
//foreach old levels
7070
foreach ([1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four'] as $lvl_num => $lvl_str) {
71-
$query = "SELECT id, itilcategories_id, groups_id_level$lvl_str FROM $parent_table";
72-
$res = $DB->doQuery($query);
73-
while ($data = $DB->fetchAssoc($res)) {
71+
$res = $DB->request([
72+
'SELECT' => [
73+
'id',
74+
'itilcategories_id',
75+
"groups_id_level$lvl_str",
76+
],
77+
'FROM' => $parent_table,
78+
]);
79+
foreach ($res as $data) {
7480
//specific case (all group of this lvl), store it for further treatment
7581
if ($data["groups_id_level$lvl_str"] == -1) {
7682
$all_lvl[$data['itilcategories_id']][$lvl_num] = $lvl_str;

inc/group_level.class.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,27 @@ public static function getAllGroupForALevel($level, $entities_id = -1)
181181
}
182182

183183
$groups_id = [];
184-
$query = 'SELECT gl.groups_id
185-
FROM ' . getTableForItemType(__CLASS__) . " gl
186-
LEFT JOIN glpi_groups gr
187-
ON gl.groups_id = gr.id
188-
WHERE gl.lvl = $level" .
189-
getEntitiesRestrictRequest(
190-
' AND ',
191-
'gr',
192-
'entities_id',
193-
$entities_id,
194-
true,
195-
);
184+
$table = getTableForItemType(__CLASS__);
185+
$query = [
186+
'SELECT' => "$table.groups_id",
187+
'FROM' => $table,
188+
'LEFT JOIN' => [
189+
'glpi_groups' => [
190+
'ON' => [
191+
$table => 'groups_id',
192+
'glpi_groups' => 'id',
193+
],
194+
],
195+
],
196+
'WHERE' => [
197+
"$table.lvl" => $level,
198+
] + getEntitiesRestrictCriteria(
199+
'glpi_groups',
200+
'entities_id',
201+
$entities_id,
202+
true,
203+
),
204+
];
196205
foreach ($DB->request($query) as $data) {
197206
$groups_id[] = $data['groups_id'];
198207
}

0 commit comments

Comments
 (0)