diff --git a/hook.php b/hook.php index 59b4d006..7e02a022 100644 --- a/hook.php +++ b/hook.php @@ -256,15 +256,24 @@ function plugin_fields_rule_matched($params = []) if (isset($params['input']['plugin_fusioninventory_agents_id'])) { foreach ($params['output'] as $field => $value) { - // check if current field is in a tab container - $query = "SELECT c.id - FROM glpi_plugin_fields_fields f - LEFT JOIN glpi_plugin_fields_containers c - ON c.id = f.plugin_fields_containers_id - WHERE f.name = '$field'"; - $res = $DB->query($query); - if ($DB->numrows($res) > 0) { - $data = $DB->fetchAssoc($res); + // check if current field is in a tab container + $iterator = $DB->request([ + 'SELECT' => 'glpi_plugin_fields_containers.id', + 'FROM' => 'glpi_plugin_fields_containers', + 'LEFT JOIN' => [ + 'glpi_plugin_fields_fields' => [ + 'FKEY' => [ + 'glpi_plugin_fields_containers' => 'id', + 'glpi_plugin_fields_fields' => 'plugin_fields_containers_id' + ] + ] + ], + 'WHERE' => [ + 'glpi_plugin_fields_fields.name' => $field, + ] + ]); + if (count($iterator) > 0) { + $data = $iterator->next(); //retrieve computer $agents_id = $params['input']['plugin_fusioninventory_agents_id']; diff --git a/inc/container.class.php b/inc/container.class.php index 4f7769c8..0a18ea30 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -102,8 +102,20 @@ public static function installBaseData(Migration $migration, $version) $migration->changeField($table, 'itemtype', 'itemtypes', 'longtext'); $migration->migrationOneTable($table); - $query = "UPDATE `$table` SET `itemtypes` = CONCAT('[\"', `itemtypes`, '\"]')"; - $DB->query($query) or die($DB->error()); + $DB->updateOrDie( + $table, + [ + 'itemtypes' => new QueryExpression( + sprintf( + 'CONCAT(%s, %s, %s)', + $DB->quoteValue('[\"'), + $DB->quoteName('itemtype'), + $DB->quoteValue('\"]') + ) + ), + ], + [1] + ); } //add display preferences for this class @@ -252,15 +264,36 @@ public static function installUserData(Migration $migration, $version) $compfields = $fields->find(['plugin_fields_containers_id' => $comptab, 'name' => $newname]); if ($compfields) { $newname = $newname . '_os'; - $DB->query("UPDATE glpi_plugin_fields_fields SET name='$newname' WHERE name='{$field['name']}' AND plugin_fields_containers_id='$ostab'"); + $DB->update( + 'glpi_plugin_fields_fields', + [ + 'name' => $newname + ], + [ + 'name' => $field['name'], + 'plugin_fields_containers_id' => $ostab + ] + ); } $compdata::addField($newname, $field['type']); $fieldnames[$field['name']] = $newname; } - $sql = "UPDATE glpi_plugin_fields_fields SET plugin_fields_containers_id='$comptab' WHERE plugin_fields_containers_id='$ostab'"; - $DB->query($sql); - $DB->query("DELETE FROM glpi_plugin_fields_containers WHERE id='$ostab'"); + $DB->update( + 'glpi_plugin_fields_fields', + [ + 'plugin_fields_containers_id' => $comptab + ], + [ + 'plugin_fields_containers_id' => $ostab + ] + ); + $DB->delete( + 'glpi_plugin_fields_containers', + [ + 'id' => $ostab + ] + ); //migrate existing data $existings = $osdata->find(); @@ -275,9 +308,16 @@ public static function installUserData(Migration $migration, $version) //drop old table $DB->query("DROP TABLE " . $osdata::getTable()); } else { - $sql = "UPDATE glpi_plugin_fields_containers SET type='dom', subtype=NULL WHERE id='$ostab'"; - $comptab = $ostab; - $DB->query($sql); + $DB->update( + 'glpi_plugin_fields_containers', + [ + 'type' => 'dom', + 'subtype' => null + ], + [ + 'id' => $ostab + ] + ); } } @@ -1017,15 +1057,25 @@ public static function getUsedItemtypes($type = 'all', $must_be_active = false) { global $DB; $itemtypes = []; - $where = $type == 'all' ? '1=1' : ('type = "' . $type . '"'); + $where = []; + + if ($type !== 'all') { + $where['type'] = $type; + } + if ($must_be_active) { - $where .= ' AND is_active = 1'; + $where['is_active'] = 1; } - $query = 'SELECT DISTINCT `itemtypes` FROM `glpi_plugin_fields_containers` WHERE ' . $where; - $result = $DB->query($query); - while (list($data) = $DB->fetchArray($result)) { - $jsonitemtype = json_decode($data); + $iterator = $DB->request([ + 'SELECT' => 'itemtypes', + 'DISTINCT' => true, + 'FROM' => self::getTable(), + 'WHERE' => $where, + ]); + + foreach ($iterator as $data) { + $jsonitemtype = json_decode($data['itemtypes']); $itemtypes = array_merge($itemtypes, $jsonitemtype); } @@ -1354,19 +1404,20 @@ public static function validateValues($data, $itemtype, $massiveaction) } else if ($field['mandatory'] == 1 && isset($data['items_id'])) { $tablename = getTableForItemType(self::getClassname($itemtype, $container->fields['name'])); - $query = "SELECT * FROM `$tablename` WHERE - `itemtype`='$itemtype' - AND `items_id`='{$data['items_id']}' - AND `plugin_fields_containers_id`='{$data['plugin_fields_containers_id']}'"; - - $db_result = []; - if ($result = $DB->query($query)) { - $db_result = $DB->fetchAssoc($result); - if (isset($db_result['plugin_fields_' . $name . 'dropdowns_id'])) { - $value = $db_result['plugin_fields_' . $name . 'dropdowns_id']; - } else if (isset($db_result[$name])) { - $value = $db_result[$name]; - } + $iterator = $DB->request([ + 'FROM' => $tablename, + 'WHERE' => [ + 'itemtype' => $itemtype, + 'items_id' => $data['items_id'], + 'plugin_fields_containers_id' => $data['plugin_fields_containers_id'], + ], + ]); + + $db_result = $iterator->next(); + if (isset($db_result['plugin_fields_' . $name . 'dropdowns_id'])) { + $value = $db_result['plugin_fields_' . $name . 'dropdowns_id']; + } else if (isset($db_result[$name])) { + $value = $db_result[$name]; } } else { if ($massiveaction) { diff --git a/inc/field.class.php b/inc/field.class.php index 378da533..5471a449 100644 --- a/inc/field.class.php +++ b/inc/field.class.php @@ -367,11 +367,16 @@ public function post_purgeItem() $old_container = $this->fields['plugin_fields_containers_id']; $old_ranking = $this->fields['ranking']; - $query = "UPDATE $table SET - ranking = ranking-1 - WHERE plugin_fields_containers_id = $old_container - AND ranking > $old_ranking"; - $DB->query($query); + $D->update( + $table, + [ + 'ranking' => new QueryExpression($DB->quoteName('ranking') . ' - 1') + ], + [ + 'plugin_fields_containers_id' => $old_container, + 'ranking' => ['>', $old_ranking] + ] + ); return true; } @@ -441,14 +446,18 @@ public function getNextRanking() { global $DB; - $sql = "SELECT max(`ranking`) AS `rank` - FROM `" . self::getTable() . "` - WHERE `plugin_fields_containers_id` = '" . - $this->fields['plugin_fields_containers_id'] . "'"; - $result = $DB->query($sql); + $iterator = $DB->request([ + 'SELECT' => new \QueryExpression( + 'max(' . $DB->quoteName('ranking') . ') AS ' . $DB->quoteName('rank') + ), + 'FROM' => self::getTable(), + 'WHERE' => [ + 'plugin_fields_containers_id' => $this->fields['plugin_fields_containers_id'] + ] + ]); - if ($DB->numrows($result) > 0) { - $data = $DB->fetchAssoc($result); + if (count($iterator) > 0) { + $data = $iterator->next(); return $data["rank"] + 1; } return 0; @@ -496,14 +505,14 @@ public function showSummary($container) $cID = $container->fields['id']; // Display existing Fields - $query = "SELECT `id`, `label` - FROM `" . $this->getTable() . "` - WHERE `plugin_fields_containers_id` = '$cID' - ORDER BY `ranking` ASC"; - $result = $DB->query($query); + $iterator = $DB->request([ + 'SELECT' => ['id', 'label'], + 'FROM' => self::getTable(), + 'WHERE' => ['plugin_fields_containers_id' => $cID], + 'ORDER' => 'ranking ASC' + ]); $rand = mt_rand(); - echo "
"; $ajax_params = [ @@ -525,7 +534,7 @@ public function showSummary($container) ""; echo __("Add a new field", "fields") . "
"; - if ($DB->numrows($result) == 0) { + if (count($iterator) == 0) { echo ""; echo "
" . __("No field for this block", "fields") . "
"; } else { @@ -548,7 +557,7 @@ public function showSummary($container) Session::initNavigateListItems('PluginFieldsField', __('Fields list')); - while ($data = $DB->fetchArray($result)) { + foreach ($iterator as $data) { if ($this->getFromDB($data['id'])) { echo ""; @@ -1154,20 +1163,39 @@ public static function showSingle($itemtype, $searchOption, $massiveaction = fal ); //find field - $query = "SELECT fields.plugin_fields_containers_id, fields.is_readonly, fields.multiple, fields.default_value - FROM glpi_plugin_fields_fields fields - LEFT JOIN glpi_plugin_fields_containers containers - ON containers.id = fields.plugin_fields_containers_id - AND containers.itemtypes LIKE '%$itemtype%' - WHERE fields.name = '$cleaned_linkfield'"; - $res = $DB->query($query); - if ($DB->numrows($res) == 0) { + $iterator = $DB->request([ + 'SELECT' => [ + 'fields.plugin_fields_containers_id', + 'fields.is_readonly', + 'fields.multiple', + 'fields.default_value' + ], + 'FROM' => self::getTable() . ' AS fields', + 'LEFT JOIN' => [ + 'glpi_plugin_fields_containers AS containers' => [ + 'FKEY' => [ + 'containers' => 'id', + 'fields' => 'plugin_fields_containers_id', + [ + 'AND' => [ + 'containers.itemtypes' => ['LIKE' => "%$itemtype%"] + ] + ] + ] + ] + ], + 'WHERE' => [ + 'fields.name' => $cleaned_linkfield + ], + ]); + + if (count($iterator) == 0) { return false; } - $data = $DB->fetchAssoc($res); + $data = $iterator->next(); - //display an hidden post field to store container id + //display a hidden post field to store container id echo Html::hidden('c_id', ['value' => $data['plugin_fields_containers_id']]); //prepare array for function prepareHtmlFields