Transfer::transferDocuments PHP Method

transferDocuments() public method

Transfer documents
public transferDocuments ( $itemtype, $ID, $newID )
$itemtype original type of transfered item
$ID original ID of the document
$newID new ID of the document
    function transferDocuments($itemtype, $ID, $newID)
    {
        global $DB;
        $need_clean_process = false;
        // if keep
        if ($this->options['keep_document']) {
            $document = new Document();
            // Get contracts for the item
            $query = "SELECT *\n                   FROM `glpi_documents_items`\n                   WHERE `items_id` = '{$ID}'\n                         AND `itemtype` = '{$itemtype}'\n                         AND `documents_id` NOT IN " . $this->item_recurs['Document'];
            if ($result = $DB->query($query)) {
                if ($DB->numrows($result) > 0) {
                    // Foreach get item
                    while ($data = $DB->fetch_assoc($result)) {
                        $need_clean_process = false;
                        $item_ID = $data['documents_id'];
                        $newdocID = -1;
                        // is already transfer ?
                        if (isset($this->already_transfer['Document'][$item_ID])) {
                            $newdocID = $this->already_transfer['Document'][$item_ID];
                            if ($newdocID != $item_ID) {
                                $need_clean_process = true;
                            }
                        } 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 `glpi_documents_items`\n                               WHERE `documents_id` = '{$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 `glpi_documents_items`\n                                                  WHERE `documents_id` = '{$item_ID}'\n                                                        AND `itemtype` = '{$dtype}'\n                                                        AND `items_id`\n                                                             NOT IN " . $this->item_search[$dtype];
                                            // contacts, contracts, and enterprises are linked as device.
                                            if (isset($this->item_recurs[$dtype])) {
                                                $query_search .= " AND `items_id`\n                                                            NOT IN " . $this->item_recurs[$dtype];
                                            }
                                            $result_search = $DB->query($query_search);
                                            if ($DB->result($result_search, 0, 'cpt') > 0) {
                                                $canbetransfer = false;
                                            }
                                        }
                                    }
                                }
                            }
                            // Yes : transfer
                            if ($canbetransfer) {
                                $this->transferItem('Document', $item_ID, $item_ID);
                                $newdocID = $item_ID;
                            } else {
                                $need_clean_process = true;
                                $document->getFromDB($item_ID);
                                // No : search contract
                                $query = "SELECT *\n                                  FROM `glpi_documents`\n                                  WHERE `entities_id` = '" . $this->to . "'\n                                        AND `name` = '" . addslashes($document->fields['name']) . "'";
                                if ($result_search = $DB->query($query)) {
                                    if ($DB->numrows($result_search) > 0) {
                                        $newdocID = $DB->result($result_search, 0, 'id');
                                        $this->addToAlreadyTransfer('Document', $item_ID, $newdocID);
                                    }
                                }
                                // found : use it
                                // not found : copy doc
                                if ($newdocID < 0) {
                                    // 1 - create new item
                                    unset($document->fields['id']);
                                    $input = $document->fields;
                                    // Not set new entity Do by transferItem
                                    unset($document->fields);
                                    $newdocID = $document->add(toolbox::addslashes_deep($input));
                                    // 2 - transfer as copy
                                    $this->transferItem('Document', $item_ID, $newdocID);
                                }
                            }
                        }
                        // Update links
                        if ($ID == $newID) {
                            if ($item_ID != $newdocID) {
                                $query = "UPDATE `glpi_documents_items`\n                                  SET `documents_id` = '{$newdocID}'\n                                  WHERE `id` = '" . $data['id'] . "'";
                                $DB->query($query);
                            }
                        } else {
                            // Same Item -> update links
                            // Copy Item -> copy links
                            if ($item_ID != $newdocID) {
                                $query = "INSERT INTO `glpi_documents_items`\n                                         (`documents_id`, `items_id`, `itemtype`)\n                                  VALUES ('{$newdocID}','{$newID}','{$itemtype}')";
                                $DB->query($query);
                            } else {
                                // same doc for new item update link
                                $query = "UPDATE `glpi_documents_items`\n                                  SET `items_id` = '{$newID}'\n                                  WHERE `id` = '" . $data['id'] . "'";
                                $DB->query($query);
                            }
                        }
                        // If clean and unused ->
                        if ($need_clean_process && $this->options['clean_document']) {
                            $query = "SELECT COUNT(*) AS cpt\n                               FROM `glpi_documents_items`\n                               WHERE `documents_id` = '{$item_ID}'";
                            if ($result_remaining = $DB->query($query)) {
                                if ($DB->result($result_remaining, 0, 'cpt') == 0) {
                                    if ($this->options['clean_document'] == 1) {
                                        $document->delete(array('id' => $item_ID));
                                    }
                                    if ($this->options['clean_document'] == 2) {
                                        // purge
                                        $document->delete(array('id' => $item_ID), 1);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } else {
            // else unlink
            $query = "DELETE\n                   FROM `glpi_documents_items`\n                   WHERE `items_id` = '{$ID}'\n                         AND `itemtype` = '{$itemtype}'";
            $DB->query($query);
        }
    }