function simulateTransfer($items)
{
global $DB, $CFG_GLPI;
// Init types :
$types = array('CartridgeItem', 'Change', 'Computer', 'ConsumableItem', 'Contact', 'Contract', 'Document', 'Link', 'Monitor', 'NetworkEquipment', 'Peripheral', 'Phone', 'Printer', 'Problem', 'Software', 'SoftwareLicense', 'SoftwareVersion', 'Supplier', 'Ticket');
$types = array_merge($types, $CFG_GLPI['device_types']);
$types = array_merge($types, Item_Devices::getDeviceTypes());
foreach ($types as $t) {
if (!isset($this->needtobe_transfer[$t])) {
$this->needtobe_transfer[$t] = array();
}
if (!isset($this->noneedtobe_transfer[$t])) {
$this->noneedtobe_transfer[$t] = array();
}
$this->item_search[$t] = $this->createSearchConditionUsingArray($this->needtobe_transfer[$t]);
$this->item_recurs[$t] = $this->createSearchConditionUsingArray($this->noneedtobe_transfer[$t]);
}
$to_entity_ancestors = getAncestorsOf("glpi_entities", $this->to);
// Copy items to needtobe_transfer
foreach ($items as $key => $tab) {
if (count($tab)) {
foreach ($tab as $ID) {
$this->addToBeTransfer($key, $ID);
}
}
}
// Computer first
$this->item_search['Computer'] = $this->createSearchConditionUsingArray($this->needtobe_transfer['Computer']);
// DIRECT CONNECTIONS
$DC_CONNECT = array();
if ($this->options['keep_dc_monitor']) {
$DC_CONNECT[] = 'Monitor';
}
if ($this->options['keep_dc_phone']) {
$DC_CONNECT[] = 'Phone';
}
if ($this->options['keep_dc_peripheral']) {
$DC_CONNECT[] = 'Peripheral';
}
if ($this->options['keep_dc_printer']) {
$DC_CONNECT[] = 'Printer';
}
if (count($DC_CONNECT) && count($this->needtobe_transfer['Computer']) > 0) {
foreach ($DC_CONNECT as $itemtype) {
$itemtable = getTableForItemType($itemtype);
// Clean DB / Search unexisting links and force disconnect
$query = "SELECT `glpi_computers_items`.`id`\n FROM `glpi_computers_items`\n LEFT JOIN `{$itemtable}`\n ON (`glpi_computers_items`.`items_id` = `{$itemtable}`.`id` )\n WHERE `glpi_computers_items`.`itemtype` = '{$itemtype}'\n AND `{$itemtable}`.`id` IS NULL";
if ($result = $DB->query($query)) {
if ($DB->numrows($result) > 0) {
while ($data = $DB->fetch_assoc($result)) {
$conn = new Computer_Item();
$conn->delete(array('id' => $data['id'], '_no_history' => true, '_no_auto_action' => true));
}
}
}
if (!($item = getItemForItemtype($itemtype))) {
continue;
}
$query = "SELECT DISTINCT `items_id`\n FROM `glpi_computers_items`\n WHERE `itemtype` = '{$itemtype}'\n AND `computers_id` IN " . $this->item_search['Computer'];
if ($result = $DB->query($query)) {
if ($DB->numrows($result) > 0) {
while ($data = $DB->fetch_assoc($result)) {
if ($item->getFromDB($data['items_id']) && $item->isRecursive() && in_array($item->getEntityID(), $to_entity_ancestors)) {
$this->addNotToBeTransfer($itemtype, $data['items_id']);
} else {
$this->addToBeTransfer($itemtype, $data['items_id']);
}
}
}
}
$this->item_search[$itemtype] = $this->createSearchConditionUsingArray($this->needtobe_transfer[$itemtype]);
if ($item->maybeRecursive()) {
$this->item_recurs[$itemtype] = $this->createSearchConditionUsingArray($this->noneedtobe_transfer[$itemtype]);
}
}
}
// End of direct connections
// License / Software : keep / delete + clean unused / keep unused
if ($this->options['keep_software']) {
// Clean DB
$query = "SELECT DISTINCT `glpi_computers_softwareversions`.`computers_id`\n FROM `glpi_computers_softwareversions`\n WHERE `glpi_computers_softwareversions`.`computers_id`\n NOT IN (SELECT id FROM `glpi_computers`)";
if ($result = $DB->query($query)) {
if ($DB->numrows($result) > 0) {
while ($data = $DB->fetch_assoc($result)) {
$query = "DELETE\n FROM `glpi_computers_softwareversions`\n WHERE `computers_id` = '" . $data['computers_id'] . "'";
$DB->query($query);
}
}
}
// Clean DB
$query = "SELECT DISTINCT `glpi_computers_softwareversions`.`softwareversions_id`\n FROM `glpi_computers_softwareversions`\n WHERE `glpi_computers_softwareversions`.`softwareversions_id`\n NOT IN (SELECT id FROM `glpi_softwareversions`)";
if ($result = $DB->query($query)) {
if ($DB->numrows($result) > 0) {
while ($data = $DB->fetch_assoc($result)) {
$query = "DELETE\n FROM `glpi_computers_softwareversions`\n WHERE `softwareversions_id` = '" . $data['softwareversions_id'] . "'";
$DB->query($query);
}
}
}
// Clean DB
$query = "SELECT `glpi_softwareversions`.`id`\n FROM `glpi_softwareversions`\n LEFT JOIN `glpi_softwares`\n ON (`glpi_softwares`.`id` = `glpi_softwareversions`.`softwares_id`)\n WHERE `glpi_softwares`.`id` IS NULL";
if ($result = $DB->query($query)) {
if ($DB->numrows($result) > 0) {
while ($data = $DB->fetch_assoc($result)) {
$query = "DELETE\n FROM `glpi_softwareversions`\n WHERE `id` = '" . $data['id'] . "'";
$DB->query($query);
}
}
}
$query = "SELECT `glpi_softwares`.`id`,\n `glpi_softwares`.`entities_id`,\n `glpi_softwares`.`is_recursive`,\n `glpi_softwareversions`.`id` AS vID\n FROM `glpi_computers_softwareversions`\n INNER JOIN `glpi_softwareversions`\n ON (`glpi_computers_softwareversions`.`softwareversions_id`\n = `glpi_softwareversions`.`id`)\n INNER JOIN `glpi_softwares`\n ON (`glpi_softwares`.`id` = `glpi_softwareversions`.`softwares_id`)\n WHERE `glpi_computers_softwareversions`.`computers_id`\n IN " . $this->item_search['Computer'];
if ($result = $DB->query($query)) {
if ($DB->numrows($result) > 0) {
while ($data = $DB->fetch_assoc($result)) {
if ($data['is_recursive'] && in_array($data['entities_id'], $to_entity_ancestors)) {
$this->addNotToBeTransfer('SoftwareVersion', $data['vID']);
} else {
$this->addToBeTransfer('SoftwareVersion', $data['vID']);
}
}
}
}
}
// Software: From user choice only
$this->item_search['Software'] = $this->createSearchConditionUsingArray($this->needtobe_transfer['Software']);
$this->item_recurs['Software'] = $this->createSearchConditionUsingArray($this->noneedtobe_transfer['Software']);
// Move license of software
// TODO : should we transfer "affected license" ?
$query = "SELECT `id`, `softwareversions_id_buy`, `softwareversions_id_use`\n FROM `glpi_softwarelicenses`\n WHERE `softwares_id` IN " . $this->item_search['Software'];
foreach ($DB->request($query) as $lic) {
$this->addToBeTransfer('SoftwareLicense', $lic['id']);
// Force version transfer (remove from item_recurs)
if ($lic['softwareversions_id_buy'] > 0) {
$this->addToBeTransfer('SoftwareVersion', $lic['softwareversions_id_buy']);
}
if ($lic['softwareversions_id_use'] > 0) {
$this->addToBeTransfer('SoftwareVersion', $lic['softwareversions_id_use']);
}
}
// Licenses: from softwares and computers (affected)
$this->item_search['SoftwareLicense'] = $this->createSearchConditionUsingArray($this->needtobe_transfer['SoftwareLicense']);
$this->item_recurs['SoftwareLicense'] = $this->createSearchConditionUsingArray($this->noneedtobe_transfer['SoftwareLicense']);
// Versions: from affected licenses and installed versions
$this->item_search['SoftwareVersion'] = $this->createSearchConditionUsingArray($this->needtobe_transfer['SoftwareVersion']);
$this->item_recurs['SoftwareVersion'] = $this->createSearchConditionUsingArray($this->noneedtobe_transfer['SoftwareVersion']);
$this->item_search['NetworkEquipment'] = $this->createSearchConditionUsingArray($this->needtobe_transfer['NetworkEquipment']);
// Devices
if ($this->options['keep_device']) {
foreach (Item_Devices::getConcernedItems() as $itemtype) {
$itemtable = getTableForItemType($itemtype);
if (isset($this->item_search[$itemtype])) {
foreach (Item_Devices::getItemAffinities($itemtype) as $itemdevicetype) {
$itemdevicetable = getTableForItemType($itemdevicetype);
$devicetype = $itemdevicetype::getDeviceType();
$devicetable = getTableForItemType($devicetype);
$fk = getForeignKeyFieldForTable($devicetable);
$query = "SELECT DISTINCT `{$itemdevicetable}`.`{$fk}`,\n `{$devicetable}`.`entities_id`,\n `{$devicetable}`.`is_recursive`\n FROM `{$itemdevicetable}`\n LEFT JOIN `{$devicetable}`\n ON (`{$itemdevicetable}`.`{$fk}` = `{$devicetable}`.`id`)\n WHERE `{$itemdevicetable}`.`itemtype` = '{$itemtype}'\n AND `{$itemdevicetable}`.`items_id`\n IN " . $this->item_search[$itemtype];
foreach ($DB->request($query) as $data) {
if ($data['is_recursive'] && in_array($data['entities_id'], $to_entity_ancestors)) {
$this->addNotToBeTransfer($devicetype, $data[$fk]);
} else {
if (!isset($this->needtobe_transfer[$devicetype][$data[$fk]])) {
$this->addToBeTransfer($devicetype, $data[$fk]);
$query2 = "SELECT `{$itemdevicetable}`.`id`\n FROM `{$itemdevicetable}`\n WHERE `{$itemdevicetable}`.`{$fk}` = '" . $data[$fk] . "'\n AND `{$itemdevicetable}`.`itemtype` = '{$itemtype}'\n AND `{$itemdevicetable}`.`items_id`\n IN " . $this->item_search[$itemtype];
foreach ($DB->request($query2) as $data2) {
$this->addToBeTransfer($itemdevicetype, $data2['id']);
}
}
}
}
}
}
}
}
foreach ($CFG_GLPI['device_types'] as $itemtype) {
$this->item_search[$itemtype] = $this->createSearchConditionUsingArray($this->needtobe_transfer[$itemtype]);
$this->item_recurs[$itemtype] = $this->createSearchConditionUsingArray($this->noneedtobe_transfer[$itemtype]);
}
foreach (Item_Devices::getDeviceTypes() as $itemtype) {
$this->item_search[$itemtype] = $this->createSearchConditionUsingArray($this->needtobe_transfer[$itemtype]);
$this->item_recurs[$itemtype] = $this->createSearchConditionUsingArray($this->noneedtobe_transfer[$itemtype]);
}
// Tickets
if ($this->options['keep_ticket']) {
foreach ($CFG_GLPI["ticket_types"] as $itemtype) {
if (isset($this->item_search[$itemtype])) {
$query = "SELECT DISTINCT `glpi_tickets`.`id`\n FROM `glpi_tickets`\n LEFT JOIN `glpi_items_tickets`\n ON `glpi_items_tickets`.`tickets_id` = `glpi_tickets`.`id`\n WHERE `itemtype` = '{$itemtype}'\n AND `items_id` IN " . $this->item_search[$itemtype];
if ($result = $DB->query($query)) {
if ($DB->numrows($result) > 0) {
while ($data = $DB->fetch_assoc($result)) {
$this->addToBeTransfer('Ticket', $data['id']);
}
}
}
}
}
}
$this->item_search['Ticket'] = $this->createSearchConditionUsingArray($this->needtobe_transfer['Ticket']);
// Contract : keep / delete + clean unused / keep unused
if ($this->options['keep_contract']) {
foreach ($CFG_GLPI["contract_types"] as $itemtype) {
if (isset($this->item_search[$itemtype])) {
$itemtable = getTableForItemType($itemtype);
$this->item_search[$itemtype] = $this->createSearchConditionUsingArray($this->needtobe_transfer[$itemtype]);
// Clean DB
$query = "SELECT `glpi_contracts_items`.`id`\n FROM `glpi_contracts_items`\n LEFT JOIN `{$itemtable}`\n ON (`glpi_contracts_items`.`items_id` = `{$itemtable}`.`id`)\n WHERE `glpi_contracts_items`.`itemtype` = '{$itemtype}'\n AND `{$itemtable}`.`id` IS NULL";
if ($result = $DB->query($query)) {
if ($DB->numrows($result) > 0) {
while ($data = $DB->fetch_assoc($result)) {
$query = "DELETE\n FROM `glpi_contracts_items`\n WHERE `id` = '" . $data['id'] . "'";
$DB->query($query);
}
}
}
// Clean DB
$query = "SELECT `glpi_contracts_items`.`id`\n FROM `glpi_contracts_items`\n LEFT JOIN `glpi_contracts`\n ON (`glpi_contracts_items`.`contracts_id` = `glpi_contracts`.`id`)\n WHERE `glpi_contracts`.`id` IS NULL";
if ($result = $DB->query($query)) {
if ($DB->numrows($result) > 0) {
while ($data = $DB->fetch_assoc($result)) {
$query = "DELETE\n FROM `glpi_contracts_items`\n WHERE `id` = '" . $data['id'] . "'";
$DB->query($query);
}
}
}
$query = "SELECT `contracts_id`,\n `glpi_contracts`.`entities_id`,\n `glpi_contracts`.`is_recursive`\n FROM `glpi_contracts_items`\n LEFT JOIN `glpi_contracts`\n ON (`glpi_contracts_items`.`contracts_id` = `glpi_contracts`.`id`)\n WHERE `glpi_contracts_items`.`itemtype` = '{$itemtype}'\n AND `glpi_contracts_items`.`items_id`\n IN " . $this->item_search[$itemtype];
if ($result = $DB->query($query)) {
if ($DB->numrows($result) > 0) {
while ($data = $DB->fetch_assoc($result)) {
if ($data['is_recursive'] && in_array($data['entities_id'], $to_entity_ancestors)) {
$this->addNotToBeTransfer('Contract', $data['contracts_id']);
} else {
$this->addToBeTransfer('Contract', $data['contracts_id']);
}
}
}
}
}
}
}
$this->item_search['Contract'] = $this->createSearchConditionUsingArray($this->needtobe_transfer['Contract']);
$this->item_recurs['Contract'] = $this->createSearchConditionUsingArray($this->noneedtobe_transfer['Contract']);
// Supplier (depending of item link) / Contract - infocoms : keep / delete + clean unused / keep unused
if ($this->options['keep_supplier']) {
// Clean DB
$query = "SELECT `glpi_contracts_suppliers`.`id`\n FROM `glpi_contracts_suppliers`\n LEFT JOIN `glpi_contracts`\n ON (`glpi_contracts_suppliers`.`contracts_id` = `glpi_contracts`.`id`)\n WHERE `glpi_contracts`.`id` IS NULL";
if ($result = $DB->query($query)) {
if ($DB->numrows($result) > 0) {
while ($data = $DB->fetch_assoc($result)) {
$query = "DELETE\n FROM `glpi_contracts_suppliers`\n WHERE `id` = '" . $data['id'] . "'";
$DB->query($query);
}
}
}
// Clean DB
$query = "SELECT `glpi_contracts_suppliers`.`id`\n FROM `glpi_contracts_suppliers`\n LEFT JOIN `glpi_suppliers`\n ON (`glpi_contracts_suppliers`.`suppliers_id` = `glpi_suppliers`.`id`)\n WHERE `glpi_suppliers`.`id` IS NULL";
if ($result = $DB->query($query)) {
if ($DB->numrows($result) > 0) {
while ($data = $DB->fetch_assoc($result)) {
$query = "DELETE\n FROM `glpi_contracts_suppliers`\n WHERE `id` = '" . $data['id'] . "'";
$DB->query($query);
}
}
}
// Supplier Contract
$query = "SELECT DISTINCT `suppliers_id`,\n `glpi_suppliers`.`is_recursive`,\n `glpi_suppliers`.`entities_id`\n FROM `glpi_contracts_suppliers`\n LEFT JOIN `glpi_suppliers`\n ON (`glpi_suppliers`.`id` = `glpi_contracts_suppliers`.`suppliers_id`)\n WHERE `contracts_id` IN " . $this->item_search['Contract'];
if ($result = $DB->query($query)) {
if ($DB->numrows($result) > 0) {
while ($data = $DB->fetch_assoc($result)) {
if ($data['is_recursive'] && in_array($data['entities_id'], $to_entity_ancestors)) {
$this->addNotToBeTransfer('Supplier', $data['suppliers_id']);
} else {
$this->addToBeTransfer('Supplier', $data['suppliers_id']);
}
}
}
}
// Ticket Supplier
$query = "SELECT DISTINCT `glpi_suppliers_tickets`.`suppliers_id`,\n `glpi_suppliers`.`is_recursive`,\n `glpi_suppliers`.`entities_id`\n FROM `glpi_tickets`\n LEFT JOIN `glpi_suppliers_tickets`\n ON (`glpi_suppliers_tickets`.`tickets_id` = `glpi_tickets`.`id`)\n LEFT JOIN `glpi_suppliers`\n ON (`glpi_suppliers`.`id` = `glpi_suppliers_tickets`.`suppliers_id`)\n WHERE `glpi_suppliers_tickets`.`suppliers_id` > '0'\n AND `glpi_tickets`.`id` IN " . $this->item_search['Ticket'];
if ($result = $DB->query($query)) {
if ($DB->numrows($result) > 0) {
while ($data = $DB->fetch_assoc($result)) {
if ($data['is_recursive'] && in_array($data['entities_id'], $to_entity_ancestors)) {
$this->addNotToBeTransfer('Supplier', $data['suppliers_id']);
} else {
$this->addToBeTransfer('Supplier', $data['suppliers_id']);
}
}
}
}
// Problem Supplier
$query = "SELECT DISTINCT `glpi_problems_suppliers`.`suppliers_id`,\n `glpi_suppliers`.`is_recursive`,\n `glpi_suppliers`.`entities_id`\n FROM `glpi_problems`\n LEFT JOIN `glpi_problems_suppliers`\n ON (`glpi_problems_suppliers`.`problems_id` = `glpi_problems`.`id`)\n LEFT JOIN `glpi_suppliers`\n ON (`glpi_suppliers`.`id` = `glpi_problems_suppliers`.`suppliers_id`)\n WHERE `glpi_problems_suppliers`.`suppliers_id` > '0'\n AND `glpi_problems`.`id` IN " . $this->item_search['Problem'];
if ($result = $DB->query($query)) {
if ($DB->numrows($result) > 0) {
while ($data = $DB->fetch_assoc($result)) {
if ($data['is_recursive'] && in_array($data['entities_id'], $to_entity_ancestors)) {
$this->addNotToBeTransfer('Supplier', $data['suppliers_id']);
} else {
$this->addToBeTransfer('Supplier', $data['suppliers_id']);
}
}
}
}
// Change Supplier
$query = "SELECT DISTINCT `glpi_changes_suppliers`.`suppliers_id`,\n `glpi_suppliers`.`is_recursive`,\n `glpi_suppliers`.`entities_id`\n FROM `glpi_changes`\n LEFT JOIN `glpi_changes_suppliers`\n ON (`glpi_changes_suppliers`.`changes_id` = `glpi_changes`.`id`)\n LEFT JOIN `glpi_suppliers`\n ON (`glpi_suppliers`.`id` = `glpi_changes_suppliers`.`suppliers_id`)\n WHERE `glpi_changes_suppliers`.`suppliers_id` > '0'\n AND `glpi_changes`.`id` IN " . $this->item_search['Change'];
if ($result = $DB->query($query)) {
if ($DB->numrows($result) > 0) {
while ($data = $DB->fetch_assoc($result)) {
if ($data['is_recursive'] && in_array($data['entities_id'], $to_entity_ancestors)) {
$this->addNotToBeTransfer('Supplier', $data['suppliers_id']);
} else {
$this->addToBeTransfer('Supplier', $data['suppliers_id']);
}
}
}
}
// Supplier infocoms
if ($this->options['keep_infocom']) {
foreach (Infocom::getItemtypesThatCanHave() as $itemtype) {
if (isset($this->item_search[$itemtype])) {
$itemtable = getTableForItemType($itemtype);
$this->item_search[$itemtype] = $this->createSearchConditionUsingArray($this->needtobe_transfer[$itemtype]);
// Clean DB
$query = "SELECT `glpi_infocoms`.`id`\n FROM `glpi_infocoms`\n LEFT JOIN `{$itemtable}`\n ON (`glpi_infocoms`.`items_id` = `{$itemtable}`.`id`)\n WHERE `glpi_infocoms`.`itemtype` = '{$itemtype}'\n AND `{$itemtable}`.`id` IS NULL";
if ($result = $DB->query($query)) {
if ($DB->numrows($result) > 0) {
while ($data = $DB->fetch_assoc($result)) {
$query = "DELETE\n FROM `glpi_infocoms`\n WHERE `id` = '" . $data['id'] . "'";
$DB->query($query);
}
}
}
$query = "SELECT DISTINCT `suppliers_id`,\n `glpi_suppliers`.`is_recursive`,\n `glpi_suppliers`.`entities_id`\n FROM `glpi_infocoms`\n LEFT JOIN `glpi_suppliers`\n ON (`glpi_suppliers`.`id` = `glpi_infocoms`.`suppliers_id`)\n WHERE `suppliers_id` > '0'\n AND `itemtype` = '{$itemtype}'\n AND `items_id` IN " . $this->item_search[$itemtype];
if ($result = $DB->query($query)) {
if ($DB->numrows($result) > 0) {
while ($data = $DB->fetch_assoc($result)) {
if ($data['is_recursive'] && in_array($data['entities_id'], $to_entity_ancestors)) {
$this->addNotToBeTransfer('Supplier', $data['suppliers_id']);
} else {
$this->addToBeTransfer('Supplier', $data['suppliers_id']);
}
}
}
}
}
}
}
}
$this->item_search['Supplier'] = $this->createSearchConditionUsingArray($this->needtobe_transfer['Supplier']);
$this->item_recurs['Supplier'] = $this->createSearchConditionUsingArray($this->noneedtobe_transfer['Supplier']);
// Contact / Supplier : keep / delete + clean unused / keep unused
if ($this->options['keep_contact']) {
// Clean DB
$query = "SELECT `glpi_contacts_suppliers`.`id`\n FROM `glpi_contacts_suppliers`\n LEFT JOIN `glpi_contacts`\n ON (`glpi_contacts_suppliers`.`contacts_id` = `glpi_contacts`.`id`)\n WHERE `glpi_contacts`.`id` IS NULL";
if ($result = $DB->query($query)) {
if ($DB->numrows($result) > 0) {
while ($data = $DB->fetch_assoc($result)) {
$query = "DELETE\n FROM `glpi_contacts_suppliers`\n WHERE `id` = '" . $data['id'] . "'";
$DB->query($query);
}
}
}
// Clean DB
$query = "SELECT `glpi_contacts_suppliers`.`id`\n FROM `glpi_contacts_suppliers`\n LEFT JOIN `glpi_suppliers`\n ON (`glpi_contacts_suppliers`.`suppliers_id` = `glpi_suppliers`.`id`)\n WHERE `glpi_suppliers`.`id` IS NULL";
if ($result = $DB->query($query)) {
if ($DB->numrows($result) > 0) {
while ($data = $DB->fetch_assoc($result)) {
$query = "DELETE\n FROM `glpi_contacts_suppliers`\n WHERE `id` = '" . $data['id'] . "'";
$DB->query($query);
}
}
}
// Supplier Contact
$query = "SELECT DISTINCT `contacts_id`,\n `glpi_contacts`.`is_recursive`,\n `glpi_contacts`.`entities_id`\n FROM `glpi_contacts_suppliers`\n LEFT JOIN `glpi_contacts`\n ON (`glpi_contacts`.`id` = `glpi_contacts_suppliers`.`contacts_id`)\n WHERE `suppliers_id` IN " . $this->item_search['Supplier'];
if ($result = $DB->query($query)) {
if ($DB->numrows($result) > 0) {
while ($data = $DB->fetch_assoc($result)) {
if ($data['is_recursive'] && in_array($data['entities_id'], $to_entity_ancestors)) {
$this->addNotToBeTransfer('Contact', $data['contacts_id']);
} else {
$this->addToBeTransfer('Contact', $data['contacts_id']);
}
}
}
}
}
$this->item_search['Contact'] = $this->createSearchConditionUsingArray($this->needtobe_transfer['Contact']);
$this->item_recurs['Contact'] = $this->createSearchConditionUsingArray($this->noneedtobe_transfer['Contact']);
// Document : keep / delete + clean unused / keep unused
if ($this->options['keep_document']) {
foreach (Document::getItemtypesThatCanHave() as $itemtype) {
if (isset($this->item_search[$itemtype])) {
$itemtable = getTableForItemType($itemtype);
// Clean DB
$query = "SELECT `glpi_documents_items`.`id`\n FROM `glpi_documents_items`\n LEFT JOIN `{$itemtable}`\n ON (`glpi_documents_items`.`items_id` = `{$itemtable}`.`id`)\n WHERE `glpi_documents_items`.`itemtype` = '{$itemtype}'\n AND `{$itemtable}`.`id` IS NULL";
if ($result = $DB->query($query)) {
if ($DB->numrows($result) > 0) {
while ($data = $DB->fetch_assoc($result)) {
$query = "DELETE\n FROM `glpi_documents_items`\n WHERE `id` = '" . $data['id'] . "'";
$DB->query($query);
}
}
}
$query = "SELECT `documents_id`, `glpi_documents`.`is_recursive`,\n `glpi_documents`.`entities_id`\n FROM `glpi_documents_items`\n LEFT JOIN `glpi_documents`\n ON (`glpi_documents`.`id` = `glpi_documents_items`.`documents_id`)\n WHERE `itemtype` = '{$itemtype}'\n AND `items_id` IN " . $this->item_search[$itemtype];
if ($result = $DB->query($query)) {
if ($DB->numrows($result) > 0) {
while ($data = $DB->fetch_assoc($result)) {
if ($data['is_recursive'] && in_array($data['entities_id'], $to_entity_ancestors)) {
$this->addNotToBeTransfer('Document', $data['documents_id']);
} else {
$this->addToBeTransfer('Document', $data['documents_id']);
}
}
}
}
}
}
}
$this->item_search['Document'] = $this->createSearchConditionUsingArray($this->needtobe_transfer['Document']);
$this->item_recurs['Document'] = $this->createSearchConditionUsingArray($this->noneedtobe_transfer['Document']);
// printer -> cartridges : keep / delete + clean
if ($this->options['keep_cartridgeitem']) {
if (isset($this->item_search['Printer'])) {
$query = "SELECT `cartridgeitems_id`\n FROM `glpi_cartridges`\n WHERE `printers_id` IN " . $this->item_search['Printer'];
if ($result = $DB->query($query)) {
if ($DB->numrows($result) > 0) {
while ($data = $DB->fetch_assoc($result)) {
$this->addToBeTransfer('CartridgeItem', $data['cartridgeitems_id']);
}
}
}
}
}
$this->item_search['CartridgeItem'] = $this->createSearchConditionUsingArray($this->needtobe_transfer['CartridgeItem']);
// Init all item_search if not defined
foreach ($types as $itemtype) {
if (!isset($this->item_search[$itemtype])) {
$this->item_search[$itemtype] = "(-1)";
}
}
}