function transferContracts($itemtype, $ID, $newID)
{
global $DB;
$need_clean_process = false;
// if keep
if ($this->options['keep_contract']) {
$contract = new Contract();
// Get contracts for the item
$query = "SELECT *\n FROM `glpi_contracts_items`\n WHERE `items_id` = '{$ID}'\n AND `itemtype` = '{$itemtype}'\n AND `contracts_id` NOT IN " . $this->item_recurs['Contract'];
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['contracts_id'];
$newcontractID = -1;
// is already transfer ?
if (isset($this->already_transfer['Contract'][$item_ID])) {
$newcontractID = $this->already_transfer['Contract'][$item_ID];
if ($newcontractID != $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_contracts_items`\n WHERE `contracts_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_contracts_items`\n WHERE `contracts_id` = '{$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('Contract', $item_ID, $item_ID);
$newcontractID = $item_ID;
} else {
$need_clean_process = true;
$contract->getFromDB($item_ID);
// No : search contract
$query = "SELECT *\n FROM `glpi_contracts`\n WHERE `entities_id` = '" . $this->to . "'\n AND `name` = '" . addslashes($contract->fields['name']) . "'";
if ($result_search = $DB->query($query)) {
if ($DB->numrows($result_search) > 0) {
$newcontractID = $DB->result($result_search, 0, 'id');
$this->addToAlreadyTransfer('Contract', $item_ID, $newcontractID);
}
}
// found : use it
// not found : copy contract
if ($newcontractID < 0) {
// 1 - create new item
unset($contract->fields['id']);
$input = $contract->fields;
$input['entities_id'] = $this->to;
unset($contract->fields);
$newcontractID = $contract->add(toolbox::addslashes_deep($input));
// 2 - transfer as copy
$this->transferItem('Contract', $item_ID, $newcontractID);
}
}
}
// Update links
if ($ID == $newID) {
if ($item_ID != $newcontractID) {
$query = "UPDATE `glpi_contracts_items`\n SET `contracts_id` = '{$newcontractID}'\n WHERE `id` = '" . $data['id'] . "'";
$DB->query($query);
}
} else {
// Same Item -> update links
// Copy Item -> copy links
if ($item_ID != $newcontractID) {
$query = "INSERT INTO `glpi_contracts_items`\n (`contracts_id`, `items_id`, `itemtype`)\n VALUES ('{$newcontractID}', '{$newID}', '{$itemtype}')";
$DB->query($query);
} else {
// same contract for new item update link
$query = "UPDATE `glpi_contracts_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_contract']) {
$query = "SELECT COUNT(*) AS cpt\n FROM `glpi_contracts_items`\n WHERE `contracts_id` = '{$item_ID}'";
if ($result_remaining = $DB->query($query)) {
if ($DB->result($result_remaining, 0, 'cpt') == 0) {
if ($this->options['clean_contract'] == 1) {
$contract->delete(array('id' => $item_ID));
}
if ($this->options['clean_contract'] == 2) {
// purge
$contract->delete(array('id' => $item_ID), 1);
}
}
}
}
}
}
}
} else {
// else unlink
$query = "DELETE\n FROM `glpi_contracts_items`\n WHERE `items_id` = '{$ID}'\n AND `itemtype` = '{$itemtype}'";
$DB->query($query);
}
}