MMCT TEAM
Server IP : 103.191.208.50  /  Your IP : 216.73.216.53
Web Server : LiteSpeed
System : Linux orion.herosite.pro 4.18.0-553.53.1.lve.el8.x86_64 #1 SMP Wed May 28 17:01:02 UTC 2025 x86_64
User : celkcksm ( 1031)
PHP Version : 7.4.33
Disable Function : show_source, system, shell_exec, passthru, popen, exec
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0755) :  /home/celkcksm/banking.ncriptech.com/../ecampus.ncriptech.com/application/core/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/celkcksm/banking.ncriptech.com/../ecampus.ncriptech.com/application/core/BaseModel.php
<?php defined('BASEPATH') or exit('No direct script access allowed');
/**
 * 
 */
class BaseModel extends CI_Model
{
	
	public $_db='';
	public $table='';
	public $table_joined='';
	public $primary_key='id';

	public function __construct(){
		$link_code=get_link_code();

		if(!empty($link_code) && $link_code!=='admin' && $link_code != 'localhost' && $link_code != '127.0.0.1' && $link_code != 'cloudcampus'){
			$db_group= $link_code;
		}else{			
			$db_group=DB_GROUP;
		}		

		// Load the correct database group
		$this->_db = $this->load->database($db_group, TRUE, TRUE);
	}
	

	public function store($data,$batch=FALSE,$return_query=FALSE){
		if($return_query==FALSE){
			if($batch==FALSE){
				$this->_db->insert($this->table,$data);
				return $this->_db->insert_id();
			}else{
				return $this->_db->insert_batch($this->table,$data);
			}	
		}else{
			return $this->_db->set($data)->get_compiled_insert($this->table);
		}	
	}

	public function store_replace($data,$return_query=FALSE){
		if($return_query==FALSE){
			return $this->_db->replace($this->table,$data);
		}else{
			$this->_db->replace($this->table,$data);
			return $this->_db->last_query();
		}
	}

	public function modify($data=array(),$param=array(),$batch=FALSE,$return_query=FALSE){
		if(!empty($data) && !empty($param)){
			if($return_query==FALSE){
				if($batch==FALSE){
					return $this->_db->update($this->table,$data,$param);
				}else{
					return $this->_db->update_batch($this->table,$data,$param);
				}
			}else{
				if($batch==FALSE){
					$this->_db->update($this->table,$data,$param);
				}else{
					$this->_db->update_batch($this->table,$data,$param);
				}
				return $this->_db->last_query();
			}
		}else{
			return 'Data and param are empty';
		}		
	}

	public function remove($param,$truncate=0,$return_query=FALSE){
		if($return_query==FALSE){
			if($truncate==0){
				return $this->_db->delete($this->table,$param);
			}else if($truncate==1){
				return $this->_db->empty_table($this->table);
			}else if($truncate==2){
				return $this->_db->truncate($this->table);
			}
		}else{
			if($truncate==0){
				$this->_db->delete($this->table,$param);
			}else if($truncate==1){
				$this->_db->empty_table($this->table);
			}else if($truncate==2){
				$this->_db->truncate($this->table);
			}
			return $this->_db->last_query();
		}	
	}

	public function remove_soft($param,$deleted_by,$return_query=FALSE){
		$data=array(
			'is_deleted'=>1,
			'deleted_by'=>$deleted_by,
			'deleted_date'=>date('Y-m-d H:i:s')
		);
		if($return_query==FALSE){			
			return $this->modify($data,$param,FALSE,$return_query=FALSE);
		}else{
			return $this->modify($data,$param,FALSE,TRUE);
		}
	}

	public function get_joined($fields,$joined_fields,$param,$param_or=NULL,$join_type='LEFT',$result_type='object',$return_multi=FALSE,$return_query=FALSE){
		$this->_db->select($fields);
		if(is_array($this->table_joined)){
			foreach ($this->table_joined as $key => $value) {
				$this->_db->join($value,$joined_fields[$key],$join_type);
			}
		}else{
			$this->_db->join($this->table_joined,$joined_fields,$join_type);
		}

		if($return_query==FALSE){
			if($return_multi==FALSE){
				if($param_or!=NULL){
					$this->_db->or_where($param_or);
				}else{
					$this->_db->where($param);
				}
				$result=$this->_db->get($this->table);
				return $result->first_row();
			}else{
				if($param_or!=NULL){
					$this->_db->or_where($param_or);
				}else{
					$this->_db->where($param);
				}
				$result=$this->_db->get($this->table);
				if($result_type=='object'){
					return $result->result();
				}else if($result_type=='array'){
					return $result->result_array();
				}	
			}	
		}else{
			if($return_multi==FALSE){
				if($param_or!=NULL){
					$this->_db->or_where($param_or);
				}else{
					$this->_db->where($param);
				}
				$this->_db->get($this->table);
				return $this->_db->last_query();
			}else{
				if($param_or!=NULL){
					$this->_db->or_where($param_or);
				}else{
					$this->_db->where($param);
				}
				$this->_db->get($this->table);
				return $this->_db->last_query();	
			}			
		}	
	}

	public function get_one($param,$is_group='',$return_query=FALSE){
		if($return_query==FALSE){
			if($is_group!=''){
				$this->_db->group_by($is_group);
			}
			return $this->_db->get_where($this->table,$param)->first_row();
		}else{
			if($is_group!=''){
				$this->_db->group_by($is_group);
			}
			$this->_db->get_where($this->table,$param)->first_row();
			return $this->_db->last_query();
		}		
	}

	public function get_one_specific($fields=null,$param,$is_group='',$return_query=FALSE){
		if($fields!=''){
			$this->_db->select($fields);
		}
		if($return_query==FALSE){
			if($is_group!=''){
				$this->_db->group_by($is_group);
			}
			return $this->_db->get_where($this->table,$param)->first_row();
		}else{
			if($is_group!=''){
				$this->_db->group_by($is_group);
			}
			$this->_db->get_where($this->table,$param)->first_row();
			return $this->_db->last_query();
		}		
	}

	public function get_one_order($param,$is_group='',$is_order='',$is_order_by='ASC',$return_query=FALSE){
		if($return_query==FALSE){
			if($is_group!=''){
				$this->_db->group_by($is_group);
			}
			if($is_order!=''){
				$this->_db->order_by($is_order,$is_order_by);
				$this->_db->limit(1);
			}
			return $this->_db->get_where($this->table,$param)->first_row();
		}else{
			if($is_group!=''){
				$this->_db->group_by($is_group);
			}
			if($is_order!=''){
				$this->_db->order_by($is_order,$is_order_by);
				$this->_db->limit(1);
			}
			$this->_db->get_where($this->table,$param)->first_row();
			return $this->_db->last_query();
		}		
	}

	public function get_many($param=null,$order_by=null,$order='DESC',$return_query=FALSE){
		if($param==null || empty($param)){
			if($return_query==FALSE){
				if($order_by!=null){
					$this->_db->order_by($order_by,$order);
				}				
				return $this->_db->get($this->table)->result();
			}else{
				if($order_by!=null){
					$this->_db->order_by($order_by,$order);
				}
				$this->_db->get($this->table);
				return $this->_db->last_query();
			}
			
		}else{
			if($return_query==FALSE){
				if($order_by!=null){
					$this->_db->order_by($order_by,$order);
				}
				return $this->_db->get_where($this->table,$param)->result();
			}else{
				if($order_by!=null){
					$this->_db->order_by($order_by,$order);
				}
				$this->_db->get_where($this->table,$param);
				return $this->_db->last_query();
			}
		}
	}


	public function get_many_specific($fields,$param=null,$order_by=null,$order='DESC',$return_query=FALSE){
		$this->_db->select($fields);
		if($param==null || empty($param)){
			if($return_query==FALSE){
				if($order_by!=null){
					$this->_db->order_by($order_by,$order);
				}				
				return $this->_db->get($this->table)->result();
			}else{
				if($order_by!=null){
					$this->_db->order_by($order_by,$order);
				}
				$this->_db->get($this->table);
				return $this->_db->last_query();
			}
			
		}else{
			if($return_query==FALSE){
				if($order_by!=null){
					$this->_db->order_by($order_by,$order);
				}
				return $this->_db->get_where($this->table,$param)->result();
			}else{
				if($order_by!=null){
					$this->_db->order_by($order_by,$order);
				}
				$this->_db->get_where($this->table,$param);
				return $this->_db->last_query();
			}
		}
	}


	public function get_limit_many($param=null,$length=6,$start=0,$order_by=null,$order='DESC',$return_query=FALSE){
		if($param==null || empty($param)){
			if($return_query==FALSE){
				if($order_by!=null){
					$this->_db->order_by($order_by,$order);
				}
				$this->_db->limit($length,$start);				
				return $this->_db->get($this->table)->result();
			}else{
				if($order_by!=null){
					$this->_db->order_by($order_by,$order);
				}
				$this->_db->limit($length,$start);
				$this->_db->get($this->table);
				return $this->_db->last_query();
			}
			
		}else{
			if($return_query==FALSE){
				if($order_by!=null){
					$this->_db->order_by($order_by,$order);
				}

				$this->_db->limit($length,$start);
				return $this->_db->get_where($this->table,$param)->result();
			}else{
				if($order_by!=null){
					$this->_db->order_by($order_by,$order);
				}
				$this->_db->limit($length,$start);
				$this->_db->get_where($this->table,$param);
				return $this->_db->last_query();
			}
		}
	}

	public function get_order_many($param=null,$order_by='',$order='ASC',$return_query=FALSE){
		if($param==null || empty($param)){
			if($return_query==FALSE){
				if($order_by!=''){
					$this->_db->order_by($order_by,$order);
				}
				return $this->_db->get($this->table)->result();
			}else{
				if($order_by!=''){
					$this->_db->order_by($order_by,$order);
				}
				$this->_db->get($this->table)->result();
				return $this->_db->last_query();
			}
			
		}else{
			if($return_query==FALSE){
				$this->_db->where($param);
				if($order_by!=''){
					$this->_db->order_by($order_by,$order);
				}
				return $this->_db->get($this->table)->result();
			}else{
				$this->_db->where($param);
				if($order_by!=''){
					$this->_db->order_by($order_by,$order);
				}
				return $this->_db->get($this->table)->result();
				return $this->_db->last_query();
			}
		}
	}

	public function get_like($param=NULL,$returned_fields=NULL,$orlike=null,$order_by=NULL,$order=NULL,$limit=null,$offset=null,$return_query=FALSE){
		if($returned_fields!=NULL){
			$this->_db->select($returned_fields);
		}
		
		if($orlike==''){
			$this->_db->like($param);
		}else if($orlike!=''){
			$this->_db->like($param);
			if(is_array($orlike)){
				foreach ($orlike as $key => $value) {
					$this->_db->or_like($value);
				}
			}			
		}
		$this->_db->order_by($order_by, $order);
		if($limit==null && $offset==null){
			$this->_db->limit($limit, $offset);
		}
		$query=$this->_db->get($this->table);
		if($return_query==FALSE){			
			return $query->result();
		}else{
			return $this->_db->last_query();
		}			
	}


	public function get_match_against($returned_fields=NULL,$match_fields,$against_val,$param=NULL,$param_or=NULL,$mode,$order_by='id',$order='ASC',$limit=null,$offset=null,$return_query=FALSE){

		//$this->_db->cache_on();
		if($returned_fields!=NULL){
			$this->_db->select($returned_fields);
		}

		$this->_db->where('MATCH('.$match_fields.') AGAINST("'.$against_val.'" IN '.$mode.' MODE)');

		if($param!=NULL){
			$this->_db->where($param);
		}

		if($param_or!=NULL){
			$this->_db->group_start();
			$this->_db->or_where($param_or,TRUE);
			$this->_db->group_end();
		}

		// $query=$this->_db->get($this->table);
		if($return_query==FALSE){			
			if($limit==null && $offset==null){
				return $this->_db->order_by($order_by, $order)->get($this->table)->result();
			}else{
				return $this->_db->order_by($order_by, $order)->get($this->table, $limit, $offset)->result();
			}
		}else{
			if($limit==null && $offset==null){
				$this->_db->order_by($order_by, $order)->get($this->table)->result();
			}else{
				$this->_db->order_by($order_by, $order)->get($this->table, $limit, $offset)->result();
			}
			return $this->_db->last_query();
		}
	}


	public function get($param=null,$search_str=null,$orlike=null,$order_by='id',$order='DESC',$limit=null,$offset=null,$return_query=FALSE){

		if(isset($this->primary_key)){
			$order_by=$this->primary_key;
		}

		if(($param==null || empty($param)) && ($search_str==null || empty($search_str))){
			if($return_query==FALSE){
				if($limit==null && $offset==null){
					return $this->_db->order_by($order_by, $order)->get($this->table)->result();
				}else{
					return $this->_db->order_by($order_by, $order)->get($this->table, $limit, $offset)->result();
				}	
			}else{
				if($limit==null && $offset==null){
					$this->_db->get($this->table)->order_by($order_by, $order)->result();
				}else{
					$this->_db->order_by($order_by, $order)->get($this->table, $limit, $offset)->result();
				}
				return $this->_db->last_query();
			}
		}else if(($param!=null || !empty($param)) && ($search_str==null || empty($search_str))){
			if($return_query==FALSE){
				if($limit==null && $offset==null){
					return $this->_db->order_by($order_by, $order)->get_where($this->table,$param)->result();
				}else{
					return $this->_db->order_by($order_by, $order)->get_where($this->table,$param,$limit, $offset)->result();
				}
				
			}else{
				if($limit==null && $offset==null){
					$this->_db->order_by($order_by, $order)->get_where($this->table,$param)->result();
				}else{
					$this->_db->order_by($order_by, $order)->get_where($this->table,$param,$limit, $offset);
				}

				return $this->_db->last_query();
			}
		}else if(($param!=null || !empty($param)) && ($search_str!=null || !empty($search_str))){
			if($return_query==FALSE){
				if($limit==null && $offset==null){
					$this->_db->where($param);
					if($orlike==''){
						$this->_db->like($search_str);
					}else if($orlike!=''){
						$this->_db->like($search_str);
						$this->_db->or_like($orlike);
					}
					$this->_db->order_by($order_by, $order);
					return $this->_db->get($this->table,$param)->result();
				}else{
					return $this->_db->order_by($order_by, $order)->get_where($this->table,$param,$limit, $offset);
				}
				
			}else{
				if($limit==null && $offset==null){
					$this->_db->where($param);
					if($orlike==''){
						$this->_db->like($search_str);
					}else if($orlike!=''){
						$this->_db->like($search_str);
						$this->_db->or_like($orlike);
					}
					$this->_db->order_by($order_by, $order);
					$this->_db->get($this->table,$param)->result();
				}else{
					$this->_db->order_by($order_by, $order)->get_where($this->table,$param,$limit, $offset);
				}

				return $this->_db->last_query();
			}
		}
	}


	public function get_count($param=null,$search_str=null,$orlike=null,$order_by='id',$order='DESC',$limit=null,$offset=null,$return_query=FALSE){

		if(isset($this->primary_key)){
			$order_by=$this->primary_key;
		}

		if(($param==null || empty($param)) && ($search_str==null || empty($search_str))){
			if($return_query==FALSE){
				if($limit==null && $offset==null){
					$this->_db->order_by($order_by, $order);
					$this->_db->from($this->table);
					return $this->_db->count_all_results();
				}else{
					$this->_db->order_by($order_by, $order);
					$this->_db->limit($limit, $offset);
					$this->_db->from($this->table);
					return $this->_db->count_all_results();
				}	
			}else{
				if($limit==null && $offset==null){
					$this->_db->order_by($order_by, $order);
					$this->_db->from($this->table);
					$this->_db->count_all_results();
				}else{
					$this->_db->order_by($order_by, $order);
					$this->_db->limit($limit, $offset);
					$this->_db->from($this->table);
					$this->_db->count_all_results();
				}
				return $this->_db->last_query();
			}
		}else if(($param!=null || !empty($param)) && ($search_str==null || empty($search_str))){
			if($return_query==FALSE){
				if($limit==null && $offset==null){
					$this->_db->where($param);
					$this->_db->order_by($order_by, $order);
					$this->_db->from($this->table);
					return $this->_db->count_all_results();
				}else{
					$this->_db->where($param);
					$this->_db->order_by($order_by, $order);
					$this->_db->limit($limit, $offset);
					$this->_db->from($this->table);
					return $this->_db->count_all_results();
				}
			}else{
				if($limit==null && $offset==null){
					$this->_db->where($param);
					$this->_db->order_by($order_by, $order);
					$this->_db->from($this->table);
					$this->_db->count_all_results();
				}else{
					$this->_db->where($param);
					$this->_db->order_by($order_by, $order);
					$this->_db->limit($limit, $offset);
					$this->_db->from($this->table);
					$this->_db->count_all_results();
				}

				return $this->_db->last_query();
			}
		}else if(($param!=null || !empty($param)) && ($search_str!=null || !empty($search_str))){
			if($return_query==FALSE){
				if($limit==null && $offset==null){
					$this->_db->where($param);
					if($orlike==''){
						$this->_db->like($search_str);
					}else if($orlike!=''){
						$this->_db->like($search_str);
						$this->_db->or_like($orlike);
					}
					$this->_db->order_by($order_by, $order);
					$this->_db->from($this->table);
					return $this->_db->count_all_results();
				}else{
					$this->_db->where($param);
					$this->_db->order_by($order_by, $order);
					$this->_db->limit($limit, $offset);
					$this->_db->from($this->table);
					return $this->_db->count_all_results();
				}
			}else{
				if($limit==null && $offset==null){
					$this->_db->where($param);
					if($orlike==''){
						$this->_db->like($search_str);
					}else if($orlike!=''){
						$this->_db->like($search_str);
						$this->_db->or_like($orlike);
					}
					$this->_db->order_by($order_by, $order);
					$this->_db->limit($limit,$offset);
					$this->_db->from($this->table);
					$this->_db->count_all_results();
				}else{
					$this->_db->where($param);
					$this->_db->order_by($order_by, $order);
					$this->_db->limit($limit, $offset);
					$this->_db->from($this->table);
					$this->_db->count_all_results();
				}

				return $this->_db->last_query();
			}
		}
	}

	public function get_total_count($param=null,$group_by=NULL,$find_in_set=NULL,$return_query=FALSE){
		if($param!=null){
			$this->_db->where($param);
		}

		if($find_in_set!=NULL){
			$this->_db->where('FIND_IN_SET("'.$find_in_set['needle'].'",'.$this->_db->dbprefix.$find_in_set['haystack'].')<>0');
		}

		if($group_by!=NULL){
			$this->_db->group_by($group_by);
		}
		$num_rows = $this->_db->count_all_results($this->table);

		if($return_query==TRUE){
			return $this->_db->last_query();
		}else{
			return $num_rows;
		}		
	}

	public function _get_total_count($param=null,$or_param=null,$group_by=NULL,$return_query=FALSE){
		if($param!=null){
			$this->_db->where($param);
		}

		if($or_param!=null){
			$this->_db->where_in($or_param['field'],$or_param['values']);
		}

		if($group_by!=NULL){
			$this->_db->group_by($group_by);
		}

		$num_rows = $this->_db->count_all_results($this->table);

		if($return_query==TRUE){
			return $this->_db->last_query();
		}else{
			return $num_rows;
		}		
	}

	public function get_many_not_in($param,$param_val,$return_query=FALSE){
		if($return_query==FALSE){
			return $this->_db->where_not_in($param,$param_val)->get($this->table)->result();
		}else if($return_query==TRUE){
			$this->_db->where_not_in($param,$param_val)->get($this->table)->result();
			return $this->_db->last_query();
		}	
	}

	public function get_in($param,$param_val,$return_query=FALSE){
		if($return_query==FALSE){
			return $this->_db->where_in($param,$param_val,FALSE)->get($this->table)->result();	
		}else if($return_query==TRUE){
			$this->_db->where_in($param,$param_val,FALSE)->get($this->table);
			return $this->_db->last_query();
		}		
	}


	public function get_find_in_set($param_val,$param_field,$find_in_field=FALSE,$return_query=FALSE){
		$this->_db->select('*');
		if($find_in_field==FALSE){
			$this->_db->where('FIND_IN_SET("'.$param_field.'","'.$param_val.'")<>0');
		}else if($find_in_field==TRUE){
			$this->_db->where('FIND_IN_SET("'.$param_val.'","'.$param_field.'")<>0');
		}
		
		$result=$this->_db->get($this->table);
		if($return_query==FALSE){
			return $result->result();
		}else if($return_query==TRUE){
			return $this->_db->last_query();
		}
	}

	public function get_many_or_not_in($param=array(),$param_val){
		return $this->_db->or_where_not_in($param,$param_val)->get($this->table)->result();	
	}	

	public function get_distinct($param,$return_query=FALSE){
		$this->_db->distinct();
		if($return_query==TRUE){
			$this->_db->where($param);
			$this->_db->get($this->table)->result();
			return $this->_db->last_query();
		}else{
			$this->_db->where($param);
			return $this->_db->get($this->table)->result(); 
		}		
	}

	public function get_group_concat($param,$param_field,$param_field_as,$return_query=FALSE){
		$this->_db->select('GROUP_CONCAT(DISTINCT '.$param_field.') as '.$param_field_as);
		$this->_db->where($param);
		$query = $this->_db->get($this->table);
		if($return_query==FALSE){
			return $query->first_row();
		}else{
			return $this->_db->last_query();
		}		
	}

	public function get_max($field,$as,$param=null,$return_query=FALSE){
		$this->_db->select_max($field, $as);

		if(!is_null($param)){
			$this->_db->where($param);
		}

		if($return_query==TRUE){
			$this->_db->get($this->table)->first_row();
			return $this->_db->last_query();
		}else{
			return $this->_db->get($this->table)->first_row();
		}	
	}

	public function get_min($field,$as,$param=null,$return_query=FALSE){
		$this->_db->select_min($field, $as);

		if(!is_null($param)){
			$this->_db->where($param);
		}

		if($return_query==TRUE){
			$this->_db->get($this->table)->first_row();
			return $this->_db->last_query();
		}else{
			return $this->_db->get($this->table)->first_row();
		}
	}

	public function get_avg($field,$as,$param=null,$return_query=FALSE){
		$this->_db->select_avg($field, $as);
		if($param!=null){
			$this->_db->where($param);
		}
		if($return_query==TRUE){
			$this->_db->get($this->table)->result();
			return $this->_db->last_query();
		}else{
			return $this->_db->get($this->table)->result();
		}	
	}

	public function get_sum_v1($field,$as,$param=null,$return_query=FALSE){
		$this->_db->select_sum($field, $as);
		if($param!=null){
			$this->_db->where($param);
		}
		if($return_query==TRUE){
			$this->_db->get($this->table)->result();
			return $this->_db->last_query();
		}else{
			return $this->_db->get($this->table)->result();
		}	
	}

	public function get_sum($field, $as, $param = null, $return_query = FALSE) {
		$this->_db->select_sum($field, $as);
		
		if ($param != null) {
			foreach ($param as $key => $value) {
				if (strpos($key, ' ') !== false) {
					// If the key contains a space, it's a full condition
					$this->_db->where($key, $value, FALSE);
				} else {
					// Otherwise, it's a simple key-value pair
					$this->_db->where($key, $value);
				}
			}
		}
		
		$query = $this->_db->get($this->table);
		
		if ($return_query == TRUE) {
			return $this->_db->last_query();
		} else {
			return $query->result();
		}
	}
	

	public function get_in_sum($field,$as,$param=null,$param_in=null,$param_val=null,$return_query=FALSE){
		$this->_db->select_sum($field, $as);
		if($param!=null){
			$this->_db->where($param);
		}
		if($param_in!=null){
			$this->_db->where_in($param_in,$param_val,FALSE);
		}
		if($return_query==TRUE){
			$this->_db->get($this->table)->result();
			return $this->_db->last_query();
		}else{
			return $this->_db->get($this->table)->result();
		}	
	}

	public function get_duplicates($param=array(),$param_or=array(),$field=null,$return_query=FALSE){
		if($field==null){
			$this->_db->select('COUNT(*) AS counted');
		}else{
			$this->_db->select($field.',COUNT('.$field.') AS counted');
		}
		if($param!=null){
			$this->_db->where($param);
		}

		if($param_or!=null){
			$this->_db->or_where($param_or);
		}
		
		if($field!=null){
			$this->_db->group_by($field);
			$this->_db->having('COUNT('.$field.')>0');
		}

		$result=$this->_db->get($this->table);
		
		if($return_query==TRUE){
			return $this->_db->last_query();
		}else{
			return $result->first_row();
		}
	}

	public function _get_duplicates($param=array(),$param_or=array(),$group_field=null,$field=null,$group=FALSE,$return_query=FALSE){
		if($field==null){
			$this->_db->select('COUNT(*) AS counted');
		}else{
			if($group_field!=null){
				$this->_db->select($group_field.',COUNT('.$field.') AS counted');
			}else{
				$this->_db->select($field.',COUNT('.$field.') AS counted');
			}			
		}
		if($param!=null){
			$this->_db->where($param);
		}

		if($group==TRUE){
			$this->_db->group_start();
		}

		if($param_or!=null){
			$this->_db->or_where($param_or);
		}

		if($group==TRUE){
			$this->_db->group_end();
		}
		
		if($field!=null){
			if($group_field!=null){
				$this->_db->group_by($group_field);
			}else{
				$this->_db->group_by($field);
			}
			
			$this->_db->having('COUNT('.$field.')>0');
		}

		$result=$this->_db->get($this->table);
		
		if($return_query==TRUE){
			return $this->_db->last_query();
		}else{
			return $result->result();
		}
	}

	public function get_last($param,$order_by,$return_query=FALSE){
		$this->_db->where($param);
		$this->_db->order_by($order_by,'DESC');
		$this->_db->limit('1');
		$query = $this->_db->get($this->table);

        if($return_query==FALSE){
            return $query->first_row();
        }else if($return_query==TRUE){
            return $this->_db->last_query();
        } 	
	}

	public function _get_last($param,$or_param=null,$order_by=null,$return_query=FALSE){
		if($param!=null){
			$this->_db->where($param);
		}
		
		if($or_param!=null){
			$this->_db->group_start();
			foreach ($or_param as $key => $value) {
				$this->_db->or_where($key,$value);
			}
			
			$this->_db->group_end();
		}
		if($order_by!=null){
			$this->_db->order_by($order_by,'DESC');
		}else{
			$this->_db->order_by('id','DESC');
		}

		$this->_db->limit('1');
		$query = $this->_db->get($this->table);

        if($return_query==FALSE){
            return $query->first_row();
        }else if($return_query==TRUE){
            return $this->_db->last_query();
        } 	
	}


	/* --------------------------------------------------------------
	 * INTERNAL METHODS
	 * ------------------------------------------------------------ */

	/**
	 * Trigger an event and call its observers. Pass through the event name
	 * (which looks for an instance variable $this->event_name), an array of
	 * parameters to pass through and an optional 'last in interation' boolean
	 */
	public function trigger($event, $data = false, $last = true)
	{
		if (isset($this->$event) && is_array($this->$event))
		{
			foreach ($this->$event as $method)
			{
				if (strpos($method, '('))
				{
					preg_match('/([a-zA-Z0-9\_\-]+)(\(([a-zA-Z0-9\_\-\., ]+)\))?/', $method, $matches);

					$method = $matches[1];
					$this->callback_parameters = explode(',', $matches[3]);
				}

				$data = call_user_func_array(array($this, $method), array($data, $last));
			}
		}

		return $data;
	}


}

MMCT - 2023