Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove some raw SQL #349

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 12 additions & 21 deletions inc/profile.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
]);
}
}
}
Expand Down Expand Up @@ -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%']]);
}
}
108 changes: 58 additions & 50 deletions inc/type.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
]);
}

/**
Expand Down Expand Up @@ -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 '';
}


Expand All @@ -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();
Expand All @@ -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'];
Expand Down Expand Up @@ -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]
])
]);
}

/**
Expand Down Expand Up @@ -2254,6 +2255,9 @@ static function install(Migration $migration) {
$allGenericObjectTypes = PluginGenericobjectType::getTypes(true);

$notepad = new Notepad();
/**
* @var class-string<CommonDBTM> $genericObjectType
*/
foreach ($allGenericObjectTypes as $genericObjectType => $genericObjectData) {
$itemtype = $genericObjectData['itemtype'];
if (! class_exists($itemtype, true)) {
Expand All @@ -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
Expand Down Expand Up @@ -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
. ')'
),
],
Expand Down
29 changes: 21 additions & 8 deletions inc/typefamily.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down