<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class saveObserver {

    protected $CI;
    protected $_data = array(
        'id_user' => 0,
        'ip' => '',
        'text' => '',
        'id_acl_resource' => '',
        'id_acl_action' => ''
    );

    public function __construct($text = '') {
        $this->CI = & get_instance();
        $this->_data['id_user'] = $this->CI->rt_user->id_user;
        $this->_data['ip'] = $this->CI->input->ip_address();

        $this->set_text($text);
    }

    public function set_text($text = '') {
        $this->_data['text'] = $text;
    }

    public function set_access($resource = '', $action = '') {
        $data = $this->CI->db
                ->select('aa.id_acl_resource, aa.id_acl_action')
                ->where('ar.id_acl_resource = aa.id_acl_resource')
                ->where('ar.active', TRUE)
                ->where('ar.acl_resource_name', $resource)
                ->where('aa.acl_action_name', $action)
                ->limit(1)
                ->get('acl_resource ar, acl_action aa')
                ->row();
        if (!empty($data)) {
            $this->_data['id_acl_resource'] = $data->id_acl_resource;
            $this->_data['id_acl_action'] = $data->id_acl_action;
        }
    }

    public function save() {
        
        $this->CI->db->insert('ciext_observer', $this->_data);
    }

}