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

	
	function __construct()
	{
		$this->conn = Database::connect();
    }
    
    public function add_system_permission(array $data){
		$sql = "INSERT INTO system_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_system_permission(array $key_col_vals, array $values) {
        
        $sql = "UPDATE system_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_system_permission_by_name($display_name){
		$sql = "SELECT * FROM system_permission WHERE display_name LIKE '%".$display_name."%'";
		$statement = $this->conn->prepare($sql);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
	}

	public function get_system_permission_by_page($page){
		$sql = "SELECT * FROM system_permission WHERE action_link LIKE '%".$page."%'";
		$statement = $this->conn->prepare($sql);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
	}
	
	
	public function read_all_system_permissions(){
		$sql = "SELECT a.*,b.* FROM system_permission a 
        JOIN system_group b ON a.group_number=b.system_group_id";
		$statement = $this->conn->prepare($sql);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

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

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

	// Empty Table
	public function empty_table($table_name){
		$sql = "TRUNCATE TABLE $table_name";
		$statement = $this->conn->prepare($sql);
		$statement->execute();
		if ($statement->rowCount() > 0) {
            $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

}