function transferSupplierContacts($ID, $newID)
{
global $DB;
$need_clean_process = false;
// if keep
if ($this->options['keep_contact']) {
$contact = new Contact();
// Get contracts for the item
$query = "SELECT *\n FROM `glpi_contacts_suppliers`\n WHERE `suppliers_id` = '{$ID}'\n AND `contacts_id` NOT IN " . $this->item_recurs['Contact'];
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['contacts_id'];
$newcontactID = -1;
// is already transfer ?
if (isset($this->already_transfer['Contact'][$item_ID])) {
$newcontactID = $this->already_transfer['Contact'][$item_ID];
if ($newcontactID != $item_ID) {
$need_clean_process = true;
}
} else {
$canbetransfer = true;
// Transfer enterprise : is the contact used for another enterprise ?
if ($ID == $newID) {
$query_search = "SELECT COUNT(*) AS cpt\n FROM `glpi_contacts_suppliers`\n WHERE `contacts_id` = '{$item_ID}'\n AND `suppliers_id`\n NOT IN " . $this->item_search['Supplier'] . "\n AND `suppliers_id`\n NOT IN " . $this->item_recurs['Supplier'];
$result_search = $DB->query($query_search);
if ($DB->result($result_search, 0, 'cpt') > 0) {
$canbetransfer = false;
}
}
// Yes : transfer
if ($canbetransfer) {
$this->transferItem('Contact', $item_ID, $item_ID);
$newcontactID = $item_ID;
} else {
$need_clean_process = true;
$contact->getFromDB($item_ID);
// No : search contract
$query = "SELECT *\n FROM `glpi_contacts`\n WHERE `entities_id` = '" . $this->to . "'\n AND `name` = '" . addslashes($contact->fields['name']) . "'\n AND `firstname`\n = '" . addslashes($contact->fields['firstname']) . "'";
if ($result_search = $DB->query($query)) {
if ($DB->numrows($result_search) > 0) {
$newcontactID = $DB->result($result_search, 0, 'id');
$this->addToAlreadyTransfer('Contact', $item_ID, $newcontactID);
}
}
// found : use it
// not found : copy contract
if ($newcontactID < 0) {
// 1 - create new item
unset($contact->fields['id']);
$input = $contact->fields;
$input['entities_id'] = $this->to;
unset($contact->fields);
$newcontactID = $contact->add(toolbox::addslashes_deep($input));
// 2 - transfer as copy
$this->transferItem('Contact', $item_ID, $newcontactID);
}
}
}
// Update links
if ($ID == $newID) {
if ($item_ID != $newcontactID) {
$query = "UPDATE `glpi_contacts_suppliers`\n SET `contacts_id` = '{$newcontactID}'\n WHERE `id` = '" . $data['id'] . "'";
$DB->query($query);
}
} else {
// Same Item -> update links
// Copy Item -> copy links
if ($item_ID != $newcontactID) {
$query = "INSERT INTO `glpi_contacts_suppliers`\n (`contacts_id`, `suppliers_id`)\n VALUES ('{$newcontactID}','{$newID}')";
$DB->query($query);
} else {
// transfer contact but copy enterprise : update link
$query = "UPDATE `glpi_contacts_suppliers`\n SET `suppliers_id` = '{$newID}'\n WHERE `id` = '" . $data['id'] . "'";
$DB->query($query);
}
}
// If clean and unused ->
if ($need_clean_process && $this->options['clean_contact']) {
$query = "SELECT COUNT(*) AS cpt\n FROM `glpi_contacts_suppliers`\n WHERE `contacts_id` = '{$item_ID}'";
if ($result_remaining = $DB->query($query)) {
if ($DB->result($result_remaining, 0, 'cpt') == 0) {
if ($this->options['clean_contact'] == 1) {
$contact->delete(array('id' => $item_ID));
}
if ($this->options['clean_contact'] == 2) {
// purge
$contact->delete(array('id' => $item_ID), 1);
}
}
}
}
}
}
}
} else {
// else unlink
$query = "DELETE\n FROM `glpi_contacts_suppliers`\n WHERE `suppliers_id` = '{$ID}'";
$DB->query($query);
}
}