Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

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

Historique | Voir | Annoter | Télécharger (84,2 ko)

1
<?php
2
/*
3
 * @version $Id: transfer.class.php 7879 2009-01-23 17:21:05Z moyo $
4
 -------------------------------------------------------------------------
5
 GLPI - Gestionnaire Libre de Parc Informatique
6
 Copyright (C) 2003-2009 by the INDEPNET Development Team.
7

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

11
 LICENSE
12

13
 This file is part of GLPI.
14

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

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

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

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

    
36

    
37
if (!defined('GLPI_ROOT')){
38
        die("Sorry. You can't access directly to this file");
39
        }
40

    
41
// Tracking Classes
42

    
43
class Transfer extends CommonDBTM{
44

    
45
        /// Already transfer item
46
        var $already_transfer=array();        
47
        /// Items simulate to move - non recursive item or recursive item not visible in destination entity
48
        var $needtobe_transfer=array();        
49
        /// Items simulate to move - recursive item visible in destination entity
50
        var $noneedtobe_transfer=array();        
51
        /// Search in need to be transfer items
52
        var $item_search=array();
53
        /// Search in need to be exclude from transfer
54
        var $item_recurs=array();
55
        /// Options used to transfer
56
        var $options=array();
57
        /// Destination entity ID
58
        var $to=-1;
59
        /// type of initial item transfered
60
        var $inittype=0;
61
        /// item types which have infocoms
62
        var $INFOCOMS_TYPES = array(COMPUTER_TYPE, NETWORKING_TYPE, PRINTER_TYPE, MONITOR_TYPE, PERIPHERAL_TYPE, PHONE_TYPE, SOFTWARE_TYPE);
63
        /// item types which have contracts
64
        var $CONTRACTS_TYPES = array(COMPUTER_TYPE, NETWORKING_TYPE, PRINTER_TYPE, MONITOR_TYPE, PERIPHERAL_TYPE, PHONE_TYPE, SOFTWARE_TYPE);
65
        /// item types which have tickets
66
        var $TICKETS_TYPES = array(COMPUTER_TYPE, NETWORKING_TYPE, PRINTER_TYPE, MONITOR_TYPE, PERIPHERAL_TYPE, PHONE_TYPE, SOFTWARE_TYPE);
67
        /// item types which have documents
68
        var $DOCUMENTS_TYPES=array(ENTERPRISE_TYPE, CONTRACT_TYPE, CONTACT_TYPE, CONSUMABLE_TYPE, CARTRIDGE_TYPE, COMPUTER_TYPE, NETWORKING_TYPE, PRINTER_TYPE, MONITOR_TYPE, PERIPHERAL_TYPE, PHONE_TYPE, SOFTWARE_TYPE,DOCUMENT_TYPE);
69

    
70

    
71
        /**
72
         * Constructor
73
        **/
74
        function Transfer(){
75
                $this->table="glpi_transfers";
76
                $this->type=TRANSFER_TYPE;
77
        }
78

    
79
        /**
80
         * Transfer items
81
         *
82
         *@param $items items to transfer
83
         *@param $to entity destination ID
84
         *@param $options options used to transfer
85
         *
86
         **/
87
        function moveItems($items,$to,$options){
88
                global $CFG_GLPI;
89
                // unset mailing
90
                $CFG_GLPI["mailing"]=0;
91
                
92
                // $items=array(TYPE => array(id_items))
93
                // $options=array()
94
                
95
                $default_options=array(
96
                        'keep_tickets'=>0,
97
                        'keep_networklinks'=>0,
98
                        'keep_reservations'=>0,
99
                        'keep_history'=>0,
100
                        'keep_devices'=>0,
101
                        'keep_infocoms'=>0,
102

    
103
                        'keep_dc_monitor'=>0,
104
                        'clean_dc_monitor'=>0,
105
                        'keep_dc_phone'=>0,
106
                        'clean_dc_phone'=>0,
107
                        'keep_dc_peripheral'=>0,
108
                        'clean_dc_peripheral'=>0,
109
                        'keep_dc_printer'=>0,
110
                        'clean_dc_printer'=>0,
111

    
112
                        'keep_enterprises'=>0,
113
                        'clean_enterprises'=>0,
114
                        'keep_contacts'=>0,
115
                        'clean_contacts'=>0,
116

    
117
                        'keep_contracts'=>0,
118
                        'clean_contracts'=>0,
119

    
120
                        'keep_softwares'=>0,
121
                        'clean_softwares'=>0,
122

    
123
                        'keep_documents'=>0,
124
                        'clean_documents'=>0,
125
                        
126
                        'keep_cartridges_type' =>0,
127
                        'clean_cartridges_type' =>0,
128
                        'keep_cartridges' =>0,
129

    
130
                        'keep_consumables' =>0,
131
                );
132
                $ci=new CommonItem();
133

    
134
                if ($to>=0){
135
                        // Store to
136
                        $this->to=$to;
137
                        // Store options
138
                        $this->options=$options;
139
                        foreach ($default_options as $key => $val){
140
                                if (!isset($this->options[$key])){
141
                                        $this->options[$key]=$val;
142
                                }
143
                        }
144
                        
145
                        // Simulate transfers To know which items need to be transfer
146
                        $this->simulateTransfer($items);
147

    
148
                        //printCleanArray($this->needtobe_transfer);
149
                        // Computer first
150
                        $this->inittype=COMPUTER_TYPE;
151
                        if (isset($items[COMPUTER_TYPE])&&count($items[COMPUTER_TYPE])){
152
                                foreach ($items[COMPUTER_TYPE] as $ID){
153
                                        $this->transferItem(COMPUTER_TYPE,$ID,$ID);
154
                                }
155
                        }
156
                        
157
                        // Inventory Items : MONITOR....
158
                        $INVENTORY_TYPES = array(NETWORKING_TYPE, PRINTER_TYPE, MONITOR_TYPE, PERIPHERAL_TYPE, PHONE_TYPE, SOFTWARE_TYPE, CARTRIDGE_TYPE, CONSUMABLE_TYPE);
159
                        foreach ($INVENTORY_TYPES as $type){
160
                                $this->inittype=$type;
161
                                if (isset($items[$type])&&count($items[$type])){
162
                                        foreach ($items[$type] as $ID){
163
                                                $this->transferItem($type,$ID,$ID);
164
                                        }
165
                                }
166
                        }
167
                        // Management Items
168
                        $MANAGEMENT_TYPES = array(ENTERPRISE_TYPE, CONTRACT_TYPE, CONTACT_TYPE, DOCUMENT_TYPE);
169
                        foreach ($MANAGEMENT_TYPES as $type){
170
                                $this->inittype=$type;
171
                                if (isset($items[$type])&&count($items[$type])){
172
                                        foreach ($items[$type] as $ID){
173
                                                $this->transferItem($type,$ID,$ID);
174
                                        }
175
                                }
176
                        }
177
                        // Tickets
178
                        $OTHER_TYPES = array(TRACKING_TYPE,GROUP_TYPE);
179
                        foreach ($OTHER_TYPES as $type){
180
                                $this->inittype=$type;
181
                                if (isset($items[$type])&&count($items[$type])){
182
                                        foreach ($items[$type] as $ID){
183
                                                $this->transferItem($type,$ID,$ID);
184
                                        }
185
                                }
186
                        }
187

    
188
                } // $to >= 0
189
        }
190
        
191
        /**
192
        * Add an item in the needtobe_transfer list
193
        *
194
        *@param $type of the item
195
        *@param $ID of the item
196
        *
197
        **/
198
        function addToBeTransfer ($type, $ID) {
199
                global $LINK_ID_TABLE;
200
                // error_log("Transfer::addToBeTransfer(".$LINK_ID_TABLE[$type].",$ID)");
201
                
202
                if (!isset($this->needtobe_transfer[$type])){
203
                        $this->needtobe_transfer[$type]=array();
204
                }
205
                // Can't be in both list (in fact, always false)
206
                if (isset($this->noneedtobe_transfer[$type][$ID]))
207
                        unset($this->noneedtobe_transfer[$type][$ID]);
208
                        
209
                $this->needtobe_transfer[$type][$ID]=$ID;
210
        }
211

    
212
        /**
213
        * Add an item in the noneedtobe_transfer list
214
        *
215
        *@param $type of the item
216
        *@param $ID of the item
217
        *
218
        **/
219
        function addNotToBeTransfer ($type, $ID) {
220
                global $LINK_ID_TABLE;
221
                // error_log("Transfer::addNotToBeTransfer(".$LINK_ID_TABLE[$type].",$ID)");
222
                
223
                if (!isset($this->noneedtobe_transfer[$type])){
224
                        $this->noneedtobe_transfer[$type]=array();
225
                }
226
                // Can't be in both list (in fact, always true)
227
                if (!isset($this->needtobe_transfer[$type][$ID])) {
228
                        $this->noneedtobe_transfer[$type][$ID]=$ID;
229
                }
230
        }
231

    
232
        /**
233
        * simulate the transfer to know which items need to be transfer
234
        *
235
        *@param $items Array of the items to transfer
236
        *
237
        **/
238
        function simulateTransfer($items){
239
                global $DB,$LINK_ID_TABLE,$CFG_GLPI;
240

    
241
                // Init types :
242
                $types=array(COMPUTER_TYPE,NETWORKING_TYPE,PRINTER_TYPE,MONITOR_TYPE,PERIPHERAL_TYPE,PHONE_TYPE,SOFTWARE_TYPE,CONTRACT_TYPE,ENTERPRISE_TYPE,CONTACT_TYPE,TRACKING_TYPE,DOCUMENT_TYPE,CARTRIDGE_TYPE, CONSUMABLE_TYPE);
243
                foreach ($types as $t){
244
                        if (!isset($this->needtobe_transfer[$t])){
245
                                        $this->needtobe_transfer[$t]=array();
246
                        }
247
                }
248
                foreach ($CFG_GLPI["recursive_type"] as $t => $table) {
249
                        if (!isset($this->noneedtobe_transfer[$t])){
250
                                        $this->noneedtobe_transfer[$t]=array();
251
                        }
252
                }
253
                // Copy items to needtobe_transfer
254
                foreach ($items as $key => $tab){
255
                        if (count($tab)){
256
                                foreach ($tab as $ID){
257
                                        $this->addToBeTransfer($key,$ID);
258
                                }
259
                        }
260
                }
261

    
262
                // Computer first
263
                $this->item_search[COMPUTER_TYPE]=$this->createSearchConditionUsingArray($this->needtobe_transfer[COMPUTER_TYPE]);
264

    
265
                // DIRECT CONNECTIONS
266

    
267
                $DC_CONNECT=array();
268
                if ($this->options['keep_dc_monitor']){
269
                        $DC_CONNECT[]=MONITOR_TYPE;
270
                }
271
                if ($this->options['keep_dc_phone']){
272
                        $DC_CONNECT[]=PHONE_TYPE;
273
                }
274
                if ($this->options['keep_dc_peripheral']){
275
                        $DC_CONNECT[]=PERIPHERAL_TYPE;
276
                }
277
                if ($this->options['keep_dc_printer']){
278
                        $DC_CONNECT[]=PRINTER_TYPE;
279
                }
280
                if (count($DC_CONNECT)&&count($this->needtobe_transfer[COMPUTER_TYPE])>0){
281
                        foreach ($DC_CONNECT as $type){
282
                                // Clean DB / Search unexisting links and force disconnect
283
                                $query="SELECT glpi_connect_wire.ID 
284
                                        FROM glpi_connect_wire 
285
                                        LEFT JOIN ".$LINK_ID_TABLE[$type]." ON (glpi_connect_wire.end1 = ".$LINK_ID_TABLE[$type].".ID ) 
286
                                        WHERE glpi_connect_wire.type='".$type."' AND ".$LINK_ID_TABLE[$type].".ID IS NULL";
287

    
288
                                if ($result = $DB->query($query)) {
289
                                        if ($DB->numrows($result)>0) { 
290
                                                while ($data=$DB->fetch_array($result)){
291
                                                        Disconnect($data['ID'],0,false);
292
                                                }
293
                                        }
294
                                }
295
                                
296

    
297
                                $query = "SELECT DISTINCT end1
298
                                FROM glpi_connect_wire 
299
                                WHERE type='".$type."' AND end2 IN ".$this->item_search[COMPUTER_TYPE];
300
                                if ($result = $DB->query($query)) {
301
                                        if ($DB->numrows($result)>0) { 
302
                                                while ($data=$DB->fetch_array($result)){
303
                                                        $this->addToBeTransfer($type,$data['end1']);
304
                                                }
305
                                        }
306
                                }
307
                        }
308
                }        
309
                $this->item_search[MONITOR_TYPE]=$this->createSearchConditionUsingArray($this->needtobe_transfer[MONITOR_TYPE]);
310
                $this->item_search[PHONE_TYPE]=$this->createSearchConditionUsingArray($this->needtobe_transfer[PHONE_TYPE]);
311
                $this->item_search[PERIPHERAL_TYPE]=$this->createSearchConditionUsingArray($this->needtobe_transfer[PERIPHERAL_TYPE]);
312
                $this->item_search[PRINTER_TYPE]=$this->createSearchConditionUsingArray($this->needtobe_transfer[PRINTER_TYPE]);
313

    
314
                // Licence / Software :  keep / delete + clean unused / keep unused 
315
                if ($this->options['keep_softwares']){
316
                        // Clean DB
317
                        $query="SELECT glpi_inst_software.ID 
318
                                FROM glpi_inst_software 
319
                                LEFT JOIN glpi_computers ON (glpi_inst_software.cID = glpi_computers.ID ) 
320
                                WHERE glpi_computers.ID IS NULL";
321

    
322
                        if ($result = $DB->query($query)) {
323
                                if ($DB->numrows($result)>0) { 
324
                                        while ($data=$DB->fetch_array($result)){
325
                                                $query="DELETE FROM glpi_inst_software WHERE ID='".$data['ID']."'";
326
                                                $DB->query($query);
327
                                        }
328
                                }
329
                        }
330

    
331
                        // Clean DB
332
                        $query="SELECT glpi_inst_software.ID 
333
                                FROM glpi_inst_software 
334
                                LEFT JOIN glpi_licenses ON (glpi_inst_software.license = glpi_licenses.ID ) 
335
                                WHERE glpi_licenses.ID IS NULL";
336

    
337
                        if ($result = $DB->query($query)) {
338
                                if ($DB->numrows($result)>0) { 
339
                                        while ($data=$DB->fetch_array($result)){
340
                                                $query="DELETE FROM glpi_inst_software WHERE ID='".$data['ID']."'";
341
                                                $DB->query($query);
342
                                        }
343
                                }
344
                        }
345

    
346
                        // Clean DB
347
                        $query="SELECT glpi_licenses.ID 
348
                                FROM glpi_licenses 
349
                                LEFT JOIN glpi_software ON (glpi_software.ID = glpi_licenses.sID ) 
350
                                WHERE glpi_software.ID IS NULL";
351

    
352
                        if ($result = $DB->query($query)) {
353
                                if ($DB->numrows($result)>0) { 
354
                                        while ($data=$DB->fetch_array($result)){
355
                                                $query="DELETE FROM glpi_licenses WHERE ID='".$data['ID']."'";
356
                                                $DB->query($query);
357
                                        }
358
                                }
359
                        }
360

    
361
                        $query = "SELECT glpi_licenses.sID
362
                                FROM glpi_inst_software 
363
                                INNER  JOIN glpi_licenses ON (glpi_inst_software.license = glpi_licenses.ID)
364
                                WHERE glpi_inst_software.cID IN ".$this->item_search[COMPUTER_TYPE];
365
                        if ($result = $DB->query($query)) {
366
                                if ($DB->numrows($result)>0) { 
367
                                        while ($data=$DB->fetch_array($result)){
368
                                                $this->addToBeTransfer(SOFTWARE_TYPE,$data['sID']);
369
                                        }
370
                                }
371
                        }
372
                }
373

    
374
                $this->item_search[SOFTWARE_TYPE]=$this->createSearchConditionUsingArray($this->needtobe_transfer[SOFTWARE_TYPE]);
375

    
376
                $this->item_search[NETWORKING_TYPE]=$this->createSearchConditionUsingArray($this->needtobe_transfer[NETWORKING_TYPE]);
377

    
378
                // Tickets
379
                if ($this->options['keep_tickets']){
380
                        foreach ($this->TICKETS_TYPES as $type)
381
                        if(isset($this->item_search[$type])){
382
                                $query="SELECT DISTINCT ID FROM glpi_tracking
383
                                WHERE device_type='$type' AND computer IN ".$this->item_search[$type];
384
                                if ($result = $DB->query($query)) {
385
                                        if ($DB->numrows($result)>0) { 
386
                                                while ($data=$DB->fetch_array($result)){
387
                                                        $this->addToBeTransfer(TRACKING_TYPE,$data['ID']);
388
                                                }
389
                                        }
390
                                }
391
                        }
392
                }        
393
                $this->item_search[TRACKING_TYPE]=$this->createSearchConditionUsingArray($this->needtobe_transfer[TRACKING_TYPE]);
394

    
395
                // Contract : keep / delete + clean unused / keep unused
396
                if ($this->options['keep_contracts']){
397
                        foreach ($this->CONTRACTS_TYPES as $type)
398
                        if (isset($this->item_search[$type])){
399
                                // Clean DB
400
                                $query="SELECT glpi_contract_device.ID FROM glpi_contract_device 
401
                                        LEFT JOIN ".$LINK_ID_TABLE[$type]." ON (glpi_contract_device.FK_device = ".$LINK_ID_TABLE[$type].".ID ) 
402
                                        WHERE glpi_contract_device.device_type='".$type."' AND ".$LINK_ID_TABLE[$type].".ID IS NULL";
403

    
404
                                if ($result = $DB->query($query)) {
405
                                        if ($DB->numrows($result)>0) { 
406
                                                while ($data=$DB->fetch_array($result)){
407
                                                        $query="DELETE FROM glpi_contract_device WHERE ID='".$data['ID']."'";
408
                                                        $DB->query($query);
409
                                                }
410
                                        }
411
                                }
412

    
413
                                $query="SELECT FK_contract, glpi_contracts.FK_entities, glpi_contracts.recursive" .
414
                                                " FROM glpi_contract_device" .
415
                                                " LEFT JOIN glpi_contracts ON (glpi_contract_device.FK_contract=glpi_contracts.ID)" .
416
                                                " WHERE device_type='$type' AND FK_device IN ".$this->item_search[$type];
417
                                if ($result = $DB->query($query)) {
418
                                        if ($DB->numrows($result)>0) { 
419
                                                while ($data=$DB->fetch_array($result)){
420
                                                        if ($data['recursive'] && in_array($data['FK_entities'], getEntityAncestors($this->to))) {
421
                                                                $this->addNotToBeTransfer(CONTRACT_TYPE,$data['FK_contract']);
422
                                                        } else {
423
                                                                $this->addToBeTransfer(CONTRACT_TYPE,$data['FK_contract']);
424
                                                        }
425
                                                }
426
                                        }
427
                                }
428
                        }
429
                }
430
                $this->item_search[CONTRACT_TYPE]=$this->createSearchConditionUsingArray($this->needtobe_transfer[CONTRACT_TYPE]);
431
                $this->item_recurs[CONTRACT_TYPE]=$this->createSearchConditionUsingArray($this->noneedtobe_transfer[CONTRACT_TYPE]);
432
                // Enterprise (depending of item link) / Contract - infocoms : keep / delete + clean unused / keep unused
433
                
434
                if ($this->options['keep_enterprises']){
435
                        // Clean DB
436
                        $query="SELECT glpi_contract_enterprise.ID FROM glpi_contract_enterprise 
437
                                LEFT JOIN glpi_contracts ON (glpi_contract_enterprise.FK_contract = glpi_contracts.ID ) 
438
                                WHERE glpi_contracts.ID IS NULL";
439

    
440
                        if ($result = $DB->query($query)) {
441
                                if ($DB->numrows($result)>0) { 
442
                                        while ($data=$DB->fetch_array($result)){
443
                                                $query="DELETE FROM glpi_contract_enterprise WHERE ID='".$data['ID']."'";
444
                                                $DB->query($query);
445
                                        }
446
                                }
447
                        }
448

    
449
                        // Clean DB
450
                        $query="SELECT glpi_contract_enterprise.ID FROM glpi_contract_enterprise 
451
                                LEFT JOIN glpi_enterprises ON (glpi_contract_enterprise.FK_enterprise = glpi_enterprises.ID ) 
452
                                WHERE glpi_enterprises.ID IS NULL";
453

    
454
                        if ($result = $DB->query($query)) {
455
                                if ($DB->numrows($result)>0) { 
456
                                        while ($data=$DB->fetch_array($result)){
457
                                                $query="DELETE FROM glpi_contract_enterprise WHERE ID='".$data['ID']."'";
458
                                                $DB->query($query);
459
                                        }
460
                                }
461
                        }
462

    
463
                        // Enterprise Contract
464
                        $query="SELECT DISTINCT FK_enterprise, glpi_enterprises.recursive, glpi_enterprises.FK_entities" .
465
                                        " FROM glpi_contract_enterprise " .
466
                                        " LEFT JOIN glpi_enterprises ON (glpi_enterprises.ID=glpi_contract_enterprise.FK_enterprise) " .
467
                                        " WHERE FK_contract IN ".$this->item_search[CONTRACT_TYPE];
468
                        if ($result = $DB->query($query)) {
469
                                if ($DB->numrows($result)>0) { 
470
                                        while ($data=$DB->fetch_array($result)){
471
                                                if ($data['recursive'] && in_array($data['FK_entities'], getEntityAncestors($this->to))) {
472
                                                        $this->addNotToBeTransfer(ENTERPRISE_TYPE,$data['FK_enterprise']);
473
                                                } else {
474
                                                        $this->addToBeTransfer(ENTERPRISE_TYPE,$data['FK_enterprise']);
475
                                                }
476
                                        }
477
                                }
478
                        }
479
                        // Ticket Enterprise
480
                        $query="SELECT DISTINCT assign_ent, glpi_enterprises.recursive, glpi_enterprises.FK_entities" .
481
                                        " FROM glpi_tracking" .
482
                                        " LEFT JOIN glpi_enterprises ON (glpi_enterprises.ID=glpi_tracking.assign_ent) " .
483
                                        " WHERE assign_ent > 0 AND glpi_tracking.ID IN ".$this->item_search[TRACKING_TYPE];
484
                        if ($result = $DB->query($query)) {
485
                                if ($DB->numrows($result)>0) { 
486
                                        while ($data=$DB->fetch_array($result)){
487
                                                if ($data['recursive'] && in_array($data['FK_entities'], getEntityAncestors($this->to))) {
488
                                                        $this->addNotToBeTransfer(ENTERPRISE_TYPE,$data['assign_ent']);
489
                                                } else {
490
                                                        $this->addToBeTransfer(ENTERPRISE_TYPE,$data['assign_ent']);
491
                                                }
492
                                        }
493
                                }
494
                        }
495

    
496

    
497

    
498
                        // Enterprise infocoms
499
                        if ($this->options['keep_infocoms']){
500
                                foreach ($this->INFOCOMS_TYPES as $type){
501
                                        if (isset($this->item_search[$type])){
502
                                                // Clean DB
503
                                                $query="SELECT glpi_infocoms.ID FROM glpi_infocoms 
504
                                                        LEFT JOIN ".$LINK_ID_TABLE[$type]." ON (glpi_infocoms.FK_device = ".$LINK_ID_TABLE[$type].".ID ) 
505
                                                        WHERE glpi_infocoms.device_type='".$type."' AND ".$LINK_ID_TABLE[$type].".ID IS NULL";
506
                
507
                                                if ($result = $DB->query($query)) {
508
                                                        if ($DB->numrows($result)>0) { 
509
                                                                while ($data=$DB->fetch_array($result)){
510
                                                                        $query="DELETE FROM glpi_infocoms WHERE ID='".$data['ID']."'";
511
                                                                        $DB->query($query);
512
                                                                }
513
                                                        }
514
                                                }
515
        
516
                                                $query="SELECT DISTINCT FK_enterprise, glpi_enterprises.recursive, glpi_enterprises.FK_entities" .
517
                                                                " FROM glpi_infocoms" .
518
                                                                " LEFT JOIN glpi_enterprises ON (glpi_enterprises.ID=glpi_infocoms.FK_enterprise) " .
519
                                                                " WHERE FK_enterprise > 0 AND device_type='$type' AND FK_device IN ".$this->item_search[$type];
520
                                                if ($result = $DB->query($query)) {
521
                                                        if ($DB->numrows($result)>0) { 
522
                                                                while ($data=$DB->fetch_array($result)){
523
                                                                        if ($data['recursive'] && in_array($data['FK_entities'], getEntityAncestors($this->to))) {
524
                                                                                $this->addNotToBeTransfer(ENTERPRISE_TYPE,$data['FK_enterprise']);
525
                                                                        } else {
526
                                                                                $this->addToBeTransfer(ENTERPRISE_TYPE,$data['FK_enterprise']);
527
                                                                        }
528
                                                                }
529
                                                        }
530
                                                }
531
                                        }
532
                                }
533
                        }
534
                }
535
                $this->item_search[ENTERPRISE_TYPE]=$this->createSearchConditionUsingArray($this->needtobe_transfer[ENTERPRISE_TYPE]);
536
                $this->item_recurs[ENTERPRISE_TYPE]=$this->createSearchConditionUsingArray($this->noneedtobe_transfer[ENTERPRISE_TYPE]);
537

    
538
                // Contact / Enterprise : keep / delete + clean unused / keep unused
539
                if ($this->options['keep_contacts']){
540
                        // Clean DB
541
                        $query="SELECT glpi_contact_enterprise.ID FROM glpi_contact_enterprise 
542
                                LEFT JOIN glpi_contacts ON (glpi_contact_enterprise.FK_contact = glpi_contacts.ID ) 
543
                                WHERE glpi_contacts.ID IS NULL";
544

    
545
                        if ($result = $DB->query($query)) {
546
                                if ($DB->numrows($result)>0) { 
547
                                        while ($data=$DB->fetch_array($result)){
548
                                                $query="DELETE FROM glpi_contact_enterprise WHERE ID='".$data['ID']."'";
549
                                                $DB->query($query);
550
                                        }
551
                                }
552
                        }
553
                        // Clean DB
554
                        $query="SELECT glpi_contact_enterprise.ID FROM glpi_contact_enterprise 
555
                                LEFT JOIN glpi_enterprises ON (glpi_contact_enterprise.FK_enterprise = glpi_enterprises.ID ) 
556
                                WHERE glpi_enterprises.ID IS NULL";
557

    
558
                        if ($result = $DB->query($query)) {
559
                                if ($DB->numrows($result)>0) { 
560
                                        while ($data=$DB->fetch_array($result)){
561
                                                $query="DELETE FROM glpi_contact_enterprise WHERE ID='".$data['ID']."'";
562
                                                $DB->query($query);
563
                                        }
564
                                }
565
                        }
566

    
567

    
568
                        // Enterprise Contact
569
                        $query="SELECT DISTINCT FK_contact, glpi_contacts.recursive, glpi_contacts.FK_entities " .
570
                                        " FROM glpi_contact_enterprise" .
571
                                        " LEFT JOIN glpi_contacts ON (glpi_contacts.ID=glpi_contact_enterprise.FK_contact) " .
572
                                        " WHERE FK_enterprise IN ".$this->item_search[ENTERPRISE_TYPE];
573
                        if ($result = $DB->query($query)) {
574
                                if ($DB->numrows($result)>0) { 
575
                                        while ($data=$DB->fetch_array($result)){
576
                                                if ($data['recursive'] && in_array($data['FK_entities'], getEntityAncestors($this->to))) {
577
                                                        $this->addNotToBeTransfer(CONTACT_TYPE,$data['FK_contact']);
578
                                                } else {
579
                                                        $this->addToBeTransfer(CONTACT_TYPE,$data['FK_contact']);
580
                                                }
581
                                        }
582
                                }
583
                        }
584
                }
585
                $this->item_search[CONTACT_TYPE]=$this->createSearchConditionUsingArray($this->needtobe_transfer[CONTACT_TYPE]);
586
                $this->item_recurs[CONTACT_TYPE]=$this->createSearchConditionUsingArray($this->noneedtobe_transfer[CONTACT_TYPE]);
587

    
588
                // Document : keep / delete + clean unused / keep unused
589
                if ($this->options['keep_documents']){
590
                        foreach ($this->DOCUMENTS_TYPES as $type)
591
                        if (isset($this->item_search[$type])){
592
                                // Clean DB
593
                                $query="SELECT glpi_doc_device.ID FROM glpi_doc_device 
594
                                        LEFT JOIN ".$LINK_ID_TABLE[$type]." ON (glpi_doc_device.FK_device = ".$LINK_ID_TABLE[$type].".ID ) 
595
                                        WHERE glpi_doc_device.device_type='".$type."' AND ".$LINK_ID_TABLE[$type].".ID IS NULL";
596
                
597
                                if ($result = $DB->query($query)) {
598
                                        if ($DB->numrows($result)>0) { 
599
                                                while ($data=$DB->fetch_array($result)){
600
                                                        $query="DELETE FROM glpi_doc_device WHERE ID='".$data['ID']."'";
601
                                                        $DB->query($query);
602
                                                }
603
                                        }
604
                                }
605

    
606
                                $query="SELECT FK_doc, glpi_docs.recursive, glpi_docs.FK_entities" .
607
                                                " FROM glpi_doc_device" .
608
                                                " LEFT JOIN glpi_docs ON (glpi_docs.ID=glpi_doc_device.FK_doc) " .
609
                                                " WHERE device_type='$type' AND FK_device IN ".$this->item_search[$type];
610
                                if ($result = $DB->query($query)) {
611
                                        if ($DB->numrows($result)>0) { 
612
                                                while ($data=$DB->fetch_array($result)){
613
                                                        if ($data['recursive'] && in_array($data['FK_entities'], getEntityAncestors($this->to))) {
614
                                                                $this->addNotToBeTransfer(DOCUMENT_TYPE,$data['FK_doc']);
615
                                                        } else {
616
                                                                $this->addToBeTransfer(DOCUMENT_TYPE,$data['FK_doc']);
617
                                                        }
618
                                                }
619
                                        }
620
                                }
621
                        }
622
                }
623
                $this->item_search[DOCUMENT_TYPE]=$this->createSearchConditionUsingArray($this->needtobe_transfer[DOCUMENT_TYPE]);
624
                $this->item_recurs[DOCUMENT_TYPE]=$this->createSearchConditionUsingArray($this->noneedtobe_transfer[DOCUMENT_TYPE]);
625

    
626
                // printer -> cartridges : keep / delete + clean
627
                if ($this->options['keep_cartridges_type']){
628
                        if (isset($this->item_search[PRINTER_TYPE])){
629
                                $query="SELECT FK_glpi_cartridges_type FROM glpi_cartridges
630
                                WHERE FK_glpi_printers IN ".$this->item_search[PRINTER_TYPE];
631
                                if ($result = $DB->query($query)) {
632
                                        if ($DB->numrows($result)>0) { 
633
                                                while ($data=$DB->fetch_array($result)){
634
                                                        $this->addToBeTransfer(CARTRIDGE_TYPE,$data['FK_glpi_cartridges_type']);
635
                                                }
636
                                        }
637
                                }
638
                        }
639
                }
640
                $this->item_search[CARTRIDGE_TYPE]=$this->createSearchConditionUsingArray($this->needtobe_transfer[CARTRIDGE_TYPE]);
641
        }
642

    
643
        /**
644
        * Create IN condition for SQL requests based on a array if ID
645
        *
646
        *@param $array array of ID
647
        *@return string of the IN condition
648
        **/
649
        function createSearchConditionUsingArray($array){
650
                if (is_array($array)&&count($array)){
651
                        $condition="";
652
                        $first=true;
653
                        foreach ($array as $ID){
654
                                if ($first){
655
                                        $first=false;
656
                                } else {
657
                                        $condition.=",";
658
                                }
659
                                $condition.=$ID;
660
                        }
661
                        return "(".$condition.")";
662
                } else {
663
                        return "(-1)";
664
                }
665
        }
666

    
667
        /**
668
        * transfer an item to another item (may be the same) in the new entity
669
        *
670
        *@param $type device type to transfer
671
        *@param $ID ID of the item to transfer
672
        *@param $newID new ID of the ite
673
        *
674
        * Transfer item to a new Item if $ID==$newID : only update FK_entities field : $ID!=$new ID -> copy datas (like template system)
675
        *@return nothing (diplays)
676
        *
677
        **/
678
        function transferItem($type,$ID,$newID){
679
                global $CFG_GLPI,$DB;
680
                
681
                $cinew=new CommonItem();
682
                // Is already transfer ?
683
                if (!isset($this->already_transfer[$type][$ID])){
684
                        // Check computer exists ?
685
                        if ($cinew->getFromDB($type,$newID)){
686

    
687
                                // Manage Ocs links 
688
                                $dataocslink=array();
689
                                $ocs_computer=false;
690
                                if ($type==COMPUTER_TYPE&&$CFG_GLPI['ocs_mode']){
691
                                        $query="SELECT * FROM glpi_ocs_link WHERE glpi_id='$ID'";
692
                                        if ($result=$DB->query($query)){
693
                                                if ($DB->numrows($result)>0){
694
                                                        $dataocslink=$DB->fetch_assoc($result);
695
                                                        $ocs_computer=true;
696
                                                }
697
                                        }
698
                                        
699
                                }
700

    
701
                                // Network connection ? keep connected / keep_disconnected / delete
702
                                if (in_array($type,
703
                                        array(COMPUTER_TYPE,NETWORKING_TYPE,PRINTER_TYPE,MONITOR_TYPE,PERIPHERAL_TYPE,PHONE_TYPE))) {
704
                                        $this->transferNetworkLink($type,$ID,$newID,$ocs_computer);
705
                                }
706
                                // Device : keep / delete : network case : delete if net connection delete in ocs case
707
                                if (in_array($type,array(COMPUTER_TYPE))){
708
                                        $this->transferDevices($type,$ID,$ocs_computer);
709
                                }
710
                                // Reservation : keep / delete
711
                                if (in_array($type,$CFG_GLPI["reservation_types"])){
712
                                        $this->transferReservations($type,$ID,$newID);
713
                                }
714
                                // History : keep / delete
715
                                $this->transferHistory($type,$ID,$newID);
716
                                // Ticket : delete / keep and clean ref / keep and move
717
                                $this->transferTickets($type,$ID,$newID);
718
                                // Infocoms : keep / delete
719
                                if (in_array($type,$this->INFOCOMS_TYPES)) {
720
                                        $this->transferInfocoms($type,$ID,$newID);
721
                                }
722
                        
723
                                if ($type==COMPUTER_TYPE){
724
                                        // Monitor Direct Connect : keep / delete + clean unused / keep unused 
725
                                        $this->transferDirectConnection($type,$ID,MONITOR_TYPE,$ocs_computer);
726
                                        // Peripheral Direct Connect : keep / delete + clean unused / keep unused 
727
                                        $this->transferDirectConnection($type,$ID,PERIPHERAL_TYPE,$ocs_computer);
728
                                        // Phone Direct Connect : keep / delete + clean unused / keep unused 
729
                                        $this->transferDirectConnection($type,$ID,PHONE_TYPE);
730
                                        // Printer Direct Connect : keep / delete + clean unused / keep unused 
731
                                        $this->transferDirectConnection($type,$ID,PRINTER_TYPE,$ocs_computer);
732
                                        // Licence / Software :  keep / delete + clean unused / keep unused 
733
                                        $this->transferSoftwares($type,$ID,$ocs_computer);
734
                                }
735
                                // Computer Direct Connect : delete link if it is the initial transfer item (no recursion)
736
                                if ($this->inittype==$type&&in_array($type,
737
                                        array(PRINTER_TYPE,MONITOR_TYPE,PERIPHERAL_TYPE,PHONE_TYPE))){
738
                                        $this->deleteDirectConnection($type,$ID);
739
                                }
740

    
741
                                // Contract : keep / delete + clean unused / keep unused
742
                                if (in_array($type,$this->CONTRACTS_TYPES)) {
743
                                        $this->transferContracts($type,$ID,$newID);
744
                                }
745

    
746
                                // Contact / Enterprise : keep / delete + clean unused / keep unused
747
                                if ($type==ENTERPRISE_TYPE){
748
                                        $this->transferEnterpriseContacts($ID,$newID);
749
                                }
750

    
751
                                // Document : keep / delete + clean unused / keep unused
752
                                if (in_array($type,$this->DOCUMENTS_TYPES)) {
753
                                        $this->transferDocuments($type,$ID,$newID);
754
                                }
755

    
756
                                // transfer compatible printers
757
                                if ($type==CARTRIDGE_TYPE) {
758
                                        $this->transferCompatiblePrinters($ID,$newID);
759
                                }
760

    
761
                                // Cartridges  and cartridges type linked to printer
762
                                if ($type==PRINTER_TYPE) {
763
                                        $this->transferPrinterCartridges($ID,$newID);
764
                                }
765
                                // TODO Init transfer of contract / docs / software : check unused : if not ? what to do ?
766
                                //if ($this->inittype==$type&&$type==DOCUMENT_TYPE&&$ID==$newID) {
767
                                
768
                                //}
769

    
770
                                // TODO Users ???? : Update right to new entity ?
771
                                // TODO Linked Users ???? : Update right to new entity ?
772
                                
773
                                
774
                                // Transfer Item
775
                                $input=array("ID"=>$newID,'FK_entities' => $this->to);
776
                                // Manage Location dropdown
777
                                if (isset($cinew->obj->fields['location'])){
778
                                        $input['location']=$this->transferDropdownLocation($cinew->obj->fields['location']);
779
                                }
780

    
781
                                // Transfer Document file if exists / Only for copy document
782
                                if ($type==DOCUMENT_TYPE && $ID!=$newID
783
                                        && !empty($cinew->obj->fields['filename'])
784
                                        && $cinew->obj->fields['FK_entities']!=$this->to
785
                                ){
786
                                        $input['filename']=$this->transferDocumentFile($cinew->obj->fields['filename']);
787
                                }
788

    
789
                                $cinew->obj->update($input);
790
                                $this->addToAlreadyTransfer($type,$ID,$newID);
791
                                doHook("item_transfer",array("type"=>$type, "ID" => $ID, "newID"=>$newID));
792
                        }
793
                }
794
        }
795
        
796
        /**
797
        * Add an item to already transfer array
798
        *
799
        *@param $type item type
800
        *@param $ID item original ID
801
        *@param $newID item new ID
802
        **/
803
        function addToAlreadyTransfer($type,$ID,$newID){
804
                if (!isset($this->already_transfer[$type])){
805
                        $this->already_transfer[$type]=array();
806
                }
807
                $this->already_transfer[$type][$ID]=$newID;
808
        }
809
        
810
        /**
811
        * Transfer document file / copy file 
812
        *
813
        *@param $filename filename of the document ot copy
814
        *@return string new doc path
815
        **/
816
        function transferDocumentFile($filename){
817
                if (is_file(GLPI_DOC_DIR."/".$filename)){
818
                        $splitter=split("/",$filename);
819
                        if (count($splitter)==2){
820
                                $dir=$splitter[0];
821
                                $file=$splitter[1];
822
                                // Save message
823
                                $tmp=$_SESSION["MESSAGE_AFTER_REDIRECT"];
824
                                $new_path=getUploadFileValidLocationName($dir,$file,0);
825
                                // Restore message
826
                                $_SESSION["MESSAGE_AFTER_REDIRECT"]=$tmp;
827
                                if (copy(GLPI_DOC_DIR."/".$filename,GLPI_DOC_DIR."/".$new_path)){
828
                                        return $new_path;
829
                                }
830
                        } 
831
                }
832
                return "";
833
        }
834

    
835
        /**
836
        * Transfer location
837
        *
838
        *@param $locID location ID
839
        *@return new location ID
840
        **/
841
        function transferDropdownLocation($locID){
842
                global $DB;
843

    
844
                if ($locID>0){
845
                        if (isset($this->already_transfer['location'][$locID])){
846
                                return $this->already_transfer['location'][$locID];
847
                        } else { // Not already transfer
848
                                // Search init item
849
                                $query="SELECT * FROM glpi_dropdown_locations WHERE ID='$locID'";
850
                                if ($result=$DB->query($query)){
851
                                        if ($DB->numrows($result)){
852
                                                $data=$DB->fetch_array($result);
853
                                                $data=addslashes_deep($data);
854
                                                // Search if the location already exists in the destination entity
855
                                                        $query="SELECT ID FROM glpi_dropdown_locations WHERE FK_entities='".$this->to."' AND completename='".$data['completename']."'";        
856
                                                        if ($result_search=$DB->query($query)){
857
                                                                // Found : -> use it
858
                                                                if ($DB->numrows($result_search)>0){
859
                                                                        $newID=$DB->result($result_search,0,'ID');
860
                                                                        $this->addToAlreadyTransfer('location',$locID,$newID);
861
                                                                        return $newID;
862
                                                                }
863
                                                        }
864
                                                        // Not found : 
865
                                                        $input=array();
866
                                                        $input['tablename']='glpi_dropdown_locations';
867
                                                        $input['FK_entities']=$this->to;
868
                                                        $input['value']=$data['name'];
869
                                                        $input['comments']=$data['comments'];
870
                                                        $input['type']="under";
871
                                                        $input['value2']=0; // parentID
872
                                                        // if parentID>0 : transfer parent ID
873
                                                        if ($data['parentID']>0){
874
                                                                $input['value2']=$this->transferDropdownLocation($data['parentID']);
875
                                                        }
876
                                                        // add item
877
                                                        $newID=addDropdown($input);
878
                                                        $this->addToAlreadyTransfer('location',$locID,$newID);
879
                                                        return $newID;
880
                                        } 
881
                                }
882
                        }
883
                }
884
                return 0;
885
        }
886
        
887
        /**
888
        * Transfer netpoint
889
        *
890
        *@param $netID netpoint ID
891
        *@return new netpoint ID
892
        **/
893
        function transferDropdownNetpoint($netID){
894
                global $DB;
895

    
896
                if ($netID>0){
897
                        if (isset($this->already_transfer['netpoint'][$netID])){
898
                                return $this->already_transfer['netpoint'][$netID];
899
                        } else { // Not already transfer
900
                                // Search init item
901
                                $query="SELECT * FROM glpi_dropdown_netpoint WHERE ID='$netID'";
902
                                if ($result=$DB->query($query)){
903
                                        if ($DB->numrows($result)){
904
                                                $data=$DB->fetch_array($result);
905
                                                $data=addslashes_deep($data);
906
                                                $locID=$this->transferDropdownLocation($data['location']);
907
                                                // Search if the location already exists in the destination entity
908
                                                        $query="SELECT ID FROM glpi_dropdown_netpoint WHERE FK_entities='".$this->to."' AND name='".$data['name']."' AND location='$locID'";        
909
                                                        if ($result_search=$DB->query($query)){
910
                                                                // Found : -> use it
911
                                                                if ($DB->numrows($result_search)>0){
912
                                                                        $newID=$DB->result($result_search,0,'ID');
913
                                                                        $this->addToAlreadyTransfer('netpoint',$netID,$newID);
914
                                                                        return $newID;
915
                                                                }
916
                                                        }
917
                                                        // Not found : 
918
                                                        $input=array();
919
                                                        $input['tablename']='glpi_dropdown_netpoint';
920
                                                        $input['FK_entities']=$this->to;
921
                                                        $input['value']=$data['name'];
922
                                                        $input['comments']=$data['comments'];
923
                                                        $input['type']="under";
924
                                                        $input['value2']=$locID; 
925
                                                        // add item
926
                                                        $newID=addDropdown($input);
927
                                                        $this->addToAlreadyTransfer('netpoint',$netID,$newID);
928
                                                        return $newID;
929
                                        } 
930
                                }
931
                        }
932
                }
933
                return 0;
934
        }        
935
        
936
        /**
937
        * Transfer cartridges of a printer
938
        *
939
        *@param $ID original ID of the printer
940
        *@param $newID new ID of the printer
941
        **/
942
        function transferPrinterCartridges($ID,$newID){
943
                global $DB;
944
                
945
                // Get cartrdiges linked
946
                $query = "SELECT *
947
                        FROM glpi_cartridges 
948
                        WHERE glpi_cartridges.FK_glpi_printers = '$ID'";
949
                if ($result = $DB->query($query)) {
950
                        if ($DB->numrows($result)>0) { 
951
                                $cart=new Cartridge();
952
                                $carttype=new CartridgeType();
953

    
954
                                while ($data=$DB->fetch_array($result)){
955
                                        $need_clean_process=false;
956
                                        // Foreach cartridges
957
                                        // if keep 
958
                                        if ($this->options['keep_cartridges_type']){ 
959
                                                $newcartID=-1;
960
                                                $newcarttypeID=-1;
961
                                                // 1 - Search carttype destination ?
962
                                                // Already transfer carttype : 
963
                                                if (isset($this->already_transfer[CARTRIDGE_TYPE][$data['FK_glpi_cartridges_type']])){
964
                                                        $newcarttypeID=$this->already_transfer[CARTRIDGE_TYPE][$data['FK_glpi_cartridges_type']];
965
                                                } else {
966
                                                        // Not already transfer cartype
967
                                                        $query="SELECT count(*) AS CPT 
968
                                                                FROM glpi_cartridges
969
                                                                WHERE glpi_cartridges.FK_glpi_cartridges_type='".$data['FK_glpi_cartridges_type']."' 
970
                                                                AND glpi_cartridges.FK_glpi_printers > 0 AND glpi_cartridges.FK_glpi_printers NOT IN ".$this->item_search[PRINTER_TYPE];
971
                                                        $result_search=$DB->query($query);
972
                                                        // Is the carttype will be completly transfer ?
973
                                                        if ($DB->result($result_search,0,'CPT')==0){
974
                                                                // Yes : transfer
975
                                                                $need_clean_process=false;
976
                                                                $this->transferItem(CARTRIDGE_TYPE,$data['FK_glpi_cartridges_type'],$data['FK_glpi_cartridges_type']);
977
                                                                $newcarttypeID=$data['FK_glpi_cartridges_type'];
978
                                                        } else {
979
                                                                // No : copy carttype
980
                                                                $need_clean_process=true;
981
                                                                $carttype->getFromDB($data['FK_glpi_cartridges_type']);
982
                                                                // Is existing carttype in the destination entity ?
983
                                                                $query="SELECT * FROM glpi_cartridges_type WHERE FK_entities='".$this->to."' AND name='".addslashes($carttype->fields['name'])."'";
984
                                                                if ($result_search=$DB->query($query)){
985
                                                                        if ($DB->numrows($result_search)>0){
986
                                                                                $newcarttypeID=$DB->result($result_search,0,'ID');
987
                                                                        }
988
                                                                }
989
                                                                // Not found -> transfer copy
990
                                                                if ($newcarttypeID<0){
991
                                                                        // 1 - create new item
992
                                                                        unset($carttype->fields['ID']);
993
                                                                        $input=$carttype->fields;
994
                                                                        $input['FK_entities']=$this->to;
995
                                                                        unset($carttype->fields);
996
                                                                        $newcarttypeID=$carttype->add($input);
997
                                                                        // 2 - transfer as copy
998
                                                                        $this->transferItem(CARTRIDGE_TYPE,$data['FK_glpi_cartridges_type'],$newcarttypeID);
999
                                                                }
1000
                                                                // Founded -> use to link : nothing to do
1001
                                                        }
1002
                                                }
1003
                                                
1004
                                                // Update cartridge if needed
1005
                                                if ($newcarttypeID>0&&$newcarttypeID!=$data['FK_glpi_cartridges_type']){
1006
                                                        $cart->update(array("ID"=>$data['ID'],'FK_glpi_cartridges_type' => $newcarttypeID));                
1007
                                                }
1008
                                        } else { // Do not keep 
1009
                                                // If same printer : delete cartridges
1010
                                                if ($ID==$newID){
1011
                                                        $del_query="DELETE FROM glpi_cartridges 
1012
                                                                WHERE FK_glpi_printers = '$ID'";
1013
                                                        $DB->query($del_query);
1014
                                                }
1015
                                                $need_clean_process=true;
1016
                                        }
1017
                                        // CLean process
1018
                                        if ($need_clean_process&&$this->options['clean_cartridges_type']){
1019
                                                // Clean carttype
1020
                                                $query2 = "SELECT COUNT(*) AS CPT
1021
                                                                FROM glpi_cartridges 
1022
                                                                WHERE FK_glpi_cartridges_type = '" . $data['FK_glpi_cartridges_type'] . "'";
1023
                                                $result2 = $DB->query($query2);
1024
                                                if ($DB->result($result2, 0, 'CPT') == 0) {
1025
                                                        if ($this->options['clean_cartridges_type']==1){ // delete
1026
                                                                $carttype->delete(array ("ID" => $data['FK_glpi_cartridges_type']));
1027
                                                        }
1028
                                                        if ($this->options['clean_cartridges_type']==2){ // purge
1029
                                                                $carttype->delete(array ("ID" => $data['FK_glpi_cartridges_type']),1);
1030
                                                        }
1031
                                                }
1032
                                        }
1033

    
1034
                                }
1035
                        }
1036
                }
1037
        
1038
        }
1039
        
1040
        /**
1041
        * Transfer softwares of a computer
1042
        *
1043
        *@param $type original type of transfered item
1044
        *@param $ID ID of the computer
1045
        *@param $ocs_computer ID of the computer in OCS if imported from OCS
1046
        **/
1047
        function transferSoftwares($type,$ID,$ocs_computer=false){
1048
                global $DB;
1049
                // Get licenses linked
1050
                $query = "SELECT glpi_licenses.sID as softID, glpi_licenses.ID as licID, glpi_inst_software.ID as instID
1051
                        FROM glpi_inst_software 
1052
                        LEFT JOIN glpi_licenses ON (glpi_inst_software.license = glpi_licenses.ID)
1053
                        WHERE glpi_inst_software.cID = '$ID'";
1054
                if ($result = $DB->query($query)) {
1055
                        if ($DB->numrows($result)>0) { 
1056
                                $lic=new License();
1057
                                $soft=new Software();
1058

    
1059
                                while ($data=$DB->fetch_array($result)){
1060
                                        $need_clean_process=false;
1061
                                        // Foreach licenses
1062
                                        // if keep 
1063
                                        if ($this->options['keep_softwares']){ 
1064
                                                if (!empty($data['softID'])&&$data['softID']>0
1065
                                                &&!empty($data['licID'])&&$data['licID']>0
1066
                                                &&!empty($data['instID'])&&$data['instID']>0){
1067
                                                        $newlicID=-1;
1068
                                                        // Already_transfer license
1069
                                                        if (isset($this->already_transfer[LICENSE_TYPE][$data['licID']])){
1070
                                                                // Copy license : update link in inst_software
1071
                                                                if ($this->already_transfer[LICENSE_TYPE][$data['licID']]!=$data['licID']){
1072
                                                                        $newlicID=$this->already_transfer[LICENSE_TYPE][$data['licID']];
1073
                                                                        $need_clean_process=true;
1074
                                                                } 
1075
                                                                // Same license : nothing to do
1076
                                                        } else {
1077
                                                        // Not already transfer license 
1078
                                                                $newsoftID=-1;
1079
                                                                // 1 - Search software destination ?
1080
                                                                // Already transfer soft : 
1081
                                                                if (isset($this->already_transfer[SOFTWARE_TYPE][$data['softID']])){
1082
                                                                        $newsoftID=$this->already_transfer[SOFTWARE_TYPE][$data['softID']];
1083
                                                                } else {
1084
                                                                        // Not already transfer soft
1085
                                                                        $query="SELECT count(*) AS CPT 
1086
                                                                                FROM glpi_inst_software INNER JOIN glpi_licenses ON (glpi_inst_software.license = glpi_licenses.ID)
1087
                                                                                WHERE glpi_licenses.sID='".$data['softID']."' AND glpi_inst_software.cID NOT IN ".$this->item_search[COMPUTER_TYPE];
1088
                                                                        $result_search=$DB->query($query);
1089
                                                                        // Is the software will be completly transfer ?
1090
                                                                        if ($DB->result($result_search,0,'CPT')==0){
1091
                                                                                // Yes : transfer
1092
                                                                                $need_clean_process=false;
1093
                                                                                $this->transferItem(SOFTWARE_TYPE,$data['softID'],$data['softID']);
1094
                                                                                $newsoftID=$data['softID'];
1095
                                                                        } else {
1096
                                                                                // No : copy software
1097
                                                                                $need_clean_process=true;
1098
                                                                                $soft->getFromDB($data['softID']);
1099
                                                                                // Is existing software in the destination entity ?
1100
                                                                                $query="SELECT * FROM glpi_software WHERE FK_entities='".$this->to."' AND name='".addslashes($soft->fields['name'])."'";
1101
                                                                                if ($result_search=$DB->query($query)){
1102
                                                                                        if ($DB->numrows($result_search)>0){
1103
                                                                                                $newsoftID=$DB->result($result_search,0,'ID');
1104
                                                                                        }
1105
                                                                                }
1106
                                                                                // Not found -> transfer copy
1107
                                                                                if ($newsoftID<0){
1108
                                                                                        // 1 - create new item
1109
                                                                                        unset($soft->fields['ID']);
1110
                                                                                        $input=$soft->fields;
1111
                                                                                        $input['FK_entities']=$this->to;
1112
                                                                                        unset($soft->fields);
1113
                                                                                        $newsoftID=$soft->add($input);
1114
                                                                                        // 2 - transfer as copy
1115
                                                                                        $this->transferItem(SOFTWARE_TYPE,$data['softID'],$newsoftID);
1116
                                                                                }
1117
                                                                                // Founded -> use to link : nothing to do
1118
                                                                        }
1119
                                                                }
1120
                                                                // 2 - Transfer licence
1121
                                                                if ($newsoftID>0&&$newsoftID!=$data['softID']){
1122
                                                                // destination soft <> original soft -> copy soft
1123
                                                                        $query="SELECT count(*) AS CPT 
1124
                                                                                FROM glpi_inst_software 
1125
                                                                                WHERE glpi_inst_software.license='".$data['licID']."' AND glpi_inst_software.cID NOT IN ".$this->item_search[COMPUTER_TYPE];
1126
                                                                        $result_search=$DB->query($query);
1127
                                                                        // Is the license will be completly transfer ?
1128
                                                                        if ($DB->result($result_search,0,'CPT')==0){
1129
                                                                                // Yes : transfer license to copy software
1130
                                                                                $lic->update(array("ID"=>$data['licID'],'sID' => $newsoftID));
1131
                                                                                $this->addToAlreadyTransfer(LICENSE_TYPE,$data['licID'],$data['licID']);
1132
                                                                        } else {
1133
                                                                                $lic->getFromDB($data['licID']);
1134
                                                                                // No : Search licence
1135
                                                                                $query="SELECT ID 
1136
                                                                                        FROM glpi_licenses WHERE sID='$newsoftID' AND  version='".addslashes($lic->fields['version'])."' AND serial='".addslashes($lic->fields['serial'])."'";
1137
                                                                                if ($result_search=$DB->query($query)){
1138
                                                                                        if ($DB->numrows($result_search)>0){
1139
                                                                                                $newlicID=$DB->result($result_search,0,'ID');
1140
                                                                                        }
1141
                                                                                }
1142
                                                                                if ($newlicID<0){
1143
                                                                                        // Not found : copy license
1144
                                                                                        unset($lic->fields['ID']);
1145
                                                                                        $input=$lic->fields;
1146
                                                                                        unset($lic->fields);
1147
                                                                                        $input['sID']=$newsoftID;
1148
                                                                                        $newlicID=$lic->add($input);
1149
                                                                                }
1150
                                                                                $this->addToAlreadyTransfer(LICENSE_TYPE,$data['licID'],$newlicID);
1151
                                                                                // Found : use it 
1152
                                                                        }
1153
                                                                } 
1154
                                                                // else destination soft = original soft -> nothing to do / keep links
1155
                                                        }
1156
                                                        // Update inst software if needed
1157
                                                        if ($newlicID>0&&$newlicID!=$data['licID']){
1158
                                                                $query="UPDATE glpi_inst_software SET license='$newlicID' WHERE ID='".$data['instID']."'";
1159
                                                                $DB->query($query);        
1160
                                                        }
1161
                                                } else {
1162
                                                        // Bad inst data : delete them
1163
                                                        $del_query="DELETE FROM glpi_inst_software 
1164
                                                                WHERE ID = '".$data['instID']."'";
1165
                                                        $DB->query($del_query);
1166
                                                        $need_clean_process=false;
1167
                                                }
1168
                                        } else { // Do not keep 
1169
                                                // Delete inst software for computer
1170
                                                $del_query="DELETE FROM glpi_inst_software 
1171
                                                        WHERE ID = '".$data['instID']."'";
1172
                                                $DB->query($del_query);
1173
                                                $need_clean_process=true;
1174
                                                if ($ocs_computer){
1175
                                                        $query="UPDATE glpi_ocs_link SET import_software = NULL WHERE glpi_id='$ID'";
1176
                                                        $DB->query($query);
1177
                                                }
1178
                                        }
1179
                                        // CLean process
1180
                                        if ($need_clean_process&&$this->options['clean_softwares']){
1181
                                                // Clean license
1182
                                                $query2 = "SELECT COUNT(*) AS CPT
1183
                                                                FROM glpi_inst_software 
1184
                                                                WHERE license = '" . $data['licID'] . "'";
1185
                                                $result2 = $DB->query($query2);
1186
                                                if ($DB->result($result2, 0, 'CPT') == 0) {
1187
                                                        $lic->delete(array (
1188
                                                                "ID" => $data['licID']
1189
                                                        ));
1190
                                                }
1191
                                                // Clean software
1192
                                                $query2 = "SELECT COUNT(*) AS CPT
1193
                                                                FROM glpi_licenses 
1194
                                                                WHERE sID = '" . $data['softID'] . "'";
1195
                                                $result2 = $DB->query($query2);
1196
                                                if ($DB->result($result2, 0, 'CPT') == 0) {
1197
                                                        if ($this->options['clean_softwares']==1){ // delete
1198
                                                                $soft->delete(array ("ID" => $data['softID']));
1199
                                                        }
1200
                                                        if ($this->options['clean_softwares']==2){ // purge
1201
                                                                $soft->delete(array ("ID" => $data['softID']),1);
1202
                                                        }
1203
                                                }
1204
                                        }
1205

    
1206
                                }
1207
                        }
1208
                }
1209
        }
1210

    
1211
        /**
1212
        * Transfer contracts
1213
        *
1214
        *@param $type original type of transfered item
1215
        *@param $ID original ID of the contract
1216
        *@param $newID new ID of the contract
1217
        **/
1218
        function transferContracts($type,$ID,$newID){
1219
                global $DB;
1220
                $need_clean_process=false;
1221

    
1222
                // if keep 
1223
                if ($this->options['keep_contracts']){
1224
                        $contract=new Contract();
1225
                        // Get contracts for the item
1226
                        $query="SELECT * FROM glpi_contract_device" .
1227
                                        " WHERE FK_device = '$ID' AND device_type = '$type' AND FK_contract NOT IN ".$this->item_recurs[CONTRACT_TYPE];
1228
                        if ($result = $DB->query($query)) {
1229
                                if ($DB->numrows($result)>0) { 
1230
                                        // Foreach get item 
1231
                                        while ($data=$DB->fetch_array($result)) {
1232
                                                $need_clean_process=false;
1233
                                                $item_ID=$data['FK_contract'];
1234
                                                $newcontractID=-1;
1235
                                                // is already transfer ?
1236
                                                if (isset($this->already_transfer[CONTRACT_TYPE][$item_ID])){
1237
                                                        $newcontractID=$this->already_transfer[CONTRACT_TYPE][$item_ID];
1238
                                                        if ($newcontractID!=$item_ID){
1239
                                                                $need_clean_process=true;
1240
                                                        }
1241
                                                } else {
1242
                                                        // No
1243
                                                        // Can be transfer without copy ? = all linked items need to be transfer (so not copy)
1244
                                                        $canbetransfer=true;
1245
                                                        $query="SELECT DISTINCT device_type FROM glpi_contract_device WHERE FK_contract='$item_ID'";
1246
                                                        
1247
                                                        if ($result_type = $DB->query($query)) {
1248
                                                                if ($DB->numrows($result_type)>0) {
1249
                                                                        while (($data_type=$DB->fetch_array($result_type)) && $canbetransfer) {
1250
                                                                                $dtype=$data_type['device_type'];
1251
                                                                                if (isset($this->item_search[$dtype])){
1252
                                                                                        // No items to transfer -> exists links
1253
                                                                                        $query_search="SELECT count(*) AS CPT 
1254
                                                                                                        FROM glpi_contract_device 
1255
                                                                                                        WHERE FK_contract='$item_ID' AND device_type='$dtype' AND FK_device NOT IN ".$this->item_search[$dtype];
1256
                                                                                        $result_search = $DB->query($query_search);
1257
                                                                                        if ($DB->result($result_search,0,'CPT')>0){
1258
                                                                                                $canbetransfer=false;
1259
                                                                                        }
1260
                                                                                } else {
1261
                                                                                        $canbetransfer=false;
1262
                                                                                }
1263
                                                                        }
1264
                                                                }
1265
                                                        }
1266
                                                        // Yes : transfer 
1267
                                                        if ($canbetransfer){
1268
                                                                $this->transferItem(CONTRACT_TYPE,$item_ID,$item_ID);
1269
                                                                $newcontractID=$item_ID;
1270
                                                        } else {
1271
                                                                $need_clean_process=true;
1272
                                                                $contract->getFromDB($item_ID);
1273
                                                                // No : search contract
1274
                                                                $query="SELECT * FROM glpi_contracts WHERE FK_entities='".$this->to."' AND name='".addslashes($contract->fields['name'])."'";
1275
                                                                if ($result_search=$DB->query($query)){
1276
                                                                        if ($DB->numrows($result_search)>0){
1277
                                                                                $newcontractID=$DB->result($result_search,0,'ID');
1278
                                                                                $this->addToAlreadyTransfer(CONTRACT_TYPE,$item_ID,$newcontractID);
1279
                                                                        }
1280
                                                                }
1281
                                                                // found : use it
1282
                                                                // not found : copy contract
1283
                                                                if ($newcontractID<0){
1284
                                                                        // 1 - create new item
1285
                                                                        unset($contract->fields['ID']);
1286
                                                                        $input=$contract->fields;
1287
                                                                        $input['FK_entities']=$this->to;
1288
                                                                        unset($contract->fields);
1289
                                                                        $newcontractID=$contract->add($input);
1290
                                                                        // 2 - transfer as copy
1291
                                                                        $this->transferItem(CONTRACT_TYPE,$item_ID,$newcontractID);
1292
                                                                }
1293
                                                        }
1294
                                                }
1295
                                                // Update links 
1296
                                                if ($ID==$newID){
1297
                                                        if ($item_ID!=$newcontractID){
1298
                                                                $query="UPDATE glpi_contract_device SET FK_contract = '$newcontractID' WHERE ID='".$data['ID']."'";
1299
                                                                $DB->query($query);
1300
                                                        }
1301
                                                        // Same Item -> update links
1302
                                                } else {
1303
                                                        // Copy Item -> copy links
1304
                                                        if ($item_ID!=$newcontractID){
1305
                                                                $query="INSERT INTO glpi_contract_device (FK_contract,FK_device,device_type) VALUES ('$newcontractID','$newID','$type')";
1306
                                                                $DB->query($query);
1307
                                                        } else { // same contract for new item update link
1308
                                                                $query="UPDATE glpi_contract_device SET FK_device = '$newID' WHERE ID='".$data['ID']."'";
1309
                                                                $DB->query($query);
1310
                                                        }
1311
                                                }
1312
                                                // If clean and unused -> 
1313
                                                if ($need_clean_process&&$this->options['clean_contracts']){
1314
                                                        $query = "SELECT COUNT(*) AS CPT 
1315
                                                                FROM glpi_contract_device 
1316
                                                                WHERE FK_contract='$item_ID'";
1317
                                                        if ($result_remaining=$DB->query($query)){
1318
                                                                if ($DB->result($result_remaining,0,'CPT')==0){
1319
                                                                        if ($this->options['clean_contracts']==1){
1320
                                                                                $contract->delete(array('ID'=>$item_ID));
1321
                                                                        } 
1322
                                                                        if ($this->options['clean_contracts']==2) { // purge
1323
                                                                                $contract->delete(array('ID'=>$item_ID),1);
1324
                                                                        }
1325
                                                                }
1326
                                                        }
1327
                                                }
1328
                                        }
1329
                                }
1330
                        }
1331
                } else {// else unlink
1332
                        $query="DELETE FROM glpi_contract_device WHERE FK_device = '$ID' AND device_type = '$type'";
1333
                        $DB->query($query);
1334
                }
1335

    
1336
        }
1337

    
1338
        /**
1339
        * Transfer documents
1340
        *
1341
        *@param $type original type of transfered item
1342
        *@param $ID original ID of the document
1343
        *@param $newID new ID of the document
1344
        **/
1345
        function transferDocuments($type,$ID,$newID){
1346
                global $DB;
1347
                $need_clean_process=false;
1348

    
1349
                // if keep 
1350
                if ($this->options['keep_documents']){
1351
                        $document=new Document();
1352
                        // Get contracts for the item
1353
                        $query="SELECT * FROM glpi_doc_device WHERE FK_device = '$ID' AND device_type = '$type' AND FK_doc NOT IN ".$this->item_recurs[DOCUMENT_TYPE];
1354
                        if ($result = $DB->query($query)) {
1355
                                if ($DB->numrows($result)>0) { 
1356
                                        // Foreach get item 
1357
                                        while ($data=$DB->fetch_array($result)) {
1358
                                                $need_clean_process=false;
1359
                                                $item_ID=$data['FK_doc'];
1360
                                                $newdocID=-1;
1361
                                                // is already transfer ?
1362
                                                if (isset($this->already_transfer[DOCUMENT_TYPE][$item_ID])){
1363
                                                        $newdocID=$this->already_transfer[DOCUMENT_TYPE][$item_ID];
1364
                                                        if ($newdocID!=$item_ID){
1365
                                                                $need_clean_process=true;
1366
                                                        }
1367
                                                } else {
1368
                                                        // No
1369
                                                        // Can be transfer without copy ? = all linked items need to be transfer (so not copy)
1370
                                                        $canbetransfer=true;
1371
                                                        $query="SELECT DISTINCT device_type FROM glpi_doc_device WHERE FK_doc='$item_ID'";
1372
                                                        if ($result_type = $DB->query($query)) {
1373
                                                                if ($DB->numrows($result_type)>0) {
1374
                                                                        while (($data_type=$DB->fetch_array($result_type)) && $canbetransfer) {
1375
                                                                                $dtype=$data_type['device_type'];
1376
                                                                                if (isset($this->item_search[$dtype])) {
1377
                                                                                        // No items to transfer -> exists links
1378
                                                                                        $query_search="SELECT count(*) AS CPT 
1379
                                                                                                        FROM glpi_doc_device 
1380
                                                                                                        WHERE FK_doc='$item_ID' AND device_type='$dtype' AND FK_device NOT IN ".$this->item_search[$dtype];
1381
                                                                                        // contacts, contracts, and enterprises are linked as device. 
1382
                                                                                        if (isset($this->item_recurs[$dtype])) {
1383
                                                                                                $query_search .= " AND FK_device NOT IN ".$this->item_recurs[$dtype];
1384
                                                                                        }
1385

    
1386
                                                                                        $result_search = $DB->query($query_search);
1387
                                                                                        if ($DB->result($result_search,0,'CPT')>0){
1388
                                                                                                $canbetransfer=false;
1389
                                                                                        }
1390
                                                                                } 
1391
                                                                        }
1392
                                                                }
1393
                                                        }
1394

    
1395
                                                        
1396
                                                        // Yes : transfer 
1397
                                                        if ($canbetransfer){
1398
                                                                $this->transferItem(DOCUMENT_TYPE,$item_ID,$item_ID);
1399
                                                                $newdocID=$item_ID;
1400
                                                        } else {
1401
                                                                $need_clean_process=true;
1402
                                                                $document->getFromDB($item_ID);
1403
                                                                // No : search contract
1404
                                                                $query="SELECT * FROM glpi_docs WHERE FK_entities='".$this->to."' AND name='".addslashes($document->fields['name'])."'";
1405
                                                                if ($result_search=$DB->query($query)){
1406
                                                                        if ($DB->numrows($result_search)>0){
1407
                                                                                $newdocID=$DB->result($result_search,0,'ID');
1408
                                                                                $this->addToAlreadyTransfer(DOCUMENT_TYPE,$item_ID,$newdocID);
1409
                                                                        }
1410
                                                                }
1411

    
1412
                                                                // found : use it
1413
                                                                // not found : copy contract
1414
                                                                if ($newdocID<0){
1415
                                                                        // 1 - create new item
1416
                                                                        unset($document->fields['ID']);
1417
                                                                        $input=$document->fields;
1418
                                                                        // Not set new entity Do by transferItem
1419
                                                                        //$input['FK_entities']=$this->to;
1420
                                                                        unset($document->fields);
1421
                                                                        $newdocID=$document->add($input);
1422
                                                                        // 2 - transfer as copy
1423
                                                                        $this->transferItem(DOCUMENT_TYPE,$item_ID,$newdocID);
1424
                                                                }
1425
                                                        }
1426
                                                }
1427
                                                // Update links 
1428
                                                if ($ID==$newID){
1429
                                                        if ($item_ID!=$newdocID){
1430
                                                                $query="UPDATE glpi_doc_device SET FK_doc = '$newdocID' WHERE ID='".$data['ID']."'";
1431
                                                                $DB->query($query);
1432
                                                        }
1433
                                                        // Same Item -> update links
1434
                                                } else {
1435
                                                        // Copy Item -> copy links
1436
                                                        if ($item_ID!=$newdocID){
1437
                                                                $query="INSERT INTO glpi_doc_device (FK_doc,FK_device,device_type) VALUES ('$newdocID','$newID','$type')";
1438
                                                                $DB->query($query);
1439
                                                        } else { // same doc for new item update link
1440
                                                                $query="UPDATE glpi_doc_device SET FK_device = '$newID' WHERE ID='".$data['ID']."'";
1441
                                                                $DB->query($query);
1442
                                                        }
1443
                                                }
1444
                                                // If clean and unused -> 
1445
                                                if ($need_clean_process&&$this->options['clean_documents']){
1446
                                                        $query = "SELECT COUNT(*) AS CPT 
1447
                                                                FROM glpi_doc_device 
1448
                                                                WHERE FK_doc='$item_ID'";
1449
                                                        if ($result_remaining=$DB->query($query)){
1450
                                                                if ($DB->result($result_remaining,0,'CPT')==0){
1451
                                                                        if ($this->options['clean_documents']==1){
1452
                                                                                $document->delete(array('ID'=>$item_ID));
1453
                                                                        } 
1454
                                                                        if ($this->options['clean_documents']==2) { // purge
1455
                                                                                $document->delete(array('ID'=>$item_ID),1);
1456
                                                                        }
1457
                                                                }
1458
                                                        }
1459
                                                }
1460
                                        }
1461
                                }
1462
                        }
1463
                } else {// else unlink
1464
                        $query="DELETE FROM glpi_doc_device WHERE FK_device = '$ID' AND device_type = '$type'";
1465
                        $DB->query($query);
1466
                }
1467

    
1468
        }
1469

    
1470
        /**
1471
        * Delete direct connection for a linked item 
1472
        *
1473
        *@param $type original type of transfered item
1474
        *@param $ID ID of the item
1475
        *@param $link_type type of the linked items to transfer
1476
        *@param $ocs_computer if computer type OCS ID of the item if available
1477
        **/
1478
        function transferDirectConnection($type,$ID,$link_type,$ocs_computer=false){
1479
                global $DB,$LINK_ID_TABLE;
1480
                // Only same Item case : no duplication of computers
1481
                // Default : delete
1482
                $keep=0;
1483
                $clean=0;
1484
                $ocs_field="";
1485

    
1486
                switch ($link_type){
1487
                        case PRINTER_TYPE:
1488
                                $keep=$this->options['keep_dc_printer'];
1489
                                $clean=$this->options['clean_dc_printer'];
1490
                                $ocs_field="import_printer";
1491
                                break;
1492
                        case MONITOR_TYPE:
1493
                                $keep=$this->options['keep_dc_monitor'];
1494
                                $clean=$this->options['clean_dc_monitor'];
1495
                                $ocs_field="import_monitor";
1496
                                break;
1497
                        case PERIPHERAL_TYPE:
1498
                                $keep=$this->options['keep_dc_peripheral'];
1499
                                $clean=$this->options['clean_dc_peripheral'];
1500
                                $ocs_field="import_peripheral";
1501
                                break;
1502
                        case PHONE_TYPE:
1503
                                $keep=$this->options['keep_dc_phone'];
1504
                                $clean=$this->options['clean_dc_phone'];
1505
                                break;
1506
                }
1507

    
1508
                $ci=new CommonItem();
1509
                // Get connections
1510
                $query = "SELECT * 
1511
                        FROM glpi_connect_wire 
1512
                        WHERE end2='$ID' AND type='".$link_type."'";
1513
                if ($result = $DB->query($query)) {
1514
                        if ($DB->numrows($result)!=0) { 
1515
                                // Foreach get item 
1516
                                while ($data=$DB->fetch_array($result)) {
1517
                                        $item_ID=$data['end1'];
1518
                                        if ($ci->getFromDB($link_type,$item_ID)) {
1519
                                                // If global :
1520
                                                if ($ci->obj->fields['is_global']==1){
1521
                                                        $need_clean_process=false;
1522
                                                        // if keep 
1523
                                                        if ($keep){
1524
                                                                $newID=-1;
1525
                                                                // Is already transfer ? 
1526
                                                                if (isset($this->already_transfer[$link_type][$item_ID])){
1527
                                                                        $newID=$this->already_transfer[$link_type][$item_ID];
1528
                                                                        // Already transfer as a copy : need clean process
1529
                                                                        if ($newID!=$item_ID){
1530
                                                                                $need_clean_process=true;
1531
                                                                        }
1532
                                                                } else { // Not yet tranfer
1533
                                                                        // Can be managed like a non global one ? = all linked computers need to be transfer (so not copy)
1534
                                                                        $query="SELECT count(*) AS CPT FROM glpi_connect_wire WHERE type='".$link_type."' AND end1='$item_ID' AND end2 NOT IN ".$this->item_search[COMPUTER_TYPE];
1535
                                                                        $result_search=$DB->query($query);
1536
                                                                        // All linked computers need to be transfer -> use unique transfer system
1537
                                                                        if ($DB->result($result_search,0,'CPT')==0){
1538
                                                                                
1539
                                                                                $need_clean_process=false;
1540
                                                                                $this->transferItem($link_type,$item_ID,$item_ID);
1541
                                                                                $newID=$item_ID;
1542
                                                                        } else { // else Transfer by Copy
1543
                                                                                $need_clean_process=true;
1544
                                                                                // Is existing global item in the destination entity ?
1545
                                                                                $query="SELECT * FROM ".$LINK_ID_TABLE[$link_type]." WHERE is_global='1' AND FK_entities='".$this->to."' AND name='".addslashes($ci->getField('name'))."'";
1546
                                                                                if ($result_search=$DB->query($query)){
1547
                                                                                        if ($DB->numrows($result_search)>0){
1548
                                                                                                $newID=$DB->result($result_search,0,'ID');
1549
                                                                                                $this->addToAlreadyTransfer($link_type,$item_ID,$newID);
1550
                                                                                        }
1551
                                                                                }
1552
                                                                                // Not found -> transfer copy
1553
                                                                                if ($newID<0){
1554
                                                                                        // 1 - create new item
1555
                                                                                        unset($ci->obj->fields['ID']);
1556
                                                                                        $input=$ci->obj->fields;
1557
                                                                                        $input['FK_entities']=$this->to;
1558
                                                                                        unset($ci->obj->fields);
1559
                                                                                        $newID=$ci->obj->add($input);
1560
                                                                                        // 2 - transfer as copy
1561
                                                                                        $this->transferItem($link_type,$item_ID,$newID);
1562
                                                                                }
1563
                                                                                // Founded -> use to link : nothing to do
1564
                                                                        }
1565
                                                                }
1566
                                                                // Finish updated link if needed
1567
                                                                if ($newID>0&&$newID!=$item_ID){
1568
                                                                        $query = "UPDATE glpi_connect_wire 
1569
                                                                        SET end1='$newID' WHERE ID = '".$data['ID']."' ";        
1570
                                                                        $DB->query($query);
1571
                                                                }
1572
                                                        } else {
1573
                                                                // Else delete link
1574
                                                                
1575
                                                                // Call Disconnect for global device (no disconnect behavior, but history )
1576
                                                                Disconnect($data['ID'],1,false);
1577

    
1578
                                                                $need_clean_process=true;
1579
                                                                // OCS clean link
1580
                                                                if ($ocs_computer&&!empty($ocs_field)){
1581
                                                                        $query="UPDATE glpi_ocs_link SET `$ocs_field` = NULL WHERE glpi_id='$ID'";
1582
                                                                        $DB->query($query);
1583
                                                                }
1584

    
1585
                                                        }
1586
                                                        // If clean and not linked dc -> delete
1587
                                                        if ($need_clean_process&&$clean){
1588
                                                                $query = "SELECT COUNT(*) AS CPT
1589
                                                                        FROM glpi_connect_wire 
1590
                                                                        WHERE end1='$item_ID' AND type='".$link_type."'";
1591
                                                                if ($result_dc=$DB->query($query)){
1592
                                                                        if ($DB->result($result_dc,0,'CPT')==0){
1593
                                                                                if ($clean==1){
1594
                                                                                        $ci->obj->delete(array('ID'=>$item_ID));
1595
                                                                                } 
1596
                                                                                if ($clean==2) { // purge
1597
                                                                                        $ci->obj->delete(array('ID'=>$item_ID),1);
1598
                                                                                }
1599
                                                                        }
1600
                                                                }
1601
                                                        }
1602
                                                } else { // If unique : 
1603
                                                        //if keep -> transfer list else unlink
1604
                                                        if ($keep){
1605
                                                                $this->transferItem($link_type,$item_ID,$item_ID);
1606
                                                        } else {
1607
                                                                // Else delete link (apply disconnect behavior)
1608
                                                                Disconnect($data['ID']);
1609

    
1610
                                                                //if clean -> delete
1611
                                                                if ($clean==1){
1612
                                                                        $ci->obj->delete(array('ID'=>$item_ID));
1613
                                                                }
1614
                                                                else if ($clean==2){ // purge
1615
                                                                        $ci->obj->delete(array('ID'=>$item_ID),1);
1616
                                                                }
1617
                                                                if ($ocs_computer&&!empty($ocs_field)){
1618
                                                                        $query="UPDATE glpi_ocs_link SET `$ocs_field` = NULL WHERE glpi_id='$ID'";
1619
                                                                        $DB->query($query);
1620
                                                                }
1621
                                                        }
1622
                                                }
1623
                                        } else {
1624
                                                // Unexisting item / Force disconnect
1625
                                                Disconnect($data['ID'],0,false);
1626
                                        }
1627
                                }
1628
                        }
1629
                }        
1630
        }
1631

    
1632
        /**
1633
        * Delete direct connection for a linked item 
1634
        *
1635
        *@param $ID ID of the item
1636
        *@param $type device type
1637
        **/
1638
        function deleteDirectConnection($type,$ID){
1639
                global $DB;
1640
                // Delete Direct connection to computers for item type 
1641
                $query = "SELECT * 
1642
                        FROM glpi_connect_wire 
1643
                        WHERE end1 = '$ID' AND type = '".$type."'";
1644
                $result = $DB->query($query);
1645
        }
1646

    
1647
        /**
1648
        * Transfer tickets
1649
        *
1650
        *@param $type original type of transfered item
1651
        *@param $ID original ID of the ticket
1652
        *@param $newID new ID of the ticket
1653
        **/
1654
        function transferTickets($type,$ID,$newID){
1655
                global $DB;
1656
                $job= new Job();
1657

    
1658
                $query = "SELECT ID, assign_ent
1659
                        FROM glpi_tracking 
1660
                        WHERE computer = '$ID' AND device_type = '$type'";
1661
                if ($result = $DB->query($query)) {
1662
                        if ($DB->numrows($result)!=0) { 
1663
                                switch ($this->options['keep_tickets']){
1664
                                        // Transfer
1665
                                        case 2: 
1666
                                                // Same Item / Copy Item -> update entity
1667
                                                while ($data=$DB->fetch_array($result)) {
1668
                                                        $assign_ent=0;
1669
                                                        if ($data['assign_ent']>0){
1670
                                                                $assign_ent=$this->transferSingleEnterprise($data['assign_ent']);
1671
                                                        }
1672
                                                        $job->update(array("ID"=>$data['ID'],'FK_entities' => $this->to, 'computer'=>$newID, 'device_type'=>$type, 'assign_ent'=>$assign_ent));
1673
                                                        $this->addToAlreadyTransfer(TRACKING_TYPE,$data['ID'],$data['ID']);
1674
                                                }
1675
                                        break;
1676
                                        // Clean ref : keep ticket but clean link
1677
                                        case 1: 
1678
                                                // Same Item / Copy Item : keep and clean ref
1679
                                                while ($data=$DB->fetch_array($result)) {
1680
                                                        $assign_ent=0;
1681
                                                        if ($data['assign_ent']>0){
1682
                                                                $assign_ent=$this->transferSingleEnterprise($data['assign_ent']);
1683
                                                        }
1684

    
1685
                                                        $job->update(array("ID"=>$data['ID'],'device_type' => 0, 'computer'=>0, 'assign_ent'=>$assign_ent));
1686
                                                        $this->addToAlreadyTransfer(TRACKING_TYPE,$data['ID'],$data['ID']);
1687
                                                }
1688
                                        break;
1689
                                        // Delete
1690
                                        case 0:
1691
                                                // Same item -> delete
1692
                                                if ($ID==$newID){
1693
                                                        while ($data=$DB->fetch_array($result)) {
1694
                                                                $job->delete(array('ID'=>$data['ID']));
1695
                                                        }
1696
                                                }
1697
                                                // Copy Item : nothing to do
1698
                                        break;
1699
                                }
1700
                        }
1701
                }
1702
        }
1703

    
1704
        /**
1705
        * Transfer history
1706
        *
1707
        *@param $type original type of transfered item
1708
        *@param $ID original ID of the history
1709
        *@param $newID new ID of the history
1710
        **/
1711
        function transferHistory($type,$ID,$newID){
1712
                global $DB;
1713

    
1714
                switch ($this->options['keep_history']){
1715
                        // delete
1716
                        case 0 :  
1717
                                // Same item -> delete
1718
                                if ($ID==$newID){ 
1719
                                        $query = "DELETE FROM glpi_history 
1720
                                                WHERE device_type = '$type' AND FK_glpi_device = '$ID'";
1721
                                        $result = $DB->query($query);
1722
                                }
1723
                                // Copy -> nothing to do
1724
                                break;
1725
                        // Keep history
1726
                        case 1 :        
1727
                        default : 
1728
                                // Copy -> Copy datas 
1729
                                if ($ID!=$newID){
1730
                                        $query = "SELECT * FROM glpi_history 
1731
                                                WHERE device_type = '$type' AND FK_glpi_device = '$ID'";
1732
                                        $result=$DB->query($query);
1733
                                        if ($result = $DB->query($query)) {
1734
                                                if ($DB->numrows($result)!=0) { 
1735
                                                        while ($data=$DB->fetch_array($result)) {
1736
                                                                $data = addslashes_deep($data);
1737
                                                                $query = "INSERT INTO glpi_history
1738
                                                                (FK_glpi_device, device_type, device_internal_type, linked_action, user_name, date_mod, id_search_option, old_value, new_value)  
1739
                                                                VALUES
1740
                                                                ('$newID','$type','".$data['device_internal_type']."','".$data['linked_action']."','". $data['user_name']."', '".$data['date_mod']."', '".$data['id_search_option']."', '".$data['old_value']."', '".$data['new_value']."');";
1741
                                                                $DB->query($query);
1742
                                                        }
1743
                                                }
1744
                                        }
1745
                                }
1746
                                // Same item -> nothing to do
1747
                                break;
1748
                }
1749
        }
1750
        /**
1751
        * Transfer compatible printers for a cartridge type
1752
        *
1753
        *@param $ID original ID of the cartridge type
1754
        *@param $newID new ID of the cartridge type
1755
        **/
1756
        function transferCompatiblePrinters($ID,$newID){
1757
                global $DB;
1758
                if ($ID!=$newID){
1759
                        
1760
                        $query="SELECT * FROM glpi_cartridges_assoc WHERE FK_glpi_cartridges_type='$ID'";
1761
                        if ($result = $DB->query($query)) {
1762
                                if ($DB->numrows($result)!=0) { 
1763
                                        
1764
                                        $cartype=new CartridgeType();
1765
                                        while ($data=$DB->fetch_array($result)) {
1766
                                                $data = addslashes_deep($data);
1767
                                                $cartype->addCompatibleType($newID,$data["FK_glpi_dropdown_model_printers"]);
1768
                                        }
1769
                                }
1770
                        }
1771
                        
1772
                }
1773
        }
1774

    
1775
        /**
1776
        * Transfer infocoms of an item
1777
        *
1778
        *@param $type type of the item to transfer
1779
        *@param $ID original ID of the item 
1780
        *@param $newID new ID of the item
1781
        **/
1782
        function transferInfocoms($type,$ID,$newID){
1783
                global $DB;
1784

    
1785
                $ic=new Infocom();
1786
                if ($ic->getFromDBforDevice($type,$ID)){
1787
                        switch ($this->options['keep_infocoms']){
1788
                                // delete
1789
                                case 0 :  
1790
                                        // Same item -> delete
1791
                                        if ($ID==$newID){ 
1792
                                                $query = "DELETE FROM glpi_infocoms 
1793
                                                        WHERE device_type = '$type' AND FK_device = '$ID'";
1794
                                                $result = $DB->query($query);
1795
                                        }
1796
                                        // Copy : nothing to do
1797
                                        break;
1798
                                // Keep
1799
                                case 1 : 
1800
                                default :
1801
                                        // transfert enterprise 
1802
                                        $FK_enterprise=0; 
1803
                                        if ($ic->fields['FK_enterprise']>0){
1804
                                                $FK_enterprise=$this->transferSingleEnterprise($ic->fields['FK_enterprise']);
1805
                                        }
1806
                                        // Copy : copy infocoms
1807
                                        if ($ID!=$newID){
1808
                                                // Copy items
1809
                                                $input=$ic->fields;
1810
                                                $input['FK_device']=$newID;
1811
                                                $input['FK_enterprise']=$FK_enterprise;
1812
                                                unset($input['ID']);
1813
                                                unset($ic->fields);
1814
                                                $ic->add($input);
1815
                                        } else {
1816
                                                // Same Item : manage only enterprise move
1817
                                                // Update enterprise
1818
                                                if ($FK_enterprise>0 && $FK_enterprise!=$ic->fields['FK_enterprise']){
1819
                                                        $ic->update(array('ID'=>$ic->fields['ID'],'FK_enterprise'=>$FK_enterprise));
1820
                                                }
1821
                                        }
1822
                                        break;
1823
                        }
1824
                }
1825
        }
1826
        /**
1827
        * Transfer an enterprise
1828
        *
1829
        *@param $ID ID of the enterprise
1830
        **/
1831
        function transferSingleEnterprise($ID){
1832
                global $DB;
1833
                // TODO clean system : needed ?
1834
                $ent=new Enterprise();
1835
                if ($this->options['keep_enterprises']&&$ent->getFromDB($ID)){
1836
                        if (isset($this->noneedtobe_transfer[ENTERPRISE_TYPE][$ID])) {
1837
                                // recursive enterprise
1838
                                return $ID;
1839
                        } else if (isset($this->already_transfer[ENTERPRISE_TYPE][$ID])){
1840
                                // Already transfer
1841
                                return $this->already_transfer[ENTERPRISE_TYPE][$ID];
1842
                        } else {
1843
                                $newID=-1;
1844
                                // Not already transfer
1845
                                $links_remaining=0;
1846
                                // All linked items need to be transfer so transfer enterprise ?
1847
                                // Search for contract
1848
                                $query="SELECT count(*) AS CPT FROM glpi_contract_enterprise WHERE FK_enterprise='$ID' AND FK_contract NOT IN ".$this->item_search[CONTRACT_TYPE];
1849
                                $result_search=$DB->query($query);
1850
                                $links_remaining=$DB->result($result_search,0,'CPT');
1851

    
1852
                                if ($links_remaining==0){
1853
                                        // Search for infocoms
1854
                                        if ($this->options['keep_infocoms']){
1855
                                                foreach ($this->INFOCOMS_TYPES as $type){
1856
                                                        $query="SELECT count(*) AS CPT FROM glpi_infocoms
1857
                                                                WHERE FK_enterprise='$ID' AND device_type='$type' AND FK_device NOT IN ".$this->item_search[$type];
1858
                                                        if ($result_search = $DB->query($query)) {
1859
                                                                $links_remaining+=$DB->result($result_search,0,'CPT');
1860
                                                        }
1861
                                                }
1862
                                        }                                        
1863
                                }
1864
                                // All linked items need to be transfer -> use unique transfer system
1865
                                if ($links_remaining==0){
1866
                                        $this->transferItem(ENTERPRISE_TYPE,$ID,$ID);
1867
                                        $newID=$ID;
1868
                                } else { // else Transfer by Copy
1869
                                        // Is existing item in the destination entity ?
1870
                                        $query="SELECT * FROM glpi_enterprises WHERE FK_entities='".$this->to."' AND name='".addslashes($ent->fields['name'])."'";
1871
                                        if ($result_search=$DB->query($query)){
1872
                                                if ($DB->numrows($result_search)>0){
1873
                                                        $newID=$DB->result($result_search,0,'ID');
1874
                                                        $this->addToAlreadyTransfer(ENTERPRISE_TYPE,$ID,$newID);
1875
                                                }
1876
                                        }
1877
                                        // Not found -> transfer copy
1878
                                        if ($newID<0){
1879
                                                // 1 - create new item
1880
                                                unset($ent->fields['ID']);
1881
                                                $input=$ent->fields;
1882
                                                $input['FK_entities']=$this->to;
1883
                                                unset($ent->fields);
1884
                                                $newID=$ent->add($input);
1885
                                                // 2 - transfer as copy
1886
                                                $this->transferItem(ENTERPRISE_TYPE,$ID,$newID);
1887
                                        }
1888
                                        // Founded -> use to link : nothing to do
1889
                                }        
1890
                                return $newID;        
1891
                        }
1892
                } else {
1893
                        return 0;
1894
                }
1895
        }
1896

    
1897
        /**
1898
        * Transfer contacts of an enterprise
1899
        *
1900
        *@param $ID original ID of the enterprise
1901
        *@param $newID new ID of the enterprise
1902
        **/
1903
        function transferEnterpriseContacts($ID,$newID){
1904
                global $DB;
1905
                $need_clean_process=false;
1906
                // if keep 
1907
                if ($this->options['keep_contacts']){
1908
                        $contact=new Contact();
1909
                        // Get contracts for the item
1910
                        $query="SELECT * FROM glpi_contact_enterprise" .
1911
                                        " WHERE FK_enterprise = '$ID' AND FK_contact NOT IN " . $this->item_recurs[CONTACT_TYPE];
1912
                        if ($result = $DB->query($query)) {
1913
                                if ($DB->numrows($result)>0) { 
1914
                                        // Foreach get item 
1915
                                        while ($data=$DB->fetch_array($result)) {
1916
                                                $need_clean_process=false;
1917
                                                $item_ID=$data['FK_contact'];
1918
                                                $newcontactID=-1;
1919
                                                // is already transfer ?
1920
                                                if (isset($this->already_transfer[CONTACT_TYPE][$item_ID])){
1921
                                                        $newcontactID=$this->already_transfer[CONTACT_TYPE][$item_ID];
1922
                                                        if ($newcontactID!=$item_ID){
1923
                                                                $need_clean_process=true;
1924
                                                        }
1925
                                                } else {
1926
                                                        $canbetransfer=true;
1927
                                                        // Transfer enterprise : is the contact used for another enterprise ?
1928
                                                        if ($ID==$newID){
1929
                                                                $query_search="SELECT count(*) AS CPT " .
1930
                                                                                " FROM glpi_contact_enterprise" .
1931
                                                                                " WHERE FK_contact='$item_ID' " .
1932
                                                                                " AND FK_enterprise NOT IN ".$this->item_search[ENTERPRISE_TYPE] .
1933
                                                                                " AND FK_enterprise NOT IN ".$this->item_recurs[ENTERPRISE_TYPE];
1934
                                                                $result_search = $DB->query($query_search);
1935
                                                                if ($DB->result($result_search,0,'CPT')>0){
1936
                                                                        $canbetransfer=false;
1937
                                                                }
1938
                                                        }
1939

    
1940
                                                        // Yes : transfer 
1941
                                                        if ($canbetransfer){
1942
                                                                $this->transferItem(CONTACT_TYPE,$item_ID,$item_ID);
1943
                                                                $newcontactID=$item_ID;
1944
                                                        } else {
1945
                                                                $need_clean_process=true;
1946
                                                                $contact->getFromDB($item_ID);
1947
                                                                // No : search contract
1948
                                                                $query="SELECT * FROM glpi_contacts WHERE FK_entities='".$this->to."' AND name='".addslashes($contact->fields['name'])."' AND firstname='".addslashes($contact->fields['firstname'])."'";
1949
                                                                if ($result_search=$DB->query($query)){
1950
                                                                        if ($DB->numrows($result_search)>0){
1951
                                                                                $newcontactID=$DB->result($result_search,0,'ID');
1952
                                                                                $this->addToAlreadyTransfer(CONTACT_TYPE,$item_ID,$newcontactID);
1953
                                                                        }
1954
                                                                }
1955
                                                                // found : use it
1956
                                                                // not found : copy contract
1957
                                                                if ($newcontactID<0){
1958
                                                                        // 1 - create new item
1959
                                                                        unset($contact->fields['ID']);
1960
                                                                        $input=$contact->fields;
1961
                                                                        $input['FK_entities']=$this->to;
1962
                                                                        unset($contact->fields);
1963
                                                                        $newcontactID=$contact->add($input);
1964
                                                                        // 2 - transfer as copy
1965
                                                                        $this->transferItem(CONTACT_TYPE,$item_ID,$newcontactID);
1966
                                                                }
1967
                                                        }
1968
                                                }
1969
                                                // Update links 
1970
                                                if ($ID==$newID){
1971
                                                        if ($item_ID!=$newcontactID){
1972
                                                                $query="UPDATE glpi_contact_enterprise SET FK_contact = '$newcontactID' WHERE ID='".$data['ID']."'";
1973
                                                                $DB->query($query);
1974
                                                        }
1975
                                                        // Same Item -> update links
1976
                                                } else {
1977
                                                        // Copy Item -> copy links
1978
                                                        if ($item_ID!=$newcontactID){
1979
                                                                $query="INSERT INTO glpi_contact_enterprise (FK_contact,FK_enterprise) VALUES ('$newcontactID','$newID')";
1980
                                                                $DB->query($query);
1981
                                                        } else { // transfer contact but copy enterprise : update link
1982
                                                                $query="UPDATE glpi_contact_enterprise SET FK_enterprise = '$newID' WHERE ID='".$data['ID']."'";
1983
                                                                $DB->query($query);
1984
                                                        }
1985
                                                }
1986
                                                // If clean and unused -> 
1987
                                                if ($need_clean_process&&$this->options['clean_contacts']){
1988
                                                        $query = "SELECT COUNT(*) AS CPT
1989
                                                                FROM glpi_contact_enterprise 
1990
                                                                WHERE FK_contact='$item_ID'";
1991
                                                        if ($result_remaining=$DB->query($query)){
1992
                                                                if ($DB->result($result_remaining,0,'CPT')==0){
1993
                                                                        if ($this->options['clean_contacts']==1){
1994
                                                                                $contact->delete(array('ID'=>$item_ID));
1995
                                                                        } 
1996
                                                                        if ($this->options['clean_contacts']==2) { // purge
1997
                                                                                $contact->delete(array('ID'=>$item_ID),1);
1998
                                                                        }
1999
                                                                }
2000
                                                        }
2001
                                                }
2002

    
2003
                                        }
2004
                                }
2005
                        }
2006
                } else {// else unlink
2007
                        $query="DELETE FROM glpi_contact_enterprise WHERE FK_enterprise = '$ID'";
2008
                        $DB->query($query);
2009
                }
2010

    
2011
        }
2012

    
2013
        /**
2014
        * Transfer reservations of an item
2015
        *
2016
        *@param $type original type of transfered item
2017
        *@param $ID original ID of the item
2018
        *@param $newID new ID of the item
2019
        **/
2020
        function transferReservations($type,$ID,$newID){
2021
                global $DB;
2022

    
2023
                $ri=new ReservationItem();
2024

    
2025
                if ($ri->getFromDBbyItem($type,$ID)){
2026
                        switch ($this->options['keep_reservations']){
2027
                                // delete
2028
                                case 0 :  
2029
                                        // Same item -> delete
2030
                                        if ($ID==$newID){ 
2031
                                                $ri->delete(array('ID'=>$ri->fields['ID']));
2032
                                        }
2033
                                        // Copy : nothing to do
2034
                                        break;
2035
                                // Keep
2036
                                case 1 : 
2037
                                default : 
2038
                                        // Copy : set item as reservable
2039
                                        if ($ID!=$newID){
2040
                                                $input['device_type']=$type;
2041
                                                $input['id_device']=$newID;
2042
                                                $input['active']=$ri->fields['active'];
2043
                                                unset($ri->fields);
2044
                                                $ri->add($input);
2045
                                        }
2046
                                        // Same item -> nothing to do
2047
                                        break;
2048
                        }
2049
                }
2050
        }
2051

    
2052
        /**
2053
        * Transfer devices of a computer
2054
        *
2055
        *@param $type original type of transfered item
2056
        *@param $ID ID of the computer
2057
        *@param $ocs_computer if computer type OCS ID of the item if available
2058
        **/
2059
        function transferDevices($type,$ID,$ocs_computer=false){
2060
                global $DB;
2061
                // Only same case because no duplication of computers
2062
                switch ($this->options['keep_devices']){
2063
                        // delete devices
2064
                        case 0 :  
2065
                                $query = "DELETE FROM glpi_computer_device 
2066
                                        WHERE FK_computers = '$ID'";
2067
                                $result = $DB->query($query);
2068
                                // Only case of ocs link update is needed (if devices are keep nothing to do)
2069
                                if ($ocs_computer){
2070
                                        $query="UPDATE glpi_ocs_link SET import_ip = NULL WHERE glpi_id='$ID'";
2071
                                        $DB->query($query);
2072
                                }
2073
                                break;
2074
                        // Keep devices
2075
                        case 1 :        
2076
                        default : 
2077
                                // Same item -> nothing to do
2078
                                break;
2079
                }
2080
        }
2081

    
2082
        /**
2083
        * Transfer network links
2084
        *
2085
        *@param $type original type of transfered item
2086
        *@param $ID original ID of the item
2087
        *@param $newID new ID of the item
2088
        *@param $ocs_computer if computer type OCS ID of the item if available
2089
        **/
2090
        function transferNetworkLink($type,$ID,$newID,$ocs_computer=false){
2091
                global $DB;
2092
                $np=new Netport();
2093

    
2094
                $query = "SELECT *
2095
                        FROM glpi_networking_ports 
2096
                        WHERE on_device = '$ID' AND device_type = '$type'";
2097
                if ($result = $DB->query($query)) {
2098
                        if ($DB->numrows($result)!=0) { 
2099
                                switch ($this->options['keep_networklinks']){
2100
                                        // Delete netport
2101
                                        case 0 : 
2102
                                                // Not a copy -> delete
2103
                                                if ($ID==$newID){
2104
                                                        while ($data=$DB->fetch_array($result)) {
2105
                                                                $np->delete(array('ID'=>$data['ID']));
2106
                                                        }
2107
                                                        // Only case of ocs link update is needed (if netports are keep nothing to do)
2108
                                                        if ($ocs_computer){
2109
                                                                $query="UPDATE glpi_ocs_link SET import_ip = NULL WHERE glpi_id='$ID'";
2110
                                                                $DB->query($query);
2111
                                                        }
2112
                                                }
2113
                                                // Copy -> do nothing
2114
                                                break;
2115
                                        // Disconnect
2116
                                        case 1 : 
2117
                                                
2118
                                                // Not a copy -> disconnect
2119
                                                if ($ID==$newID){ 
2120
                                                        while ($data=$DB->fetch_array($result)) {
2121
                                                                removeConnector($data['ID']);
2122
                                                                if ($data['netpoint']){
2123
                                                                        $netpointID=$this->transferDropdownNetpoint($data['netpoint']);
2124
                                                                        $input['ID']=$data['ID'];
2125
                                                                        $input['netpoint']=$netpointID;
2126
                                                                        $np->update($input);
2127
                                                                }
2128
                                                        }
2129
                                                } else { // Copy -> copy netports
2130
                                                        while ($data=$DB->fetch_array($result)) {
2131
                                                                $data = addslashes_deep($data);
2132
                                                                unset($data['ID']);
2133
                                                                $data['on_device']=$newID;
2134
                                                                $data['netpoint']=$this->transferDropdownNetpoint($data['netpoint']);
2135
                                                                unset($np->fields);
2136
                                                                $np->add($data);
2137
                                                        }
2138
                                                }
2139
                                                break;
2140
                                        // Keep network links 
2141
                                        case 2 : 
2142
                                        default : 
2143
                                                // Copy -> Copy netpoints (do not keep links)
2144
                                                if ($ID!=$newID){
2145
                                                        while ($data=$DB->fetch_array($result)) {
2146
                                                                unset($data['ID']);
2147
                                                                $data['on_device']=$newID;
2148
                                                                $data['netpoint']=$this->transferDropdownNetpoint($data['netpoint']);
2149
                                                                unset($np->fields);
2150
                                                                $np->add($data);
2151
                                                        }
2152
                                                } else {
2153
                                                        while ($data=$DB->fetch_array($result)) {
2154
                                                                // Not a copy -> only update netpoint
2155
                                                                if ($data['netpoint']){
2156
                                                                        $netpointID=$this->transferDropdownNetpoint($data['netpoint']);
2157
                                                                        $input['ID']=$data['ID'];
2158
                                                                        $input['netpoint']=$netpointID;
2159
                                                                        $np->update($input);
2160
                                                                }
2161
                                                        }
2162
                                                }
2163

    
2164
                                                break;
2165

    
2166
                                }
2167
                        }
2168
                }
2169
        }
2170

    
2171
        /**
2172
         * Print the transfer form
2173
         *
2174
         *
2175
         * Print transfer form
2176
         *
2177
         *@param $target filename : where to go when done.
2178
         *@param $ID Integer : Id of the contact to print
2179
         *@param $withtemplate='' boolean : template or basic item
2180
         *
2181
         *@return boolean item found
2182
         *
2183
         **/
2184
        function showForm ($target,$ID,$withtemplate='') {
2185

    
2186
                global $CFG_GLPI, $LANG;
2187

    
2188
                if (!haveRight("transfer","r")) return false;
2189

    
2190
                $edit_form=true;
2191
                if (!ereg("transfer.form.php",$_SERVER['PHP_SELF'])){
2192
                        $edit_form=false;
2193
                }
2194

    
2195
                $con_spotted=false;
2196
                // TODO : Force do not use cache for this item due to right problem : need to split into 2 fonctions
2197
                $use_cache=false;
2198
                if (empty($ID)) {
2199
                        if($this->getEmpty()) $con_spotted = true;
2200
                        $use_cache=false;
2201
                } else {
2202
                        if($this->getFromDB($ID)) $con_spotted = true;
2203
                }
2204

    
2205
                if ($con_spotted){
2206
                        $fctdropdown = (haveRight("transfer","w") ? "dropdownArrayValues" : "dropdownArrayValuesReadOnly");
2207
                        echo "<form method='post' name=form action=\"$target\"><div class='center'>";
2208

    
2209
                        echo "<table class='tab_cadre_fixe' cellpadding='2' >";
2210
                        if ($edit_form){
2211
                                echo "<tr><th colspan='4'>";
2212
                                if (empty($ID)) {
2213
                                        echo $LANG["transfer"][2];
2214
                                } else {
2215
                                        echo $LANG["common"][2]." $ID";
2216
                                }                
2217
                                echo "</th></tr>";
2218
                        } else {
2219
                                        echo "<tr>";
2220
                                        echo "<td class='tab_bg_2' valign='top' colspan='4'>";
2221
                                        
2222
                                        echo "<div class='center'>";
2223
                                        dropdown("glpi_entities",'to_entity');
2224
                                        echo "&nbsp;<input type='submit' name='transfer' value=\"".$LANG["buttons"][48]."\" class='submit'></div>";
2225
                                        echo "</td>";
2226
                                        echo "</tr>";
2227
                        }
2228

    
2229
                        if (!$use_cache||!($CFG_GLPI["cache"]->start($ID."_".$_SESSION["glpilanguage"],"GLPI_".$this->type))) {
2230
                                if ($edit_form){
2231
                                        echo "<tr class='tab_bg_1'>";
2232
                                        echo "<td colspan='2'>".$LANG["common"][16].":        </td><td colspan='2'>";
2233
                                        autocompletionTextField("name","glpi_transfers","name",$this->fields["name"],30);        
2234
                                        echo "</td>";
2235
                                        echo "</tr>";
2236
                                }
2237

    
2238
                                $keep=array(0=>$LANG["buttons"][6],
2239
                                                1=>$LANG["buttons"][49]);
2240
                                $clean=array(0=>$LANG["buttons"][49],
2241
                                        1=>$LANG["buttons"][6],
2242
                                        2=>$LANG["buttons"][22]);
2243

    
2244
                                echo "<tr class='tab_bg_1'>";
2245
                                echo "<td>".$LANG["common"][66]." -> ".$LANG["title"][38].":        </td><td>";
2246
                                $fctdropdown('keep_history',$keep,$this->fields['keep_history']);
2247
                                echo "</td>";
2248
                                echo "<td colspan='2'>&nbsp;</td>";
2249
                                echo "</tr>";
2250

    
2251
                                echo "<tr class='tab_bg_2'>";
2252
                                echo "<td colspan='4' class='center'><strong>".$LANG["Menu"][38]."</strong></td></tr>";
2253

    
2254
                                echo "<tr class='tab_bg_1'>";
2255
                                echo "<td>".$LANG["common"][66]." -> ".$LANG["networking"][6].":        </td><td>";
2256
                                $options=array(0=>$LANG["buttons"][6],
2257
                                                1=>$LANG["buttons"][49]." - ".$LANG["buttons"][10] ,
2258
                                                2=>$LANG["buttons"][49]." - ".$LANG["buttons"][9] );
2259
                                $fctdropdown('keep_networklinks',$options,$this->fields['keep_networklinks']);
2260
                                echo "</td>";
2261
                                echo "<td>".$LANG["common"][66]." -> ".$LANG["title"][28].":        </td><td>";
2262
                                $options=array(0=>$LANG["buttons"][6],
2263
                                                1=>$LANG["buttons"][49]." - ".$LANG["buttons"][10] ,
2264
                                                2=>$LANG["buttons"][49]." - ".$LANG["buttons"][48] );
2265
                                $fctdropdown('keep_tickets',$options,$this->fields['keep_tickets']);
2266
                                echo "</td>";
2267
                                echo "</tr>";
2268

    
2269

    
2270

    
2271
                                echo "<tr class='tab_bg_1'>";
2272
                                echo "<td>".$LANG["Menu"][0]." -> ".$LANG["Menu"][4].":        </td><td>";
2273
                                $fctdropdown('keep_softwares',$keep,$this->fields['keep_softwares']);
2274
                                echo "</td>";
2275
                                echo "<td>".$LANG["Menu"][4].": ".$LANG["transfer"][3]."        </td><td>";
2276
                                $fctdropdown('clean_softwares',$clean,$this->fields['clean_softwares']);
2277
                                echo "</td>";
2278
                                echo "</tr>";
2279

    
2280
                                echo "<tr class='tab_bg_1'>";
2281
                                echo "<td>".$LANG["common"][66]." -> ".$LANG["Menu"][17].":        </td><td>";        
2282
                                $fctdropdown('keep_reservations',$keep,$this->fields['keep_reservations']);
2283
                                echo "</td>";
2284
                                echo "<td>".$LANG["Menu"][0]." -> ".$LANG["title"][30].":        </td><td>";
2285
                                $fctdropdown('keep_devices',$keep,$this->fields['keep_devices']);
2286
                                echo "</td>";
2287
                                echo "</tr>";
2288

    
2289
                                echo "<tr class='tab_bg_1'>";
2290
                                echo "<td>".$LANG["Menu"][2]." -> ".$LANG["Menu"][21]." / ".$LANG["cartridges"][12].":        </td><td>";
2291
                                $fctdropdown('keep_cartridges_type',$keep,$this->fields['keep_cartridges_type']);
2292
                                echo "</td>";
2293
                                echo "<td>".$LANG["cartridges"][12].": ".$LANG["transfer"][3]."        </td><td>";
2294
                                $fctdropdown('clean_cartridges_type',$clean,$this->fields['clean_cartridges_type']);
2295
                                echo "</td>";
2296
                                echo "</tr>";
2297

    
2298
                                echo "<tr class='tab_bg_1'>";
2299
                                echo "<td>".$LANG["cartridges"][12]." -> ".$LANG["Menu"][21].":        </td><td>";
2300
                                $fctdropdown('keep_cartridges',$keep,$this->fields['keep_cartridges']);
2301
                                echo "</td>";                                
2302
                                echo "<td>".$LANG["common"][66]." -> ".$LANG["financial"][3].":        </td><td>";
2303
                                $fctdropdown('keep_infocoms',$keep,$this->fields['keep_infocoms']);
2304
                                echo "</td>";
2305
                                echo "</tr>";
2306

    
2307
                                echo "<tr class='tab_bg_1'>";
2308
                                echo "<td>".$LANG["setup"][92]." -> ".$LANG["Menu"][32].":        </td><td>";
2309
                                $fctdropdown('keep_consumables',$keep,$this->fields['keep_consumables']);
2310
                                echo "</td>";
2311
                                echo "<td colspan='2'>&nbsp;</td>";
2312
                                echo "</tr>";
2313

    
2314
                                echo "<tr class='tab_bg_2'>";
2315
                                echo "<td colspan='4' class='center'><strong>".$LANG["connect"][0]."</strong></td></tr>";
2316

    
2317
                                echo "<tr class='tab_bg_1'>";
2318
                                echo "<td>".$LANG["Menu"][3].":        </td><td>";
2319
                                $fctdropdown('keep_dc_monitor',$keep,$this->fields['keep_dc_monitor']);
2320
                                echo "</td>";
2321
                                echo "<td>".$LANG["Menu"][3].": ".$LANG["transfer"][3]."        </td><td>";
2322
                                $fctdropdown('clean_dc_monitor',$clean,$this->fields['clean_dc_monitor']);
2323
                                echo "</td>";
2324
                                echo "</tr>";
2325

    
2326
                                echo "<tr class='tab_bg_1'>";
2327
                                echo "<td>".$LANG["Menu"][2].":        </td><td>";
2328
                                $fctdropdown('keep_dc_printer',$keep,$this->fields['keep_dc_printer']);
2329
                                echo "</td>";
2330
                                echo "<td>".$LANG["Menu"][2].": ".$LANG["transfer"][3]."        </td><td>";
2331
                                $fctdropdown('clean_dc_printer',$clean,$this->fields['clean_dc_printer']);
2332
                                echo "</td>";
2333
                                echo "</tr>";
2334

    
2335
                                echo "<tr class='tab_bg_1'>";
2336
                                echo "<td>".$LANG["Menu"][16].":        </td><td>";
2337
                                $fctdropdown('keep_dc_peripheral',$keep,$this->fields['keep_dc_peripheral']);
2338
                                echo "</td>";
2339
                                echo "<td>".$LANG["Menu"][16].": ".$LANG["transfer"][3]."        </td><td>";
2340
                                $fctdropdown('clean_dc_peripheral',$clean,$this->fields['clean_dc_peripheral']);
2341
                                echo "</td>";
2342
                                echo "</tr>";
2343

    
2344
                                echo "<tr class='tab_bg_1'>";
2345
                                echo "<td>".$LANG["Menu"][34].":        </td><td>";
2346
                                $fctdropdown('keep_dc_phone',$keep,$this->fields['keep_dc_phone']);
2347
                                echo "</td>";
2348
                                echo "<td>".$LANG["Menu"][34].": ".$LANG["transfer"][3]."        </td><td>";
2349
                                $fctdropdown('clean_dc_phone',$clean,$this->fields['clean_dc_phone']);
2350
                                echo "</td>";
2351
                                echo "</tr>";
2352

    
2353

    
2354
                                echo "<tr class='tab_bg_2'>";
2355
                                echo "<td colspan='4' class='center'><strong>".$LANG["Menu"][26]."</strong></td></tr>";
2356

    
2357

    
2358
                                echo "<tr class='tab_bg_1'>";
2359
                                echo "<td>".$LANG["common"][66]." -> ".$LANG["Menu"][23].":        </td><td>";
2360
                                $fctdropdown('keep_enterprises',$keep,$this->fields['keep_enterprises']);
2361
                                echo "</td>";
2362
                                echo "<td>".$LANG["Menu"][23].": ".$LANG["transfer"][3]."        </td><td>";
2363
                                $fctdropdown('clean_enterprises',$clean,$this->fields['clean_enterprises']);
2364
                                echo "</td>";
2365
                                echo "</tr>";
2366

    
2367
                                echo "<tr class='tab_bg_1'>";
2368
                                echo "<td>".$LANG["Menu"][23]." -> ".$LANG["Menu"][22].":        </td><td>";
2369
                                $fctdropdown('keep_contacts',$keep,$this->fields['keep_contacts']);
2370
                                echo "</td>";
2371
                                echo "<td>".$LANG["Menu"][22].": ".$LANG["transfer"][3]."        </td><td>";
2372
                                $fctdropdown('clean_contacts',$clean,$this->fields['clean_contacts']);
2373
                                echo "</td>";
2374
                                echo "</tr>";
2375

    
2376
                                echo "<tr class='tab_bg_1'>";
2377
                                echo "<td>".$LANG["common"][66]." -> ".$LANG["Menu"][27].":        </td><td>";
2378
                                $fctdropdown('keep_documents',$keep,$this->fields['keep_documents']);
2379
                                echo "</td>";
2380
                                echo "<td>".$LANG["Menu"][27].": ".$LANG["transfer"][3]."        </td><td>";
2381
                                $fctdropdown('clean_documents',$clean,$this->fields['clean_documents']);
2382
                                echo "</td>";
2383
                                echo "</tr>";
2384

    
2385
                                echo "<tr class='tab_bg_1'>";
2386
                                echo "<td>".$LANG["common"][66]." -> ".$LANG["Menu"][25].":        </td><td>";
2387
                                $fctdropdown('keep_contracts',$keep,$this->fields['keep_contracts']);
2388
                                echo "</td>";
2389
                                echo "<td>".$LANG["Menu"][25].": ".$LANG["transfer"][3]."        </td><td>";
2390
                                $fctdropdown('clean_contracts',$clean,$this->fields['clean_contracts']);
2391
                                echo "</td>";
2392
                                echo "</tr>";
2393

    
2394
                                if ($use_cache){
2395
                                        $CFG_GLPI["cache"]->end();
2396
                                }
2397
                        }
2398

    
2399
                        if (haveRight("transfer","w")) {
2400
                                if ($edit_form){
2401
                                        if ($ID=="") {
2402
                                                echo "<tr>";
2403
                                                echo "<td class='tab_bg_2' valign='top' colspan='4'>";
2404
                                                echo "<div class='center'><input type='submit' name='add' value=\"".$LANG["buttons"][8]."\" class='submit'></div>";
2405
                                                echo "</td>";
2406
                                                echo "</tr>";
2407
                                        } else {
2408
                                                echo "<tr>";
2409
                                                echo "<td class='tab_bg_2' valign='top' colspan='2'>";
2410
                                                echo "<input type='hidden' name='ID' value=\"$ID\">\n";
2411
                                                echo "<div class='center'><input type='submit' name='update' value=\"".$LANG["buttons"][7]."\" class='submit' ></div>";
2412
                                                echo "</td>\n\n";
2413
                                                echo "<td class='tab_bg_2' valign='top' colspan='2'>\n";
2414
                                                echo "<div class='center'><input type='submit' name='delete' value=\"".$LANG["buttons"][6]."\" class='submit'></div>";
2415
                                                echo "</td>";
2416
                                                echo "</tr>";
2417
        
2418
                                        }
2419
                                } 
2420
                        }
2421
                        echo "</table></div></form>";
2422

    
2423
                } else {
2424
                        echo "<div class='center'><strong>".$LANG["common"][54]."</strong></div>";
2425
                        return false;
2426

    
2427
                }
2428
                return true;
2429
        }
2430
        /// Display items to transfers
2431
        function showTransferList(){
2432
                global $LANG,$LINK_ID_TABLE,$DB,$CFG_GLPI;
2433
                $ci=new CommonItem();
2434
                if (isset($_SESSION['glpitransfer_list'])&&count($_SESSION['glpitransfer_list'])){
2435
                        echo "<div class='center'><strong>".$LANG["transfer"][5]."<br>".$LANG["transfer"][6];
2436
                        echo "</strong>";
2437
                        echo "</div>";
2438
                        //echo '<tr><th colspan="2">'.$LANG["transfer"][4].'</th></tr>';
2439
                        echo "<table class='tab_cadre_fixe' >";
2440
                        echo '<tr><th>'.$LANG["transfer"][7].'</th><th>'.$LANG["transfer"][8].":&nbsp;";
2441
                        $rand=dropdownValue("glpi_transfers","ID",0,0,-1,array('value_fieldname'=>'ID',
2442
                        'to_update'=>"transfer_form", 'url'=>$CFG_GLPI["root_doc"]."/ajax/transfers.php"));
2443
                        echo '</th></tr>';
2444
                        echo "<tr><td class='tab_bg_1' valign='top'>";
2445
                        
2446
                        foreach ($_SESSION['glpitransfer_list'] as $type => $tab){
2447
                                if (count($tab)){
2448
                                        $table=$LINK_ID_TABLE[$type];
2449
                                        $query="SELECT $table.ID, $table.name, glpi_entities.completename AS locname, glpi_entities.ID AS entID 
2450
                                                FROM $table LEFT JOIN glpi_entities ON ($table.FK_entities = glpi_entities.ID) 
2451
                                                WHERE $table.ID IN ".$this->createSearchConditionUsingArray($tab)."
2452
                                                ORDER BY locname, $table.name";
2453
                                        $entID=-1;
2454
                                        if ($result=$DB->query($query)){
2455
                                                if ($DB->numrows($result)){
2456
                                                        $ci->setType($type);
2457
                                                        echo '<h3>'.$ci->getType().'</h3>';
2458
                                                        while ($data=$DB->fetch_assoc($result)){
2459
                                                                if ($entID!=$data['entID']){
2460
                                                                        if ($entID!=-1){
2461
                                                                                echo '<br>';
2462
                                                                        }
2463
                                                                        $entID=$data['entID'];
2464
                                                                        if ($entID>0){
2465
                                                                                echo '<strong>'.$data['locname'].'</strong><br>';
2466
                                                                        } else {
2467
                                                                                echo '<strong>'.$LANG["entity"][2].'</strong><br>';
2468
                                                                        }
2469
                                                                }
2470
                                                                echo ($data['name'] ? $data['name']."<br>" : "(".$data['ID'].")<br>");
2471
                                                        }
2472
                                                }
2473
                                        }
2474
                                }
2475
                        }
2476
                        echo "</td><td class='tab_bg_2' valign='top'>";
2477
                        if (countElementsInTable('glpi_transfers')==0){
2478
                                echo $LANG["search"][15];
2479
                        } else {
2480
                                
2481
                                $params=array('ID'=>'__VALUE__');
2482
                                ajaxUpdateItemOnSelectEvent("dropdown_ID","transfer_form",$CFG_GLPI["root_doc"]."/ajax/transfers.php",$params,false);
2483
                                //ajaxUpdateItem("transfer_form",$CFG_GLPI["root_doc"]."/ajax/transfers.php",$params,false,"dropdown_ID".$rand);
2484
                        }
2485
                        echo "<div align='center' id='transfer_form'>";
2486
                        echo "<a href='".$_SERVER['PHP_SELF']."?clear=1'>".$LANG["transfer"][4]."</a>";
2487
                        echo "</div>";
2488
                        echo '</td></tr>';
2489
                        echo '</table>';
2490
                } else {
2491
                        echo $LANG["common"][24];
2492
                }
2493

    
2494
        }
2495
}
2496

    
2497

    
2498
?>
Redmine Appliance - Powered by TurnKey Linux