function updateGenericServicesCatalogs($action = 'update')
 {
     global $DB;
     $entity = new Entity();
     $pmServicescatalog = new PluginMonitoringServicescatalog();
     $pmBusinessrulegroup = new PluginMonitoringBusinessrulegroup();
     $pmBusinessrulecomponent = new PluginMonitoringBusinessrule_component();
     $existingSCs = array();
     // Find existing instances of generic services catalog ...
     $a_SCs = $this->find("`name` LIKE '" . $this->getName() . "%'");
     foreach ($a_SCs as $a_SC) {
         // Toolbox::logInFile("pm", "SC : ".$a_SC['id'].", name : ".$a_SC['name'].", generic : ".$a_SC['is_generic']."\n");
         if ($a_SC['name'] == $this->getField('name')) {
             continue;
         }
         $existingSCs[$a_SC['name']] = $a_SC;
     }
     if ($action == 'delete') {
         foreach ($existingSCs as $name => $a_SC) {
             $pmServicescatalog->getFromDB($a_SC['id']);
             $pmServicescatalog->delete($pmServicescatalog->fields);
             // Toolbox::logInFile("pm", "Deleted : ".$a_SC['name']."\n");
             $pmBusinessrulecomponent = new PluginMonitoringBusinessrule_component();
             $pmBusinessrule = new PluginMonitoringBusinessrule();
             // Get business rules groups ...
             $a_BRgroups = $pmBusinessrulegroup->find("`plugin_monitoring_servicescatalogs_id`='" . $a_SC['id'] . "'");
             // Delete business groups components
             foreach ($a_BRgroups as $a_BRgroup) {
                 // Toolbox::logInFile("pm", "a_BRgroup : ".$a_BRgroup['id']."\n");
                 $a_brcomponents = $pmBusinessrulecomponent->find("`plugin_monitoring_businessrulegroups_id`='" . $a_BRgroup['id'] . "'");
                 foreach ($a_brcomponents as $a_brcomponent) {
                     // Toolbox::logInFile("pm", "a_brcomponent : ".$a_brcomponent['id']."\n");
                     $pmBusinessrulecomponent->getFromDB($a_brcomponent['id']);
                     $pmBusinessrulecomponent->delete($pmBusinessrulecomponent->fields);
                 }
             }
             // Delete business groups rules
             foreach ($a_BRgroups as $a_BRgroup) {
                 $a_brs = $pmBusinessrule->find("`plugin_monitoring_businessrulegroups_id`='" . $a_BRgroup['id'] . "'");
                 foreach ($a_brs as $a_br) {
                     // Toolbox::logInFile("pm", "a_br : ".$a_br['id']."\n");
                     $pmBusinessrulecomponent->getFromDB($a_brcomponent['id']);
                     $pmBusinessrulecomponent->delete($pmBusinessrulecomponent->fields);
                 }
             }
         }
         return;
     } else {
         foreach ($existingSCs as $name => $a_SC) {
             $pmServicescatalog->getFromDB($a_SC['id']);
             $pmBusinessrulegroup = new PluginMonitoringBusinessrulegroup();
             $pmBusinessrulecomponent = new PluginMonitoringBusinessrule_component();
             $pmBusinessrule = new PluginMonitoringBusinessrule();
             // Get business rules groups ...
             $a_BRgroups = $pmBusinessrulegroup->find("`plugin_monitoring_servicescatalogs_id`='" . $a_SC['id'] . "'");
             // Delete business groups components
             foreach ($a_BRgroups as $a_BRgroup) {
                 // Toolbox::logInFile("pm", "a_BRgroup : ".$a_BRgroup['id']."\n");
                 $a_brcomponents = $pmBusinessrulecomponent->find("`plugin_monitoring_businessrulegroups_id`='" . $a_BRgroup['id'] . "'");
                 foreach ($a_brcomponents as $a_brcomponent) {
                     // Toolbox::logInFile("pm", "a_brcomponent : ".$a_brcomponent['id']."\n");
                     $pmBusinessrulecomponent->getFromDB($a_brcomponent['id']);
                     $pmBusinessrulecomponent->delete($pmBusinessrulecomponent->fields);
                 }
             }
             // Delete business groups rules
             foreach ($a_BRgroups as $a_BRgroup) {
                 $a_brs = $pmBusinessrule->find("`plugin_monitoring_businessrulegroups_id`='" . $a_BRgroup['id'] . "'");
                 foreach ($a_brs as $a_br) {
                     // Toolbox::logInFile("pm", "a_br : ".$a_br['id']."\n");
                     $pmBusinessrulecomponent->getFromDB($a_brcomponent['id']);
                     $pmBusinessrulecomponent->delete($pmBusinessrulecomponent->fields);
                 }
             }
         }
     }
     // Find entities concerned ...
     $a_entitiesServices = $this->getGenericServicesEntities();
     foreach ($a_entitiesServices as $idEntity => $a_entityServices) {
         // New entity ... so it must exist a derivated SC !
         $entity->getFromDB($idEntity);
         // Toolbox::logInFile("pm", "Found entity : ".$idEntity." / ".$entity->getName()."\n");
         $scName = $this->getName() . " - " . $entity->getName();
         if (isset($existingSCs[$scName])) {
             // Update SC
             $pmServicescatalog->getFromDB($existingSCs[$scName]['id']);
             $pmServicescatalog->fields = $this->fields;
             unset($pmServicescatalog->fields['id']);
             $pmServicescatalog->fields['id'] = $existingSCs[$scName]['id'];
             $pmServicescatalog->fields['entities_id'] = $idEntity;
             $pmServicescatalog->fields['is_generic'] = 0;
             $pmServicescatalog->fields['name'] = $DB->escape($scName);
             $pmServicescatalog->update($pmServicescatalog->fields);
             /*            // Finish updating if needed ...
                         $a_BRgroups = $pmBusinessrulegroup->find("`plugin_monitoring_servicescatalogs_id`='".$this->fields['id']."'");
                         foreach ($a_BRgroups as $a_BRgroup) {
                            $pmBusinessrulegroup = $pmBusinessrulegroup->find("`plugin_monitoring_servicescatalogs_id`='".$a_SC['id']."' AND `name`='".$a_BRgroup['name']."'");
                            $pmBusinessrulegroup->fields = $a_BRgroup->fields;
                            unset($pmBusinessrulegroup->fields['id']);
                            $pmBusinessrulegroup->fields['plugin_monitoring_servicescatalogs_id'] = $pmServicescatalog->fields['id'];
                            $pmBusinessrulegroup->update($pmBusinessrulegroup->fields);
                         }
             */
             // Toolbox::logInFile("pm", "Updated : ".$scName."\n");
         } else {
             // Add SC
             $pmServicescatalog = new PluginMonitoringServicescatalog();
             $pmServicescatalog->getEmpty();
             $pmServicescatalog->fields = $this->fields;
             unset($pmServicescatalog->fields['id']);
             $pmServicescatalog->fields['entities_id'] = $idEntity;
             $pmServicescatalog->fields['is_recursive'] = 0;
             $pmServicescatalog->fields['is_generic'] = 0;
             $pmServicescatalog->fields['name'] = $DB->escape($scName);
             $pmServicescatalog->fields['plugin_monitoring_servicescatalogs_id'] = $this->fields['id'];
             $pmServicescatalog->add($pmServicescatalog->fields);
             /*
                         // Finish updating if needed ...
                         $a_BRgroups = $pmBusinessrulegroup->find("`plugin_monitoring_servicescatalogs_id`='".$this->fields['id']."'");
                         foreach ($a_BRgroups as $a_BRgroup) {
                            $ref = new PluginMonitoringBusinessrulegroup();
                            $ref->getFromDB($a_BRgroup['id']);
                            $pmBusinessrulegroup = new PluginMonitoringBusinessrulegroup();
                            $pmBusinessrulegroup->getEmpty();
                            $pmBusinessrulegroup->fields = $ref->fields;
                            unset($pmBusinessrulegroup->fields['id']);
                            $pmBusinessrulegroup->fields['plugin_monitoring_servicescatalogs_id'] = $pmServicescatalog->fields['id'];
                            $pmBusinessrulegroup->add($pmBusinessrulegroup->fields);
             
                            $a_brcomponents = $pmBusinessrulecomponent->find("`plugin_monitoring_businessrulegroups_id`='".$a_BRgroup['id']."'");
                            foreach ($a_brcomponents as $a_brcomponent) {
                               $ref = new PluginMonitoringBusinessrule_component();
                               $ref->getFromDB($a_brcomponent['id']);
                               $pmBusinessrule_component = new PluginMonitoringBusinessrule_component();
                               $pmBusinessrule_component->getEmpty();
                               $pmBusinessrule_component->fields = $ref->fields;
                               unset($pmBusinessrule_component->fields['id']);
                               $pmBusinessrule_component->fields['plugin_monitoring_businessrulegroups_id'] = $pmBusinessrulegroup->fields['id'];
                               $pmBusinessrule_component->add($pmBusinessrule_component->fields);
                            }
                         }
             */
             // Toolbox::logInFile("pm", "Added : ".$scName."\n");
         }
     }
 }
 function showForm($items_id, $servicescatalogs_id, $options = array())
 {
     global $CFG_GLPI;
     if ($items_id != '') {
         $this->getFromDB($items_id);
     } else {
         $this->getEmpty();
     }
     $this->showFormHeader($options);
     $pmBusinessrule_component = new PluginMonitoringBusinessrule_component();
     $pmBusinessrule_component->replayDynamicServices($items_id);
     $pmSC = new PluginMonitoringServicescatalog();
     $pmSC->getFromDB($servicescatalogs_id);
     $rand = mt_rand();
     echo "<tr class='tab_bg_1'>";
     echo "<td>";
     echo "<input type='hidden' name='plugin_monitoring_servicescatalogs_id' value='" . $servicescatalogs_id . "'/>";
     echo __('Name') . "&nbsp;:";
     echo "</td>";
     echo "<td>";
     echo "<input type='text' name='name' value='" . $this->fields["name"] . "' size='30'/>";
     echo "</td>";
     if ($items_id != '') {
         if (Session::haveRight("plugin_monitoring_servicescatalog", CREATE)) {
             echo "<th colspan='2' width='60%'>";
             echo __('Resources', 'monitoring');
             echo "&nbsp;";
             echo "<img onClick=\"Ext.get('ressources" . $rand . "').setDisplayed('block')\"\n                       title=\"" . __('add') . "\" alt=\"" . __('add') . "\"\n                       class='pointer'  src='" . $CFG_GLPI["root_doc"] . "/pics/add_dropdown.png'>";
             echo "</th>";
         }
     }
     echo "</tr>";
     echo "<tr>";
     echo "<td valign='top'>";
     echo __('Logical operator') . "&nbsp;:";
     echo "</td>";
     echo "<td valign='top'>";
     $first_operator = array();
     $first_operator['and'] = "and";
     $first_operator['or'] = "or";
     $first_operator['2 of:'] = __('2 of', 'monitoring');
     $first_operator['3 of:'] = __('3 of', 'monitoring');
     $first_operator['4 of:'] = __('4 of', 'monitoring');
     $first_operator['5 of:'] = __('5 of', 'monitoring');
     $first_operator['6 of:'] = __('6 of', 'monitoring');
     $first_operator['7 of:'] = __('7 of', 'monitoring');
     $first_operator['8 of:'] = __('8 of', 'monitoring');
     $first_operator['9 of:'] = __('9 of', 'monitoring');
     $first_operator['10 of:'] = __('10 of', 'monitoring');
     Dropdown::showFromArray('operator', $first_operator, array("value" => $this->fields['operator']));
     echo "</td>";
     if ($items_id != '') {
         echo "<td colspan='2'>";
         if (Session::haveRight("plugin_monitoring_servicescatalog", CREATE)) {
             // ** Dropdown to display
             echo "<div style='display:none' id='ressources" . $rand . "' >";
             // Static (a service for an host)
             echo "<table>";
             echo "<tr class='tab_bg_1'>";
             echo "<td>";
             echo "<form name='form' method='post' action='" . $CFG_GLPI['root_doc'] . "/plugins/monitoring/front/businessrule.form.php'>";
             echo "<input type='hidden' name='plugin_monitoring_businessrulegroups_id' value='" . $items_id . "' />";
             echo "<input type='hidden' name='is_generic' value='" . $pmSC->getField('is_generic') . "' />";
             PluginMonitoringBusinessrule::dropdownService(0, array('name' => 'type'));
             echo "<input type='submit' name='add' value=\"" . __('Add') . "\" class='submit'>";
             Html::closeForm();
             echo "</td>";
             echo "</tr>";
             echo "</table>";
             echo "<hr>";
             // Dynamique (all service of component of component catalog)
             echo "<table>";
             echo "<tr class='tab_bg_1'>";
             echo "<td><strong>";
             echo __('Components catalog', 'monitoring');
             echo ' + ';
             echo __('Component', 'monitoring');
             echo " (" . __('Dynamic', 'monitoring') . ")";
             echo " :</strong></td>";
             echo "</tr>";
             echo "<tr class='tab_bg_1'>";
             echo "<td>";
             echo "<form name='form' method='post' action='" . $CFG_GLPI['root_doc'] . "/plugins/monitoring/front/businessrule_component.form.php'>";
             echo "<input type='hidden' name='plugin_monitoring_businessrulegroups_id' value='" . $items_id . "' />";
             $options = array('toupdate' => array('value_fieldname' => 'plugin_monitoring_componentscalalog_id', 'to_update' => "componentdropdown" . $rand, 'url' => $CFG_GLPI["root_doc"] . "/plugins/monitoring/ajax/dropdownComponent.php", 'moreparams' => array()));
             Dropdown::show('PluginMonitoringComponentscatalog', $options);
             echo '<div id="componentdropdown' . $rand . '"></div>';
             //PluginMonitoringBusinessrule::dropdownService(0, array('name' => 'type'));
             echo "<input type='submit' name='add' value=\"" . __('Add') . "\" class='submit'>";
             Html::closeForm();
             echo "</td>";
             echo "</tr>";
             echo "</table>";
             echo "<hr>";
             echo "</div>";
         }
         $pmBusinessrule = new PluginMonitoringBusinessrule();
         $pmService = new PluginMonitoringService();
         $a_services = $pmBusinessrule->find("`plugin_monitoring_businessrulegroups_id`='" . $items_id . "'" . " AND `is_dynamic`='0'");
         if (count($a_services) > 0) {
             echo "<strong>" . __('Static hosts', 'monitoring') . " :</strong>";
             echo "<table width='100%'>";
             foreach ($a_services as $gdata) {
                 if ($pmService->getFromDB($gdata['plugin_monitoring_services_id'])) {
                     $shortstate = $pmService->getShortState();
                     echo "<tr class='tab_bg_1'>";
                     echo "<td>";
                     echo "<img src='" . $pmService->getShortState(array('image' => '32')) . "'/>";
                     echo "</td>";
                     echo "<td>";
                     echo $pmService->getLink();
                     echo "</td>";
                     echo "<td>";
                     echo "<input type='submit' name='deletebusinessrules-" . $gdata['id'] . "' value=\"" . _sx('button', 'Delete permanently') . "\" class='submit'>";
                     echo "</td>";
                     echo "</tr>";
                 } else {
                     // resource deleted
                     echo "<tr class='tab_bg_1'>";
                     echo "<td colspan='2' bgcolor='#ff0000'>";
                     echo __('Resource deleted', 'monitoring');
                     echo "</td>";
                     echo "<td>";
                     echo "<input type='submit' name='deletebusinessrules-" . $gdata['id'] . "' value=\"" . __('Clean') . "\" class='submit'>";
                     echo "</td>";
                     echo "</tr>";
                 }
             }
             echo "</table>";
             echo "<hr/>";
         }
         $a_br_components = $pmBusinessrule_component->find("`plugin_monitoring_businessrulegroups_id`='" . $items_id . "'");
         $pmComponentscatalog_Component = new PluginMonitoringComponentscatalog_Component();
         $pmComponentscatalog = new PluginMonitoringComponentscatalog();
         $pmComponent = new PluginMonitoringComponent();
         if (count($a_br_components) > 0) {
             echo "<strong>" . __('Dynamic hosts', 'monitoring') . " :</strong>";
             echo "<table width='100%'>";
             foreach ($a_br_components as $a_br_component) {
                 echo "<tr class='tab_bg_1'>";
                 echo "<td>";
                 $pmComponentscatalog_Component->getFromDB($a_br_component['plugin_monitoring_componentscatalogs_components_id']);
                 $pmComponentscatalog->getFromDB($pmComponentscatalog_Component->fields['plugin_monitoring_componentscalalog_id']);
                 echo $pmComponentscatalog->getLink();
                 echo ' > ';
                 $pmComponent->getFromDB($pmComponentscatalog_Component->fields['plugin_monitoring_components_id']);
                 echo $pmComponent->getLink();
                 echo "</td>";
                 echo "<td>";
                 echo "<input type='submit' name='deletebrcomponents-" . $a_br_component['id'] . "' value=\"" . _sx('button', 'Delete permanently') . "\" class='submit'>";
                 echo "</td>";
                 echo "</tr>";
             }
             echo "</table>";
         }
         if (!($pmSC->fields['is_generic'] && $pmSC->fields['is_recursive'])) {
             echo "<table width='100%'>";
             $a_services = $pmBusinessrule->find("`plugin_monitoring_businessrulegroups_id`='" . $items_id . "'" . " AND `is_dynamic`='1'");
             foreach ($a_services as $gdata) {
                 $pmService->getFromDB($gdata["plugin_monitoring_services_id"]);
                 $shortstate = $pmService->getShortState();
                 echo "<tr class='tab_bg_1'>";
                 echo "<td width='130'>&nbsp;";
                 echo "</td>";
                 echo "<td>";
                 echo "<img src='" . $pmService->getShortState(array('image' => '32')) . "'/>";
                 echo "</td>";
                 echo "<td>";
                 $pmComponentscatalog_Host = new PluginMonitoringComponentscatalog_Host();
                 $pmComponentscatalog_Host->getFromDB($pmService->fields['plugin_monitoring_componentscatalogs_hosts_id']);
                 echo $pmService->getLink();
                 echo "</td>";
                 echo "</tr>";
             }
             echo "</table>";
         } else {
             echo "<strong>" . __('Dynamic hosts list will be defined during Shinken configuration build.', 'monitoring') . "</strong>";
         }
     }
     echo "</td>";
     echo "</tr>";
     $this->showFormButtons($options);
     return true;
 }