. * * ------------------------------------------------------------------------ * * This file is used to manage the printer changes (history). * * ------------------------------------------------------------------------ * * @package FusionInventory * @author David Durieux * @copyright Copyright (c) 2010-2016 FusionInventory team * @license AGPL License 3.0 or (at your option) any later version * http://www.gnu.org/licenses/agpl-3.0-standalone.html * @link http://www.fusioninventory.org/ * @link https://github.com/fusioninventory/fusioninventory-for-glpi * */ if (!defined('GLPI_ROOT')) { die("Sorry. You can't access this file directly"); } /** * Manage the printer changes (history). */ class PluginFusioninventoryPrinterLog extends CommonDBTM { /** * Get search function for the class * * @return array */ function rawSearchOptions() { $tab = []; $tab[] = [ 'id' => 'common', 'name' => __('History meter printer', 'fusioninventory') ]; $tab[] = [ 'id' => '1', 'table' => "glpi_printers", 'field' => 'name', 'linkfield' => 'printers_id', 'name' => __('Name'), 'datatype' => 'itemlink', 'itemlink_type' => 'Printer', ]; $tab[] = [ 'id' => '2', 'table' => $this->getTable(), 'field' => 'id', 'name' => 'id', ]; $tab[] = [ 'id' => '24', 'table' => 'glpi_locations', 'field' => 'name', 'linkfield' => 'locations_id', 'name' => __('Location'), 'datatype' => 'itemlink', 'itemlink_type' => 'Location', ]; $tab[] = [ 'id' => '19', 'table' => 'glpi_printertypes', 'field' => 'name', 'linkfield' => 'printertypes_id', 'name' => __('Type'), 'datatype' => 'itemlink', 'itemlink_type' => 'PrinterType', ]; // $tab[] = [ // 'id' => 2, // 'table' => 'glpi_printermodels', // 'field' => 'name', // 'linkfield' => 'printermodels_id', // 'name' => __('Model'), // 'datatype' => 'itemptype', // ]; $tab[] = [ 'id' => '18', 'table' => 'glpi_states', 'field' => 'name', 'linkfield' => 'states_id', 'name' => __('Status'), 'datatype' => '>dropdown', ]; $tab[] = [ 'id' => '20', 'table' => 'glpi_printers', 'field' => 'serial', 'linkfield' => 'printers_id', 'name' => __('Serial Number'), ]; $tab[] = [ 'id' => '23', 'table' => 'glpi_printers', 'field' => 'otherserial', 'linkfield' => 'printers_id', 'name' => __('Inventory number'), ]; $tab[] = [ 'id' => '21', 'table' => 'glpi_users', 'field' => 'name', 'linkfield' => 'users_id', 'name' => __('User'), ]; $tab[] = [ 'id' => '3', 'table' => 'glpi_manufacturers', 'field' => 'name', 'linkfield' => 'manufacturers_id', 'name' => Manufacturer::getTypeName(1), ]; $joinparams = [ 'jointype' => 'itemtype_item', 'specific_itemtype' => 'Printer', ]; $networkNameJoin = [ 'jointype' => 'itemtype_item', 'specific_itemtype' => 'NetworkPort', 'beforejoin' => [ 'table' => 'glpi_networkports', 'joinparams' => $joinparams, ], ]; $tab[] = [ 'id' => '5', 'table' => 'glpi_ipaddresses', 'field' => 'name', 'name' => __('IP'), 'forcegroupby' => true, 'massiveaction' => false, 'joinparams' => [ 'jointype' => 'itemtype_item', 'specific_itemtype' => 'NetworkName', 'beforejoin' => [ 'table' => 'glpi_networknames', 'joinparams' => $networkNameJoin, ], ], ]; $tab[] = [ 'id' => '6', 'table' => 'glpi_plugin_fusioninventory_printerlogs', 'field' => 'pages_total', 'linkfield' => 'id', 'name' => __('Total number of printed pages', 'fusioninventory'), ]; $tab[] = [ 'id' => '7', 'table' => 'glpi_plugin_fusioninventory_printerlogs', 'field' => 'pages_n_b', 'linkfield' => 'id', 'name' => __('Number of printed black and white pages', 'fusioninventory'), ]; $tab[] = [ 'id' => '8', 'table' => 'glpi_plugin_fusioninventory_printerlogs', 'field' => 'pages_color', 'linkfield' => 'id', 'name' => __('Number of printed color pages', 'fusioninventory'), ]; $tab[] = [ 'id' => '9', 'table' => $this->getTable(), 'field' => 'pages_recto_verso', 'linkfield' => 'id', 'name' => __('Number of pages printed duplex', 'fusioninventory'), ]; $tab[] = [ 'id' => '10', 'table' => $this->getTable(), 'field' => 'scanned', 'linkfield' => 'id', 'name' => __('Number of scanned pages', 'fusioninventory'), ]; $tab[] = [ 'id' => '11', 'table' => $this->getTable(), 'field' => 'pages_total_print', 'linkfield' => 'id', 'name' => __('Total number of printed pages (print)', 'fusioninventory'), ]; $tab[] = [ 'id' => '12', 'table' => $this->getTable(), 'field' => 'pages_n_b_print', 'linkfield' => 'id', 'name' => __('Number of printed black and white pages (print)', 'fusioninventory'), ]; $tab[] = [ 'id' => '13', 'table' => $this->getTable(), 'field' => 'pages_color_print', 'linkfield' => 'id', 'name' => __('Number of printed color pages (print)', 'fusioninventory'), ]; $tab[] = [ 'id' => '14', 'table' => $this->getTable(), 'field' => 'pages_total_copy', 'linkfield' => 'id', 'name' => __('Total number of printed pages (copy)', 'fusioninventory'), ]; $tab[] = [ 'id' => '15', 'table' => $this->getTable(), 'field' => 'pages_n_b_copy', 'linkfield' => 'id', 'name' => __('Number of printed black and white pages (copy)', 'fusioninventory'), ]; $tab[] = [ 'id' => '16', 'table' => $this->getTable(), 'field' => 'pages_color_copy', 'linkfield' => 'id', 'name' => __('Number of printed color pages (copy)', 'fusioninventory'), ]; $tab[] = [ 'id' => '17', 'table' => $this->getTable(), 'field' => 'pages_total_fax', 'linkfield' => 'id', 'name' => __('Total number of printed pages (fax)', 'fusioninventory'), ]; return $tab; } /** * Count number entries for the printer * * @global object $DB * @param integer $printers_id * @return integer */ function countAllEntries($printers_id) { global $DB; $num = 0; $query = "SELECT count(DISTINCT `id`) FROM ".$this->getTable()." WHERE `printers_id` = '".$printers_id."';"; $result_num=$DB->query($query); if ($result_num) { $field = $DB->result($result_num, 0, 0); if ($field) { $num += $field; } } return $num; } /** * Get logs of printer * * @global object $DB * @param integer $printers_id * @param integer $begin * @param integer $limit * @return array|false */ function getEntries($printers_id, $begin, $limit) { global $DB; $datas=[]; $query = "SELECT * FROM ".$this->getTable()." WHERE `printers_id` = '".$printers_id."' LIMIT ".$begin.", ".$limit.";"; $result=$DB->query($query); if ($result) { $i = 0; while ($data=$DB->fetchAssoc($result)) { $data['date'] = Html::convDateTime($data['date']); $datas["$i"] = $data; $i++; } return $datas; } return false; } /** * Get printed pages statistics * * @global object $DB * @param integer $printers_id * @return array|false */ function stats($printers_id) { global $DB; $query = "SELECT MIN(`date`) AS `min_date`, MIN(`pages`) AS `min_pages`, ". "MAX(`date`) AS `max_date`, MAX(`pages`) AS `max_pages` FROM ".$this->getTable()." WHERE `printers_id` = '".$printers_id."';"; $result = $DB->query($query); if ($result) { $fields = $DB->fetchAssoc($result); if ($fields) { $output = []; $output['num_days'] = ceil((strtotime($fields['max_date']) - strtotime($fields['min_date']))/(60*60*24)); $output['num_pages'] = $fields['max_pages'] - $fields['min_pages']; $output['pages_per_day'] = round($output['num_pages'] / $output['num_days']); return $output; } } return false; } /** * Display form * * @param integer $printers_id * @param array $options * @return boolean */ function showForm($printers_id, $options = []) { if (!Session::haveRight('plugin_fusioninventory_printer', READ)) { return false; } // display stats $stats = $this->stats($printers_id); if ($stats) { $this->initForm($printers_id, $options); $this->showFormHeader($options); echo "