<?php

if (!function_exists('_make_rtgrid_obs')) {

    class rtgrid_obs_helper_add_observer implements SplObserver {

        public $acl_access_resource = '';
        public $acl_access_action = '';
        public $message = '';
        public $module_text = '';

        public function update(SplSubject $subject) {
            $CI = & get_instance();
            if (in_array($subject->current_action, array('rtgrid_action_add', 'rtgrid_action_add_and_next')) && $subject->current_action_return) {
                $CI->load->helper('ciext/observer');
                $save = new saveObserver();
                $save->set_access($this->acl_access_resource, $this->acl_access_action);
                $link = str_replace('add', $subject->_primery_key_value, $CI->uri->uri_string());
                $save->set_text(str_replace(array('{link}', '{module}'), array($link, $this->module_text), $this->message));
                $save->save();
            }
        }

    }

    class rtgrid_obs_helper_edit_observer implements SplObserver {

        public $acl_access_resource = '';
        public $acl_access_action = '';
        public $message = '';
        public $module_text = '';

        public function update(SplSubject $subject) {
            $CI = & get_instance();
            if (in_array($subject->current_action, array('rtgrid_action_save', 'rtgrid_action_save_and_back_to_list')) && $subject->current_action_return) {
                $CI->load->helper('ciext/observer');
                $save = new saveObserver();
                $save->set_access($this->acl_access_resource, $this->acl_access_action);
                $link = '/' . $CI->uri->uri_string();
                $save->set_text(str_replace(array('{link}', '{module}'), array($link, $this->module_text), $this->message));
                $save->save();
            }
        }

    }

    class rtgrid_obs_helper_delete_observer implements SplObserver {

        public $acl_access_resource = '';
        public $acl_access_action = '';
        public $message = '';
        public $module_text = '';

        public function update(SplSubject $subject) {

            if ($subject->current_action == 'delete' && $subject->current_action_return) {
                $CI = & get_instance();
                $CI->load->helper('ciext/observer');
                $save = new saveObserver();
                $save->set_access($this->acl_access_resource, $this->acl_access_action);
                $save->set_text(str_replace('{module}', $this->module_text, $this->message));

                $save->save();
            }
        }

    }

    function _make_rtgrid_obs($class, $acl_access_resource, $acl_access_action, $message, $module_text) {
        if (empty($message) || is_null($module_text)) {
            $CI = & get_instance();
            $module_text = (is_null($module_text)) ? $CI->lang->line('<br />Module - %s') : $module_text;
            $module_name = $CI->lang->line('module_name');
            if (!empty($module_name)) {
                $module_text = sprintf($module_text, $module_name);
            }
        }
        switch ($class) {
            case 'add':
                $obs = new rtgrid_obs_helper_add_observer();
                $message = ($message) ?: $CI->lang->line("Added new <a href=\"{link}\">record</a>{module}");
                break;
            case 'edit':
                $obs = new rtgrid_obs_helper_edit_observer();
                $message = ($message) ?: $CI->lang->line("Edited <a href=\"{link}\">record</a>{module}");
                break;
            case 'delete':
                $obs = new rtgrid_obs_helper_delete_observer();
                $message = ($message) ?: $CI->lang->line('Deleted record(s){module}');
                break;

            default :

                return FALSE;
                break;
        }
        $obs->acl_access_resource = $acl_access_resource;
        $obs->acl_access_action = $acl_access_action;
        $obs->message = $message;
        $obs->module_text = $module_text;

        return $obs;
    }

}

if (!function_exists('make_rtgrid_add_obs')) {

    function make_rtgrid_add_obs($acl_access_resource, $acl_access_action, $message = '', $module_text = NULL) {

        return _make_rtgrid_obs('add', $acl_access_resource, $acl_access_action, $message, $module_text);
    }

}

if (!function_exists('make_rtgrid_edit_obs')) {

    function make_rtgrid_edit_obs($acl_access_resource, $acl_access_action, $message = '', $module_text = NULL) {

        return _make_rtgrid_obs('edit', $acl_access_resource, $acl_access_action, $message, $module_text);
    }

}

if (!function_exists('make_rtgrid_delete_obs')) {

    function make_rtgrid_delete_obs($acl_access_resource, $acl_access_action, $message = '', $module_text = NULL) {

        return _make_rtgrid_obs('delete', $acl_access_resource, $acl_access_action, $message, $module_text);
    }

}

