ryxeo-glpi-git / ajax / dropdownValue.php @ b67d8923
Historique | Voir | Annoter | Télécharger (10 ko)
1 | b67d8923 | Eric Seigne | <?php
|
---|---|---|---|
2 | /*
|
||
3 | * @version $Id: dropdownValue.php 7882 2009-01-23 18:24:05Z moyo $
|
||
4 | -------------------------------------------------------------------------
|
||
5 | GLPI - Gestionnaire Libre de Parc Informatique
|
||
6 | Copyright (C) 2003-2009 by the INDEPNET Development Team.
|
||
7 | |||
8 | http://indepnet.net/ http://glpi-project.org
|
||
9 | -------------------------------------------------------------------------
|
||
10 | |||
11 | LICENSE
|
||
12 | |||
13 | This file is part of GLPI.
|
||
14 | |||
15 | GLPI is free software; you can redistribute it and/or modify
|
||
16 | it under the terms of the GNU General Public License as published by
|
||
17 | the Free Software Foundation; either version 2 of the License, or
|
||
18 | (at your option) any later version.
|
||
19 | |||
20 | GLPI is distributed in the hope that it will be useful,
|
||
21 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
23 | GNU General Public License for more details.
|
||
24 | |||
25 | You should have received a copy of the GNU General Public License
|
||
26 | along with GLPI; if not, write to the Free Software
|
||
27 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||
28 | --------------------------------------------------------------------------
|
||
29 | */
|
||
30 | |||
31 | // ----------------------------------------------------------------------
|
||
32 | // Original Author of file: Julien Dombre
|
||
33 | // Purpose of file:
|
||
34 | // ----------------------------------------------------------------------
|
||
35 | |||
36 | // Direct access to file
|
||
37 | if(ereg("dropdownValue.php",$_SERVER['PHP_SELF'])){ |
||
38 | define('GLPI_ROOT','..'); |
||
39 | // Include plugin if it is a plugin table
|
||
40 | if (!ereg("plugin",$_POST['table'])){ |
||
41 | $AJAX_INCLUDE=1; |
||
42 | } |
||
43 | include (GLPI_ROOT."/inc/includes.php"); |
||
44 | header("Content-Type: text/html; charset=UTF-8"); |
||
45 | header_nocache(); |
||
46 | }; |
||
47 | |||
48 | if (!defined('GLPI_ROOT')){ |
||
49 | die("Can not acces directly to this file"); |
||
50 | } |
||
51 | |||
52 | checkLoginUser(); |
||
53 | |||
54 | |||
55 | if (isset($_POST["entity_restrict"])&&!is_numeric($_POST["entity_restrict"])&&!is_array($_POST["entity_restrict"])){ |
||
56 | $_POST["entity_restrict"]=unserialize(stripslashes($_POST["entity_restrict"])); |
||
57 | } |
||
58 | // Make a select box with preselected values
|
||
59 | if (!isset($_POST["limit"])) $_POST["limit"]=$CFG_GLPI["dropdown_limit"]; |
||
60 | |||
61 | $where="WHERE 1 "; |
||
62 | |||
63 | if (in_array($_POST['table'],$CFG_GLPI["deleted_tables"])){ |
||
64 | $where.=" AND deleted=0 "; |
||
65 | } |
||
66 | if (in_array($_POST['table'],$CFG_GLPI["template_tables"])){ |
||
67 | $where.=" AND is_template=0 "; |
||
68 | } |
||
69 | |||
70 | if (isset($_POST['onlyglobal'])&&$_POST['onlyglobal']){ |
||
71 | $where.=" AND is_global=1 "; |
||
72 | } |
||
73 | |||
74 | $NBMAX=$CFG_GLPI["dropdown_max"]; |
||
75 | $LIMIT="LIMIT 0,$NBMAX"; |
||
76 | if ($_POST['searchText']==$CFG_GLPI["ajax_wildcard"]) $LIMIT=""; |
||
77 | |||
78 | if (in_array($_POST['table'],$CFG_GLPI["dropdowntree_tables"])){ |
||
79 | if ($_POST['searchText']!=$CFG_GLPI["ajax_wildcard"]){ |
||
80 | $where.=" AND completename ".makeTextSearch($_POST['searchText']); |
||
81 | } |
||
82 | |||
83 | |||
84 | $multi=false; |
||
85 | |||
86 | // Manage multiple Entities dropdowns
|
||
87 | $add_order=""; |
||
88 | if (in_array($_POST['table'],$CFG_GLPI["specif_entities_tables"])||$_POST['table']=='glpi_entities'){ |
||
89 | |||
90 | $multi=in_array($_POST['table'],$CFG_GLPI["recursive_type"]); |
||
91 | |||
92 | $field='FK_entities'; |
||
93 | $add_order=" FK_entities, "; |
||
94 | if ($_POST['table']=='glpi_entities'){ |
||
95 | $field='ID'; |
||
96 | $add_order=" "; |
||
97 | |||
98 | } |
||
99 | |||
100 | if (isset($_POST["entity_restrict"]) && !($_POST["entity_restrict"]<0)){ |
||
101 | $where.=getEntitiesRestrictRequest(" AND ",$_POST['table'],$field,$_POST["entity_restrict"]); |
||
102 | if (is_array($_POST["entity_restrict"]) && count($_POST["entity_restrict"])>1) { |
||
103 | $multi=true; |
||
104 | } |
||
105 | } else {
|
||
106 | $where.=getEntitiesRestrictRequest(" AND ",$_POST['table'],$field); |
||
107 | if (count($_SESSION['glpiactiveentities'])>1) { |
||
108 | $multi=true; |
||
109 | } |
||
110 | } |
||
111 | if ($_POST['table']=='glpi_entities'){ |
||
112 | $multi=false; |
||
113 | } |
||
114 | } |
||
115 | |||
116 | $query = "SELECT * FROM `".$_POST['table']."` $where ORDER BY $add_order completename $LIMIT"; |
||
117 | //error_log("SQL1:".$query);
|
||
118 | $result = $DB->query($query); |
||
119 | |||
120 | echo "<select id='dropdown_".$_POST["myname"].$_POST["rand"]."' name=\"".$_POST['myname']."\" size='1'>"; |
||
121 | |||
122 | if ($_POST['searchText']!=$CFG_GLPI["ajax_wildcard"]&&$DB->numrows($result)==$NBMAX) |
||
123 | echo "<option class='tree' value=\"0\">--".$LANG["common"][11]."--</option>"; |
||
124 | $display_selected=true; |
||
125 | switch ($_POST["table"]){ |
||
126 | case "glpi_dropdown_kbcategories" : |
||
127 | echo "<option class='tree' value=\"0\">--".$LANG["knowbase"][12]."--</option>"; |
||
128 | break;
|
||
129 | case "glpi_entities" : |
||
130 | // If entity=0 allowed
|
||
131 | if (isset($_POST["entity_restrict"])&& |
||
132 | (($_POST["entity_restrict"]<0 && in_array(0,$_SESSION['glpiactiveentities'])) |
||
133 | || (is_array($_POST["entity_restrict"]) && in_array(0,$_POST["entity_restrict"])))) |
||
134 | { |
||
135 | echo "<option class='tree' value=\"0\">--".$LANG["entity"][2]."--</option>"; |
||
136 | } |
||
137 | // Entity=0 already add above
|
||
138 | if ($_POST['value']==0){ |
||
139 | $display_selected=false; |
||
140 | } |
||
141 | break;
|
||
142 | default :
|
||
143 | echo "<option class='tree' value=\"0\">-----</option>"; |
||
144 | break;
|
||
145 | } |
||
146 | |||
147 | if ($display_selected){ |
||
148 | $outputval=getDropdownName($_POST['table'],$_POST['value']); |
||
149 | if (!empty($outputval)&&$outputval!=" ") |
||
150 | echo "<option class='tree' selected value='".$_POST['value']."'>".$outputval."</option>"; |
||
151 | } |
||
152 | |||
153 | if ($DB->numrows($result)) { |
||
154 | $prev=-1; |
||
155 | while ($data =$DB->fetch_array($result)) { |
||
156 | |||
157 | $ID = $data['ID']; |
||
158 | $level = $data['level']; |
||
159 | |||
160 | $output=$data['name']; |
||
161 | |||
162 | if ($multi && $data["FK_entities"]!=$prev) { |
||
163 | if ($prev>=0) { |
||
164 | echo "</optgroup>"; |
||
165 | } |
||
166 | $prev=$data["FK_entities"]; |
||
167 | echo "<optgroup label=\"". getDropdownName("glpi_entities", $prev) ."\">"; |
||
168 | } |
||
169 | |||
170 | $class=" class='tree' "; |
||
171 | $raquo=""; |
||
172 | if ($level==1){ |
||
173 | $class=" class='treeroot' "; |
||
174 | $raquo=""; |
||
175 | } |
||
176 | |||
177 | if ($CFG_GLPI['flat_dropdowntree']){ |
||
178 | $output=$data['completename']; |
||
179 | if ($level>1){ |
||
180 | $class=""; |
||
181 | $raquo=""; |
||
182 | $level=0; |
||
183 | } |
||
184 | } |
||
185 | |||
186 | if (empty($output)) { |
||
187 | $output="($ID)"; |
||
188 | } |
||
189 | |||
190 | $style=$class; |
||
191 | $addcomment=""; |
||
192 | if (isset($data["comments"])) $addcomment=" - ".$data["comments"]; |
||
193 | |||
194 | echo "<option value=\"$ID\" $style title=\"".cleanInputText($data['completename'].$addcomment)."\">".str_repeat(" ", $level).$raquo.utf8_substr($output,0,$_POST["limit"])."</option>"; |
||
195 | } |
||
196 | |||
197 | } |
||
198 | echo "</select>"; |
||
199 | |||
200 | } else { // Not dropdowntree_tables |
||
201 | $where .=" AND ID NOT IN (".$_POST['value']; |
||
202 | if (isset($_POST['used'])) { |
||
203 | if (is_array($_POST['used'])) { |
||
204 | $used=$_POST['used']; |
||
205 | } else {
|
||
206 | $used=unserialize(stripslashes($_POST['used'])); |
||
207 | } |
||
208 | foreach($used as $val) |
||
209 | $where .= ", $val"; |
||
210 | } |
||
211 | $where .= ") "; |
||
212 | |||
213 | $multi=false; |
||
214 | if (in_array($_POST['table'],$CFG_GLPI["specif_entities_tables"])){ |
||
215 | $multi=in_array($_POST['table'],$CFG_GLPI["recursive_type"]); |
||
216 | |||
217 | if (isset($_POST["entity_restrict"]) && !($_POST["entity_restrict"]<0)){ |
||
218 | $where.=getEntitiesRestrictRequest("AND",$_POST['table'],"FK_entities",$_POST["entity_restrict"],$multi); |
||
219 | if (is_array($_POST["entity_restrict"]) && count($_POST["entity_restrict"])>1) { |
||
220 | $multi=true; |
||
221 | } |
||
222 | } else {
|
||
223 | $where.=getEntitiesRestrictRequest("AND",$_POST['table'],'','',$multi); |
||
224 | if (count($_SESSION['glpiactiveentities'])>1) { |
||
225 | $multi=true; |
||
226 | } |
||
227 | } |
||
228 | } |
||
229 | |||
230 | $field="name"; |
||
231 | if (ereg("glpi_device",$_POST['table'])) $field="designation"; |
||
232 | |||
233 | if ($_POST['searchText']!=$CFG_GLPI["ajax_wildcard"]) |
||
234 | $where.=" AND $field ".makeTextSearch($_POST['searchText']); |
||
235 | |||
236 | switch ($_POST['table']){ |
||
237 | case "glpi_contacts": |
||
238 | $query = "SELECT FK_entities, CONCAT(name,' ',firstname) as $field, `".$_POST['table']."`.comments, ".$_POST['table'].".ID FROM `".$_POST['table']."` $where"; |
||
239 | break;
|
||
240 | default :
|
||
241 | $query = "SELECT * FROM `".$_POST['table']."` $where"; |
||
242 | break;
|
||
243 | } |
||
244 | if ($multi) { |
||
245 | $query.=" ORDER BY FK_entities, $field $LIMIT"; |
||
246 | } else {
|
||
247 | $query.=" ORDER BY $field $LIMIT"; |
||
248 | } |
||
249 | //error_log("SQL2:".$query);
|
||
250 | $result = $DB->query($query); |
||
251 | |||
252 | echo "<select id='dropdown_".$_POST["myname"].$_POST["rand"]."' name=\"".$_POST['myname']."\" size='1'>"; |
||
253 | |||
254 | if ($_POST['searchText']!=$CFG_GLPI["ajax_wildcard"]&&$DB->numrows($result)==$NBMAX) |
||
255 | echo "<option value=\"0\">--".$LANG["common"][11]."--</option>"; |
||
256 | |||
257 | echo "<option value=\"0\">-----</option>"; |
||
258 | |||
259 | $output=getDropdownName($_POST['table'],$_POST['value']); |
||
260 | if (!empty($output)&&$output!=" "){ |
||
261 | echo "<option selected value='".$_POST['value']."'>".$output."</option>"; |
||
262 | } |
||
263 | |||
264 | if ($DB->numrows($result)) { |
||
265 | $prev=-1; |
||
266 | while ($data =$DB->fetch_array($result)) { |
||
267 | $output = $data[$field]; |
||
268 | if (isset($_POST['withserial'])&&isset($data["serial"])) $output.=" - ".$data["serial"]; |
||
269 | if (isset($_POST['withotherserial'])&&isset($data["otherserial"])) $output.=" - ".$data["otherserial"]; |
||
270 | $ID = $data['ID']; |
||
271 | $addcomment=""; |
||
272 | if (isset($data["comments"])) $addcomment=" - ".$data["comments"]; |
||
273 | |||
274 | if (empty($output)) $output="($ID)"; |
||
275 | if ($multi && $data["FK_entities"]!=$prev) { |
||
276 | if ($prev>=0) { |
||
277 | echo "</optgroup>"; |
||
278 | } |
||
279 | $prev=$data["FK_entities"]; |
||
280 | echo "<optgroup label=\"". getDropdownName("glpi_entities", $prev) ."\">"; |
||
281 | } |
||
282 | echo "<option value=\"$ID\" title=\"".cleanInputText($output.$addcomment)."\">".utf8_substr($output,0,$_POST["limit"])."</option>"; |
||
283 | } |
||
284 | if ($multi) { |
||
285 | echo "</optgroup>"; |
||
286 | } |
||
287 | } |
||
288 | echo "</select>"; |
||
289 | } |
||
290 | |||
291 | if (isset($_POST["comments"])&&$_POST["comments"]){ |
||
292 | $params=array('value'=>'__VALUE__','table'=>$_POST["table"]); |
||
293 | ajaxUpdateItemOnSelectEvent("dropdown_".$_POST["myname"].$_POST["rand"],"comments_".$_POST["myname"].$_POST["rand"],$CFG_GLPI["root_doc"]."/ajax/comments.php",$params,false); |
||
294 | } |
||
295 | |||
296 | if (isset($_POST["update_item"])&& |
||
297 | (is_array($_POST["update_item"])||strlen($_POST["update_item"])>0)){ |
||
298 | if (!is_array($_POST["update_item"])){ |
||
299 | $data=unserialize(stripslashes($_POST["update_item"])); |
||
300 | } else $data=$_POST["update_item"]; |
||
301 | |||
302 | if (is_array($data)&&count($data)){ |
||
303 | $params=array(); |
||
304 | if (isset($data['value_fieldname'])){ |
||
305 | $params=array($data['value_fieldname']=>'__VALUE__'); |
||
306 | } |
||
307 | if (isset($data["moreparams"])&&is_array($data["moreparams"])&&count($data["moreparams"])){ |
||
308 | foreach ($data["moreparams"] as $key => $val){ |
||
309 | $params[$key]=$val; |
||
310 | } |
||
311 | } |
||
312 | ajaxUpdateItemOnSelectEvent("dropdown_".$_POST["myname"].$_POST["rand"],$data['to_update'],$data['url'],$params,false); |
||
313 | } |
||
314 | } |
||
315 | |||
316 | |||
317 | ?> |