<?php

class Permissions {

    private $permissions = array();
    public $User_ID;

    public function __construct($User_ID, $is_get_prmsn = false) {
        $this->db = DBConnectorFactory::loadDriver();
        $this->User_ID = $User_ID;
        if ($is_get_prmsn || !isset(smc_session()['permissions']) || $this->User_ID != smc_session()['user_logged_id'])
            $this->permissions = $this->queryPermissions($this->User_ID);
        else
            $this->permissions = smc_session()['permissions'];
    }

    private function queryPermissions($User_ID) {
        $data = array();
        $data['system_permissions'] = array();
        $query = $this->db->query('select Role_ID from tbl_common_users where User_ID = "' . $User_ID . '" and Status_ID = 1');
        $role_ids = $this->db->assoc($query)['Role_ID'];
        if (!empty($role_ids)) {
            $query_permission = $this->db->query('select distinct p.* from tbl_common_permission p
                    inner join tbl_common_role_permission r using(Permission_ID)
                where r.Role_ID in (' . $role_ids . ') ');
            while ($row = $this->db->assoc($query_permission)) {
//            $data[] = $row;
                $data['system_permissions'][$row['Permission_Name']] = $row['Permission_Name'];
            }
        }
//        smc_session()['system_permissions'] = $data['system_permissions'];
        return $data;
    }

    public function getPermissions() {
        return $this->permissions;
    }

    public function hasPermission($perm) {
        if (smc_session()['default_setting']['is_production'] == 1 && Session::is_smart_admin())
            return true;
        if (array_key_exists($perm, $this->permissions['system_permissions']))
            return true;
        return false;
    }

    //return all permissions under the desired permission name 
    public static function getPermissionIds($permission_name) {
        $data = array();
        $db = DBConnectorFactory::loadDriver();
        $names = is_array($permission_name) ? "'" . implode("','", $permission_name) . "'" : "'{$permission_name}'";
        $query = $db->query("select Permission_ID, Permission_Name from tbl_common_permission where Permission_Name in ({$names})");
        while ($row = $db->assoc($query))
            $data[$row['Permission_Name']] = $row['Permission_ID'];
        return $data;
    }

    //return all permissions under the desired permission id
    public static function getChildPermissions($permission_ids, &$data, $include_parent = true) {
        $db = DBConnectorFactory::loadDriver();
        if (is_array($permission_ids))
            $permission_ids = implode(',', $permission_ids);
        if (empty($permission_ids))
            return;
        //
        if ($include_parent) {
            $query = $db->query("select Permission_ID, Permission_Name from tbl_common_permission where Permission_ID in ({$permission_ids})");
            while ($row = $db->assoc($query))
                $data[$row['Permission_ID']] = $row['Permission_Name'];
        }
        //get child permissions
        $tmp_ids = '';
        $query = $db->query("select Permission_ID, Permission_Name from tbl_common_permission where Parent_ID in ({$permission_ids})");
        while ($row = $db->assoc($query)) {
            $tmp_ids .= (empty($tmp_ids) ? '' : ',') . $row['Permission_ID'];
            $data[$row['Permission_ID']] = $row['Permission_Name'];
        }
        self::getChildPermissions($tmp_ids, $data, false);
    }

}
