Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

ryxeo-glpi-git / inc / commondbtm.class.php @ b67d8923

Historique | Voir | Annoter | Télécharger (26,3 ko)

1
<?php
2
/*
3
 * @version $Id: commondbtm.class.php 7885 2009-01-23 19:28:11Z remi $
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
/// Common DataBase Table Manager Class
36
class CommonDBTM {
37

    
38
        /// Data of the Item
39
        var $fields        = array();
40
        /// Table name
41
        var $table="";
42
        /// GLPI Item type
43
        var $type=-1;
44
        /// Make an history of the changes
45
        var $dohistory=false;
46
        /// Is an item specific to entity
47
        var $entity_assign=false;
48
        /// Is an item that can be recursivly assign to an entity
49
        var $may_be_recursive=false;
50
        /// Is an item that can be private or assign to an entity
51
        var $may_be_private=false;
52
        /// Black list fields for date_mod updates
53
        var $date_mod_blacklist        = array();
54

    
55
        /// set false to desactivate automatic message on action
56
        var $auto_message_on_action=true;
57

    
58
        /**
59
         * Constructor
60
         **/
61
        function CommonDBTM () {
62

    
63
        }
64

    
65
        /**
66
         * Clean cache used by the item $ID
67
         *
68
         *@param $ID ID of the item
69
         *@return nothing
70
         *
71
         **/
72
        function cleanCache($ID){
73
                global $CFG_GLPI;
74
                cleanAllItemCache($ID,"GLPI_".$this->type);
75
                cleanAllItemCache("comments_".$ID,"GLPI_".$this->type);
76
                $CFG_GLPI["cache"]->remove("data_".$ID,"GLPI_".$this->table,true);
77
                cleanRelationCache($this->table);
78
        }
79

    
80
        /**
81
         * Retrieve an item from the database
82
         *
83
         *@param $ID ID of the item to get
84
         *@return true if succeed else false
85
         *@todo Specific ones : Reservation Item
86
         *
87
        **/
88
        function getFromDB ($ID) {
89

    
90
                // Make new database object and fill variables
91
                global $DB,$CFG_GLPI;
92
                // != 0 because 0 is consider as empty
93
                if (strlen($ID)==0) return false;
94

    
95
                $query = "SELECT * FROM `".$this->table."` WHERE (`".$this->getIndexName()."` = '".$ID."')";
96

    
97
                if ($result = $DB->query($query)) {
98
                        if ($DB->numrows($result)==1){
99
                                $this->fields = $DB->fetch_assoc($result);
100
                                return true;
101
                        } 
102
                }
103
                return false;;
104

    
105
        }
106
        /**
107
         * Get the name of the index field
108
         *
109
         *@return name of the index field
110
         *
111
         **/
112
        function getIndexName(){
113
                return "ID";
114
        }
115
        /**
116
         * Get an empty item
117
         *
118
         *@return true if succeed else false
119
         *
120
         **/
121
        function getEmpty () {
122
                //make an empty database object
123
                global $DB;
124
                if ($fields = $DB->list_fields($this->table)){
125
                        foreach ($fields as $key => $val){
126
                                $this->fields[$key] = "";
127
                        }
128
                } else {
129
                        return false;
130
                }
131
                if (isset($this->fields['FK_entities'])&&isset($_SESSION["glpiactive_entity"])){
132
                        $this->fields['FK_entities']=$_SESSION["glpiactive_entity"];
133
                }
134
                $this->post_getEmpty();
135
                return true;
136
        }
137
        /**
138
         * Actions done at the end of the getEmpty function
139
         *
140
         *@return nothing
141
         *
142
         **/
143
        function post_getEmpty () {
144
        }
145
        /**
146
         * Update the item in the database
147
         * 
148
         *  @param $updates fields to update
149
          *  @param $oldvalues old values of the updated fields
150
         *@return nothing
151
         *
152
         **/
153
        function updateInDB($updates,$oldvalues=array())  {
154

    
155
                global $DB,$CFG_GLPI;
156

    
157
                for ($i=0; $i < count($updates); $i++) {
158
                        $query  = "UPDATE `".$this->table."` SET `";
159
                        $query .= $updates[$i]."`";
160

    
161
                        if ($this->fields[$updates[$i]]=="NULL"){
162
                                $query .= " = ";
163
                                $query .= $this->fields[$updates[$i]];
164
                        } else {
165
                                $query .= " = '";
166
                                $query .= $this->fields[$updates[$i]]."'";
167
                        }
168
                        $query .= " WHERE ID ='";
169
                        $query .= $this->fields["ID"];        
170
                        $query .= "'";
171
                        if (!$DB->query($query)){
172
                                if (isset($oldvalues[$updates[$i]])){
173
                                        unset($oldvalues[$updates[$i]]);
174
                                }
175
                        }
176
                }
177

    
178
                if(count($oldvalues)){
179
                        constructHistory($this->fields["ID"],$this->type,$oldvalues,$this->fields);
180
                }
181

    
182
                
183
                $this->cleanCache($this->fields["ID"]);
184
                return true;
185
        }
186

    
187
        /**
188
         * Add an item to the database
189
         *
190
         *@return new ID of the item is insert successfull else false
191
         *
192
         **/
193
        function addToDB() {
194

    
195
                global $DB;
196
                //unset($this->fields["ID"]);
197
                $nb_fields=count($this->fields);
198
                if ($nb_fields>0){                
199

    
200
                        // Build query
201
                        $query = "INSERT INTO `".$this->table."` (";
202
                        $i=0;
203
                        foreach ($this->fields as $key => $val) {
204
                                $fields[$i] = $key;
205
                                $values[$i] = $val;
206
                                $i++;
207
                        }                
208

    
209
                        for ($i=0; $i < $nb_fields; $i++) {
210
                                $query .= "`".$fields[$i]."`";
211
                                if ($i!=$nb_fields-1) {
212
                                        $query .= ",";
213
                                }
214
                        }
215
                        $query .= ") VALUES (";
216
                        for ($i=0; $i < $nb_fields; $i++) {
217
                                $query .= "'".$values[$i]."'";
218
                                if ($i!=$nb_fields-1) {
219
                                        $query .= ",";
220
                                }
221
                        }
222
                        $query .= ")";
223
                        if ($result=$DB->query($query)) {
224
                                $this->fields["ID"]=$DB->insert_id();
225
                                cleanRelationCache($this->table);
226
                                return $this->fields["ID"];
227
                        } else {
228
                                return false;
229
                        }
230
                } else return false;
231
        }
232

    
233
        /**
234
         * Restore item = set deleted flag to 0
235
         *
236
         *@param $ID ID of the item
237
         *
238
         *
239
         *@return true if succeed else false
240
         *
241
         **/
242
        function restoreInDB($ID) {
243
                global $DB,$CFG_GLPI;
244
                if (in_array($this->table,$CFG_GLPI["deleted_tables"])){
245
                        $query = "UPDATE `".$this->table."` SET deleted='0' WHERE (ID = '$ID')";
246
                        if ($result = $DB->query($query)) {
247
                                return true;
248
                        } else {
249
                                return false;
250
                        }
251
                } else {
252
                        return false;
253
                }
254
        }
255
        /**
256
         * Mark deleted or purge an item in the database
257
         *
258
         *@param $ID ID of the item
259
         *@param $force force the purge of the item (not used if the table do not have a deleted field)
260
         *
261
         *@return true if succeed else false
262
         *
263
         **/
264
        function deleteFromDB($ID,$force=0) {
265

    
266
                global $DB,$CFG_GLPI;
267

    
268
                if ($force==1||!in_array($this->table,$CFG_GLPI["deleted_tables"])){
269

    
270
                        $this->cleanDBonPurge($ID);
271

    
272
                        $this->cleanHistory($ID);
273

    
274
                        $this->cleanRelationData($ID);
275

    
276
                        $query = "DELETE from `".$this->table."` WHERE ID = '$ID'";
277

    
278
                        if ($result = $DB->query($query)) {
279
                                $this->post_deleteFromDB($ID);
280
                                $this->cleanCache($ID);
281
                                return true;
282
                        } else {
283
                                return false;
284
                        }
285
                }else {
286
                        $query = "UPDATE `".$this->table."` SET deleted='1' WHERE ID = '$ID'";                
287
                        $this->cleanDBonMarkDeleted($ID);
288

    
289
                        if ($result = $DB->query($query)){
290
                                $this->cleanCache($ID);
291
                                return true;
292
                        } else {
293
                                return false;
294
                        }
295
                }
296
        }
297

    
298
        /**
299
         * Clean data in the tables which have linked the deleted item
300
         *
301
         *@param $ID ID of the item
302
         *
303
         *
304
         *@return nothing
305
         *
306
         **/
307
        function cleanHistory($ID){
308
                global $DB;
309
                if ($this->dohistory){
310
                        $query = "DELETE FROM glpi_history WHERE ( device_type = '".$this->type."' AND FK_glpi_device = '$ID')";
311
                        $DB->query($query);
312
                }
313
        }
314

    
315
        /**
316
         * Clean data in the tables which have linked the deleted item
317
         *
318
         *@param $ID ID of the item
319
         *
320
         *
321
         *@return nothing
322
         *
323
         **/
324
        function cleanRelationData($ID){
325
                global $DB;
326
                $RELATION=getDbRelations();
327
                if (isset($RELATION[$this->table])){
328
                        foreach ($RELATION[$this->table] as $tablename => $field){
329
                                if ($tablename[0]!='_'){
330
                                        if (!is_array($field)){
331
                                                $query="UPDATE `$tablename` SET `$field` = 0 WHERE `$field`='$ID' ";
332
                                                $DB->query($query);
333
                                        } else {
334
                                                foreach ($field as $f){
335
                                                        $query="UPDATE `$tablename` SET `$f` = 0 WHERE `$f`='$ID' ";
336
                                                        $DB->query($query);
337
                                                }
338
                                        }
339
                                }
340
                        }
341
                }
342
        }
343
        /**
344
         * Actions done after the DELETE of the item in the database
345
         *
346
         *@param $ID ID of the item
347
         *
348
         *@return nothing
349
         *
350
         **/
351
        function post_deleteFromDB($ID){
352
        }
353

    
354
        /**
355
         * Actions done when item is deleted from the database
356
         *
357
         *@param $ID ID of the item
358
         *
359
         *@return nothing
360
         **/
361
        function cleanDBonPurge($ID) {
362
        }
363
        /**
364
         * Actions done when item flag deleted is set to an item
365
         *
366
         *@param $ID ID of the item
367
         *
368
         *@return nothing
369
         *
370
         **/
371
        function cleanDBonMarkDeleted($ID) {
372
        }
373
        // Common functions
374

    
375
        /**
376
         * Add an item in the database with all it's items.
377
         *
378
         *@param $input array : the _POST vars returned bye the item form when press add
379
         *
380
         *@return integer the new ID of the added item
381
         *@todo specific ones : reservationresa , planningtracking
382
         * 
383
        **/
384
        function add($input) {
385
                global $DB;
386
                
387
                $addMessAfterRedirect = false;
388

    
389
                if ($DB->isSlave()) {
390
                        return false;
391
                }
392
                        
393
                $input['_item_type_']=$this->type;
394
                $input=doHookFunction("pre_item_add",$input);
395

    
396
                if (isset($input['add'])){
397
                        $input['_add']=$input['add'];
398
                        unset($input['add']);
399
                }
400
                $input=$this->prepareInputForAdd($input);
401

    
402
                if ($input&&is_array($input)){
403
                        $this->fields=array();
404
                        $table_fields=$DB->list_fields($this->table);
405

    
406
                        // fill array for udpate
407
                        foreach ($input as $key => $val) {
408
                                if ($key[0]!='_'&& isset($table_fields[$key])&&(!isset($this->fields[$key]) || $this->fields[$key] != $input[$key])) {
409
                                        $this->fields[$key] = $input[$key];
410
                                }
411
                        }
412
                        // Auto set date_mod if exsist
413
                        if (isset($table_fields['date_mod'])){
414
                                $this->fields['date_mod']=$_SESSION["glpi_currenttime"];
415
                        }
416

    
417
                        if ($newID= $this->addToDB()){
418
                                $this->addMessageOnAddAction($input);
419
                                $this->post_addItem($newID,$input);
420
                                doHook("item_add",array("type"=>$this->type, "ID" => $newID));
421
                                return $newID;
422
                        } else {
423
                                return false;
424
                        }
425

    
426
                } else {
427
                        return false;
428
                }        
429
        }
430

    
431
        /**
432
         * Add a message on add action
433
         *
434
         *@param $input array : the _POST vars returned bye the item form when press add
435
         *
436
        **/
437
        function addMessageOnAddAction($input){
438
                global $INFOFORM_PAGES, $CFG_GLPI, $LANG;
439

    
440
                if (!isset($INFOFORM_PAGES[$this->type])){
441
                        return;
442
                }
443

    
444
                $addMessAfterRedirect=false;
445
                if (isset($input['_add'])){
446
                        $addMessAfterRedirect=true;
447
                }
448
                if (isset($input['_no_message']) || !$this->auto_message_on_action){
449
                        $addMessAfterRedirect=false;
450
                }
451

    
452
                if ($addMessAfterRedirect) {
453
                        addMessageAfterRedirect($LANG["common"][70] . 
454
                        ": <a href='" . $CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[$this->type] . "?ID=" . $this->fields['ID'] . (isset($input['is_template'])?"&amp;withtemplate=1":"")."'>" .
455
                        (isset($this->fields["name"]) && !empty($this->fields["name"]) ? stripslashes($this->fields["name"]) : "(".$this->fields['ID'].")") . "</a>");
456
                } 
457
        }
458

    
459
        /**
460
         * Prepare input datas for adding the item
461
         *
462
         *@param $input datas used to add the item
463
         *
464
         *@return the modified $input array
465
         *
466
         **/
467
        function prepareInputForAdd($input) {
468
                return $input;
469
        }
470
        /**
471
         * Actions done after the ADD of the item in the database
472
         * 
473
         *@param $newID ID of the new item 
474
         *@param $input datas used to add the item
475
         *
476
         * @return nothing 
477
         * 
478
        **/
479
        function post_addItem($newID,$input) {
480
        }
481

    
482

    
483
        /**
484
         * Update some elements of an item in the database.
485
         *
486
         *@param $input array : the _POST vars returned bye the item form when press update
487
         *@param $history boolean : do history log ?
488
         *
489
         *
490
         *@return Nothing (call to the class member)
491
         *@todo specific ones : reservationresa, planningtracking
492
         *
493
        **/
494
    function update($input,$history=1) {
495
                global $DB;
496
                if ($DB->isSlave())
497
                        return false;
498

    
499
                $input['_item_type_']=$this->type;
500
                $input=doHookFunction("pre_item_update",$input);
501

    
502
                $input=$this->prepareInputForUpdate($input);
503

    
504

    
505
                if (isset($input['update'])){
506
                        $input['_update']=$input['update'];
507
                        unset($input['update']);
508
                }
509

    
510
                if ($this->getFromDB($input[$this->getIndexName()])){
511

    
512
                        // Fill the update-array with changes
513
                        $x=0;
514
                        $updates=array();
515
                        $oldvalues=array();
516
                        foreach ($input as $key => $val) {
517
                                if (array_key_exists($key,$this->fields)){
518
                                        // Secu for null values on history
519
                                        // TODO : Int with NULL default value in DB -> default value 0
520
/*                                        if (is_null($this->fields[$key])){
521
                                                if (is_int($input[$key])||$input[$key]=='0')         $this->fields[$key]=0;
522
                                        }
523
*/
524
                                        if ($this->fields[$key] != stripslashes($input[$key])) {
525
                                                if ($key!="ID"){
526
                                                        // Do logs
527
                                                        if ($this->dohistory&&$history){
528
                                                                $oldvalues[$key]=$this->fields[$key];
529
                                                        }
530
                                                        $this->fields[$key] = $input[$key];
531
                                                        $updates[$x] = $key;
532
                                                        $x++;
533
                                                }
534
                                        }
535
                                }
536
                        }
537

    
538
                        if(count($updates)){
539
                                if (isset($this->fields['date_mod'])){
540
                                        // is a non blacklist field exists
541
                                        if (count(array_diff($updates,$this->date_mod_blacklist)) > 0){
542
                                                $this->fields['date_mod']=$_SESSION["glpi_currenttime"];
543
                                                $updates[$x++] = 'date_mod';
544
                                        }
545
                                }
546

    
547
                                list($input,$updates)=$this->pre_updateInDB($input,$updates);
548
                                
549

    
550
                                if ($this->updateInDB($updates,$oldvalues)){
551
                                        $this->addMessageOnUpdateAction($input);
552
                                        doHook("item_update",array("type"=>$this->type, "ID" => $input["ID"]));
553
                                }
554
                        } 
555
                        $this->post_updateItem($input,$updates,$history);
556
                }
557
        }
558

    
559
        /**
560
         * Add a message on update action
561
         *
562
         *@param $input array : the _POST vars returned bye the item form when press add
563
         *
564
        **/
565
        function addMessageOnUpdateAction($input){
566
                global $INFOFORM_PAGES, $CFG_GLPI, $LANG;
567

    
568
                if (!isset($INFOFORM_PAGES[$this->type])){
569
                        return;
570
                }
571

    
572
                $addMessAfterRedirect=false;
573
                if (isset($input['_update'])){
574
                        $addMessAfterRedirect=true;
575
                }
576
                if (isset($input['_no_message']) || !$this->auto_message_on_action){
577
                        $addMessAfterRedirect=false;
578
                }
579

    
580
                if ($addMessAfterRedirect) {
581
                        addMessageAfterRedirect($LANG["common"][71].": ".(isset($this->fields["name"]) && !empty($this->fields["name"]) ? stripslashes($this->fields["name"]) : "(".$this->fields['ID'].")"));
582
                } 
583
        }
584

    
585
        /**
586
         * Prepare input datas for updating the item
587
         *
588
         *@param $input datas used to update the item
589
         * 
590
         *@return the modified $input array 
591
         * 
592
        **/
593
        function prepareInputForUpdate($input) {
594
                return $input;
595
        }
596

    
597
        /**
598
         * Actions done after the UPDATE of the item in the database
599
         *
600
         *@param $input datas used to update the item
601
         *@param $updates array of the updated fields 
602
         *@param $history store changes history ? 
603
         * 
604
         *@return nothing 
605
         * 
606
        **/
607
        function post_updateItem($input,$updates,$history=1) {
608
        }
609

    
610
        /**
611
         * Actions done before the UPDATE of the item in the database
612
         *
613
         *@param $input datas used to update the item
614
         *@param $updates array of the updated fields
615
         * 
616
         *@return nothing
617
         * 
618
        **/
619
        function pre_updateInDB($input,$updates) {
620
                return array($input,$updates);
621
        }
622

    
623
        /**
624
         * Delete an item in the database.
625
         *
626
         *@param $input array : the _POST vars returned bye the item form when press delete
627
         *@param $force boolean : force deletion
628
         *@param $history boolean : do history log ?
629
         *
630
         *
631
         *@return Nothing ()
632
         *
633
         **/
634
        function delete($input,$force=0,$history=1) {
635
                global $DB;
636
                
637
                if ($DB->isSlave())
638
                        return false;
639

    
640
                $input['_item_type_']=$this->type;
641
                if ($force){
642
                        $input=doHookFunction("pre_item_purge",$input);
643
                        if (isset($input['purge'])){
644
                                $input['_purge']=$input['purge'];
645
                                unset($input['purge']);
646
                        }
647
                } else {
648
                        $input=doHookFunction("pre_item_delete",$input);
649
                        if (isset($input['delete'])){
650
                                $input['_delete']=$input['delete'];
651
                                unset($input['delete']);
652
                        }
653
                }
654

    
655
                if ($this->getFromDB($input[$this->getIndexName()])){
656
                        if ($this->pre_deleteItem($this->fields["ID"])){
657
                                if ($this->deleteFromDB($this->fields["ID"],$force)){
658
                                        if ($force){
659
                                                $this->addMessageOnPurgeAction($input);
660
                                                doHook("item_purge",array("type"=>$this->type, "ID" => $this->fields["ID"]));
661
                                        } else {
662
                                                $this->addMessageOnDeleteAction($input);
663

    
664
                                                if ($this->dohistory&&$history){
665
                                                        $changes[0] = 0;
666
                                                        $changes[1] = $changes[2] = "";
667
                                
668
                                                        historyLog ($this->fields["ID"],$this->type,$changes,0,HISTORY_DELETE_ITEM);
669
                                                }
670
                                                doHook("item_delete",array("type"=>$this->type, "ID" => $this->fields["ID"]));
671
                                        }
672
                                }
673
                                return true;
674
                        } else {
675
                                return false;
676
                        }
677
                } else {
678
                        return false;
679
                }
680

    
681
        }
682

    
683
        /**
684
         * Add a message on delete action
685
         *
686
         *@param $input array : the _POST vars returned bye the item form when press add
687
         *
688
        **/
689
        function addMessageOnDeleteAction($input){
690
                global $INFOFORM_PAGES, $CFG_GLPI, $LANG;
691

    
692
                if (!isset($INFOFORM_PAGES[$this->type])){
693
                        return;
694
                }
695

    
696
                if (!in_array($this->table,$CFG_GLPI["deleted_tables"])){
697
                        return;
698
                }
699

    
700
                $addMessAfterRedirect=false;
701
                if (isset($input['_delete'])){
702
                        $addMessAfterRedirect=true;
703
                }
704
                if (isset($input['_no_message']) || !$this->auto_message_on_action){
705
                        $addMessAfterRedirect=false;
706
                }
707

    
708
                if ($addMessAfterRedirect) {
709
                        addMessageAfterRedirect($LANG["common"][72] . 
710
                        ": <a href='" . $CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[$this->type] . "?ID=" . $this->fields['ID'] . "'>" .
711
                        (isset($this->fields["name"]) && !empty($this->fields["name"]) ? stripslashes($this->fields["name"]) : "(".$this->fields['ID'].")") . "</a>");
712
                } 
713
        }
714

    
715
        /**
716
         * Add a message on purge action
717
         *
718
         *@param $input array : the _POST vars returned bye the item form when press add
719
         *
720
        **/
721
        function addMessageOnPurgeAction($input){
722
                global $INFOFORM_PAGES, $CFG_GLPI, $LANG;
723

    
724
                if (!isset($INFOFORM_PAGES[$this->type])){
725
                        return;
726
                }
727

    
728
                $addMessAfterRedirect=false;
729
                if (isset($input['_purge'])){
730
                        $addMessAfterRedirect=true;
731
                }
732
                if (isset($input['_no_message']) || !$this->auto_message_on_action){
733
                        $addMessAfterRedirect=false;
734
                }
735

    
736
                if ($addMessAfterRedirect) {
737
                        addMessageAfterRedirect($LANG["common"][73].": ".(isset($this->fields["name"]) && !empty($this->fields["name"]) ? stripslashes($this->fields["name"]) : "(".$this->fields['ID'].")"));
738
                } 
739
        }
740

    
741

    
742
        
743
        /**
744
         * Actions done before the DELETE of the item in the database / Maybe used to add another check for deletion 
745
         *
746
         *@param $ID ID of the item to delete
747
         * 
748
         *@return bool : true if item need to be deleted else false
749
         * 
750
        **/
751
        function pre_deleteItem($ID) {
752
                return true;
753
        }
754
        /** 
755
         * Restore an item trashed in the database. 
756
         * 
757
         *@param $input array : the _POST vars returned bye the item form when press restore 
758
         *@param $history boolean : do history log ?
759
         * 
760
         *@return Nothing () 
761
         *@todo specific ones : cartridges / consumables 
762
         * 
763
        **/ 
764
        // specific ones : cartridges / consumables
765
        function restore($input,$history=1) {
766

    
767
                if (isset($input['restore'])){
768
                        $input['_restore']=$input['restore'];
769
                        unset($input['restore']);
770
                }
771

    
772
                $input['_item_type_']=$this->type;
773
                $input=doHookFunction("pre_item_restore",$input);
774

    
775
                if ($this->getFromDB($input[$this->getIndexName()])){
776
                        if ($this->restoreInDB($input["ID"])){
777
                                $this->addMessageOnRestoreAction($input);
778

    
779
                                if ($this->dohistory&&$history){
780
                                        $changes[0] = 0;
781
                                        $changes[1] = $changes[2] = "";
782
        
783
                                        historyLog ($input["ID"],$this->type,$changes,0,HISTORY_RESTORE_ITEM);
784
                                }
785
        
786
                                doHook("item_restore",array("type"=>$this->type, "ID" => $input["ID"]));
787
                        }
788
                }
789
        }
790

    
791
        /**
792
         * Add a message on restore action
793
         *
794
         *@param $input array : the _POST vars returned bye the item form when press add
795
         *
796
        **/
797
        function addMessageOnRestoreAction($input){
798
                global $INFOFORM_PAGES, $CFG_GLPI, $LANG;
799

    
800
                if (!isset($INFOFORM_PAGES[$this->type])){
801
                        return;
802
                }
803

    
804
                $addMessAfterRedirect=false;
805
                if (isset($input['_restore'])){
806
                        $addMessAfterRedirect=true;
807
                }
808
                if (isset($input['_no_message']) || !$this->auto_message_on_action){
809
                        $addMessAfterRedirect=false;
810
                }
811

    
812
                if ($addMessAfterRedirect) {
813
                        addMessageAfterRedirect($LANG["common"][74] . 
814
                        ": <a href='" . $CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[$this->type] . "?ID=" . $this->fields['ID'] . "'>" .
815
                        (isset($this->fields["name"]) && !empty($this->fields["name"]) ? stripslashes($this->fields["name"]) : "(".$this->fields['ID'].")") . "</a>");
816
                } 
817
        }
818

    
819
        /**
820
         * Reset fields of the item 
821
         *
822
        **/
823
        function reset(){
824
                $this->fields=array();
825

    
826
        }
827

    
828
        /**
829
         * Define onglets to display
830
         *
831
         *@param $withtemplate is a template view ?
832
         * 
833
         *@return array containing the onglets
834
         * 
835
        **/
836
        function defineOnglets($withtemplate){
837
                return array();
838
        }
839

    
840
        /**
841
         * Show onglets
842
         *
843
         *@param $ID ID of the item to display
844
         *@param $withtemplate is a template view ?
845
         *@param $actif active onglet
846
         *@param $nextprevcondition condition used to find next/previous items
847
         *@param $nextprev_item field used to define next/previous items
848
         *@param $addurlparam parameters to add to the URLs 
849
         * 
850
         *@return Nothing () 
851
         *  
852
        **/
853
        function showOnglets($ID,$withtemplate,$actif,$nextprevcondition="",$nextprev_item="",$addurlparam=""){
854
                global $LANG,$CFG_GLPI;
855

    
856
                $target=$_SERVER['PHP_SELF']."?ID=".$ID;
857
        
858
                $template="";
859
                if(!empty($withtemplate)){
860
                        $template="&amp;withtemplate=$withtemplate";
861
                }
862
        
863
                echo "<div id='barre_onglets'><ul id='onglet'>";
864
        
865
                if (count($onglets=$this->defineOnglets($withtemplate))){
866
                        //if (empty($withtemplate)&&haveRight("reservation_central","r")&&function_exists("isReservable")){
867
                        //        $onglets[11]=$LANG["Menu"][17];
868
                        //        ksort($onglets);
869
                        //}
870
                        foreach ($onglets as $key => $val ) {
871
                                echo "<li "; if ($actif==$key){ echo "class='actif'";} echo  "><a href='$target&amp;onglet=$key$template$addurlparam'>".$val."</a></li>";
872
                        }
873
                        if(empty($withtemplate)){
874
                                echo "<li class='invisible'>&nbsp;</li>";
875
                                echo "<li "; if ($actif=="-1") {echo "class='actif'";} echo "><a href='$target&amp;onglet=-1$template$addurlparam'>".$LANG["common"][66]."</a></li>";
876
                        }
877
                }
878
        
879
        
880
        
881
                displayPluginHeadings($target,$this->type,$withtemplate,$actif);
882
        
883
                echo "<li class='invisible'>&nbsp;</li>";
884
        
885
                if (empty($withtemplate)&&preg_match("/\?ID=([0-9]+)/",$target,$ereg)){
886
                        $ID=$ereg[1];
887
                        $next=getNextItem($this->table,$ID,$nextprevcondition,$nextprev_item);
888
                        $prev=getPreviousItem($this->table,$ID,$nextprevcondition,$nextprev_item);
889
                        $cleantarget=preg_replace("/\?ID=([0-9]+)/","",$target);
890
                        if ($prev>0) {
891
                                echo "<li><a href='$cleantarget?ID=$prev$addurlparam'><img src=\"".$CFG_GLPI["root_doc"]."/pics/left.png\" alt='".$LANG["buttons"][12]."' title='".$LANG["buttons"][12]."'></a></li>";
892
                        }
893
                        if ($next>0) {
894
                                echo "<li><a href='$cleantarget?ID=$next$addurlparam'><img src=\"".$CFG_GLPI["root_doc"]."/pics/right.png\" alt='".$LANG["buttons"][11]."' title='".$LANG["buttons"][11]."'></a></li>";
895
                        }
896
                }
897
        
898
                echo "</ul></div>";
899
        } 
900

    
901
        /**
902
         * Have I the right to "write" the Object
903
         * 
904
         * @return Array of can_edit (can write) + can_recu (can make recursive)
905
        **/
906
/*        function canEditAndRecurs () {
907
                global $CFG_GLPI;
908
                
909
                $can_edit = $this->canCreate();
910

911
                if (!isset($CFG_GLPI["recursive_type"][$this->type])) {
912
                        $can_recu = false;
913
                        
914
                } else if (!isset($this->fields["ID"])) {
915
                        $can_recu = haveRecursiveAccessToEntity($_SESSION["glpiactive_entity"]);
916
                                
917
                } else {
918
                        if ($this->fields["recursive"]) {
919
                                $can_edit = $can_edit && haveRecursiveAccessToEntity($this->fields["FK_entities"]);
920
                                $can_recu = $can_edit;
921
                        }        
922
                        else {
923
                                $can_recu = $can_edit && haveRecursiveAccessToEntity($this->fields["FK_entities"]);        
924
                        }
925
                }
926
        
927
                return array($can_edit, $can_recu);                
928
        }
929
*/        
930
        /**
931
         * Have I the right to "write" the Object
932
         *
933
         * @return bitmask : 0:no, 1:can_edit (can write), 2:can_recu (can make recursive)
934
        **/
935
/*        function canEdit () {
936
                list($can_edit,$can_recu)=$this->canEditAndRecurs();
937
                return ($can_edit?1:0)+($can_recu?2:0);
938
        }
939
*/
940

    
941

    
942
        /**
943
         * Have I the right to "create" the Object
944
         * 
945
         * May be overloaded if needed (ex kbitem)
946
         *
947
         * @return booleen
948
         **/
949
        function canCreate () {
950
                return haveTypeRight($this->type,"w");
951
        }
952

    
953
        /**
954
         * Have I the right to "view" the Object
955
         * 
956
         * May be overloaded if needed
957
         *
958
         * @return booleen
959
         **/
960
        function canView () {
961
                return haveTypeRight($this->type,"r");
962
        }
963

    
964
        /**
965
         * Check right on an item
966
         *
967
         * @param $ID ID of the item (-1 if new item)
968
         * @param $right Right to check : r / w / recursive
969
         * @param $entity entity to check right (used for adding item)
970
         *
971
         * @return boolean
972
        **/
973
        function can($ID,$right,$entity=-1){
974

    
975
                $entity_to_check=-1;
976
                $recursive_state_to_check=0;
977
                // Get item if not already loaded
978
                if (empty($ID)||$ID<=0){
979
                        $this->getEmpty($ID);
980
                        // No entity define : adding process : use active entity
981
                        if ($entity==-1){
982
                                $entity_to_check=$_SESSION["glpiactive_entity"];
983
                        } else { 
984
                                $entity_to_check=$entity;
985
                        }
986
                } else {
987
                        if (!isset($this->fields['ID'])||$this->fields['ID']!=$ID){
988
                                // Item not found : no right
989
                                if (!$this->getFromDB($ID)){
990
                                        return false;
991
                                }
992
                        }
993
                        if ($this->entity_assign){
994
                                $entity_to_check=$this->fields["FK_entities"];
995
                                if ($this->may_be_recursive){
996
                                        $recursive_state_to_check=$this->fields["recursive"];
997
                                }
998
                        }
999

    
1000
                } 
1001

    
1002
//                echo $ID."_".$entity_to_check."_".$recursive_state_to_check.'<br>';
1003
                switch ($right){
1004
                        case 'r':
1005
                                // Personnal item
1006
                                if ($this->may_be_private && $this->fields['private'] && $this->fields['FK_users']==$_SESSION["glpiID"]){
1007
                                        return true;
1008
                                } else {
1009
                                        // Check Global Right
1010
                                        if ($this->canView()){
1011
                                                // Is an item assign to an entity
1012
                                                if ($this->entity_assign){
1013
                                                        // Can be recursive check 
1014
                                                        if ($this->may_be_recursive){
1015
                                                                return haveAccessToEntity($entity_to_check,$recursive_state_to_check);
1016
                                                        } else { // Non recursive item
1017
                                                                return haveAccessToEntity($entity_to_check);
1018
                                                        }
1019
                                                } else { // Global item
1020
                                                        return true;
1021
                                                }
1022
                                        }
1023
                                }
1024
                                break;
1025
                        case 'w':
1026
                                // Personnal item
1027
                                if ($this->may_be_private && $this->fields['private'] && $this->fields['FK_users']==$_SESSION["glpiID"]){
1028
                                        return true;
1029
                                } else {
1030
                                        // Check Global Right
1031
                                        if ($this->canCreate()){
1032
                                                // Is an item assign to an entity
1033
                                                if ($this->entity_assign){
1034
                                                        // Have access to entity
1035
                                                        return haveAccessToEntity($entity_to_check);
1036
                                                } else { // Global item
1037
                                                        return true;
1038
                                                }
1039
                                        }
1040
                                }
1041
                                break;
1042
                        case 'recursive':
1043
                                if ($this->entity_assign && $this->may_be_recursive){
1044
                                        if ($this->canCreate() && haveAccessToEntity($entity_to_check)){
1045
                                                // Can make recursive if recursive access to entity
1046
                                                return haveRecursiveAccessToEntity($entity_to_check);
1047
                                        }
1048
                                }
1049
                                break;
1050
                }
1051
                return false;
1052

    
1053
        }
1054
        /**
1055
         * Check right on an item with block
1056
         *
1057
         * @param $ID ID of the item (-1 if new item)
1058
         * @param $right Right to check : r / w / recursive
1059
         * @param $entity entity to check right (used for adding item)
1060
         * @return nothing
1061
        **/
1062
        function check($ID,$right,$entity=-1) {
1063
                global $CFG_GLPI;
1064
        
1065
                if (!$this->can($ID,$right,$entity)) {
1066
                        // Gestion timeout session
1067
                        if (!isset ($_SESSION["glpiID"])) {
1068
                                glpi_header($CFG_GLPI["root_doc"] . "/index.php");
1069
                                exit ();
1070
                        }
1071
                        displayRightError();
1072
                }
1073
        }
1074
}
1075

    
1076
?>
Redmine Appliance - Powered by TurnKey Linux