<?php

class user {

	function addUser($arr)
		{									
			$sql	=	sprintf("INSERT INTO `users` (`firstname`, `lastname`, `email`, `usertypeid`, `username`, `password`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s')",$arr[0], $arr[1], $arr[2], $arr[3], $arr[4], $arr[5]);
								 												
					
			if (mysql_query($sql))
				return true;
			else
				return "Error: " . mysql_error();
			
		}
		
	function addProduct($arr)
		{									
			$sql	=	sprintf("INSERT INTO `product` (`producttitle`, `product_code`, `catid`, `currentstocklevel`, `alertcount`, `product_status_id`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s')", $arr[0], $arr[1], $arr[2], $arr[3], $arr[4], $arr[5]);

			if (mysql_query($sql))
				return true;
			else
				return "Error: " . mysql_error();
			
		}		
	
	function updateUser($arr)
		{							
			$sql	=	sprintf("UPDATE `users` SET `firstname` = '%s', `lastname` = '%s', `email` = '%s', `usertypeid` = '%s', `username` = '%s', `password` = '%s' WHERE `id` = '%s'",$arr[0], $arr[1], $arr[2], $arr[3], $arr[4], $arr[5], $arr[6]);				
														
			if (mysql_query($sql))
				return true;
			else
				return "Error: " . mysql_error();
		}
		
	function updateProduct($arr)
		{										
			$sql	=	sprintf("UPDATE `product` SET `producttitle` = '%s', `product_code` = '%s', `catid` = '%s', `currentstocklevel` = '%s', `alertcount` = '%s', `product_status_id` = '%s' WHERE `id` = '%s'",$arr[0], $arr[1], $arr[2], $arr[3], $arr[4], $arr[5], $arr[6]);				
											
			if (mysql_query($sql))
				return true;
			else
				return "Error: " . mysql_error();
		}		
		
	function deleteUser($uid)
		{
			$user_id = ($uid != '') ? $uid : 0;
			
			$sql = sprintf("delete from users where id = '%s'", $user_id);
			
			if (mysql_query($sql))
				return true;
			else
				return "Error: " . mysql_error();
		}
		
	function deleteProduct($uid)
		{
			$user_id = ($uid != '') ? $uid : 0;

			$sql = sprintf("UPDATE `product` SET `delete` = b'1' WHERE `id` = '%s'", $user_id);
			
			if (mysql_query($sql))
				return true;
			else
				return "Error: " . mysql_error();
		}		
		
	function isUserName($email)
		{
			if ($email == '') return false;
			
			$sql = sprintf("select username from users where username = '%s'", $email);
			$res = mysql_query($sql);
			if (mysql_num_rows($res) > 0)
				return true;
			else
				return false;
		}
		
	function isProductCode($email)
		{
			if ($email == '') return false;
			
			$sql = sprintf("select `product_code` from product where `product_code` = '%s'", $email);
			$res = mysql_query($sql);
			if (mysql_num_rows($res) > 0)
				return true;
			else
				return false;
		}		
		
	function isEmail_admin($email)
		{
			if ($email == '') return false;
			
			$sql = sprintf("select email from  admin where email = '%s'", $email);
			$res = mysql_query($sql);
			if (mysql_num_rows($res) > 0)
				return true;
			else
				return false;
		}		
	
	function getEmail($uid)	
		{
			$user_id = ($uid != '') ? $uid : 0;
			$sql = sprintf("select username from users where user_id = '%s'", $user_id);
			$res = mysql_query($sql);
			$row = mysql_fetch_array($res);
			
			return $row[0];
		}
		
	function getProductCode($uid)	
		{
			$user_id = ($uid != '') ? $uid : 0;
			$sql = sprintf("select `product_code` from product where `id` = '%s'", $user_id);
			$res = mysql_query($sql);
			$row = mysql_fetch_array($res);
			
			return $row[0];
		}		
				
	function getUserInfoById($uid)
		{
			$user_id = ($uid != '') ? $uid : 0;
			
			$sql = sprintf("select * from users where id = '%s'", $user_id);
			
			$res = mysql_query($sql);
			
			return mysql_fetch_array($res);
		}
		
	function getProductInfo($uid)
		{
			$user_id = ($uid != '') ? $uid : 0;
			
			$sql = sprintf("select * from product where id = '%s'", $user_id);
			
			$res = mysql_query($sql);
			
			return mysql_fetch_array($res);
		}		
		
	function getUserInfoById_News($uid)
		{
			$user_id = ($uid != '') ? $uid : 0;
			
			$sql = sprintf("select * from landingpagenews where id = '%s'", $user_id);
			
			$res = mysql_query($sql);
			
			return mysql_fetch_array($res);
		}
	
	function deleteUser_News($uid)
		{
			$user_id = ($uid != '') ? $uid : 0;
			
			$sql = sprintf("delete from landingpagenews where id = '%s'", $user_id);
			
			if (mysql_query($sql))
				return true;
			else
				return "Error: " . mysql_error();
		}	
}

?>