<?php

/*
 * *************************************************
 * Copyright (C) 2021 Earworks.                    *
 * Codeigniter Project Template Modification       *
 * Please contact our email for more information : *
 * contact@earworks.co.id                          *
 * *************************************************
 */

defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description of Role_model
 *
 * @author Andhy Taslin <andhy@earworks.co.id>
 * @since 1.0
 */
class Role_model extends MY_Model {
    
    // login type (please define too at User_model for system requirement)
    const LOGIN_TYPE_BACKOFFICE = 1;
    const LOGIN_TYPE_PUBLIC = 2;
    const LOGIN_TYPE_VISITOR = 3;

    protected $table = 'roles';
    public $timestamps = true;
    
    public static $relation_list = [
        'permissions' => Permission_model::class
    ];
    
    public static function login_type_label($selected = null) {
        $lib = [
            self::LOGIN_TYPE_BACKOFFICE => lang('BACKOFFICE'),
            self::LOGIN_TYPE_PUBLIC => lang('PUBLIC'),
             self::LOGIN_TYPE_VISITOR => lang('VISITOR'),
        ];
        return array_key_exists($selected, $lib) ? $lib[$selected] : $lib;
    }
    /**
     * The users that belong to the role.
     */
    public function users() {
        return $this->belongsToMany(User_model::class, 'user_roles', 'user_id', 'role_id')->withTimestamps();
    }

    public function permissions() {
        return $this->belongsToMany(Permission_model::class, 'role_permissions', 'role_id','permission_id')->withTimestamps();
    }

}
