From 434f9d5dc19ee5c7e8e492ddcbe5f1c47d24b665 Mon Sep 17 00:00:00 2001
From: mdtrooper <tres.14159@gmail.com>
Date: Wed, 29 Sep 2010 17:16:34 +0000
Subject: [PATCH] 2010-09-29  Miguel de Dios  <miguel.dedios@artica.es>

	* include/functions_groups.php: added first version of file with the
	function "checkUsedGroup".

	*include/Image/Canvas.php, include/Image/Canvas/GD.php,
	include/functions.php, mobile/operation/agents/view_agents.php: cleaned
	source code style.

	* godmode/groups/group_list.php: fixed the check if the group to delete is
	empty.

	Fixes: #3074223



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@3320 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
---
 pandora_console/ChangeLog                     |  14 ++
 pandora_console/godmode/groups/group_list.php |  26 ++--
 pandora_console/include/Image/Canvas.php      |   2 +-
 pandora_console/include/Image/Canvas/GD.php   |   2 +-
 pandora_console/include/functions.php         |   1 +
 pandora_console/include/functions_groups.php  | 121 ++++++++++++++++++
 .../mobile/operation/agents/view_agents.php   |   1 -
 7 files changed, 155 insertions(+), 12 deletions(-)
 create mode 100644 pandora_console/include/functions_groups.php

diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog
index acf5c1805c..48c7d7fe83 100644
--- a/pandora_console/ChangeLog
+++ b/pandora_console/ChangeLog
@@ -1,3 +1,17 @@
+2010-09-29  Miguel de Dios  <miguel.dedios@artica.es>
+
+	* include/functions_groups.php: added first version of file with the
+	function "checkUsedGroup".
+	
+	*include/Image/Canvas.php, include/Image/Canvas/GD.php,
+	include/functions.php, mobile/operation/agents/view_agents.php: cleaned
+	source code style.
+
+	* godmode/groups/group_list.php: fixed the check if the group to delete is
+	empty.
+	
+	Fixes: #3074223
+
 2010-09-29  Sergio Martin <sergio.martin@artica.es>
 
 	* include/functions_db.php
diff --git a/pandora_console/godmode/groups/group_list.php b/pandora_console/godmode/groups/group_list.php
index 5f8440e921..291d2e29b9 100644
--- a/pandora_console/godmode/groups/group_list.php
+++ b/pandora_console/godmode/groups/group_list.php
@@ -26,6 +26,8 @@ if (! give_acl($config['id_user'], 0, "PM")) {
 	return;
 }
 
+require_once("include/functions_groups.php");
+
 if (is_ajax ()) {
 	$get_group_json = (bool) get_parameter ('get_group_json');
 	$get_group_agents = (bool) get_parameter ('get_group_agents');
@@ -139,10 +141,12 @@ if ($update_group) {
 if ($delete_group) {
 	$id_group = (int) get_parameter ('id_group');
 	
-	$sql = sprintf ('SELECT * FROM tagente WHERE id_grupo = %d', $id_group);
-	$agent = process_sql ($sql);
-			
-	if(!$agent){
+//	$sql = sprintf ('SELECT * FROM tagente WHERE id_grupo = %d', $id_group);
+//	$agent = process_sql ($sql);
+
+	$usedGroup = checkUsedGroup($id_group);
+	
+	if (!$usedGroup['return']) {
 		
 		$group = get_db_row_filter('tgrupo', array('id_grupo' => $id_group));
 		
@@ -154,14 +158,18 @@ if ($delete_group) {
 		$sql = sprintf ('DELETE FROM tgrupo WHERE id_grupo = %d', $id_group);
 		$result = process_sql ($sql);
 	}
-	else
-		echo "<h3 class='error'>".__('The group is not empty.')."</h3>";
+	else {
+		echo "<h3 class='error'>" .
+			sprintf(__('The group is not empty. It is use in %s.'), implode(', ', $usedGroup['tables'])) . "</h3>";
+	}
 
 	
-	if (!$result || $agent )
-		echo "<h3 class='error'>".__('There was a problem deleting group')."</h3>"; 
-	else
+	if ((!$result) || (!$usedGroup['return'])) {
+		echo "<h3 class='error'>".__('There was a problem deleting group')."</h3>";
+	} 
+	else {
 		echo "<h3 class='suc'>".__('Group successfully deleted')."</h3>";
+	}
 		 
 }
 
diff --git a/pandora_console/include/Image/Canvas.php b/pandora_console/include/Image/Canvas.php
index cd88e9699f..5092bb2c8c 100644
--- a/pandora_console/include/Image/Canvas.php
+++ b/pandora_console/include/Image/Canvas.php
@@ -409,7 +409,7 @@ class Image_Canvas
         }    
         if (isset($params['end1'])) {
             $angle = Image_Canvas_Tool::getAngle($x0, $y0, $x1, $y1);
-            //print "<pre>"; var_dump($params, $angle); print "</pre>";
+            
             $this->drawEnd(
                 array(
                     'end' => $params['end1'], 
diff --git a/pandora_console/include/Image/Canvas/GD.php b/pandora_console/include/Image/Canvas/GD.php
index 9f78792062..83df6ecf12 100644
--- a/pandora_console/include/Image/Canvas/GD.php
+++ b/pandora_console/include/Image/Canvas/GD.php
@@ -745,7 +745,7 @@ ialias'))) {
         $x = $this->_getX($params['x']);
         $y = $this->_getY($params['y']);
         $size = $params['size'];
-        //var_dump($params);
+        
         $angle = deg2rad((isset($params['angle']) ? $params['angle'] : 0));
         $pi2 = pi() / 2;
         switch ($params['end']) {
diff --git a/pandora_console/include/functions.php b/pandora_console/include/functions.php
index 08c7977d70..884f19e9f8 100644
--- a/pandora_console/include/functions.php
+++ b/pandora_console/include/functions.php
@@ -917,6 +917,7 @@ function enterprise_include ($filename) {
 
 function enterprise_include_once ($filename) {
 	global $config;
+	
 	// Load enterprise extensions
 	$filepath = realpath ($config["homedir"].'/'.ENTERPRISE_DIR.'/'.$filename);
 	if ($filepath === false)
diff --git a/pandora_console/include/functions_groups.php b/pandora_console/include/functions_groups.php
new file mode 100644
index 0000000000..3f1757e0bb
--- /dev/null
+++ b/pandora_console/include/functions_groups.php
@@ -0,0 +1,121 @@
+<?php
+
+// Pandora FMS - http://pandorafms.com
+// ==================================================
+// Copyright (c) 2005-2010 Artica Soluciones Tecnologicas
+// Please see http://pandorafms.org for full contribution list
+
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the  GNU Lesser General Public License
+// as published by the Free Software Foundation; version 2
+
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+
+/**
+ * Check if the group is in use in the Pandora DB. 
+ * 
+ * @param integer $idGroup The id of group.
+ * 
+ * @return bool Return false if the group is unused in the Pandora, else true.
+ */
+function checkUsedGroup($idGroup) {
+	$return = array();
+	$return['return'] = false;
+	$return['tables'] = array();
+	
+	$numRows = get_db_num_rows('SELECT * FROM tagente WHERE id_grupo = ' . $idGroup . ';');
+	if ($numRows > 0) {
+		$return['return'] = true;
+		$return['tables'][] = __('Agents'); 
+	}
+	
+	$numRows = get_db_num_rows('SELECT * FROM talert_actions WHERE id_group = ' . $idGroup . ';');
+	if ($numRows > 0) {
+		$return['return'] = true;
+		$return['tables'][] = __('Alert Actions'); 
+	}
+	
+	$numRows = get_db_num_rows('SELECT * FROM talert_templates WHERE id_group = ' . $idGroup . ';');
+	if ($numRows > 0) {
+		$return['return'] = true;
+		$return['tables'][] = __('Alert Templates'); 
+	}
+	
+	$numRows = get_db_num_rows('SELECT * FROM trecon_task WHERE id_group = ' . $idGroup . ';');
+	if ($numRows > 0) {
+		$return['return'] = true;
+		$return['tables'][] = __('Recon task'); 
+	}
+	
+	$numRows = get_db_num_rows('SELECT * FROM tgraph WHERE id_group = ' . $idGroup . ';');
+	if ($numRows > 0) {
+		$return['return'] = true;
+		$return['tables'][] = __('Graphs'); 
+	}
+	
+	$numRows = get_db_num_rows('SELECT * FROM treport WHERE id_group = ' . $idGroup . ';');
+	if ($numRows > 0) {
+		$return['return'] = true;
+		$return['tables'][] = __('Reports'); 
+	}
+	
+	$numRows = get_db_num_rows('SELECT * FROM tlayout WHERE id_group = ' . $idGroup . ';');
+	if ($numRows > 0) {
+		$return['return'] = true;
+		$return['tables'][] = __('Layout visual console'); 
+	}
+	
+	$numRows = get_db_num_rows('SELECT * FROM tplanned_downtime WHERE id_group = ' . $idGroup . ';');
+	if ($numRows > 0) {
+		$return['return'] = true;
+		$return['tables'][] = __('Plannet down time'); 
+	}
+	
+	$numRows = get_db_num_rows('SELECT * FROM tgraph WHERE id_group = ' . $idGroup . ';');
+	if ($numRows > 0) {
+		$return['return'] = true;
+		$return['tables'][] = __('Graphs'); 
+	}
+	
+	$numRows = get_db_num_rows('SELECT * FROM tgis_map WHERE group_id = ' . $idGroup . ';');
+	if ($numRows > 0) {
+		$return['return'] = true;
+		$return['tables'][] = __('GIS maps'); 
+	}
+	
+	$numRows = get_db_num_rows('SELECT * FROM tgis_map_connection WHERE group_id = ' . $idGroup . ';');
+	if ($numRows > 0) {
+		$return['return'] = true;
+		$return['tables'][] = __('GIS connections'); 
+	}
+	
+	$numRows = get_db_num_rows('SELECT * FROM tgis_map_layer WHERE tgrupo_id_grupo = ' . $idGroup . ';');
+	if ($numRows > 0) {
+		$return['return'] = true;
+		$return['tables'][] = __('GIS map layers'); 
+	}
+	
+	$numRows = get_db_num_rows('SELECT * FROM tnetwork_map WHERE id_group = ' . $idGroup . ';');
+	if ($numRows > 0) {
+		$return['return'] = true;
+		$return['tables'][] = __('Network maps'); 
+	}
+	
+	$hookEnterprise = enterprise_include_once('include/functions_groups.php');
+	if ($hookEnterprise !== ENTERPRISE_NOT_HOOK) {
+		$returnEnterprise = enterprise_hook('checkUsedGroupEnterprise', array($idGroup));
+		
+		if ($returnEnterprise['return']) {
+			$return['return'] = true;
+			$return['tables'] = array_merge($return['tables'], $returnEnterprise['tables']);
+		}
+	}
+	
+	return $return;
+}
+
+?>
\ No newline at end of file
diff --git a/pandora_console/mobile/operation/agents/view_agents.php b/pandora_console/mobile/operation/agents/view_agents.php
index a2c57a3682..ce58941c2b 100644
--- a/pandora_console/mobile/operation/agents/view_agents.php
+++ b/pandora_console/mobile/operation/agents/view_agents.php
@@ -524,7 +524,6 @@ class viewGraph {
 		
 			foreach($columns as $col => $attr){
 				$data[] = $attr[1] ($row[$attr[0]]);
-				//debugPrint( "\$data[] = ".$attr[1]." (".$row[$attr[0]].");");
 			}
 		
 			array_push ($table->data, $data);