/**
  * Add a new memory component
  *
  * @param type $data
  * @param type $computers_id
  * @param type $no_history
  *
  * @return nothing
  */
 function addMemory($data, $computers_id, $no_history)
 {
     $item_DeviceMemory = new Item_DeviceMemory();
     $deviceMemory = new DeviceMemory();
     $memories_id = $deviceMemory->import($data);
     $data['devicememories_id'] = $memories_id;
     $data['itemtype'] = 'Computer';
     $data['items_id'] = $computers_id;
     $data['is_dynamic'] = 1;
     $data['_no_history'] = $no_history;
     $item_DeviceMemory->add($data, array(), !$no_history);
 }
 /**
  * @param $plugin_ocsinventoryng_ocsservers_id
  * @param $itemtype
  * @param int $ID
  * @param $ocsSnmp
  * @param $loc_id
  * @param $dom_id
  * @param $action
  * @param bool $linked
  * @return int
  */
 static function addOrUpdateComputer($plugin_ocsinventoryng_ocsservers_id, $itemtype, $ID = 0, $ocsSnmp, $loc_id, $dom_id, $action, $linked = false)
 {
     global $DB;
     $snmpDevice = new $itemtype();
     $cfg_ocs = PluginOcsinventoryngOcsServer::getConfig($plugin_ocsinventoryng_ocsservers_id);
     $input = array("is_dynamic" => 1, "entities_id" => isset($_SESSION['glpiactive_entity']) ? $_SESSION['glpiactive_entity'] : 0);
     if ($cfg_ocs['importsnmp_name'] && $action == "add" || $cfg_ocs['linksnmp_name'] && $linked || $action == "update" && $cfg_ocs['importsnmp_name'] && !$linked || $action == "update" && $cfg_ocs['linksnmp_name'] && $linked) {
         $input["name"] = $ocsSnmp['META']['NAME'];
     }
     if ($cfg_ocs['importsnmp_contact'] && $action == "add" || $cfg_ocs['linksnmp_contact'] && $linked || $action == "update" && $cfg_ocs['importsnmp_name'] && !$linked || $action == "update" && $cfg_ocs['linksnmp_name'] && $linked) {
         $input["contact"] = $ocsSnmp['META']['CONTACT'];
     }
     if ($cfg_ocs['importsnmp_comment'] && $action == "add" || $cfg_ocs['linksnmp_comment'] && $linked || $action == "update" && $cfg_ocs['importsnmp_name'] && !$linked || $action == "update" && $cfg_ocs['linksnmp_name'] && $linked) {
         $input["comment"] = $ocsSnmp['META']['DESCRIPTION'];
     }
     if ($loc_id > 0) {
         $input["locations_id"] = $loc_id;
     }
     if ($dom_id > 0 && $itemtype != "Phone") {
         $input["domains_id"] = $dom_id;
     }
     $id_item = 0;
     if ($action == "add") {
         $id_item = $snmpDevice->add($input, array('unicity_error_message' => true), $cfg_ocs['history_hardware']);
     } else {
         $input["id"] = $ID;
         $id_item = $ID;
         if ($snmpDevice->getFromDB($id_item)) {
             $input["entities_id"] = $snmpDevice->fields['entities_id'];
         }
         $snmpDevice->update($input, $cfg_ocs['history_hardware'], array('unicity_error_message' => false, '_no_history' => !$cfg_ocs['history_hardware']));
     }
     if ($id_item > 0 && isset($ocsSnmp['MEMORIES']) && ($cfg_ocs['importsnmp_computermemory'] && $action == "add" || $cfg_ocs['linksnmp_computermemory'] && $linked || $action == "update" && $cfg_ocs['importsnmp_computermemory'] && !$linked || $action == "update" && $cfg_ocs['linksnmp_computermemory'] && $linked) && count($ocsSnmp['MEMORIES']) > 0 && $ocsSnmp['MEMORIES'][0]['CAPACITY'] > 0) {
         $dev['designation'] = __('Computer Memory', 'ocsinventoryng');
         $item = new $itemtype();
         $entity = isset($_SESSION['glpiactive_entity']) ? $_SESSION['glpiactive_entity'] : 0;
         if ($item->getFromDB($id_item)) {
             $entity = $item->fields['entities_id'];
         }
         $dev['entities_id'] = $entity;
         $device = new DeviceMemory();
         $device_id = $device->import($dev);
         if ($device_id) {
             $CompDevice = new Item_DeviceMemory();
             if ($cfg_ocs['history_devices']) {
                 $table = getTableForItemType("Item_DeviceMemory");
                 $query = "DELETE\n                            FROM `" . $table . "`\n                            WHERE `items_id` = '" . $id_item . "'\n                            AND `itemtype` = '" . $itemtype . "'";
                 $DB->query($query);
             }
             //            CANNOT USE BEFORE 9.1.2 - for _no_history problem
             //            $CompDevice->deleteByCriteria(array('items_id' => $id_item,
             //               'itemtype' => $itemtype), 1);
             $CompDevice->add(array('items_id' => $id_item, 'itemtype' => $itemtype, 'size' => $ocsSnmp['MEMORIES'][0]['CAPACITY'], 'entities_id' => $entity, 'devicememories_id' => $device_id, 'is_dynamic' => 1), array(), $cfg_ocs['history_devices']);
         }
     }
     if ($id_item > 0 && isset($ocsSnmp['NETWORKS']) && ($cfg_ocs['importsnmp_computernetworkcards'] && $action == "add" || $cfg_ocs['linksnmp_computernetworkcards'] && $linked || $action == "update" && $cfg_ocs['importsnmp_computernetworkcards'] && !$linked || $action == "update" && $cfg_ocs['linksnmp_computernetworkcards'] && $linked) && count($ocsSnmp['NETWORKS']) > 0) {
         $CompDevice = new Item_DeviceNetworkCard();
         if ($cfg_ocs['history_devices']) {
             $table = getTableForItemType("Item_DeviceNetworkCard");
             $query = "DELETE\n                            FROM `" . $table . "`\n                            WHERE `items_id` = '" . $id_item . "'\n                            AND `itemtype` = '" . $itemtype . "'";
             $DB->query($query);
         }
         //            CANNOT USE BEFORE 9.1.2 - for _no_history problem
         //         $CompDevice->deleteByCriteria(array('items_id' => $id_item,
         //                                             'itemtype' => $itemtype), 1);
         foreach ($ocsSnmp['NETWORKS'] as $k => $net) {
             $dev["designation"] = $net['SLOT'];
             $dev["comment"] = $net['TYPE'];
             $mac = $net['MACADDR'];
             /*$speed = 0;
               if (strstr($processor['SPEED'], "GHz")) {
                  $speed = str_replace("GHz", "", $processor['SPEED']);
                  $speed = $speed * 1000;
               }
               if (strstr($processor['SPEED'], "MHz")) {
                  $speed = str_replace("MHz", "", $processor['SPEED']);
               }*/
             $item = new $itemtype();
             $entity = isset($_SESSION['glpiactive_entity']) ? $_SESSION['glpiactive_entity'] : 0;
             if ($item->getFromDB($id_item)) {
                 $entity = $item->fields['entities_id'];
             }
             $dev['entities_id'] = $entity;
             $device = new DeviceNetworkCard();
             $device_id = $device->import($dev);
             if ($device_id) {
                 $CompDevice->add(array('items_id' => $id_item, 'itemtype' => $itemtype, 'mac' => $mac, 'entities_id' => $entity, 'devicenetworkcards_id' => $device_id, 'is_dynamic' => 1), array(), $cfg_ocs['history_devices']);
             }
         }
     }
     if ($id_item > 0 && isset($ocsSnmp['SOFTWARES']) && ($cfg_ocs['importsnmp_computersoftwares'] && $action == "add" || $cfg_ocs['linksnmp_computersoftwares'] && $linked || $action == "update" && $cfg_ocs['importsnmp_computersoftwares'] && !$linked || $action == "update" && $cfg_ocs['linksnmp_computersoftwares'] && $linked) && count($ocsSnmp['SOFTWARES']) > 0) {
         $entity = isset($_SESSION['glpiactive_entity']) ? $_SESSION['glpiactive_entity'] : 0;
         if ($item->getFromDB($id_item)) {
             $entity = $item->fields['entities_id'];
         }
         PluginOcsinventoryngOcsServer::updateSoftware($cfg_ocs, $id_item, $ocsSnmp["SOFTWARES"], $entity);
     }
     if ($id_item > 0 && isset($ocsSnmp['CPU']) && ($cfg_ocs['importsnmp_computerprocessors'] && $action == "add" || $cfg_ocs['linksnmp_computerprocessors'] && $linked || $action == "update" && $cfg_ocs['importsnmp_computerprocessors'] && !$linked || $action == "update" && $cfg_ocs['linksnmp_computerprocessors'] && $linked) && count($ocsSnmp['CPU']) > 0) {
         $CompDevice = new Item_DeviceProcessor();
         if ($cfg_ocs['history_devices']) {
             $table = getTableForItemType("Item_DeviceProcessor");
             $query = "DELETE\n                            FROM `" . $table . "`\n                            WHERE `items_id` = '" . $id_item . "'\n                            AND `itemtype` = '" . $itemtype . "'";
             $DB->query($query);
         }
         //            CANNOT USE BEFORE 9.1.2 - for _no_history problem
         //         $CompDevice->deleteByCriteria(array('items_id' => $id_item,
         //                                             'itemtype' => $itemtype), 1);
         foreach ($ocsSnmp['CPU'] as $k => $processor) {
             $dev["designation"] = $processor['TYPE'];
             $dev["manufacturers_id"] = Dropdown::importExternal('Manufacturer', PluginOcsinventoryngOcsServer::encodeOcsDataInUtf8($cfg_ocs['ocs_db_utf8'], $processor['MANUFACTURER']));
             $speed = 0;
             if (strstr($processor['SPEED'], "GHz")) {
                 $speed = str_replace("GHz", "", $processor['SPEED']);
                 $speed = $speed * 1000;
             }
             if (strstr($processor['SPEED'], "MHz")) {
                 $speed = str_replace("MHz", "", $processor['SPEED']);
             }
             $item = new $itemtype();
             $entity = isset($_SESSION['glpiactive_entity']) ? $_SESSION['glpiactive_entity'] : 0;
             if ($item->getFromDB($id_item)) {
                 $entity = $item->fields['entities_id'];
             }
             $dev['entities_id'] = $entity;
             $device = new DeviceProcessor();
             $device_id = $device->import($dev);
             if ($device_id) {
                 $CompDevice->add(array('items_id' => $id_item, 'itemtype' => $itemtype, 'frequency' => $speed, 'entities_id' => $entity, 'deviceprocessors_id' => $device_id, 'is_dynamic' => 1), array(), $cfg_ocs['history_devices']);
             }
         }
     }
     if ($id_item > 0 && isset($ocsSnmp['VIRTUALMACHINES']) && ($cfg_ocs['importsnmp_computervm'] && $action == "add" || $cfg_ocs['linksnmp_computervm'] && $linked || $action == "update" && $cfg_ocs['importsnmp_computervm'] && !$linked || $action == "update" && $cfg_ocs['linksnmp_computervm'] && $linked) && count($ocsSnmp['VIRTUALMACHINES']) > 0) {
         $already_processed = array();
         $virtualmachine = new ComputerVirtualMachine();
         foreach ($ocsSnmp['VIRTUALMACHINES'] as $k => $ocsVirtualmachine) {
             $ocsVirtualmachine = Toolbox::clean_cross_side_scripting_deep(Toolbox::addslashes_deep($ocsVirtualmachine));
             $vm = array();
             $vm['name'] = $ocsVirtualmachine['NAME'];
             $vm['vcpu'] = $ocsVirtualmachine['CPU'];
             $vm['ram'] = $ocsVirtualmachine['MEMORY'];
             $vm['uuid'] = $ocsVirtualmachine['UUID'];
             $vm['computers_id'] = $id_item;
             $vm['is_dynamic'] = 1;
             $vm['virtualmachinestates_id'] = Dropdown::importExternal('VirtualMachineState', $ocsVirtualmachine['POWER']);
             //$vm['virtualmachinetypes_id'] = Dropdown::importExternal('VirtualMachineType', $ocsVirtualmachine['VMTYPE']);
             //$vm['virtualmachinesystems_id'] = Dropdown::importExternal('VirtualMachineType', $ocsVirtualmachine['SUBSYSTEM']);
             $query = "SELECT `id`\n                         FROM `glpi_computervirtualmachines`\n                         WHERE `computers_id`='{$id_item}'\n                            AND `is_dynamic`";
             if ($ocsVirtualmachine['UUID']) {
                 $query .= " AND `uuid`='" . $ocsVirtualmachine['UUID'] . "'";
             } else {
                 // Failback on name
                 $query .= " AND `name`='" . $ocsVirtualmachine['NAME'] . "'";
             }
             $results = $DB->query($query);
             if ($DB->numrows($results) > 0) {
                 $id = $DB->result($results, 0, 'id');
             } else {
                 $id = 0;
             }
             if (!$id) {
                 $virtualmachine->reset();
                 $id_vm = $virtualmachine->add($vm, array(), $cfg_ocs['history_vm']);
                 if ($id_vm) {
                     $already_processed[] = $id_vm;
                 }
             } else {
                 if ($virtualmachine->getFromDB($id)) {
                     $vm['id'] = $id;
                     $virtualmachine->update($vm, $cfg_ocs['history_vm']);
                 }
                 $already_processed[] = $id;
             }
             // Delete Unexisting Items not found in OCS
             //Look for all ununsed virtual machines
             $query = "SELECT `id`\n                      FROM `glpi_computervirtualmachines`\n                      WHERE `computers_id`='{$id_item}'\n                         AND `is_dynamic`";
             if (!empty($already_processed)) {
                 $query .= "AND `id` NOT IN (" . implode(',', $already_processed) . ")";
             }
             foreach ($DB->request($query) as $data) {
                 //Delete all connexions
                 $virtualmachine->delete(array('id' => $data['id'], '_ocsservers_id' => $plugin_ocsinventoryng_ocsservers_id, '_no_history' => !$cfg_ocs['history_vm']), true, $cfg_ocs['history_vm']);
             }
         }
     }
     if ($id_item > 0 && ($cfg_ocs['importsnmp_createport'] && $action == "add" || $cfg_ocs['linksnmp_createport'] && $linked || $action == "update" && $cfg_ocs['importsnmp_createport'] && !$linked || $action == "update" && $cfg_ocs['linksnmp_createport'] && $linked)) {
         //Add network port
         $ip = $ocsSnmp['META']['IPADDR'];
         $mac = $ocsSnmp['META']['MACADDR'];
         $np = new NetworkPort();
         $np->getFromDBByQuery("WHERE `mac` LIKE '{$mac}' AND `items_id` = '{$id_item}' AND `itemtype` LIKE '{$itemtype}' ");
         if (count($np->fields) < 1) {
             $item = new $itemtype();
             $entity = isset($_SESSION['glpiactive_entity']) ? $_SESSION['glpiactive_entity'] : 0;
             if ($item->getFromDB($id_item)) {
                 $entity = $item->fields['entities_id'];
             }
             $port_input = array('name' => $ocsSnmp['META']['NAME'], 'mac' => $mac, 'items_id' => $id_item, 'itemtype' => $itemtype, 'instantiation_type' => "NetworkPortEthernet", "entities_id" => $entity, "NetworkName__ipaddresses" => array("-100" => $ip), '_create_children' => 1, 'is_deleted' => 0);
             $np->add($port_input, array(), $cfg_ocs['history_network']);
         }
     }
     return $id_item;
 }