<?php
/**
 * Model for User_permission
 *
 * @author Mills ABeiku Godfred
 */
class UserPermission
{
	
	public $conn;
	

	
	function __construct()
	{
		$this->conn = Database::connect();
    }
    
    public function add_user_permission(array $data){
		$sql = "INSERT INTO user_permission ";
		$fields = array_keys($data);
		$vals = array_values($data);

		$sql .= "(".implode(",", $fields).")";
		$arr = array();
		foreach ($fields as $f) {
			$arr[] = "?";
		}
		$sql .= " VALUES(".implode(",", $arr).")";

		$statement = $this->conn->prepare($sql);

		foreach ($vals as $i => $v) {
			$statement->bindValue($i + 1, $v);
		}

		return $statement->execute();

    }
    
    public function update_user_permission(array $key_col_vals, array $values) {
        
        $sql = "UPDATE user_permission SET ";
        $fields = array_keys($values);
        $vals = array_values($values);
        $key_cols = array_keys($key_col_vals);
        $key_vals = array_values($key_col_vals);
        foreach ($fields as $i=>$f) {
            $fields[$i] .= " = ? ";
        }

        $sql .= implode(",", $fields);
        $sql .= " WHERE " ;
        
        foreach ($key_cols as $i=>$f) {
            $key_cols[$i] .= " = ? ";
        }
        
        $sql .= implode(" AND ", $key_cols);

        $statement = $this->conn->prepare($sql);
        foreach ($vals as $i=>$v) {
            $statement->bindValue($i+1, $v);
        }
        
        foreach ($key_vals as $i=>$v) {
           if($i==0){
               $statement->bindValue((count($fields)+$i+1), $v);
            }else{
               $statement->bindValue((count($fields)+$i), $v);
            }
        }
        
       return  $statement->execute();

    }


    public function get_user_permission_by_name($user_permission_name){
		$sql = "SELECT * FROM user_permission WHERE user_permission_name LIKE '%".$user_permission_name."%'";
		$statement = $this->conn->prepare($sql);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
	}
	
	
	public function read_all_user_permissions(){
		$sql = "SELECT a.*,b.*,c.* FROM user_permission a 
        JOIN system_permission b ON a.system_permission_id=b.system_permission_id 
        JOIN employee c ON a.employee_id=c.employee_id";
		$statement = $this->conn->prepare($sql);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

    public function read_user_permission($user_permission_id){
		$sql = "SELECT * FROM user_permission WHERE user_permission_id=:user_permission_id";
		$statement = $this->conn->prepare($sql);
        $statement->bindParam(":user_permission_id",$user_permission_id);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
	}

    public function read_page_user_permission($employee_id){
		$sql = "SELECT a.* ,b.*,c.* FROM user_permission a
        JOIN system_permission b ON a.system_permission_id=b.system_permission_id 
        JOIN system_group c ON b.group_number=c.system_group_id 
         WHERE a.employee_id=:employee_id";
		$statement = $this->conn->prepare($sql);
        $statement->bindParam(":employee_id",$employee_id);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
	}

    public function check_current_page($action_link){
		$sql = "SELECT a.* ,b.*,c.* FROM user_permission a
        JOIN system_permission b ON a.system_permission_id=b.system_permission_id 
        JOIN system_group c ON b.group_number=c.system_group_id 
         WHERE b.action_link=:action_link AND a.state='Limit'";
		$statement = $this->conn->prepare($sql);
        $statement->bindParam(":action_link",$action_link);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
	}

	public function request_sms_legit_receivers($role_id){
		$sql = "SELECT a.*,b.*,c.* FROM user_permission a 
		JOIN system_permission b ON a.system_permission_id=b.system_permission_id 
		JOIN employee c ON a.role_id=c.employee_role_id 
		WHERE a.role_id=:role_id AND b.action_link='item_request_approval.php'";
		$statement = $this->conn->prepare($sql);
        $statement->bindParam(":role_id",$role_id);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
	}

    public function validate_user_permission($system_permission_id,$employee_id){
		$sql = "SELECT * FROM user_permission WHERE
         system_permission_id=:system_permission_id AND employee_id=:employee_id";
		$statement = $this->conn->prepare($sql);
        $statement->bindParam(":system_permission_id",$system_permission_id);
        $statement->bindParam(":employee_id",$employee_id);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
	}

	public function remove_user_permission($user_permission_id){
		$sql = "DELETE FROM user_permission WHERE user_permission_id=:user_permission_id";
		$statement = $this->conn->prepare($sql);
        $statement->bindParam(":user_permission_id",$user_permission_id);
		$statement->execute();
		if ($statement->rowCount() > 0) {
            $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

    public function remove_to_add($system_permission_id,$employee_id){
		$sql = "DELETE FROM user_permission WHERE
        system_permission_id=:system_permission_id AND employee_id=:employee_id";
		$statement = $this->conn->prepare($sql);
        $statement->bindParam(":system_permission_id",$system_permission_id);
        $statement->bindParam(":employee_id",$employee_id);
		$statement->execute();
		if ($statement->rowCount() > 0) {
            $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

}