Search::prepareDatasForSearch PHP 메소드

prepareDatasForSearch() 정적인 공개 메소드

Prepare search criteria to be used for a search
static public prepareDatasForSearch ( $itemtype, array $params, array $forcedisplay = [] ) : array
$itemtype item type
$params array array of parameters may include sort, order, start, list_limit, deleted, criteria, metacriteria
$forcedisplay array array of columns to display (default empty = empty use display pref and search criterias)
리턴 array prepare to be used for a search (include criterias and others needed informations)
    static function prepareDatasForSearch($itemtype, array $params, array $forcedisplay = array())
    {
        global $CFG_GLPI;
        // Default values of parameters
        $p['criteria'] = array();
        $p['metacriteria'] = array();
        $p['sort'] = '1';
        //
        $p['order'] = 'ASC';
        //
        $p['start'] = 0;
        //
        $p['is_deleted'] = 0;
        $p['export_all'] = 0;
        if (class_exists($itemtype)) {
            $p['target'] = $itemtype::getSearchURL();
        } else {
            $p['target'] = Toolbox::getItemTypeSearchURL($itemtype);
        }
        $p['display_type'] = self::HTML_OUTPUT;
        $p['list_limit'] = $_SESSION['glpilist_limit'];
        $p['massiveactionparams'] = array();
        foreach ($params as $key => $val) {
            switch ($key) {
                case 'order':
                    if (in_array($val, array('ASC', 'DESC'))) {
                        $p[$key] = $val;
                    }
                    break;
                case 'sort':
                    $p[$key] = intval($val);
                    if ($p[$key] <= 0) {
                        $p[$key] = 1;
                    }
                    break;
                case 'is_deleted':
                    if ($val == 1) {
                        $p[$key] = '1';
                    }
                    break;
                default:
                    $p[$key] = $val;
                    break;
            }
        }
        // Set display type for export if define
        if (isset($p['display_type'])) {
            // Limit to 10 element
            if ($p['display_type'] == self::GLOBAL_SEARCH) {
                $p['list_limit'] = self::GLOBAL_DISPLAY_COUNT;
            }
        }
        if ($p['export_all']) {
            $p['start'] = 0;
        }
        $data = array();
        $data['search'] = $p;
        $data['itemtype'] = $itemtype;
        // Instanciate an object to access method
        $data['item'] = NULL;
        if ($itemtype != 'AllAssets') {
            $data['item'] = getItemForItemtype($itemtype);
        }
        $data['display_type'] = $data['search']['display_type'];
        if (!$CFG_GLPI['allow_search_all']) {
            foreach ($p['criteria'] as $val) {
                if (isset($val['field']) && $val['field'] == 'all') {
                    Html::displayRightError();
                }
            }
        }
        if (!$CFG_GLPI['allow_search_view']) {
            foreach ($p['criteria'] as $val) {
                if (isset($val['field']) && $val['field'] == 'view') {
                    Html::displayRightError();
                }
            }
        }
        /// Get the items to display
        // Add searched items
        $forcetoview = false;
        if (is_array($forcedisplay) && count($forcedisplay)) {
            $forcetoview = true;
        }
        $data['search']['all_search'] = false;
        $data['search']['view_search'] = false;
        // If no research limit research to display item and compute number of item using simple request
        $data['search']['no_search'] = true;
        $data['toview'] = self::addDefaultToView($itemtype);
        if (!$forcetoview) {
            // Add items to display depending of personal prefs
            $displaypref = DisplayPreference::getForTypeUser($itemtype, Session::getLoginUserID());
            if (count($displaypref)) {
                foreach ($displaypref as $val) {
                    array_push($data['toview'], $val);
                }
            }
        } else {
            $data['toview'] = array_merge($data['toview'], $forcedisplay);
        }
        if (count($p['criteria']) > 0) {
            foreach ($p['criteria'] as $key => $val) {
                if (isset($val['field']) && !in_array($val['field'], $data['toview'])) {
                    if ($val['field'] != 'all' && $val['field'] != 'view') {
                        array_push($data['toview'], $val['field']);
                    } else {
                        if ($val['field'] == 'all') {
                            $data['search']['all_search'] = true;
                        } else {
                            if ($val['field'] == 'view') {
                                $data['search']['view_search'] = true;
                            }
                        }
                    }
                }
                if (isset($val['value']) && strlen($val['value']) > 0) {
                    $data['search']['no_search'] = false;
                }
            }
        }
        if (count($p['metacriteria'])) {
            $data['search']['no_search'] = false;
        }
        // Add order item
        if (!in_array($p['sort'], $data['toview'])) {
            array_push($data['toview'], $p['sort']);
        }
        // Special case for Ticket : put ID in front
        if ($itemtype == 'Ticket') {
            array_unshift($data['toview'], 2);
        }
        $limitsearchopt = self::getCleanedOptions($itemtype);
        // Clean and reorder toview
        $tmpview = array();
        foreach ($data['toview'] as $key => $val) {
            if (isset($limitsearchopt[$val]) && !in_array($val, $tmpview)) {
                $tmpview[] = $val;
            }
        }
        $data['toview'] = $tmpview;
        $data['tocompute'] = $data['toview'];
        // Force item to display
        if ($forcetoview) {
            foreach ($data['toview'] as $val) {
                if (!in_array($val, $data['tocompute'])) {
                    array_push($data['tocompute'], $val);
                }
            }
        }
        return $data;
    }

Usage Example

 /**
  * Display list of hosts
  */
 function showHostsBoard($params, $width = '', $limit = '')
 {
     global $DB, $CFG_GLPI;
     if (!isset($_SESSION['plugin_monitoring_reduced_interface'])) {
         $_SESSION['plugin_monitoring_reduced_interface'] = false;
     }
     $col_to_display = array(0, 10, 1, 2, 3, 6, 7, 8, 9, 11);
     $data = Search::prepareDatasForSearch('PluginMonitoringHost', $params, $col_to_display);
     $data['tocompute'] = $data['toview'];
     Search::constructSQL($data);
     //echo "<pre>";      print_r($data['sql']['search']);
     Search::constructDatas($data);
     //      if (! isset($_GET['order'])) {
     //         $_GET['order'] = "ASC";
     //      }
     //      if (! isset($_GET['sort'])) {
     //         $_GET['sort'] = "";
     //      }
     //
     //      $order = "ASC";
     //      if (isset($_GET['order'])) {
     //         $order = $_GET['order'];
     //      }
     //      $where = '';
     //      if (isset($_GET['field'])) {
     //         foreach ($_GET['field'] as $key=>$value) {
     //            $wheretmp = '';
     //            if (isset($_GET['link'][$key])) {
     //               $wheretmp.= " ".$_GET['link'][$key]." ";
     //            }
     //            $wheretmp .= Search::addWhere(
     //                                   "",
     //                                   0,
     //                                   "PluginMonitoringHost",
     //                                   $_GET['field'][$key],
     //                                   $_GET['searchtype'][$key],
     //                                   $_GET['contains'][$key]);
     //            if (!strstr($wheretmp, "``.``")) {
     //               if ($where != ''
     //                       AND !isset($_GET['link'][$key])) {
     //                  $where .= " AND ";
     //               }
     //               $where .= $wheretmp;
     //            }
     //         }
     //      }
     //      if ($where != '') {
     //         $where = "(".$where;
     //         $where .= ") AND ";
     //      }
     //      $where .= " CONCAT_WS('', `glpi_computers`.`entities_id`, `glpi_printers`.`entities_id`, `glpi_networkequipments`.`entities_id`) IN (".$_SESSION['glpiactiveentities_string'].")";
     //
     //      if ($where != '') {
     //         $where = " WHERE ".$where;
     //         $where = str_replace("`".getTableForItemType("PluginMonitoringDisplay")."`.",
     //                 "", $where);
     //
     //      }
     //
     //      $leftjoin = "
     //         LEFT JOIN `glpi_computers`
     //            ON `glpi_plugin_monitoring_hosts`.`items_id` = `glpi_computers`.`id`
     //               AND `glpi_plugin_monitoring_hosts`.`itemtype`='Computer'
     //         LEFT JOIN `glpi_printers`
     //            ON `glpi_plugin_monitoring_hosts`.`items_id` = `glpi_printers`.`id`
     //               AND `glpi_plugin_monitoring_hosts`.`itemtype`='Printer'
     //         LEFT JOIN `glpi_networkequipments`
     //            ON `glpi_plugin_monitoring_hosts`.`items_id` = `glpi_networkequipments`.`id`
     //               AND `glpi_plugin_monitoring_hosts`.`itemtype`='NetworkEquipment'
     //         LEFT JOIN `glpi_entities`
     //            ON CONCAT_WS('', `glpi_computers`.`entities_id`, `glpi_printers`.`entities_id`, `glpi_networkequipments`.`entities_id`) = `glpi_entities`.`id`
     //
     //      ";
     //
     //      // * ORDER
     //      $ORDERQUERY = "ORDER BY entity_name ASC, host_name ASC";
     //      $toview = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
     //      $toviewComplete = array(
     //          'ITEM_0' => 'entity_name',
     //          'ITEM_1' => 'host_name',
     //          'ITEM_2' => 'host_state',
     //          'ITEM_3' => 'service_state',
     //          'ITEM_4' => 'last_check',
     //          'ITEM_5' => 'event',
     //          'ITEM_6' => 'perf_data',
     //          'ITEM_7' => 'is_acknowledged'
     //      );
     //      foreach ($toview as $key => $val) {
     //         if ($_GET['sort']==$val) {
     //            $ORDERQUERY = Search::addOrderBy("PluginMonitoringHost", $_GET['sort'],
     //                                             $_GET['order'], $key);
     //            foreach ($toviewComplete as $keyi=>$vali) {
     //               $ORDERQUERY= str_replace($keyi, $vali, $ORDERQUERY);
     //            }
     //         }
     //      }
     //
     ////            `glpi_computers`.*
     //
     //      $query = "SELECT
     //            `glpi_entities`.`name` AS entity_name,
     //            CONCAT_WS('', `glpi_computers`.`id`, `glpi_printers`.`id`, `glpi_networkequipments`.`id`) AS idComputer,
     //            CONCAT_WS('', `glpi_computers`.`name`, `glpi_printers`.`name`, `glpi_networkequipments`.`name`) AS host_name,
     //            `glpi_plugin_monitoring_hosts`.*,
     //            `glpi_plugin_monitoring_hosts`.`state` AS host_state,
     //            `glpi_plugin_monitoring_hosts`.`is_acknowledged` AS host_acknowledged
     //         FROM `glpi_plugin_monitoring_hosts`
     //         ".$leftjoin."
     //         ".$where."
     //         ".$ORDERQUERY;
     //      // Toolbox::logInFile("pm", "Query hosts - $query\n");
     //
     //      $result = $DB->query($query);
     //
     //      if (! isset($_GET["start"])) {
     //         $_GET["start"]=0;
     //      }
     //      $start=$_GET['start'];
     //      if (! isset($_GET["order"])) {
     //         $_GET["order"]="ASC";
     //      }
     $rand = mt_rand();
     if (!isset($data['data']) || !isset($data['data']['totalcount'])) {
         return false;
     }
     // Contruct Pager parameters
     $globallinkto = Toolbox::append_params(array('criteria' => Toolbox::stripslashes_deep($data['search']['criteria']), 'metacriteria' => Toolbox::stripslashes_deep($data['search']['metacriteria'])), '&amp;');
     $parameters = "sort=" . $data['search']['sort'] . "&amp;order=" . $data['search']['order'] . '&amp;' . $globallinkto;
     if (isset($_GET['_in_modal'])) {
         $parameters .= "&amp;_in_modal=1";
     }
     // If the begin of the view is before the number of items
     if ($data['data']['count'] > 0) {
         // Display pager only for HTML
         if ($data['display_type'] == Search::HTML_OUTPUT) {
             $search_config_top = "";
             $search_config_bottom = "";
             Html::printPager($data['search']['start'], $data['data']['totalcount'], $data['search']['target'], $parameters, $data['itemtype'], 0, $search_config_top);
         }
         // Define begin and end var for loop
         // Search case
         $begin_display = $data['data']['begin'];
         $end_display = $data['data']['end'];
     }
     echo '<div id="custom_date" style="display:none"></div>';
     echo '<div id="custom_time" style="display:none"></div>';
     if ($width == '') {
         echo "<table class='tab_cadrehov' style='width:100%;'>";
     } else {
         echo "<table class='tab_cadrehov' style='width:" . $width . "px;'>";
     }
     $num = 0;
     if (Session::haveRight("plugin_monitoring_hostcommand", CREATE)) {
         // Host test command ...
         $pmCommand = new PluginMonitoringCommand();
         $a_commands = array();
         $a_list = $pmCommand->find("command_name LIKE 'host_action'");
         foreach ($a_list as $dt) {
             $host_command_name = $dt['name'];
             $host_command_command = $dt['command_line'];
         }
     }
     echo "<tr class='tab_bg_1'>";
     $this->showHeaderItem(__('Entity'), 0, $num, $begin_display, $globallinkto, 'host.php', 'PluginMonitoringHost');
     $this->showHeaderItem(__('Type'), 0, $num, $begin_display, $globallinkto, 'host.php', 'PluginMonitoringHost');
     $this->showHeaderItem(__('Host', 'monitoring'), 1, $num, $begin_display, $globallinkto, 'host.php', 'PluginMonitoringHost');
     $this->showHeaderItem(__('Host state'), 2, $num, $begin_display, $globallinkto, 'host.php', 'PluginMonitoringHost');
     if (isset($host_command_name)) {
         echo '<th>' . __('Host action', 'monitoring') . '</th>';
     }
     echo '<th>' . __('Host resources state', 'monitoring') . '</th>';
     echo '<th>' . __('IP address', 'monitoring') . '</th>';
     $this->showHeaderItem(__('Last check', 'monitoring'), 4, $num, $begin_display, $globallinkto, 'host.php', 'PluginMonitoringHost');
     $this->showHeaderItem(__('Result details', 'monitoring'), 5, $num, $begin_display, $globallinkto, 'host.php', 'PluginMonitoringHost');
     $this->showHeaderItem(__('Performance data', 'monitoring'), 6, $num, $begin_display, $globallinkto, 'host.php', 'PluginMonitoringHost');
     if (Session::haveRight("plugin_monitoring_acknowledge", READ) || Session::haveRight("plugin_monitoring_downtime", READ)) {
         $this->showHeaderItem(__('Maintenance', 'monitoring'), 7, $num, $begin_display, $globallinkto, 'host.php', 'PluginMonitoringHost');
     }
     echo "</tr>";
     foreach ($data['data']['rows'] as $row) {
         // Reduced array or not ?
         if ($_SESSION['plugin_monitoring_reduced_interface'] && $row[3]['displayname'] == 'UP') {
             continue;
         }
         if (isset($host_command_name)) {
             $row['host_command_name'] = $host_command_name;
             $row['host_command_command'] = $host_command_command;
         }
         // Get all host services except if state is ok or is already acknowledged ...
         $a_ret = PluginMonitoringHost::getServicesState($row['id'], "`glpi_plugin_monitoring_services`.`state` != 'OK'\n                                                         AND `glpi_plugin_monitoring_services`.`is_acknowledged` = '0'");
         $row['host_services_state'] = $a_ret[0];
         $row['host_services_state_list'] = $a_ret[1];
         // Get host first IP address
         $row['ip'] = __('Unknown IP address', 'monitoring');
         $ip = PluginMonitoringHostaddress::getIp($row[9]['displayname'], $row[1]['displayname'], '');
         if ($ip != '') {
             $row['ip'] = $ip;
         }
         echo "<tr class='tab_bg_3'>";
         $this->displayHostLine($row);
         echo "</tr>";
     }
     echo "</table>";
     echo "<br/>";
     Html::printPager($data['search']['start'], $data['data']['totalcount'], $data['search']['target'], $parameters, '', 0, $search_config_bottom);
 }
All Usage Examples Of Search::prepareDatasForSearch