<?php
	date_default_timezone_set('America/New_York');
	class UtilitiesReporting
	{
		var $username;     //Username given on sign-up

		function UtilitiesReporting(){

		}
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		function getClientCities(){
			global $database;
			
			$query = "
				SELECT 
					COUNT(city) AS CityCount, 
					city 
				FROM 
					".TBLCLIENT." 
				GROUP BY city  
				ORDER BY COUNT(city) DESC
				LIMIT 0, 20
			";
			
			$result = $database->query($query);
			$numRows = mysql_numrows($result);
			
			if(!$result || $numRows < 0){
				if(DEBUG == true){
					printf("Error in query: %s", $query);
				}
			}
			
			echo "
				<table class='full'>
					<tbody>";
			for($i=0; $i<$numRows; $i++){
				$city = mysql_result($result, $i, "city");
				$CityCount = mysql_result($result, $i, "CityCount");
				$CityCount = number_format($CityCount);
				echo "
					<tr> 
						<td>$city</td> 
						<td class='ar'>$CityCount</td> 
					</tr> 
				";
			}//End for loop through result set
			
			echo "
				</tbody>
			</table>";
		}//END getClientCities
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		
		function getTotalClientCount(){
			global $database;
			$city = mysql_real_escape_string($city);
			$query = "
				SELECT 
					COUNT(*) AS ClientCount
				FROM 
					".TBLCLIENT." 
				";
		
			$result = $database->query($query);
			$numRows = mysql_numrows($result);
			
			if(!$result || $numRows < 0){
				if(DEBUG == true){
					printf("Error in query: %s", $query);
				}
			}

			for($i=0; $i<$numRows; $i++){
				$ClientCount = mysql_result($result, $i, "ClientCount");

				return $ClientCount;
			}//End for loop through result set
		}//END getTotalClientCount
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		function getNoCityClients(){
			global $database;
			$city = mysql_real_escape_string($city);
			$query = "
				SELECT 
					COUNT(*) AS EmptyCityCount
				FROM 
					".TBLCLIENT." 
				WHERE city = ''
				";
		
			$result = $database->query($query);
			$numRows = mysql_numrows($result);
			
			if(!$result || $numRows < 0){
				if(DEBUG == true){
					printf("Error in query: %s", $query);
				}
			}

			for($i=0; $i<$numRows; $i++){
				$EmptyCityCount = mysql_result($result, $i, "EmptyCityCount");

				return $EmptyCityCount;
			}//End for loop through result set
		}//END getTotalClientCount
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		function getTotalUsers(){
			global $database;
			$city = mysql_real_escape_string($city);
			$query = "
				SELECT 
					COUNT(*) AS UserCount
				FROM 
					".TBL_USERS;
		
			$result = $database->query($query);
			$numRows = mysql_numrows($result);
			
			if(!$result || $numRows < 0){
				if(DEBUG == true){
					printf("Error in query: %s", $query);
				}
			}

			for($i=0; $i<$numRows; $i++){
				$UserCount = mysql_result($result, $i, "UserCount");

				return $UserCount;
			}//End for loop through result set
		}//END getTotalClientCount
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		function getTotalClients(){
			global $database;
			$city = mysql_real_escape_string($city);
			$query = "
				SELECT 
					COUNT(*) AS ClientCount
				FROM 
					".TBLCLIENT;
		
			$result = $database->query($query);
			$numRows = mysql_numrows($result);
			
			if(!$result || $numRows < 0){
				if(DEBUG == true){
					printf("Error in query: %s", $query);
				}
			}

			for($i=0; $i<$numRows; $i++){
				$ClientCount = mysql_result($result, $i, "ClientCount");

				return number_format($ClientCount);
			}//End for loop through result set
		}//END getTotalClientCount
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		function getTotalActiveUsers(){
			global $database;
			$city = mysql_real_escape_string($city);
			$query = "
				SELECT 
					COUNT(*) AS ActiveUserCount
				FROM 
					".TBL_ACTIVE_USERS;
		
			$result = $database->query($query);
			$numRows = mysql_numrows($result);
			
			if(!$result || $numRows < 0){
				if(DEBUG == true){
					printf("Error in query: %s", $query);
				}
			}

			for($i=0; $i<$numRows; $i++){
				$ActiveUserCount = mysql_result($result, $i, "ActiveUserCount");

				return $ActiveUserCount;
			}//End for loop through result set
		}//END getTotalClientCount
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		function setActivityRecord($insertedClientID, $activityDESC, $now, $activityType, $username){
			global $database;
			
			$insertedClientID 	= mysql_real_escape_string($insertedClientID);
			$activityDESC 		= mysql_real_escape_string($activityDESC);
			$now 				= mysql_real_escape_string($now);
			$activityType 		= mysql_real_escape_string($activityType);
			$username 			= mysql_real_escape_string($username);
			
			$query = "
				INSERT INTO ".TBLCLIENTACTIVITY."
				(
					ClientID, ActivityType, Activity, Type, username, DateModified
				)
				VALUES
				(
					'$insertedClientID', '$activityDESC', '$now', '$activityType', '$username', NOW()
				)
			
			";
			
			$result = $database->query($query);
			
			if(!$result){
				if(DEBUG == true){
					printf("Error in query: %s", $query);
				}
				return;
			}
		}//END setActivityRecord
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		
	};

	$reporting = new UtilitiesReporting;
?>