function activation($ID)
 {
     global $DB;
     $mib_networking = new PluginFusioninventoryMib();
     $mib_networking->getFromDB($ID);
     $data['ID'] = $ID;
     $data = $mib_networking->fields;
     if ($mib_networking->fields["activation"] == "1") {
         $data['activation'] = 0;
     } else {
         $data['activation'] = 1;
     }
     $data["links_oid_fields"] = $data["mapping_type"] . "||" . $data["mapping_name"];
     $mib_networking->update($data);
 }
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with GLPI; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  ------------------------------------------------------------------------
*/
// Original Author of file: David DURIEUX
// Purpose of file:
// ----------------------------------------------------------------------
$NEEDED_ITEMS = array("setup", "rulesengine", "fusioninventory", "search");
define('GLPI_ROOT', '../../..');
include GLPI_ROOT . "/inc/includes.php";
PluginFusioninventoryAuth::checkRight("snmp_models", "r");
$plugin_fusioninventory_model_infos = new PluginFusioninventoryModelInfos();
$plugin_fusioninventory_mib_networking = new PluginFusioninventoryMib();
$importexport = new PluginFusioninventoryImportExport();
commonHeader($LANG['plugin_fusioninventory']["title"][0], $_SERVER["PHP_SELF"], "plugins", "fusioninventory", "models");
PluginFusioninventoryDisplay::mini_menu();
//if (isset ($_POST["add"]) && isset($_POST["ID"])) {
if (isset($_POST["add"])) {
    PluginFusioninventoryAuth::checkRight("snmp_models", "w");
    $plugin_fusioninventory_model_infos->add($_POST);
    glpi_header($_SERVER['HTTP_REFERER']);
} else {
    if (isset($_POST["update"])) {
        PluginFusioninventoryAuth::checkRight("snmp_models", "w");
        $plugin_fusioninventory_model_infos->update($_POST);
        glpi_header($_SERVER['HTTP_REFERER']);
    } else {
        if (isset($_POST["delete"])) {
 function generatemodels()
 {
     global $DB;
     $ptmi = new PluginFusioninventoryModelInfos();
     $ptmn = new PluginFusioninventoryMib();
     $query = "SELECT glpi_plugin_fusioninventory_construct_device.ID, type  FROM glpi_plugin_fusioninventory_construct_device\n         LEFT JOIN glpi_plugin_fusioninventory_construct_walks on glpi_plugin_fusioninventory_construct_device.ID = construct_device_id\n         WHERE type IN (1,2,3)\n            AND log!=''";
     if ($result = $DB->query($query)) {
         while ($data = $DB->fetch_array($result)) {
             // Load mibs
             $a_mib = array();
             $count_mib = 0;
             $query_mibs = "SELECT * FROM glpi_plugin_fusioninventory_construct_mibs\n               WHERE construct_device_id='" . $data["ID"] . "' ";
             if ($result_mibs = $DB->query($query_mibs)) {
                 while ($data_mibs = $DB->fetch_array($result_mibs)) {
                     $a_mib[$data_mibs['mib_oid_id']]['mapping_type'] = $data_mibs['mapping_type'];
                     $a_mib[$data_mibs['mib_oid_id']]['mapping_name'] = $data_mibs['mapping_name'];
                     $a_mib[$data_mibs['mib_oid_id']]['oid_port_counter'] = $data_mibs['oid_port_counter'];
                     $a_mib[$data_mibs['mib_oid_id']]['oid_port_dyn'] = $data_mibs['oid_port_dyn'];
                     $a_mib[$data_mibs['mib_oid_id']]['vlan'] = $data_mibs['vlan'];
                     $count_mib++;
                 }
             }
             // See if model exactly exists
             $query_models = "SELECT * FROM glpi_plugin_fusioninventory_model_infos";
             $existent = 0;
             if ($result_models = $DB->query($query_models)) {
                 while ($data_models = $DB->fetch_array($result_models)) {
                     if ($existent != '1') {
                         $count_mib_model = 0;
                         $query_mibs_model = "SELECT * FROM glpi_plugin_fusioninventory_mib\n                        WHERE FK_model_infos='" . $data_models['ID'] . "' ";
                         if ($result_mib_model = $DB->query($query_mibs_model)) {
                             while ($data_mib_model = $DB->fetch_array($result_mib_model)) {
                                 $count_mib_model++;
                                 if ($existent != '-1') {
                                     if (isset($a_mib[$data_mib_model['FK_mib_oid']]['mapping_type'])) {
                                         // Oid Existe, on vérifie si tous les paramètres sont pareils
                                         if ($a_mib[$data_mib_model['FK_mib_oid']]['mapping_type'] == $data_mib_model['mapping_type'] and $a_mib[$data_mib_model['FK_mib_oid']]['mapping_name'] == $data_mib_model['mapping_name'] and $a_mib[$data_mib_model['FK_mib_oid']]['oid_port_counter'] == $data_mib_model['oid_port_counter'] and $a_mib[$data_mib_model['FK_mib_oid']]['oid_port_dyn'] == $data_mib_model['oid_port_dyn'] and $a_mib[$data_mib_model['FK_mib_oid']]['vlan'] == $data_mib_model['vlan']) {
                                         } else {
                                             $existent = '-1';
                                         }
                                     } else {
                                         $existent = '-1';
                                     }
                                 }
                             }
                         }
                         if ($existent == '0' and $count_mib == $count_mib_model) {
                             // Add number in database
                             $query_update = "UPDATE glpi_plugin_fusioninventory_construct_device\n                           SET snmpmodel_id='" . $data_models['ID'] . "'\n                           WHERE ID='" . $data["ID"] . "'";
                             $DB->query($query_update);
                             $existent = 1;
                         } else {
                             $existent = 0;
                         }
                     }
                 }
             }
             if ($existent != '1') {
                 // Create model
                 $a_input = array();
                 $a_input['name'] = rand(10000, 10000000);
                 $a_input['device_type'] = $data["type"];
                 $a_input['activation'] = 1;
                 $id = $ptmi->add($a_input);
                 $query_mibs = "SELECT * FROM glpi_plugin_fusioninventory_construct_mibs\n                  WHERE construct_device_id='" . $data["ID"] . "' ";
                 if ($result_mibs = $DB->query($query_mibs)) {
                     while ($data_mibs = $DB->fetch_array($result_mibs)) {
                         $a_input = array();
                         $a_input['FK_model_infos'] = $id;
                         $a_input['FK_mib_oid'] = $data_mibs['mib_oid_id'];
                         $a_input['oid_port_counter'] = $data_mibs['oid_port_counter'];
                         $a_input['oid_port_dyn'] = $data_mibs['oid_port_dyn'];
                         $a_input['vlan'] = $data_mibs['vlan'];
                         $a_input['links_oid_fields'] = $data_mibs['mapping_type'] . "||" . $data_mibs['mapping_name'];
                         $a_input['activation'] = 1;
                         $ptmn->add($a_input);
                     }
                 }
                 $query_update = "UPDATE glpi_plugin_fusioninventory_construct_device\n                  SET snmpmodel_id='" . $id . "'\n                  WHERE ID='" . $data["ID"] . "'";
                 $DB->query($query_update);
             }
         }
     }
     // Add Number
     //key : Networking0006
     $query = "SELECT * FROM glpi_plugin_fusioninventory_model_infos\n         WHERE discovery_key LIKE 'Networking%'\n         ORDER BY discovery_key DESC\n         LIMIT 1";
     $result = $DB->query($query);
     $data = $DB->fetch_assoc($result);
     $num = 1;
     if (!empty($data['discovery_key'])) {
         $num = str_replace('Networking', '', $data['discovery_key']);
         $num++;
     }
     $query = "SELECT * FROM glpi_plugin_fusioninventory_model_infos\n         WHERE (discovery_key IS NULL OR discovery_key='')\n            AND device_type='" . NETWORKING_TYPE . "' ";
     if ($result = $DB->query($query)) {
         while ($data = $DB->fetch_array($result)) {
             while (strlen($num) < 4) {
                 $num = "0" . $num;
             }
             $query_update = "UPDATE glpi_plugin_fusioninventory_model_infos\n               SET discovery_key='Networking" . $num . "'\n                  WHERE ID='" . $data['ID'] . "'";
             $DB->query($query_update);
             $num++;
         }
     }
     // Printers
     $query = "SELECT * FROM glpi_plugin_fusioninventory_model_infos\n         WHERE discovery_key LIKE 'Printer%'\n         ORDER BY discovery_key DESC\n         LIMIT 1";
     $result = $DB->query($query);
     $data = $DB->fetch_assoc($result);
     if (empty($data['discovery_key'])) {
         $num = '1';
     } else {
         $num = str_replace('Networking', '', $data['discovery_key']);
         $num++;
     }
     $query = "SELECT * FROM glpi_plugin_fusioninventory_model_infos\n         WHERE (discovery_key IS NULL OR discovery_key='')\n            AND device_type='" . PRINTER_TYPE . "' ";
     if ($result = $DB->query($query)) {
         while ($data = $DB->fetch_array($result)) {
             while (strlen($num) < 4) {
                 $num = "0" . $num;
             }
             $query_update = "UPDATE glpi_plugin_fusioninventory_model_infos\n               SET discovery_key='Printer" . $num . "'\n                  WHERE ID='" . $data['ID'] . "'";
             $DB->query($query_update);
             $num++;
         }
     }
 }
 /**
  * Add MODEL string to XML node
  *
  *@param $p_sxml_node XML node to complete
  *@param $p_id Model id
  *@return nothing
  **/
 function addModel($p_sxml_node, $p_id)
 {
     $models = new PluginFusioninventoryModelInfos();
     $mib_networking = new PluginFusioninventoryMib();
     $models->getFromDB($p_id);
     $sxml_model = $p_sxml_node->addChild('MODEL');
     $sxml_model->addAttribute('ID', $p_id);
     $sxml_model->addAttribute('NAME', $models->fields['name']);
     $mib_networking->oidList($sxml_model, $p_id);
 }