@@ -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 ' ];
0 commit comments