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

    const STATUS_ACTIVE = 1;
    const STATUS_BANNED = 2;

    protected $table = "role_permissions";
    public $timestamps = true;
     public $incrementing = false;
    public $primaryKey = ['role_id', 'permission_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('role_id', $parent_id)->delete();
        $insert = [];
        foreach($child_ids as $id){
            $insert[] = [
                'role_id' => $parent_id,
                'permission_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, $role_id = null, $permission_id = null) {
        return $query->where('role_id', $role_id)->where('permission_id', $permission_id);
    }

}
