diff --git a/inc/profile.class.php b/inc/profile.class.php index 8337590..ab905a4 100644 --- a/inc/profile.class.php +++ b/inc/profile.class.php @@ -206,26 +206,19 @@ function saveProfileToDB($params) { $types = PluginGenericobjectType::getTypes(true); if (!empty ($types)) { foreach ($types as $tmp => $profile) { - $query = "UPDATE `".getTableForItemType(__CLASS__)."` " . - "SET "; - - if (isset($params[$profile['itemtype']]) && $params[$profile['itemtype']] == 'NULL') { - $query.="`right`='' "; - } else { - if (isset($params[$profile['itemtype']])) { - $query.="`right`='".$params[$profile['itemtype']]."'"; - } else { - $query.="`right`=''"; - } - } + $right = (isset($params[$profile['itemtype']]) && $params[$profile['itemtype']] !== 'NULL') ? $params[$profile['itemtype']] : ''; - if (isset($params[$profile['itemtype'].'_open_ticket'])) { - $query.=", `open_ticket`='".$params[$profile['itemtype'].'_open_ticket']."' "; + $update_params = [ + 'right' => $right + ]; + $open_ticket_key = $profile['itemtype'].'_open_ticket'; + if (isset($params[$open_ticket_key])) { + $update_params['open_ticket'] = $params[$open_ticket_key]; } - - $query.="WHERE `profiles_id`='".$params['profiles_id']."' " . - "AND `itemtype`='".$profile['itemtype']."'"; - $DB->query($query); + $DB->update(getTableForItemType(__CLASS__), $update_params, [ + 'profiles_id' => $params['profiles_id'], + 'itemtype' => $profile['itemtype'] + ]); } } } @@ -443,8 +436,6 @@ static function install(Migration $migration) { static function uninstall() { global $DB; - $query = "DELETE FROM `glpi_profilerights` - WHERE `name` LIKE '%plugin_genericobject%'"; - $DB->query($query) or die($DB->error()); + $DB->deleteOrDie('glpi_profilerights', ['name' => ['LIKE', '%plugin_genericobject%']]); } } diff --git a/inc/type.class.php b/inc/type.class.php index 471789f..3687f63 100644 --- a/inc/type.class.php +++ b/inc/type.class.php @@ -81,11 +81,12 @@ static function &getInstance($itemtype, $refresh = false) { function getFromDBByType($itemtype) { global $DB; - $query = "SELECT * FROM `" . getTableForItemType(__CLASS__) . "` " . - "WHERE `itemtype`='$itemtype'"; - $result = $DB->query($query); - if ($DB->numrows($result) > 0) { - $this->fields = $DB->fetchArray($result); + $it = $DB->request([ + 'FROM' => getTableForItemType(__CLASS__), + 'WHERE' => ['itemtype' => $itemtype] + ]); + if (count($it) > 0) { + $this->fields = $it->current(); } else { $this->getEmpty(); } @@ -1122,10 +1123,12 @@ public static function addTable($itemtype) { ) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;"; $DB->query($query); - $query = "INSERT INTO `glpi_displaypreferences` (`id`, `itemtype`, `num`, `rank`, `users_id`) " . - "VALUES (NULL, '$itemtype', '2', '1', '0');"; - $DB->query($query); - + $DB->insert('glpi_displaypreferences', [ + 'itemtype' => $itemtype, + 'num' => 2, + 'rank' => 1, + 'users_id' => 0 + ]); } /** @@ -1636,14 +1639,16 @@ public static function deleteItemsTable($itemtype) { */ static function getNameByID($itemtype) { global $DB; - $query = "SELECT `name` FROM `".getTableForItemType(__CLASS__)."` " . - "WHERE `itemtype`='$itemtype'"; - $result = $DB->query($query); - if ($DB->numrows($result)) { - return $DB->result($result, 0, "name"); - } else { - return ""; + + $it = $DB->request([ + 'SELECT' => ['name'], + 'FROM' => getTableForItemType(__CLASS__), + 'WHERE' => ['itemtype' => $itemtype] + ]); + if (count($it)) { + return $it->current()['name']; } + return ''; } @@ -1653,8 +1658,6 @@ static function getNameByID($itemtype) { * @return nothing */ public static function deleteTicketAssignation($itemtype) { - global $DB; - $types = ['Item_Ticket', 'Item_Problem', 'Change_Item']; foreach ($types as $type) { $item = new $type(); @@ -1668,8 +1671,6 @@ public static function deleteTicketAssignation($itemtype) { * @return nothing */ public static function deleteSimcardAssignation($itemtype) { - global $DB; - $plugin = new Plugin(); if ($plugin->isActivated('simcard') && $plugin->isActivated('simcard')) { $types = ['PluginSimcardSimcard_Item']; @@ -1753,13 +1754,13 @@ static function deleteNetworking($itemtype) { static function deleteReservations($itemtype) { global $DB; - $reservation = new Reservation(); - $query = "DELETE FROM - `glpi_reservations` - WHERE `reservationitems_id` in ( - SELECT `id` from `glpi_reservationitems` WHERE `itemtype`='$itemtype' - )"; - $DB->query($query); + $DB->delete('glpi_reservations', [ + 'reservationitems_id' => new \QuerySubQuery([ + 'SELECT' => 'id', + 'FROM' => 'glpi_reservationitems', + 'WHERE' => ['itemtype' => $itemtype] + ]) + ]); } /** @@ -2254,6 +2255,9 @@ static function install(Migration $migration) { $allGenericObjectTypes = PluginGenericobjectType::getTypes(true); $notepad = new Notepad(); + /** + * @var class-string $genericObjectType + */ foreach ($allGenericObjectTypes as $genericObjectType => $genericObjectData) { $itemtype = $genericObjectData['itemtype']; if (! class_exists($itemtype, true)) { @@ -2265,27 +2269,31 @@ static function install(Migration $migration) { die(); } $genericObjectTypeInstance = new $genericObjectType(); - if ($DB->fieldExists($genericObjectTypeInstance->getTable(), "notepad")) { - $query = "INSERT INTO `" . $notepad->getTable() . "` - (`items_id`, - `itemtype`, - `date_creation`, - `date_mod`, - `content` - ) - SELECT - `id` as `items_id`, - '" . $genericObjectType . "' as `itemtype`, - now() as `date_creation`, - now() as `date_mod`, - `notepad` as `content` - FROM `" . $genericObjectTypeInstance->getTable() . "` - WHERE notepad IS NOT NULL - AND notepad <> ''"; - $DB->query($query) or die($DB->error()); + if ($DB->fieldExists($genericObjectType::getTable(), "notepad")) { + $it = $DB->request([ + 'SELECT' => ['id', 'notepad'], + 'FROM' => $genericObjectType::getTable(), + 'WHERE' => [ + 'notepad' => ['<>', ''], + 'NOT' => ['notepad' => null] + ] + ]); + if (count($it)) { + $data = $it->current(); + $DB->insertOrDie( + Notepad::getTable(), + [ + 'itemtype' => $genericObjectType, + 'items_id' => $data['id'], + 'date_mod' => $_SESSION['glpi_currenttime'], + 'date_creation' => $_SESSION['glpi_currenttime'], + 'content' => $data['notepad'] + ] + ); + } } - $migration->dropField($genericObjectTypeInstance->getTable(), "notepad"); - $migration->migrationOneTable($genericObjectTypeInstance->getTable()); + $migration->dropField($genericObjectType::getTable(), "notepad"); + $migration->migrationOneTable($genericObjectType::getTable()); } //Displayprefs @@ -2379,11 +2387,11 @@ private static function normalizeNamesAndItemtypes(Migration $migration) { [ 'linked_itemtypes' => new \QueryExpression( 'REPLACE(' - . $DB->quoteName('linked_itemtypes') + . $DB::quoteName('linked_itemtypes') . ',' - . $DB->quoteValue('"' . $old_itemtype .'"') // itemtype is surrounded by quotes + . $DB::quoteValue('"' . $old_itemtype .'"') // itemtype is surrounded by quotes . ',' - . $DB->quoteValue('"' . $new_itemtype .'"') // itemtype is surrounded by quotes + . $DB::quoteValue('"' . $new_itemtype .'"') // itemtype is surrounded by quotes . ')' ), ], diff --git a/inc/typefamily.class.php b/inc/typefamily.class.php index 4fa0c58..85e1782 100644 --- a/inc/typefamily.class.php +++ b/inc/typefamily.class.php @@ -75,15 +75,28 @@ static function uninstall() { static function getFamilies() { global $DB; - $query = "SELECT f.id as id, f.name as name, t.itemtype as itemtype - FROM glpi_plugin_genericobject_typefamilies as f - LEFT JOIN glpi_plugin_genericobject_types AS t - ON (f.id = t.plugin_genericobject_typefamilies_id) - WHERE t.id IN (SELECT DISTINCT `id` - FROM glpi_plugin_genericobject_types - WHERE is_active=1)"; + $it = $DB->request([ + 'SELECT' => ['f.id AS id', 'f.name AS name', 't.itemtype AS itemtype'], + 'FROM' => new QueryExpression('glpi_plugin_genericobject_typefamilies AS f'), + 'LEFT JOIN' => [ + 'glpi_plugin_genericobject_types AS t' => [ + 'ON' => [ + 'f' => 'id', + 't' => 'plugin_genericobject_typefamilies_id' + ] + ] + ], + 'WHERE' => [ + 't.id' => new QuerySubQuery([ + 'SELECT ' => ['id'], + 'DISTINCT' => true, + 'FROM' => 'glpi_plugin_genericobject_types', + 'WHERE' => ['is_active' => 1] + ]) + ] + ]); $families = []; - foreach ($DB->request($query) as $fam) { + foreach ($it as $fam) { $itemtype = $fam['itemtype']; if ($itemtype::canCreate()) { $families[$fam['id']] = $fam['name'];