/**
  * @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 addOrUpdateOther($plugin_ocsinventoryng_ocsservers_id, $itemtype, $ID = 0, $ocsSnmp, $loc_id, $dom_id, $action, $linked = false)
 {
     $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 && ($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;
 }
 /**
  * import ipdiscover object
  * @param type $ipDiscoveryObject array
  * @param type $plugin_ocsinventoryng_ocsservers_id integer
  * @param $subnet
  * @return array
  */
 static function importIpDiscover($ipDiscoveryObject, $plugin_ocsinventoryng_ocsservers_id, $subnet)
 {
     global $DB;
     $id = null;
     $identify = false;
     $cfg_ocs = PluginOcsinventoryngOcsServer::getConfig($plugin_ocsinventoryng_ocsservers_id);
     if (isset($ipDiscoveryObject["ocsItemType"]) && $ipDiscoveryObject["ocsItemType"] == Dropdown::EMPTY_VALUE) {
         return array('status' => PluginOcsinventoryngOcsServer::IPDISCOVER_FAILED_IMPORT);
     }
     if ($ipDiscoveryObject["itemDescription"] == '') {
         return array('status' => PluginOcsinventoryngOcsServer::IPDISCOVER_FAILED_IMPORT);
     }
     if ($ipDiscoveryObject["itemName"] == "") {
         $ipDiscoveryObject["itemName"] = $ipDiscoveryObject["itemDescription"];
     }
     switch ($ipDiscoveryObject["glpiItemType"]) {
         //empty dropdown value
         case '0':
             return array('status' => PluginOcsinventoryngOcsServer::IPDISCOVER_FAILED_IMPORT);
     }
     if (isset($ipDiscoveryObject["ocsItemType"])) {
         $identify = true;
     }
     $mac = $ipDiscoveryObject["macAdress"];
     $netPort = new NetworkPort();
     $netPort->getFromDBByQuery("WHERE `mac` = '{$mac}' ");
     if (count($netPort->fields) < 1) {
         $input = array('is_dynamic' => 1, 'locations_id' => 0, 'domains_id' => 0, 'entities_id' => $ipDiscoveryObject["entity"], 'name' => $ipDiscoveryObject["itemName"], 'comment' => $ipDiscoveryObject["itemDescription"]);
         $device = new $ipDiscoveryObject["glpiItemType"]();
         $id = $device->add($input, array(), $cfg_ocs['history_devices']);
         //ipdiscover link
         $date = date("Y-m-d H:i:s");
         $glpiType = $ipDiscoveryObject["glpiItemType"];
         $ip = $ipDiscoveryObject["itemIp"];
         $glpiQuery = "INSERT INTO `glpi_plugin_ocsinventoryng_ipdiscoverocslinks`\n                       (`items_id`,`itemtype`,`macaddress`,`last_update`,`subnet`,`plugin_ocsinventoryng_ocsservers_id`)\n                       VALUES('{$id}','{$glpiType}','{$mac}','{$date}','{$subnet}','{$plugin_ocsinventoryng_ocsservers_id}')";
         $DB->query($glpiQuery);
         //add port
         $port_input = array('name' => $ipDiscoveryObject["itemName"] . "-" . $ip, 'mac' => $mac, 'items_id' => $id, 'itemtype' => $glpiType, 'instantiation_type' => "NetworkPortEthernet", "entities_id" => $ipDiscoveryObject["entity"], "NetworkName__ipaddresses" => array("-100" => $ip), '_create_children' => 1, 'is_deleted' => 0);
         $netPort->add($port_input, array(), $cfg_ocs['history_network']);
         /* $netPortInput = array(
                       "itemtype"           => $glpiType,
                       "items_id"           => $id,
                       'entities_id'        => $ipDiscoveryObject["entity"],
                       "name"               => $ipDiscoveryObject["itemName"] . "-" . $ip,
                       "instantiation_type" => "NetworkPortEthernet",
                       "mac"                => $mac
                   );
         
                   $netPort->splitInputForElements($netPortInput);
                   $NewNetPortId = $netPort->add($netPortInput);
                   $netPort->updateDependencies(1);
                   //make link to IPAdress manualy
         
                   //add ipAdress
                   $networkName= new NetworkName();
                   $networkNameId=$networkName->add(array("items_id"=>$NewNetPortId,"itemtype"=>"NetworkPort"));
                   $ipAdresses = new IPAddress();
         
                   $input = array('name'        => $ip,
                               'itemtype'    => 'NetworkName',
                               'items_id'    => $networkNameId,
                               'is_deleted'  => 0,
                                'mainitems_id'=>$id,
                                'mainitemtype'=>$glpiType);
                   $ipAdresses->add($input);*/
     }
     if ($id && $identify) {
         //identify object
         //WAS IS DAS ? CHMA
         $userId = Session::getLoginUserID();
         $query = "SELECT `glpi_users`.`name` \n                         FROM `glpi_users`\n                         WHERE glpi_users.id like '{$userId}'";
         $queryResult = $DB->query($query);
         $userAssoc = $DB->fetch_assoc($queryResult);
         if ($userAssoc) {
             $ocsClient = new PluginOcsinventoryngOcsServer();
             $DBOCS = $ocsClient->getDBocs($plugin_ocsinventoryng_ocsservers_id)->getDB();
             $ocsType = $ipDiscoveryObject["ocsItemType"];
             $description = $ipDiscoveryObject["itemDescription"];
             $user = $userAssoc["name"];
             $ocsQuery = "INSERT INTO `network_devices` (`description`,`type`,`macaddr`,`user`)\n                            VALUES('{$description}','{$ocsType}','{$mac}','{$user}')";
             $DBOCS->query($ocsQuery);
         }
     }
     if ($id) {
         return array('status' => PluginOcsinventoryngOcsServer::IPDISCOVER_IMPORTED);
     } else {
         Session::addMessageAfterRedirect($mac . " : " . __('Unable to add. an object with same MAC address already exists.', 'ocsinventoryng'), false, ERROR);
         return array('status' => PluginOcsinventoryngOcsServer::IPDISCOVER_FAILED_IMPORT);
     }
 }