<?php

namespace App\System\Model;
use App\System\Basic\beModel;

class UserGroupModel extends beModel {
    public $table = "be_user_groups";
    public $primary_key = "ugid";
    public static $usergroups = [];
    public static function beRenderUserName($name,$template){
        return str_replace("{username}",$name,$template);
    }
    public static function beCheckPermission($ugid,$permission){
        $usergroup = [];
        if($ugid == 0){
            global $be;
            $usergroup = $be->user["group"];
        }
        else{
            if(!self::$usergroups[$ugid]){
                $usergroup = self::beSelect("ugid,ugpermissions")->beWhere("ugid",$ugid)->beGet();
                $usergroup["ugpermissions"] = json_decode($usergroup["ugpermissions"],true);
                self::$usergroups[$ugid] = $usergroup;
            }
            else{
                $usergroup = self::$usergroups[$ugid];
            }
        }
        if($usergroup["ugpermissions"][$permission] == 0){
            return false;
        }
        return $usergroup["ugpermissions"][$permission];
    }
}

?>