<commit_before><?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * A simple model to agilize our work, 
 * this class model uses CodeIgniter Active Record to query build
 *
 * @link https://github.com/wallacesilva/ci-simple-model
 * @copyright Copyright (c) 2014, <NAME> <http://wallacesilva.com>
 */

class MY_Model extends CI_Model 
{   
    /**
     * This model's default database table name. Set on extend.
     */
    public $_tablename;

    /**
     * Auto change table to get methods from $CI->db
     */
    public $_auto_requiretable = true;

    /**
     * Methods to auto set tablename
     */
    public $_requiretable = array(  'get', 'get_where', 'count_all_results', 'count_all', 
                                    'insert', 'insert_batch', 'update', 'update_batch', 
                                    'delete', 'empty_table', 'truncate'
                                ); // possible 'from'
    
    /**
     * Initialise the model, this use the default database $this->db.
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function __call($method, $args)
    {       

        if (is_callable( array($this, $method) )){ // call method on this class first (for overwrite methods)

            return call_user_func_array( array($this, $method), $args );

        } elseif (is_callable( array($this->db, $method) )) { // call method from db

            $method_args = $args;

            if ($this->_auto_requiretable && in_array($method, $this->_requiretable)) {

                $method_args = array($this->_tablename);
                $method_args = array_merge($method_args, $args);

            }

            return call_user_func_array( array($this->db, $method), $method_args );

        } else {

            throw new Exception("Error on call method (".$method.") with arguments (".(string)$args.")", 1);
            return false;
            
        }

    }

    /**
     * Check if item/id exists on database
     * @param integer $id Id item from table
     * @return boolean 
     */
    public function check_exist($id)
    {
        // verifica sem tem mensagem
        if (empty($message_error))
            $message_error = 'Ops! Erro ao processar dados.';

        // if not valid :false
        if ($id < 1) 
            return false;

        $item = $this->find($id);

        return (bool)(isset($item->id) && $item->id == $id);

    }

    public function check_exist_redirect($id, $message_error=null, $redirect_to=false)
    {   
        if (!$this->check_exist($id)) {

            $this->session->set_flashdata('msg_error', $message_error);
            redirect($redirect_to);

        } 
        
    }

    /**
     * Count all records with where
     * @param array/string $where Where to count all records
     */
    public function countAll( $where=null )
    {

        if( is_array($where) )
          $this->db->where( $where );

        $total_results = $this->db->count_all_results( $this->_tablename );

        return $total_results;

    }

    /**
     * Get a Row/record from the database table
     * @param integer $id Id to get record
     */<commit_msg>fixed message error and fixed real check exist item on table<commit_after><?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * A simple model to agilize our work, 
 * this class model uses CodeIgniter Active Record to query build
 *
 * @link https://github.com/wallacesilva/ci-simple-model
 * @copyright Copyright (c) 2014, <NAME> <http://wallacesilva.com>
 */

class MY_Model extends CI_Model 
{   
    /**
     * This model's default database table name. Set on extend.
     */
    public $_tablename;

    /**
     * Auto change table to get methods from $CI->db
     */
    public $_auto_requiretable = true;

    /**
     * Methods to auto set tablename
     */
    public $_requiretable = array(  'get', 'get_where', 'count_all_results', 'count_all', 
                                    'insert', 'insert_batch', 'update', 'update_batch', 
                                    'delete', 'empty_table', 'truncate'
                                ); // possible 'from'
    
    /**
     * Initialise the model, this use the default database $this->db.
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function __call($method, $args)
    {       

        if (is_callable( array($this, $method) )){ // call method on this class first (for overwrite methods)

            return call_user_func_array( array($this, $method), $args );

        } elseif (is_callable( array($this->db, $method) )) { // call method from db

            $method_args = $args;

            if ($this->_auto_requiretable && in_array($method, $this->_requiretable)) {

                $method_args = array($this->_tablename);
                $method_args = array_merge($method_args, $args);

            }

            return call_user_func_array( array($this->db, $method), $method_args );

        } else {

            throw new Exception("Error on call method (".$method.") with arguments (".(string)$args.")", 1);
            return false;
            
        }

    }

    /**
     * Check if item/id exists on database
     * @param integer $id Id item from table
     * @return boolean 
     */
    public function check_exist($id)
    {
        // if not valid :false
        if ($id < 1) 
            return false;

        $item = $this->find($id);

        return (bool)(isset($item[0]->id) && $item[0]->id == $id);

    }

    public function check_exist_redirect($id, $message_error=null, $redirect_to=false)
    {   
        if (!$this->check_exist($id)) {

            // verifica sem tem mensagem
            if (empty($message_error))
                $message_error = 'Ops! Erro ao processar dados.';

            $this->session->set_flashdata('msg_error', $message_error);
            redirect($redirect_to);

        } 
        
    }

    /**
     * Count all records with where
     * @param array/string $where Where to count all records
     */
    public function countAll( $where=null )
    {

        if( is_array($where) )
          $this->db->where( $where );

        $total_results = $this->db->count_all_results( $this->_tablename );

        return $total_results;

    }

    /**
     * Get a Row/record from the database table
     * @param integer $id Id to get record
     */
{'fraction_non_alphanumeric': 0.12939173502553783, 'fraction_numerical': 0.0021668472372697724, 'mean_word_length': 1.9875173370319001, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 43, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2825836', 'n_tokens_mistral': 1910, 'n_tokens_neox': 1818, 'n_words': 506}