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

	
	function __construct()
	{
		$this->conn = Database::connect();
    }
    
    public function add_role_permission(array $data){
		$sql = "INSERT INTO role_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_role_permission(array $key_col_vals, array $values) {
        
        $sql = "UPDATE role_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_role_permission_by_name($role_permission_name){
		$sql = "SELECT * FROM role_permission WHERE role_permission_name LIKE '%".$role_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_role_permissions(){
		$sql = "SELECT a.*,b.*,c.* FROM role_permission a 
        JOIN system_permission b ON a.system_permission_id=b.system_permission_id 
        JOIN role c ON a.role_id=c.role_id";
		$statement = $this->conn->prepare($sql);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

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

    public function read_user_role_permission($role_id,$system_group_id){
		$sql = "SELECT a.* ,b.*,c.* FROM role_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.role_id=:role_id AND b.group_number=:system_group_id";
		$statement = $this->conn->prepare($sql);
        $statement->bindParam(":role_id",$role_id);
        $statement->bindParam(":system_group_id",$system_group_id);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
	}

	public function check_page_role_permission($role_id,$system_permission_id){
		$sql = "SELECT a.* ,b.*,c.* FROM role_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.role_id=:role_id AND b.system_permission_id=:system_permission_id";
		$statement = $this->conn->prepare($sql);
        $statement->bindParam(":role_id",$role_id);
        $statement->bindParam(":system_permission_id",$system_permission_id);
		$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 role_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_role_permission($system_permission_id,$role_id){
		$sql = "SELECT * FROM role_permission WHERE
         system_permission_id=:system_permission_id AND role_id=:role_id";
		$statement = $this->conn->prepare($sql);
        $statement->bindParam(":system_permission_id",$system_permission_id);
        $statement->bindParam(":role_id",$role_id);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
	}

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

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

}