Transfer::transferSingleSupplier PHP Method

transferSingleSupplier() public method

Transfer an enterprise
public transferSingleSupplier ( $ID )
$ID ID of the enterprise
    function transferSingleSupplier($ID)
    {
        global $DB, $CFG_GLPI;
        // TODO clean system : needed ?
        $ent = new Supplier();
        if ($this->options['keep_supplier'] && $ent->getFromDB($ID)) {
            if (isset($this->noneedtobe_transfer['Supplier'][$ID])) {
                // recursive enterprise
                return $ID;
            }
            if (isset($this->already_transfer['Supplier'][$ID])) {
                // Already transfer
                return $this->already_transfer['Supplier'][$ID];
            }
            $newID = -1;
            // Not already transfer
            $links_remaining = 0;
            // All linked items need to be transfer so transfer enterprise ?
            // Search for contract
            $query = "SELECT COUNT(*) AS cpt\n                   FROM `glpi_contracts_suppliers`\n                   WHERE `suppliers_id` = '{$ID}'\n                         AND `contracts_id` NOT IN " . $this->item_search['Contract'];
            $result_search = $DB->query($query);
            $links_remaining = $DB->result($result_search, 0, 'cpt');
            if ($links_remaining == 0) {
                // Search for infocoms
                if ($this->options['keep_infocom']) {
                    foreach (Infocom::getItemtypesThatCanHave() as $itemtype) {
                        if (isset($this->item_search[$itemtype])) {
                            $query = "SELECT COUNT(*) AS cpt\n                               FROM `glpi_infocoms`\n                               WHERE `suppliers_id` = '{$ID}'\n                                     AND `itemtype` = '{$itemtype}'\n                                     AND `items_id` NOT IN " . $this->item_search[$itemtype];
                            if ($result_search = $DB->query($query)) {
                                $links_remaining += $DB->result($result_search, 0, ' cpt');
                            }
                        }
                    }
                }
            }
            // All linked items need to be transfer -> use unique transfer system
            if ($links_remaining == 0) {
                $this->transferItem('Supplier', $ID, $ID);
                $newID = $ID;
            } else {
                // else Transfer by Copy
                // Is existing item in the destination entity ?
                $query = "SELECT *\n                      FROM `glpi_suppliers`\n                      WHERE `entities_id` = '" . $this->to . "'\n                            AND `name` = '" . addslashes($ent->fields['name']) . "'";
                if ($result_search = $DB->query($query)) {
                    if ($DB->numrows($result_search) > 0) {
                        $newID = $DB->result($result_search, 0, 'id');
                        $this->addToAlreadyTransfer('Supplier', $ID, $newID);
                    }
                }
                // Not found -> transfer copy
                if ($newID < 0) {
                    // 1 - create new item
                    unset($ent->fields['id']);
                    $input = $ent->fields;
                    $input['entities_id'] = $this->to;
                    unset($ent->fields);
                    $newID = $ent->add(toolbox::addslashes_deep($input));
                    // 2 - transfer as copy
                    $this->transferItem('Supplier', $ID, $newID);
                }
                // Founded -> use to link : nothing to do
            }
            return $newID;
        }
        return 0;
    }