Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

ryxeo-glpi-git / inc / search.function.php @ b67d8923

Historique | Voir | Annoter | Télécharger (107 ko)

1
<?php
2
/*
3
 * @version $Id: search.function.php 7763 2009-01-06 18:44:50Z 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
if (!defined('GLPI_ROOT')){
32
        die("Sorry. You can't access directly to this file");
33
        }
34

    
35
/** \file search.function.php
36
 * Generic functions for Search Engine
37
 */
38
// Get search_option array / Already include in includes.php
39
if (!isset($SEARCH_OPTION)){
40
        $SEARCH_OPTION=getSearchOptions();
41
}
42

    
43
/**
44
 * Clean search options depending of user active profile
45
 *
46
 * @param $type item type to manage
47
 * @param $action action which is used to manupulate searchoption (r/w)
48
 * @return clean $SEARCH_OPTION array
49
 */
50
function cleanSearchOption($type,$action='r'){
51
        global $CFG_GLPI,$SEARCH_OPTION;
52
        $options=$SEARCH_OPTION[$type];
53
        $todel=array();
54
        if (!haveRight('contract_infocom',$action)&&in_array($type,$CFG_GLPI["infocom_types"])){
55
                $todel=array_merge($todel,array('financial',25,26,27,28,29,30,37,38,50,51,52,53,54,55,56,57,58,59,120,122,130,131,132,133,134,135,136,137,138));
56
        }
57

    
58
        if ($type==COMPUTER_TYPE){
59
                if (!haveRight('networking',$action)){
60
                        $todel=array_merge($todel,array('network',20,21,22,83,84,85));
61
                }
62
                if (!$CFG_GLPI['ocs_mode']||!haveRight('view_ocsng',$action)){
63
                        $todel=array_merge($todel,array('ocsng',100,101,102,103));
64
                }
65
        }
66
        if (!haveRight('notes',$action)){
67
                $todel[]=90;
68
        }
69
        if (count($todel)){
70
                foreach ($todel as $ID){
71
                        if (isset($options[$ID])){
72
                                unset($options[$ID]);
73
                        }
74
                }
75
        }
76

    
77
        return $options;
78
}
79

    
80

    
81
/**
82
 * Completion of the URL $_GET values with the $_SESSION values or define default values
83
 *
84
 *
85
 * @param $type item type to manage
86
 * @param $usesession Use datas save in session
87
 * @param $save Save params to session
88
 * @return nothing
89
 */
90
function manageGetValuesInSearch($type=0,$usesession=true,$save=true){
91
        global $_GET;
92
        $tab=array();
93

    
94

    
95
        $default_values["start"]=0;
96
        $default_values["order"]="ASC";
97
        $default_values["deleted"]=0;
98
        $default_values["distinct"]="N";
99
        $default_values["link"]=array();
100
        $default_values["field"]=array(0=>"view");
101
        $default_values["contains"]=array(0=>"");
102
        $default_values["link2"]=array();
103
        $default_values["field2"]=array(0=>"view");
104
        $default_values["contains2"]=array(0=>"");
105
        $default_values["type2"]="";
106
        $default_values["sort"]=1;
107

    
108
        if ($usesession&&isset($_GET["reset_before"])){
109
                if (isset($_SESSION['glpisearch'][$type])){
110
                        unset($_SESSION['glpisearch'][$type]);
111
                }
112
                if (isset($_SESSION['glpisearchcount'][$type])){
113
                        unset($_SESSION['glpisearchcount'][$type]);
114
                }
115
                if (isset($_SESSION['glpisearchcount2'][$type])){
116
                        unset($_SESSION['glpisearchcount2'][$type]);
117
                }
118
                // Bookmark use
119
                if (isset($_GET["glpisearchcount"])){
120
                        $_SESSION["glpisearchcount"][$type]=$_GET["glpisearchcount"];
121
                }
122
                // Bookmark use
123
                if (isset($_GET["glpisearchcount2"])){
124
                        $_SESSION["glpisearchcount2"][$type]=$_GET["glpisearchcount2"];
125
                }
126
        }
127

    
128
        if (is_array($_GET)&&$save){
129
                foreach ($_GET as $key => $val){
130
                        $_SESSION['glpisearch'][$type][$key]=$val;
131
                }
132
        }
133

    
134
        foreach ($default_values as $key => $val){
135
                if (!isset($_GET[$key])){
136
                        if ($usesession&&isset($_SESSION['glpisearch'][$type][$key])) {
137
                                $_GET[$key]=$_SESSION['glpisearch'][$type][$key];
138
                        } else {
139
                                $_GET[$key] = $val;
140
                                $_SESSION['glpisearch'][$type][$key] = $val;
141
                        }
142
                }
143
        }
144

    
145
        if (!isset($_SESSION["glpisearchcount"][$type])) {
146
                if (isset($_GET["glpisearchcount"])){
147
                        $_SESSION["glpisearchcount"][$type]=$_GET["glpisearchcount"];
148
                } else {
149
                        $_SESSION["glpisearchcount"][$type]=1;
150
                }
151
        }
152
        if (!isset($_SESSION["glpisearchcount2"][$type])) {
153
                // Set in URL for bookmark
154
                if (isset($_GET["glpisearchcount2"])){
155
                        $_SESSION["glpisearchcount2"][$type]=$_GET["glpisearchcount2"];
156
                } else {
157
                        $_SESSION["glpisearchcount2"][$type]=0;
158
                }
159
        }
160

    
161
}
162

    
163
/**
164
 * Print generic search form
165
 *
166
 * 
167
 *
168
 *@param $type type to display the form
169
 *@param $target url to post the form
170
 *@param $field array of the fields selected in the search form
171
 *@param $contains array of the search strings
172
 *@param $sort the "sort by" field value
173
 *@param $deleted the deleted value 
174
 *@param $link array of the link between each search.
175
 *@param $distinct only display distinct items
176
 *@param $contains2 array of the search strings for meta items
177
 *@param $field2 array of the fields selected in the search form for meta items
178
 *@param $type2 type to display the form for meta items
179
 *@param $link2 array of the link between each search. for meta items
180
 *
181
 *@return nothing (diplays)
182
 *
183
 **/
184
function searchForm($type,$target,$field="",$contains="",$sort= "",$deleted= 0,$link="",$distinct="Y",$link2="",$contains2="",$field2="",$type2=""){
185
        global $LANG,$SEARCH_OPTION,$CFG_GLPI,$LINK_ID_TABLE;
186

    
187
        $options=cleanSearchOption($type);
188

    
189
        // Meta search names
190
        $names=array(
191
                        COMPUTER_TYPE => $LANG["Menu"][0],
192
                        //                NETWORKING_TYPE => $LANG["Menu"][1],
193
                        PRINTER_TYPE => $LANG["Menu"][2],
194
                        MONITOR_TYPE => $LANG["Menu"][3],
195
                        PERIPHERAL_TYPE => $LANG["Menu"][16],
196
                        SOFTWARE_TYPE => $LANG["Menu"][4],
197
                        PHONE_TYPE => $LANG["Menu"][34],        
198
                    );
199

    
200
        echo "<form method='get' action=\"$target\">";
201
        echo "<table class='tab_cadre_fixe'>";
202
//        echo "<tr><th colspan='5'>".$LANG["search"][0].":</th></tr>";
203
        echo "<tr class='tab_bg_1'>";
204
        echo "<td>";
205
        echo "<table>";
206

    
207
        // Display normal search parameters
208
        for ($i=0;$i<$_SESSION["glpisearchcount"][$type];$i++){
209
                echo "<tr><td class='right'>";
210
                // First line display add / delete images for normal and meta search items
211
                if ($i==0){
212
                        echo "<a href='".$CFG_GLPI["root_doc"]."/front/computer.php?add_search_count=1&amp;type=$type'><img src=\"".$CFG_GLPI["root_doc"]."/pics/plus.png\" alt='+' title='".$LANG["search"][17]."'></a>&nbsp;&nbsp;&nbsp;&nbsp;";
213
                        if ($_SESSION["glpisearchcount"][$type]>1)
214
                                echo "<a href='".$CFG_GLPI["root_doc"]."/front/computer.php?delete_search_count=1&amp;type=$type'><img src=\"".$CFG_GLPI["root_doc"]."/pics/moins.png\" alt='-' title='".$LANG["search"][18]."'></a>&nbsp;&nbsp;&nbsp;&nbsp;";
215

    
216
                        if (isset($names[$type])){
217
                                echo "<a href='".$CFG_GLPI["root_doc"]."/front/computer.php?add_search_count2=1&amp;type=$type'><img src=\"".$CFG_GLPI["root_doc"]."/pics/meta_plus.png\" alt='+' title='".$LANG["search"][19]."'></a>&nbsp;&nbsp;&nbsp;&nbsp;";
218
                                if ($_SESSION["glpisearchcount2"][$type]>0)
219
                                        echo "<a href='".$CFG_GLPI["root_doc"]."/front/computer.php?delete_search_count2=1&amp;type=$type'><img src=\"".$CFG_GLPI["root_doc"]."/pics/meta_moins.png\" alt='-' title='".$LANG["search"][20]."'></a>&nbsp;&nbsp;&nbsp;&nbsp;";
220
                        }
221
                }
222
                // Display link item
223
                if ($i>0) {
224
                        echo "<select name='link[$i]'>";
225

    
226
                        echo "<option value='AND' ";
227
                        if(is_array($link)&&isset($link[$i]) && $link[$i] == "AND") echo "selected";
228
                        echo ">AND</option>";
229

    
230
                        echo "<option value='OR' ";
231
                        if(is_array($link)&&isset($link[$i]) && $link[$i] == "OR") echo "selected";
232
                        echo ">OR</option>";                
233

    
234
                        echo "<option value='AND NOT' ";
235
                        if(is_array($link)&&isset($link[$i]) && $link[$i] == "AND NOT") echo "selected";
236
                        echo ">AND NOT</option>";
237

    
238
                        echo "<option value='OR NOT' ";
239
                        if(is_array($link)&&isset($link[$i]) && $link[$i] == "OR NOT") echo "selected";
240
                        echo ">OR NOT</option>";
241

    
242
                        echo "</select>&nbsp;";
243
                }
244
                // display search field
245
                echo "<input type='text' size='15' name=\"contains[$i]\" value=\"". (is_array($contains)&&isset($contains[$i])?stripslashes($contains[$i]):"" )."\" >";
246
                echo "&nbsp;";
247
                echo $LANG["search"][10]."&nbsp;";
248

    
249
                // display select box to define serach item
250
                echo "<select name=\"field[$i]\" size='1'>";
251
                echo "<option value='view' ";
252
                if(is_array($field)&&isset($field[$i]) && $field[$i] == "view") echo "selected";
253
                echo ">".$LANG["search"][11]."</option>";
254

    
255
                reset($options);
256
                $first_group=true;
257
                foreach ($options as $key => $val) {
258
                        // print groups
259
                        if (!is_array($val)){
260
                                if (!$first_group) echo "</optgroup>";
261
                                else $first_group=false;
262
                                echo "<optgroup label=\"$val\">";
263
                        }else {
264
                                echo "<option value=\"".$key."\""; 
265
                                if(is_array($field)&&isset($field[$i]) && $key == $field[$i]) echo "selected";
266
                                echo ">". utf8_substr($val["name"],0,32) ."</option>\n";
267
                        }
268
                }
269
                if (!$first_group)
270
                        echo "</optgroup>";
271

    
272
                echo "<option value='all' ";
273
                if(is_array($field)&&isset($field[$i]) && $field[$i] == "all") echo "selected";
274
                echo ">".$LANG["common"][66]."</option>";
275

    
276
                echo "</select>&nbsp;";
277

    
278

    
279
                echo "</td></tr>";
280
        }
281

    
282
        // Display meta search items
283
        $linked=array();
284
        if ($_SESSION["glpisearchcount2"][$type]>0){
285
                // Define meta search items to linked
286
                switch ($type){
287
                        case COMPUTER_TYPE :
288
                                $linked=array(PRINTER_TYPE,MONITOR_TYPE,PERIPHERAL_TYPE,SOFTWARE_TYPE,PHONE_TYPE);
289
                                break;
290
                        /*                        case NETWORKING_TYPE :
291
                                                $linked=array(COMPUTER_TYPE,PRINTER_TYPE,PERIPHERAL_TYPE);
292
                                                break;
293
                         */                        
294
                        case PRINTER_TYPE :
295
                                $linked=array(COMPUTER_TYPE);
296
                                break;
297
                        case MONITOR_TYPE :
298
                                $linked=array(COMPUTER_TYPE);
299
                                break;
300
                        case PERIPHERAL_TYPE :
301
                                $linked=array(COMPUTER_TYPE);
302
                                break;
303
                        case SOFTWARE_TYPE :
304
                                $linked=array(COMPUTER_TYPE);
305
                                break;
306
                        case PHONE_TYPE :
307
                                $linked=array(COMPUTER_TYPE);
308
                                break;
309
                }
310
        }
311

    
312
        if (is_array($linked)&&count($linked)>0)
313
                for ($i=0;$i<$_SESSION["glpisearchcount2"][$type];$i++){
314
                        echo "<tr><td class='left'>";
315
                        $rand=mt_rand();
316

    
317
                        // Display link item (not for the first item)
318
                        //if ($i>0) {
319
                        echo "<select name='link2[$i]'>";
320

    
321
                        echo "<option value='AND' ";
322
                        if(is_array($link2)&&isset($link2[$i]) && $link2[$i] == "AND") echo "selected";
323
                        echo ">AND</option>";
324

    
325
                        echo "<option value='OR' ";
326
                        if(is_array($link2)&&isset($link2[$i]) && $link2[$i] == "OR") echo "selected";
327
                        echo ">OR</option>";                
328

    
329
                        echo "<option value='AND NOT' ";
330
                        if(is_array($link2)&&isset($link2[$i]) && $link2[$i] == "AND NOT") echo "selected";
331
                        echo ">AND NOT</option>";
332

    
333
                        echo "<option value='OR NOT' ";
334
                        if(is_array($link2)&&isset($link2[$i]) && $link2[$i] == "OR NOT") echo "selected";
335
                        echo ">OR NOT</option>";
336

    
337
                        echo "</select>";
338
                        //}
339
                        // Display select of the linked item type available
340
                        echo "<select name='type2[$i]' id='type2_".$type."_".$i."_$rand'>";
341
                        echo "<option value='-1'>-----</option>";
342
                        foreach ($linked as $key)
343
                                echo "<option value='$key'>".utf8_substr($names[$key],0,20)."</option>";
344
                        echo "</select>";
345

    
346
                        // Ajax script for display search meat item
347
                        echo "<span id='show_".$type."_".$i."_$rand'>&nbsp;</span>\n";        
348

    
349
                        $params=array('type'=>'__VALUE__',
350
                                        'num'=>$i,
351
                                        'field'=>(is_array($field2)&&isset($field2[$i])?$field2[$i]:""),
352
                                        'val'=>(is_array($contains2)&&isset($contains2[$i])?$contains2[$i]:""),
353
        
354
                        );
355
                        ajaxUpdateItemOnSelectEvent("type2_".$type."_".$i."_$rand","show_".$type."_".$i."_$rand",$CFG_GLPI["root_doc"]."/ajax/updateSearch.php",$params,false);
356

    
357
                        
358
                        if (is_array($type2)&&isset($type2[$i])&&$type2[$i]>0){
359
                                echo "<script type='text/javascript' >";
360
                                echo "window.document.getElementById('type2_".$type."_".$i."_$rand').value='".$type2[$i]."';";
361
                                echo "</script>\n";
362

    
363
                                $params['type']=$type2[$i];
364
                                ajaxUpdateItem("show_".$type."_".$i."_$rand",$CFG_GLPI["root_doc"]."/ajax/updateSearch.php",$params,false);
365
                        }
366
                        echo "</td></tr>";
367
                }
368

    
369
        echo "</table>";
370
        echo "</td>";
371

    
372
        // Display sort selection
373
        echo "<td>";
374
        echo $LANG["search"][4];
375
        echo "&nbsp;<select name='sort' size='1'>";
376
        reset($options);
377
        $first_group=true;
378
        foreach ($options as $key => $val) {
379
                if (!is_array($val)){
380
                        if (!$first_group) echo "</optgroup>";
381
                        else $first_group=false;
382
                        echo "<optgroup label=\"$val\">";
383
                }else {
384

    
385
                        echo "<option value=\"".$key."\"";
386
                        if($key == $sort) echo " selected";
387
                        echo ">".utf8_substr($val["name"],0,20)."</option>\n";
388
                }
389
        }
390
        if (!$first_group)
391
                echo "</optgroup>";
392

    
393
        echo "</select> ";
394
        echo "</td>";
395

    
396
        // Display deleted selection
397
        echo "<td>";
398
        //        echo "<table>";
399
        if (in_array($LINK_ID_TABLE[$type],$CFG_GLPI["deleted_tables"])){
400
                //echo "<tr><td>";
401
                dropdownYesNo("deleted",$deleted);
402
                echo "<img src=\"".$CFG_GLPI["root_doc"]."/pics/showdeleted.png\" alt='".$LANG["common"][3]."' title='".$LANG["common"][3]."'>";
403
                //echo "</td></tr>";
404
        }
405

    
406
        echo "</td>";
407
        // Display Reset search
408
        echo "<td align='center'>";
409
        echo "<a href='".$CFG_GLPI["root_doc"]."/front/computer.php?reset_search=reset_search&amp;type=$type' ><img title=\"".$LANG["buttons"][16]."\" alt=\"".$LANG["buttons"][16]."\" src='".$CFG_GLPI["root_doc"]."/pics/reset.png' class='calendrier'></a>";
410
        showSaveBookmarkButton(BOOKMARK_SEARCH,$type);
411

    
412
        echo "</td>";
413
        // Display submit button
414
        echo "<td width='80' class='tab_bg_2'>";
415
        echo "<input type='submit' value=\"".$LANG["buttons"][0]."\" class='submit' >";
416
        echo "</td></tr>"; 
417
        echo "</table>";
418
        // Reset to start when submit new search
419
        echo "<input type='hidden' name='start' value='0'>";
420
        echo "</form>";
421

    
422
}
423
/**
424
 * Generic Search and list function
425
 *
426
 *
427
 * Build the query, make the search and list items after a search.
428
 *
429
 *@param $target filename where to go when done.
430
 *@param $field array of fields in witch the search would be done
431
 *@param $type type to display the form
432
 *@param $contains array of the search strings
433
 *@param $distinct display only distinct items
434
 *@param $sort the "sort by" field value
435
 *@param $order ASC or DSC (for mysql query)
436
 *@param $start row number from witch we start the query (limit $start,xxx)
437
 *@param $deleted Query on deleted items or not.
438
 *@param $link array of the link between each search.
439
 *@param $contains2 array of the search strings for meta items
440
 *@param $field2 array of the fields selected in the search form for meta items
441
 *@param $type2 type to display the form for meta items
442
 *@param $link2 array of the link between each search. for meta items
443
 *
444
 *
445
 *@return Nothing (display)
446
 *
447
 **/
448
function showList ($type,$target,$field,$contains,$sort,$order,$start,$deleted,$link,$distinct,$link2="",$contains2="",$field2="",$type2=""){
449
        global $DB,$INFOFORM_PAGES,$SEARCH_OPTION,$LINK_ID_TABLE,$CFG_GLPI,$LANG,$PLUGIN_HOOKS;
450

    
451
        $limitsearchopt=cleanSearchOption($type);
452

    
453
        $itemtable=$LINK_ID_TABLE[$type];
454
        if (isset($CFG_GLPI["union_search_type"][$type])){
455
                $itemtable=$CFG_GLPI["union_search_type"][$type];
456
        }
457
        $LIST_LIMIT=$_SESSION["glpilist_limit"];
458
        if (isset($_GET['export_all'])) {
459
                $start=0;
460
        }
461
        // Set display type for export if define
462
        $output_type=HTML_OUTPUT;
463
        if (isset($_GET["display_type"])){
464
                $output_type=$_GET["display_type"];
465
                // Limit to 10 element
466
                if ($_GET["display_type"]==GLOBAL_SEARCH){
467
                        $LIST_LIMIT=GLOBAL_SEARCH_DISPLAY_COUNT;
468
                }
469
        }
470

    
471
        $entity_restrict=in_array($itemtable,$CFG_GLPI["specif_entities_tables"]);
472

    
473
        // Define meta table where search must be done in HAVING clause
474
        $META_SPECIF_TABLE=array("glpi_device_ram","glpi_device_hdd","glpi_device_processor","glpi_tracking");
475

    
476
        $names=array(
477
                        COMPUTER_TYPE => $LANG["Menu"][0],
478
                        //                NETWORKING_TYPE => $LANG["Menu"][1],
479
                        PRINTER_TYPE => $LANG["Menu"][2],
480
                        MONITOR_TYPE => $LANG["Menu"][3],
481
                        PERIPHERAL_TYPE => $LANG["Menu"][16],
482
                        SOFTWARE_TYPE => $LANG["Menu"][4],
483
                        PHONE_TYPE => $LANG["Menu"][34],
484
                    );        
485

    
486
        // Get the items to display
487
        $toview=addDefaultToView($type);
488

    
489
        // Add default items
490
        $query="SELECT * FROM glpi_display WHERE type='$type' AND FK_users='".$_SESSION["glpiID"]."' ORDER by rank";
491
        $result=$DB->query($query);
492
        // GET default serach options
493
        if ($DB->numrows($result)==0){
494
                $query="SELECT * FROM glpi_display WHERE type='$type' AND FK_users='0' ORDER by rank";
495
                $result=$DB->query($query);
496
        }
497

    
498
        if ($DB->numrows($result)>0){
499
                while ($data=$DB->fetch_array($result)){
500
                        array_push($toview,$data["num"]);
501
                }
502
        }
503

    
504
        // Add searched items
505
        if (count($field)>0)
506
                foreach($field as $key => $val)
507
                        if (!in_array($val,$toview)&&$val!="all"&&$val!="view")
508
                                array_push($toview,$val);
509

    
510
        // Add order item
511
        if (!in_array($sort,$toview))
512
                array_push($toview,$sort);
513

    
514
        // Manage search on all item
515
        $SEARCH_ALL=array();
516
        if (in_array("all",$field)){
517
                foreach ($field as $key => $val)
518
                        if ($val=="all"){
519
                                array_push($SEARCH_ALL,array("contains"=>$contains[$key]));
520
                        }
521
        }
522

    
523
        // Clean toview array
524
        $toview=array_unique($toview);
525
        foreach ($toview as $key => $val){
526
                if (!isset($limitsearchopt[$val])){
527
                        unset($toview[$key]);
528
                }
529
        }
530
        $toview_count=count($toview);
531

    
532
        // Construct the request 
533
        //// 1 - SELECT
534
        $SELECT ="SELECT ".addDefaultSelect($type);
535

    
536
        // Add select for all toview item
537
        foreach ($toview as $key => $val){
538
                $SELECT.=addSelect($type,$val,$key,0);
539
        }
540

    
541
        //// 2 - FROM AND LEFT JOIN
542
        // Set reference table
543
        
544
        $FROM = " FROM ".$itemtable;
545

    
546

    
547
        // Init already linked tables array in order not to link a table several times
548
        $already_link_tables=array();
549
        // Put reference table
550
        array_push($already_link_tables,$itemtable);
551

    
552
        // Add default join
553
        $COMMONLEFTJOIN=addDefaultJoin($type,$itemtable,$already_link_tables);
554
        $FROM.=$COMMONLEFTJOIN;
555

    
556

    
557
        // Add all table for toview items
558
        foreach ($toview as $key => $val){
559
                $FROM.=addLeftJoin($type,$itemtable,$already_link_tables,$SEARCH_OPTION[$type][$val]["table"],$SEARCH_OPTION[$type][$val]["linkfield"]);
560
        }
561

    
562

    
563
        // Search all case :
564
        if (count($SEARCH_ALL)>0)
565
                foreach ($SEARCH_OPTION[$type] as $key => $val){
566
                        // Do not search on Group Name
567
                        if (is_array($val)){
568
                                $FROM.=addLeftJoin($type,$itemtable,$already_link_tables,$SEARCH_OPTION[$type][$key]["table"],$SEARCH_OPTION[$type][$key]["linkfield"]);
569
                        }
570
                }
571

    
572

    
573
        //// 3 - WHERE
574

    
575
        // default string
576
        $WHERE="";
577
        $COMMONWHERE = addDefaultWhere($type);
578
        $first=empty($COMMONWHERE);
579
        
580

    
581
        // Add deleted if item have it
582
        if (in_array($itemtable,$CFG_GLPI["deleted_tables"])){
583
                $LINK= " AND " ;
584
                if ($first) {$LINK=" ";$first=false;}
585
                $COMMONWHERE.= $LINK.$itemtable.".deleted='$deleted' ";
586
        }
587
        // Remove template items
588
        if (in_array($itemtable,$CFG_GLPI["template_tables"])){
589
                $LINK= " AND " ;
590
                if ($first) {$LINK=" ";$first=false;}
591
                $COMMONWHERE.= $LINK.$itemtable.".is_template='0' ";
592
        }
593

    
594
        // Add Restrict to current entities
595
        if ($entity_restrict){
596
                $LINK= " AND " ;
597
                if ($first) {$LINK=" ";$first=false;}
598

    
599
                if (isset($CFG_GLPI["union_search_type"][$type])) {
600
                        // Will be replace below in Union/Recursivity Hack 
601
                        $COMMONWHERE.=$LINK." ENTITYRESTRICT ";
602
                } else if (in_array($itemtable, $CFG_GLPI["recursive_type"])) {
603
                        $COMMONWHERE.=getEntitiesRestrictRequest($LINK,$itemtable,'','',true);
604
                } else {
605
                        $COMMONWHERE.=getEntitiesRestrictRequest($LINK,$itemtable);
606
                }
607
        }
608
        $first=true;
609
        // Add search conditions
610
        // If there is search items
611
        if ($_SESSION["glpisearchcount"][$type]>0&&count($contains)>0) {
612
                $i=0;
613
                for ($key=0;$key<$_SESSION["glpisearchcount"][$type];$key++){
614
                        // if real search (strlen >0) and not all and view search
615
                        if (isset($contains[$key])&&strlen($contains[$key])>0&&$field[$key]!="all"&&$field[$key]!="view"){
616
                                $LINK=" ";
617
                                $NOT=0;
618
                                $tmplink="";
619
                                if (is_array($link)&&isset($link[$key])){
620
                                        if (ereg("NOT",$link[$key])){
621
                                                $tmplink=" ".ereg_replace(" NOT","",$link[$key]);
622
                                                $NOT=1;
623
                                        } else {
624
                                                $tmplink=" ".$link[$key];
625
                                        }
626
                                } else {
627
                                        $tmplink=" AND ";
628
                                }
629
                        
630
                                // Manage Link if not first item
631
                                if (!$first||$i>0) {
632
                                        $LINK=$tmplink;
633
                                }
634
                                // Add Where clause if not to be done ine HAVING CLAUSE
635
                                if (!in_array($SEARCH_OPTION[$type][$field[$key]]["table"],$META_SPECIF_TABLE)){
636
                                        $WHERE.= addWhere($LINK,$NOT,$type,$field[$key],$contains[$key]);
637
                                        $i++;
638
                                }
639
                                // if real search (strlen >0) and view search
640
                        } else if (isset($contains[$key])&&strlen($contains[$key])>0&&$field[$key]=="view"){
641
                                $LINK=" OR ";
642
                                $NOT=0;
643
                                $globallink=" AND ";
644
                                if (is_array($link)&&isset($link[$key])){
645
                                        switch ($link[$key]){
646
                                                case "AND";
647
                                                        $LINK=" OR ";
648
                                                        $globallink=" AND ";
649
                                                        break;
650
                                                case "AND NOT";
651
                                                        $LINK=" AND ";
652
                                                        $NOT=1;
653
                                                        $globallink=" AND ";
654
                                                        break;
655
                                                case "OR";
656
                                                        $LINK=" OR ";
657
                                                        $globallink=" OR ";
658
                                                        break;
659
                                                case "OR NOT";
660
                                                        $LINK=" AND ";
661
                                                        $NOT=1;
662
                                                        $globallink=" OR ";
663
                                                        break;
664
                                        }
665
                                } else {
666
                                        $tmplink=" AND ";
667
                                }
668

    
669
                                // Manage Link if not first item
670
                                if (!$first||$i>0) {
671
                                        $WHERE.=$globallink;
672
                                }
673

    
674
                                $WHERE.= " ( ";
675
                                $first2=true;
676
                                foreach ($toview as $key2 => $val2){
677
                                        // Add Where clause if not to be done ine HAVING CLAUSE
678
                                        if (!in_array($SEARCH_OPTION[$type][$val2]["table"],$META_SPECIF_TABLE)){
679
                                                $tmplink=$LINK;
680
                                                if ($first2) {
681
                                                        $tmplink=" ";
682
                                                        $first2=false;
683
                                                }
684
                                                $WHERE.= addWhere($tmplink,$NOT,$type,$val2,$contains[$key]);
685
                                        }
686
                                }
687
                                $WHERE.=" ) ";
688
                                $i++;
689
                                // if real search (strlen >0) and all search
690
                        } else if (isset($contains[$key])&&strlen($contains[$key])>0&&$field[$key]=="all"){
691

    
692
                                $LINK=" OR ";
693
                                $NOT=0;
694
                                $globallink=" AND ";
695
                                if (is_array($link)&&isset($link[$key])){
696
                                        switch ($link[$key]){
697
                                                case "AND";
698
                                                        $LINK=" OR ";
699
                                                        $globallink=" AND ";
700
                                                        break;
701
                                                case "AND NOT";
702
                                                        $LINK=" AND ";
703
                                                        $NOT=1;
704
                                                        $globallink=" AND ";
705
                                                        break;
706
                                                case "OR";
707
                                                        $LINK=" OR ";
708
                                                        $globallink=" OR ";
709
                                                        break;
710
                                                case "OR NOT";
711
                                                        $LINK=" AND ";
712
                                                        $NOT=1;
713
                                                        $globallink=" OR ";
714
                                                        break;
715
                                        }
716
                                } else {
717
                                        $tmplink=" AND ";
718
                                }
719

    
720
                                // Manage Link if not first item
721
                                if (!$first||$i>0) {
722
                                        $WHERE.=$globallink;
723
                                }
724

    
725

    
726
                                $WHERE.= " ( ";
727
                                $first2=true;
728

    
729
                                foreach ($SEARCH_OPTION[$type] as $key2 => $val2)
730
                                        if (is_array($val2)){
731
                                                // Add Where clause if not to be done ine HAVING CLAUSE
732
                                                if (!in_array($val2["table"],$META_SPECIF_TABLE)){
733
                                                        $tmplink=$LINK;
734
                                                        if ($first2) {
735
                                                                $tmplink=" ";
736
                                                                $first2=false;
737
                                                        }
738
                                                        $WHERE.= addWhere($tmplink,$NOT,$type,$key2,$contains[$key]);
739
                                                }
740
                                        }
741

    
742
                                $WHERE.=")";
743
                                $i++;
744
                        } 
745
                }
746
        }
747

    
748

    
749
        //// 4 - ORDER
750
        $ORDER="ORDER BY ID";
751
        foreach($toview as $key => $val){
752
                if ($sort==$val){
753
                        $ORDER= addOrderBy($type,$sort,$order,$key);        
754
                }
755
        }
756

    
757

    
758

    
759

    
760
        //// 5 - META SEARCH
761
        // Preprocessing
762
        if ($_SESSION["glpisearchcount2"][$type]>0&&is_array($type2)){
763

    
764
                // a - SELECT 
765
                for ($i=0;$i<$_SESSION["glpisearchcount2"][$type];$i++)
766
                        if (isset($type2[$i])&&$type2[$i]>0&&isset($contains2[$i])&&strlen($contains2[$i]))        {
767
                                $SELECT.=addSelect($type2[$i],$field2[$i],$i,1,$type2[$i]);                
768
                        }
769

    
770
                // b - ADD LEFT JOIN 
771
                // Already link meta table in order not to linked a table several times
772
                $already_link_tables2=array();
773
                // Link reference tables
774
                for ($i=0;$i<$_SESSION["glpisearchcount2"][$type];$i++)
775
                        if (isset($type2[$i])&&$type2[$i]>0&&isset($contains2[$i])&&strlen($contains2[$i])) {
776
                                if (!in_array($LINK_ID_TABLE[$type2[$i]],$already_link_tables2)){
777
                                        $FROM.=addMetaLeftJoin($type,$type2[$i],$already_link_tables2,
778
                                                (($contains2[$i]=="NULL")||(strstr($link2[$i],"NOT"))));
779
                                }
780
                        }
781
                // Link items tables
782
                for ($i=0;$i<$_SESSION["glpisearchcount2"][$type];$i++)
783
                        if (isset($type2[$i])&&$type2[$i]>0&&isset($contains2[$i])&&strlen($contains2[$i])) {
784
                                if (!in_array($SEARCH_OPTION[$type2[$i]][$field2[$i]]["table"]."_".$type2[$i],$already_link_tables2)){
785
                                        $FROM.=addLeftJoin($type2[$i],$LINK_ID_TABLE[$type2[$i]],$already_link_tables2,$SEARCH_OPTION[$type2[$i]][$field2[$i]]["table"],$SEARCH_OPTION[$type2[$i]][$field2[$i]]["linkfield"],0,1,$type2[$i]);                                
786
                                }
787

    
788
                        }
789

    
790
        }
791

    
792

    
793
        //// 6 - Add item ID
794

    
795
        // Add ID to the select
796
        if (!empty($itemtable)){
797
                $SELECT.=$itemtable.".ID AS ID ";
798
        }
799

    
800
        //// 7 - Manage GROUP BY
801
        $GROUPBY="";
802
        // Meta Search / Search All / Count tickets
803
        if ($_SESSION["glpisearchcount2"][$type]>0||count($SEARCH_ALL)>0||in_array(60,$toview)){
804
                $GROUPBY=" GROUP BY $itemtable.ID";
805
        }
806
        
807
        // GROUP BY plugin case : force group by
808
        if (empty($GROUPBY)){
809
                if ($type>1000){
810
                        if (isset($PLUGIN_HOOKS['plugin_types'][$type])){
811
                                $function='plugin_'.$PLUGIN_HOOKS['plugin_types'][$type].'_forceGroupBy';
812
                                if (function_exists($function)){
813
                                        if ($function($type)){
814
                                                $GROUPBY=" GROUP BY $itemtable.ID";
815
                                        }
816
                                }
817
                        }
818
                } else { // CORE type : display a plugin item needed forcegroupby ?
819
                        foreach ($toview as $key2 => $val2){
820
                                if ($val2>1000 && isset($SEARCH_OPTION[$type][$val2]["forcegroupby"])){
821
                                        $GROUPBY=" GROUP BY $itemtable.ID";
822
                                }
823
                        }
824
                }
825
        }
826

    
827

    
828
        // Specific case of group by : multiple links with the reference table
829
        if (empty($GROUPBY)){
830
                foreach ($toview as $key2 => $val2){
831
                        if (empty($GROUPBY)&&(($val2=="all")
832
                                                ||($type==COMPUTER_TYPE&&ereg("glpi_device",$SEARCH_OPTION[$type][$val2]["table"]))
833
                                                ||(ereg("glpi_contracts",$SEARCH_OPTION[$type][$val2]["table"]))
834
                                                ||($SEARCH_OPTION[$type][$val2]["table"]=="glpi_licenses")
835
                                                ||($SEARCH_OPTION[$type][$val2]["table"]=="glpi_networking_ports")
836
                                                ||($SEARCH_OPTION[$type][$val2]["table"]=="glpi_dropdown_netpoint")
837
                                                ||($SEARCH_OPTION[$type][$val2]["table"]=="glpi_registry")
838
                                                ||($type==USER_TYPE)
839
                                                ||($type==CONTACT_TYPE&&$SEARCH_OPTION[$type][$val2]["table"]=="glpi_enterprises")
840
                                                ||($type==ENTERPRISE_TYPE&&$SEARCH_OPTION[$type][$val2]["table"]=="glpi_contacts")
841
                                             )) 
842

    
843
                                $GROUPBY=" GROUP BY $itemtable.ID ";
844
                }
845
        }
846

    
847
        // Specific search define in META_SPECIF_TABLE : only for computer search (not meta search)
848
        if ($type==COMPUTER_TYPE){
849
                // For each real search item 
850
                foreach($contains as $key => $val)
851
                        if (strlen($val)>0){
852
                                // If not all and view search
853
                                if ($field[$key]!="all"&&$field[$key]!="view"){
854
                                        foreach ($toview as $key2 => $val2){
855

    
856
                                                if (($val2==$field[$key])&&in_array($SEARCH_OPTION[$type][$val2]["table"],$META_SPECIF_TABLE)){
857
                                                        if (!isset($link[$key])) {
858
                                                                $link[$key]="AND";
859
                                                        }
860
                                                        
861
                                                        $GROUPBY=addGroupByHaving($GROUPBY,$SEARCH_OPTION[$type][$field[$key]]["table"].".".$SEARCH_OPTION[$type][$field[$key]]["field"],strtolower($contains[$key]),$key2,0,$link[$key]);
862
                                                }
863
                                        }
864
                                }
865
                        }
866
        } 
867

    
868
        $first=empty($WHERE);
869

    
870
        // Specific search for others item linked  (META search)
871
        if (is_array($type2)){
872
                for ($key=0;$key<$_SESSION["glpisearchcount2"][$type];$key++){
873
                        if (isset($type2[$key])&&$type2[$key]>0&&isset($contains2[$key])&&strlen($contains2[$key]))
874
                        {
875
                                $LINK="";
876
                                if (isset($link2[$key])) $LINK=$link2[$key];
877

    
878
                                // For AND NOT statement need to take into account all the group by items
879
                                if ($SEARCH_OPTION[$type2[$key]][$field2[$key]]["meta"]==1 || ereg("AND NOT",$link2[$key])){                
880
                                        $GROUPBY=addGroupByHaving($GROUPBY,$SEARCH_OPTION[$type2[$key]][$field2[$key]]["table"].".".$SEARCH_OPTION[$type2[$key]][$field2[$key]]["field"],strtolower($contains2[$key]),$key,1,$LINK);
881
                                } else { // Meta Where Search
882
                                        $LINK=" ";
883
                                        $NOT=0;
884
                                        // Manage Link if not first item
885
                                        if (!$first) {
886
                                                if (is_array($link2)&&isset($link2[$key])&&ereg("NOT",$link2[$key])){
887
                                                        $LINK=" ".ereg_replace(" NOT","",$link2[$key]);
888
                                                        $NOT=1;
889
                                                }
890
                                                else if (is_array($link2)&&isset($link2[$key]))
891
                                                        $LINK=" ".$link2[$key];
892
                                                else $LINK=" AND ";
893
                                        }
894

    
895
                                        $WHERE.= addWhere($LINK,$NOT,$type2[$key],$field2[$key],$contains2[$key],1);
896
                                }
897
                                $first=false;
898
                        }
899
                }
900
        }
901

    
902
        // If no research limit research to display item and compute number of item using simple request
903
        $nosearch=true;
904
        for ($i=0;$i<$_SESSION["glpisearchcount"][$type];$i++)
905
                if (isset($contains[$i])&&strlen($contains[$i])>0) $nosearch=false;
906

    
907
        if ($_SESSION["glpisearchcount2"][$type]>0)        
908
                $nosearch=false;
909

    
910
        $LIMIT="";
911
        $numrows=0;
912
        //No search : count number of items using a simple count(ID) request and LIMIT search
913
        if ($nosearch) {
914
                $LIMIT= " LIMIT $start, ".$LIST_LIMIT;
915

    
916
                $query_num="SELECT count(DISTINCT $itemtable.ID) FROM ".$itemtable.$COMMONLEFTJOIN;
917
                
918
                $first=true;
919

    
920
                if (!empty($COMMONWHERE)){
921
                        $LINK= " AND " ;
922
                        if ($first) {$LINK=" WHERE ";$first=false;}
923
                        $query_num.= $LINK.$COMMONWHERE;
924
                }
925

    
926
                // Union Search :
927
                if (isset($CFG_GLPI["union_search_type"][$type])){
928
                        $tmpquery=$query_num;
929
                        $numrows=0;
930
                        foreach ($CFG_GLPI[$CFG_GLPI["union_search_type"][$type]] as $ctype){
931
                                if (haveTypeRight($ctype,'r')){
932
                                        // No ref table case
933
                                        if (empty($LINK_ID_TABLE[$type])){
934
                                                $query_num=ereg_replace($CFG_GLPI["union_search_type"][$type],$LINK_ID_TABLE[$ctype],$tmpquery);
935
                                                // State case :
936
                                                if ($type==STATE_TYPE){
937
                                                        $query_num.=" AND ".$LINK_ID_TABLE[$ctype].".state > 0 ";
938
                                                }
939
                                        } else {// Ref table case
940
                                                $replace="FROM ".$LINK_ID_TABLE[$type]." INNER JOIN ".$LINK_ID_TABLE[$ctype]." ON (".$LINK_ID_TABLE[$type].".id_device = ".$LINK_ID_TABLE[$ctype].".ID AND ".$LINK_ID_TABLE[$type].".device_type='$ctype')";
941
                                                $query_num=ereg_replace("FROM ".$CFG_GLPI["union_search_type"][$type],$replace,$tmpquery);
942
                                                $query_num=ereg_replace($CFG_GLPI["union_search_type"][$type],$LINK_ID_TABLE[$ctype],$query_num);
943
                                        }
944
                                        // Union/Recursivity Hack
945
                                        if (isset($CFG_GLPI["recursive_type"][$ctype])) {
946
                                                $query_num=ereg_replace("ENTITYRESTRICT",getEntitiesRestrictRequest('',$LINK_ID_TABLE[$ctype],'','',true),$query_num);
947
                                        } else {
948
                                                $query_num=ereg_replace("ENTITYRESTRICT",getEntitiesRestrictRequest('',$LINK_ID_TABLE[$ctype]),$query_num);
949
                                        }
950
                                        $result_num = $DB->query($query_num);
951
                                        $numrows+= $DB->result($result_num,0,0);
952
                                }
953
                        }
954
                } else {
955
                        
956
                        $result_num = $DB->query($query_num);
957
                        $numrows= $DB->result($result_num,0,0);
958
                }
959
        }
960
        
961
        // If export_all reset LIMIT condition
962
        if (isset($_GET['export_all'])) $LIMIT="";
963

    
964

    
965
        if (!empty($WHERE)||!empty($COMMONWHERE)){
966
                if (!empty($COMMONWHERE)){
967
                        $WHERE=' WHERE '.$COMMONWHERE.(!empty($WHERE)?' AND ( '.$WHERE.' )':'');
968
                } else {
969
                        $WHERE=' WHERE  '.$WHERE.' ';
970
                }
971
                $first=false;
972
        }
973

    
974
        $DB->query("SET SESSION group_concat_max_len = 9999999;");
975

    
976
        // Create QUERY
977
        if (isset($CFG_GLPI["union_search_type"][$type])){
978
                $first=true;
979
                $QUERY="";
980
                foreach ($CFG_GLPI[$CFG_GLPI["union_search_type"][$type]] as $ctype){
981
                        if (haveTypeRight($ctype,'r')){
982
                                if ($first){
983
                                        $first=false;
984
                                } else {
985
                                        $QUERY.=" UNION ";
986
                                }
987
                                $tmpquery="";
988
                                // No ref table case
989
                                if (empty($LINK_ID_TABLE[$type])){
990
                                                $tmpquery=$SELECT.", $ctype AS TYPE ".$FROM.$WHERE;
991
                                                $tmpquery=ereg_replace($CFG_GLPI["union_search_type"][$type],$LINK_ID_TABLE[$ctype],$tmpquery);
992
                                                // State case :
993
                                                if ($type==STATE_TYPE){
994
                                                        $tmpquery.=" AND ".$LINK_ID_TABLE[$ctype].".state > 0 ";
995
                                                }
996
                                } else {// Ref table case
997
                                                $tmpquery=$SELECT.", $ctype AS TYPE, ".$LINK_ID_TABLE[$type].".ID AS refID ".$FROM.$WHERE;
998
                                                $replace="FROM ".$LINK_ID_TABLE[$type]." INNER JOIN ".$LINK_ID_TABLE[$ctype]." ON (".$LINK_ID_TABLE[$type].".id_device = ".$LINK_ID_TABLE[$ctype].".ID AND ".$LINK_ID_TABLE[$type].".device_type='$ctype')";
999
                                                $tmpquery=ereg_replace("FROM ".$CFG_GLPI["union_search_type"][$type],$replace,$tmpquery);
1000
                                                $tmpquery=ereg_replace($CFG_GLPI["union_search_type"][$type],$LINK_ID_TABLE[$ctype],$tmpquery);
1001
                                }
1002
                                // Union/Recursivity Hack
1003
                                if (isset($CFG_GLPI["recursive_type"][$ctype])) {
1004
                                        $tmpquery=ereg_replace("ENTITYRESTRICT",getEntitiesRestrictRequest('',$LINK_ID_TABLE[$ctype],'','',true),$tmpquery);
1005
                                } else {
1006
                                        $tmpquery=ereg_replace("ENTITYRESTRICT",getEntitiesRestrictRequest('',$LINK_ID_TABLE[$ctype]),$tmpquery);
1007
                                }
1008
                                // SOFTWARE HACK
1009
                                if ($ctype==SOFTWARE_TYPE){
1010
                                        $tmpquery=ereg_replace("glpi_software.serial","''",$tmpquery);
1011
                                        $tmpquery=ereg_replace("glpi_software.otherserial","''",$tmpquery);
1012
                                }
1013

    
1014
                                $QUERY.=$tmpquery;
1015
                        }
1016
                }
1017
                if (empty($QUERY)){
1018
                        echo displaySearchError($output_type);
1019
                        return;
1020
                }
1021
                $QUERY.=ereg_replace($CFG_GLPI["union_search_type"][$type].".","",$ORDER).$LIMIT;
1022
        } else {
1023
                $QUERY=$SELECT.$FROM.$WHERE.$GROUPBY.$ORDER.$LIMIT;
1024
        }
1025

    
1026
//        echo $QUERY."<br>\n";
1027

    
1028
        // Get it from database and DISPLAY
1029
        if ($result = $DB->query($QUERY)) {
1030

    
1031
                // if real search or complete export : get numrows from request 
1032
                if (!$nosearch||isset($_GET['export_all'])) 
1033
                        $numrows= $DB->numrows($result);
1034

    
1035
                // Contruct Pager parameters
1036
                $globallinkto=getMultiSearchItemForLink("field",$field).getMultiSearchItemForLink("link",$link).getMultiSearchItemForLink("contains",$contains).getMultiSearchItemForLink("field2",$field2).getMultiSearchItemForLink("contains2",$contains2).getMultiSearchItemForLink("type2",$type2).getMultiSearchItemForLink("link2",$link2);
1037

    
1038
                $parameters="sort=$sort&amp;order=$order".$globallinkto;
1039

    
1040

    
1041
                if ($output_type==GLOBAL_SEARCH){
1042
                        $ci = new CommonItem();        
1043
                        $ci->setType($type);
1044
                        echo "<div class='center'><h2>".$ci->getType($type);
1045
                        // More items
1046
                        if ($numrows>$start+GLOBAL_SEARCH_DISPLAY_COUNT){
1047
                                echo " <a href='$target?$parameters'>".$LANG["common"][66]."</a>";
1048
                        }
1049
                        echo "</h2></div>";
1050
                }
1051

    
1052

    
1053
                // If the begin of the view is before the number of items
1054
                if ($start<$numrows) {
1055

    
1056
                        // Display pager only for HTML
1057
                        if ($output_type==HTML_OUTPUT){
1058

    
1059
                                // For plugin add new parameter if available
1060
                                if ($type>1000){
1061
                                        if (isset($PLUGIN_HOOKS['plugin_types'][$type])){
1062

    
1063
                                                $function='plugin_'.$PLUGIN_HOOKS['plugin_types'][$type].'_addParamFordynamicReport';
1064

    
1065
                                                if (function_exists($function)){
1066
                                                        $out=$function($type);
1067
                                                        if (is_array($out)&&count($out)){
1068
                                                                foreach ($out as $key => $val){
1069
                                                                        if (is_array($val)){
1070
                                                                                $parameters.=getMultiSearchItemForLink($key,$val);
1071
                                                                        } else {
1072
                                                                                $parameters.="&amp;$key=$val";
1073
                                                                        }
1074
                                                                }
1075
                                                        }
1076
                                                } 
1077
                                        } 
1078
                                }
1079
                                printPager($start,$numrows,$target,$parameters,$type);
1080
                        }
1081

    
1082
                        // Form to massive actions
1083
                        $isadmin=(haveTypeRight($type,"w")||(in_array($type,$CFG_GLPI["infocom_types"])&&haveTypeRight(INFOCOM_TYPE,"w")));
1084

    
1085
                        if ($isadmin&&$output_type==HTML_OUTPUT){
1086
                                echo "<form method='post' name='massiveaction_form' id='massiveaction_form' action=\"".$CFG_GLPI["root_doc"]."/front/massiveaction.php\">";
1087
                        }
1088

    
1089
                        // Compute number of columns to display
1090
                        // Add toview elements
1091
                        $nbcols=$toview_count;
1092
                        // Add meta search elements if real search (strlen>0) or only NOT search
1093
                        if ($_SESSION["glpisearchcount2"][$type]>0&&is_array($type2))
1094
                                for ($i=0;$i<$_SESSION["glpisearchcount2"][$type];$i++)
1095
                                        if (isset($type2[$i])&&isset($contains2[$i])&&strlen($contains2[$i])>0&&$type2[$i]>0&&(!isset($link2[$i])||!ereg("NOT",$link2[$i]))) {
1096
                                                $nbcols++;
1097
                                        }
1098

    
1099
                        if ($output_type==HTML_OUTPUT)// HTML display - massive modif
1100
                                $nbcols++;
1101

    
1102
                        // Define begin and end var for loop
1103
                        // Search case
1104
                        $begin_display=$start;
1105
                        $end_display=$start+$LIST_LIMIT;
1106
                        // No search Case
1107
                        if ($nosearch){
1108
                                $begin_display=0;
1109
                                $end_display=min($numrows-$start,$LIST_LIMIT);
1110
                        }
1111
                        // Export All case
1112
                        if (isset($_GET['export_all'])) {
1113
                                $begin_display=0;
1114
                                $end_display=$numrows;
1115
                        }
1116

    
1117

    
1118
                        // Display List Header
1119
                        echo displaySearchHeader($output_type,$end_display-$begin_display+1,$nbcols);
1120
                        // New Line for Header Items Line
1121
                        echo displaySearchNewLine($output_type);
1122
                        $header_num=1;
1123

    
1124
                        if ($output_type==HTML_OUTPUT){// HTML display - massive modif
1125
                                $search_config="";
1126
                                if (haveRight("search_config","w")||haveRight("search_config_global","w")){
1127
                                        $tmp= " class='pointer'  onClick=\"var w = window.open('".$CFG_GLPI["root_doc"]."/front/popup.php?popup=search_config&amp;type=$type' ,'glpipopup', 'height=400, width=1000, top=100, left=100, scrollbars=yes' ); w.focus();\"";
1128

    
1129
                                        $search_config= "<img alt='".$LANG["setup"][252]."' title='".$LANG["setup"][252]."' src='".$CFG_GLPI["root_doc"]."/pics/options_search.png' ";
1130
                                        $search_config.=$tmp.">";
1131
                                        //$search_config.= "<img alt='".$LANG["buttons"][6]."' title='".$LANG["buttons"][6]."' src='".$CFG_GLPI["root_doc"]."/pics/moins.png' ";
1132
                                        //$search_config.=$tmp.">";
1133
                                }
1134

    
1135
                                echo displaySearchHeaderItem($output_type,$search_config,$header_num,"",0,$order);
1136
                        }
1137

    
1138
                        // Display column Headers for toview items
1139
                        foreach ($toview as $key => $val){
1140
                                $linkto="$target?sort=".$val."&amp;order=".($order=="ASC"?"DESC":"ASC")."&amp;start=$start".$globallinkto;
1141
                                echo displaySearchHeaderItem($output_type,$SEARCH_OPTION[$type][$val]["name"],$header_num,$linkto,$sort==$val,$order);
1142
                        }
1143

    
1144
                        // Display columns Headers for meta items
1145
                        if ($_SESSION["glpisearchcount2"][$type]>0&&is_array($type2))
1146
                                for ($i=0;$i<$_SESSION["glpisearchcount2"][$type];$i++)
1147
                                        if (isset($type2[$i])&&$type2[$i]>0&&isset($contains2[$i])&&strlen($contains2[$i])&&(!isset($link2[$i])
1148
                                                                ||(!ereg("NOT",$link2[$i]) || $contains2[$i]=="NULL"))) {
1149
                                                echo displaySearchHeaderItem($output_type,$names[$type2[$i]]." - ".$SEARCH_OPTION[$type2[$i]][$field2[$i]]["name"],$header_num);
1150
                                        }
1151
                        // Add specific column Header
1152
                        if ($type==SOFTWARE_TYPE)
1153
                                echo displaySearchHeaderItem($output_type,$LANG["software"][11],$header_num);
1154
                        if ($type==CARTRIDGE_TYPE)
1155
                                echo displaySearchHeaderItem($output_type,$LANG["cartridges"][0],$header_num);        
1156
                        if ($type==CONSUMABLE_TYPE)
1157
                                echo displaySearchHeaderItem($output_type,$LANG["consumables"][0],$header_num);
1158
                        if ($type==STATE_TYPE||$type==RESERVATION_TYPE){
1159
                                echo displaySearchHeaderItem($output_type,$LANG["state"][6],$header_num);
1160
                                $ci = new CommonItem();
1161
                                
1162
                        }
1163
                        if ($type==RESERVATION_TYPE&&$output_type==HTML_OUTPUT){
1164
                                if (haveRight("reservation_central","w")){
1165
                                        echo displaySearchHeaderItem($output_type,"&nbsp;",$header_num);
1166
                                        echo displaySearchHeaderItem($output_type,"&nbsp;",$header_num);
1167
                                }
1168
                                echo displaySearchHeaderItem($output_type,"&nbsp;",$header_num);
1169
                        }
1170
                        // End Line for column headers                
1171
                        echo displaySearchEndLine($output_type);
1172

    
1173
                        // if real search seek to begin of items to display (because of complete search)
1174
                        if (!$nosearch)
1175
                                $DB->data_seek($result,$start);
1176

    
1177
                        // Define begin and end var for loop
1178
                        // Search case
1179
                        $i=$begin_display;                        
1180

    
1181
                        // Num of the row (1=header_line)
1182
                        $row_num=1;
1183
                        // Display Loop
1184
                        while ($i < $numrows && $i<($end_display)){
1185
                                // Column num
1186
                                $item_num=1;
1187
                                // Get data and increment loop variables
1188
                                $data=$DB->fetch_assoc($result);
1189
                                $i++;
1190
                                $row_num++;
1191
                                // New line
1192
                                echo displaySearchNewLine($output_type,($i%2));
1193

    
1194

    
1195
                                if ($output_type==HTML_OUTPUT){// HTML display - massive modif
1196
                                        $tmpcheck="";
1197
                                        if ($isadmin){
1198
                                                if (isset($CFG_GLPI["recursive_type"][$type]) && !in_array($data["FK_entities"],$_SESSION["glpiactiveentities"])) {
1199
                                                        echo "&nbsp;";
1200
                                                } else {
1201
                                                        $sel="";
1202
                                                        if (isset($_GET["select"])&&$_GET["select"]=="all") {
1203
                                                                $sel="checked";
1204
                                                        }
1205
                                                        if (isset($_SESSION['glpimassiveactionselected'][$data["ID"]])){
1206
                                                                $sel="checked";
1207
                                                        }
1208
                                                        $tmpcheck="<input type='checkbox' name='item[".$data["ID"]."]' value='1' $sel>";                                                        
1209
                                                }                                                
1210
                                        }
1211
                                        echo displaySearchItem($output_type,$tmpcheck,$item_num,$row_num,"width='10'");
1212
                                }
1213

    
1214
                                // Print first element - specific case for user 
1215
                                echo displaySearchItem($output_type,giveItem($type,$SEARCH_OPTION[$type][1]["table"].".".$SEARCH_OPTION[$type][1]["field"],$data,0,$SEARCH_OPTION[$type][1]["linkfield"]),$item_num,$row_num,displayConfigItem($type,$SEARCH_OPTION[$type][1]["table"].".".$SEARCH_OPTION[$type][1]["field"]));
1216

    
1217
                                // Print other toview items
1218
                                foreach ($toview as $key => $val){
1219
                                        // Do not display first item
1220
                                        if ($key>0){
1221
                                                echo displaySearchItem($output_type,giveItem($type,$SEARCH_OPTION[$type][$val]["table"].".".$SEARCH_OPTION[$type][$val]["field"],$data,$key,$SEARCH_OPTION[$type][$val]["linkfield"]),$item_num,$row_num,displayConfigItem($type,$SEARCH_OPTION[$type][$val]["table"].".".$SEARCH_OPTION[$type][$val]["field"]));
1222
                                        }
1223
                                }
1224

    
1225
                                // Print Meta Item
1226
                                if ($_SESSION["glpisearchcount2"][$type]>0&&is_array($type2))
1227
                                        for ($j=0;$j<$_SESSION["glpisearchcount2"][$type];$j++)
1228
                                                if (isset($type2[$j])&&$type2[$j]>0&&isset($contains2[$j])&&strlen($contains2[$j])&&(!isset($link2[$j])
1229
                                                                        ||(!ereg("NOT",$link2[$j]) || $contains2[$j]=="NULL"))){
1230

    
1231
                                                        // General case
1232
                                                        if (strpos($data["META_$j"],"$$$$")===false){
1233
                                                                echo displaySearchItem($output_type,$data["META_$j"],$item_num,$row_num);
1234
                                                        // Case of GROUP_CONCAT item : split item and multilline display
1235
                                                        } else {
1236
                                                                $split=explode("$$$$",$data["META_$j"]);
1237
                                                                $count_display=0;
1238
                                                                $out="";
1239
                                                                for ($k=0;$k<count($split);$k++)
1240
                                                                        if ($contains2[$j]=="NULL"||(strlen($contains2[$j])==0
1241
                                                                                ||preg_match('/'.$contains2[$j].'/i',$split[$k])
1242
                                                                        )){
1243

    
1244
                                                                                if ($count_display) $out.= "<br>";
1245
                                                                                $count_display++;
1246
                                                                                $out.= $split[$k];
1247
                                                                        }
1248
                                                                echo displaySearchItem($output_type,$out,$item_num,$row_num);
1249

    
1250
                                                        }
1251
                                                }
1252
                                // Specific column display
1253
                                if ($type==CARTRIDGE_TYPE){
1254
                                        echo displaySearchItem($output_type,countCartridges($data["ID"],$data["ALARM"],$output_type),$item_num,$row_num);
1255
                                }
1256
                                if ($type==SOFTWARE_TYPE){
1257
                                        echo displaySearchItem($output_type,countInstallations($data["ID"],$output_type),$item_num,$row_num);
1258
                                }                
1259
                                if ($type==CONSUMABLE_TYPE){
1260
                                        echo displaySearchItem($output_type,countConsumables($data["ID"],$data["ALARM"],$output_type),$item_num,$row_num);
1261
                                }        
1262
                                if ($type==STATE_TYPE||$type==RESERVATION_TYPE){
1263
                                        $ci->setType($data["TYPE"]);
1264
                                        echo displaySearchItem($output_type,$ci->getType(),$item_num,$row_num);
1265
                                }        
1266
                                if ($type==RESERVATION_TYPE&&$output_type==HTML_OUTPUT){
1267
                                        if (haveRight("reservation_central","w")){
1268
                                                if ($data["ACTIVE"]){
1269
                                                        echo displaySearchItem($output_type,"<a href=\"".$CFG_GLPI["root_doc"]."/front/reservation.php?ID=".$data["refID"]."&amp;active=0\"  title='".$LANG["buttons"][42]."'><img src=\"".$CFG_GLPI["root_doc"]."/pics/moins.png\" alt='' title=''></a>",$item_num,$row_num,"class='center'");
1270
                                                } else {
1271
                                                        echo displaySearchItem($output_type,"<a href=\"".$CFG_GLPI["root_doc"]."/front/reservation.php?ID=".$data["refID"]."&amp;active=1\"  title='".$LANG["buttons"][41]."'><img src=\"".$CFG_GLPI["root_doc"]."/pics/plus.png\" alt='' title=''></a>",$item_num,$row_num,"class='center'");
1272
                                                }
1273

    
1274
                                                echo displaySearchItem($output_type,"<a href=\"javascript:confirmAction('".addslashes($LANG["reservation"][38])."\\n".addslashes($LANG["reservation"][39])."','".$CFG_GLPI["root_doc"]."/front/reservation.php?ID=".$data["refID"]."&amp;delete=delete')\"  title='".$LANG["reservation"][6]."'><img src=\"".$CFG_GLPI["root_doc"]."/pics/delete.png\" alt='' title=''></a>",$item_num,$row_num,"class='center'");
1275
                                        }
1276
                                        if ($data["ACTIVE"]){
1277
                                                echo displaySearchItem($output_type,"<a href='".$target."?show=resa&amp;ID=".$data["refID"]."' title='".$LANG["reservation"][21]."'><img src=\"".$CFG_GLPI["root_doc"]."/pics/reservation-3.png\" alt='' title=''></a>",$item_num,$row_num,"class='center'");
1278
                                        } else {
1279
                                                echo displaySearchItem($output_type,"&nbsp;",$item_num,$row_num);
1280
                                        }
1281
                                }
1282
                                // End Line
1283
                                echo displaySearchEndLine($output_type);
1284
                        }
1285
                        $title="";
1286
                        // Create title
1287
                        if ($output_type==PDF_OUTPUT_LANDSCAPE || $output_type==PDF_OUTPUT_PORTRAIT){
1288
                                if ($_SESSION["glpisearchcount"][$type]>0&&count($contains)>0) {
1289
                                        for ($key=0;$key<$_SESSION["glpisearchcount"][$type];$key++){
1290
                                                if (strlen($contains[$key])){
1291
                                                        if (isset($link[$key])) $title.=" ".$link[$key]." ";
1292
                                                        switch ($field[$key]){
1293
                                                                case "all":
1294
                                                                        $title.=$LANG["common"][66];
1295
                                                                break;
1296
                                                                case "view":
1297
                                                                        $title.=$LANG["search"][11];
1298
                                                                break;
1299
                                                                default :
1300
                                                                $title.=$SEARCH_OPTION[$type][$field[$key]]["name"];
1301
                                                                break;
1302
                                                        }
1303
                                                        $title.=" = ".$contains[$key];
1304
                                                }
1305
                                        }
1306
                                }
1307
                                if ($_SESSION["glpisearchcount2"][$type]>0&&count($contains2)>0) {
1308
                                        for ($key=0;$key<$_SESSION["glpisearchcount2"][$type];$key++){
1309
                                                if (strlen($contains2[$key])){
1310
                                                        if (isset($link2[$key])) $title.=" ".$link2[$key]." ";
1311
                                                        $title.=$names[$type2[$key]]."/";
1312
                                                        $title.=$SEARCH_OPTION[$type2[$key]][$field2[$key]]["name"];
1313
                                                        $title.=" = ".$contains2[$key];
1314
                                                }
1315
                                        }
1316
                                }
1317
                        }
1318

    
1319
                        // Display footer
1320
                        echo displaySearchFooter($output_type,$title);
1321

    
1322

    
1323
                        // Delete selected item
1324
                        if ($output_type==HTML_OUTPUT){
1325
                                if ($isadmin){
1326
                                        
1327
                                        echo "<table width='80%'>";
1328
                                        echo "<tr><td><img src=\"".$CFG_GLPI["root_doc"]."/pics/arrow-left.png\" alt=''></td><td class='center'><a onclick= \"if ( markAllRows('massiveaction_form') ) return false;\" href='".$_SERVER['PHP_SELF']."?select=all' >".$LANG["buttons"][18]."</a></td>";
1329
        
1330
                                        echo "<td>/</td><td class='center'><a onclick= \"if ( unMarkAllRows('massiveaction_form') ) return false;\" href='".$_SERVER['PHP_SELF']."?select=none'>".$LANG["buttons"][19]."</a>";
1331
                                        echo "</td><td class='left' width='80%'>";
1332
                                        dropdownMassiveAction($type,$deleted);
1333
                                        echo "</td>";
1334
                                        echo "</table>";
1335
        
1336
                                        
1337
                                        // End form for delete item
1338
                                        echo "</form>";
1339
                                } else {
1340
                                        echo "<br>";
1341
                                }
1342
                        }
1343

    
1344
                        if ($output_type==HTML_OUTPUT) // In case of HTML display
1345
                                printPager($start,$numrows,$target,$parameters);
1346

    
1347
                } else {
1348
                        echo displaySearchError($output_type);
1349

    
1350
                }
1351
        } else {
1352
                echo $DB->error();
1353
        }
1354
        // Clean selection 
1355
        $_SESSION['glpimassiveactionselected']=array();
1356
}
1357

    
1358

    
1359

    
1360
/**
1361
 * Generic Function to add GROUP BY to a request
1362
 *
1363
 *
1364
 *@param $field field to add
1365
 *@param $GROUPBY group by strign to complete
1366
 *@param $val value search
1367
 *@param $num item number 
1368
 *@param $meta is it a meta item ?
1369
 *@param $link link to use 
1370
 *
1371
 *
1372
 *@return select string
1373
 *
1374
 **/
1375
function addGroupByHaving($GROUPBY,$field,$val,$num,$meta=0,$link=""){
1376

    
1377
        $NOT=0;
1378
        if (ereg("NOT",$link)){
1379
                $NOT=1;
1380
                $link=ereg_replace(" NOT","",$link);
1381
        }
1382

    
1383
        if (empty($link)) $link="AND";
1384

    
1385
        $NAME="ITEM_";
1386
        if ($meta) $NAME="META_";
1387

    
1388
        if (!ereg("GROUP BY ID",$GROUPBY)) $GROUPBY=" GROUP BY ID ";
1389

    
1390
        if (ereg("HAVING",$GROUPBY)) $GROUPBY.=" ".$link." ";
1391
        else $GROUPBY.=" HAVING ";
1392

    
1393
        switch ($field){
1394
                case "glpi_tracking.count" :
1395
                        $search=array("/\&lt;/","/\&gt;/");
1396
                        $replace=array("<",">");
1397
                        $val=preg_replace($search,$replace,$val);
1398
        
1399
                        if (ereg("([<>])([=]*)[[:space:]]*([0-9]*)",$val,$regs)){
1400
                                if ($NOT){
1401
                                        if ($regs[1]=='<') {
1402
                                                $regs[1]='>';
1403
                                        } else {
1404
                                                $regs[1]='<';
1405
                                        }
1406
                                } 
1407
                                $regs[1].=$regs[2];
1408
                                $GROUPBY.= " ($NAME$num ".$regs[1]." ".$regs[3]." ) ";
1409
                        } else {
1410
                                if (!$NOT){
1411
                                        $GROUPBY.=" ( $NAME$num = ".(intval($val)).") ";
1412
                                } else {
1413
                                        $GROUPBY.=" ( $NAME$num <> ".(intval($val)).") ";
1414
                                }
1415
                        }
1416
                break;
1417
                case "glpi_device_ram.specif_default" :
1418
                case "glpi_device_processor.specif_default" :
1419
                case "glpi_device_hdd.specif_default" :
1420
                        $search=array("/\&lt;/","/\&gt;/");
1421
                        $replace=array("<",">");
1422
                        $val=preg_replace($search,$replace,$val);
1423
                        if (ereg("([<>])([=]*)[[:space:]]*([0-9]*)",$val,$regs)){
1424
                                if ($NOT){
1425
                                        if ($regs[1]=='<') {
1426
                                                $regs[1]='>';
1427
                                        } else {
1428
                                                $regs[1]='<';
1429
                                        }
1430
                                } 
1431
                                $regs[1].=$regs[2];
1432
                                $GROUPBY.= " ($NAME$num ".$regs[1]." ".$regs[3]." ) ";
1433
                        } else {
1434
                                if ($field=="glpi_device_hdd.specif_default"){
1435
                                        $larg=1000;
1436
                                } else {
1437
                                        $larg=100;
1438
                                }
1439
                                if (!$NOT){
1440
                                        $GROUPBY.=" ( $NAME$num < ".(intval($val)+$larg)." AND $NAME$num > ".(intval($val)-$larg)." ) ";
1441
                                } else {
1442
                                        $GROUPBY.=" ( $NAME$num > ".(intval($val)+$larg)." OR $NAME$num < ".(intval($val)-$larg)." ) ";
1443
                                }
1444
                        }
1445
                break;
1446
                default :
1447
                        $ADD="";
1448
                        if (($NOT&&$val!="NULL")||$val=='^$') {
1449
                                $ADD=" OR $NAME$num IS NULL";
1450
                        }
1451

    
1452
                        $GROUPBY.= " ( ".$NAME.$num.makeTextSearch($val,$NOT)." $ADD ) ";
1453

    
1454
                break;
1455
        }
1456

    
1457
        return $GROUPBY;
1458
}
1459

    
1460
/**
1461
 * Generic Function to add ORDER BY to a request
1462
 *
1463
 *
1464
 *@param $type ID of the device type
1465
 *@param $ID field to add
1466
 *@param $order order define
1467
 *@param $key item number
1468
 *
1469
 *
1470
 *@return select string
1471
 *
1472
 **/
1473
function addOrderBy($type,$ID,$order,$key=0){
1474
        global $SEARCH_OPTION,$CFG_GLPI,$PLUGIN_HOOKS;
1475

    
1476
        $table=$SEARCH_OPTION[$type][$ID]["table"];
1477
        $field=$SEARCH_OPTION[$type][$ID]["field"];
1478
        $linkfield=$SEARCH_OPTION[$type][$ID]["linkfield"];
1479

    
1480

    
1481
        if (isset($CFG_GLPI["union_search_type"][$type])){
1482
                return " ORDER BY ITEM_$key $order ";
1483
        }
1484

    
1485
        // Plugin can overirde core definition for its type
1486
        if ($type>1000){
1487
                if (isset($PLUGIN_HOOKS['plugin_types'][$type])){
1488
                        $function='plugin_'.$PLUGIN_HOOKS['plugin_types'][$type].'_addOrderBy';
1489
                        if (function_exists($function)){
1490
                                $out=$function($type,$ID,$order,$key);
1491
                                if (!empty($out)){
1492
                                        return $out;
1493
                                }
1494
                        } 
1495
                } 
1496
        }
1497

    
1498

    
1499
        switch($table.".".$field){
1500
                case "glpi_device_hdd.specif_default" :
1501
                case "glpi_device_ram.specif_default" :
1502
                case "glpi_device_processor.specif_default" :
1503
                case "glpi_tracking.count" :
1504
                        return " ORDER BY ITEM_$key $order ";
1505
                break;
1506
                case "glpi_auth_tables.name" :
1507
                        return " ORDER BY glpi_users.auth_method, glpi_auth_ldap.name, glpi_auth_mail.name $order ";
1508
                break;
1509
                case "glpi_contracts.end_date":
1510
                        return " ORDER BY ADDDATE(glpi_contracts.begin_date, INTERVAL glpi_contracts.duration MONTH) $order ";
1511
                break;
1512
                case "glpi_infocoms.end_warranty":
1513
                        return " ORDER BY ADDDATE(glpi_infocoms.buy_date, INTERVAL glpi_infocoms.warranty_duration MONTH) $order "; 
1514
                break; 
1515
                case "glpi_contracts.expire":
1516
                        return " ORDER BY ADDDATE(glpi_contracts.begin_date, INTERVAL glpi_contracts.duration MONTH) $order ";
1517
                break;
1518
                case "glpi_contracts.expire_notice":
1519
                        return " ORDER BY ADDDATE(glpi_contracts.begin_date, INTERVAL (glpi_contracts.duration-glpi_contracts.notice) MONTH) $order ";
1520
                break;
1521
                case "glpi_users.name" :
1522
                        $linkfield="";
1523
                        if (!empty($SEARCH_OPTION[$type][$ID]["linkfield"])){
1524
                                $linkfield="_".$SEARCH_OPTION[$type][$ID]["linkfield"];
1525
                        }
1526
                        if ($type==USER_TYPE){
1527
                                return " ORDER BY ".$table.$linkfield.".$field $order";
1528
                        } else {
1529
                                return " ORDER BY ".$table.$linkfield.".realname $order, ".$table.$linkfield.".firstname $order, ".$table.$linkfield.".name $order";
1530
                        }
1531
                        break;
1532
                case "glpi_networking_ports.ifaddr" :
1533
                            return " ORDER BY INET_ATON($table.$field) $order ";
1534
                    break;
1535
                default:
1536

    
1537

    
1538
                        // Link with plugin tables 
1539
                        if ($type<=1000){
1540
                                if (preg_match("/^glpi_plugin_([a-zA-Z]+)/", $table, $matches) 
1541
                                || preg_match("/^glpi_dropdown_plugin_([a-zA-Z]+)/", $table, $matches) ){
1542
                                        if (count($matches)==2){
1543
                                                $plug=$matches[1];
1544

    
1545

    
1546
                                                $function='plugin_'.$plug.'_addOrderBy';
1547
                                                if (function_exists($function)){
1548
                                                        $out=$function($type,$ID,$order,$key);
1549
                                                        if (!empty($out)){
1550
                                                                return $out;
1551
                                                        }
1552
                                                } 
1553
                                        }
1554
                                } 
1555
                        }
1556

    
1557

    
1558
                        return " ORDER BY $table.$field $order ";
1559
                break;
1560
        }
1561

    
1562
}
1563

    
1564
/**
1565
 * Generic Function to add default columns to view
1566
 *
1567
 *
1568
 *@param $type device type
1569
 *
1570
 *
1571
 *@return select string
1572
 *
1573
 **/
1574
function addDefaultToView ($type){
1575
        global $CFG_GLPI;
1576

    
1577
        $toview=array();
1578

    
1579
        // Add first element (name)
1580
        array_push($toview,1);
1581
        
1582
        // Add entity view : 
1583
        if (isMultiEntitiesMode() && (isset($CFG_GLPI["union_search_type"][$type]) || isset($CFG_GLPI["recursive_type"][$type]) || count($_SESSION["glpiactiveentities"])>1)) {
1584
                array_push($toview,80);  
1585
        }
1586
        return $toview;
1587
}
1588

    
1589

    
1590
/**
1591
 * Generic Function to add default select to a request
1592
 *
1593
 *
1594
 *@param $type device type
1595
 *
1596
 *
1597
 *@return select string
1598
 *
1599
 **/
1600
function addDefaultSelect ($type){
1601
        global $CFG_GLPI, $LINK_ID_TABLE;
1602
        
1603
        switch ($type){
1604
                case RESERVATION_TYPE:
1605
                        $ret = "glpi_reservation_item.active as ACTIVE, ";
1606
                break;
1607
                case CARTRIDGE_TYPE:
1608
                        $ret = "glpi_cartridges_type.alarm as ALARM, ";
1609
                break;
1610
                case CONSUMABLE_TYPE:
1611
                        $ret = "glpi_consumables_type.alarm as ALARM, ";
1612
                break;
1613
                default :
1614
                        $ret = "";
1615
                break;
1616
        }
1617
        if (isset($CFG_GLPI["recursive_type"][$type])) {
1618
                $ret .= $LINK_ID_TABLE[$type].".FK_entities, ".$LINK_ID_TABLE[$type].".recursive, ";
1619
        }
1620
        return $ret;
1621
}
1622

    
1623
/**
1624
 * Generic Function to add select to a request
1625
 *
1626
 *
1627
 *@param $ID ID of the item to add
1628
 *@param $num item num in the request
1629
 *@param $type device type
1630
 *@param $meta is it a meta item ?
1631
 *@param $meta_type meta type table ID
1632
 *
1633
 *
1634
 *@return select string
1635
 *
1636
 **/
1637
function addSelect ($type,$ID,$num,$meta=0,$meta_type=0){
1638
        global $LINK_ID_TABLE,$SEARCH_OPTION,$PLUGIN_HOOKS;
1639

    
1640
        $table=$SEARCH_OPTION[$type][$ID]["table"];
1641
        $field=$SEARCH_OPTION[$type][$ID]["field"];
1642
        $addtable="";
1643
        $NAME="ITEM";
1644
        if ($meta) {
1645
                $NAME="META";
1646
                if ($LINK_ID_TABLE[$meta_type]!=$table)
1647
                        $addtable="_".$meta_type;
1648
        }
1649

    
1650
        // Plugin can override core definition for its type
1651
        if ($type>1000){
1652
                if (isset($PLUGIN_HOOKS['plugin_types'][$type])){
1653
                        $function='plugin_'.$PLUGIN_HOOKS['plugin_types'][$type].'_addSelect';
1654
                        if (function_exists($function)){
1655
                                $out=$function($type,$ID,$num);
1656
                                if (!empty($out)){
1657
                                        return $out;
1658
                                }
1659
                        } 
1660
                } 
1661
        }
1662

    
1663

    
1664
        switch ($table.".".$field){
1665
                case "glpi_software.name" :
1666
                case "glpi_computers.name" :
1667
                case "glpi_printers.name" :
1668
                case "glpi_networking.name" :
1669
                case "glpi_phones.name" :
1670
                case "glpi_monitors.name" :
1671
                case "glpi_peripherals.name" :
1672
                case "glpi_cartridges_type.name" :
1673
                case "glpi_consumables_type.name" :
1674
                case "glpi_contacts.name" :
1675
                case "glpi_type_docs.name" :
1676
                case "glpi_links.name" :
1677
                case "glpi_entities.name" :
1678
                case "glpi_docs.name" :
1679
                case "glpi_ocs_config.name" :
1680
                case "glpi_mailgate.name" :
1681
                case "glpi_transfers.name" :
1682
                case "state_types.name":
1683
                case "reservation_types.name":
1684
                        if ($meta){
1685
                                return " GROUP_CONCAT( DISTINCT ".$table.$addtable.".".$field." SEPARATOR '$$$$') AS ".$NAME."_$num, ";
1686
                        }
1687
                        else {
1688
                                return $table.$addtable.".".$field." AS ".$NAME."_$num, ".$table.$addtable.".ID AS ".$NAME."_".$num."_2, ";
1689
                        }
1690
                break;
1691

    
1692

    
1693
                case "glpi_enterprises.name" :
1694
                case "glpi_enterprises_infocoms.name" :
1695
                        if ($type==CONTACT_TYPE){
1696
                                return " GROUP_CONCAT( DISTINCT ".$table.$addtable.".".$field." SEPARATOR '$$$$') AS ".$NAME."_$num, ";
1697
                        } else {
1698
                                return $table.$addtable.".".$field." AS ".$NAME."_$num, ".$table.$addtable.".website AS ".$NAME."_".$num."_2, ".$table.$addtable.".ID AS ".$NAME."_".$num."_3, ";
1699
                        }
1700
                break;
1701
                // Contact for display in the enterprise item
1702
                case "glpi_contacts.completename":
1703
                        return " GROUP_CONCAT( DISTINCT CONCAT(".$table.$addtable.".name, ' ', ".$table.$addtable.".firstname) SEPARATOR '$$$$') AS ".$NAME."_$num, ";
1704
                break;
1705
                case "glpi_users.name" :
1706
                        $linkfield="";
1707
                if (!empty($SEARCH_OPTION[$type][$ID]["linkfield"]))
1708
                        $linkfield="_".$SEARCH_OPTION[$type][$ID]["linkfield"];
1709

    
1710
                        if ($meta){
1711
                                return " GROUP_CONCAT( DISTINCT CONCAT(".$table.$linkfield.$addtable.".realname,".$table.$linkfield.$addtable.".firstname) SEPARATOR '$$$$') AS ".$NAME."_$num, ";
1712
                        } else {
1713
                                return $table.$linkfield.$addtable.".".$field." AS ".$NAME."_$num,
1714
                                        ".$table.$linkfield.$addtable.".realname AS ".$NAME."_".$num."_2,
1715
                                        ".$table.$linkfield.$addtable.".ID AS ".$NAME."_".$num."_3,
1716
                                        ".$table.$linkfield.$addtable.".firstname AS ".$NAME."_".$num."_4,";
1717
                        }
1718
                break;
1719

    
1720

    
1721
                case "glpi_contracts.end_date" :
1722
                        return $table.$addtable.".begin_date AS ".$NAME."_$num, ".$table.$addtable.".duration AS ".$NAME."_".$num."_2, ";
1723
                break;
1724
                case "glpi_infocoms.end_warranty":
1725
                        return $table.$addtable.".buy_date AS ".$NAME."_$num, ".$table.$addtable.".warranty_duration AS ".$NAME."_".$num."_2, ";
1726
                break;
1727
                case "glpi_contracts.expire_notice" : // ajout jmd
1728
                        return $table.$addtable.".begin_date AS ".$NAME."_$num, ".$table.$addtable.".duration AS ".$NAME."_".$num."_2, ".$table.$addtable.".notice AS ".$NAME."_".$num."_3, ";
1729
                break;
1730
                case "glpi_contracts.expire" : // ajout jmd
1731
                        return $table.$addtable.".begin_date AS ".$NAME."_$num, ".$table.$addtable.".duration AS ".$NAME."_".$num."_2, ";
1732
                break;
1733
                case "glpi_device_hdd.specif_default" :
1734
                        return " SUM(DEVICE_".HDD_DEVICE.".specificity) / COUNT( DEVICE_".HDD_DEVICE.".ID) * COUNT( DISTINCT DEVICE_".HDD_DEVICE.".ID) AS ".$NAME."_".$num.", ";
1735
                break;
1736
                case "glpi_device_ram.specif_default" :
1737
                        return " SUM(DEVICE_".RAM_DEVICE.".specificity) / COUNT( DEVICE_".RAM_DEVICE.".ID) * COUNT( DISTINCT DEVICE_".RAM_DEVICE.".ID) AS ".$NAME."_".$num.", ";
1738
                break;
1739
                case "glpi_device_processor.specif_default" :
1740
                        return " SUM(DEVICE_".PROCESSOR_DEVICE.".specificity) / COUNT( DEVICE_".PROCESSOR_DEVICE.".ID) AS ".$NAME."_".$num.", ";
1741
                break;
1742
                case "glpi_networking_ports.ifmac" :
1743
                        if ($type==COMPUTER_TYPE)
1744
                                return " GROUP_CONCAT( DISTINCT ".$table.$addtable.".".$field." SEPARATOR '$$$$') AS ".$NAME."_$num, GROUP_CONCAT( DISTINCT DEVICE_".NETWORK_DEVICE.".specificity  SEPARATOR '$$$$') AS ".$NAME."_".$num."_2, ";
1745
                        else return " GROUP_CONCAT( DISTINCT ".$table.$addtable.".".$field." SEPARATOR '$$$$') AS ".$NAME."_$num, ";
1746
                break;
1747
                case "glpi_profiles.name" :
1748
                        if ($type==USER_TYPE){
1749
                                return " GROUP_CONCAT( ".$table.$addtable.".".$field." SEPARATOR '$$$$') AS ".$NAME."_$num, 
1750
                                        GROUP_CONCAT( glpi_entities.completename SEPARATOR '$$$$') AS ".$NAME."_".$num."_2,
1751
                                        GROUP_CONCAT( glpi_users_profiles.recursive SEPARATOR '$$$$') AS ".$NAME."_".$num."_3,";
1752
                        } else {
1753
                                return $table.$addtable.".".$field." AS ".$NAME."_$num, ";
1754
                        }
1755
                case "glpi_entities.completename" :
1756
                        if ($type==USER_TYPE){
1757
                                return " GROUP_CONCAT( ".$table.$addtable.".completename SEPARATOR '$$$$') AS ".$NAME."_$num, 
1758
                                        GROUP_CONCAT( glpi_profiles.name SEPARATOR '$$$$') AS ".$NAME."_".$num."_2,
1759
                                        GROUP_CONCAT( glpi_users_profiles.recursive SEPARATOR '$$$$') AS ".$NAME."_".$num."_3,";
1760
                        } else {
1761
                                return $table.$addtable.".completename AS ".$NAME."_$num, ".$table.$addtable.".ID AS ".$NAME."_".$num."_2, ";
1762
                        }
1763

    
1764
                case "glpi_groups.name" :
1765
                        if ($type==USER_TYPE){
1766
                                return " GROUP_CONCAT( DISTINCT ".$table.$addtable.".".$field." SEPARATOR '$$$$') AS ".$NAME."_$num, ";
1767
                        } else {
1768
                                return $table.$addtable.".".$field." AS ".$NAME."_$num, ";
1769
                        }
1770
                break;
1771
                case "glpi_auth_tables.name":
1772
                        return "glpi_users.auth_method AS ".$NAME."_".$num.", glpi_users.id_auth AS ".$NAME."_".$num."_2, glpi_auth_ldap".$addtable.".".$field." AS ".$NAME."_".$num."_3, glpi_auth_mail".$addtable.".".$field." AS ".$NAME."_".$num."_4, ";
1773
                break;
1774
                case "glpi_contracts.name" :
1775
                case "glpi_contracts.num" :
1776
                        if ($type!=CONTRACT_TYPE){
1777
                                return " GROUP_CONCAT( DISTINCT ".$table.$addtable.".".$field." SEPARATOR '$$$$') AS ".$NAME."_$num, ";
1778
                        } else {
1779
                                return $table.$addtable.".".$field." AS ".$NAME."_$num, ";
1780
                        }
1781
                break;
1782
                case "glpi_licenses.serial" :
1783
                case "glpi_licenses.version" :
1784
                        if ($meta){
1785
                                return " GROUP_CONCAT( DISTINCT CONCAT(".$table.$addtable.".serial, ' - ',".$table.$addtable.".version) SEPARATOR '$$$$') AS ".$NAME."_".$num.", ";
1786
                        } else {
1787
                                return " GROUP_CONCAT( DISTINCT ".$table.$addtable.".".$field." SEPARATOR '$$$$') AS ".$NAME."_".$num.", ";
1788
                        }
1789
                break;
1790
                case "glpi_networking_ports.ifaddr" :
1791
                case "glpi_dropdown_netpoint.name" :
1792
                case "glpi_registry.registry_ocs_name" :
1793
                case "glpi_registry.registry_value" :
1794
                        return " GROUP_CONCAT( DISTINCT ".$table.$addtable.".".$field." SEPARATOR '$$$$') AS ".$NAME."_".$num.", ";
1795
                break;
1796
                case "glpi_tracking.count" :
1797
                        return " COUNT(DISTINCT glpi_tracking.ID) AS ".$NAME."_".$num.", ";
1798
                break;
1799
                default:
1800

    
1801

    
1802
                        // Link with plugin tables 
1803
                        if ($type<=1000){
1804
                                if (preg_match("/^glpi_plugin_([a-zA-Z]+)/", $table, $matches) 
1805
                                || preg_match("/^glpi_dropdown_plugin_([a-zA-Z]+)/", $table, $matches) ){
1806
                                        if (count($matches)==2){
1807
                                                $plug=$matches[1];
1808

    
1809
                                                $function='plugin_'.$plug.'_addSelect';
1810
                                                if (function_exists($function)){
1811
                                                        $out=$function($type,$ID,$num);
1812
                                                        if (!empty($out)){
1813
                                                                return $out;
1814
                                                        }
1815
                                                } 
1816
                                        }
1817
                                } 
1818
                        }
1819

    
1820

    
1821
                        if ($meta){
1822
                                return " GROUP_CONCAT( DISTINCT ".$table.$addtable.".".$field." SEPARATOR '$$$$') AS ".$NAME."_$num, ";
1823
                        }
1824
                        else {
1825
                                return $table.$addtable.".".$field." AS ".$NAME."_$num, ";
1826
                        }
1827
                        break;
1828
        }
1829

    
1830
}
1831

    
1832

    
1833
/**
1834
 * Generic Function to add default where to a request
1835
 *
1836
 *
1837
 *@param $type device type
1838
 *
1839
 *@return select string
1840
 *
1841
 **/
1842
function addDefaultWhere ($type){
1843
        switch ($type){
1844
                // No link
1845
                case USER_TYPE:
1846
                        // View all entities
1847
                        if (isViewAllEntities()){
1848
                                return "";
1849
                        } else {
1850
                                return getEntitiesRestrictRequest("","glpi_users_profiles");
1851
                        }
1852
                break;
1853
                default :
1854
                        return "";
1855
                break;
1856
        }
1857
}
1858

    
1859
/**
1860
 * Generic Function to add where to a request
1861
 *
1862
 *
1863
 *@param $val item num in the request
1864
 *@param $nott is it a negative serach ?
1865
 *@param $link link string
1866
 *@param $type device type
1867
 *@param $ID ID of the item to search
1868
 *@param $meta is a meta search (meta=2 in search.class.php)
1869
 *
1870
 *@return select string
1871
 *
1872
 **/
1873
function addWhere ($link,$nott,$type,$ID,$val,$meta=0){
1874
        global $LINK_ID_TABLE,$LANG,$SEARCH_OPTION,$PLUGIN_HOOKS;
1875

    
1876
        $table=$SEARCH_OPTION[$type][$ID]["table"];
1877
        $field=$SEARCH_OPTION[$type][$ID]["field"];
1878
        
1879
        $inittable=$table;
1880
        if ($meta&&$LINK_ID_TABLE[$type]!=$table) {
1881
                $table.="_".$type;
1882
        }
1883

    
1884
        // Hack to allow search by ID on every sub-table
1885
        if (preg_match('/^\$\$\$\$([0-9]+)$/',$val,$regs)){
1886
                 return $link." ($table.ID ".($nott?"<>":"=").$regs[1].") ";
1887
         }
1888

    
1889
        $SEARCH=makeTextSearch($val,$nott);
1890

    
1891
        // Plugin can override core definition for its type
1892
        if ($type>1000){
1893
                if (isset($PLUGIN_HOOKS['plugin_types'][$type])){
1894
                        $function='plugin_'.$PLUGIN_HOOKS['plugin_types'][$type].'_addWhere';
1895
                        if (function_exists($function)){
1896
                                $out=$function($link,$nott,$type,$ID,$val);
1897
                                if (!empty($out)){
1898
                                        return $out;
1899
                                }
1900
                        } 
1901
                } 
1902
        }
1903

    
1904

    
1905
        switch ($inittable.".".$field){
1906
                case "glpi_users.name" :
1907
                        $linkfield="";
1908
                        if (!empty($SEARCH_OPTION[$type][$ID]["linkfield"])){
1909
                                $linkfield="_".$SEARCH_OPTION[$type][$ID]["linkfield"];
1910

    
1911
                                if ($meta&&$LINK_ID_TABLE[$type]!=$inittable) {
1912
                                        $table=$inittable;
1913
                                        $linkfield.="_".$type;
1914
                                }
1915
                        }
1916
                        if ($type==USER_TYPE){ // glpi_users case / not link table
1917
                                return $link." ( $table$linkfield.$field $SEARCH ) ";
1918
                        } else {
1919
                                $ADD="";
1920
                                if ($nott) {
1921
                                        $ADD=" OR $table$linkfield.$field IS NULL";
1922
                                }
1923
                                return $link." ( $table$linkfield.$field $SEARCH OR $table$linkfield.realname $SEARCH OR $table$linkfield.firstname $SEARCH OR CONCAT($table$linkfield.realname,' ',$table$linkfield.firstname) $SEARCH $ADD) ";
1924
                        }
1925
                        break;
1926
                case "glpi_device_hdd.specif_default" :
1927
                case "glpi_device_ram.specif_default" :
1928
                case "glpi_device_processor.specif_default" :
1929
                        return $link." $table.$field ".makeTextSearch("",$nott);
1930
                        break;
1931
                case "glpi_networking_ports.ifmac" :
1932
                        $ADD="";
1933
                        if ($type==COMPUTER_TYPE){
1934
                                if ($nott) {
1935
                                        $ADD=" OR $table.$field IS NULL";
1936
                                }
1937

    
1938
                                return $link." (  DEVICE_".NETWORK_DEVICE.".specificity $SEARCH OR $table.$field $SEARCH $ADD ) ";
1939
                        } else {
1940
                                if ($nott) {
1941
                                        $ADD=" OR $table.$field IS NULL";
1942
                                }
1943

    
1944
                                return $link." $table.$field $SEARCH $ADD";
1945
                        }
1946
                        break;
1947

    
1948
                case "glpi_infocoms.end_warranty" :
1949
                case "glpi_contracts.end_date" :
1950
                case "glpi_ocs_link.last_update":
1951
                case "glpi_ocs_link.last_ocs_update":
1952
                case "glpi_computers.date_mod":
1953
                case "glpi_printers.date_mod":
1954
                case "glpi_networking.date_mod":
1955
                case "glpi_peripherals.date_mod":
1956
                case "glpi_software.date_mod":
1957
                case "glpi_phones.date_mod":
1958
                case "glpi_monitors.date_mod":
1959
                case "glpi_contracts.begin_date":
1960
                case "glpi_docs.date_mod":
1961
                case "glpi_infocoms.buy_date":
1962
                case "glpi_infocoms.use_date":
1963
                case "state_types.date_mod":
1964
                case "reservation_types.date_mod":
1965
                case "glpi_users.last_login":
1966
                case "glpi_users.date_mod":
1967
                        $date_computation=$table.".".$field;
1968
                        $interval_search=" MONTH ";
1969
                        switch ($table.".".$field){
1970
                                case "glpi_contracts.end_date":
1971
                                        $date_computation=" ADDDATE($table.begin_date, INTERVAL $table.duration MONTH) ";
1972
                                        break;
1973
                                case "glpi_infocoms.end_warranty":
1974
                                        $date_computation=" ADDDATE($table.buy_date, INTERVAL $table.warranty_duration MONTH) ";
1975
                                        break;
1976
                        }
1977
                        
1978
                        $search=array("/\&lt;/","/\&gt;/");
1979
                        $replace=array("<",">");
1980
                        $val=preg_replace($search,$replace,$val);
1981
                        if (ereg("([<>=])(.*)",$val,$regs)){
1982
                                if (is_numeric($regs[2])){
1983
                                        return $link." NOW() ".$regs[1]." ADDDATE($date_computation, INTERVAL ".$regs[2]." $interval_search) ";        
1984
                                } else {
1985
                                        // Reformat date if needed
1986
                                        $regs[2]=preg_replace('/(\d{1,2})-(\d{1,2})-(\d{4})/','\3-\2-\1',$regs[2]);
1987
                                        if (ereg('[0-9]{2,4}-[0-9]{1,2}-[0-9]{1,2}',$regs[2])){
1988
                                                return $link." $date_computation ".$regs[1]." '".$regs[2]."'";
1989
                                        } else {
1990
                                                return "";
1991
                                        }
1992
                                }
1993
                        } else { // standard search
1994
                                // Date format modification if needed
1995
                                $val=preg_replace('/(\d{1,2})-(\d{1,2})-(\d{4})/','\3-\2-\1',$val);
1996
                                $SEARCH=makeTextSearch($val,$nott);
1997
                                $ADD="";        
1998
                                if ($nott) {
1999
                                        $ADD=" OR $date_computation IS NULL";
2000
                                }
2001
                                return $link." ( $date_computation $SEARCH $ADD )";
2002
                        }
2003
                        break;
2004
                case "glpi_contracts.expire" :
2005
                        $search=array("/\&lt;/","/\&gt;/");
2006
                        $replace=array("<",">");
2007
                        $val=preg_replace($search,$replace,$val);
2008
                        if (ereg("([<>=])(.*)",$val,$regs)){
2009
                                return $link." DATEDIFF(ADDDATE($table.begin_date, INTERVAL $table.duration MONTH),CURDATE() )".$regs[1].$regs[2]." ";
2010
                                } else {
2011
                                return $link." ADDDATE($table.begin_date, INTERVAL $table.duration MONTH) $SEARCH ";                
2012
                        }
2013
                        break;
2014
                // ajout jmd
2015
                case "glpi_contracts.expire_notice" :
2016
                        $search=array("/\&lt;/","/\&gt;/");
2017
                        $replace=array("<",">");
2018
                        $val=preg_replace($search,$replace,$val);
2019
                        if (ereg("([<>])(.*)",$val,$regs)){
2020
                                return $link." $table.notice<>0 AND DATEDIFF(ADDDATE($table.begin_date, INTERVAL ($table.duration - $table.notice) MONTH),CURDATE() )".$regs[1].$regs[2]." ";
2021
                        } else {
2022
                                return $link." ADDDATE($table.begin_date, INTERVAL ($table.duration - $table.notice) MONTH) $SEARCH ";                
2023
                        }
2024
                        break;
2025

    
2026
                case "glpi_infocoms.value":
2027
                case "glpi_infocoms.warranty_value":
2028
                        if (is_numeric($val)){
2029
                                $search=array("/\&lt;/","/\&gt;/");
2030
                                $replace=array("<",">");
2031
                                $val=preg_replace($search,$replace,$val);
2032
                                if (ereg("([<>])([=]*)[[:space:]]*([0-9]*)",$val,$regs)){
2033
                                        if ($nott){
2034
                                                if ($regs[1]=='<') {
2035
                                                        $regs[1]='>';
2036
                                                } else {
2037
                                                        $regs[1]='<';
2038
                                                }
2039
                                        } 
2040
                                                $regs[1].=$regs[2];
2041
                                        
2042
                                        return $link." ($table.$field ".$regs[1]." ".$regs[3]." ) ";
2043
                                } else {
2044
        
2045
                                        $interval=100;
2046
                                        $ADD="";
2047
                                        if ($nott&&$val!="NULL") $ADD=" OR $table.$field IS NULL";
2048
                                        if ($nott){
2049
                                                return $link." ($table.$field < ".intval($val)."-$interval OR $table.$field > ".intval($val)."+$interval ".$ADD." ) ";
2050
                                        } else {
2051
                                                return $link." (($table.$field >= ".intval($val)."-$interval AND $table.$field <= ".intval($val)."+$interval) ".$ADD." ) ";
2052
                                        }
2053
                                }
2054
                        }
2055
                        break;
2056
                case "glpi_infocoms.amort_time":
2057
                case "glpi_infocoms.warranty_duration":
2058
                        $ADD="";
2059
                        if ($nott&&$val!="NULL") {
2060
                                $ADD=" OR $table.$field IS NULL";
2061
                        }
2062
                        if (is_numeric($val))
2063
                        {
2064
                                if ($nott){
2065
                                        return $link." ($table.$field <> ".intval($val)." ".$ADD." ) ";
2066
                                } else {
2067
                                        return $link." ($table.$field = ".intval($val)."  ".$ADD." ) ";
2068
                                }
2069
                        }
2070
                        break;
2071
                case "glpi_infocoms.amort_type":
2072
                        $ADD="";
2073
                        if ($nott&&$val!="NULL") {
2074
                                $ADD=" OR $table.$field IS NULL";
2075
                        }
2076
                        if (eregi($val,getAmortTypeName(1))) {
2077
                                $val=1;
2078
                        } else if (eregi($val,getAmortTypeName(2))) {
2079
                                $val=2;
2080
                        } 
2081

    
2082
                        
2083
                        if (is_int($val)&&$val>0){
2084
                                if ($nott){
2085
                                        return $link." ($table.$field <> $val ".$ADD." ) ";
2086
                                } else {
2087
                                        return $link." ($table.$field = $val  ".$ADD." ) ";
2088
                                }
2089
                        }
2090
                        break;
2091
                case "glpi_contacts.completename":
2092
                        return $link." ($table.name $SEARCH OR $table.firstname $SEARCH ) ";
2093
                break;
2094
                case "glpi_auth_tables.name":
2095
                        return $link." (glpi_auth_mail.name $SEARCH OR glpi_auth_ldap.name $SEARCH ) ";
2096
                break;
2097
                case "glpi_contracts.renewal":
2098
                        return $link." ".$table.".".$field."=".getContractRenewalIDByName($val);
2099
                break;
2100
                default:
2101

    
2102
                        // Link with plugin tables 
2103
                        if ($type<=1000){
2104
                                if (preg_match("/^glpi_plugin_([a-zA-Z]+)/", $inittable, $matches) 
2105
                                || preg_match("/^glpi_dropdown_plugin_([a-zA-Z]+)/", $inittable, $matches) ){
2106
                                        if (count($matches)==2){
2107
                                                $plug=$matches[1];
2108

    
2109
                                                $function='plugin_'.$plug.'_addWhere';
2110
                                                if (function_exists($function)){
2111
                                                        $out=$function($link,$nott,$type,$ID,$val);
2112
                                                        if (!empty($out)){
2113
                                                                return $out;
2114
                                                        }
2115
                                                } 
2116
                                        }
2117
                                } 
2118
                        }
2119

    
2120
                        $ADD="";        
2121
                        if (($nott&&$val!="NULL")||$val=='^$') {
2122
                                $ADD=" OR $table.$field IS NULL";
2123
                        }
2124
                        
2125
                        return $link." ($table.$field $SEARCH ".$ADD." ) ";
2126
                        break;
2127
        }
2128

    
2129
}
2130

    
2131

    
2132
/**
2133
 * Generic Function to display Items
2134
 *
2135
 *
2136
 *@param $field field which have a specific display type
2137
 *@param $type device type
2138
 *
2139
 *
2140
 *@return string to print
2141
 *
2142
 **/
2143
function displayConfigItem ($type,$field){
2144

    
2145
        switch ($field){
2146
                case "glpi_ocs_link.last_update":
2147
                case "glpi_ocs_link.last_ocs_update":
2148
                case "glpi_computers.date_mod":
2149
                case "glpi_printers.date_mod":
2150
                case "glpi_networking.date_mod":
2151
                case "glpi_peripherals.date_mod":
2152
                case "glpi_phones.date_mod":
2153
                case "glpi_software.date_mod":
2154
                case "glpi_monitors.date_mod":
2155
                case "glpi_docs.date_mod":
2156
                case "glpi_ocs_config.date_mod" :
2157
                case "glpi_users.last_login":
2158
                case "glpi_users.date_mod":        
2159
                                return " class='center'";
2160
                        break;
2161
                default:
2162
                        return "";
2163
                        break;
2164
        }
2165

    
2166
}
2167

    
2168
/**
2169
 * Generic Function to display Items
2170
 *
2171
 *
2172
 *@param $field field to add
2173
 *@param $data array containing data results
2174
 *@param $num item num in the request
2175
 *@param $type device type
2176
 *@param $linkfield field used to link
2177
 *
2178
 *
2179
 *@return string to print
2180
 *
2181
 **/
2182
function giveItem ($type,$field,$data,$num,$linkfield=""){
2183
        global $CFG_GLPI,$INFOFORM_PAGES,$CFG_GLPI,$LANG,$LINK_ID_TABLE,$PLUGIN_HOOKS;
2184

    
2185

    
2186
        if (isset($CFG_GLPI["union_search_type"][$type])){
2187
                return giveItem ($data["TYPE"],ereg_replace($CFG_GLPI["union_search_type"][$type],$LINK_ID_TABLE[$data["TYPE"]],$field),$data,$num,$linkfield);
2188
        }
2189

    
2190
        // Plugin can override core definition for its type
2191
        if ($type>1000){
2192
                if (isset($PLUGIN_HOOKS['plugin_types'][$type])){
2193
                        $function='plugin_'.$PLUGIN_HOOKS['plugin_types'][$type].'_giveItem';
2194
                        if (function_exists($function)){
2195
                                $out=$function($type,$field,$data,$num,$linkfield);
2196
                                if (!empty($out)){
2197
                                        return $out;
2198
                                }
2199
                        } 
2200
                } 
2201
        }
2202

    
2203
        switch ($field){
2204
                case "glpi_computers.name" :
2205
                        if (!empty($data["ITEM_".$num."_2"])){
2206
                                $out= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[COMPUTER_TYPE]."?ID=".$data["ITEM_".$num."_2"]."\">";
2207
                                $out.= $data["ITEM_$num"];
2208
                                if ($CFG_GLPI["view_ID"]||empty($data["ITEM_$num"])) {
2209
                                        $out.= " (".$data["ITEM_".$num."_2"].")";
2210
                                }
2211
                                $out.= "</a>";
2212
                                return $out;
2213
                        }
2214
                break;
2215
                case "glpi_printers.name" :
2216
                        if (!empty($data["ITEM_".$num."_2"])){
2217
                                $out= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[PRINTER_TYPE]."?ID=".$data["ITEM_".$num."_2"]."\">";
2218
                                $out.= $data["ITEM_$num"];
2219
                                if ($CFG_GLPI["view_ID"]||empty($data["ITEM_$num"])) {
2220
                                        $out.= " (".$data["ITEM_".$num."_2"].")";
2221
                                }
2222
                                $out.= "</a>";
2223
                                return $out;
2224
                        }
2225
                break;
2226
                case "glpi_networking.name" :
2227
                        if (!empty($data["ITEM_".$num."_2"])){
2228
                                $out= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[NETWORKING_TYPE]."?ID=".$data["ITEM_".$num."_2"]."\">";
2229
                                $out.= $data["ITEM_$num"];
2230
                                if ($CFG_GLPI["view_ID"]||empty($data["ITEM_$num"])) {
2231
                                        $out.= " (".$data["ITEM_".$num."_2"].")";
2232
                                }
2233
                                $out.= "</a>";
2234
                                return $out;
2235
                        }
2236
                break;
2237
                case "glpi_phones.name" :
2238
                        if (!empty($data["ITEM_".$num."_2"])){
2239
                                $out= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[PHONE_TYPE]."?ID=".$data["ITEM_".$num."_2"]."\">";
2240
                                $out.= $data["ITEM_$num"];
2241
                                if ($CFG_GLPI["view_ID"]||empty($data["ITEM_$num"])) {
2242
                                        $out.= " (".$data["ITEM_".$num."_2"].")";
2243
                                }
2244
                                $out.= "</a>";
2245
                                return $out;
2246
                        }
2247
                break;
2248
                case "glpi_monitors.name" :
2249
                        if (!empty($data["ITEM_".$num."_2"])){
2250
                                $out= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[MONITOR_TYPE]."?ID=".$data["ITEM_".$num."_2"]."\">";
2251
                                $out.= $data["ITEM_$num"];
2252
                                if ($CFG_GLPI["view_ID"]||empty($data["ITEM_$num"])) {
2253
                                        $out.= " (".$data["ITEM_".$num."_2"].")";
2254
                                }
2255
                                $out.= "</a>";
2256
                                return $out;
2257
                        }
2258
                break;
2259
                case "glpi_software.name" :
2260
                        if (!empty($data["ITEM_".$num."_2"])){
2261
                                $out= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[SOFTWARE_TYPE]."?ID=".$data["ITEM_".$num."_2"]."\">";
2262
                                $out.= $data["ITEM_$num"];
2263
                                if ($CFG_GLPI["view_ID"]||empty($data["ITEM_$num"])) {
2264
                                        $out.= " (".$data["ITEM_".$num."_2"].")";
2265
                                }
2266
                                $out.= "</a>";
2267
                                return $out;
2268
                        }
2269
                break;
2270
                case "glpi_peripherals.name" :
2271
                        if (!empty($data["ITEM_".$num."_2"])){
2272
                                $out= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[PERIPHERAL_TYPE]."?ID=".$data["ITEM_".$num."_2"]."\">";
2273
                                $out.= $data["ITEM_$num"];
2274
                                if ($CFG_GLPI["view_ID"]||empty($data["ITEM_$num"])) {
2275
                                        $out.= " (".$data["ITEM_".$num."_2"].")";
2276
                                }
2277
                                $out.= "</a>";
2278
                                return $out;
2279
                        }
2280
                break;
2281
                case "glpi_cartridges_type.name" :
2282
                        if (!empty($data["ITEM_".$num."_2"])){
2283
                                $out= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[CARTRIDGE_TYPE]."?ID=".$data["ITEM_".$num."_2"]."\">";
2284
                                $out.= $data["ITEM_$num"];
2285
                                if ($CFG_GLPI["view_ID"]||empty($data["ITEM_$num"])) {
2286
                                        $out.= " (".$data["ITEM_".$num."_2"].")";
2287
                                }
2288
                                $out.= "</a>";
2289
                                return $out;
2290
                        }
2291
                break;
2292
                case "glpi_consumables_type.name" :
2293
                        if (!empty($data["ITEM_".$num."_2"])){
2294
                                $out= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[CONSUMABLE_TYPE]."?ID=".$data["ITEM_".$num."_2"]."\">";
2295
                                $out.= $data["ITEM_$num"];
2296
                                if ($CFG_GLPI["view_ID"]||empty($data["ITEM_$num"])) {
2297
                                        $out.= " (".$data["ITEM_".$num."_2"].")";
2298
                                }
2299
                                $out.= "</a>";
2300
                                return $out;
2301
                        }
2302
                break;
2303
                case "glpi_contacts.name" :
2304
                        if (!empty($data["ITEM_".$num."_2"])){
2305
                                $out= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[CONTACT_TYPE]."?ID=".$data["ITEM_".$num."_2"]."\">";
2306
                                $out.= $data["ITEM_$num"];
2307
                                if ($CFG_GLPI["view_ID"]||empty($data["ITEM_$num"])) {
2308
                                        $out.= " (".$data["ITEM_".$num."_2"].")";
2309
                                }
2310
                                $out.= "</a>";
2311
                                return $out;
2312
                        }
2313
                break;
2314
                case "glpi_type_docs.name" :
2315
                        if (!empty($data["ITEM_".$num."_2"])){
2316
                                $out= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[TYPEDOC_TYPE]."?ID=".$data["ITEM_".$num."_2"]."\">";
2317
                                $out.= $data["ITEM_$num"];
2318
                                if ($CFG_GLPI["view_ID"]||empty($data["ITEM_$num"])) {
2319
                                        $out.= " (".$data["ITEM_".$num."_2"].")";
2320
                                }
2321
                                $out.= "</a>";
2322
                                return $out;
2323
                        }
2324
                break;
2325
                case "glpi_links.name" :
2326
                        if (!empty($data["ITEM_".$num."_2"])){
2327
                                $out= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[LINK_TYPE]."?ID=".$data["ITEM_".$num."_2"]."\">";
2328
                                $out.= $data["ITEM_$num"];
2329
                                if ($CFG_GLPI["view_ID"]||empty($data["ITEM_$num"])) {
2330
                                        $out.= " (".$data["ITEM_".$num."_2"].")";
2331
                                }
2332
                                $out.= "</a>";
2333
                                return $out;
2334
                        }
2335
                break;
2336
                case "glpi_docs.name" :
2337
                        if (!empty($data["ITEM_".$num."_2"])){
2338
                                $out= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[DOCUMENT_TYPE]."?ID=".$data["ITEM_".$num."_2"]."\">";
2339
                                $out.= $data["ITEM_$num"];
2340
                                if ($CFG_GLPI["view_ID"]||empty($data["ITEM_$num"])) {
2341
                                        $out.= " (".$data["ITEM_".$num."_2"].")";
2342
                                }
2343
                                $out.= "</a>";
2344
                                return $out;
2345
                        }
2346
                break;
2347
                case "glpi_ocs_config.name" :
2348
                        if (!empty($data["ITEM_".$num."_2"])){
2349
                                $out= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[OCSNG_TYPE]."?ID=".$data["ITEM_".$num."_2"]."\">";
2350
                                $out.= $data["ITEM_$num"];
2351
                                if ($CFG_GLPI["view_ID"]||empty($data["ITEM_$num"])) {
2352
                                        $out.= " (".$data["ITEM_".$num."_2"].")";
2353
                                }
2354
                                $out.= "</a>";
2355
                                return $out;
2356
                        }
2357
                break;
2358
                case "glpi_entities.name" :
2359
                        if (!empty($data["ITEM_".$num."_2"])){
2360
                                $out= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[ENTITY_TYPE]."?ID=".$data["ITEM_".$num."_2"]."\">";
2361
                                $out.= $data["ITEM_$num"];
2362
                                if ($CFG_GLPI["view_ID"]||empty($data["ITEM_$num"])) {
2363
                                        $out.= " (".$data["ITEM_".$num."_2"].")";
2364
                                }
2365
                                $out.= "</a>";
2366
                                return $out;
2367
                        }
2368
                break;
2369
                case "glpi_mailgate.name" :
2370
                        if (!empty($data["ITEM_".$num."_2"])){
2371
                                $out= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[MAILGATE_TYPE]."?ID=".$data["ITEM_".$num."_2"]."\">";
2372
                                $out.= $data["ITEM_$num"];
2373
                                if ($CFG_GLPI["view_ID"]||empty($data["ITEM_$num"])) {
2374
                                        $out.= " (".$data["ITEM_".$num."_2"].")";
2375
                                }
2376
                                $out.= "</a>";
2377
                                return $out;
2378
                        }
2379
                break;
2380
                case "glpi_transfers.name" :
2381
                        if (!empty($data["ITEM_".$num."_2"])){
2382
                                $out= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[TRANSFER_TYPE]."?ID=".$data["ITEM_".$num."_2"]."\">";
2383
                                $out.= $data["ITEM_$num"];
2384
                                if ($CFG_GLPI["view_ID"]||empty($data["ITEM_$num"])) {
2385
                                        $out.= " (".$data["ITEM_".$num."_2"].")";
2386
                                }
2387
                                $out.= "</a>";
2388
                                return $out;
2389
                        }
2390
                break;
2391
                case "glpi_licenses.version" :
2392
                case "glpi_licenses.serial" :
2393
                case "glpi_networking_ports.ifaddr" :
2394
                case "glpi_dropdown_netpoint.name" :
2395
                case "glpi_registry.registry_ocs_name" :
2396
                case "glpi_registry.registry_value" :
2397
                $out="";
2398
                $split=explode("$$$$",$data["ITEM_$num"]);
2399

    
2400
                $count_display=0;
2401
                for ($k=0;$k<count($split);$k++)
2402
                        if (strlen(trim($split[$k]))>0){
2403
                                if ($count_display) $out.= "<br>";
2404
                                $count_display++;
2405
                                $out.= $split[$k];
2406
                        }
2407
                return $out;
2408

    
2409
                break;
2410
                case "glpi_users.name" :                
2411
                        // USER search case
2412
                        if (empty($linkfield)){
2413
                                $out= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[$type]."?ID=".$data['ID']."\">";
2414
                                $out.= $data["ITEM_$num"];
2415
                                if ($CFG_GLPI["view_ID"]||empty($data["ITEM_$num"])) $out.= " (".$data["ID"].")";
2416
                                $out.= "</a>";
2417
                        } else {
2418
                                $type=USER_TYPE;
2419
                                $out="";
2420
                                if ($data["ITEM_".$num."_3"]>0)
2421
                                        $out= "<a href=\"".$CFG_GLPI["root_doc"]."/front/user.form.php?ID=".$data["ITEM_".$num."_3"]."\">";
2422
                                // print realname or login name
2423
                                if (!empty($data["ITEM_".$num."_2"])||!empty($data["ITEM_".$num."_4"]))
2424
                                        $out .= $data["ITEM_".$num."_2"]." ".$data["ITEM_".$num."_4"];
2425
                                else $out .= $data["ITEM_$num"];
2426

    
2427
                                if ($data["ITEM_".$num."_3"]>0&&($CFG_GLPI["view_ID"]||(empty($data["ITEM_$num"])))) $out.= " (".$data["ITEM_".$num."_3"].")";
2428

    
2429
                                if ($data["ITEM_".$num."_3"]>0)
2430
                                        $out.= "</a>";
2431
                        }
2432
                return $out;
2433
                break;
2434
                case "glpi_groups.name" :                
2435
                        if (empty($linkfield)){
2436
                                $out="";
2437
                                $split=explode("$$$$",$data["ITEM_$num"]);
2438

    
2439
                                $count_display=0;
2440
                                for ($k=0;$k<count($split);$k++)
2441
                                        if (strlen(trim($split[$k]))>0){
2442
                                                if ($count_display) $out.= "<br>";
2443
                                                $count_display++;
2444
                                                $out.= $split[$k];
2445
                                        }
2446
                                return $out;
2447
                        } else {
2448
                                if ($type==GROUP_TYPE){
2449
                                        $out= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[$type]."?ID=".$data['ID']."\">";
2450
                                        $out.= $data["ITEM_$num"];
2451
                                        if ($CFG_GLPI["view_ID"]||empty($data["ITEM_$num"])) $out.= " (".$data["ID"].")";
2452
                                        $out.= "</a>";
2453
                                } else {
2454
                                        $out= $data["ITEM_$num"];
2455
                                }
2456
                        }
2457
                return $out;
2458
                break;
2459
                case "glpi_computers.comments" :
2460
                case "glpi_networking.comments" :
2461
                case "glpi_printers.comments" :
2462
                case "glpi_monitors.comments" :
2463
                case "glpi_peripherals.comments" :
2464
                case "glpi_software.comments" :
2465
                case "glpi_contacts.comments" :
2466
                case "glpi_enterprises.comments" :
2467
                case "glpi_users.comments" :
2468
                case "glpi_phones.comments" :
2469
                case "glpi_groups.comments" :
2470
                case "glpi_entities.comments" :
2471
                case "glpi_consumables_type.comments" :
2472
                case "glpi_docs.comment" :
2473
                case "glpi_cartridges_type.comments" :
2474
                        return nl2br($data["ITEM_$num"]);
2475
                break;
2476
                case "glpi_profiles.name" :
2477
                        if ($type==PROFILE_TYPE){
2478
                                $out= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[$type]."?ID=".$data['ID']."\">";
2479
                                $out.= $data["ITEM_$num"];
2480
                                if ($CFG_GLPI["view_ID"]||empty($data["ITEM_$num"])) {
2481
                                        $out.= " (".$data["ID"].")";
2482
                                }
2483
                                $out.= "</a>";
2484
                        } else if ($type==USER_TYPE){        
2485
                                $out="";
2486

    
2487
                                $split=explode("$$$$",$data["ITEM_$num"]);
2488
                                $split2=explode("$$$$",$data["ITEM_".$num."_2"]);
2489
                                $split3=explode("$$$$",$data["ITEM_".$num."_3"]);
2490

    
2491
                                $count_display=0;
2492
                                $added=array();
2493
                                for ($k=0;$k<count($split);$k++)
2494
                                        if (strlen(trim($split[$k]))>0){
2495
                                                $text=$split[$k]." - ".$split2[$k];
2496
                                                if ($split3[$k]){
2497
                                                        $text.=" (R)";
2498
                                                }
2499
                                                if (!in_array($text,$added)){
2500
                                                        if ($count_display) $out.= "<br>";
2501
                                                        $count_display++;
2502
                                                        $out.= $text;
2503
                                                        $added[]=$text;
2504
                                                }
2505
                                        }
2506
                                return $out;
2507
                        } else {
2508
                                $out= $data["ITEM_$num"];
2509
                        }
2510
                        return $out;
2511
                break;
2512
                case "glpi_entities.completename" :
2513
                        if ($type==ENTITY_TYPE){
2514
                                $out= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[$type]."?ID=".$data['ID']."\">";
2515
                                $out.= $data["ITEM_$num"];
2516
                                if ($CFG_GLPI["view_ID"]||empty($data["ITEM_$num"])) {
2517
                                        $out.= " (".$data["ID"].")";
2518
                                }
2519
                                $out.= "</a>";
2520
                        } else if ($type==USER_TYPE){        
2521
                                $out="";
2522

    
2523
                                $split=explode("$$$$",$data["ITEM_$num"]);
2524
                                $split2=explode("$$$$",$data["ITEM_".$num."_2"]);
2525
                                $split3=explode("$$$$",$data["ITEM_".$num."_3"]);
2526
                                $added=array();
2527
                                $count_display=0;
2528
                                for ($k=0;$k<count($split);$k++)
2529
                                        if (strlen(trim($split[$k]))>0){
2530
                                                $text=$split[$k]." - ".$split2[$k];
2531
                                                if ($split3[$k]){
2532
                                                        $text.=" (R)";
2533
                                                }
2534
                                                if (!in_array($text,$added)){
2535
                                                        if ($count_display) $out.= "<br>";
2536
                                                        $count_display++;
2537
                                                        $out.= $text;
2538
                                                        $added[]=$text;
2539
                                                }
2540
                                        }
2541
                                return $out;
2542
                        } else {
2543
                                if ($data["ITEM_".$num."_2"]==0){
2544
                                        $out=$LANG["entity"][2];
2545
                                } else {
2546
                                        $out= $data["ITEM_$num"];
2547
                                }
2548
                        }
2549
                        return $out;
2550
                        break;
2551
                case "glpi_contracts.name" :
2552
                        if (empty($linkfield)){
2553
                                $out="";
2554
                                $split=explode("$$$$",$data["ITEM_$num"]);
2555

    
2556
                                $count_display=0;
2557
                                for ($k=0;$k<count($split);$k++)
2558
                                        if (strlen(trim($split[$k]))>0){
2559
                                                if ($count_display) $out.= "<br>";
2560
                                                $count_display++;
2561
                                                $out.= $split[$k];
2562
                                        }
2563
                        } else {
2564
                                if ($type==CONTRACT_TYPE){
2565
                                        $out= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[$type]."?ID=".$data['ID']."\">";
2566
                                        $out.= $data["ITEM_$num"];
2567
                                        if ($CFG_GLPI["view_ID"]||empty($data["ITEM_$num"])) $out.= " (".$data["ID"].")";
2568
                                        $out.= "</a>";
2569
                                } else {
2570
                                        $out= $data["ITEM_$num"];
2571
                                }
2572
                        }
2573
                return $out;
2574
                case "glpi_contracts.num" :
2575
                        if (empty($linkfield)){
2576
                                $out="";
2577
                                $split=explode("$$$$",$data["ITEM_$num"]);
2578

    
2579
                                $count_display=0;
2580
                                for ($k=0;$k<count($split);$k++)
2581
                                        if (strlen(trim($split[$k]))>0){
2582
                                                if ($count_display) $out.= "<br>";
2583
                                                $count_display++;
2584
                                                $out.= $split[$k];
2585
                                        }
2586
                                return $out;
2587
                        } else {
2588
                                return $data["ITEM_$num"];
2589
                        }
2590
                break;
2591

    
2592
                case "glpi_contacts.completename":
2593
                                $out="";
2594
                                $split=explode("$$$$",$data["ITEM_$num"]);
2595

    
2596
                                $count_display=0;
2597
                                for ($k=0;$k<count($split);$k++)
2598
                                        if (strlen(trim($split[$k]))>0){
2599
                                                if ($count_display) $out.= "<br>";
2600
                                                $count_display++;
2601
                                                $out.= $split[$k];
2602
                                        }
2603
                                return $out;
2604
                        break;
2605
                case "glpi_enterprises.name" :
2606
                        if (empty($linkfield)){
2607
                                if ($type==CONTACT_TYPE){
2608
                                        $out="";
2609
                                        $split=explode("$$$$",$data["ITEM_$num"]);
2610
        
2611
                                        $count_display=0;
2612
                                        for ($k=0;$k<count($split);$k++)
2613
                                                if (strlen(trim($split[$k]))>0){
2614
                                                        if ($count_display) $out.= "<br>";
2615
                                                        $count_display++;
2616
                                                        $out.= $split[$k];
2617
                                                }
2618
                                        return $out;
2619

    
2620
                                } else {
2621
                                        $out= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[$type]."?ID=".$data['ID']."\">";
2622
                                        $out.= $data["ITEM_$num"];
2623
                                        if ($CFG_GLPI["view_ID"]||empty($data["ITEM_$num"])) $out.= " (".$data["ID"].")";
2624
                                        $out.= "</a>";
2625
                                        if (!empty($data["ITEM_".$num."_2"])){
2626
                                                $out.= "<a href='".formatOutputWebLink($data["ITEM_".$num."_2"])."' target='_blank'><img src='".$CFG_GLPI["root_doc"]."/pics/web.png' alt='website'></a>";
2627
                                        }
2628
                                }
2629
                        } else {
2630
                                $type=ENTERPRISE_TYPE;
2631
                                $out="";
2632
                                if ($data["ITEM_".$num."_3"]>0)
2633
                                        $out= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[$type]."?ID=".$data["ITEM_".$num."_3"]."\">";
2634
                                $out.= $data["ITEM_$num"];
2635
                                if ($data["ITEM_".$num."_3"]>0&&($CFG_GLPI["view_ID"]||(empty($data["ITEM_$num"])))) $out.= " (".$data["ITEM_".$num."_3"].")";
2636
                                if ($data["ITEM_".$num."_3"]>0)
2637
                                        $out.= "</a>";
2638
                                if (!empty($data["ITEM_".$num."_2"])){
2639
                                        $out.= "<a href='".formatOutputWebLink($data["ITEM_".$num."_2"])."' target='_blank'><img src='".$CFG_GLPI["root_doc"]."/pics/web.png' alt='website'></a>";
2640
                                }
2641
                        }
2642
                        return $out;
2643
                break;        
2644
                case "glpi_enterprises_infocoms.name" :
2645
                        $type=ENTERPRISE_TYPE;
2646
                        $out="";
2647
                        if (!empty($data["ITEM_".$num."_3"])){
2648
                                $out.= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[$type]."?ID=".$data["ITEM_".$num."_3"]."\">";
2649
                                $out.= $data["ITEM_$num"];
2650
                                if ($CFG_GLPI["view_ID"]||empty($data["ITEM_$num"])) 
2651
                                        $out.= " (".$data["ITEM_".$num."_3"].")";
2652
                                $out.= "</a>";
2653
                        }
2654
                        return $out;
2655
                break;
2656
                case "glpi_type_docs.icon" :
2657
                        if (!empty($data["ITEM_$num"])){
2658
                                return "<img class='middle' alt='' src='".$CFG_GLPI["typedoc_icon_dir"]."/".$data["ITEM_$num"]."'>";
2659
                        }
2660
                        else {
2661
                                return "&nbsp;";
2662
                        }
2663
                break;        
2664

    
2665
                case "glpi_docs.filename" :                
2666
                        return getDocumentLink($data["ITEM_$num"]);
2667
                break;                
2668
                case "glpi_docs.link" :
2669
                case "glpi_enterprises.website" :
2670
                        if (!empty($data["ITEM_$num"])){
2671
                                $link=$data["ITEM_$num"];
2672
                                if (strlen($data["ITEM_$num"])>30){
2673
                                        $link=utf8_substr($data["ITEM_$num"],0,30)."...";
2674
                                }
2675
                                return "<a href=\"".$data["ITEM_$num"]."\" target='_blank'>".$link."</a>";
2676
                        } else return "&nbsp;";
2677
                        break;        
2678
                case "glpi_enterprises.email" :
2679
                case "glpi_contacts.email" :
2680
                case "glpi_users.email" :
2681
                        if (!empty($data["ITEM_$num"])){
2682
                                return "<a href='mailto:".$data["ITEM_$num"]."'>".$data["ITEM_$num"]."</a>";
2683
                        } else {
2684
                                return "&nbsp;";
2685
                        }
2686
                        break;        
2687
                case "glpi_device_hdd.specif_default" :
2688
                case "glpi_device_ram.specif_default" :
2689
                case "glpi_device_processor.specif_default" :
2690
                        return $data["ITEM_".$num];
2691
                        break;
2692
                case "glpi_networking_ports.ifmac" :
2693
                        $out="";
2694
                        if ($type==COMPUTER_TYPE){
2695
                                $displayed=array();
2696
                                if (!empty($data["ITEM_".$num."_2"])){
2697
                                        $split=explode("$$$$",$data["ITEM_".$num."_2"]);
2698
                                        $count_display=0;
2699
                                        for ($k=0;$k<count($split);$k++){
2700
                                                $lowstr=strtolower($split[$k]);
2701
                                                if (strlen(trim($split[$k]))>0
2702
                                                        &&!in_array($lowstr,$displayed)){        
2703
                                                        if ($count_display) {
2704
                                                                $out.= "<br>";
2705
                                                        } 
2706
                                                        $count_display++;
2707
                                                        $out.= $split[$k];
2708
                                                        $displayed[]=$lowstr;
2709
                                                }
2710
                                        }
2711
                                        if (!empty($data["ITEM_".$num])) $out.= "<br>";
2712
                                }
2713

    
2714
                                if (!empty($data["ITEM_".$num])){
2715
                                        $split=explode("$$$$",$data["ITEM_".$num]);
2716
                                        $count_display=0;
2717
                                        for ($k=0;$k<count($split);$k++){
2718
                                                $lowstr=strtolower($split[$k]);
2719
                                                if (strlen(trim($split[$k]))>0 
2720
                                                        &&!in_array($lowstr,$displayed)){        
2721
                                                        if ($count_display) {
2722
                                                                $out.= "<br>";
2723
                                                        }
2724
                                                        $count_display++;
2725
                                                        $out.= $split[$k];
2726
                                                        $displayed[]=$lowstr;
2727
                                                }
2728
                                        }
2729
                                }
2730
                        } else {
2731
                                $split=explode("$$$$",$data["ITEM_".$num]);
2732
                                $count_display=0;
2733
                                for ($k=0;$k<count($split);$k++){
2734
                                        if (strlen(trim($split[$k]))>0){        
2735
                                                if ($count_display){
2736
                                                        $out.= "<br>";
2737
                                                }
2738
                                                $count_display++;
2739
                                                $out.= $split[$k];
2740
                                        }
2741
                                }
2742
                        }
2743
                        return $out;
2744
                        break;
2745
                case "glpi_contracts.duration":
2746
                case "glpi_contracts.notice":
2747
                case "glpi_contracts.periodicity":
2748
                case "glpi_contracts.facturation":
2749
                        if (!empty($data["ITEM_$num"])){
2750
                                return $data["ITEM_$num"]." ".$LANG["financial"][57];
2751
                        } else {
2752
                                return "&nbsp;";
2753
                        }
2754
                        break;
2755
                case "glpi_contracts.renewal":
2756
                        return getContractRenewalName($data["ITEM_$num"]);
2757
                        break;
2758
                case "glpi_ocs_link.last_update":
2759
                case "glpi_ocs_link.last_ocs_update":
2760
                case "glpi_computers.date_mod":
2761
                case "glpi_printers.date_mod":
2762
                case "glpi_networking.date_mod":
2763
                case "glpi_peripherals.date_mod":
2764
                case "glpi_phones.date_mod":
2765
                case "glpi_software.date_mod":
2766
                case "glpi_monitors.date_mod":
2767
                case "glpi_docs.date_mod":
2768
                case "glpi_ocs_config.date_mod" :
2769
                case "glpi_users.last_login":
2770
                case "glpi_users.date_mod":        
2771
                        return convDateTime($data["ITEM_$num"]);
2772
                        break;
2773
                case "glpi_infocoms.end_warranty":
2774
                case "glpi_contracts.end_date":
2775
                        if ($data["ITEM_$num"]!=''&&$data["ITEM_$num"]!="0000-00-00"){
2776
                                return getWarrantyExpir($data["ITEM_$num"],$data["ITEM_".$num."_2"]);
2777
                        } else {
2778
                                return "&nbsp;"; 
2779
                        }
2780
                break;
2781
                case "glpi_contracts.expire_notice": // ajout jmd
2782
                        if ($data["ITEM_$num"]!=''&&$data["ITEM_$num"]!="0000-00-00"){
2783
                                return getExpir($data["ITEM_$num"],$data["ITEM_".$num."_2"],$data["ITEM_".$num."_3"]);
2784
                        } else {
2785
                                return "&nbsp;"; 
2786
                        }
2787
                case "glpi_contracts.expire": // ajout jmd
2788
                        if ($data["ITEM_$num"]!=''&&$data["ITEM_$num"]!="0000-00-00"){
2789
                                return getExpir($data["ITEM_$num"],$data["ITEM_".$num."_2"]);
2790
                        } else {
2791
                                return "&nbsp;"; 
2792
                        }
2793
                case "glpi_contracts.begin_date":
2794
                case "glpi_infocoms.buy_date":
2795
                case "glpi_infocoms.use_date":
2796
                        return convDate($data["ITEM_$num"]);
2797
                        break;
2798
                case "glpi_infocoms.amort_time":
2799
                        if (!empty($data["ITEM_$num"])){
2800
                                return $data["ITEM_$num"]." ".$LANG["financial"][9];
2801
                        } else { 
2802
                                return "&nbsp;";
2803
                        }
2804
                        break;
2805
                case "glpi_infocoms.warranty_duration":
2806
                        if (!empty($data["ITEM_$num"])){
2807
                                return $data["ITEM_$num"]." ".$LANG["financial"][57];
2808
                        } else {
2809
                                return "&nbsp;";
2810
                        }
2811
                        break;
2812
                case "glpi_infocoms.amort_type":
2813
                        return getAmortTypeName($data["ITEM_$num"]);
2814
                        break;
2815
                case "glpi_infocoms.value":
2816
                case "glpi_infocoms.warranty_value":
2817
                        return formatNumber($data["ITEM_$num"]);
2818
                        break;
2819
                case "glpi_infocoms.alert":
2820
                        if ($data["ITEM_$num"]==pow(2,ALERT_END)){
2821
                                return $LANG["financial"][80];
2822
                        } 
2823
                        return "";
2824
                        break;
2825
                case "glpi_contracts.alert":
2826
                        switch ($data["ITEM_$num"]){
2827
                                case pow(2,ALERT_END);
2828
                                        return $LANG["buttons"][32];
2829
                                        break;
2830
                                case pow(2,ALERT_NOTICE);
2831
                                        return $LANG["financial"][10];
2832
                                        break;
2833
                                case pow(2,ALERT_END)+pow(2,ALERT_NOTICE);
2834
                                        return $LANG["buttons"][32]." + ".$LANG["financial"][10];
2835
                                        break;
2836
                        } 
2837
                        return "";
2838
                        break;
2839
                case "glpi_tracking.count":
2840
                        if ($data["ITEM_$num"]>0&&haveRight("show_all_ticket","1")){
2841
                                $out= "<a href=\"".$CFG_GLPI["root_doc"]."/front/tracking.php?reset=reset_before&status=all&type=$type&item=".$data['ID']."\">";
2842
                                $out.= $data["ITEM_$num"];
2843
                                $out.="</a>";
2844
                        } else {
2845
                                $out= $data["ITEM_$num"];
2846
                        }
2847
                        return $out;
2848
                        break;
2849
                case "glpi_auth_tables.name" :
2850
                        return getAuthMethodName($data["ITEM_".$num], $data["ITEM_".$num."_2"], 1,$data["ITEM_".$num."_3"].$data["ITEM_".$num."_4"]);
2851
                        break;
2852
                case "glpi_reservation_item.comments" :
2853
                        if (empty($data["ITEM_$num"])){
2854
                                return  "<a href='".$CFG_GLPI["root_doc"]."/front/reservation.php?comment=".$data["refID"]."' title='".$LANG["reservation"][22]."'>".$LANG["common"][49]."</a>";
2855
                        }else{
2856
                                return "<a href='".$CFG_GLPI["root_doc"]."/front/reservation.php?comment=".$data['refID']."' title='".$LANG["reservation"][22]."'>". resume_text($data["ITEM_$num"])."</a>";
2857
                        }
2858
                        break;
2859
                default:
2860

    
2861
                        // Link with plugin tables : need to know left join structure
2862
                        if ($type<=1000){
2863
                                if (preg_match("/^glpi_plugin_([a-zA-Z]+)/", $field, $matches) 
2864
                                || preg_match("/^glpi_dropdown_plugin_([a-zA-Z]+)/", $field, $matches) ){
2865
                                        if (count($matches)==2){
2866
                                                $plug=$matches[1];
2867
        
2868
                                                $function='plugin_'.$plug.'_giveItem';
2869
                                                if (function_exists($function)){
2870
                                                        $out=$function($type,$field,$data,$num,$linkfield);
2871
                                                        if (!empty($out)){
2872
                                                                return $out;
2873
                                                        }
2874
                                                } 
2875
                                        }
2876
                                } 
2877
                        }
2878
                        
2879
                        return $data["ITEM_$num"];
2880
                        break;
2881
        }
2882

    
2883
}
2884

    
2885

    
2886
/**
2887
 * Generic Function to get transcript table name
2888
 *
2889
 *
2890
 *@param $table reference table
2891
 *@param $device_type device type ID
2892
 *@param $meta_type meta table type ID
2893
 *
2894
 *@return Left join string
2895
 *
2896
 **/
2897
function translate_table($table,$device_type=0,$meta_type=0){
2898

    
2899
        $ADD="";
2900
        if ($meta_type) $ADD="_".$meta_type;
2901

    
2902
        switch ($table){
2903
                case "glpi_computer_device":
2904
                        if ($device_type==0)
2905
                                return $table.$ADD;
2906
                        else return "DEVICE_".$device_type.$ADD;
2907
                        break;
2908
                default :
2909
                        return $table.$ADD;
2910
                        break;
2911
        }
2912

    
2913
}
2914

    
2915

    
2916
/**
2917
 * Generic Function to add Default left join to a request
2918
 *
2919
 *
2920
 *@param $type reference ID
2921
 *@param $ref_table reference table
2922
 *@param $already_link_tables array of tables already joined
2923
 *
2924
 *@return Left join string
2925
 *
2926
 **/
2927
function addDefaultJoin ($type,$ref_table,&$already_link_tables){
2928

    
2929
        switch ($type){
2930
                // No link
2931
                case USER_TYPE:
2932
                        return addLeftJoin($type,$ref_table,$already_link_tables,"glpi_users_profiles","");
2933
                break;
2934
                default :
2935
                        return "";
2936
                break;
2937
        }
2938
}
2939

    
2940

    
2941

    
2942
/**
2943
 * Generic Function to add left join to a request
2944
 *
2945
 *
2946
 *@param $type reference ID
2947
 *@param $ref_table reference table
2948
 *@param $already_link_tables array of tables already joined
2949
 *@param $new_table new table to join
2950
 *@param $device_type device_type for search on computer device
2951
 *@param $meta is it a meta item ?
2952
 *@param $meta_type meta type table
2953
 *@param $linkfield linkfield for LeftJoin
2954
 *
2955
 *
2956
 *@return Left join string
2957
 *
2958
 **/
2959
function addLeftJoin ($type,$ref_table,&$already_link_tables,$new_table,$linkfield,$device_type=0,$meta=0,$meta_type=0){
2960

    
2961
        global $PLUGIN_HOOKS,$LANG;
2962

    
2963
        // Rename table for meta left join
2964
        $AS="";
2965
        $nt=$new_table;
2966

    
2967
        // Multiple link possibilies case
2968
        if ($new_table=="glpi_users"){
2969
                $AS = " AS ".$new_table."_".$linkfield;
2970
                $nt.="_".$linkfield;
2971
        }
2972

    
2973
        $addmetanum="";
2974
        $rt=$ref_table;
2975
        if ($meta) {
2976
                $AS= " AS ".$nt."_".$meta_type;
2977
                $nt=$nt."_".$meta_type;
2978
                //$rt.="_".$meta_type;
2979
        }
2980

    
2981
        // Auto link
2982
        if ($ref_table==$new_table) return "";
2983
        
2984
        if (in_array(translate_table($new_table,$device_type,$meta_type).".".$linkfield,$already_link_tables)) return "";
2985
        else array_push($already_link_tables,translate_table($new_table,$device_type,$meta_type).".".$linkfield);
2986

    
2987

    
2988
        // Plugin can override a left join definition  for its type
2989
        if ($type>1000){
2990
                if (isset($PLUGIN_HOOKS['plugin_types'][$type])){
2991
                        $function='plugin_'.$PLUGIN_HOOKS['plugin_types'][$type].'_addLeftJoin';
2992
                        if (function_exists($function)){
2993
                                $out=$function($type,$ref_table,$new_table,$linkfield,$already_link_tables);
2994
                                if (!empty($out)){
2995
                                        return $out;
2996
                                }
2997
                        } 
2998
                } 
2999
        }
3000

    
3001
        switch ($new_table){
3002
                // No link
3003
                case "glpi_auth_tables":
3004
                        return " LEFT JOIN glpi_auth_ldap ON (glpi_users.auth_method = ".AUTH_LDAP." AND glpi_users.id_auth = glpi_auth_ldap.ID) 
3005
                                LEFT JOIN glpi_auth_mail ON (glpi_users.auth_method = ".AUTH_MAIL." AND glpi_users.id_auth = glpi_auth_mail.ID) ";
3006
                break;
3007
                case "glpi_reservation_item":
3008
                        return "";
3009
                break;
3010
                case "glpi_entities_data":
3011
                        return " LEFT JOIN $new_table $AS ON ($rt.ID = $nt.FK_entities) ";
3012
                break;
3013
                case "glpi_ocs_link":
3014
                        return " LEFT JOIN $new_table $AS ON ($rt.ID = $nt.glpi_id) ";
3015
                break;
3016
                case "glpi_ocs_link":
3017
                        return " LEFT JOIN $new_table $AS ON ($rt.ID = $nt.glpi_id) ";
3018
                break;
3019
                case "glpi_registry":
3020
                        return " LEFT JOIN $new_table $AS ON ($rt.ID = $nt.computer_id) ";
3021
                break;
3022
                case "glpi_dropdown_os":
3023
                        if ($type==SOFTWARE_TYPE){
3024
                                return " LEFT JOIN $new_table $AS ON ($rt.platform = $nt.ID) ";
3025
                        } else  {
3026
                                return " LEFT JOIN $new_table $AS ON ($rt.os = $nt.ID) ";
3027
                        }
3028
                break;
3029
                case "glpi_networking_ports":
3030
                        $out="";
3031
                // Add networking device for computers
3032
                if ($type==COMPUTER_TYPE){
3033
                        $out=addLeftJoin($type,$ref_table,$already_link_tables,"glpi_computer_device",$linkfield,NETWORK_DEVICE,$meta,$meta_type);
3034
                }
3035
                return $out." LEFT JOIN $new_table $AS ON ($rt.ID = $nt.on_device AND $nt.device_type='$type') ";
3036
                break;
3037
                case "glpi_dropdown_netpoint":
3038
                        // Link to glpi_networking_ports before
3039
                        $out=addLeftJoin($type,$ref_table,$already_link_tables,"glpi_networking_ports",$linkfield);
3040
                return $out." LEFT JOIN $new_table $AS ON (glpi_networking_ports.netpoint = $nt.ID) ";
3041
                break;
3042
                case "glpi_tracking":
3043
                        return " LEFT JOIN $new_table $AS ON ($nt.device_type='$type' AND $rt.ID = $nt.computer) ";
3044
                break;
3045
                case "glpi_users":
3046
                        return " LEFT JOIN $new_table $AS ON ($rt.$linkfield = $nt.ID) ";
3047
                break;
3048
                case "glpi_enterprises":
3049
                        if ($type==CONTACT_TYPE){
3050
                                $out=addLeftJoin($type,$ref_table,$already_link_tables,"glpi_contact_enterprise","FK_contact");
3051
                                return $out." LEFT JOIN $new_table $AS ON (glpi_contact_enterprise.FK_enterprise = $nt.ID ".
3052
                                getEntitiesRestrictRequest("AND","glpi_enterprises",'','',true).") ";
3053
                        } else {
3054
                                return " LEFT JOIN $new_table $AS ON ($rt.FK_enterprise = $nt.ID) ";
3055
                        }
3056
                break;
3057
                case "glpi_contacts":
3058
                        $out=addLeftJoin($type,$ref_table,$already_link_tables,"glpi_contact_enterprise","FK_enterprise");
3059
                        return $out." LEFT JOIN $new_table $AS ON (glpi_contact_enterprise.FK_contact = $nt.ID ".
3060
                                getEntitiesRestrictRequest("AND","glpi_contacts",'','',true)." ) ";
3061
                break;
3062
                case "glpi_contact_enterprise":
3063
                        return " LEFT JOIN $new_table $AS ON ($rt.ID = $nt.$linkfield) ";
3064
                break;
3065
                case "glpi_dropdown_manufacturer":
3066
                        return " LEFT JOIN $new_table $AS ON ($rt.FK_glpi_enterprise = $nt.ID) ";
3067
                break;
3068

    
3069
                case "glpi_enterprises_infocoms":
3070
                        $out=addLeftJoin($type,$ref_table,$already_link_tables,"glpi_infocoms",$linkfield);
3071
                return $out." LEFT JOIN glpi_enterprises AS glpi_enterprises_infocoms ON (glpi_infocoms.FK_enterprise = $nt.ID) ";
3072
                break;
3073
                case "glpi_dropdown_budget":
3074
                        $out=addLeftJoin($type,$ref_table,$already_link_tables,"glpi_infocoms",$linkfield);
3075
                return $out." LEFT JOIN $new_table $AS ON (glpi_infocoms.budget = $nt.ID) ";
3076
                break;
3077
                case "glpi_infocoms":
3078
                        return " LEFT JOIN $new_table $AS ON ($rt.ID = $nt.FK_device AND $nt.device_type='$type') ";
3079
                break;
3080
                case "glpi_contract_device":
3081
                        return " LEFT JOIN $new_table $AS ON ($rt.ID = $nt.FK_device AND $nt.device_type='$type') ";
3082
                break;
3083
                case "glpi_users_profiles":
3084
                        return " LEFT JOIN $new_table $AS ON ($rt.ID = $nt.FK_users) ";
3085
                break;
3086

    
3087
                case "glpi_profiles":
3088
                        // Link to glpi_users_profiles before
3089
                        $out=addLeftJoin($type,$rt,$already_link_tables,"glpi_users_profiles",$linkfield);
3090
                        if ($type==USER_TYPE){
3091
                                $out.=addLeftJoin($type,"glpi_users_profiles",$already_link_tables,"glpi_complete_entities","FK_entities");
3092
                        }
3093
                return $out." LEFT JOIN $new_table $AS ON (glpi_users_profiles.FK_profiles = $nt.ID) ";
3094
                break;
3095
                case "glpi_entities":
3096
                        if ($type==USER_TYPE){
3097
                                $out=addLeftJoin($type,"glpi_users_profiles",$already_link_tables,"glpi_profiles","");
3098
                                $out.=addLeftJoin($type,"glpi_users_profiles",$already_link_tables,"glpi_complete_entities","FK_entities");
3099
                                return $out;
3100
                        } else {
3101
                                return " LEFT JOIN $new_table $AS ON ($rt.$linkfield = $nt.ID) ";
3102
                        }
3103
                break;
3104
                case "glpi_complete_entities":
3105
                        array_push($already_link_tables,translate_table("glpi_entities",$device_type,$meta_type).".".$linkfield);
3106

    
3107
                        if (empty($AS)){
3108
                                $AS = "AS glpi_entities";
3109
                        }
3110
                        return " LEFT JOIN ( SELECT * FROM glpi_entities UNION SELECT 0 AS ID, '".addslashes($LANG["entity"][2])."' AS name, -1 AS parentID, '".addslashes($LANG["entity"][2])."' AS completename, '' AS comments, -1 AS level) 
3111
                                $AS ON ($rt.$linkfield = glpi_entities.ID) ";
3112
                        break;
3113
                case "glpi_users_groups":
3114
                        return " LEFT JOIN $new_table $AS ON ($rt.ID = $nt.FK_users) ";
3115
                break;
3116

    
3117
                case "glpi_groups":
3118
                        if (empty($linkfield)){
3119
                                // Link to glpi_users_group before
3120
                                $out=addLeftJoin($type,$rt,$already_link_tables,"glpi_users_groups",$linkfield);
3121

    
3122
                                return $out." LEFT JOIN $new_table $AS ON (glpi_users_groups.FK_groups = $nt.ID) ";
3123
                        } else {
3124
                                return " LEFT JOIN $new_table $AS ON ($rt.$linkfield = $nt.ID) ";
3125
                        }
3126

    
3127
                break;
3128
                case "glpi_contracts":
3129
                        $out=addLeftJoin($type,$rt,$already_link_tables,"glpi_contract_device",$linkfield);
3130
                return $out." LEFT JOIN $new_table $AS ON (glpi_contract_device.FK_contract = $nt.ID) ";
3131
                break;
3132
                case "glpi_licenses":
3133
                        if (!$meta){
3134
                                return " LEFT JOIN $new_table $AS ON ($rt.ID = $nt.sID) ";
3135
                        } else {
3136
                                return "";
3137
                        }
3138
                break;        
3139
                case "glpi_computer_device":
3140
                        if ($device_type==0){
3141
                                return " LEFT JOIN $new_table $AS ON ($rt.ID = $nt.FK_computers ) ";
3142
                        } else {
3143
                                return " LEFT JOIN $new_table AS DEVICE_".$device_type." ON ($rt.ID = DEVICE_".$device_type.".FK_computers AND DEVICE_".$device_type.".device_type='$device_type') ";
3144
                        }
3145
                break;        
3146
                case "glpi_device_processor":
3147
                        $out=addLeftJoin($type,$ref_table,$already_link_tables,"glpi_computer_device",$linkfield,PROCESSOR_DEVICE,$meta,$meta_type);
3148
                        return $out." LEFT JOIN $new_table $AS ON (DEVICE_".PROCESSOR_DEVICE.".FK_device = $nt.ID) ";
3149
                break;                
3150
                case "glpi_device_ram":
3151
                        $out=addLeftJoin($type,$ref_table,$already_link_tables,"glpi_computer_device",$linkfield,RAM_DEVICE,$meta,$meta_type);
3152
                        return $out." LEFT JOIN $new_table $AS ON (DEVICE_".RAM_DEVICE.".FK_device = $nt.ID) ";
3153
                break;                
3154
                case "glpi_device_iface":
3155
                        $out=addLeftJoin($type,$ref_table,$already_link_tables,"glpi_computer_device",$linkfield,NETWORK_DEVICE,$meta,$meta_type);
3156
                        return $out." LEFT JOIN $new_table $AS ON (DEVICE_".NETWORK_DEVICE.".FK_device = $nt.ID) ";
3157
                break;        
3158
                case "glpi_device_sndcard":
3159
                        $out=addLeftJoin($type,$ref_table,$already_link_tables,"glpi_computer_device",$linkfield,SND_DEVICE,$meta,$meta_type);
3160
                        return $out." LEFT JOIN $new_table $AS ON (DEVICE_".SND_DEVICE.".FK_device = $nt.ID) ";
3161
                break;                
3162
                case "glpi_device_gfxcard":
3163
                        $out=addLeftJoin($type,$ref_table,$already_link_tables,"glpi_computer_device",$linkfield,GFX_DEVICE,$meta,$meta_type);
3164
                        return $out." LEFT JOIN $new_table $AS ON (DEVICE_".GFX_DEVICE.".FK_device = $nt.ID) ";
3165
                break;        
3166
                case "glpi_device_moboard":
3167
                        $out=addLeftJoin($type,$ref_table,$already_link_tables,"glpi_computer_device",$linkfield,MOBOARD_DEVICE,$meta,$meta_type);
3168
                        return $out." LEFT JOIN $new_table $AS ON (DEVICE_".MOBOARD_DEVICE.".FK_device = $nt.ID) ";
3169
                break;        
3170
                case "glpi_device_hdd":
3171
                        $out=addLeftJoin($type,$ref_table,$already_link_tables,"glpi_computer_device",$linkfield,HDD_DEVICE,$meta,$meta_type);
3172
                        return $out." LEFT JOIN $new_table $AS ON (DEVICE_".HDD_DEVICE.".FK_device = $nt.ID) ";
3173
                break;
3174
                default :
3175

    
3176
                        if ($type<=1000){
3177
                                if (preg_match("/^glpi_plugin_([a-zA-Z]+)/", $new_table, $matches) 
3178
                                || preg_match("/^glpi_dropdown_plugin_([a-zA-Z]+)/", $new_table, $matches) ){
3179
                                        if (count($matches)==2){
3180
                                                $plug=$matches[1];
3181
                                                $function='plugin_'.$plug.'_addLeftJoin';
3182
                                                if (function_exists($function)){
3183
                                                        $out=$function($type,$ref_table,$new_table,$linkfield,$already_link_tables);
3184
                                                        if (!empty($out)){
3185
                                                                return $out;
3186
                                                        }
3187
                                                } 
3188
                                        }
3189
                                } 
3190
                        }
3191

    
3192
                        if (!empty($linkfield)){
3193
                                return " LEFT JOIN $new_table $AS ON ($rt.$linkfield = $nt.ID) ";
3194
                        } else {
3195
                                return "";
3196
                        }
3197

    
3198
                        break;
3199
        }
3200
}
3201

    
3202

    
3203
/**
3204
 * Generic Function to add left join for meta items
3205
 *
3206
 *
3207
 *@param $from_type reference item type ID 
3208
 *@param $to_type item type to add
3209
 *@param $already_link_tables2 array of tables already joined
3210
 *@param $nullornott Used LEFT JOIN (null generation) or INNER JOIN for strict join
3211
 *
3212
 *
3213
 *@return Meta Left join string
3214
 *
3215
 **/
3216
function addMetaLeftJoin($from_type,$to_type,&$already_link_tables2,$nullornott){
3217
        global $LINK_ID_TABLE;
3218

    
3219
        $LINK=" INNER JOIN ";
3220
        if ($nullornott)
3221
                $LINK=" LEFT JOIN ";
3222

    
3223
        switch ($from_type){
3224
                case COMPUTER_TYPE :
3225
                        switch ($to_type){
3226
                                /*                                case NETWORKING_TYPE :
3227
                                                                array_push($already_link_tables2,$LINK_ID_TABLE[NETWORKING_TYPE]."_$to_type");
3228
                                                                return " $LINK glpi_networking_ports as ports ON (glpi_computers.ID = ports.on_device AND ports.device_type='".COMPUTER_TYPE."') ".
3229
                                                                " $LINK glpi_networking_wire as wire1 ON (ports.ID = wire1.end1) ".
3230
                                                                " $LINK glpi_networking_ports as ports21 ON (ports21.device_type='".NETWORKING_TYPE."' AND wire1.end2 = ports21.ID ) ".
3231
                                                                " $LINK glpi_networking_wire as wire2 ON (ports.ID = wire2.end2) ".
3232
                                                                " $LINK glpi_networking_ports as ports22 ON (ports22.device_type='".NETWORKING_TYPE."' AND wire2.end1 = ports22.ID ) ".
3233
                                                                " $LINK glpi_networking$to_type ON (glpi_networking$to_type.ID = ports22.on_device OR glpi_networking.ID = ports21.on_device)";
3234
                                                                break;
3235
                                 */                                
3236
                                case PRINTER_TYPE :
3237
                                        array_push($already_link_tables2,$LINK_ID_TABLE[PRINTER_TYPE]);
3238
                                        return " $LINK glpi_connect_wire AS conn_print_$to_type ON (conn_print_$to_type.end2=glpi_computers.ID  AND conn_print_$to_type.type='".PRINTER_TYPE."') ".
3239
                                                " $LINK glpi_printers ON (conn_print_$to_type.end1=glpi_printers.ID) ";
3240
                                        break;                                
3241
                                case MONITOR_TYPE :
3242
                                        array_push($already_link_tables2,$LINK_ID_TABLE[MONITOR_TYPE]);
3243
                                        return " $LINK glpi_connect_wire AS conn_mon_$to_type ON (conn_mon_$to_type.end2=glpi_computers.ID  AND conn_mon_$to_type.type='".MONITOR_TYPE."') ".
3244
                                                " $LINK glpi_monitors ON (conn_mon_$to_type.end1=glpi_monitors.ID) ";
3245
                                        break;                                
3246
                                case PERIPHERAL_TYPE :
3247
                                        array_push($already_link_tables2,$LINK_ID_TABLE[PERIPHERAL_TYPE]);
3248
                                        return " $LINK glpi_connect_wire AS conn_periph_$to_type ON (conn_periph_$to_type.end2=glpi_computers.ID  AND conn_periph_$to_type.type='".PERIPHERAL_TYPE."') ".
3249
                                                " $LINK glpi_peripherals ON (conn_periph_$to_type.end1=glpi_peripherals.ID) ";
3250
                                        break;                                
3251
                                case PHONE_TYPE :
3252
                                        array_push($already_link_tables2,$LINK_ID_TABLE[PHONE_TYPE]);
3253
                                        return " $LINK glpi_connect_wire AS conn_phones_$to_type ON (conn_phones_$to_type.end2=glpi_computers.ID  AND conn_phones_$to_type.type='".PHONE_TYPE."') ".
3254
                                                " $LINK glpi_phones ON (conn_phones_$to_type.end1=glpi_phones.ID) ";
3255
                                        break;                        
3256

    
3257
                                case SOFTWARE_TYPE :
3258
                                        array_push($already_link_tables2,$LINK_ID_TABLE[SOFTWARE_TYPE]);
3259
                                        return " $LINK glpi_inst_software as inst_$to_type ON (inst_$to_type.cID = glpi_computers.ID) ".
3260
                                                " $LINK glpi_licenses as glpi_licenses_$to_type ON ( inst_$to_type.license=glpi_licenses_$to_type.ID ) ".
3261
                                                " $LINK glpi_software ON (glpi_licenses_$to_type.sID = glpi_software.ID)"; 
3262
                                        break;
3263
                        }
3264
                        break;
3265
                case MONITOR_TYPE :
3266
                        switch ($to_type){
3267
                                case COMPUTER_TYPE :
3268
                                        array_push($already_link_tables2,$LINK_ID_TABLE[COMPUTER_TYPE]);
3269
                                        return " $LINK glpi_connect_wire AS conn_mon_$to_type ON (conn_mon_$to_type.end1=glpi_monitors.ID  AND conn_mon_$to_type.type='".MONITOR_TYPE."') ".
3270
                                                " $LINK glpi_computers ON (conn_mon_$to_type.end2=glpi_computers.ID) ";
3271

    
3272
                                        break;
3273
                        }
3274
                        break;                
3275
                case PRINTER_TYPE :
3276
                        switch ($to_type){
3277
                                case COMPUTER_TYPE :
3278
                                        array_push($already_link_tables2,$LINK_ID_TABLE[COMPUTER_TYPE]);
3279
                                        return " $LINK glpi_connect_wire AS conn_mon_$to_type ON (conn_mon_$to_type.end1=glpi_printers.ID  AND conn_mon_$to_type.type='".PRINTER_TYPE."') ".
3280
                                                " $LINK glpi_computers ON (conn_mon_$to_type.end2=glpi_computers.ID) ";
3281

    
3282
                                        break;
3283
                        }
3284
                        break;                
3285
                case PERIPHERAL_TYPE :
3286
                        switch ($to_type){
3287
                                case COMPUTER_TYPE :
3288
                                        array_push($already_link_tables2,$LINK_ID_TABLE[COMPUTER_TYPE]);
3289
                                        return " $LINK glpi_connect_wire AS conn_mon_$to_type ON (conn_mon_$to_type.end1=glpi_peripherals.ID  AND conn_mon_$to_type.type='".PERIPHERAL_TYPE."') ".
3290
                                                " $LINK glpi_computers ON (conn_mon_$to_type.end2=glpi_computers.ID) ";
3291

    
3292
                                        break;
3293
                        }
3294
                        break;                
3295
                case PHONE_TYPE :
3296
                        switch ($to_type){
3297
                                case COMPUTER_TYPE :
3298
                                        array_push($already_link_tables2,$LINK_ID_TABLE[COMPUTER_TYPE]);
3299
                                        return " $LINK glpi_connect_wire AS conn_mon_$to_type ON (conn_mon_$to_type.end1=glpi_phones.ID  AND conn_mon_$to_type.type='".PHONE_TYPE."') ".
3300
                                                " $LINK glpi_computers ON (conn_mon_$to_type.end2=glpi_computers.ID) ";
3301

    
3302
                                        break;
3303
                        }
3304
                        break;
3305
                case SOFTWARE_TYPE :
3306
                        switch ($to_type){
3307
                                case COMPUTER_TYPE :
3308
                                        array_push($already_link_tables2,$LINK_ID_TABLE[COMPUTER_TYPE]);
3309
                                        return " $LINK glpi_licenses as glpi_licenses_$to_type ON ( glpi_licenses_$to_type.sID = glpi_software.ID ) ".
3310
                                                " $LINK glpi_inst_software as inst_$to_type ON (inst_$to_type.license = glpi_licenses_$to_type.ID) ".
3311
                                                " $LINK glpi_computers ON (inst_$to_type.cID = glpi_computers.ID)";
3312

    
3313
                                        break;
3314
                        }
3315
                        break;                
3316

    
3317

    
3318
        }
3319

    
3320
}
3321

    
3322

    
3323
/**
3324
 * Convert an array to be add in url
3325
 *
3326
 *
3327
 * @param $name name of array
3328
 * @param $array array to be added
3329
 * @return string to add
3330
 *
3331
 */
3332
function getMultiSearchItemForLink($name,$array){
3333
        $out="";
3334
        
3335
        if (is_array($array)&&count($array)>0){
3336
                foreach($array as $key => $val){
3337
                        //                if ($name!="link"||$key!=0)
3338
                        $out.="&amp;".$name."[$key]=".$val;
3339
                }
3340
        }
3341
        return $out;
3342

    
3343
}
3344
/**
3345
 * Is the search item related to infocoms
3346
 *
3347
 *
3348
 * @param $device_type item type
3349
 * @param $searchID ID of the element in $SEARCH_OPTION
3350
 * @return boolean
3351
 *
3352
 */
3353
function isInfocomSearch($device_type,$searchID){
3354
        global $CFG_GLPI;
3355
        return (($searchID>=25&&$searchID<=28)
3356
        ||($searchID>=37&&$searchID<=38)
3357
        ||($searchID>=50&&$searchID<=59)
3358
        ||($searchID>=120&&$searchID<=122))&&in_array($device_type,$CFG_GLPI["infocom_types"]);
3359
}
3360

    
3361
?>
Redmine Appliance - Powered by TurnKey Linux