<?php 
include 'session.php';
extract($_REQUEST);
$act=$action;

if($chkstatus !=null)
	$status =1;
else
	$status =0;

include 'includes/image_thumb.php';


switch($act)
{
	case 'insert':
	
	if(!empty($txtuser_firstname)) {
		$strChk = "select count(user_ID) from a_users where user_email = '$txtuser_email' and IsActive != '2'";		
 		$reslt = $db->get_a_line($strChk);
		if($reslt[0] == 0) {			
			if(isset($_FILES["user_photo"])){
				//validate image file allowed (jpg,png,gif)
				$file_info = getimagesize($_FILES["user_photo"]['tmp_name']);
				$file_mime = explode('/',$file_info['mime']);				
				if(!in_array($file_mime[1],array('jpg','jpeg','gif','png','bmp') ) ){
					echo json_encode(array("rslt"=>"7"));
					exit();
				}
				//image upload path - starts			
				$obj=new Gthumb();			
				$path=$obj->genThumbAdminusersImage('user_photo');							
				//image upload path - ends	
			}
			$str="insert into a_users(user_firstname,user_lastname,user_name,user_email,user_pwd,RoleId,user_photo,IsActive,UserId, createddate)values('".getRealescape($txtuser_firstname)."','".getRealescape($txtuser_lastname)."','".getRealescape($txtuser_email)."','".getRealescape($txtuser_email)."','".md5($txtuser_password)."','".getRealescape($txtRoleId)."','".$path."','".$status."','".$_SESSION["UserId"]."', NOW())";						
			$rslt = $db->insert($str);				
			$log = $db->insert_log("insert","a_users","","User Added Newly","user",$str);
			
			//echo json_encode(array("rslt"=>$rslt)); //success
			echo json_encode(array("rslt"=>"1")); //success
		}
		else {
			 echo json_encode(array("rslt"=>"3")); //same exists
		}
	}
	else {
		echo json_encode(array("rslt"=>"4"));  //no values
	}
	
	break;
	
	
	case 'update':	 	
	//$edit_id
	$today=date("Y-m-d");	
	if(!empty($txtuser_firstname)) {
		$strChk = "select count(user_ID) from a_users where user_email = '$txtuser_email' and IsActive != '2' and user_ID != '".$edit_id."' ";
 		$reslt = $db->get_a_line($strChk);
		if($reslt[0] == 0) {
			$str = "update a_users set user_firstname = '".getRealescape($txtuser_firstname)."',user_lastname='".getRealescape($txtuser_lastname)."', user_email='".getRealescape($txtuser_email)."' , user_name='".getRealescape($txtuser_email)."' , RoleId='".getRealescape($txtRoleId)."' , ";
			if(isset($_FILES["user_photo"])){
				//validate image file allowed (jpg,png,gif)
				$file_info = getimagesize($_FILES["user_photo"]['tmp_name']);
				$file_mime = explode('/',$file_info['mime']);				
				if(!in_array($file_mime[1],array('jpg','jpeg','gif','png','bmp') ) ){
					echo json_encode(array("rslt"=>"7"));
					exit();
				}
				//image upload path - starts			
				$obj=new Gthumb();			
				$path=$obj->genThumbAdminusersImage('user_photo');	
				//image upload path - ends
				$str .= " user_photo='".$path."' , ";				
			}
			$str .= " IsActive = '".$status."', ModifiedDate = '$today' , UserId='".$_SESSION["UserId"]."'  where user_ID = '".$edit_id."' ";			
			$db->insert($str);
			$db->insert_log("update","a_users",$edit_id,"User updated","User",$str);

			echo json_encode(array("rslt"=>"2"));
		}
		else {
			echo json_encode(array("rslt"=>"3")); //same exists
		}
	}
	else {
		echo json_encode(array("rslt"=>"4"));  //no values
	}
		
	break;
	
	case 'del':
	  $edit_id = base64_decode($Id);
	  
	  $today = date("Y-m-d");
	  $str="update a_users set IsActive = '2', ModifiedDate = '$today' , UserId='".$_SESSION["UserId"]."'  where user_ID = '".$edit_id."'";
	  $db->insert($str); 	 
	  
	  $db->insert_log("delete","a_users",$edit_id,"User deleted","User",$str);
 	  echo json_encode(array("rslt"=>"5")); //deletion
	  	 
		
	break;
	
	case 'changestatus':
	  $edit_id = base64_decode($Id);
	  
	  $today = date("Y-m-d");
	  $status = $actval;
	  $str="update a_users set IsActive = '$status', ModifiedDate = '$today' , UserId='".$_SESSION["UserId"]."'  where user_ID = '".$edit_id."'";
	  $db->insert($str); 	 
	  
	  $db->insert_log("changestatus","a_users",$edit_id,"Userinfo status changed","User",$str);
 	  echo json_encode(array("rslt"=>"6")); //deletion
	  	 
		
	break;
}



?>