Profile_User::getEntitiesForUser PHP Method

getEntitiesForUser() static public method

retrieve the entities associated to a user
static public getEntitiesForUser ( $users_id, $child = false ) : Array
$users_id Integer ID of the user
$child Boolean when true, include child entity when recursive right (false by default)
return Array of entity ID
    static function getEntitiesForUser($users_id, $child = false)
    {
        global $DB;
        $query = "SELECT `entities_id`, `is_recursive`\n                FROM `glpi_profiles_users`\n                WHERE `users_id` = '{$users_id}'";
        $entities = array();
        foreach ($DB->request($query) as $data) {
            if ($child && $data['is_recursive']) {
                foreach (getSonsOf('glpi_entities', $data['entities_id']) as $id) {
                    $entities[$id] = $id;
                }
            } else {
                $entities[$data['entities_id']] = $data['entities_id'];
            }
        }
        return $entities;
    }

Usage Example

Example #1
0
 /**
  * Show Links for an item
  *
  * @param $item                     CommonDBTM object
  * @param $withtemplate    integer  withtemplate param (default '')
  **/
 static function showForItem(CommonDBTM $item, $withtemplate = '')
 {
     global $DB, $CFG_GLPI;
     if (!self::canView()) {
         return false;
     }
     if ($item->isNewID($item->getID())) {
         return false;
     }
     $restrict = $item->getEntityID();
     if ($item->getType() == 'User') {
         $restrict = Profile_User::getEntitiesForUser($item->getID());
     }
     $query = "SELECT `glpi_links`.`id`,\n                       `glpi_links`.`link` AS link,\n                       `glpi_links`.`name` AS name ,\n                       `glpi_links`.`data` AS data,\n                       `glpi_links`.`open_window` AS open_window\n                FROM `glpi_links`\n                INNER JOIN `glpi_links_itemtypes`\n                     ON `glpi_links`.`id` = `glpi_links_itemtypes`.`links_id`\n                WHERE `glpi_links_itemtypes`.`itemtype`='" . $item->getType() . "' " . getEntitiesRestrictRequest(" AND", "glpi_links", "entities_id", $restrict, true) . "\n                ORDER BY name";
     $result = $DB->query($query);
     echo "<div class='spaced'><table class='tab_cadre_fixe'>";
     if ($DB->numrows($result) > 0) {
         echo "<tr><th>" . self::getTypeName(Session::getPluralNumber()) . "</th></tr>";
         while ($data = $DB->fetch_assoc($result)) {
             $links = self::getAllLinksFor($item, $data);
             foreach ($links as $link) {
                 echo "<tr class='tab_bg_2'>";
                 echo "<td class='center'>{$link}</td></tr>";
             }
         }
         echo "</table></div>";
     } else {
         echo "<tr class='tab_bg_2'><th>" . self::getTypeName(Session::getPluralNumber()) . "</th></tr>";
         echo "<tr class='tab_bg_2'><td class='center b'>" . __('No link defined') . "</td></tr>";
         echo "</table></div>";
     }
 }