Migration::updateDisplayPrefs PHP Method

updateDisplayPrefs() public method

Update display preferences
public updateDisplayPrefs ( $toadd = [], $todel = [] )
$toadd array items to add : itemtype => array of values
$todel array items to del : itemtype => array of values
    function updateDisplayPrefs($toadd = array(), $todel = array())
    {
        global $DB;
        //TRANS: %s is the table or item to migrate
        $this->displayMessage(sprintf(__('Data migration - %s'), 'glpi_displaypreferences'));
        if (count($toadd)) {
            foreach ($toadd as $type => $tab) {
                $query = "SELECT DISTINCT `users_id`\n                      FROM `glpi_displaypreferences`\n                      WHERE `itemtype` = '{$type}'";
                if ($result = $DB->query($query)) {
                    if ($DB->numrows($result) > 0) {
                        while ($data = $DB->fetch_assoc($result)) {
                            $query = "SELECT MAX(`rank`)\n                               FROM `glpi_displaypreferences`\n                               WHERE `users_id` = '" . $data['users_id'] . "'\n                                     AND `itemtype` = '{$type}'";
                            $result = $DB->query($query);
                            $rank = $DB->result($result, 0, 0);
                            $rank++;
                            foreach ($tab as $newval) {
                                $query = "SELECT *\n                                  FROM `glpi_displaypreferences`\n                                  WHERE `users_id` = '" . $data['users_id'] . "'\n                                        AND `num` = '{$newval}'\n                                        AND `itemtype` = '{$type}'";
                                if ($result2 = $DB->query($query)) {
                                    if ($DB->numrows($result2) == 0) {
                                        $query = "INSERT INTO `glpi_displaypreferences`\n                                               (`itemtype` ,`num` ,`rank` ,`users_id`)\n                                        VALUES ('{$type}', '{$newval}', '" . $rank++ . "',\n                                                '" . $data['users_id'] . "')";
                                        $DB->query($query);
                                    }
                                }
                            }
                        }
                    } else {
                        // Add for default user
                        $rank = 1;
                        foreach ($tab as $newval) {
                            $query = "INSERT INTO `glpi_displaypreferences`\n                                      (`itemtype` ,`num` ,`rank` ,`users_id`)\n                               VALUES ('{$type}', '{$newval}', '" . $rank++ . "', '0')";
                            $DB->query($query);
                        }
                    }
                }
            }
        }
        if (count($todel)) {
            // delete display preferences
            foreach ($todel as $type => $tab) {
                if (count($tab)) {
                    $query = "DELETE\n                         FROM `glpi_displaypreferences`\n                         WHERE `itemtype` = '{$type}'\n                               AND `num` IN (" . implode(',', $tab) . ")";
                    $DB->query($query);
                }
            }
        }
    }