function addComponentToItems($componentscatalogs_id, $components_id)
 {
     global $DB;
     $pmService = new PluginMonitoringService();
     $pmComponentscatalog_rule = new PluginMonitoringComponentscatalog_rule();
     $pmNetworkport = new PluginMonitoringNetworkport();
     $pluginMonitoringNetworkport = 0;
     $query = "SELECT * FROM `" . $pmComponentscatalog_rule->getTable() . "`\n         WHERE `itemtype`='PluginMonitoringNetworkport'\n            AND `plugin_monitoring_componentscalalog_id`='" . $componentscatalogs_id . "'\n         LIMIT 1";
     $result = $DB->query($query);
     if ($DB->numrows($result) > 0) {
         $pluginMonitoringNetworkport = 1;
     }
     $query = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n         WHERE `plugin_monitoring_componentscalalog_id`='" . $componentscatalogs_id . "'";
     $result = $DB->query($query);
     while ($data = $DB->fetch_array($result)) {
         $itemtype = $data['itemtype'];
         $item = new $itemtype();
         $item->getFromDB($data['items_id']);
         if ($pluginMonitoringNetworkport == '0') {
             $input = array();
             $input['entities_id'] = $item->fields['entities_id'];
             $input['plugin_monitoring_componentscatalogs_hosts_id'] = $data['id'];
             $input['plugin_monitoring_components_id'] = $components_id;
             $input['name'] = Dropdown::getDropdownName("glpi_plugin_monitoring_components", $components_id);
             $input['state'] = 'WARNING';
             $input['state_type'] = 'HARD';
             $pmService->add($input);
         } else {
             if ($pluginMonitoringNetworkport == '1') {
                 $a_services_created = array();
                 $querys = "SELECT * FROM `glpi_plugin_monitoring_services`\n               WHERE `plugin_monitoring_components_id`='" . $components_id . "'\n                  AND `plugin_monitoring_componentscatalogs_hosts_id`='" . $data['id'] . "'";
                 $results = $DB->query($querys);
                 while ($datas = $DB->fetch_array($results)) {
                     $a_services_created[$datas['networkports_id']] = $datas['id'];
                 }
                 $a_ports = $pmNetworkport->find("`itemtype`='" . $data['itemtype'] . "'\n               AND `items_id`='" . $data['items_id'] . "'");
                 foreach ($a_ports as $datap) {
                     if (isset($a_services_created[$datap["id"]])) {
                         unset($a_services_created[$datap["id"]]);
                     } else {
                         $input = array();
                         $input['networkports_id'] = $datap['networkports_id'];
                         $input['entities_id'] = $item->fields['entities_id'];
                         $input['plugin_monitoring_componentscatalogs_hosts_id'] = $data['id'];
                         $input['plugin_monitoring_components_id'] = $components_id;
                         $input['name'] = Dropdown::getDropdownName("glpi_plugin_monitoring_components", $components_id);
                         $input['state'] = 'WARNING';
                         $input['state_type'] = 'HARD';
                         $pmService->add($input);
                     }
                 }
                 foreach ($a_services_created as $id) {
                     $_SESSION['plugin_monitoring_hosts'] = $data;
                     $pmService->delete(array('id' => $id));
                 }
             }
         }
     }
 }
 static function isThisItemCheckRule($parm)
 {
     global $DB;
     $itemtype = get_class($parm);
     $items_id = $parm->fields['id'];
     $a_find = array();
     $pmComponentscatalog_rule = new PluginMonitoringComponentscatalog_rule();
     $pmComponentscatalog = new PluginMonitoringComponentscatalog();
     $pmSearch = new PluginMonitoringSearch();
     $query = "SELECT * FROM `" . $pmComponentscatalog_rule->getTable() . "`\n         WHERE `itemtype`='" . $itemtype . "'";
     $result = $DB->query($query);
     $get_tmp = array();
     if (isset($_GET)) {
         $get_tmp = $_GET;
     }
     while ($data = $DB->fetch_array($result)) {
         if (isset($_SESSION["glpisearchcount"][$data['itemtype']])) {
             unset($_SESSION["glpisearchcount"][$data['itemtype']]);
         }
         if (isset($_SESSION["glpisearchcount2"][$data['itemtype']])) {
             unset($_SESSION["glpisearchcount2"][$data['itemtype']]);
         }
         $_GET = importArrayFromDB($data['condition']);
         $_GET["glpisearchcount"] = count($_GET['field']);
         if (isset($_GET['field2'])) {
             $_GET["glpisearchcount2"] = count($_GET['field2']);
         }
         if (!isset($_SESSION['glpiactiveentities_string'])) {
             $_SESSION['glpiactiveentities_string'] = $parm->fields['entities_id'];
         }
         // Load right entity
         $pmComponentscatalog->getFromDB($data['plugin_monitoring_componentscalalog_id']);
         $default_entity = 0;
         if (isset($_SESSION['glpiactive_entity'])) {
             $default_entity = $_SESSION['glpiactive_entity'];
         }
         $entities_isrecursive = 0;
         if (isset($_SESSION['glpiactiveentities']) and count($_SESSION['glpiactiveentities']) > 1) {
             $entities_isrecursive = 1;
         }
         if (!isset($_SESSION['glpiactiveprofile']['entities'])) {
             $_SESSION['glpiactiveprofile']['entities'] = array($pmComponentscatalog->fields['entities_id'] => array('id' => $pmComponentscatalog->fields['entities_id'], 'name' => '', 'is_recursive' => $pmComponentscatalog->fields['is_recursive']));
         }
         Session::changeActiveEntities($pmComponentscatalog->fields['entities_id'], $pmComponentscatalog->fields['is_recursive']);
         Search::manageGetValues($data['itemtype']);
         $resultr = $pmSearch->constructSQL($itemtype, $_GET, $items_id);
         if ($DB->numrows($resultr) > 0) {
             $a_find[$data['plugin_monitoring_componentscalalog_id']] = 1;
         } else {
             if (!isset($a_find[$data['plugin_monitoring_componentscalalog_id']])) {
                 $a_find[$data['plugin_monitoring_componentscalalog_id']] = 0;
             }
         }
         // Reload current entity
         Session::changeActiveEntities($default_entity, $entities_isrecursive);
     }
     if (count($get_tmp) > 0) {
         $_GET = $get_tmp;
     }
     $pmComponentscatalog_Host = new PluginMonitoringComponentscatalog_Host();
     foreach ($a_find as $componentscalalog_id => $is_present) {
         if ($is_present == '0') {
             // * Remove from dynamic if present
             $query = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n               WHERE `plugin_monitoring_componentscalalog_id`='" . $componentscalalog_id . "'\n                  AND `itemtype`='" . $itemtype . "'\n                  AND `items_id`='" . $items_id . "'\n                  AND`is_static`='0'";
             $result = $DB->query($query);
             while ($data = $DB->fetch_array($result)) {
                 $pmComponentscatalog_Host->delete(array('id' => $data['id']));
             }
         } else {
             //  add if not present
             $query = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n               WHERE `plugin_monitoring_componentscalalog_id`='" . $componentscalalog_id . "'\n                  AND `itemtype`='" . $itemtype . "'\n                  AND `items_id`='" . $items_id . "'\n                     LIMIT 1";
             $result = $DB->query($query);
             if ($DB->numrows($result) == '0') {
                 $input = array();
                 $input['plugin_monitoring_componentscalalog_id'] = $componentscalalog_id;
                 $input['is_static'] = '0';
                 $input['items_id'] = $items_id;
                 $input['itemtype'] = $itemtype;
                 $componentscatalogs_hosts_id = $pmComponentscatalog_Host->add($input);
                 $pmComponentscatalog_Host->linkComponentsToItem($componentscalalog_id, $componentscatalogs_hosts_id);
             } else {
                 $data2 = $DB->fetch_assoc($result);
                 // modify entity of services (if entity of device is changed)
                 $item = new $itemtype();
                 $item->getFromDB($items_id);
                 $queryu = "UPDATE `glpi_plugin_monitoring_services`\n                     SET `entities_id`='" . $item->fields['entities_id'] . "'\n                        WHERE `plugin_monitoring_componentscatalogs_hosts_id`='" . $data2['id'] . "'";
                 $DB->query($queryu);
             }
         }
     }
     if ($itemtype == 'NetworkEquipment') {
         //Get networkports
         $pmComponentscatalog_rule->isThisItemCheckRuleNetworkport($parm);
     }
 }
 static function isThisItemCheckRule($parm)
 {
     global $DB;
     $itemtype = get_class($parm);
     $items_id = $parm->fields['id'];
     $session_glpisearch = array();
     if (isset($_SESSION['glpisearch'])) {
         $session_glpisearch = $_SESSION['glpisearch'];
     }
     $session_glpisearchcount = array();
     if (isset($_SESSION['glpisearchcount'])) {
         $session_glpisearchcount = $_SESSION['glpisearchcount'];
     }
     $session_glpisearchcount2 = array();
     if (isset($_SESSION['glpisearchcount2'])) {
         $session_glpisearchcount2 = $_SESSION['glpisearchcount2'];
     }
     $a_find = array();
     $pmComponentscatalog_rule = new PluginMonitoringComponentscatalog_rule();
     $pmComponentscatalog = new PluginMonitoringComponentscatalog();
     $query = "SELECT * FROM `" . $pmComponentscatalog_rule->getTable() . "`\n         WHERE `itemtype`='" . $itemtype . "'";
     $result = $DB->query($query);
     $get_tmp = array();
     if (isset($_GET)) {
         $get_tmp = $_GET;
     }
     while ($data = $DB->fetch_array($result)) {
         if (!isset($_SESSION['glpiactiveentities_string'])) {
             $_SESSION['glpiactiveentities_string'] = $parm->fields['entities_id'];
         }
         // Load right entity
         $pmComponentscatalog->getFromDB($data['plugin_monitoring_componentscalalog_id']);
         $default_entity = 0;
         if (isset($_SESSION['glpiactive_entity'])) {
             $default_entity = $_SESSION['glpiactive_entity'];
         }
         $entities_isrecursive = 0;
         if (isset($_SESSION['glpiactiveentities']) and count($_SESSION['glpiactiveentities']) > 1) {
             $entities_isrecursive = 1;
         }
         if (!isset($_SESSION['glpiactiveprofile']['entities'])) {
             $_SESSION['glpiactiveprofile']['entities'] = array($pmComponentscatalog->fields['entities_id'] => array('id' => $pmComponentscatalog->fields['entities_id'], 'name' => '', 'is_recursive' => $pmComponentscatalog->fields['is_recursive']));
         }
         Session::changeActiveEntities($pmComponentscatalog->fields['entities_id'], $pmComponentscatalog->fields['is_recursive']);
         $itemtype = $data['itemtype'];
         $condition = importArrayFromDB($data['condition']);
         $params = Search::manageParams($itemtype, $condition, FALSE);
         $datar = Search::prepareDatasForSearch($itemtype, $params);
         Search::constructSQL($datar);
         $DBread = DBConnection::getReadConnection();
         $DBread->query("SET SESSION group_concat_max_len = 16384;");
         $resultr = $DBread->query($datar['sql']['search']);
         /// Check group concat limit : if warning : increase limit
         if ($result2 = $DBread->query('SHOW WARNINGS')) {
             if ($DBread->numrows($result2) > 0) {
                 $res = $DBread->fetch_assoc($result2);
                 if ($res['Code'] == 1260) {
                     $DBread->query("SET SESSION group_concat_max_len = 4194304;");
                     $resultr = $DBread->query($datar['sql']['search']);
                 }
             }
         }
         $find = 0;
         while ($datar = $DB->fetch_array($resultr)) {
             if ($datar['id'] == $items_id) {
                 $find = 1;
                 break;
             }
         }
         if ($find == 1) {
             $a_find[$data['plugin_monitoring_componentscalalog_id']] = 1;
         } else {
             if (!isset($a_find[$data['plugin_monitoring_componentscalalog_id']])) {
                 $a_find[$data['plugin_monitoring_componentscalalog_id']] = 0;
             }
         }
         // Reload current entity
         Session::changeActiveEntities($default_entity, $entities_isrecursive);
     }
     if (count($get_tmp) > 0) {
         $_GET = $get_tmp;
     }
     $pmComponentscatalog_Host = new PluginMonitoringComponentscatalog_Host();
     foreach ($a_find as $componentscalalog_id => $is_present) {
         if ($is_present == '0') {
             // * Remove from dynamic if present
             $query = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n               WHERE `plugin_monitoring_componentscalalog_id`='" . $componentscalalog_id . "'\n                  AND `itemtype`='" . $itemtype . "'\n                  AND `items_id`='" . $items_id . "'\n                  AND`is_static`='0'";
             $result = $DB->query($query);
             while ($data = $DB->fetch_array($result)) {
                 $pmComponentscatalog_Host->delete(array('id' => $data['id']));
             }
         } else {
             //  add if not present
             $query = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n               WHERE `plugin_monitoring_componentscalalog_id`='" . $componentscalalog_id . "'\n                  AND `itemtype`='" . $itemtype . "'\n                  AND `items_id`='" . $items_id . "'\n                     LIMIT 1";
             $result = $DB->query($query);
             if ($DB->numrows($result) == '0') {
                 $input = array();
                 $input['plugin_monitoring_componentscalalog_id'] = $componentscalalog_id;
                 $input['is_static'] = '0';
                 $input['items_id'] = $items_id;
                 $input['itemtype'] = $itemtype;
                 $pmComponentscatalog_Host->add($input);
             } else {
                 $data2 = $DB->fetch_assoc($result);
                 // modify entity of services (if entity of device is changed)
                 $item = new $itemtype();
                 $item->getFromDB($items_id);
                 $queryu = "UPDATE `glpi_plugin_monitoring_services`\n                     SET `entities_id`='" . $item->fields['entities_id'] . "'\n                        WHERE `plugin_monitoring_componentscatalogs_hosts_id`='" . $data2['id'] . "'";
                 $DB->query($queryu);
             }
         }
     }
     if ($itemtype == 'NetworkEquipment') {
         //Get networkports
         $pmComponentscatalog_rule->isThisItemCheckRuleNetworkport($parm);
     }
     $_SESSION['glpisearch'] = $session_glpisearch;
     $_SESSION['glpisearchcount'] = $session_glpisearchcount;
     $_SESSION['glpisearchcount2'] = $session_glpisearchcount2;
 }