<?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 Roles_model
 *
 * @author Andhy Taslin <andhy@earworks.co.id>
 * @since 1.0
 */
class User_role_model extends MY_Model {

    const STATUS_ACTIVE = 1;
    const STATUS_BANNED = 2;

    protected $table = "user_roles";
    public $timestamps = true;
    public $incrementing = false;
    public $primaryKey = ['user_id', 'role_id'];

    public static function status_label($selected = null) {
        $lib = [
            self::STATUS_ACTIVE => lang('ACTIVE'),
            self::STATUS_BANNED => lang('BANNED'),
        ];
        return array_key_exists($selected, $lib) ? $lib[$selected] : $lib;
    }
    public static function arrange($parent_id, $child_ids = []){
        static::get_dbo()->where('user_id', $parent_id)->delete();
        $insert = [];
        foreach($child_ids as $id){
            $insert[] = [
                'user_id' => $parent_id,
                'role_id' => $id,
                'created_at' =>date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
                'created_by' => null,
                'updated_by' => null
            ];
        }
        return static::insert($insert);
    }
      public function scopeWhere_key($query, $user_id = null, $role_id = null) {
        return $query->where('user_id', $user_id)->where('role_id', $role_id);
    }
}
