/** * Check delete / purge on a template of printer */ public function testPrinterTemplate() { $printer = new Printer(); // Create $id[0] = $printer->add(array('name' => "Printer 1", 'entities_id' => 0, 'is_template' => 1)); $this->assertGreaterThan(0, $id[0], "Fail to create Printer Template"); $this->assertTrue($printer->getFromDB($id[0]), "Fail: can't read Template"); $this->assertEquals(0, $printer->fields['is_deleted'], "Fail: is_deleted set"); $this->assertEquals(1, $printer->fields['is_template'], "Fail: is_template not set"); // Delete (= purge) $this->assertTrue($printer->delete(array('id' => $id[0]), 0), "Fail: can't delete Template"); $this->assertFalse($printer->getFromDB($id[0]), "Fail: can read Template (deleted)"); }
/** * 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); }
/** * @covers Printer::delete * @covers Printer::restore */ public function testDelete() { $obj = new Printer(); $this->assertTrue($obj->maybeDeleted()); // Add $id = $obj->add(['name' => __METHOD__]); $this->assertGreaterThan(0, $id); $this->assertTrue($obj->getFromDB($id)); $this->assertEquals(0, $obj->getField('is_deleted')); $this->assertEquals(0, $obj->isDeleted()); // Delete $this->assertTrue($obj->delete(['id' => $id], 0)); $this->assertTrue($obj->getFromDB($id)); $this->assertEquals(1, $obj->getField('is_deleted')); $this->assertEquals(1, $obj->isDeleted()); // Restore $this->assertTrue($obj->restore(['id' => $id], 0)); $this->assertTrue($obj->getFromDB($id)); $this->assertEquals(0, $obj->getField('is_deleted')); $this->assertEquals(0, $obj->isDeleted()); // Purge $this->assertTrue($obj->delete(['id' => $id], 1)); $this->assertFalse($obj->getFromDB($id)); }
/** * Show printer graph form **/ function showGraph($id, $options = array()) { global $DB, $CFG_GLPI; $printer = new Printer(); $where = ''; $begin = ''; $end = ''; $timeUnit = 'day'; $graphField = 'pages_total'; $pagecounters = array(); $graphType = 'day'; if (isset($_SESSION['glpi_plugin_fusioninventory_graph_begin'])) { $begin = $_SESSION['glpi_plugin_fusioninventory_graph_begin']; } if ($begin == 'NULL' or $begin == '') { $begin = date("Y-m-01"); // first day of current month } if (isset($_SESSION['glpi_plugin_fusioninventory_graph_end'])) { $end = $_SESSION['glpi_plugin_fusioninventory_graph_end']; } if (isset($_SESSION['glpi_plugin_fusioninventory_graph_type'])) { $graphType = $_SESSION['glpi_plugin_fusioninventory_graph_type']; } if ($end == 'NULL' or $end == '') { $end = date("Y-m-d"); // today } if (isset($_SESSION['glpi_plugin_fusioninventory_graph_timeUnit'])) { $timeUnit = $_SESSION['glpi_plugin_fusioninventory_graph_timeUnit']; } if (!isset($_SESSION['glpi_plugin_fusioninventory_graph_printersComp'])) { $_SESSION['glpi_plugin_fusioninventory_graph_printersComp'] = array(); } if (isset($_SESSION['glpi_plugin_fusioninventory_graph_printerCompAdd'])) { $printerCompAdd = $_SESSION['glpi_plugin_fusioninventory_graph_printerCompAdd']; if (!key_exists($printerCompAdd, $_SESSION['glpi_plugin_fusioninventory_graph_printersComp'])) { $oPrinter = new Printer(); if ($oPrinter->getFromDB($printerCompAdd)) { $_SESSION['glpi_plugin_fusioninventory_graph_printersComp'][$printerCompAdd] = $oPrinter->getField('name'); } } } elseif (isset($_SESSION['glpi_plugin_fusioninventory_graph_printerCompRemove'])) { unset($_SESSION['glpi_plugin_fusioninventory_graph_printersComp'][$_SESSION['glpi_plugin_fusioninventory_graph_printerCompRemove']]); } $oPrinter = new Printer(); $printers = $_SESSION['glpi_plugin_fusioninventory_graph_printersComp']; $printersView = $printers; // printers without the current printer if (isset($printersView[$id])) { unset($printersView[$id]); } else { if ($oPrinter->getFromDB($id)) { $printers[$id] = $oPrinter->getField('name'); } } $printersList = ''; foreach ($printers as $printers_id => $printername) { if ($printersList != '') { $printersList .= '<br/>'; } if ($printers_id == $id) { $printersList .= $printername; } else { $oPrinter->getFromDB($printers_id); $printersList .= $oPrinter->getLink(1); } } $printersIds = ""; foreach (array_keys($printers) as $printerId) { if ($printersIds != '') { $printersIds .= ', '; } $printersIds .= $printerId; } $where = " WHERE `printers_id` IN(" . $printersIds . ")"; if ($begin != '' || $end != '') { $where .= " AND " . getDateRequest("`date`", $begin, $end); } $group = ''; switch ($timeUnit) { case 'day': $group = "GROUP BY `printers_id`, `year`, `month`, `day`"; break; case 'week': $group = "GROUP BY `printers_id`, `year`, `month`, `week`"; break; case 'month': $group = "GROUP BY `printers_id`, `year`, `month`"; break; case 'year': $group = "GROUP BY `printers_id`, `year`"; break; } echo "<form method='post' name='snmp_form' id='snmp_form' action='" . $CFG_GLPI['root_doc'] . "/plugins/fusioninventory/front/printer_info.form.php'>"; echo "<table class='tab_cadre' cellpadding='5' width='950'>"; $mapping = new PluginFusioninventoryMapping(); $maps = $mapping->find("`itemtype`='Printer'"); foreach ($maps as $mapfields) { if (!isset($mapfields["shortlocale"])) { $mapfields["shortlocale"] = $mapfields["locale"]; } $pagecounters[$mapfields['name']] = $mapping->getTranslation($mapfields); } echo "<tr class='tab_bg_1'>"; echo "<th colspan='4'>"; echo __('Printed page counter', 'fusioninventory'); echo "</th>"; echo "</tr>"; echo "<tr class='tab_bg_1'>"; echo "<td class='left'>" . __('Start date') . " :</td>"; echo "<td class='left'>"; Html::showDateFormItem("graph_begin", $begin); echo "</td>"; echo "<td class='left'>" . __('Time unit', 'fusioninventory') . " :</td>"; echo "<td class='left'>"; $elementsTime = array('day' => _n('Day', 'Days', 1), 'week' => __('Week'), 'month' => _n('Month', 'Months', 1), 'year' => __('Year', 'fusioninventory')); Dropdown::showFromArray('graph_timeUnit', $elementsTime, array('value' => $timeUnit)); echo "</td>"; echo "</tr>"; echo "<tr class='tab_bg_1'>"; echo "<td class='left'>" . __('End date') . " :</td>"; echo "<td class='left'>"; Html::showDateFormItem("graph_end", $end); echo "</td>"; echo "<td class='left'>" . __('Display', 'fusioninventory') . " :</td>"; echo "<td class='left'>"; $elements = array('total' => __('Total counter', 'fusioninventory'), 'day' => __('pages per day', 'fusioninventory')); Dropdown::showFromArray('graph_type', $elements, array('value' => $graphType)); echo "</td>"; echo "</tr>"; echo "<tr class='tab_bg_2'>"; echo "<td class='center' colspan='4'>\n <input type='submit' class='submit' name='graph_plugin_fusioninventory_printer_period'\n value='" . __('Update') . "'/>"; echo "</td>"; echo "</tr>\n"; echo "<tr>"; echo "<th colspan='4'>" . __('Printers to compare', 'fusioninventory') . "</th>"; echo "</tr>"; echo "<tr class='tab_bg_1'>"; echo "<td class='left' rowspan='3'>" . __('Printer') . " :</td>"; echo "<td class='left' rowspan='3'>"; echo $printersList; echo "</td>"; echo "<td class='left'>" . __('Add a printer', 'fusioninventory') . " :</td>"; echo "<td class='left'>"; $printersused = array(); foreach ($printersView as $printer_id => $name) { $printersused[] = $printer_id; } $printer->getFromDB($id); Dropdown::show('Printer', array('name' => 'graph_printerCompAdd', 'entity' => $printer->fields['entities_id'], 'used' => $printersused)); echo " <input type='submit' value=\"" . __('Add') . "\" class='submit' " . "name='graph_plugin_fusioninventory_printer_add'>"; echo "</td>"; echo "</tr>\n"; echo "<tr class='tab_bg_1'>"; echo "<td class='left'>" . __('Remove a printer', 'fusioninventory') . " :</td>"; echo "<td class='left'>"; $printersTmp = $printersView; $printersTmp[0] = "-----"; asort($printersTmp); Dropdown::showFromArray('graph_printerCompRemove', $printersTmp); echo " <input type='submit' value=\"" . __('Delete', 'fusioninventory') . "\" " . "class='submit' name='graph_plugin_fusioninventory_printer_remove'>"; echo "</td>"; echo "</tr>\n"; echo "<tr class='tab_bg_1'>"; echo "<td colspan='2'></td>"; echo "</tr>"; echo "</table>"; Html::closeForm(); $elementsField = array('pages_total' => $pagecounters['pagecountertotalpages'], 'pages_n_b' => $pagecounters['pagecounterblackpages'], 'pages_color' => $pagecounters['pagecountercolorpages'], 'pages_recto_verso' => $pagecounters['pagecounterrectoversopages'], 'scanned' => $pagecounters['pagecounterscannedpages'], 'pages_total_print' => $pagecounters['pagecountertotalpages_print'], 'pages_n_b_print' => $pagecounters['pagecounterblackpages_print'], 'pages_color_print' => $pagecounters['pagecountercolorpages_print'], 'pages_total_copy' => $pagecounters['pagecountertotalpages_copy'], 'pages_n_b_copy' => $pagecounters['pagecounterblackpages_copy'], 'pages_color_copy' => $pagecounters['pagecountercolorpages_copy'], 'pages_total_fax' => $pagecounters['pagecountertotalpages_fax']); echo "<br/>"; $a_graph = array(); foreach ($elementsField as $graphField => $name) { $query = "SELECT `printers_id`, DAY(`date`)-1 AS `day`, WEEK(`date`) AS `week`,\n MONTH(`date`) AS `month`, YEAR(`date`) AS `year`, `date`,\n `{$graphField}`\n FROM `glpi_plugin_fusioninventory_printerlogs`" . $where . " AND `" . $graphField . "` > 0 " . $group; $result = $DB->query($query); if ($DB->numrows($result) == 0) { unset($elementsField[$graphField]); } } foreach ($elementsField as $graphField => $name) { $query = "SELECT `printers_id`, DAY(`date`)-1 AS `day`, WEEK(`date`) AS `week`,\n MONTH(`date`) AS `month`, YEAR(`date`) AS `year`, `date`,\n `{$graphField}`\n FROM `glpi_plugin_fusioninventory_printerlogs`" . $where . $group . "\n ORDER BY `year`, `month`, `day`, `printers_id`"; $input = array(); $result = $DB->query($query); if ($result) { if ($DB->numrows($result) != 0) { $pages = array(); $data = array(); $date = ''; while ($data = $DB->fetch_assoc($result)) { switch ($timeUnit) { case 'day': $split = explode(" ", $data['date']); $date = $split[0]; break; case 'week': $split = explode(" ", $data['date']); $date = $split[0]; break; case 'month': $split = explode(" ", $data['date']); $split2 = explode("-", $split[0]); $date = $split2[0] . "-" . $split2[1]; break; case 'year': $split = explode(" ", $data['date']); $split2 = explode("-", $split[0]); $date = $split2[0]; break; } if ($graphType == 'day') { if (!isset($pages[$data['printers_id']])) { $pages[$data['printers_id']] = $data[$graphField]; } else { $y = $data[$graphField] - $pages[$data['printers_id']]; if ($y < 0) { $y = 0; } $input[] = array('x' => $date, 'y' => $y); if ($data[$graphField] > 0) { $pages[$data['printers_id']] = $data[$graphField]; } } } else { $input[] = array('x' => $date, 'y' => $data[$graphField]); } } } } $continue = 1; if ($continue == '0' or $continue == '-1') { echo "<table class='tab_cadre' cellpadding='5' width='900'>"; echo "<tr class='tab_bg_1'>"; echo "<th>"; echo $name; echo "</th>"; echo "</tr>"; echo "<tr class='tab_bg_1'>"; echo "<td align='center'>"; if ($continue == '0') { echo __('Too datas to display', 'fusioninventory'); } echo "</td>"; echo "</tr>"; echo "</table><br/>"; } else { if (count($input > 0)) { $split = explode(' > ', $name); $a_graph[] = array('key' => $split[count($split) - 1], 'values' => $input); } } } // Display graph echo '<div id="chartPrinter">' . '<svg style="height: 400px; width: 950px;"></svg>' . '</div>'; echo "<script type='text/javascript'>\n function drawGraph() {\n var chart = nv.models.multiBarChart();\n\n chart.yAxis\n .tickFormat(d3.format(',0f'));\n\n d3.select('#chartPrinter svg')\n .datum(exampleData())\n .transition().duration(500).call(chart);\n\n nv.utils.windowResize(chart.update);\n }\n "; echo ' function exampleData() { return ' . json_encode($a_graph) . ' } drawGraph(); </script>'; }
/** * @test */ public function PrinterDicoGlobalManagement() { $this->mark_incomplete(); global $DB; $DB->connect(); $printer = new Printer(); $printer->getFromDB(1); $this->assertEquals('1', $printer->fields['is_global'], 'Printer p3 may be managed global'); }
/** * @test */ public function ComputerPrinter() { global $DB; $DB->connect(); $a_dataLink = getAllDatasFromTable("glpi_computers_items", "`itemtype`='Printer'\n AND `computers_id`='1'"); $this->assertEquals(1, count($a_dataLink), "Number of printers not right"); $a_dataLink = current($a_dataLink); $printer = new Printer(); $printer->getFromDB($a_dataLink['items_id']); unset($printer->fields['date_mod']); $a_reference = array('id' => '1', 'entities_id' => '0', 'is_recursive' => '0', 'name' => 'HP Deskjet 5700 Series', 'contact' => 'ddurieux', 'contact_num' => NULL, 'users_id_tech' => '0', 'groups_id_tech' => '0', 'serial' => 'MY47L1W1JHEB6', 'otherserial' => NULL, 'have_serial' => '0', 'have_parallel' => '0', 'have_usb' => '1', 'have_wifi' => '0', 'have_ethernet' => '0', 'comment' => NULL, 'memory_size' => NULL, 'locations_id' => '0', 'domains_id' => '0', 'networks_id' => '0', 'printertypes_id' => '0', 'printermodels_id' => '0', 'manufacturers_id' => '0', 'is_global' => '0', 'is_deleted' => '0', 'is_template' => '0', 'template_name' => NULL, 'init_pages_counter' => '0', 'last_pages_counter' => '0', 'users_id' => '0', 'groups_id' => '0', 'states_id' => '0', 'ticket_tco' => '0.0000', 'is_dynamic' => '0'); $this->assertEquals($a_reference, $printer->fields); }
/** * UnLink a cartridge linked to a printer * * UnLink the cartridge identified by $ID * * @param $ID : cartridge identifier * * @return boolean **/ function uninstall($ID) { global $DB; if ($this->getFromDB($ID)) { $printer = new Printer(); $toadd = ''; if ($printer->getFromDB($this->getField("printers_id"))) { $toadd .= ", `pages` = '" . $printer->fields['last_pages_counter'] . "' "; } $query = "UPDATE`" . $this->getTable() . "`\n SET `date_out` = '" . date("Y-m-d") . "'\n {$toadd}\n WHERE `id`='{$ID}'"; if ($result = $DB->query($query) && $DB->affected_rows() > 0) { $changes[0] = '0'; $changes[1] = ''; $changes[2] = __('Uninstalling a cartridge'); Log::history($this->getField("printers_id"), 'Printer', $changes, 0, Log::HISTORY_LOG_SIMPLE_MESSAGE); return true; } return false; } }
/** * @test */ public function updatePrinterFromNetdiscovery() { global $DB; $DB->connect(); $pfCNetworkDiscovery = new PluginFusioninventoryCommunicationNetworkDiscovery(); $GLPIlog = new GLPIlogs(); $networkName = new NetworkName(); $iPAddress = new IPAddress(); $_SESSION['SOURCE_XMLDEVICE'] = array('AUTHSNMP' => '1', 'DESCRIPTION' => 'Photosmart D7200 series', 'ENTITY' => '0', 'FIRMWARE' => '', 'IP' => '192.168.20.102', 'MAC' => '00:21:5a:0b:bb:c4', 'MANUFACTURER' => 'Hewlett-Packard', 'MODEL' => '', 'MODELSNMP' => 'Printer0093', 'NETBIOSNAME' => 'HP00215A0BBBC4', 'SERIAL' => 'MY89AQG0V9050N', 'SNMPHOSTNAME' => 'HP0BBBC4new', 'TYPE' => 'PRINTER'); $printer = new Printer(); $a_printers = $printer->find("`serial`='MY89AQG0V9050N'"); $a_printer = current($a_printers); $printers_id = $a_printer['id']; $printer->getFromDB($printers_id); $pfCNetworkDiscovery->importDevice($printer); $GLPIlog->testSQLlogs(); $GLPIlog->testPHPlogs(); $printer->getFromDB($printers_id); $this->assertEquals('HP0BBBC4new', $printer->fields['name'], 'Name must be updated'); $a_printerextends = getAllDatasFromTable('glpi_plugin_fusioninventory_printers', "`printers_id`='" . $printers_id . "'"); $this->assertEquals('1', count($a_printerextends), 'May have one printer extend line for this printer'); $a_printerextend = current($a_printerextends); $this->assertEquals('1', $a_printerextend['plugin_fusioninventory_configsecurities_id'], 'SNMPauth may be with id 1'); $this->assertEquals('Photosmart D7200 series', $a_printerextend['sysdescr'], 'Sysdescr not updated correctly'); // Check mac $networkPort = new NetworkPort(); $a_ports = $networkPort->find("`itemtype`='Printer' AND `items_id`='" . $printers_id . "'"); $this->assertEquals('1', count($a_ports), 'May have one network port'); $a_port = current($a_ports); $this->assertEquals('00:21:5a:0b:bb:c4', $a_port['mac'], 'Mac address'); // check ip $a_networknames = $networkName->find("`itemtype`='NetworkPort'\n AND `items_id`='" . $a_port['id'] . "'"); $this->assertEquals('1', count($a_networknames), 'May have one networkname'); $a_networkname = current($a_networknames); $a_ipaddresses = $iPAddress->find("`itemtype`='NetworkName'\n AND `items_id`='" . $a_networkname['id'] . "'"); $this->assertEquals('1', count($a_ipaddresses), 'May have one IP address'); $a_ipaddress = current($a_ipaddresses); $this->assertEquals('192.168.20.102', $a_ipaddress['name'], 'IP address'); }
echo "<th>" . __('Inventory number') . "</th>"; echo "<th>" . __('Model') . "</th>"; echo "<th>" . __('SNMP authentication') . "</th>"; echo "<th>" . __('Status') . "</th>"; echo "</tr>"; if ($result = $DB->query($query)) { while ($data = $DB->fetch_array($result)) { echo "<tr class='tab_bg_1'>"; echo "<td>"; if ($data['network_id'] > 0) { $class = new NetworkEquipment(); $class->getFromDB($data['network_id']); } else { if ($data['printer_id'] > 0) { $class = new Printer(); $class->getFromDB($data['printer_id']); } } echo $class->getLink(1); echo "</td>"; echo "<td>" . Html::convDateTime($data['last_fusioninventory_update']) . "</td>"; echo "<td>"; if ($data['network_id'] > 0) { echo __('Networks'); } else { if ($data['printer_id'] > 0) { echo __('Printers'); } } echo "</td>"; echo "<td>" . $data['ip'] . "</td>";
/** * @covers Printer::deleteByCriteria * @covers Printer::restore */ public function testDeleteByCriteria() { $obj = new Printer(); $this->assertTrue($obj->maybeDeleted()); // Add $id = $obj->add(['name' => __METHOD__, 'entities_id' => getItemByTypeName('Entity', '_test_root_entity', true)]); $this->assertGreaterThan(0, $id); $this->assertTrue($obj->getFromDB($id)); $this->assertEquals(0, $obj->getField('is_deleted')); $this->assertEquals(0, $obj->isDeleted()); $nb_before = countElementsInTable('glpi_logs', "itemtype = 'Printer'\n AND items_id = '{$id}'"); // DeleteByCriteria without history $this->assertTrue($obj->deleteByCriteria(['name' => __METHOD__], 0, 0)); $this->assertTrue($obj->getFromDB($id)); $this->assertEquals(1, $obj->getField('is_deleted')); $this->assertEquals(1, $obj->isDeleted()); $nb_after = countElementsInTable('glpi_logs', "itemtype = 'Printer'\n AND items_id = '{$id}'"); $this->assertEquals($nb_before, $nb_after); // Restore $this->assertTrue($obj->restore(['id' => $id], 0)); $this->assertTrue($obj->getFromDB($id)); $this->assertEquals(0, $obj->getField('is_deleted')); $this->assertEquals(0, $obj->isDeleted()); $nb_before = countElementsInTable('glpi_logs', "itemtype = 'Printer'\n AND items_id = '{$id}'"); // DeleteByCriteria with history $this->assertTrue($obj->deleteByCriteria(['name' => __METHOD__], 0, 1)); $this->assertTrue($obj->getFromDB($id)); $this->assertEquals(1, $obj->getField('is_deleted')); $this->assertEquals(1, $obj->isDeleted()); $nb_after = countElementsInTable('glpi_logs', "itemtype = 'Printer'\n AND items_id = '{$id}'"); $this->assertEquals($nb_before + 1, $nb_after); }