static function getItemsDynamicly($parm)
 {
     global $DB;
     $pmCc_Rule = new PluginMonitoringComponentscatalog_rule();
     $pmComponentscatalog_Host = new PluginMonitoringComponentscatalog_Host();
     $pmComponentscatalog = new PluginMonitoringComponentscatalog();
     $pmSearch = new PluginMonitoringSearch();
     $pmService = new PluginMonitoringService();
     $devices_present = array();
     $devicesnetworkport_present = array();
     if ($pmCc_Rule->getFromDB($parm->fields['id'])) {
         // Load right entity
         $pmComponentscatalog->getFromDB($pmCc_Rule->fields['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;
         }
         Session::changeActiveEntities($pmComponentscatalog->fields['entities_id'], $pmComponentscatalog->fields['is_recursive']);
         $get_tmp = '';
         $itemtype = $pmCc_Rule->fields['itemtype'];
         if (isset($_GET)) {
             $get_tmp = $_GET;
         }
         if (isset($_SESSION["glpisearchcount"][$pmCc_Rule->fields['itemtype']])) {
             unset($_SESSION["glpisearchcount"][$pmCc_Rule->fields['itemtype']]);
         }
         if (isset($_SESSION["glpisearchcount2"][$pmCc_Rule->fields['itemtype']])) {
             unset($_SESSION["glpisearchcount2"][$pmCc_Rule->fields['itemtype']]);
         }
         $_GET = importArrayFromDB($pmCc_Rule->fields['condition']);
         $_GET["glpisearchcount"] = count($_GET['field']);
         if (isset($_GET['field2'])) {
             $_GET["glpisearchcount2"] = count($_GET['field2']);
         }
         Search::manageGetValues($pmCc_Rule->fields['itemtype']);
         $queryd = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n            WHERE `plugin_monitoring_componentscalalog_id`='" . $pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"] . "'\n               AND `itemtype`='" . $pmCc_Rule->fields['itemtype'] . "'\n               AND `is_static`='0'";
         $result = $DB->query($queryd);
         while ($data = $DB->fetch_array($result)) {
             $devices_present[$data['id']] = 1;
         }
         $queryd = "SELECT `glpi_plugin_monitoring_services`.`id` FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n               LEFT JOIN `glpi_plugin_monitoring_services`\n                  ON `plugin_monitoring_componentscatalogs_hosts_id` = \n                     `glpi_plugin_monitoring_componentscatalogs_hosts`.`id`\n            WHERE `plugin_monitoring_componentscalalog_id`='" . $pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"] . "'\n               AND `itemtype`='NetworkEquipment'\n               AND `is_static`='0'";
         $result = $DB->query($queryd);
         while ($data = $DB->fetch_array($result)) {
             $devicesnetworkport_present[$data['id']] = 1;
         }
         $glpilist_limit = $_SESSION['glpilist_limit'];
         $_SESSION['glpilist_limit'] = 500000;
         $result = $pmSearch->constructSQL($itemtype, $_GET);
         $_SESSION['glpilist_limit'] = $glpilist_limit;
         while ($data = $DB->fetch_array($result)) {
             $networkports_id = 0;
             $itemtype_device = $pmCc_Rule->fields['itemtype'];
             $items_id_device = $data['id'];
             if ($itemtype_device == 'PluginMonitoringNetworkport') {
                 //               $queryh = "SELECT `itemtype`, `items_id`,
                 //                     `glpi_plugin_monitoring_services`.`id`,
                 //                     `glpi_plugin_monitoring_componentscatalogs_hosts`.`id` as hid
                 //                     FROM `glpi_plugin_monitoring_componentscatalogs_hosts`
                 //                  LEFT JOIN `glpi_plugin_monitoring_services`
                 //                     ON `plugin_monitoring_componentscatalogs_hosts_id` =
                 //                        `glpi_plugin_monitoring_componentscatalogs_hosts`.`id`
                 //                  WHERE `plugin_monitoring_componentscalalog_id`='".$pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"]."'
                 //                     AND `itemtype`='".$itemtype_device."'
                 //                     AND `items_id`='".$items_id_device."'
                 //                     AND `glpi_plugin_monitoring_services`.`id` IS NULL";
                 //               $resulth = $DB->query($queryh);
                 //               while ($datah=$DB->fetch_array($resulth)) {
                 //                  $pmComponentscatalog_Host->delete(array('id'=>$datah['hid']));
                 //               }
                 $pmNetworkport = new PluginMonitoringNetworkport();
                 $pmNetworkport->getFromDB($data['id']);
                 $itemtype_device = $pmNetworkport->fields['itemtype'];
                 $items_id_device = $pmNetworkport->fields['items_id'];
                 $networkports_id = $pmNetworkport->fields['networkports_id'];
                 $networkPort = new NetworkPort();
                 if ($networkPort->getFromDB($networkports_id)) {
                     $querynet = "SELECT `itemtype`, `items_id`,\n                        `glpi_plugin_monitoring_services`.`id`               \n                        FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n                     LEFT JOIN `glpi_plugin_monitoring_services`\n                        ON `plugin_monitoring_componentscatalogs_hosts_id` = \n                           `glpi_plugin_monitoring_componentscatalogs_hosts`.`id`\n                     WHERE `plugin_monitoring_componentscalalog_id`='" . $pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"] . "'\n                        AND `itemtype`='" . $itemtype_device . "'\n                        AND `items_id`='" . $items_id_device . "'\n                        AND `networkports_id`='" . $networkports_id . "'\n                           LIMIT 1";
                     $resultnet = $DB->query($querynet);
                     if ($DB->numrows($resultnet) == 0) {
                         $input = array();
                         $input['plugin_monitoring_componentscalalog_id'] = $pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"];
                         $input['is_static'] = '0';
                         $input['items_id'] = $items_id_device;
                         $input['itemtype'] = $itemtype_device;
                         $componentscatalogs_hosts_id = $pmComponentscatalog_Host->add($input);
                         $pmComponentscatalog_Host->linkComponentsToItem($pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"], $componentscatalogs_hosts_id, $networkports_id);
                     } else {
                         $data2 = $DB->fetch_assoc($resultnet);
                         // modify entity of services (if entity of device is changed)
                         $itemtype = $data2['itemtype'];
                         $item = new $itemtype();
                         $item->getFromDB($data2['items_id']);
                         $queryu = "UPDATE `glpi_plugin_monitoring_services`\n                           SET `entities_id`='" . $item->fields['entities_id'] . "'\n                              WHERE `id`='" . $data2['id'] . "'";
                         $DB->query($queryu);
                         unset($devicesnetworkport_present[$data2['id']]);
                     }
                 } else {
                     $pmNetworkport->delete($pmNetworkport->fields);
                 }
                 // Reload current entity
                 Session::changeActiveEntities($default_entity, $entities_isrecursive);
             } else {
                 $queryh = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n                  WHERE `plugin_monitoring_componentscalalog_id`='" . $pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"] . "'\n                     AND `itemtype`='" . $itemtype_device . "'\n                     AND `items_id`='" . $items_id_device . "'\n                        LIMIT 1";
                 $resulth = $DB->query($queryh);
                 if ($DB->numrows($resulth) == '0') {
                     $input = array();
                     $input['plugin_monitoring_componentscalalog_id'] = $pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"];
                     $input['is_static'] = '0';
                     $input['items_id'] = $items_id_device;
                     $input['itemtype'] = $itemtype_device;
                     $componentscatalogs_hosts_id = $pmComponentscatalog_Host->add($input);
                     $pmComponentscatalog_Host->linkComponentsToItem($pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"], $componentscatalogs_hosts_id, $networkports_id);
                 } else {
                     $data2 = $DB->fetch_assoc($resulth);
                     // modify entity of services (if entity of device is changed)
                     $itemtype = $data2['itemtype'];
                     $item = new $itemtype();
                     $item->getFromDB($data2['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);
                     unset($devices_present[$data2['id']]);
                 }
             }
         }
         // Reload current entity
         Session::changeActiveEntities($default_entity, $entities_isrecursive);
         foreach ($devicesnetworkport_present as $id => $num) {
             $_SESSION['plugin_monitoring_hosts']['itemtype'] = $itemtype_device;
             $_SESSION['plugin_monitoring_hosts']['items_id'] = $items_id_device;
             $pmService->delete(array('id' => $id));
         }
     } else {
         // Purge
         $queryd = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n            WHERE `plugin_monitoring_componentscalalog_id`='" . $parm->fields["plugin_monitoring_componentscalalog_id"] . "'\n               AND `itemtype`='" . $parm->fields['itemtype'] . "'\n               AND `is_static`='0'";
         $result = $DB->query($queryd);
         while ($data = $DB->fetch_array($result)) {
             $devices_present[$data['id']] = 1;
         }
     }
     foreach ($devices_present as $id => $num) {
         $pmComponentscatalog_Host->delete(array('id' => $id));
     }
     return true;
 }
if (!$DB->connected) {
    die("No DB connection\n");
}
// Exit if plugin monitoring not activated
$Plugin = new Plugin();
if (!$Plugin->isActivated('monitoring')) {
    echo "Plugin monitoring not activated!\n";
    exit;
}
// * used to clean networkports
echo "Delete orphaned networkports ";
$pmNetworkport = new PluginMonitoringNetworkport();
$query = "SELECT `glpi_plugin_monitoring_networkports`.* \n           FROM `glpi_plugin_monitoring_networkports`\n           LEFT JOIN `glpi_networkports`\n               ON `glpi_plugin_monitoring_networkports`.`networkports_id`\n                  = `glpi_networkports`.`id`\n           WHERE `glpi_networkports`.`id` IS NULL";
$result = $DB->query($query);
while ($data = $DB->fetch_array($result)) {
    $pmNetworkport->delete($data);
    echo ".";
}
echo " done\n";
// * Clean services
echo "Delete orphaned services ";
$pmService = new PluginMonitoringService();
$query = "SELECT `glpi_plugin_monitoring_services`.* \n           FROM `glpi_plugin_monitoring_services`\n           LEFT JOIN `glpi_plugin_monitoring_componentscatalogs_hosts`\n               ON `plugin_monitoring_componentscatalogs_hosts_id`\n                  = `glpi_plugin_monitoring_componentscatalogs_hosts`.`id`\n           WHERE `glpi_plugin_monitoring_componentscatalogs_hosts`.`id` IS NULL";
$result = $DB->query($query);
while ($data = $DB->fetch_array($result)) {
    $pmService->delete($data);
    echo ".";
}
echo " done\n";
// * clean services for networkport not linked with networkport
echo "Delete services not have netwokrport linked";
 static function deleteNetworkPort($parm)
 {
     global $DB;
     if ($parm->fields['itemtype'] == 'NetworkEquipment') {
         $query = "SELECT * FROM `glpi_plugin_monitoring_networkports`\n            WHERE `networkports_id`='" . $parm->fields['id'] . "'";
         $result = $DB->query($query);
         while ($data = $DB->fetch_array($result)) {
             $pmNetworkPort = new PluginMonitoringNetworkport();
             $pmNetworkPort->delete($data);
         }
     }
 }
 static function getItemsDynamicly($parm)
 {
     global $DB;
     $pmCc_Rule = new PluginMonitoringComponentscatalog_rule();
     $pmComponentscatalog_Host = new PluginMonitoringComponentscatalog_Host();
     $pmComponentscatalog = new PluginMonitoringComponentscatalog();
     $pmService = new PluginMonitoringService();
     $devices_present = array();
     $devicesnetworkport_present = array();
     if ($pmCc_Rule->getFromDB($parm->fields['id'])) {
         // Load right entity
         $pmComponentscatalog->getFromDB($pmCc_Rule->fields['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();
         }
         Session::changeActiveEntities($pmComponentscatalog->fields['entities_id'], $pmComponentscatalog->fields['is_recursive']);
         $get_tmp = '';
         $itemtype = $pmCc_Rule->fields['itemtype'];
         $condition = importArrayFromDB($pmCc_Rule->fields['condition']);
         $params = Search::manageParams($itemtype, $condition, FALSE);
         $queryd = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n            WHERE `plugin_monitoring_componentscalalog_id`='" . $pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"] . "'\n               AND `itemtype`='" . $pmCc_Rule->fields['itemtype'] . "'\n               AND `is_static`='0'";
         $result = $DB->query($queryd);
         while ($data = $DB->fetch_array($result)) {
             $devices_present[$data['id']] = 1;
         }
         $queryd = "SELECT `glpi_plugin_monitoring_services`.`id` FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n               LEFT JOIN `glpi_plugin_monitoring_services`\n                  ON `plugin_monitoring_componentscatalogs_hosts_id` =\n                     `glpi_plugin_monitoring_componentscatalogs_hosts`.`id`\n            WHERE `plugin_monitoring_componentscalalog_id`='" . $pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"] . "'\n               AND `itemtype`='NetworkEquipment'\n               AND `networkports_id`!='0'\n               AND `is_static`='0'";
         $result = $DB->query($queryd);
         while ($data = $DB->fetch_array($result)) {
             $devicesnetworkport_present[$data['id']] = 1;
         }
         $data = Search::prepareDatasForSearch($itemtype, $params);
         Search::constructSQL($data);
         $DBread = DBConnection::getReadConnection();
         $DBread->query("SET SESSION group_concat_max_len = 16384;");
         $result = $DBread->query($data['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;");
                     $result = $DBread->query($data['sql']['search']);
                 }
             }
         }
         while ($data = $DB->fetch_array($result)) {
             $networkports_id = 0;
             $itemtype_device = $pmCc_Rule->fields['itemtype'];
             $items_id_device = $data['id'];
             if ($itemtype_device == 'PluginMonitoringNetworkport') {
                 $pmNetworkport = new PluginMonitoringNetworkport();
                 $pmNetworkport->getFromDB($data['id']);
                 $itemtype_device = $pmNetworkport->fields['itemtype'];
                 $items_id_device = $pmNetworkport->fields['items_id'];
                 $networkports_id = $pmNetworkport->fields['networkports_id'];
                 $networkPort = new NetworkPort();
                 if ($networkPort->getFromDB($networkports_id)) {
                     $querynet = "SELECT `itemtype`, `items_id`,\n                        `glpi_plugin_monitoring_services`.`id`\n                        FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n                     LEFT JOIN `glpi_plugin_monitoring_services`\n                        ON `plugin_monitoring_componentscatalogs_hosts_id` =\n                           `glpi_plugin_monitoring_componentscatalogs_hosts`.`id`\n                     WHERE `plugin_monitoring_componentscalalog_id`='" . $pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"] . "'\n                        AND `itemtype`='" . $itemtype_device . "'\n                        AND `items_id`='" . $items_id_device . "'\n                        AND `networkports_id`='" . $networkports_id . "'\n                           LIMIT 1";
                     $resultnet = $DB->query($querynet);
                     if ($DB->numrows($resultnet) == 0) {
                         $input = array();
                         $input['plugin_monitoring_componentscalalog_id'] = $pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"];
                         $input['is_static'] = '0';
                         $input['items_id'] = $items_id_device;
                         $input['itemtype'] = $itemtype_device;
                         $input['networkports_id'] = $networkports_id;
                         $componentscatalogs_hosts_id = $pmComponentscatalog_Host->add($input);
                     } else {
                         $data2 = $DB->fetch_assoc($resultnet);
                         // modify entity of services (if entity of device is changed)
                         $itemtype = $data2['itemtype'];
                         $item = new $itemtype();
                         $item->getFromDB($data2['items_id']);
                         $queryu = "UPDATE `glpi_plugin_monitoring_services`\n                           SET `entities_id`='" . $item->fields['entities_id'] . "'\n                              WHERE `id`='" . $data2['id'] . "'";
                         $DB->query($queryu);
                         unset($devicesnetworkport_present[$data2['id']]);
                     }
                 } else {
                     $pmNetworkport->delete($pmNetworkport->fields);
                 }
                 // Reload current entity
                 Session::changeActiveEntities($default_entity, $entities_isrecursive);
             } else {
                 $queryh = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n                  WHERE `plugin_monitoring_componentscalalog_id`='" . $pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"] . "'\n                     AND `itemtype`='" . $itemtype_device . "'\n                     AND `items_id`='" . $items_id_device . "'\n                        LIMIT 1";
                 $resulth = $DB->query($queryh);
                 if ($DB->numrows($resulth) == '0') {
                     $input = array();
                     $input['plugin_monitoring_componentscalalog_id'] = $pmCc_Rule->fields["plugin_monitoring_componentscalalog_id"];
                     $input['is_static'] = '0';
                     $input['items_id'] = $items_id_device;
                     $input['itemtype'] = $itemtype_device;
                     $componentscatalogs_hosts_id = $pmComponentscatalog_Host->add($input);
                 } else {
                     $data2 = $DB->fetch_assoc($resulth);
                     // modify entity of services (if entity of device is changed)
                     $itemtype = $data2['itemtype'];
                     $item = new $itemtype();
                     $item->getFromDB($data2['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);
                     unset($devices_present[$data2['id']]);
                 }
             }
         }
         // Reload current entity
         Session::changeActiveEntities($default_entity, $entities_isrecursive);
         foreach ($devicesnetworkport_present as $id => $num) {
             $_SESSION['plugin_monitoring_hosts']['itemtype'] = $itemtype_device;
             $_SESSION['plugin_monitoring_hosts']['items_id'] = $items_id_device;
             $pmService->delete(array('id' => $id));
         }
     } else {
         // Purge
         $queryd = "SELECT * FROM `glpi_plugin_monitoring_componentscatalogs_hosts`\n            WHERE `plugin_monitoring_componentscalalog_id`='" . $parm->fields["plugin_monitoring_componentscalalog_id"] . "'\n               AND `itemtype`='" . $parm->fields['itemtype'] . "'\n               AND `is_static`='0'";
         $result = $DB->query($queryd);
         while ($data = $DB->fetch_array($result)) {
             $devices_present[$data['id']] = 1;
         }
     }
     foreach ($devices_present as $id => $num) {
         $pmComponentscatalog_Host->delete(array('id' => $id));
     }
     return true;
 }