dropdownConnect.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  * @version $Id: dropdownConnect.php 21179 2013-06-25 07:08:30Z moyo $
00004  -------------------------------------------------------------------------
00005  GLPI - Gestionnaire Libre de Parc Informatique
00006  Copyright (C) 2003-2013 by the INDEPNET Development Team.
00007 
00008  http://indepnet.net/   http://glpi-project.org
00009  -------------------------------------------------------------------------
00010 
00011  LICENSE
00012 
00013  This file is part of GLPI.
00014 
00015  GLPI is free software; you can redistribute it and/or modify
00016  it under the terms of the GNU General Public License as published by
00017  the Free Software Foundation; either version 2 of the License, or
00018  (at your option) any later version.
00019 
00020  GLPI is distributed in the hope that it will be useful,
00021  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  GNU General Public License for more details.
00024 
00025  You should have received a copy of the GNU General Public License
00026  along with GLPI. If not, see <http://www.gnu.org/licenses/>.
00027  --------------------------------------------------------------------------
00028  */
00029 
00030 /** @file
00031 * @brief
00032 */
00033 
00034 if (strpos($_SERVER['PHP_SELF'],"dropdownConnect.php")) {
00035    include ('../inc/includes.php');
00036    header("Content-Type: text/html; charset=UTF-8");
00037    Html::header_nocache();
00038 }
00039 
00040 if (!defined('GLPI_ROOT')) {
00041    die("Can not acces directly to this file");
00042 }
00043 
00044 if (!isset($_POST['fromtype']) || !($fromitem = getItemForItemtype($_POST['fromtype']))) {
00045    exit();
00046 }
00047 
00048 $fromitem->checkGlobal('w');
00049 
00050 if (isset($_POST["used"]) && !is_numeric($_POST["used"]) && !is_array($_POST["used"])) {
00051    $used = Toolbox::decodeArrayFromInput($_POST["used"]);
00052 } else {
00053    $used = $_POST["used"];
00054 }
00055 
00056 if (isset($used[$_POST['itemtype']])) {
00057    $used = $used[$_POST['itemtype']];
00058 } else {
00059    $used = array();
00060 }
00061 
00062 
00063 
00064 if (isset($_POST["entity_restrict"])
00065     && !is_numeric($_POST["entity_restrict"])
00066     && !is_array($_POST["entity_restrict"])) {
00067 
00068    $_POST["entity_restrict"] = Toolbox::decodeArrayFromInput($_POST["entity_restrict"]);
00069 }
00070 
00071 // Make a select box
00072 $table = getTableForItemType($_POST["itemtype"]);
00073 if (!$item = getItemForItemtype($_POST['itemtype'])) {
00074    exit;
00075 }
00076 
00077 $where = "";
00078 
00079 if ($item->maybeDeleted()) {
00080    $where .= " AND `$table`.`is_deleted` = '0' ";
00081 }
00082 if ($item->maybeTemplate()) {
00083    $where .= " AND `$table`.`is_template` = '0' ";
00084 }
00085 
00086 if (isset($_POST['searchText']) && (strlen($_POST['searchText']) > 0)
00087     && ($_POST['searchText'] != $CFG_GLPI["ajax_wildcard"])) {
00088    $where .= " AND (`$table`.`name` ".Search::makeTextSearch($_POST['searchText'])."
00089                     OR `$table`.`otherserial` ".Search::makeTextSearch($_POST['searchText'])."
00090                     OR `$table`.`serial` ".Search::makeTextSearch($_POST['searchText'])." )";
00091 }
00092 
00093 $multi = $item->maybeRecursive();
00094 
00095 if (isset($_POST["entity_restrict"]) && !($_POST["entity_restrict"] < 0)) {
00096    $where .= getEntitiesRestrictRequest(" AND ", $table, '', $_POST["entity_restrict"], $multi);
00097    if (is_array($_POST["entity_restrict"]) && (count($_POST["entity_restrict"]) > 1)) {
00098       $multi = true;
00099    }
00100 
00101 } else {
00102    $where .= getEntitiesRestrictRequest(" AND ", $table, '', $_SESSION['glpiactiveentities'],
00103                                         $multi);
00104    if (count($_SESSION['glpiactiveentities']) > 1) {
00105       $multi = true;
00106    }
00107 }
00108 
00109 $NBMAX = $CFG_GLPI["dropdown_max"];
00110 $LIMIT = "LIMIT 0,$NBMAX";
00111 
00112 if (isset($_POST['searchText']) && ($_POST['searchText'] == $CFG_GLPI["ajax_wildcard"])) {
00113    $LIMIT = "";
00114 }
00115 
00116 $where_used = '';
00117 if (!empty($used)) {
00118    $where_used = " AND `$table`.`id` NOT IN ('".implode("','",$used)."')";
00119 }
00120 
00121 if ($_POST["onlyglobal"]
00122     && ($_POST["itemtype"] != 'Computer')) {
00123    $CONNECT_SEARCH = " WHERE `$table`.`is_global` = '1' ";
00124 } else {
00125    if ($_POST["itemtype"] == 'Computer') {
00126       $CONNECT_SEARCH = " WHERE 1
00127                                 $where_used";
00128    } else {
00129       $CONNECT_SEARCH = " WHERE ((`glpi_computers_items`.`id` IS NULL
00130                                   $where_used)
00131                                  OR `$table`.`is_global` = '1') ";
00132    }
00133 }
00134 
00135 $LEFTJOINCONNECT = "";
00136 
00137 if (($_POST["itemtype"] != 'Computer')
00138      && !$_POST["onlyglobal"]) {
00139    $LEFTJOINCONNECT = " LEFT JOIN `glpi_computers_items`
00140                            ON (`$table`.`id` = `glpi_computers_items`.`items_id`
00141                                AND `glpi_computers_items`.`itemtype` = '".$_POST['itemtype']."')";
00142 }
00143 
00144 $query = "SELECT DISTINCT `$table`.`id`,
00145                           `$table`.`name` AS name,
00146                           `$table`.`serial` AS serial,
00147                           `$table`.`otherserial` AS otherserial,
00148                           `$table`.`entities_id` AS entities_id
00149           FROM `$table`
00150           $LEFTJOINCONNECT
00151           $CONNECT_SEARCH
00152                 $where
00153           ORDER BY entities_id,
00154                    name ASC
00155           $LIMIT";
00156 
00157 $result = $DB->query($query);
00158 
00159 echo "<select name='".$_POST['myname']."' size='1'>";
00160 
00161 if (isset($_POST['searchText'])
00162     && ($_POST['searchText'] != $CFG_GLPI["ajax_wildcard"])
00163     && ($DB->numrows($result) == $NBMAX)) {
00164    echo "<option value='0'>--".__('Limited view')."--</option>";
00165 }
00166 echo "<option value='0'>".Dropdown::EMPTY_VALUE."</option>";
00167 
00168 if ($DB->numrows($result)) {
00169    $prev = -1;
00170 
00171    while ($data = $DB->fetch_assoc($result)) {
00172       if ($multi && $data["entities_id"]!=$prev) {
00173          if ($prev>=0) {
00174             echo "</optgroup>";
00175          }
00176          $prev = $data["entities_id"];
00177          echo "<optgroup label=\"". Dropdown::getDropdownName("glpi_entities", $prev) ."\">";
00178       }
00179       $output = $data['name'];
00180       $ID     = $data['id'];
00181 
00182       if ($_SESSION["glpiis_ids_visible"]
00183           || empty($output)) {
00184          $output = sprintf(__('%1$s (%2$s)'), $output, $ID);
00185       }
00186       if (!empty($data['serial'])) {
00187          $output = sprintf(__('%1$s - %2$s'), $output, $data["serial"]);
00188       }
00189       if (!empty($data['otherserial'])) {
00190          $output = sprintf(__('%1$s - %2$s'), $output, $data["otherserial"]);
00191       }
00192 
00193       echo "<option value='$ID' title=\"".Html::cleanInputText($output)."\">".
00194             Toolbox::substr($output, 0, $_SESSION["glpidropdown_chars_limit"])."</option>";
00195    }
00196 
00197    if ($multi && $prev>=0) {
00198       echo "</optgroup>";
00199    }
00200 }
00201 echo "</select>";
00202 ?>