<?php
require_once(LIB_PATH.DS.'database.php');
class ADMINACCOUNT {
	protected static $table_name="admin_account";
	public $id;

	public $password;
	public $surname;
	public $othernames;
	public $email;
	public $accessed_Level;
	public $status;
	public $pictures;
	public $datereg;
	

public static function authenticate($username="", $password=""){
global $database;
$username= $database->escape_value($username);
$password = $database->escape_value($password);
$sql = "SELECT * FROM ".self ::$table_name." ";
$sql .= " WHERE username='{$username}' ";
$sql .= "AND password='{$password}' LIMIT 1";	

$result_array = self::find_by_sql($sql);
return !empty($result_array)? array_shift($result_array) : false;
//return $result_array;
}


public static function authenticate_by_email($email="", $password=""){
global $database;
//$email= $database->escape_value($email);
//$password = $database->escape_value($password);
$sql = "SELECT * FROM ".self ::$table_name." ";
$sql .= " WHERE email='{$email}' ";
$sql .= "AND password='{$password}' LIMIT 1";	

$result_array = self::find_by_sql($sql);
return !empty($result_array)? array_shift($result_array) : false;
}

public function fullname(){
	
if (isset($this->formno)&& isset($this->username)){
	return $this->formno ." ". $this->username;	
	} else {
		return "";
	}
}



//Common database Methods
public static function find_all(){
	global $database;
	return self::find_by_sql("SELECT * FROM ".self:: $table_name);
	//return $result_set;
	
}
public static function find_by_id($id=0){
	global $database;
	$result_array = self::find_by_sql("SELECT * FROM  ".self::$table_name." WHERE id={$id} LIMIT 1");
	//$found = $database->fetch_array($result_set);
	//return $found;
	return !empty($result_array)? array_shift($result_array) : false;
	
}

public static function find_by_sql($sql=""){
	  global $database; $connection='';
        $result_set = $database->query($connection, $sql);
        $object_array = array();
        while ($row = $database->fetch_array($result_set)) {
            $object_array[] = self::instantiate($row);
        }
        return $object_array;

	
}
private static function instantiate($record){
	
	$object = new self;
	foreach ($record as $attribute=>$value){
	if ($object->has_attribute($attribute)){
	$object->$attribute = $value;
	}
		
	}
	return $object;
}
private function has_attribute($attribute){
	
$object_vars = get_object_vars($this);
return array_key_exists($attribute,$object_vars);	
}

public function save(){
return isset($this->id) ?	$this->update() : $this->create();	
}

public function create(){
global $database;
	$sql = "INSERT INTO ".self ::$table_name." (";
	$sql .= "formno, email";
	$sql .= ") 	VALUES ('";
	$sql .= $database->escape_value($this->formno)."', '"	;
	$sql .= $database->escape_value($this->email)."') '"	;
	if ($database->query($sql)){
		$this->id=$database->insert_id();
		return true;
	}else {
		return false;
	}
	
}

public function update($email,$password,$surname,$othernames,$pics,$id){
	
	global $database;
	$sql = "UPDATE  ".self ::$table_name." SET password='{$password}',surname='{$surname}',othernames='{$othernames}',email='{$email}',status='active',pictures='{$pics}' Where id={$id} ";
	//$sql .= "password ='". $password."', ";	
	//$sql .= "surname ='". $surname."', ";	
	//$sql .="othernames = '". $othernames."', "; 
	//$sql .=" email = '". $email."', "; 
	///$sql .= "status ='". 'active'."', ";	
	//$sql .= "pictures ='". $pics."', ";	
	//$sql .= " WHERE id=". $id;
	
	$database->query($sql);
return ($database->affected_rows()==1)? true: false;
	}
	
	public function update2($email,$password,$surname,$othernames,$id){
	
	global $database;
	$sql = "UPDATE  ".self ::$table_name." SET password='{$password}',surname='{$surname}',othernames='{$othernames}',email='{$email}',status='active' Where id={$id} ";
	//$sql .= "password ='". $password."', ";	
	//$sql .= "surname ='". $surname."', ";	
	//$sql .="othernames = '". $othernames."', "; 
	//$sql .=" email = '". $email."', "; 
	///$sql .= "status ='". 'active'."', ";	
	//$sql .= "pictures ='". $pics."', ";	
	//$sql .= " WHERE id=". $id;
	
	$database->query($sql);
return ($database->affected_rows()==1)? true: false;
	}
	
public function delete(){
	
global $database;
$sql = "DELETE FROM ".self ::$table_name." ";
$sql .= "WHERE id=" . $database->escape_value($this->id);
$sql .= " LIMIT 1";	
$database->query($sql);
return ($database->affected_rows() ==1) ? true: false;
}

//Closing tags for user's class
}

?>