Transfer::transferDevices PHP Method

transferDevices() public method

Transfer devices of an item
public transferDevices ( $itemtype, $ID, $newID )
$itemtype original type of transfered item
$ID ID of the item
$newID new ID of the item
    function transferDevices($itemtype, $ID, $newID)
    {
        global $DB, $CFG_GLPI;
        // Only same case because no duplication of computers
        switch ($this->options['keep_device']) {
            // delete devices
            case 0:
                foreach (Item_Devices::getItemAffinities($itemtype) as $type) {
                    $table = getTableForItemType($type);
                    $query = "DELETE\n                         FROM `{$table}`\n                         WHERE `itemtype` = '{$itemtype}'\n                               AND `items_id` = '{$ID}'";
                    $result = $DB->query($query);
                }
            default:
                // Keep devices
                foreach (Item_Devices::getItemAffinities($itemtype) as $itemdevicetype) {
                    $itemdevicetable = getTableForItemType($itemdevicetype);
                    $devicetype = $itemdevicetype::getDeviceType();
                    $devicetable = getTableForItemType($devicetype);
                    $fk = getForeignKeyFieldForTable($devicetable);
                    $device = new $devicetype();
                    // Get contracts for the item
                    $query = "SELECT *\n                         FROM `{$itemdevicetable}`\n                         WHERE `items_id` = '{$ID}'\n                               AND `itemtype` = '{$itemtype}'\n                               AND `{$fk}` NOT IN " . $this->item_recurs[$devicetype];
                    if ($result = $DB->query($query)) {
                        if ($DB->numrows($result) > 0) {
                            // Foreach get item
                            while ($data = $DB->fetch_assoc($result)) {
                                $item_ID = $data[$fk];
                                $newdeviceID = -1;
                                // is already transfer ?
                                if (isset($this->already_transfer[$devicetype][$item_ID])) {
                                    $newdeviceID = $this->already_transfer[$devicetype][$item_ID];
                                } else {
                                    // No
                                    // Can be transfer without copy ? = all linked items need to be transfer (so not copy)
                                    $canbetransfer = true;
                                    $query = "SELECT DISTINCT `itemtype`\n                                     FROM `{$itemdevicetable}`\n                                     WHERE `{$fk}` = '{$item_ID}'";
                                    if ($result_type = $DB->query($query)) {
                                        if ($DB->numrows($result_type) > 0) {
                                            while (($data_type = $DB->fetch_assoc($result_type)) && $canbetransfer) {
                                                $dtype = $data_type['itemtype'];
                                                if (isset($this->item_search[$dtype])) {
                                                    // No items to transfer -> exists links
                                                    $query_search = "SELECT COUNT(*) AS cpt\n                                                        FROM `{$itemdevicetable}`\n                                                        WHERE `{$fk}` = '{$item_ID}'\n                                                              AND `itemtype` = '{$dtype}'\n                                                              AND `items_id`\n                                                                  NOT IN " . $this->item_search[$dtype];
                                                    $result_search = $DB->query($query_search);
                                                    if ($DB->result($result_search, 0, 'cpt') > 0) {
                                                        $canbetransfer = false;
                                                    }
                                                } else {
                                                    $canbetransfer = false;
                                                }
                                            }
                                        }
                                    }
                                    // Yes : transfer
                                    if ($canbetransfer) {
                                        $this->transferItem($devicetype, $item_ID, $item_ID);
                                        $newdeviceID = $item_ID;
                                    } else {
                                        $device->getFromDB($item_ID);
                                        // No : search device
                                        $field = "name";
                                        if (!FieldExists($devicetable, "name")) {
                                            $field = "designation";
                                        }
                                        $query = "SELECT *\n                                        FROM `{$devicetable}`\n                                        WHERE `entities_id` = '" . $this->to . "'\n                                              AND `" . $field . "` = '" . addslashes($device->fields[$field]) . "'";
                                        if ($result_search = $DB->query($query)) {
                                            if ($DB->numrows($result_search) > 0) {
                                                $newdeviceID = $DB->result($result_search, 0, 'id');
                                                $this->addToAlreadyTransfer($devicetype, $item_ID, $newdeviceID);
                                            }
                                        }
                                        // found : use it
                                        // not found : copy contract
                                        if ($newdeviceID < 0) {
                                            // 1 - create new item
                                            unset($device->fields['id']);
                                            $input = $device->fields;
                                            // Fix for fields with NULL in DB
                                            foreach ($input as $key => $value) {
                                                if ($value == '') {
                                                    unset($input[$key]);
                                                }
                                            }
                                            $input['entities_id'] = $this->to;
                                            unset($device->fields);
                                            $newdeviceID = $device->add(Toolbox::addslashes_deep($input));
                                            // 2 - transfer as copy
                                            $this->transferItem($devicetype, $item_ID, $newdeviceID);
                                        }
                                    }
                                }
                                // Update links
                                $query = "UPDATE `{$itemdevicetable}`\n                                  SET `{$fk}` = '{$newdeviceID}',\n                                      `items_id` = '{$newID}'\n                                  WHERE `id` = '" . $data['id'] . "'";
                                $DB->query($query);
                                $this->transferItem($itemdevicetype, $data['id'], $data['id']);
                            }
                        }
                    }
                }
                break;
        }
    }