getDatas() static public method

Get datas based on search parameters
static public getDatas ( $itemtype, $params, array $forcedisplay = [] ) : data
$itemtype item type to manage
$params search params passed to prepareDatasForSearch function
$forcedisplay array array of columns to display (default empty = empty use display pref and search criterias)
return data array
Beispiel #1
0
 private function doSearch($itemtype, $params, array $forcedisplay = array())
 {
     global $DEBUG_SQL;
     // check param itemtype exists (to avoid search errors)
     $this->assertTrue(is_subclass_of($itemtype, "CommonDBTM"));
     // login to glpi if needed
     if (!isset($_SESSION['glpiname'])) {
         $this->Login();
     }
     // force session in debug mode (to store & retrieve sql errors)
     $glpi_use_mode = $_SESSION['glpi_use_mode'];
     $_SESSION['glpi_use_mode'] = Session::DEBUG_MODE;
     // don't compute last request from session
     $params['reset'] = 'reset';
     // do search
     $params = Search::manageParams($itemtype, $params);
     $data = Search::getDatas($itemtype, $params, $forcedisplay);
     // append existing errors to returned data
     $data['last_errors'] = array();
     if (isset($DEBUG_SQL['errors'])) {
         $data['last_errors'] = implode(', ', $DEBUG_SQL['errors']);
         unset($DEBUG_SQL['errors']);
     }
     // restore glpi mode to previous
     $_SESSION['glpi_use_mode'] = $glpi_use_mode;
     // do not store this search from session
     Search::resetSaveSearch();
     return $data;
 }
Beispiel #2
0
 /**
  * Expose the GLPI searchEngine
  *
  * @param $itemtype     string    itemtype (class) of object
  * @param $params       array     with theses options :
  *    - 'criteria': array of criterion object to filter search.
  *        Optionnal.
  *        Each criterion object must provide :
  *           - link: (optionnal for 1st element) logical operator in [AND, OR, AND NOT, AND NOT].
  *           - field: id of searchoptions.
  *           - searchtype: type of search in [contains, equals, notequals, lessthan, morethan, under, notunder].
  *           - value : value to search.
  *    - 'metacriteria' (optionnal): array of metacriterion object to filter search.
  *                                  Optionnal.
  *                                  A meta search is a link with another itemtype
  *                                  (ex: Computer with softwares).
  *         Each metacriterion object must provide :
  *            - link: logical operator in [AND, OR, AND NOT, AND NOT]. Mandatory
  *            - itemtype: second itemtype to link.
  *            - field: id of searchoptions.
  *            - searchtype: type of search in [contains, equals, notequals, lessthan, morethan, under, notunder].
  *            - value : value to search.
  *    - 'sort' :  id of searchoption to sort by (default 1). Optionnal.
  *    - 'order' : ASC - Ascending sort / DESC Descending sort (default ASC). Optionnal.
  *    - 'range' : a string with a couple of number for start and end of pagination separated by a '-'. Ex : 150-200. (default 0-50)
  *                Optionnal.
  *    - 'forcedisplay': array of columns to display (default empty = empty use display pref and search criterias).
  *                      Some columns will be always presents (1-id, 2-name, 80-Entity).
  *                      Optionnal.
  *    - 'rawdata': boolean for displaying raws data of Search engine of glpi (like sql request, and full searchoptions)
  *
  * @return     Array   of raw rows from Search class
  */
 protected function searchItems($itemtype, $params = array())
 {
     global $DEBUG_SQL;
     $this->initEndpoint();
     // check rights
     if ($itemtype != 'AllAssets' && !$itemtype::canView()) {
         return $this->messageRightError();
     }
     // retrieve searchoptions
     $soptions = $this->listSearchOptions($itemtype);
     // Check the criterias are valid
     if (isset($params['criteria']) && is_array($params['criteria'])) {
         foreach ($params['criteria'] as $criteria) {
             if (isset($criteria['field']) && ctype_digit($criteria['field']) && !array_key_exists($criteria['field'], $soptions)) {
                 return $this->returnError(__("Bad field ID in search criteria"));
             }
         }
     }
     // manage forcedisplay
     if (isset($params['forcedisplay'])) {
         if (!is_array($params['forcedisplay'])) {
             $params['forcedisplay'] = array(intval($params['forcedisplay']));
         }
         $params['forcedisplay'] = array_combine($params['forcedisplay'], $params['forcedisplay']);
     } else {
         $params['forcedisplay'] = array();
     }
     // transform range parameter in start and limit variables
     if (isset($params['range']) > 0) {
         if (preg_match("/^[0-9]+-[0-9]+\$/", $params['range'])) {
             $range = explode("-", $params['range']);
             $params['start'] = $range[0];
             $params['list_limit'] = $range[1] - $range[0] + 1;
             $params['range'] = $range;
         } else {
             $this->returnError("range must be in format : [start-end] with integers");
         }
     } else {
         $params['range'] = array(0, $_SESSION['glpilist_limit']);
     }
     // force reset
     $params['reset'] = 'reset';
     // force logging sql queries
     $_SESSION['glpi_use_mode'] = Session::DEBUG_MODE;
     // call Core Search method
     $rawdata = Search::getDatas($itemtype, $params, $params['forcedisplay']);
     // probably a sql error
     if (!isset($rawdata['data']) || count($rawdata['data']) === 0) {
         $this->returnError("Unexpected SQL Error : " . array_splice($DEBUG_SQL['errors'], -2)[0], 500, "ERROR_SQL", false);
     }
     $cleaned_data = array('totalcount' => $rawdata['data']['totalcount'], 'count' => count($rawdata['data']['rows']), 'sort' => $rawdata['search']['sort'], 'order' => $rawdata['search']['order']);
     if ($params['range'][0] > $cleaned_data['totalcount']) {
         $this->returnError("Provided range exceed total count of data: " . $cleaned_data['totalcount'], 400, "ERROR_RANGE_EXCEED_TOTAL");
     }
     // fix end range
     if ($params['range'][1] > $cleaned_data['totalcount'] - 1) {
         $params['range'][1] = $cleaned_data['totalcount'] - 1;
     }
     //prepare cols (searchoptions_id) for cleaned data
     $cleaned_cols = array();
     foreach ($rawdata['data']['cols'] as $col) {
         $cleaned_cols[] = $col['id'];
     }
     // prepare cols wwith uid
     if (isset($params['uid_cols'])) {
         $uid_cols = array();
         foreach ($cleaned_cols as $col) {
             $uid_cols[] = $soptions[$col]['uid'];
         }
     }
     foreach ($rawdata['data']['rows'] as $row) {
         $raw = $row['raw'];
         $id = $raw['id'];
         // keep row itemtype for all asset
         if ($itemtype == 'AllAssets') {
             $current_id = $raw['id'];
             $current_itemtype = $raw['TYPE'];
         }
         // retrive value (and manage multiple values)
         $clean_values = array();
         foreach ($row as $rkey => $rvalues) {
             // skip index who are not real columns (ex: raw, entities_id, etc)
             if (!is_integer($rkey)) {
                 continue;
             }
             // manage multiple values (ex: IP adresses)
             $current_values = array();
             for ($valindex = 0; $valindex < $rvalues['count']; $valindex++) {
                 $current_values[] = $rvalues[$valindex]['name'];
             }
             if (count($current_values) == 1) {
                 $current_values = $current_values[0];
             }
             $clean_values[] = $current_values;
         }
         // combine cols (searchoptions_id) with values (raws data)
         if (isset($params['uid_cols'])) {
             $current_line = array_combine($uid_cols, $clean_values);
         } else {
             $current_line = array_combine($cleaned_cols, $clean_values);
         }
         // if all asset, provide type in returned data
         if ($itemtype == 'AllAssets') {
             $current_line['id'] = $current_id;
             $current_line['itemtype'] = $current_itemtype;
         }
         // append to final array
         if (isset($params['withindexes'])) {
             $cleaned_data['data'][$id] = $current_line;
         } else {
             $cleaned_data['data'][] = $current_line;
         }
     }
     // add rows with their html
     if (isset($params['giveItems'])) {
         $cleaned_data['data_html'] = array();
         foreach ($rawdata['data']['rows'] as $row) {
             $new_row = array();
             foreach ($row as $cell_key => $cell) {
                 if (isset($cell['displayname'])) {
                     $new_row[$cell_key] = $cell['displayname'];
                 }
             }
             $new_row = array_combine($cleaned_cols, $new_row);
             if (isset($params['withindexes'])) {
                 $cleaned_data['data_html'][$row['id']] = $new_row;
             } else {
                 $cleaned_data['data_html'][] = $new_row;
             }
         }
     }
     if (isset($params['rawdata']) && $params['rawdata']) {
         $cleaned_data['rawdata'] = $rawdata;
     }
     $cleaned_data['content-range'] = implode('-', $params['range']) . "/" . $cleaned_data['totalcount'];
     // return data
     return $cleaned_data;
 }
 /**
  * Get computers belonging to a dynamic group
  * @since 0.85+1.0
  * @param group the group object
  * @return an array of computer ids
  */
 static function getTargetsByGroup(PluginFusioninventoryDeployGroup $group)
 {
     $search_params = PluginFusioninventoryDeployGroup::getSearchParamsAsAnArray($group, false, true);
     if (isset($search_params['metacriteria']) && empty($search_params['metacriteria'])) {
         unset($search_params['metacriteria']);
     }
     $search_params['sort'] = '';
     //Only retrieve computers IDs
     $results = Search::getDatas('PluginFusioninventoryComputer', $search_params, array('2'));
     $ids = array();
     foreach ($results['data']['rows'] as $id => $row) {
         $ids[$row['id']] = $row['id'];
     }
     return $ids;
 }