<?php
	function ReportError($e, $mysqliMessage = ""){
		echo "<div style='background-color:#CCCCCC;
											border:1px dotted red;
											width:300px;
											height:380px;
											margin:0;
											padding:5px;
											word-wrap: break-word;
											display:block;
											position:fixed;
											top:15%;
											left:20%;
											font-family:Verdana,Geneva,sans-serif;
											font-size:70%;
											font-weight:bold;'>";
			echo "Error: ".$e->getMessage()."<br />";
			echo "File: ".basename($e->getFile())."<br />";
			echo "Line: ".$e->getLIne()."<br />";
			if ($mysqliMessage != "") {
				echo mysqli_connect_error();
			}
		echo "</div>\n";
	}
	
	chdir('../');
	
	include('includes/include_all.inc.php');
	
	//Create User object
	$obj_users = new Users();
	
	if(!$obj_users->checkUserLoginOrNot()){
	    header('Location: '.BASEURL.'login.php');
	} 
	
	if($_SESSION['user_type']!='super' && $_SESSION['user_type']!='admin'){ 
	    session_destroy();
	    header('Location: '.BASEURL.'login.php');
	}
	

	if(isset($_GET["id"])) {
	     if(!empty($_GET["id"])) {
		  $groupId = (int)$_GET['id'];
	     }
	} else {
	      header('Location: '.BASEURL.'admin/index.php');
	      exit;
	}
	
	$groupname = $_GET["groupname"];
		
	$deleteQuery = "DELETE
				    FROM wp_groups
				    WHERE
				    id = $groupId";
				    //echo $deleteQuery; exit;
	
	//try{
	//	$result = mysql_query($deleteQuery);
	//	
	//	if(!$result){
	//		throw new Exception ("Group deletion has failed.");
	//	}
	//}	
	//catch (Exception $e){
	//	ReportError($e);
	//	header('Location: '.BASEURL.'admin/index.php');
	//	exit();
	//}
	
	//:: now delete all the cases that belong to the user.
	//:: cascade delete the cases that belong to the users
	//:: is not apparently working
	
	$getUserIds = "SELECT
				      id
				  FROM wp_users
				  WHERE
				        user_group = '$groupname'";
	
	$result = mysql_query($getUserIds);
	
	if (mysql_num_rows($result) > 0 ) { // do not do anything if there are no users in the group
	
		$userIds = "";
		$first = true;
		//::
		//:: get a comma separated list of userids to delete from cases
		//::
		while ($oneRow = mysql_fetch_assoc($result)) {
		     if ($first) {
			$userIds = "(".$oneRow['id'];
		     } else {
			$userIds .= ", ".$oneRow['id'];
		     }
		}
		$userIds .= ")";
		
		$deleteCasesQuery = "DELETE
						     FROM wp_groupusers_data
						     WHERE
							user_id IN $userIds";
						    echo $deleteCasesQuery; exit;
		
		try{
			$result = mysql_query($deleteCasesQuery);
			
			if(!$result){
				throw new Exception ("Cascased users deletion has failed.");
			}
		}	
		catch (Exception $e){
			ReportError($e);
			header('Location: '.BASEURL.'admin/index.php');
			exit();
		}
		
		//:: now delete all the users that belong to the group.
		//:: cascade delete the users that belong to the group
		//:: is not working in MySQL
		$deleteUsersQuery = "DELETE
						     FROM wp_users
						     WHERE
							user_group = '$groupname'";
						    //echo $deleteQuery; exit;
		
		//try{
		//	$result = mysql_query($deleteUsersQuery);
		//	
		//	if(!$result){
		//		throw new Exception ("Cascased users deletion has failed.");
		//	}
		//}	
		//catch (Exception $e){
		//	ReportError($e);
		//	header('Location: '.BASEURL.'admin/index.php');
		//	exit();
		//}
	}

	header('Location: '.BASEURL.'admin/index.php');
?>


