/**
  * @test
  */
 public function PrinterToInventoryWithoutIP()
 {
     self::restore_database();
     $printer = new Printer();
     $networkport = new NetworkPort();
     $networkName = new NetworkName();
     $pfPrinter = new PluginFusioninventoryPrinter();
     $pfTask = new PluginFusioninventoryTask();
     $pfTaskjob = new PluginFusioninventoryTaskjob();
     $computer = new Computer();
     $pfAgent = new PluginFusioninventoryAgent();
     $communication = new PluginFusioninventoryCommunication();
     // Create computers + agents
     $input = array('entities_id' => 0, 'name' => 'computer1');
     $computers_id = $computer->add($input);
     $input = array('entities_id' => 0, 'name' => 'computer1', 'version' => '{"INVENTORY":"v2.3.11"}', 'device_id' => 'computer1', 'useragent' => 'FusionInventory-Agent_v2.3.11', 'computers_id' => $computers_id);
     $pfAgent->add($input);
     // Create printer
     $input = array('name' => 'printer 001', 'entities_id' => 0);
     $printers_id = $printer->add($input);
     // Add port
     $networkports_id = $networkport->add(array('itemtype' => 'Printer', 'instantiation_type' => 'NetworkPortEthernet', 'items_id' => $printers_id, 'entities_id' => 0));
     $networknames_id = $networkName->add(array('entities_id' => 0, 'itemtype' => 'NetworkPort', 'items_id' => $networkports_id));
     $input = array('printers_id' => 1, 'plugin_fusioninventory_configsecurities_id' => 2);
     $pfPrinter->add($input);
     // Add task
     // create task
     $input = array('entities_id' => 0, 'name' => 'network inventory', 'is_active' => 1);
     $tasks_id = $pfTask->add($input);
     // create taskjob
     $input = array('plugin_fusioninventory_tasks_id' => $tasks_id, 'entities_id' => 0, 'name' => 'inventory', 'method' => 'networkinventory', 'targets' => '[{"Printer":"' . $printers_id . '"}]', 'actors' => '[{"PluginFusioninventoryAgent":"1"}]');
     $pfTaskjob->add($input);
     PluginFusioninventoryTask::cronTaskscheduler();
     $data = $pfTask->getJoblogs(array(1));
     // Task is prepared
     // Agent will get data
     $communication->getTaskAgent(1);
     $message = $communication->getMessage();
     $json = json_encode($message);
     $array = json_decode($json, TRUE);
     $ref = array();
     $this->assertEquals($ref, $array, 'XML of SNMP inventory task');
 }
 /**
  * Function to update Printer
  *
  * @param array $a_inventory data fron agent inventory
  * @param id $items_id id of the printer
  *
  * @return nothing
  */
 function updatePrinter($a_inventory, $items_id)
 {
     global $DB;
     $printer = new Printer();
     $pfPrinter = new PluginFusioninventoryPrinter();
     $printer->getFromDB($items_id);
     if (!isset($_SESSION['glpiactiveentities_string'])) {
         $_SESSION['glpiactiveentities_string'] = $printer->fields['entities_id'];
     }
     if (!isset($_SESSION['glpiactiveentities'])) {
         $_SESSION['glpiactiveentities'] = array($printer->fields['entities_id']);
     }
     if (!isset($_SESSION['glpiactive_entity'])) {
         $_SESSION['glpiactive_entity'] = $printer->fields['entities_id'];
     }
     // * Printer
     $db_printer = $printer->fields;
     $a_lockable = PluginFusioninventoryLock::getLockFields('glpi_printers', $items_id);
     $a_ret = PluginFusioninventoryToolbox::checkLock($a_inventory['Printer'], $db_printer, $a_lockable);
     $a_inventory['Printer'] = $a_ret[0];
     $input = $a_inventory['Printer'];
     $input['id'] = $items_id;
     $printer->update($input);
     // * Printer fusion (ext)
     $db_printer = array();
     $query = "SELECT *\n            FROM `" . getTableForItemType("PluginFusioninventoryPrinter") . "`\n            WHERE `printers_id` = '{$items_id}'";
     $result = $DB->query($query);
     while ($data = $DB->fetch_assoc($result)) {
         foreach ($data as $key => $value) {
             $db_printer[$key] = Toolbox::addslashes_deep($value);
         }
     }
     if (count($db_printer) == '0') {
         // Add
         $a_inventory['PluginFusioninventoryPrinter']['printers_id'] = $items_id;
         $pfPrinter->add($a_inventory['PluginFusioninventoryPrinter']);
     } else {
         // Update
         $idtmp = $db_printer['id'];
         unset($db_printer['id']);
         unset($db_printer['printers_id']);
         unset($db_printer['plugin_fusioninventory_configsecurities_id']);
         $a_ret = PluginFusioninventoryToolbox::checkLock($a_inventory['PluginFusioninventoryPrinter'], $db_printer);
         $a_inventory['PluginFusioninventoryPrinter'] = $a_ret[0];
         $input = $a_inventory['PluginFusioninventoryPrinter'];
         $input['id'] = $idtmp;
         $pfPrinter->update($input);
     }
     // * Ports
     $this->importPorts($a_inventory, $items_id);
     // Page counters
     $this->importPageCounters($a_inventory['pagecounters'], $items_id);
     // Cartridges
     $this->importCartridges($a_inventory['cartridge'], $items_id);
 }
 /**
  * Function to import discovered device
  *
  * @param $items_id id of the device to import
  *
  * @return nothing
  *
  **/
 function import($items_id, $Import = 0, $NoImport = 0)
 {
     global $DB;
     $NetworkPort = new NetworkPort();
     $a_NetworkPorts = $NetworkPort->find("`items_id` = '" . $items_id . "'\n                      AND `itemtype` = 'PluginFusioninventoryUnmanaged'");
     $this->getFromDB($items_id);
     $this->fields = Toolbox::addslashes_deep($this->fields);
     $data = array();
     switch ($this->fields['item_type']) {
         case 'Printer':
             $Printer = new Printer();
             $data["entities_id"] = $this->fields["entities_id"];
             if (!empty($this->fields["name"])) {
                 $data["name"] = $this->fields["name"];
             }
             $data["locations_id"] = $this->fields["locations_id"];
             $data["serial"] = $this->fields["serial"];
             $data["otherserial"] = $this->fields["otherserial"];
             $data["contact"] = $this->fields["contact"];
             $data["domain"] = $this->fields["domain"];
             $data["comment"] = $this->fields["comment"];
             $data["is_dynamic"] = 1;
             $printer_id = $Printer->add($data);
             foreach ($a_NetworkPorts as $data_Port) {
                 $data_Port['items_id'] = $printer_id;
                 $data_Port['itemtype'] = $Printer->getType();
                 $NetworkPort->update($data_Port);
             }
             // Import SNMP
             $pfPrinter = new PluginFusioninventoryPrinter();
             $_SESSION['glpi_plugins_fusinvsnmp_table'] = "glpi_plugin_fusioninventory_printers";
             $query = "SELECT *\n                      FROM `glpi_plugin_fusioninventory_printers`\n                      WHERE `printers_id`='" . $printer_id . "' ";
             $result = $DB->query($query);
             $data = array();
             if ($DB->numrows($result) > 0) {
                 $data = $DB->fetch_assoc($result);
             }
             $data['sysdescr'] = $this->fields['sysdescr'];
             $data['plugin_fusioninventory_configsecurities_id'] = $this->fields['plugin_fusioninventory_configsecurities_id'];
             if ($DB->numrows($result) == 0) {
                 $data['printers_id'] = $printer_id;
                 $pfPrinter->add($data);
             } else {
                 $pfPrinter->update($data);
             }
             $this->deleteFromDB($items_id, 1);
             $Import++;
             break;
         case 'NetworkEquipment':
             $NetworkEquipment = new NetworkEquipment();
             $data["entities_id"] = $this->fields["entities_id"];
             if (!empty($this->fields["name"])) {
                 $data["name"] = $this->fields["name"];
             }
             $data["locations_id"] = $this->fields["locations_id"];
             $data["serial"] = $this->fields["serial"];
             $data["otherserial"] = $this->fields["otherserial"];
             $data["contact"] = $this->fields["contact"];
             $data["domain"] = $this->fields["domain"];
             $data["comment"] = $this->fields["comment"];
             $data["is_dynamic"] = 1;
             //            $data_Port = current($a_NetworkPorts);
             //            $data["ip"] = $data_Port["ip"];
             //            $data["mac"] = $data_Port["mac"];
             $NetworkEquipment_id = $NetworkEquipment->add($data);
             foreach ($a_NetworkPorts as $data_Port) {
                 $data_Port['items_id'] = $NetworkEquipment_id;
                 $data_Port['itemtype'] = $NetworkEquipment->getType();
                 $NetworkPort->update($data_Port);
             }
             $pfNetworkEquipment = new PluginFusioninventoryNetworkEquipment();
             $_SESSION['glpi_plugins_fusinvsnmp_table'] = "glpi_plugin_fusioninventory_networkequipments";
             $query = "SELECT *\n                      FROM `glpi_plugin_fusioninventory_networkequipments`\n                      WHERE `networkequipments_id`='" . $NetworkEquipment_id . "' ";
             $result = $DB->query($query);
             $data = array();
             if ($DB->numrows($result) > 0) {
                 $data = $DB->fetch_assoc($result);
             }
             $data['sysdescr'] = $this->fields['sysdescr'];
             $data['plugin_fusioninventory_configsecurities_id'] = $this->fields['plugin_fusioninventory_configsecurities_id'];
             if ($DB->numrows($result) == 0) {
                 $data['networkequipments_id'] = $NetworkEquipment_id;
                 $pfNetworkEquipment->add($data);
             } else {
                 $pfNetworkEquipment->update($data);
             }
             $this->deleteFromDB($items_id, 1);
             $Import++;
             break;
         case 'Peripheral':
             $Peripheral = new Peripheral();
             $data["entities_id"] = $this->fields["entities_id"];
             if (!empty($this->fields["name"])) {
                 $data["name"] = $this->fields["name"];
             }
             $data["locations_id"] = $this->fields["locations_id"];
             $data["serial"] = $this->fields["serial"];
             $data["otherserial"] = $this->fields["otherserial"];
             $data["contact"] = $this->fields["contact"];
             $data["comment"] = $this->fields["comment"];
             $data["is_dynamic"] = 1;
             $Peripheral_id = $Peripheral->add($data);
             foreach ($a_NetworkPorts as $data_Port) {
                 $data_Port['items_id'] = $Peripheral_id;
                 $data_Port['itemtype'] = $Peripheral->getType();
                 $NetworkPort->update($data_Port);
             }
             $this->deleteFromDB($items_id, 1);
             $Import++;
             break;
         case 'Computer':
             $Computer = new Computer();
             $data["entities_id"] = $this->fields["entities_id"];
             if (!empty($this->fields["name"])) {
                 $data["name"] = $this->fields["name"];
             }
             $data["locations_id"] = $this->fields["locations_id"];
             $data["serial"] = $this->fields["serial"];
             $data["otherserial"] = $this->fields["otherserial"];
             $data["contact"] = $this->fields["contact"];
             $data["domain"] = $this->fields["domain"];
             $data["comment"] = $this->fields["comment"];
             $data["is_dynamic"] = 1;
             $Computer_id = $Computer->add($data);
             foreach ($a_NetworkPorts as $data_Port) {
                 $data_Port['items_id'] = $Computer_id;
                 $data_Port['itemtype'] = $Computer->getType();
                 $NetworkPort->update($data_Port);
             }
             $this->deleteFromDB($items_id, 1);
             $Import++;
             break;
         case 'Phone':
             $Phone = new Phone();
             $data["entities_id"] = $this->fields["entities_id"];
             $data["name"] = $this->fields["name"];
             $data["locations_id"] = $this->fields["locations_id"];
             $data["serial"] = $this->fields["serial"];
             $data["otherserial"] = $this->fields["otherserial"];
             $data["contact"] = $this->fields["contact"];
             $data["comment"] = $this->fields["comment"];
             $data["is_dynamic"] = 1;
             $phone_id = $Phone->add($data);
             foreach ($a_NetworkPorts as $data_Port) {
                 $data_Port['items_id'] = $phone_id;
                 $data_Port['itemtype'] = $Phone->getType();
                 $NetworkPort->update($data_Port);
             }
             $this->deleteFromDB($items_id, 1);
             $Import++;
             break;
         default:
             $NoImport++;
             break;
     }
     return array($Import, $NoImport);
 }