/** form for Cartridge
  *
  * @since version 0.84
  *
  * @param $ID      integer  Id of the cartridge
  * @param $options array    of possible options:
  *     - parent Object : the printers where the cartridge is used
  **/
 function showForm($ID, $options = array())
 {
     global $DB, $CFG_GLPI;
     if (isset($options['parent']) && !empty($options['parent'])) {
         $printer = $options['parent'];
     }
     if (!$this->getFromDB($ID)) {
         return false;
     }
     $printer = new Printer();
     $printer->check($this->getField('printers_id'), UPDATE);
     $cartitem = new CartridgeItem();
     $cartitem->getFromDB($this->getField('cartridgeitems_id'));
     $is_old = !empty($this->fields['date_out']);
     $is_used = !empty($this->fields['date_use']);
     $options['colspan'] = 2;
     $options['candel'] = false;
     // Do not permit delete here
     $options['canedit'] = $is_used;
     // Do not permit edit if cart is not used
     $this->showFormHeader($options);
     echo "<tr class='tab_bg_1'>";
     echo "<td>" . _n('Printer', 'Printers', 1) . "</td><td>";
     echo $printer->getLink();
     echo "<input type='hidden' name='printers_id' value='" . $this->getField('printers_id') . "'>\n";
     echo "<input type='hidden' name='cartridgeitems_id' value='" . $this->getField('cartridgeitems_id') . "'>\n";
     echo "</td>\n";
     echo "<td>" . _n('Cartridge model', 'Cartridge models', 1) . "</td>";
     echo "<td>" . $cartitem->getLink() . "</td></tr>\n";
     echo "<tr class='tab_bg_1'>";
     echo "<td>" . __('Add date') . "</td>";
     echo "<td>" . Html::convDate($this->fields["date_in"]) . "</td>";
     echo "<td>" . __('Use date') . "</td><td>";
     if ($is_used && !$is_old) {
         Html::showDateField("date_use", array('value' => $this->fields["date_use"], 'maybeempty' => false, 'canedit' => true, 'min' => $this->fields["date_in"]));
     } else {
         echo Html::convDate($this->fields["date_use"]);
     }
     echo "</td></tr>\n";
     if ($is_old) {
         echo "<tr class='tab_bg_1'>";
         echo "<td>" . __('End date') . "</td><td>";
         Html::showDateField("date_out", array('value' => $this->fields["date_out"], 'maybeempty' => false, 'canedit' => true, 'min' => $this->fields["date_use"]));
         echo "</td>";
         echo "<td>" . __('Printer counter') . "</td><td>";
         echo "<input type='text' name='pages' value=\"" . $this->fields['pages'] . "\">";
         echo "</td></tr>\n";
     }
     $this->showFormButtons($options);
     return true;
 }
    /**
     * 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') . "&nbsp;:</td>";
        echo "<td class='left'>";
        Html::showDateFormItem("graph_begin", $begin);
        echo "</td>";
        echo "<td class='left'>" . __('Time unit', 'fusioninventory') . "&nbsp;:</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') . "&nbsp;:</td>";
        echo "<td class='left'>";
        Html::showDateFormItem("graph_end", $end);
        echo "</td>";
        echo "<td class='left'>" . __('Display', 'fusioninventory') . "&nbsp;:</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') . "&nbsp;:</td>";
        echo "<td class='left' rowspan='3'>";
        echo $printersList;
        echo "</td>";
        echo "<td class='left'>" . __('Add a printer', 'fusioninventory') . "&nbsp;:</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 "&nbsp;<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') . "&nbsp;:</td>";
        echo "<td class='left'>";
        $printersTmp = $printersView;
        $printersTmp[0] = "-----";
        asort($printersTmp);
        Dropdown::showFromArray('graph_printerCompRemove', $printersTmp);
        echo "&nbsp;<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>';
    }
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>";
        echo "<td>" . $data['serial'] . "</td>";
        echo "<td>" . $data['otherserial'] . "</td>";
        if ($data['network_id'] > 0) {