grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026.
--------------------------------------------------------------------------
*/
/** @file
* @brief
*/
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}
class DisplayPreference extends CommonDBTM {
// From CommonGLPI
public $taborientation = 'horizontal';
public $get_item_to_display_tab = false;
// From CommonDBTM
public $auto_message_on_action = false;
protected $displaylist = false;
static $rightname = 'search_config';
const PERSONAL = 1024;
const GENERAL = 2048;
/**
* @see CommonDBTM::prepareInputForAdd()
**/
function prepareInputForAdd($input) {
global $DB;
$query = "SELECT MAX(`rank`)
FROM `".$this->getTable()."`
WHERE `itemtype` = '".$input["itemtype"]."'
AND `users_id` = '".$input["users_id"]."'";
$result = $DB->query($query);
$input["rank"] = $DB->result($result,0,0)+1;
return $input;
}
/**
* @since version 0.85
*
* @see CommonDBTM::processMassiveActionsForOneItemtype()
**/
static function processMassiveActionsForOneItemtype(MassiveAction $ma, CommonDBTM $item,
array $ids) {
switch ($ma->getAction()) {
case 'delete_for_user' :
$input = $ma->getInput();
if (isset($input['users_id'])) {
$user = new User();
$user->getFromDB($input['users_id']);
foreach ($ids as $id) {
if ($input['users_id'] == Session::getLoginUserID()) {
if ($item->deleteByCriteria(array('users_id' => $input['users_id'],
'itemtype' => $id))) {
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK);
} else {
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
$ma->addMessage($user->getErrorMessage(ERROR_ON_ACTION));
}
} else {
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_NORIGHT);
$ma->addMessage($user->getErrorMessage(ERROR_RIGHT));
}
}
} else {
$ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_KO);
}
return;
}
parent::processMassiveActionsForOneItemtype($ma, $item, $ids);
}
/**
* Get display preference for a user for an itemtype
*
* @param $itemtype itemtype
* @param $user_id user ID
**/
static function getForTypeUser($itemtype, $user_id) {
global $DB;
$query = "SELECT *
FROM `glpi_displaypreferences`
WHERE `itemtype` = '$itemtype'
AND (`users_id` = '$user_id' OR `users_id` = '0')
ORDER BY `users_id`, `rank`";
$result = $DB->query($query);
$default_prefs = array();
$user_prefs = array();
while ($data = $DB->fetch_assoc($result)) {
if ($data["users_id"] != 0) {
$user_prefs[] = $data["num"];
} else {
$default_prefs[] = $data["num"];
}
}
return count($user_prefs) ? $user_prefs : $default_prefs;
}
/**
* Active personal config based on global one
*
* @param $input array parameter (itemtype,users_id)
**/
function activatePerso(array $input) {
global $DB;
if (!Session::haveRight(self::$rightname, self::PERSONAL)) {
return false;
}
$query = "SELECT *
FROM `".$this->getTable()."`
WHERE `itemtype` = '".$input["itemtype"]."'
AND `users_id` = '0'";
$result = $DB->query($query);
if ($DB->numrows($result)) {
while ($data = $DB->fetch_assoc($result)) {
unset($data["id"]);
$data["users_id"] = $input["users_id"];
$this->fields = $data;
$this->addToDB();
}
} else {
// No items in the global config
$searchopt = Search::getOptions($input["itemtype"]);
if (count($searchopt) > 1) {
$done = false;
foreach ($searchopt as $key => $val) {
if (is_array($val)
&& ($key != 1)
&& !$done) {
$data["users_id"] = $input["users_id"];
$data["itemtype"] = $input["itemtype"];
$data["rank"] = 1;
$data["num"] = $key;
$this->fields = $data;
$this->addToDB();
$done = true;
}
}
}
}
}
/**
* Order to move an item
*
* @param $input array parameter (id,itemtype,users_id)
* @param $action up or down
**/
function orderItem(array $input, $action) {
global $DB;
// Get current item
$query = "SELECT `rank`
FROM `".$this->getTable()."`
WHERE `id` = '".$input['id']."'";
$result = $DB->query($query);
$rank1 = $DB->result($result, 0, 0);
// Get previous or next item
$query = "SELECT `id`, `rank`
FROM `".$this->getTable()."`
WHERE `itemtype` = '".$input['itemtype']."'
AND `users_id` = '".$input["users_id"]."'";
switch ($action) {
case "up" :
$query .= " AND `rank` < '$rank1'
ORDER BY `rank` DESC";
break;
case "down" :
$query .= " AND `rank` > '$rank1'
ORDER BY `rank` ASC";
break;
default :
return false;
}
$result = $DB->query($query);
$rank2 = $DB->result($result, 0, "rank");
$ID2 = $DB->result($result, 0, "id");
// Update items
$query = "UPDATE `".$this->getTable()."`
SET `rank` = '$rank2'
WHERE `id` = '".$input['id']."'";
$DB->query($query);
$query = "UPDATE `".$this->getTable()."`
SET `rank` = '$rank1'
WHERE `id` = '$ID2'";
$DB->query($query);
}
/**
* Print the search config form
*
* @param $target form target
* @param $itemtype item type
*
* @return nothing
**/
function showFormPerso($target, $itemtype) {
global $CFG_GLPI, $DB;
$searchopt = Search::getCleanedOptions($itemtype);
if (!is_array($searchopt)) {
return false;
}
$item = NULL;
if ($itemtype != 'AllAssets') {
$item = getItemForItemtype($itemtype);
}
$IDuser = Session::getLoginUserID();
echo "
";
// Defined items
$query = "SELECT *
FROM `".$this->getTable()."`
WHERE `itemtype` = '$itemtype'
AND `users_id` = '$IDuser'
ORDER BY `rank`";
$result = $DB->query($query);
$numrows = 0;
$numrows = $DB->numrows($result);
if ($numrows == 0) {
Session::checkRight(self::$rightname, self::PERSONAL);
echo "
\n";
} else {
$already_added = self::getForTypeUser($itemtype, $IDuser);
echo "
| ";
echo " |
";
echo "| ";
echo " |
\n";
// print first element
echo "";
echo "| ".$searchopt[1]["name"]." | ";
echo " | ";
echo "
";
// print entity
if (Session::isMultiEntitiesMode()
&& (isset($CFG_GLPI["union_search_type"][$itemtype])
|| ($item && $item->maybeRecursive())
|| (count($_SESSION["glpiactiveentities"]) > 1))
&& isset($searchopt[80])) {
echo "";
echo "| ".$searchopt[80]["name"]." | ";
echo " | ";
echo "
";
}
$i = 0;
if ($numrows) {
while ($data = $DB->fetch_assoc($result)) {
if (($data["num"] !=1) && isset($searchopt[$data["num"]])) {
echo "";
echo "| ";
echo $searchopt[$data["num"]]["name"]." | ";
if ($i != 0) {
echo "";
echo " | \n";
} else {
echo " | ";
}
if ($i != ($numrows-1)) {
echo "";
echo " | \n";
} else {
echo " | ";
}
echo "";
echo " | \n";
echo "
";
$i++;
}
}
}
echo "
";
}
echo "
";
}
/**
* Print the search config form
*
* @param $target form target
* @param $itemtype item type
*
* @return nothing
**/
function showFormGlobal($target, $itemtype) {
global $CFG_GLPI, $DB;
$searchopt = Search::getOptions($itemtype);
if (!is_array($searchopt)) {
return false;
}
$IDuser = 0;
$item = NULL;
if ($itemtype != 'AllAssets') {
$item = getItemForItemtype($itemtype);
}
$global_write = Session::haveRight(self::$rightname, self::GENERAL);
echo "";
// Defined items
$query = "SELECT *
FROM `".$this->getTable()."`
WHERE `itemtype` = '$itemtype'
AND `users_id` = '$IDuser'
ORDER BY `rank`";
$result = $DB->query($query);
$numrows = $DB->numrows($result);
echo "
| ";
echo __('Select default items to show')." |
\n";
if ($global_write) {
$already_added = self::getForTypeUser($itemtype, $IDuser);
echo "| ";
echo " |
";
}
// print first element
echo "";
echo "| ".$searchopt[1]["name"];
if ($global_write) {
echo " | ";
}
echo " |
";
// print entity
if (Session::isMultiEntitiesMode()
&& (isset($CFG_GLPI["union_search_type"][$itemtype])
|| ($item && $item->maybeRecursive())
|| (count($_SESSION["glpiactiveentities"]) > 1))
&& isset($searchopt[80])) {
echo "";
echo "| ".$searchopt[80]["name"]." | ";
echo " | ";
echo "
";
}
$i = 0;
if ($numrows) {
while ($data=$DB->fetch_assoc($result)) {
if (($data["num"] != 1)
&& isset($searchopt[$data["num"]])) {
echo "| ";
echo $searchopt[$data["num"]]["name"];
echo " | ";
if ($global_write) {
if ($i != 0) {
echo "";
echo " | ";
} else {
echo " | \n";
}
if ($i != ($numrows-1)) {
echo "";
echo " | ";
} else {
echo " | \n";
}
echo "";
echo " | \n";
}
echo "
";
$i++;
}
}
}
echo "
";
echo "
";
}
/**
* show defined display preferences for a user
*
* @param $users_id integer user ID
**/
static function showForUser($users_id) {
global $DB;
$url = Toolbox::getItemTypeFormURL(__CLASS__);
$query = "SELECT `itemtype`,
COUNT(*) AS nb
FROM `glpi_displaypreferences`
WHERE `users_id` = '$users_id'
GROUP BY `itemtype`";
$req = $DB->request($query);
if ($req->numrows() > 0) {
$rand = mt_rand();
echo "";
Html::openMassiveActionsForm('mass'.__CLASS__.$rand);
$massiveactionparams = array('width' => 400,
'height' => 200,
'container' => 'mass'.__CLASS__.$rand,
'specific_actions' => array(__CLASS__.MassiveAction::CLASS_ACTION_SEPARATOR.'delete_for_user'
=> _x('button', 'Delete permanently')),
'extraparams' => array('massive_action_fields' => array('users_id')));
Html::showMassiveActions($massiveactionparams);
echo Html::hidden('users_id', array('value' => $users_id,
'data-glpicore-ma-tags' => 'common'));
echo "
";
echo "";
echo "| ";
Html::checkAllAsCheckbox('mass'.__CLASS__.$rand);
echo " | ";
echo "".__('Type')." |
";
foreach ($req as $data) {
echo "| ";
Html::showMassiveActionCheckBox(__CLASS__, $data["itemtype"]);
echo " | ";
if ($item = getItemForItemtype($data["itemtype"])) {
$name = $item->getTypeName(1);
} else {
$name = $data["itemtype"];
}
echo "$name | ".$data['nb']." | ";
echo "
";
}
echo "
";
$massiveactionparams['ontop'] = false;
Html::showMassiveActions($massiveactionparams);
Html::closeForm();
echo "
";
} else {
echo "";
echo "| ".__('No item found')." |
";
echo "
";
}
}
/**
* For tab management : force isNewItem
*
* @since version 0.83
**/
function isNewItem() {
return false;
}
function defineTabs($options=array()) {
$ong = array();
$this->addStandardTab(__CLASS__, $ong, $options);
$ong['no_all_tab'] = true;
return $ong;
}
function getTabNameForItem(CommonGLPI $item, $withtemplate=0) {
switch ($item->getType()) {
case 'Preference' :
if (Session::haveRight(self::$rightname, self::PERSONAL)) {
return __('Personal View');
}
break;
case __CLASS__:
$ong = array();
$ong[1] = __('Global View');
if (Session::haveRight(self::$rightname, self::PERSONAL)) {
$ong[2] = __('Personal View');
}
return $ong;
}
return '';
}
static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) {
switch ($item->getType()) {
case 'Preference' :
self::showForUser(Session::getLoginUserID());
return true;
case __CLASS__ :
switch ($tabnum) {
case 1 :
$item->showFormGlobal($_GET['_target'], $_GET["displaytype"]);
return true;
case 2 :
Session::checkRight(self::$rightname, self::PERSONAL);
$item->showFormPerso($_GET['_target'], $_GET["displaytype"]);
return true;
}
}
return false;
}
/**
* @since version 0.85
*
* @see commonDBTM::getRights()
**/
function getRights($interface='central') {
//TRANS: short for : Search result user display
$values[self::PERSONAL] = array('short' => __('User display'),
'long' => __('Search result user display'));
//TRANS: short for : Search result default display
$values[self::GENERAL] = array('short' => __('Default display'),
'long' => __('Search result default display'));
return $values;
}
}