<?php

class Modulos_acciones_roles_model extends CI_Model {

    public function __construct() {
        parent::__construct();
        $this->load->database();
        $this->table = 'modulos_acciones_roles';
    }

    public function get_all_modulos_acciones_activas_by_rol_id($rol_id) {
        $this->db->select($this->table . '.id AS modulo_accion_rol_id, ' . $this->table . '.modulo_accion_id');
        $this->db->from($this->table);
        $this->db->where($this->table . '.rol_id', $rol_id);
        $this->db->where($this->table . '.status', 1);
        $result = $this->db->get();
        return $result->result_array();
    }

    public function accion_permitida_by_rol_id($modulo_accion_id, $rol_id) {
        $this->db->select('id');
        $this->db->from($this->table);
        $this->db->where($this->table . '.modulo_accion_id', $modulo_accion_id);
        $this->db->where($this->table . '.rol_id', $rol_id);
        $this->db->where($this->table . '.status', 1);
        $this->db->limit(1);
        $result = $this->db->get();
        $consulta = $result->row_array();
        return $consulta !== NULL;
    }

    public function insert($data) {
        if ($this->db->insert($this->table, $data)) {
            $id = $this->db->insert_id();
            if (create_log($this->table, $id, $data)) {
                return $id;
            }
        }
        return FALSE;
    }

    public function update($id, $data) {
        if ($this->db->update($this->table, $data, ['id' => $id])) {
            if (create_log($this->table, $id, $data)) {
                return $id;
            }
        }
        return FALSE;
    }

}
