<?php
/*
*
* -------------------------------------------------------
* CLASSNAME:        admin_users
* GENERATION DATE:  30.12.2013
* FOR MYSQL TABLE:  admin_users
* FOR MYSQL DB:     web-school
* -------------------------------------------------------
* CODE GENERATED BY:
* PHP-MYSQL-CLASS GENERATOR
* -------------------------------------------------------
*
*/

include_once("class.database.php");

// **********************
// CLASS DECLARATION
// **********************

class admin_users
{ // class : begin


// **********************
// ATTRIBUTE DECLARATION
// **********************

var $id;   // KEY ATTR. WITH AUTOINCREMENT

var $first_name;   // (normal Attribute)
var $last_name;   // (normal Attribute)
var $username;   // (normal Attribute)
var $password;   // (normal Attribute)
var $email;   // (normal Attribute)
var $date_updated;   // (normal Attribute)
var $type;   // (normal Attribute)
var $phone;   // (normal Attribute)
var $user_key;   // (normal Attribute)
var $status;   // (normal Attribute)

var $database; // Instance of class database


// **********************
// CONSTRUCTOR METHOD
// **********************

function admin_users()
{

$this->database = new Database();

}


// **********************
// GETTER METHODS
// **********************


function getid()
{
return $this->id;
}

function getfirst_name()
{
return $this->first_name;
}

function getlast_name()
{
return $this->last_name;
}

function getusername()
{
return $this->username;
}

function getpassword()
{
return $this->password;
}

function getemail()
{
return $this->email;
}

function getdate_updated()
{
return $this->date_updated;
}

function gettype()
{
return $this->type;
}

function getphone()
{
return $this->phone;
}

function getuser_key()
{
return $this->user_key;
}

function getstatus()
{
return $this->status;
}

// **********************
// SETTER METHODS
// **********************


function setid($val)
{
$this->id =  $val;
}

function setfirst_name($val)
{
$this->first_name =  $val;
}

function setlast_name($val)
{
$this->last_name =  $val;
}

function setusername($val)
{
$this->username =  $val;
}

function setpassword($val)
{
$this->password =  $val;
}

function setemail($val)
{
$this->email =  $val;
}

function setdate_updated($val)
{
$this->date_updated =  $val;
}

function settype($val)
{
$this->type =  $val;
}

function setphone($val)
{
$this->phone =  $val;
}

function setuser_key($val)
{
$this->user_key =  $val;
}

function setstatus($val)
{
$this->status =  $val;
}

// **********************
// SELECT METHOD / LOAD
// **********************

function select($id)
{

$sql =  "SELECT * FROM admin_users WHERE id = $id;";
$result =  $this->database->query($sql);
$result = $this->database->result;
$row = mysql_fetch_object($result);


$this->id = $row->id;

$this->first_name = $row->first_name;

$this->last_name = $row->last_name;

$this->username = $row->username;

$this->password = $row->password;

$this->email = $row->email;

$this->date_updated = $row->date_updated;

$this->type = $row->type;

$this->phone = $row->phone;

$this->user_key = $row->user_key;

$this->status = $row->status;

}





// **********************
// DELETE
// **********************

function delete($id)
{
	$org_id=$_SESSION['org_id'];
$sql = "DELETE FROM admin_users WHERE id = $id AND type='$org_id'";
$result = $this->database->query($sql);

}

// **********************
// INSERT
// **********************

function insert()
{
$this->id = ""; // clear key for autoincrement

$sql = "INSERT INTO admin_users ( first_name,last_name,username,password,email,date_updated,type,phone,user_key,status ) VALUES ( '$this->first_name','$this->last_name','$this->username','$this->password','$this->email','$this->date_updated','$this->type','$this->phone','$this->user_key','$this->status' )";
$result = $this->database->query($sql);
$this->id = mysql_insert_id($this->database->link);

}

// **********************
// UPDATE
// **********************

function update($id)
{



$sql = " UPDATE admin_users SET  first_name = '$this->first_name',last_name = '$this->last_name',username = '$this->username',password = '$this->password',email = '$this->email',date_updated = '$this->date_updated',type = '$this->type',phone = '$this->phone',user_key = '$this->user_key',status = '$this->status' WHERE id = $id ";

$result = $this->database->query($sql);



}


// **********************
// SELECT ALL
// **********************
function selectall($cond='',$limit='',$order)
{
	$sql =  "SELECT * FROM admin_users ";
	if($cond!='')
		$sql .= " WHERE $cond ";
	
	if($order!='')
		$sql .= " ORDER BY $order ";
		
	if($limit!='')
		$sql .= " LIMIT $limit ";
	
	$sql .=";";
	
	$result =  $this->database->query($sql);
	$result = $this->database->result;
	$this->rows=array();
	while($row=mysql_fetch_object($result))
	{
	$this->rows[] = $row;
	}
	return $this->rows;
}

// **********************
// CUSTOM METHOD / LOAD
// **********************
/*Email Check*/
function checkemail($email)
	{
		$org_id=$_SESSION['org_id'];
		$sql =  "SELECT * FROM admin_users WHERE email = '$email' AND type='$org_id';";
		$result =  $this->database->query($sql);
		$result = $this->database->result;
		
		if($row=mysql_fetch_object($result))
		{
			return $row->id;
		}
		else 
		{
			return -1;
		}
	}
/*Email Check*/
/*Check  Username*/
function checkuser($user)
	{
		$sql =  "SELECT * FROM admin_users WHERE username = '$user';";
		
		$result =  $this->database->query($sql);
		$result = $this->database->result;
		
		if($row=mysql_fetch_object($result))
		{
			return $row->id;
		}
		else 
		{
			return -1;
		}
	}
/*Check  Username*/
function login()
{
	//$sql =  "SELECT * FROM admin_users WHERE username='".$this->username."' AND password='".$this->password."' AND status=1 ;" ;
	$sql = " SELECT  admin_users.id as id,admin_users.type as org_id,admin_users.username as username,admin_users.status as status,admin_users.first_name as fname,admin_users.last_name as lname,admin_users.user_key as userkey FROM `admin_users`  left join  client_org on admin_users.type=client_org.id  WHERE admin_users.username='".$this->username."' AND admin_users.password='".$this->password."' AND client_org.status=1 "; 
	$result =  $this->database->query($sql);
	$result = $this->database->result;
	if($row=mysql_fetch_object($result))
	{ 
		ob_start();
		session_start();
		$_SESSION['admin_id']=$row->id;
		$_SESSION['admin_fname']=$row->fname;
		$_SESSION['admin_lname']=$row->lname;
		$_SESSION['org_id']=$row->org_id;
		$_SESSION['user_key']=$row->userkey;
		$_SESSION['username']=$row->username;
		$_SESSION['status']=$row->status;
		$_SESSION['last_action']=time();
		ob_clean();
		header('location:dashboard.php');
	}
	else
	{
		return false;
	}
}

function check_admin_login()
{
	$time=time();
	error_reporting(0);
	ob_start();ob_clean();
	
 //$host  = $_SERVER['HTTP_HOST'];
//$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
//$extra = 'index.php';
	if(!$_SESSION['admin_id']=='')
	{ 
		if(($time-$_SESSION['last_action'])>80000) 
		{
			unset($_SESSION['admin_id']);
			unset($_SESSION['admin_fname']);
			unset($_SESSION['admin_lname']);
			unset($_SESSION['username']);
			unset($_SESSION['org_id']);
			unset($_SESSION['phone']);
			unset($_SESSION['user_key']);
			unset($_SESSION['status']);
			unset($_SESSION['last_action']);
			session_destroy();
			header("location: index.php");
			
			}
		else
		{
			//die('aaaa'.$_SESSION['last_action']);	
			$_SESSION['last_action']=time();
			return true;
		}
	}
	else
	{
		header("location: index.php");
		
	}	
}
function logout()
{
			session_start();
			unset($_SESSION['admin_id']);
			unset($_SESSION['admin_fname']);
			unset($_SESSION['admin_lname']);
			unset($_SESSION['username']);
			unset($_SESSION['org_id']);
			unset($_SESSION['phone']);
			unset($_SESSION['user_key']);
			unset($_SESSION['status']);
			unset($_SESSION['last_action']);
			session_destroy();
			header('location:index.php');
}

/*  Check User */
 function check_user($username)
	{
		$sql =  "SELECT * FROM admin_users WHERE username = '$username';";
		
		$result =  $this->database->query($sql);
		$result = $this->database->result;
		if($row=mysql_fetch_object($result))
		{
			return $row->id;
		}
		else 
		{
			return -1;
		}
	}
	/*  Check User */
	
	
	
	/*Select Email*/
function selectuser_email($email)
{

$sql =  "SELECT * FROM admin_users WHERE email = '$email';";
$result =  $this->database->query($sql);
$result = $this->database->result;
$row = mysql_fetch_object($result);

$this->id = $row->id;

$this->first_name = $row->first_name;

$this->last_name = $row->last_name;

$this->username = $row->username;

$this->password = $row->password;

$this->email = $row->email;

$this->date_updated = $row->date_updated;

$this->type = $row->type;

$this->phone = $row->phone;

$this->user_key = $row->user_key;

}
	/*Select Email*/
	
	/* Select by user key */
function resetpassword($activecode)
{

$sql =  "SELECT * FROM admin_users WHERE user_key = '$activecode';";
$result =  $this->database->query($sql);
$result = $this->database->result;
$row = mysql_fetch_object($result);

$this->id = $row->id;

$this->first_name = $row->first_name;

$this->last_name = $row->last_name;

$this->username = $row->username;

$this->password = $row->password;

$this->email = $row->email;

$this->date_updated = $row->date_updated;

$this->type = $row->type;

$this->phone = $row->phone;

$this->user_key = $row->user_key;

$this->status = $row->status;
}
	/*Select by user key*/
	
	/*update status*/
	function update_status($id)
{
$sql = " UPDATE admin_users SET  status = '$this->status' WHERE id = $id ";
$result = $this->database->query($sql);
}
	/*update status*/
	
	/*update key*/
	function update_key($id)
{
$sql = " UPDATE admin_users SET  user_key = '$this->user_key' WHERE id = $id ";
$result = $this->database->query($sql);
}
	/*update key*/	
	/*update key after password reset */
	function reset_pass_key($id)
{
$sql = " UPDATE admin_users SET  password = '$this->password',user_key = '',date_updated = '$this->date_updated' WHERE id = $id ";
$result = $this->database->query($sql);
}
	/*update key after password reset*/
	/*Org Select all*/
function selectorgall()
{
	$org_id=$_SESSION['org_id'];
	$sql =  "SELECT * FROM admin_users WHERE type='$org_id' ";
	$result =  $this->database->query($sql);
	$result = $this->database->result;
	$this->rows=array();
	while($row=mysql_fetch_object($result))
	{
	$this->rows[] = $row;
	}
	return $this->rows;
}
/*Org Select all*/
/*  Check check_userid */
 function check_userid($id)
	{
		$org_id=$_SESSION['org_id'];
		$sql =  "SELECT * FROM admin_users WHERE id = '$id' AND type='$org_id';";
		//die($sql);
		$result =  $this->database->query($sql);
		$result = $this->database->result;
		if($row=mysql_fetch_object($result))
		{
			return $row->id;
		}
		else 
		{
			return -1;
		}
	}
	/*  Check check_userid */	
} // class : end

?>