function displayGraphs($itemtype, $items_id)
 {
     global $CFG_GLPI;
     $pmComponent = new PluginMonitoringComponent();
     $pmConfig = new PluginMonitoringConfig();
     $pmComponentscatalog_Host = new PluginMonitoringComponentscatalog_Host();
     $networkPort = new NetworkPort();
     $item = new $itemtype();
     $item->getFromDB($items_id);
     $pmComponent->getFromDB($item->fields['plugin_monitoring_components_id']);
     if (!isset($_SESSION['glpi_plugin_monitoring']['perfname'][$pmComponent->fields['id']])) {
         PluginMonitoringToolbox::loadPreferences($pmComponent->fields['id']);
     }
     $css_width = '950';
     if (isset($_GET['mobile'])) {
         $css_width = '300';
     }
     echo "<table class='tab_cadre' width='" . $css_width . "'>";
     echo "<tr class='tab_bg_1'>";
     echo "<th>";
     $title = Dropdown::getDropdownName(getTableForItemType('PluginMonitoringComponent'), $item->fields['plugin_monitoring_components_id']);
     if (!is_null($item->fields['networkports_id']) && $item->fields['networkports_id'] > 0) {
         $networkPort->getFromDB($item->fields['networkports_id']);
         $title .= " [" . $networkPort->getLink() . "]";
     }
     $title .= ' ' . __('on', 'monitoring') . ' ';
     $pmComponentscatalog_Host->getFromDB($item->fields["plugin_monitoring_componentscatalogs_hosts_id"]);
     if (isset($pmComponentscatalog_Host->fields['itemtype']) and $pmComponentscatalog_Host->fields['itemtype'] != '') {
         $itemtype2 = $pmComponentscatalog_Host->fields['itemtype'];
         $item2 = new $itemtype2();
         $item2->getFromDB($pmComponentscatalog_Host->fields['items_id']);
         $title .= str_replace("'", "\"", $item2->getLink() . " (" . $item2->getTypeName() . ")");
     }
     echo $title;
     echo "</th>";
     echo "<th width='200'>";
     if (!isset($_GET['mobile'])) {
         echo "<form method='post'>";
         $a_timezones = PluginMonitoringConfig::getTimezones();
         if (!isset($_SESSION['plugin_monitoring_timezone'])) {
             $_SESSION['plugin_monitoring_timezone'] = '0';
         }
         $a_timezones_allowed = array();
         $pmConfig->getFromDB(1);
         $a_temp = importArrayFromDB($pmConfig->fields['timezones']);
         foreach ($a_temp as $key) {
             $a_timezones_allowed[$key] = $a_timezones[$key];
         }
         if (count($a_timezones_allowed) == '0') {
             $a_timezones_allowed['0'] = $a_timezones['0'];
         }
         Dropdown::showFromArray('plugin_monitoring_timezone', $a_timezones_allowed, array('value' => $_SESSION['plugin_monitoring_timezone']));
         echo "&nbsp;<input type='submit' name='update' value=\"" . __('Save') . "\" class='submit'>";
         Html::closeForm();
     }
     echo "</th>";
     echo "</tr>";
     $timezone = '0';
     if (isset($_SESSION['plugin_monitoring_timezone'])) {
         $timezone = $_SESSION['plugin_monitoring_timezone'];
     }
     if (!isset($_GET['mobile'])) {
         echo "<tr class='tab_bg_1'>";
         echo "<th colspan='2'>";
         echo "<div id='legendlink'><a onClick='\$(\"#options\").toggle();'>[ Options ]</a></div>";
         echo "</th>";
         echo "</tr>";
         // * Display perfname
         echo "<tr class='tab_bg_1'>";
         echo "<td colspan='2'>";
         echo "<div id='options' style='display:none'>";
         PluginMonitoringToolbox::preferences($pmComponent->fields['id'], 0);
         echo "</div>";
         echo "</td>";
         echo "</tr>";
         // * Display date slider
         echo "<tr class='tab_bg_1'>";
         echo "<th colspan='2'>";
         echo __('Select date', 'monitoring') . " - " . __('Select time', 'monitoring');
         echo "</th>";
         echo "</tr>";
         echo "<tr class='tab_bg_1'>";
         echo "<th colspan='2'>";
         $start = time();
         $oldvalue = current(getAllDatasFromTable('glpi_plugin_monitoring_serviceevents', "`plugin_monitoring_services_id`='" . $items_id . "'", false, 'date ASC LIMIT 1'));
         $date = new DateTime($oldvalue['date']);
         if ($date->getTimestamp() < $start) {
             $start = $date->getTimestamp();
         }
         $nbdays = round((date('U') - $start) / 86400);
         echo "<script type=\"text/javascript\">\n            \$(function() {\n                \$( \"#custom_date\" ).datepicker({ minDate: -" . $nbdays . ", maxDate: \"+0D\", dateFormat:'mm/dd/yy' });\n                \$( \"#custom_time\" ).timepicker();\n\n            });\n         </script>";
         echo '<center><input type="text" id="custom_date" value="' . date('m/d/Y') . '"> ' . ' <input type="text" id="custom_time" value="' . date('H:i') . '"></center>';
         echo "</th>";
         echo "</tr>";
     }
     $a_list = array();
     $a_list["2h"] = __("Last 2 hours", "monitoring");
     $a_list["12h"] = __("Last 12 hours", "monitoring");
     $a_list["1d"] = __("Last 24 hours", "monitoring");
     $a_list["1w"] = __("Last 7 days (average)", "monitoring");
     //      $a_list["1m"]     = __("Last month (average)", "monitoring");
     //      $a_list["0y6m"]   = __("Last 6 months (average)", "monitoring");
     //      $a_list["1y"]     = __("Last year (average)", "monitoring");
     foreach ($a_list as $time => $name) {
         echo "<tr class='tab_bg_1'>";
         echo "<th colspan='2'>";
         echo $name;
         echo "</th>";
         echo "</tr>";
         echo "<tr class='tab_bg_1'>";
         echo "<td align='center' colspan='2' style='position: relative'>";
         $pmServicegraph = new PluginMonitoringServicegraph();
         $part = '';
         $width = '950';
         if (isset($_GET['mobile'])) {
             $width = '294';
         }
         $pmServicegraph->displayGraph($pmComponent->fields['graph_template'], $itemtype, $items_id, $timezone, $time, $part, $width);
         echo "</td>";
         echo "</tr>";
     }
     echo "</table>";
 }
   function displayGraph($rrdtool_template, $itemtype, $items_id, $timezone, $time='1d', $part='', $width='900') {
      global $CFG_GLPI;

      $pmComponent = new PluginMonitoringComponent();
//      if (isset($_GET['itemtype'])) {
//         $itemtype = $_GET['itemtype'];
//         $items_id = $_GET['items_id'];
//      }
      $item = new $itemtype();
      if ($item->getFromDB($items_id)) {
         $pmComponent->getFromDB($item->fields['plugin_monitoring_components_id']);
         $ident = $items_id.$time;

         if ($part == ''
                 OR $part == 'div') {
            echo '<div id="chart'.$ident.'">'.
                '<svg style="width: '.$width.'px;display: block;"></svg>'.
              '</div>';

            echo "<div id=\"updategraph".$items_id.$time."\"></div>";
         }
         if ($part == ''
                 OR $part == 'js') {
            if (!isset($_SESSION['glpi_plugin_monitoring']['perfname'][$pmComponent->fields['id']])) {
               PluginMonitoringToolbox::loadPreferences($pmComponent->fields['id']);
            }
            if (! isset($_SESSION['glpi_plugin_monitoring']['perfname'][$pmComponent->fields['id']])) {
               $format = '%H:%M';
            } else {
               if (isset($_SESSION['glpi_plugin_monitoring']['perfname'][$pmComponent->fields['id']][''])) {
                  unset($_SESSION['glpi_plugin_monitoring']['perfname'][$pmComponent->fields['id']]['']);
               }
               $pmServicegraph = new PluginMonitoringServicegraph();
               $a_ret = $pmServicegraph->generateData($rrdtool_template,
                                            $itemtype,
                                            $items_id,
                                            $timezone,
                                            $time,
                                            '',
                                            $_SESSION['glpi_plugin_monitoring']['perfname'][$pmComponent->fields['id']]);
               $format = $a_ret[2];
            }
            echo "<script>";
            $formaty = ".0f";
            echo '

            var data'.$ident.' = [];
            var chart'.$ident.';
            redraw'.$ident.' = function () {
               nv.addGraph(function() {
                  chart'.$ident.' = nv.models.lineChart();

                  chart'.$ident.'.useInteractiveGuideline(true);

                  chart'.$ident.'.xAxis
                     .tickFormat(function(d) { return d3.time.format("'.$format.'")(new Date(d)); });

                  chart'.$ident.'.yAxis
                     .axisLabel("test")
                     .tickFormat(d3.format(\''.$formaty.'\'));

                  chart'.$ident.'.forceY([0]);

                  d3.select("#chart'.$ident.' svg")
                    .attr("height", 300)
                    .datum(data'.$ident.')
                    .transition().duration(50)
                    .call(chart'.$ident.');



                 return chart'.$ident.';
               });
            };';

            echo "
            (function worker".$items_id.$time."() {
              startDate = new Date($('#custom_date').val());
              startTime = Date.parse('04/03/1980 ' + ($('#custom_time').val()) + ':00');
              $.getJSON('".$CFG_GLPI["root_doc"]."/plugins/monitoring/ajax/updateChart.php"
                    ."?rrdtool_template=".$rrdtool_template."&itemtype=".$itemtype.
                          "&items_id=".$items_id.
                          "&timezone=".$timezone.
                          "&time=".$time."&customdate=' + (startDate.getTime()/1000.0) + '".
                          "&customtime=' + (startTime/1000.0) + '".
                          "&components_id=".$pmComponent->fields['id']."', function(data) {
                data".$ident." = data;

                redraw".$ident."();
                setTimeout(worker".$items_id.$time.", 30000);
              });
            })();";
            echo "
            </script>";
         }

                 }
      return;
   }
   function showSyntheseReport($componentscatalogs_id) {
      global $CFG_GLPI;

      if (!isset($_SESSION['glpi_plugin_monitoring']['synthese'])) {
         $_SESSION['glpi_plugin_monitoring']['synthese'] = array();
      }
      if (!isset($_SESSION['glpi_plugin_monitoring']['synthese'][$componentscatalogs_id])) {
         $_SESSION['glpi_plugin_monitoring']['synthese'][$componentscatalogs_id] = array();
      }
      $sess = $_SESSION['glpi_plugin_monitoring']['synthese'][$componentscatalogs_id];
      $pmComponentscatalog_Component = new PluginMonitoringComponentscatalog_Component();
      $pmComponent = new PluginMonitoringComponent();
      $a_options = array();

      $this->getFromDB($componentscatalogs_id);

      echo "<form name='form' method='post'
         action='".$CFG_GLPI['root_doc']."/plugins/monitoring/front/report_componentscatalog.form.php'>";

      echo "<table class='tab_cadre_fixe'>";
      echo '<tr class="tab_bg_1">';
      echo '<th colspan="5">';
      echo __('Report');
      echo "<input type='hidden' name='componentscatalogs_id' value='".$componentscatalogs_id."' />";
      echo "<input type='hidden' name='reporttype' value='synthese' />";
      $a_options['componentscatalogs_id'] = $componentscatalogs_id;
      echo '</th>';
      echo '</tr>';

      echo '<tr class="tab_bg_1">';
      echo '<td>';
      echo '</td>';
      echo '<td colspan="2">';
      $default_value = 12;
      if (isset($sess['synthesenumber'])) {
         $default_value = $sess['synthesenumber'];
      }
      Dropdown::showNumber("synthesenumber", array(
                'value' => $default_value,
                'min'   => 2,
                'max'   => 30)
      );
      $a_options['synthesenumber'] = $default_value;
      echo "&nbsp;";
      $a_time = array('week' => __('Week'),
                      'month' => __('Month'),
                      'year' => __('Year'));
      $default_value = 'week';
      if (isset($sess['synthesenumber'])) {
         $default_value = $sess['synthesenumber'];
      }
      Dropdown::showFromArray("syntheseperiod", $a_time, array('value' => $default_value));
      $a_options['syntheseperiod'] = $default_value;
      echo '</td>';
      echo "<td>".__('End date')." :</td>";
      echo "<td>";
      $default_value = date('Y-m-d');
      if (isset($sess['synthesedate_end'])) {
         $default_value = $sess['synthesedate_end'];
      }
      Html::showDateFormItem("synthesedate_end", $default_value);
      $a_options['synthesedate_end'] = $default_value;
      echo "</td>";
      echo '</tr>';

      echo "</table>";

      echo "<table class='tab_cadre_fixe'>";
      $a_composants = $pmComponentscatalog_Component->find("`plugin_monitoring_componentscalalog_id`='".$componentscatalogs_id."'");
      foreach ($a_composants as $comp_data) {
         $pmComponent->getFromDB($comp_data['plugin_monitoring_components_id']);

         echo "<tr class='tab_bg_1'>";
         echo "<td width='10'>";
         //echo "<input type='checkbox' name='components_id[]' value='".$pmComponent->getID()."' checked />";
         echo "<input type='hidden' name='components_id[]' value='".$pmComponent->getID()."' />";
         $a_options['components_id'][] = $pmComponent->getID();
         echo "</td>";
         echo "<td>";
         echo $pmComponent->getLink();
         echo "</td>";
         echo "</tr>";

         echo "<tr class='tab_bg_1'>";
         echo "<td width='10'>";
         echo "</td>";
         echo "<td>";

         PluginMonitoringToolbox::loadPreferences($pmComponent->getID());

         $a_perfnames = PluginMonitoringServicegraph::getperfdataNames($pmComponent->fields['graph_template']);
         echo "<table class='tab_cadre_fixe'>";
         echo "<tr class='tab_bg_3'>";
         echo "<td rowspan='".count($a_perfnames)."' width='90'>";
         echo __('Use for report', 'monitoring')."&nbsp;:";

         echo "</td>";
         $i = 0;
         $j = 0;
         if (!isset($_SESSION['glpi_plugin_monitoring']['perfname'][$pmComponent->getID()])) {
            foreach ($a_perfnames as $name) {
               $_SESSION['glpi_plugin_monitoring']['perfname'][$pmComponent->getID()][$name] = 'checked';
            }
         }

         foreach ($a_perfnames as $name) {
            if ($i > 0) {
               echo "<tr class='tab_bg_3'>";
            }
            echo "<td>";
            $checked = "checked";
            if (isset($sess['perfname'])
                 && isset($sess['perfname'][$pmComponent->getID()])) {

               if (isset($sess['perfname'][$pmComponent->getID()])) {
                  $checked = "";
               }
               if (isset($sess['perfname'][$pmComponent->getID()][$name])) {
                  $checked = "checked";
               }
            } else {
               if (isset($_SESSION['glpi_plugin_monitoring']['perfname'][$pmComponent->getID()])) {
                  $checked = "";
               }
               if (isset($_SESSION['glpi_plugin_monitoring']['perfname'][$pmComponent->getID()][$name])) {
                  $checked = $_SESSION['glpi_plugin_monitoring']['perfname'][$pmComponent->getID()][$name];
               }
            }
            echo "<input type='checkbox' name='perfname[".$pmComponent->getID()."][".$name."]' value='".$name."' ".$checked."/> ".$name;
            if ($checked == 'checked') {
               $a_options['perfname'][$pmComponent->getID()][] = $name;
            }
            echo "</td>";
            echo "<td>";
            echo __('Best is high value', 'monitoring').' :';
            echo "</td>";
            echo "<td>";
            $default_value = 1;
            if (isset($sess['perfname_val'])
                 && isset($sess['perfname_val'][$pmComponent->getID()])) {

               if (isset($sess['perfname_val'][$pmComponent->getID()][$name])) {
                  $default_value = $sess['perfname_val'][$pmComponent->getID()][$name];
               }
            }
            Dropdown::showYesNo('perfname_val['.$pmComponent->getID().']['.$name.']', $default_value);
            if ($checked == 'checked') {
               $a_options['perfname_val'][$pmComponent->getID()][$name] = $default_value;
            }
            echo "</td>";
            echo "</tr>";
            $i++;
         }

         echo "</table>";

         echo "</td>";

         echo "</tr>";
      }
      echo "<tr class='tab_bg_1'>";
      echo "<td colspan='2' align='center'>";
      echo "<input type='submit' class='submit' name='update' value='".__('Save')."'/>";
      echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
         <input type='submit' class='submit' name='generatepdf' value='".__('Generate PDF', 'monitoring')."'/>";
      echo "</td>";
      echo "</tr>";
      echo "</table>";

      Html::closeForm();

      if (isset($_SESSION['plugin_monitoring_report'])) {
//         $a_options = $_SESSION['plugin_monitoring_report'];
      }
      $this->generateSyntheseReport(
              $_SESSION['glpi_plugin_monitoring']['synthese'][$componentscatalogs_id],
              FALSE);
   }
    function displayGraph($rrdtool_template, $itemtype, $items_id, $timezone, $time = '1d', $part = '', $width = '900')
    {
        global $CFG_GLPI;
        $pmComponent = new PluginMonitoringComponent();
        //      if (isset($_GET['itemtype'])) {
        //         $itemtype = $_GET['itemtype'];
        //         $items_id = $_GET['items_id'];
        //      }
        $item = new $itemtype();
        if ($item->getFromDB($items_id)) {
            $pmComponent->getFromDB($item->fields['plugin_monitoring_components_id']);
            $ident = $items_id . $time;
            if ($part == '' or $part == 'div') {
                echo '<div id="chart' . $ident . '">' . '<svg style="width: ' . $width . 'px;display: block;height: 300px;"></svg>' . '</div>';
                echo "<div id=\"updategraph" . $items_id . $time . "\"></div>";
            }
            if ($part == '' or $part == 'js') {
                if (!isset($_SESSION['glpi_plugin_monitoring']['perfname'][$pmComponent->fields['id']])) {
                    PluginMonitoringToolbox::loadPreferences($pmComponent->fields['id']);
                }
                if (!isset($_SESSION['glpi_plugin_monitoring']['perfname'][$pmComponent->fields['id']])) {
                    $format = '%H:%M';
                } else {
                    if (isset($_SESSION['glpi_plugin_monitoring']['perfname'][$pmComponent->fields['id']][''])) {
                        unset($_SESSION['glpi_plugin_monitoring']['perfname'][$pmComponent->fields['id']]['']);
                    }
                    $pmServicegraph = new PluginMonitoringServicegraph();
                    $a_ret = $pmServicegraph->generateData($rrdtool_template, $itemtype, $items_id, $timezone, $time, '', $_SESSION['glpi_plugin_monitoring']['perfname'][$pmComponent->fields['id']]);
                    $format = $a_ret[2];
                }
                echo "<script>";
                echo '

            var formaty = ".0f";
            var data' . $ident . ' = [];
            var chart' . $ident . ';
            redraw' . $ident . ' = function () {
               nv.addGraph(function() {
                  if (typeof data' . $ident . '[0] != "undefined") {
                     formaty = data' . $ident . '[0]["formaty"];
                  }
                  chart' . $ident . ' = nv.models.lineChart();

                  chart' . $ident . '.useInteractiveGuideline(true);

                  chart' . $ident . '.xAxis
                     .tickFormat(function(d) { return d3.time.format("' . $format . '")(new Date(d)); });

                  chart' . $ident . '.yAxis
                     .axisLabel("test")
                     .tickFormat(d3.format(formaty));

                  chart' . $ident . '.forceY([0]);

                  d3.select("#chart' . $ident . ' svg")
                    .attr("height", 300)
                    .datum(data' . $ident . ')
                    .transition().duration(50)
                    .call(chart' . $ident . ');

                 return chart' . $ident . ';
               });
            };';
                echo "\n            (function worker" . $items_id . $time . "() {\n              startDate = new Date(\$('#custom_date').val());\n              startTime = Date.parse('04/03/1980 ' + (\$('#custom_time').val()) + ':00');\n              \$.getJSON('" . $CFG_GLPI["root_doc"] . "/plugins/monitoring/ajax/updateChart.php" . "?rrdtool_template=" . $rrdtool_template . "&itemtype=" . $itemtype . "&items_id=" . $items_id . "&timezone=" . $timezone . "&time=" . $time . "&customdate=' + (startDate.getTime()/1000.0) + '" . "&customtime=' + (startTime/1000.0) + '" . "&components_id=" . $pmComponent->fields['id'] . "', function(data) {\n                data" . $ident . " = data;\n\n                var newDate = \$('#custom_date').val() + ' ' + \$('#custom_time').val();\n                var timestamp = new Date(newDate).getTime();\n                if (timestamp > (new Date().getTime() - 3600000)) {\n                    var datenow = new Date();\n                    \$('#custom_time').val(datenow.getHours() + ':' + datenow.getMinutes());\n                    \$('#custom_date').val((datenow.getMonth() + 1) + '/' + datenow.getDate() + '/' + datenow.getFullYear());\n                }\n\n                redraw" . $ident . "();\n                ";
                $refresh = 30;
                // 30 seconds
                if ($time == '12h') {
                    $refresh = 240;
                    // 4 minutes
                } else {
                    if ($time == '1d') {
                        $refresh = 600;
                        // 10 minutes
                    } else {
                        if ($time == '1w') {
                            $refresh = 1800;
                            // 30 minutes
                        }
                    }
                }
                echo "\n                setTimeout(worker" . $items_id . $time . ", " . $refresh . "000);\n              });\n            })();";
                echo "\n            </script>";
            }
        }
        return;
    }
if ($_GET['customdate'] == '' && $_GET['customtime'] == '') {
    $enddate = '';
} else {
    if ($_GET['customdate'] == '') {
        $enddate = mktime(date('H', $_GET['customtime']), date('i', $_GET['customtime']), date('s', $_GET['customtime']));
    } else {
        if ($_GET['customtime'] == '') {
            $enddate = $_GET['customdate'];
        } else {
            // have the 2 defined
            $enddate = mktime(date('H', $_GET['customtime']), date('i', $_GET['customtime']), date('s', $_GET['customtime']), date('n', $_GET['customdate']), date('d', $_GET['customdate']), date('Y', $_GET['customdate']));
        }
    }
}
if (isset($_GET['components_id']) && !isset($_SESSION['glpi_plugin_monitoring']['perfname'][$_GET['components_id']])) {
    PluginMonitoringToolbox::loadPreferences($_GET['components_id']);
}
if (!isset($_SESSION['glpi_plugin_monitoring']['perfname'][$_GET['components_id']])) {
    echo json_encode(array());
    //   echo __('No data ...', 'monitoring');
    exit;
}
if (isset($_SESSION['glpi_plugin_monitoring']['perfname'][$_GET['components_id']][''])) {
    unset($_SESSION['glpi_plugin_monitoring']['perfname'][$_GET['components_id']]['']);
}
$a_ret = $pmServicegraph->generateData($_GET['rrdtool_template'], $_GET['itemtype'], $_GET['items_id'], $_GET['timezone'], $_GET['time'], $enddate, $_SESSION['glpi_plugin_monitoring']['perfname'][$_GET['components_id']]);
$mydatat = $a_ret[0];
$a_labels = $a_ret[1];
$pmComponent = new PluginMonitoringComponent();
$pmCommand = new PluginMonitoringCommand();
$pmComponent->getFromDB($_GET['components_id']);
 function hasCounters()
 {
     if ($this->fields['graph_template'] == 0) {
         return false;
     }
     // Get component graph configuration ...
     if (!isset($_SESSION['glpi_plugin_monitoring']['perfname'][$this->fields['id']])) {
         PluginMonitoringToolbox::loadPreferences($this->fields['id']);
     }
     $a_perf = PluginMonitoringPerfdata::getArrayPerfdata($this->fields['graph_template']);
     $myPerfdata = array();
     foreach ($a_perf['parseperfdata'] as $data) {
         $i = 0;
         foreach ($data['DS'] as $data2) {
             $counter = preg_replace("/[^A-Za-z0-9\\-_]/", "", $data['name']);
             if ($data['incremental'][$i] == '1') {
                 $myPerfdata[$counter] = $data2['dsname'];
             }
             $i++;
         }
     }
     return $myPerfdata;
 }
    static function preferences($components_id, $loadpreferences = 1, $displayonly = 0)
    {
        global $CFG_GLPI;
        if ($loadpreferences == 1) {
            if (!PluginMonitoringToolbox::loadPreferences($components_id)) {
                return false;
            }
        }
        $pmComponent = new PluginMonitoringComponent();
        $pmComponent->getFromDB($components_id);
        $a_perfnames = array();
        $a_perfnames = PluginMonitoringServicegraph::getperfdataNames($pmComponent->fields['graph_template']);
        echo "<table class='tab_cadre_fixe'>";
        echo "<tr class='tab_bg_3'>";
        echo "<td rowspan='" . ceil(count($a_perfnames) / 7) . "' width='90'>";
        echo __('Display', 'monitoring') . "&nbsp;:";
        echo "</td>";
        $i = 0;
        $j = 0;
        if (!isset($_SESSION['glpi_plugin_monitoring']['perfname'][$components_id])) {
            foreach ($a_perfnames as $name) {
                $_SESSION['glpi_plugin_monitoring']['perfname'][$components_id][$name] = 'checked';
            }
        }
        echo "<td>";
        $a_incremental = array();
        $a_perfdatadetails = getAllDatasFromTable('glpi_plugin_monitoring_perfdatadetails', "plugin_monitoring_perfdatas_id='" . $pmComponent->fields['graph_template'] . "'");
        foreach ($a_perfdatadetails as $data) {
            for ($nb = 1; $nb <= 15; $nb++) {
                if ($data['dsnameincr' . $nb] == '1') {
                    $a_incremental[$data['dsname' . $nb]] = 1;
                }
            }
        }
        $a_list = array();
        $a_list_val = array();
        foreach ($a_perfnames as $name) {
            $a_list[] = $name;
            if (isset($_SESSION['glpi_plugin_monitoring']['perfname'][$components_id][$name])) {
                $a_list_val[] = $name;
            }
            if (isset($a_incremental[$name])) {
                $name .= ' | diff';
                if (isset($_SESSION['glpi_plugin_monitoring']['perfname'][$components_id][$name])) {
                    $a_list_val[] = $name;
                }
                $a_list[] = $name;
            }
        }
        //      <input name="perfname" id="jquery-tagbox-select" type="text" value="'.implode('####', $a_list_val).'" />';
        echo '      <div id="tagbox-container"></div>';
        echo "        <script>\n            \$('#tagbox-container').tagbox({\n                taglist: ['" . implode("', '", $a_list) . "'],\n                selectedlist: ['" . implode("', '", $a_list_val) . "'],\n                cols: 3,\n                maxtags: 4,\n                expand: true\n            });\n\n            \$('#tagbox-container').on('tagAdded', function() {\n               \$.get('" . $CFG_GLPI["root_doc"] . "/plugins/monitoring/ajax/updatePerfname.php" . "?components_id=" . $components_id . "&db=" . $loadpreferences . "&perfname=' + \$('.tagbox').data('selected'));\n            });\n            \$('#tagbox-container').on('tagRemoved', function() {\n               \$.get('" . $CFG_GLPI["root_doc"] . "/plugins/monitoring/ajax/updatePerfname.php" . "?components_id=" . $components_id . "&db=" . $loadpreferences . "&perfname=' + \$('.tagbox').data('selected'));\n            });\n        </script>";
        echo "</td>";
        echo "</tr>";
        echo "</table>";
        if ($displayonly == 1) {
            return;
        }
        // * Invert perfname
        $a_perfnames = array();
        $a_perfnames = PluginMonitoringServicegraph::getperfdataNames($pmComponent->fields['graph_template']);
        echo "<table class='tab_cadre_fixe'>";
        echo "<tr class='tab_bg_3'>";
        echo "<td rowspan='" . ceil(count($a_perfnames) / 7) . "' width='90'>";
        echo __('Invert values', 'monitoring') . "&nbsp;:";
        echo "</td>";
        $i = 0;
        $j = 0;
        echo "<td>";
        echo '<select id="jquery-tagbox-select2-options">';
        $a_incremental = array();
        $a_perfdatadetails = getAllDatasFromTable('glpi_plugin_monitoring_perfdatadetails', "plugin_monitoring_perfdatas_id='" . $pmComponent->fields['graph_template'] . "'");
        foreach ($a_perfdatadetails as $data) {
            for ($nb = 1; $nb <= 15; $nb++) {
                if ($data['dsnameincr' . $nb] == '1') {
                    $a_incremental[$data['dsname' . $nb]] = 1;
                }
            }
        }
        $a_list_val2 = array();
        foreach ($a_list_val as $name) {
            $disabled = '';
            if (isset($_SESSION['glpi_plugin_monitoring']['perfnameinvert'][$components_id][$name])) {
                $a_list_val2[] = $name;
                $disabled = 'disabled="disabled"';
            }
            echo '<option value="' . $name . '" ' . $disabled . '>' . $name . '</option>';
            if (isset($a_incremental[$name])) {
                $name .= ' | diff';
                $disabled = '';
                if (isset($_SESSION['glpi_plugin_monitoring']['perfnameinvert'][$components_id][$name])) {
                    $a_list_val[] = $name;
                    $disabled = 'disabled="disabled"';
                }
                echo '<option value="' . $name . '" ' . $disabled . '>' . $name . '</option>';
            }
        }
        echo '</select>
      <input name="perfnameinvert" id="jquery-tagbox-select2" type="text" value="' . implode('####', $a_list_val2) . '" />';
        echo "</td>";
        //      foreach ($a_perfnames as $name) {
        //         if ($i == 'O'
        //                 AND $j == '1') {
        //            echo "<tr>";
        //         }
        //         echo "<td>";
        //         $checked = "";
        //         if (isset($_SESSION['glpi_plugin_monitoring']['perfnameinvert'][$components_id][$name])) {
        //            $checked = $_SESSION['glpi_plugin_monitoring']['perfnameinvert'][$components_id][$name];
        //         }
        //         echo "<input type='checkbox' name='perfnameinvert[]' value='".$name."' ".$checked."/> ".$name;
        //         echo "</td>";
        //         $i++;
        //         if ($i == 6) {
        //            $i = 0;
        //            echo "</tr>";
        //         }
        //         $j = 1;
        //      }
        //      if ($i != 6) {
        //         echo "<td colspan='".(6-$i)."'></td>";
        //         echo "</tr>";
        //      }
        echo "<tr class='tab_bg_3'>";
        echo "<td colspan='9' align='center'>";
        echo "<input type='hidden' name='id' value='" . $components_id . "'/>";
        echo "<input type='submit' name='updateperfdata' value=\"" . __('Save') . "\" class='submit'>";
        echo "</td>";
        echo "</tr>";
        echo "</table>";
        // * Define color of perfname
        $a_perfnames = array();
        $a_perfnames = PluginMonitoringServicegraph::getperfdataNames($pmComponent->fields['graph_template']);
        foreach ($a_perfnames as $key => $name) {
            if (!isset($_SESSION['glpi_plugin_monitoring']['perfname'][$components_id][$name])) {
                unset($a_perfnames[$key]);
            }
        }
        echo "<table class='tab_cadre_fixe'>";
        echo "<tr class='tab_bg_1'>";
        echo "<td rowspan='" . ceil(count($a_perfnames) / 4) . "' width='90'>";
        echo __('Colors', 'monitoring') . "&nbsp;:";
        echo "</td>";
        $i = 0;
        $j = 0;
        $a_colors_warn = PluginMonitoringServicegraph::colors("warn");
        $a_colors_crit = PluginMonitoringServicegraph::colors("crit");
        $a_colors = PluginMonitoringServicegraph::colors();
        foreach ($a_list_val as $name) {
            if ($i == 'O' and $j == '1') {
                echo "<tr>";
            }
            echo "<td>";
            echo $name . "&nbsp;:";
            echo "</td>";
            echo "<td>";
            $color = 'ffffff';
            if (isset($_SESSION['glpi_plugin_monitoring']['perfnamecolor'][$components_id][$name])) {
                $color = $_SESSION['glpi_plugin_monitoring']['perfnamecolor'][$components_id][$name];
            } else {
                if (strstr($name, "warn")) {
                    $color = array_shift($a_colors_warn);
                } else {
                    if (strstr($name, "crit")) {
                        $color = array_shift($a_colors_crit);
                    } else {
                        $color = array_shift($a_colors);
                    }
                }
            }
            echo ' <input class="color" id="color' . $name . '" name="perfnamecolor[' . $name . ']" value="' . $color . '" size="6" />';
            echo '<script type="text/javascript">
var myPicker = new jscolor.color(document.getElementById(\'color' . $name . '\'), {})
myPicker.fromString(\'' . $color . '\')
</script>
';
            //         echo " <select name='perfnamecolor[".$name."]' id='color".$name."'>";
            //         echo "<option value=''>".Dropdown::EMPTY_VALUE."</option>";
            //         foreach ($a_colors as $color) {
            //            $checked = '';
            //            if (isset($_SESSION['glpi_plugin_monitoring']['perfnamecolor'][$components_id][$name])
            //                    AND $_SESSION['glpi_plugin_monitoring']['perfnamecolor'][$components_id][$name] == $color) {
            //               $checked = 'selected';
            //            }
            //            echo "<option value='".$color."' style='background-color: #".$color.";' ".$checked.">".$color."</option>";
            //         }
            //         echo "</select>";
            echo "</td>";
            $i++;
            if ($i == 4) {
                $i = 0;
                echo "</tr>";
            }
            $j = 1;
        }
        if ($i != 4) {
            echo "<td colspan='" . (4 - $i) * 2 . "'></td>";
            echo "</tr>";
        }
        echo "<tr>";
        echo "<td colspan='9' align='center'>";
        echo "<input type='hidden' name='id' value='" . $components_id . "'/>";
        echo "<input type='submit' name='updateperfdata' value=\"" . __('Save') . "\" class='submit'>";
        echo "</td>";
        echo "</tr>";
        echo "</table>";
        Html::closeForm();
        return true;
    }