<?php
class Role {
    private	$db;
    private $info;
    public function __construct(){
        $this->db			=	DBConnectorFactory::loadDriver();	
    }
    public function setData(array $info){
        $this->info			=	$info;
        return $this;
    }
    public function getRelatedRoles($term = null, $page, $limit) {
        $data = array();
        $data['is_more'] = false;
        $is_used_role = false;
        $data['data'] = array();
        $cond = '';
        //we stop it since when add user the roles is wrong
//        if(!Session::is_smart_admin()){
//            //used_role send 1 from roles inside add/edit user and not send in case of role permission parent role
//            $is_used_role = !empty($this->info['used_role']) ? true : false;
//        }
        //get roles to control for current user
        $roles = CommonFunctions::get_user_roles($is_used_role);
        $data_filter = array();
        if (!is_null($term)) {
            $cond .= " AND  Role_Name LIKE ?  ";
            $data_filter[] = "%".$term."%";
        } 
         $cond.= (empty($roles) ? " and 1=2 " :" and Role_ID IN ({$roles}) ");
        $query = $this->db->prepare_Stmt("select 
                                        Role_ID as id,
                                        Role_Name as text
                                   from 
                                        tbl_common_roles
                                    where 1=1
                                ". $cond . "
                                limit " . ($page * $limit) . "," . ($limit + 1),$data_filter);
        while ($row = $this->db->assoc($query)) {
            $data['data'][] = $row;
        }
        if (count($data['data']) > $limit) {
            array_pop($data['data']);
            $data['is_more'] = true;
        }
        return $data;
    }
    public function getAllRoles($term = null, $page, $limit) {
        $data = array();
        $data['is_more'] = false;
        $data['data'] = array();
        $cond = '';
        $data_filter = array();
        if (!is_null($term)) {
            $cond .= " AND  Role_Name LIKE ?  ";
            $data_filter[] = "%".$term."%";
        } 
        if (!empty($this->info['without_admin'])) {
            $cond .= " AND  Role_ID <> 1 ";
        } 
        $query = $this->db->prepare_Stmt("select 
                                        Role_ID as id,
                                        Role_Name as text
                                   from 
                                        tbl_common_roles
                                    where 1=1
                                    ". $cond . "
                                limit " . ($page * $limit) . "," . ($limit + 1),$data_filter);
        while ($row = $this->db->assoc($query)) {
            $data['data'][] = $row;
        }
        if (count($data['data']) > $limit) {
            array_pop($data['data']);
            $data['is_more'] = true;
        }
        return $data;
    }
    public function getUserRoles($role_ids){
        $data = array();
        $query = $this->db->query("select Role_ID as id,
                                          Role_Name as text
                                   from tbl_common_roles
                                   where Role_ID IN ({$role_ids})");
        while($row=$this->db->assoc($query)){
            $data[] = $row;
        }
        return $data;
    }
    public function getPermissionsByRoleID($role_id){
        $data = array();
        $result = $this->db->query('SELECT Permission_ID FROM  tbl_common_role_permission where Role_ID ="'.$role_id.'"');
        while($row          =   $this->db->assoc($result))
            $data[]         =   $row['Permission_ID'];
        return $data;
    }
}