Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

ryxeo-glpi-git / inc / rule.dictionnary.dropdown.class.php @ b67d8923

Historique | Voir | Annoter | Télécharger (15,4 ko)

1
<?php
2
/*
3
 * @version $Id: rule.dictionnary.dropdown.class.php 7875 2009-01-23 15:16:47Z moyo $
4
 -------------------------------------------------------------------------
5
 GLPI - Gestionnaire Libre de Parc Informatique
6
 Copyright (C) 2003-2009 by the INDEPNET Development Team.
7

8
 http://indepnet.net/   http://glpi-project.org
9
 -------------------------------------------------------------------------
10

11
 LICENSE
12

13
 This file is part of GLPI.
14

15
 GLPI is free software; you can redistribute it and/or modify
16
 it under the terms of the GNU General Public License as published by
17
 the Free Software Foundation; either version 2 of the License, or
18
 (at your option) any later version.
19

20
 GLPI is distributed in the hope that it will be useful,
21
 but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 GNU General Public License for more details.
24

25
 You should have received a copy of the GNU General Public License
26
 along with GLPI; if not, write to the Free Software
27
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28
 --------------------------------------------------------------------------
29
 */
30

    
31
// ----------------------------------------------------------------------
32
// Original Author of file: Walid Nouh
33
// Purpose of file:
34
// ----------------------------------------------------------------------
35

    
36
class RuleDictionnaryDropdown extends RuleCached{
37

    
38
        /**
39
         * Constructor
40
         * @param $type dropdown type
41
        **/
42
        function RuleDictionnaryDropdown($type){
43
                parent::RuleCached();
44
                $this->rule_type=$type;
45
                $this->can_sort=true;
46
                $this->right="rule_dictionnary_dropdown";
47
        }
48

    
49
        function maxActionsCount(){
50
                return 1;
51
        }
52

    
53
        function getTitle() {
54
                global $LANG;
55
                switch ($this->rule_type){
56
                        case RULE_DICTIONNARY_MANUFACTURER :
57
                                return $LANG["rulesengine"][36];
58
                        break;
59
                        case RULE_DICTIONNARY_MODEL_COMPUTER :
60
                                return $LANG["rulesengine"][50];
61
                        break;
62
                        case RULE_DICTIONNARY_TYPE_COMPUTER :
63
                                return $LANG["rulesengine"][60];
64
                        break;
65
                        case RULE_DICTIONNARY_MODEL_MONITOR :
66
                                return $LANG["rulesengine"][51];
67
                        break;
68
                        case RULE_DICTIONNARY_TYPE_MONITOR :
69
                                return $LANG["rulesengine"][61];
70
                        break;
71
                        case RULE_DICTIONNARY_MODEL_PRINTER :
72
                                return $LANG["rulesengine"][54];
73
                        break;
74
                        case RULE_DICTIONNARY_TYPE_PRINTER :
75
                                return $LANG["rulesengine"][64];
76
                        break;
77
                        case RULE_DICTIONNARY_MODEL_PHONE :
78
                                return $LANG["rulesengine"][52];
79
                        break;
80
                        case RULE_DICTIONNARY_TYPE_PHONE :
81
                                return $LANG["rulesengine"][62];
82
                        break;
83
                        case RULE_DICTIONNARY_MODEL_PERIPHERAL :
84
                                return $LANG["rulesengine"][53];
85
                        break;
86
                        case RULE_DICTIONNARY_TYPE_PERIPHERAL :
87
                                return $LANG["rulesengine"][63];
88
                        break;
89
                        case RULE_DICTIONNARY_MODEL_NETWORKING :
90
                                return $LANG["rulesengine"][55];
91
                        break;
92
                        case RULE_DICTIONNARY_TYPE_NETWORKING :
93
                                return $LANG["rulesengine"][65];
94
                        break;
95
                        case RULE_DICTIONNARY_OS :
96
                                return $LANG["rulesengine"][67];
97
                        break;
98
                        case RULE_DICTIONNARY_OS_SP :
99
                                return $LANG["rulesengine"][68];
100
                        break;
101
                        case RULE_DICTIONNARY_OS_VERSION :
102
                                return $LANG["rulesengine"][69];
103
                        break;
104
                }
105
        }
106

    
107
        function showCacheRuleHeader(){
108
                if (in_array($this->rule_type,array(RULE_DICTIONNARY_MODEL_COMPUTER,
109
                                                RULE_DICTIONNARY_MODEL_MONITOR,
110
                                                RULE_DICTIONNARY_MODEL_PRINTER,
111
                                                RULE_DICTIONNARY_MODEL_PHONE,
112
                                                RULE_DICTIONNARY_MODEL_PERIPHERAL,
113
                                                RULE_DICTIONNARY_MODEL_NETWORKING,
114
                                                        ))){
115
                        global $LANG;
116
                        echo "<th colspan='3'>".$LANG["rulesengine"][100]." : ".$this->fields["name"]."</th></tr>";
117
                        echo "<tr>";
118
                        echo "<td class='tab_bg_1'>".$LANG["rulesengine"][104]."</td>";
119
                        echo "<td class='tab_bg_1'>".$LANG["common"][5]."</td>";
120
                        echo "<td class='tab_bg_1'>".$LANG["rulesengine"][105]."</td>";
121
                        echo "</tr>";
122
                } else {
123
                        parent::showCacheRuleHeader();
124
                }
125
        }
126

    
127
        function showCacheRuleDetail($fields){
128
                if (in_array($this->rule_type,array(RULE_DICTIONNARY_MODEL_COMPUTER,
129
                                                RULE_DICTIONNARY_MODEL_MONITOR,
130
                                                RULE_DICTIONNARY_MODEL_PRINTER,
131
                                                RULE_DICTIONNARY_MODEL_PHONE,
132
                                                RULE_DICTIONNARY_MODEL_PERIPHERAL,
133
                                                RULE_DICTIONNARY_MODEL_NETWORKING,
134
                ))){
135
                        global $LANG;
136
                        echo "<td class='tab_bg_2'>".$fields["old_value"]."</td>";
137
                        echo "<td class='tab_bg_2'>".($fields["manufacturer"]!=''?$fields["manufacturer"]:'')."</td>";                
138
                        echo "<td class='tab_bg_2'>".($fields["new_value"]!=''?$fields["new_value"]:$LANG["rulesengine"][106])."</td>";
139
                } else {
140
                        parent::showCacheRuleDetail($fields);
141
                }
142
        }
143

    
144

    
145
}
146

    
147

    
148

    
149
class DictionnaryDropdownCollection extends RuleCachedCollection{
150
        /// dropdown table
151
        var $item_table="";
152
        
153

    
154
        /**
155
         * Constructor
156
         * @param $type dropdown type
157
        **/
158
        function DictionnaryDropdownCollection($type){
159
                $this->rule_type = $type;
160
                $this->rule_class_name = 'RuleDictionnaryDropdown';
161
                $this->right="rule_dictionnary_dropdown";
162

    
163
                switch ($this->rule_type){
164
                        case RULE_DICTIONNARY_MANUFACTURER :
165
                                $this->item_table="glpi_dropdown_manufacturer";
166
                                //Init cache system values
167
                                $this->initCache("glpi_rule_cache_manufacturer");
168
                        break;
169
                        case RULE_DICTIONNARY_MODEL_COMPUTER :
170
                                $this->item_table="glpi_dropdown_model";
171
                                //Init cache system values
172
                                $this->initCache("glpi_rule_cache_model_computer",array("name"=>"old_value","manufacturer"=>"manufacturer"));
173
                        break;
174
                        case RULE_DICTIONNARY_TYPE_COMPUTER :
175
                                $this->item_table="glpi_type_computers";
176
                                //Init cache system values
177
                                $this->initCache("glpi_rule_cache_type_computer");
178
                        break;
179
                        case RULE_DICTIONNARY_MODEL_MONITOR :
180
                                $this->item_table="glpi_dropdown_model_monitors";
181
                                //Init cache system values
182
                                $this->initCache("glpi_rule_cache_model_monitor",array("name"=>"old_value","manufacturer"=>"manufacturer"));
183
                        break;
184
                        case RULE_DICTIONNARY_TYPE_MONITOR :
185
                                $this->item_table="glpi_type_monitors";
186
                                //Init cache system values
187
                                $this->initCache("glpi_rule_cache_type_monitor");
188
                        break;
189
                        case RULE_DICTIONNARY_MODEL_PRINTER :
190
                                $this->item_table="glpi_dropdown_model_printers";
191
                                //Init cache system values
192
                                $this->initCache("glpi_rule_cache_model_printer",array("name"=>"old_value","manufacturer"=>"manufacturer"));
193
                        break;
194
                        case RULE_DICTIONNARY_TYPE_PRINTER :
195
                                $this->item_table="glpi_type_printer";
196
                                $this->initCache("glpi_rule_cache_type_printer");
197
                        break;
198
                        case RULE_DICTIONNARY_MODEL_PHONE :
199
                                $this->item_table="glpi_dropdown_model_phones";
200
                                $this->initCache("glpi_rule_cache_model_phone",array("name"=>"old_value","manufacturer"=>"manufacturer"));
201
                        break;
202
                        case RULE_DICTIONNARY_TYPE_PHONE :
203
                                $this->item_table="glpi_type_phones";
204
                                $this->initCache("glpi_rule_cache_type_phone");
205
                        break;
206
                        case RULE_DICTIONNARY_MODEL_PERIPHERAL :
207
                                $this->item_table="glpi_dropdown_model_peripherals";
208
                                $this->initCache("glpi_rule_cache_model_peripheral",array("name"=>"old_value","manufacturer"=>"manufacturer"));
209
                        break;
210
                        case RULE_DICTIONNARY_TYPE_PERIPHERAL :
211
                                $this->item_table="glpi_type_peripherals";
212
                                $this->initCache("glpi_rule_cache_type_peripheral");
213
                        break;
214
                        case RULE_DICTIONNARY_MODEL_NETWORKING :
215
                                $this->item_table="glpi_dropdown_model_networking";
216
                                $this->initCache("glpi_rule_cache_model_networking",array("name"=>"old_value","manufacturer"=>"manufacturer"));
217
                        break;
218
                        case RULE_DICTIONNARY_TYPE_NETWORKING :
219
                                $this->item_table="glpi_type_networkings";
220
                                $this->initCache("glpi_rule_cache_type_networking");
221
                        break;
222
                        case RULE_DICTIONNARY_OS :
223
                                $this->item_table="glpi_dropdown_os";
224
                                $this->initCache("glpi_rule_cache_os");
225
                        break;
226
                        case RULE_DICTIONNARY_OS_SP :
227
                                $this->item_table="glpi_dropdown_os_sp";
228
                                $this->initCache("glpi_rule_cache_os_sp");
229
                        break;
230
                        case RULE_DICTIONNARY_OS_VERSION :
231
                                $this->item_table="glpi_dropdown_os_version";
232
                                $this->initCache("glpi_rule_cache_os_version");
233
                        break;
234
                }
235
                
236

    
237
        }
238

    
239
        function getTitle() {
240
                global $LANG;
241
                switch ($this->rule_type){
242
                        case RULE_DICTIONNARY_MANUFACTURER :
243
                                return $LANG["rulesengine"][36];
244
                        break;
245
                        case RULE_DICTIONNARY_MODEL_COMPUTER :
246
                                return $LANG["rulesengine"][50];
247
                        break;
248
                        case RULE_DICTIONNARY_TYPE_COMPUTER :
249
                                return $LANG["rulesengine"][60];
250
                        break;
251
                        case RULE_DICTIONNARY_MODEL_MONITOR :
252
                                return $LANG["rulesengine"][51];
253
                        break;
254
                        case RULE_DICTIONNARY_TYPE_MONITOR :
255
                                return $LANG["rulesengine"][61];
256
                        break;
257
                        case RULE_DICTIONNARY_MODEL_PRINTER :
258
                                return $LANG["rulesengine"][54];
259
                        break;
260
                        case RULE_DICTIONNARY_TYPE_PRINTER :
261
                                return $LANG["rulesengine"][64];
262
                        break;
263
                        case RULE_DICTIONNARY_MODEL_PHONE :
264
                                return $LANG["rulesengine"][52];
265
                        break;
266
                        case RULE_DICTIONNARY_TYPE_PHONE :
267
                                return $LANG["rulesengine"][62];
268
                        break;
269
                        case RULE_DICTIONNARY_MODEL_PERIPHERAL :
270
                                return $LANG["rulesengine"][53];
271
                        break;
272
                        case RULE_DICTIONNARY_TYPE_PERIPHERAL :
273
                                return $LANG["rulesengine"][63];
274
                        break;
275
                        case RULE_DICTIONNARY_MODEL_NETWORKING :
276
                                return $LANG["rulesengine"][55];
277
                        break;
278
                        case RULE_DICTIONNARY_TYPE_NETWORKING :
279
                                return $LANG["rulesengine"][65];
280
                        break;
281
                        case RULE_DICTIONNARY_OS :
282
                                return $LANG["rulesengine"][67];
283
                        break;
284
                        case RULE_DICTIONNARY_OS_SP :
285
                                return $LANG["rulesengine"][68];
286
                        break;
287
                        case RULE_DICTIONNARY_OS_VERSION :
288
                                return $LANG["rulesengine"][69];
289
                        break;
290
                }
291
        }
292

    
293
        function getRuleClass(){
294
                return new $this->rule_class_name($this->rule_type);
295
        }
296

    
297

    
298
        /**
299
         * Replay collection rules on an existing DB
300
         * @param $offset offset used to begin
301
         * @param $maxtime maximum time of process (reload at the end)
302
         * @return -1 on completion else current offset
303
        **/
304
        function replayRulesOnExistingDB($offset=0,$maxtime=0){
305
                global $DB,$LANG;
306

    
307

    
308

    
309
                // Model check : need to check using manufacturer extra data so specific function
310
                if (in_array($this->rule_type,array(RULE_DICTIONNARY_MODEL_COMPUTER,
311
                                                RULE_DICTIONNARY_MODEL_MONITOR,
312
                                                RULE_DICTIONNARY_MODEL_PRINTER,
313
                                                RULE_DICTIONNARY_MODEL_PHONE,
314
                                                RULE_DICTIONNARY_MODEL_PERIPHERAL,
315
                                                RULE_DICTIONNARY_MODEL_NETWORKING,
316
                ))){
317
                        return $this->replayRulesOnExistingDBForModel($offset,$maxtime);
318
                }
319

    
320

    
321
                if (isCommandLine())
322
                        echo "replayRulesOnExistingDB started : " . date("r") . "\n";
323

    
324
                // Get All items
325
                $Sql="SELECT * FROM " . $this->item_table;
326
                if ($offset) {
327
                        $Sql .= " LIMIT $offset,999999999";
328
                } 
329
                
330
                $result = $DB->query($Sql);
331

    
332
                $nb = $DB->numrows($result)+$offset;
333
                $i  = $offset;
334
                if ($result && $nb>$offset) {
335
                        // Step to refresh progressbar
336
                        $step=($nb>20 ? floor($nb/20) : 1);
337
                        $send = array ();
338
                        $send["tablename"] = $this->item_table;
339
                        while ($data = $DB->fetch_array($result)){
340
                                if (!($i % $step)) {
341
                                        if (isCommandLine()) {
342
                                                echo "replayRulesOnExistingDB : $i/$nb\r";
343
                                        } else {
344
                                                changeProgressBarPosition($i,$nb,"$i / $nb");
345
                                        }
346
                                }
347

    
348
                                //Replay Type dictionnary
349
                                $ID=externalImportDropdown($this->item_table,addslashes($data["name"]),-1,array(),addslashes($data["comments"]));
350
                                
351
                                if ($data['ID'] != $ID) {
352
                                        $tomove[$data['ID']]=$ID;
353
                                        $send["oldID"] = $data['ID'];
354
                                        $send["newID"] = $ID;
355
                                        replaceDropDropDown($send);
356
                                }                
357

    
358
                                $i++;
359
                                if ($maxtime) {
360
                                        $crt=explode(" ",microtime());
361
                                        if ($crt[0]+$crt[1] > $maxtime) {
362
                                                break;
363
                                        }
364
                                }
365
                        } // end while 
366
                }
367
                
368
                if (isCommandLine()) {
369
                        echo "replayRulesOnExistingDB ended : " . date("r") . "\n";                        
370
                } else {
371
                        changeProgressBarPosition($i,$nb,"$i / $nb");
372
                }
373
                
374
                return ($i==$nb ? -1 : $i);
375
        } // function
376

    
377

    
378
        /**
379
         * Replay collection rules on an existing DB for model dropdowns
380
         * @param $offset offset used to begin
381
         * @param $maxtime maximum time of process (reload at the end)
382
         * @return -1 on completion else current offset
383
        **/
384
        function replayRulesOnExistingDBForModel($offset=0,$maxtime=0){
385
                global $DB,$LANG;
386

    
387

    
388
                if (isCommandLine())
389
                        echo "replayRulesOnExistingDB started : " . date("r") . "\n";
390

    
391
                // Model check : need to check using manufacturer extra data
392
                $model_table="";
393
                // Find linked table to model
394
                $RELATION = getDbRelations();
395
                if (isset ($RELATION[$this->item_table])){
396
                        foreach ($RELATION[$this->item_table] as $table => $field){ 
397
                                if ($field=="model"){
398
                                        $model_table=$table;
399
                                }
400
                        }
401
                } 
402
                if (empty($model_table)) {
403
                        echo "Error replaying rules";
404
                        return false;
405
                }
406

    
407

    
408
                // Need to give manufacturer from item table
409
                $Sql="SELECT DISTINCT glpi_dropdown_manufacturer.ID AS idmanu, glpi_dropdown_manufacturer.name AS manufacturer, ".
410
                        $this->item_table.".ID AS ID, ".$this->item_table.".name AS name, ".$this->item_table.".comments AS comments ".
411
                        "FROM ".$this->item_table.", $model_table LEFT JOIN glpi_dropdown_manufacturer ON ($model_table.FK_glpi_enterprise=glpi_dropdown_manufacturer.ID) ".
412
                        "WHERE $model_table.model=".$this->item_table.".ID ";
413
                if ($offset) {
414
                        $Sql .= " LIMIT $offset,999999999";
415
                }
416
                $result = $DB->query($Sql);
417

    
418
                $nb = $DB->numrows($result)+$offset;
419
                $i  = $offset;
420
                
421
                if ($result && $nb>$offset) {
422
                        // Step to refresh progressbar
423
                        $step=($nb>20 ? floor($nb/20) : 1);
424
                        $tocheck=array();
425
                        while ($data = $DB->fetch_array($result)){
426

    
427
                                if (!($i % $step)) {
428
                                        if (isCommandLine()) {
429
                                                echo "replayRulesOnExistingDB : $i/$nb\r";
430
                                        } else {
431
                                                changeProgressBarPosition($i,$nb,"$i / $nb");
432
                                        }
433
                                }
434
                                // Model case
435
                                if (isset($data["manufacturer"])){
436
                                        $data["manufacturer"] = processManufacturerName($data["manufacturer"]);
437
                                }
438

    
439
                                //Replay Type dictionnary
440
                                $ID=externalImportDropdown($this->item_table,addslashes($data["name"]),-1,$data,addslashes($data["comments"]));
441

    
442
                                if ($data['ID'] != $ID) {
443
                                        $tocheck[$data["ID"]][]=$ID;
444

    
445
                                        $sql = "UPDATE $model_table SET model='".$ID."' WHERE model='".$data['ID']."'";
446
                                        if (empty($data['idmanu'])){
447
                                                $sql .= " AND (FK_glpi_enterprise IS NULL OR FK_glpi_enterprise = 0)";
448
                                        } else {
449
                                                $sql .= " AND FK_glpi_enterprise='".$data['idmanu']."'";
450
                                        }
451

    
452
                                        $DB->query($sql);
453
                                }                
454

    
455
                                $i++;
456
                                if ($maxtime) {
457
                                        $crt=explode(" ",microtime());
458
                                        if ($crt[0]+$crt[1] > $maxtime) {
459
                                                break;
460
                                        }
461
                                }
462
                        } 
463

    
464
                        foreach ($tocheck AS $ID => $tab)         {
465
                                $sql="SELECT COUNT(*) FROM $model_table WHERE model='$ID'";
466
                                $result = $DB->query($sql);
467
                                $deletecartmodel=false;
468
                                // No item left : delete old item
469
                                if ($result && $DB->result($result,0,0)==0) {
470
                                        $Sql = "DELETE FROM ".$this->item_table." WHERE ID='".$ID."'";
471
                                        $resdel = $DB->query($Sql);
472
                                        $deletecartmodel=true;
473
                                } 
474
                                // Manage cartridge assoc Update items
475
                                if ($this->rule_type==RULE_DICTIONNARY_MODEL_PRINTER){
476
                                        $sql="SELECT * FROM glpi_cartridges_assoc WHERE FK_glpi_dropdown_model_printers = '$ID'";
477
                                        if ($result=$DB->query($sql)){
478
                                                if ($DB->numrows($result)){        
479
                                                        // Get compatible cartridge type
480
                                                        $carttype=array();
481
                                                        while ($data=$DB->fetch_array($result)){
482
                                                                $carttype[]=$data['FK_glpi_cartridges_type'];
483
                                                        }
484
                                                        // Delete cartrodges_assoc
485
                                                        if ($deletecartmodel){
486
                                                                $sql="DELETE FROM glpi_cartridges_assoc WHERE FK_glpi_dropdown_model_printers = 'ID'";
487
                                                                $DB->query($sql);
488
                                                        }
489
                                                        // Add new assoc
490
                                                        if (!class_exists('CartridgeType')){
491
                                                                include_once (GLPI_ROOT . "/inc/cartridge.function.php");
492
                                                        }
493
                                                        $ct=new CartridgeType();
494
                                                        foreach ($carttype as $cartID){
495
                                                                foreach ($tab as $model){
496
                                                                        $ct->addCompatibleType($cartID,$model);
497
                                                                }
498
                                                        }
499
                                                }
500
                                        }                                                
501
                                }
502
        
503
                        } // each tocheck
504
                }
505
                if (isCommandLine()) {
506
                        echo "replayRulesOnExistingDB ended : " . date("r") . "\n";                        
507
                } else {
508
                        changeProgressBarPosition($i,$nb,"$i / $nb");
509
                }
510
                return ($i==$nb ? -1 : $i);
511
        }
512

    
513
}        
514
?>
Redmine Appliance - Powered by TurnKey Linux