<?php

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

/**
 * Description of rtgrid_edit_action_model
 *
 * @author Igor Ratsun
 */
class rtgrid_edit_action_model extends CI_Model {

    public function save($table, $primery_key, $primery_key_value, $post_data = array()) {
        if (!empty($post_data)) {
            if ($primery_key_value == 0) {
                $this->db->insert($table, $post_data);
            } else {
                $this->db->
                        where($primery_key, $primery_key_value)->
                        update($table, $post_data);
            }
            
            return TRUE;
        }
        
        return FALSE;
    }
    
    public function save_and_back_to_list($table, $primery_key, $primery_key_value, $post_data = array())
    {
        $this->rtgrid_edit_model->redirect_url = preg_replace('/\/edit\/\d+/', '', $this->uri->uri_string());
        
        return $this->save($table, $primery_key, $primery_key_value, $post_data);
    }

    public function add($table, $primery_key, $primery_key_value, $post_data = array())
    {
        $this->rtgrid_edit_model->redirect_url = str_replace('/edit/add', '', $this->uri->uri_string());
        
        return $this->save($table, $primery_key, $primery_key_value, $post_data);
    }
    
    public function add_and_next($table, $primery_key, $primery_key_value, $post_data = array()) {
    
        return $this->save($table, $primery_key, $primery_key_value, $post_data);
    }

}

