1
\$\begingroup\$

Can someone help me refactor this to be more optimized? Performance seems to be very optimal, but I think more can be squeezed from it.

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

    class o7th_Model extends CI_Model {

        private $msg = '';
        private $custom_msg = null;
        private $last_id;
        private $full_qry_count;

        // construct our class, including the parent, database, form_validation, and pagination
        public function __construct(){
            parent::__construct();
            $this->load->database();
            $this->load->library('form_validation');
            $this->load->library('pagination');
        }

        // Setup a list assoc array to return
        /*
         * USAGE - the only array parameter required is the 'table_name'
            self::qlist(array('select'=>'FIELDS', --- should be a comma seperate string of field names
                         'where'=>array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE'),), --- can have as many of these field/value arrays as you need
                         'or_where'=>array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE'),), --- can have as many of these field/value arrays as you need 
                         'where_in'=>array(array('field'=>'FIELD_NAME', 'value'=>array('VAL1', 'VAL2', etc...)),), --- can have as many of these field/value arrays as you need, value needs to be an array
                         'or_where_in'=>array(array('field'=>'FIELD_NAME', 'value'=>array('VAL1', 'VAL2', etc...)),), --- can have as many of these field/value arrays as you need, value needs to be an array
                         'where_not_in'=>array(array('field'=>'FIELD_NAME', 'value'=>array('VAL1', 'VAL2', etc...)),), --- can have as many of these field/value arrays as you need, value needs to be an array
                         'or_where_not_in'=>array(array('field'=>'FIELD_NAME', 'value'=>array('VAL1', 'VAL2', etc...)),), --- can have as many of these field/value arrays as you need, value needs to be an array
                         'like'=>array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE', 'wildcard'=>'before/after/both/none'),), --- can have as many of these field/value arrays as you need, wildcard is optional
                         'or_like'array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE', 'wildcard'=>'before/after/both/none'),), --- can have as many of these field/value arrays as you need, wildcard is optional
                         'not_like'array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE', 'wildcard'=>'before/after/both/none'),), --- can have as many of these field/value arrays as you need, wildcard is optional
                         'or_not_like'array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE', 'wildcard'=>'before/after/both/none'),), --- can have as many of these field/value arrays as you need, wildcard is optional
                         'group_by'=>array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE'),), --- can have as many of these field/value arrays as you need
                         'distinct'=>'TRUE/FALSE/NULL', --- TRUE enables SELECT DISTINCT, FALSE/NULL doesnt bother adding it
                         'having'=>array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE'),), --- can have as many of these field/value arrays as you need
                         'or_having'=>array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE'),), --- can have as many of these field/value arrays as you need
                         'order_by'=>array(array('field'=>'FIELD_NAME', 'direction'=>'ASC/DESC'),), --- can have as many of these field/direction arrays as you need, direction is optional
                         'limit'=>array(array('limit'=>'NUMBER_TO_LIMIT_RESULTS', 'offset'=>'NUMBER_TO_LIMIT_RESULTS')),
                         'join'=>array(array('table'=>'TABLE_NAME_TO_JOIN', 'on'=>'EX: a.field1 = b.field2', 'direction'=>'left/right/outer/inner/left outer/right outer'),), --- can have as many of these table/on/direction arrays as you need
                         'validations'=>array(array('field'=>'FORM_FIELD', 'label'=>'FORM_FIELD_LABEL', 'validation'=>'VALIDATION - See CI Docs'),), --- can have as many of these field/value/validation arrays as you need
                         'pagination'=>array('page_num'=>'CURRENT_PAGE_WE_ARE_ON', 'per_page'=>'HOW_MANY_RECORD_PER_PAGE'), --- page_num = the page number we are currently on, per_page is how many should be displayed per page: to display the links echo the self::paginator($base_url, $per_page, $total_rows, $num_links, $uri_segment, $aclass = null); method
                         'custom_message'=>'TO OVERRIDE THE INTERNAL ERROR MESSAGE RETURN',
                         'table_name'=>'TABLE_NAME')); --- REQUIRED!!!
         */
        public function qlist(){
            try{
                $tArgs = func_get_args();
                $tbl = self::prepArgs(1, $tArgs);
                if($tbl != null){
                    $qry = $this->db->get($tbl);
                    if($qry->num_rows() > 0){
                        $rs = $qry->result_array();
                        $this->full_qry_count = count($rs);
                        if(array_key_exists('pagination', $tArgs[0])){
                            $rs = array_slice($rs, $tArgs[0]['pagination']['page_num'], $tArgs[0]['pagination']['per_page']);
                        }
                    }else{
                        $rs = null;
                        $this->msg .= 'There were no records found, for the query performed<br />' . $this->db->_error_message();
                    }
                }else{
                    $rs = null;
                    $this->msg .= 'There were no records found, for the query performed<br />' . $this->msg;
                }
                return $rs;
            }catch(Exception $ex){
                $this->msg .= $ex->getMessage();
                return null;
            }
        }

        // setup a return assoc array details record, only returns 1 record
        /*
         * USAGE - the only array parameter required is the 'table_name'
            self::qdetails(array('select'=>'FIELDS', --- should be a comma seperate string of field names
                         'where'=>array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE'),), --- can have as many of these field/value arrays as you need
                         'or_where'=>array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE'),), --- can have as many of these field/value arrays as you need 
                         'where_in'=>array(array('field'=>'FIELD_NAME', 'value'=>array('VAL1', 'VAL2', etc...)),), --- can have as many of these field/value arrays as you need, value needs to be an array
                         'or_where_in'=>array(array('field'=>'FIELD_NAME', 'value'=>array('VAL1', 'VAL2', etc...)),), --- can have as many of these field/value arrays as you need, value needs to be an array
                         'where_not_in'=>array(array('field'=>'FIELD_NAME', 'value'=>array('VAL1', 'VAL2', etc...)),), --- can have as many of these field/value arrays as you need, value needs to be an array
                         'or_where_not_in'=>array(array('field'=>'FIELD_NAME', 'value'=>array('VAL1', 'VAL2', etc...)),), --- can have as many of these field/value arrays as you need, value needs to be an array
                         'like'=>array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE', 'wildcard'=>'before/after/both/none'),), --- can have as many of these field/value arrays as you need, wildcard is optional
                         'or_like'array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE', 'wildcard'=>'before/after/both/none'),), --- can have as many of these field/value arrays as you need, wildcard is optional
                         'not_like'array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE', 'wildcard'=>'before/after/both/none'),), --- can have as many of these field/value arrays as you need, wildcard is optional
                         'or_not_like'array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE', 'wildcard'=>'before/after/both/none'),), --- can have as many of these field/value arrays as you need, wildcard is optional
                         'group_by'=>array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE'),), --- can have as many of these field/value arrays as you need
                         'distinct'=>'TRUE/FALSE/NULL', --- TRUE enables SELECT DISTINCT, FALSE/NULL doesnt bother adding it
                         'having'=>array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE'),), --- can have as many of these field/value arrays as you need
                         'or_having'=>array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE'),), --- can have as many of these field/value arrays as you need
                         'order_by'=>array(array('field'=>'FIELD_NAME', 'direction'=>'ASC/DESC'),), --- can have as many of these field/direction arrays as you need, direction is optional
                         'limit'=>array(array('limit'=>'NUMBER_TO_LIMIT_RESULTS', 'offset'=>'NUMBER_TO_LIMIT_RESULTS')),
                         'join'=>array(array('table'=>'TABLE_NAME_TO_JOIN', 'on'=>'EX: a.field1 = b.field2', 'direction'=>'left/right/outer/inner/left outer/right outer'),), --- can have as many of these table/on/direction arrays as you need
                         'validations'=>array(array('field'=>'FORM_FIELD', 'label'=>'FORM_FIELD_LABEL', 'validation'=>'VALIDATION - See CI Docs'),), --- can have as many of these field/value/validation arrays as you need
                         'custom_message'=>'TO OVERRIDE THE INTERNAL ERROR MESSAGE RETURN',
                         'table_name'=>'TABLE_NAME')); --- REQUIRED!!!
         */
        public function qdetails(){
            try{
                $tbl = self::prepArgs(1, func_get_args());
                if($tbl != null){
                    $qry = $this->db->get($tbl);
                    if($qry->num_rows() > 0){
                        return $qry->row_array();
                    }else{
                        $this->msg .= 'There were no records found, for the query performed<br />' . $this->db->_error_message();
                        return null;
                    }
                }else{
                    $this->msg .= 'There were no records found, for the query performed<br />' . $this->msg;
                    return null;
                }
            }catch(Exception $ex){
                $this->msg .= $ex->getMessage();
                return null;
            }
        }

        // setup db insertion, return BOOLEAN
        /*
         * USAGE - the only array parameter required is the 'table_name', and 'insert'
            self::qinsert(array('insert'=>array('DB_FIELD_NAME'=>'VALUE',), --- can have as many of these field/value items as you need
                         'validations'=>array(array('field'=>'FORM_FIELD', 'label'=>'FORM_FIELD_LABEL', 'validation'=>'VALIDATION - See CI Docs'),), --- can have as many of these field/value/validation arrays as you need
                         'custom_message'=>'TO OVERRIDE THE INTERNAL ERROR MESSAGE RETURN',
                         'table_name'=>'TABLE_NAME')); --- REQUIRED!!!
         */
        public function qinsert(){
            try{
                return self::prepArgs(2, func_get_args());
            }catch(Exception $ex){
                $this->msg .= $ex->getMessage();
                return FALSE;
            }
        }

        // setup db edit, return BOOLEAN
        /*
         * USAGE - the only array parameter required is the 'table_name', and a where or like clause
            self::qedit(array('update'=>array('DB_FIELD_NAME'=>'VALUE',), --- can have as many of these field/value items as you need
                         'where'=>array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE'),), --- can have as many of these field/value arrays as you need
                         'or_where'=>array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE'),), --- can have as many of these field/value arrays as you need 
                         'where_in'=>array(array('field'=>'FIELD_NAME', 'value'=>array('VAL1', 'VAL2', etc...)),), --- can have as many of these field/value arrays as you need, value needs to be an array
                         'or_where_in'=>array(array('field'=>'FIELD_NAME', 'value'=>array('VAL1', 'VAL2', etc...)),), --- can have as many of these field/value arrays as you need, value needs to be an array
                         'where_not_in'=>array(array('field'=>'FIELD_NAME', 'value'=>array('VAL1', 'VAL2', etc...)),), --- can have as many of these field/value arrays as you need, value needs to be an array
                         'or_where_not_in'=>array(array('field'=>'FIELD_NAME', 'value'=>array('VAL1', 'VAL2', etc...)),), --- can have as many of these field/value arrays as you need, value needs to be an array
                         'like'=>array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE', 'wildcard'=>'before/after/both/none'),), --- can have as many of these field/value arrays as you need, wildcard is optional
                         'or_like'array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE', 'wildcard'=>'before/after/both/none'),), --- can have as many of these field/value arrays as you need, wildcard is optional
                         'not_like'array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE', 'wildcard'=>'before/after/both/none'),), --- can have as many of these field/value arrays as you need, wildcard is optional
                         'or_not_like'array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE', 'wildcard'=>'before/after/both/none'),), --- can have as many of these field/value arrays as you need, wildcard is optional
                         'group_by'=>array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE'),), --- can have as many of these field/value arrays as you need
                         'having'=>array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE'),), --- can have as many of these field/value arrays as you need
                         'or_having'=>array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE'),), --- can have as many of these field/value arrays as you need
                         'join'=>array(array('table'=>'TABLE_NAME_TO_JOIN', 'on'=>'EX: a.field1 = b.field2', 'direction'=>'left/right/outer/inner/left outer/right outer'),), --- can have as many of these table/on/direction arrays as you need
                         'validations'=>array(array('field'=>'FORM_FIELD', 'label'=>'FORM_FIELD_LABEL', 'validation'=>'VALIDATION - See CI Docs'),), --- can have as many of these field/value/validation arrays as you need
                         'custom_message'=>'TO OVERRIDE THE INTERNAL ERROR MESSAGE RETURN',
                         'table_name'=>'TABLE_NAME')); --- REQUIRED!!!
         */
        public function qupdate(){
            try{
                return self::prepArgs(3, func_get_args());
            }catch(Exception $ex){
                $this->msg .= $ex->getMessage();
                return FALSE;
            }
        }

        // setup db delete, return BOOLEAN
        /*
         * USAGE - the only array parameter required is the 'table_name', and a where or like clause
            self::qdelete(array('where'=>array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE'),), --- can have as many of these field/value arrays as you need
                         'or_where'=>array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE'),), --- can have as many of these field/value arrays as you need 
                         'where_in'=>array(array('field'=>'FIELD_NAME', 'value'=>array('VAL1', 'VAL2', etc...)),), --- can have as many of these field/value arrays as you need, value needs to be an array
                         'or_where_in'=>array(array('field'=>'FIELD_NAME', 'value'=>array('VAL1', 'VAL2', etc...)),), --- can have as many of these field/value arrays as you need, value needs to be an array
                         'where_not_in'=>array(array('field'=>'FIELD_NAME', 'value'=>array('VAL1', 'VAL2', etc...)),), --- can have as many of these field/value arrays as you need, value needs to be an array
                         'or_where_not_in'=>array(array('field'=>'FIELD_NAME', 'value'=>array('VAL1', 'VAL2', etc...)),), --- can have as many of these field/value arrays as you need, value needs to be an array
                         'like'=>array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE', 'wildcard'=>'before/after/both/none'),), --- can have as many of these field/value arrays as you need, wildcard is optional
                         'or_like'array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE', 'wildcard'=>'before/after/both/none'),), --- can have as many of these field/value arrays as you need, wildcard is optional
                         'not_like'array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE', 'wildcard'=>'before/after/both/none'),), --- can have as many of these field/value arrays as you need, wildcard is optional
                         'or_not_like'array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE', 'wildcard'=>'before/after/both/none'),), --- can have as many of these field/value arrays as you need, wildcard is optional
                         'group_by'=>array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE'),), --- can have as many of these field/value arrays as you need
                         'having'=>array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE'),), --- can have as many of these field/value arrays as you need
                         'or_having'=>array(array('field'=>'FIELD_NAME', 'value'=>'FIELD_VALUE'),), --- can have as many of these field/value arrays as you need
                         'join'=>array(array('table'=>'TABLE_NAME_TO_JOIN', 'on'=>'EX: a.field1 = b.field2', 'direction'=>'left/right/outer/inner/left outer/right outer'),), --- can have as many of these table/on/direction arrays as you need
                         'custom_message'=>'TO OVERRIDE THE INTERNAL ERROR MESSAGE RETURN',
                         'table_name'=>'TABLE_NAME')); --- REQUIRED!!!
         */
        public function qdelete(){
            try{
                return self::prepArgs(4, func_get_args());
            }catch(Exception $ex){
                $this->msg .= $ex->getMessage();
                return FALSE;
            }
        }

        // get the last inserted id - only valid on inserts
        public function last_insert_id(){
            try{
                return $this->last_id;
            }catch(Exception $ex){
                $this->msg .= $ex->getMessage();
                return null;
            }
        }

        // return a message if any of the returns above are invalid/false/errored out -- a specififed custom_message will override this
        public function message(){
            return ($this->custom_msg != null) ? $this->custom_msg : $this->msg;
        }

        // return a number of records based on the query run only valid on the selects
        public function fullrecordcount(){
            try{
                return $this->full_qry_count;
            }catch(Exception $ex){
                $this->msg .= $ex->getMessage();
                return null;
            }
        }

        // return the pagination links -- only valid for qlist
        public function paginator($base_url, $per_page, $total_rows, $num_links, $uri_segment, $aclass = null){
            try{
                $config['base_url'] = $base_url;
                $config['per_page'] = $per_page;
                $config['total_rows'] = $total_rows;
                $config['num_links'] = $num_links;
                $config['first_link'] = '<span class="fa fa-angle-double-left page_num"></span>';
                $config['last_link'] = '<span class="fa fa-angle-double-right page_num"></span>';
                $config['cur_tag_open'] = '<span class="page_num bold">';
                $config['cur_tag_close'] = '</span>';
                $config['next_link'] = '<span class="fa fa-angle-right page_num"></span>';
                $config['prev_link'] = '<span class="fa fa-angle-left page_num"></span>';
                $config['uri_segment'] = $uri_segment;
                $config['num_tag_open'] = '<span class="page_num">';
                $config['num_tag_close'] = '</span>';
                if($aclass != null){
                    $config['anchor_class'] = 'class="' . $aclass . '" ';   
                }
                $this->pagination->initialize($config);
                return $this->pagination->create_links();
            }catch(Exception $ex){
                $this->msg .= $ex->getMessage();
                return null;
            }
        }

        // setup all possible arguments for all of the above, return name of the table from the arguments
        private function prepArgs($which, $args){
            if($args){
                try{
                    if(array_key_exists('custom_message', $args[0])){
                        $this->custom_msg = $args[0]['custom_message'];
                    }
                    switch($which){
                        case 1: // select
                            return self::setupSelect($args);
                            break;
                        case 2: // insert
                            return self::setupInsert($args);
                            break;
                        case 3: // update
                            return self::setupUpdate($args);
                            break;
                        case 4: // delete
                            return self::setupDelete($args);
                            break;
                    }
                }catch(Exception $ex){
                    $this->msg .= $ex->getMessage();
                }
            }else{
                $this->msg .= 'You have not passed in any arguments to your query, please have a look over your code.';
            }
        }

        // setup our edit
        private function setupUpdate($args){
            try{
                // setup our arguments
                $this->setupArgs($args);
                $data = array();
                $tname = '';
                $valid = TRUE;
                // needs to include at least some kind of filter
                if(!$this->validateFilters($args)){
                    $valid = FALSE;
                    $this->msg .= 'You need to have at least one filter, in order to update your record.';
                }
                // array of data to insert: required
                if(array_key_exists('update', $args[0])){
                    $data = $args[0]['update'];
                }else{
                    $valid = FALSE;
                    $this->msg .= 'You need to specify field/value pairs of data to update.';
                }
                // table name: required
                if(array_key_exists('table_name', $args[0])){
                    $tname = $args[0]['table_name'];
                }else{
                    $valid = FALSE;
                    $this->msg .= 'You need to specify a table name to update.';
                }
                // setup our validations
                $valid = $this->setupValidations($args);
                if($valid){
                    $this->db->update($tname, $data);
                    if(!($this->db->affected_rows() > 0)){
                        $this->msg .= $this->db->_error_message();
                        return FALSE;
                    }else{
                        return TRUE;
                    }
                }else{
                    return $valid;  
                }
            }catch(Exception $ex){
                $this->msg .= $ex->getMessage();
                return FALSE;
            }
        }

        // setup our delete
        private function setupDelete($args){
            try{
                // setup our arguments
                $this->setupArgs($args);
                // needs to include at least some kind of filter
                if(!$this->validateFilters($args)){
                    $this->msg .= 'You need to have at least one filter, in order to delete your record.';
                    return null;
                }
                // table name: required
                if(array_key_exists('table_name', $args[0])){
                    if($args[0]['table_name'] != null){
                        $this->db->delete($args[0]['table_name']);
                        if($this->db->affected_rows() <= 0){
                            $this->msg = 'There was an issue removing that record.<br />' . $this->db->_error_message();
                            return FALSE;
                        }
                    }else{
                        $this->msg = 'There was an issue removing that record.<br />' . $this->msg;
                        return FALSE;
                    }
                }else{
                    $this->msg .= 'A table name is required in order to delete a record from it';
                    return FALSE;
                }
                return TRUE;
            }catch(Exception $ex){
                $this->msg .= $ex->getMessage();
                return FALSE;
            }
        }

        // setup(and run) our insert
        private function setupInsert($args){
            try{
                $data = array();
                $tname = '';
                $valid = TRUE;
                // array of data to insert: required
                if(array_key_exists('insert', $args[0])){
                    $data = $args[0]['insert'];
                }else{
                    $valid = FALSE;
                    $this->msg .= 'You need to specify field/value pairs of data to insert.';
                }
                // table name: required
                if(array_key_exists('table_name', $args[0])){
                    $tname = $args[0]['table_name'];
                }else{
                    $valid = FALSE;
                    $this->msg .= 'You need to specify a table name to insert data into.';
                }
                // setup our validations
                $valid = $this->setupValidations($args);
                if($valid){
                    $this->db->insert($tname, $data);
                    if(!($this->db->affected_rows() > 0)){
                        $this->msg .= $this->db->_error_message();
                        return FALSE;
                    }else{
                        $this->last_id = $this->db->insert_id();
                        return TRUE;
                    }
                }else{
                    return $valid;  
                }
            }catch(Exception $ex){
                $this->msg .= $ex->getMessage();
                return FALSE;
            }
        }

        // setup our select
        private function setupSelect($args){
            try{
                $tname = null;
                // select field names
                if(array_key_exists('select', $args[0])){
                    $this->db->select($args[0]['select']);
                }
                // setup our arguments
                $this->setupArgs($args);
                // setup our validations
                $valid = $this->setupValidations($args);
                // table name: required
                if($valid){
                    if(array_key_exists('table_name', $args[0])){
                        $tname = $args[0]['table_name'];
                    }else{
                        $this->msg .= 'A table name is required to select any records from.';
                    }
                }
                return $tname;
            }catch(Exception $ex){
                $this->msg .= $ex->getMessage();
                return null;
            }
        }

        // setup validations
        private function setupValidations($args){
            if(array_key_exists('validations', $args[0])){
                $v = $args[0]['validations'];
                $vCt = count($v);
                for($vv = 0; $vv < $vCt; ++$vv){
                    $this->form_validation->set_rules($v[$vv]['field'], $v[$vv]['label'], $v[$vv]['validation']);
                }
                if ($this->form_validation->run() === FALSE){
                    $this->msg .= validation_errors();
                    return FALSE;
                }
                return TRUE;
            }
            return TRUE;
        }

        // setup the arguments
        private function setupArgs($args){
            // where clause(s)
            if(array_key_exists('where', $args[0])){
                $w = $args[0]['where'];
                $wCt = count($w);
                for($ww = 0; $ww < $wCt; ++$ww){
                    if($w[$ww]['value']){
                        $this->db->where($w[$ww]['field'], $w[$ww]['value']);
                    }
                }
            }
            // or_where clause(s)
            if(array_key_exists('or_where', $args[0])){
                $w = $args[0]['or_where'];
                $wCt = count($w);
                for($ww = 0; $ww < $wCt; ++$ww){
                    if($w[$ww]['value']){
                        $this->db->or_where($w[$ww]['field'], $w[$ww]['value']);
                    }
                }
            }
            // where_in clause(s)
            if(array_key_exists('where_in', $args[0])){
                $w = $args[0]['where_in'];
                $wCt = count($w);
                for($ww = 0; $ww < $wCt; ++$ww){
                    if($w[$ww]['value']){
                        $this->db->where_in($w[$ww]['field'], $w[$ww]['value']);
                    }
                }
            }
            // or_where_in clause(s)
            if(array_key_exists('or_where_in', $args[0])){
                $w = $args[0]['or_where_in'];
                $wCt = count($w);
                for($ww = 0; $ww < $wCt; ++$ww){
                    if($w[$ww]['value']){
                        $this->db->or_where_in($w[$ww]['field'], $w[$ww]['value']);
                    }
                }
            }
            // where_not_in clause(s)
            if(array_key_exists('where_not_in', $args[0])){
                $w = $args[0]['where_not_in'];
                $wCt = count($w);
                for($ww = 0; $ww < $wCt; ++$ww){
                    if($w[$ww]['value']){
                        $this->db->where_not_in($w[$ww]['field'], $w[$ww]['value']);
                    }
                }
            }
            // or_where_not_in clause(s)
            if(array_key_exists('or_where_not_in', $args[0])){
                $w = $args[0]['or_where_not_in'];
                $wCt = count($w);
                for($ww = 0; $ww < $wCt; ++$ww){
                    if($w[$ww]['value']){
                        $this->db->or_where_not_in($w[$ww]['field'], $w[$ww]['value']);
                    }
                }
            }
            // like clause(s)
            if(array_key_exists('like', $args[0])){
                $l = $args[0]['like'];
                $lCt = count($l);
                for($ll = 0; $ll < $lCt; ++$ll){
                    if($l[$ll]['value']){
                        $this->db->like($l[$ll]['field'], $l[$ll]['value'], (isset($l[$ll]['wildcard'])) ? $l[$ll]['wildcard'] : null);
                    }
                }
            }
            // or_like clause(s)
            if(array_key_exists('or_like', $args[0])){
                $l = $args[0]['or_like'];
                $lCt = count($l);
                for($ll = 0; $ll < $lCt; ++$ll){
                    if($l[$ll]['value']){
                        $this->db->or_like($l[$ll]['field'], $l[$ll]['value'], (isset($l[$ll]['wildcard'])) ? $l[$ll]['wildcard'] : null);
                    }
                }
            }
            // not_like clause(s)
            if(array_key_exists('not_like', $args[0])){
                $l = $args[0]['not_like'];
                $lCt = count($l);
                for($ll = 0; $ll < $lCt; ++$ll){
                    if($l[$ll]['value']){
                        $this->db->not_like($l[$ll]['field'], $l[$ll]['value'], (isset($l[$ll]['wildcard'])) ? $l[$ll]['wildcard'] : null);
                    }
                }
            }
            // or_not_like clause(s)
            if(array_key_exists('or_not_like', $args[0])){
                $l = $args[0]['or_not_like'];
                $lCt = count($l);
                for($ll = 0; $ll < $lCt; ++$ll){
                    if($l[$ll]['value']){
                        $this->db->or_not_like($l[$ll]['field'], $l[$ll]['value'], (isset($l[$ll]['wildcard'])) ? $l[$ll]['wildcard'] : null);
                    }
                }
            }
            // group_by clause(s)
            if(array_key_exists('group_by', $args[0])){
                $g = $args[0]['group_by'];
                $gCt = count($g);
                for($gg = 0; $gg < $gCt; ++$gg){
                    if($g[$gg]['value']){
                        $this->db->group_by($g[$gg]['field'], $g[$gg]['value']);
                    }
                }
            }
            // distinct flag
            if(array_key_exists('distinct', $args[0])){
                if($args[0]['distinct'] == TRUE){
                    $this->db->distinct();
                }
            }
            // having clause(s)
            if(array_key_exists('having', $args[0])){
                $h = $args[0]['having'];
                $hCt = count($h);
                for($hh = 0; $hh < $hCt; ++$hh){
                    if($h[$hh]['value']){
                        $this->db->having($h[$hh]['field'], $h[$hh]['value']);
                    }
                }
            }
            // or_having clause(s)
            if(array_key_exists('or_having', $args[0])){
                $h = $args[0]['or_having'];
                $hCt = count($h);
                for($hh = 0; $hh < $hCt; ++$hh){
                    if($h[$hh]['value']){
                        $this->db->or_having($h[$hh]['field'], $h[$hh]['value']);
                    }
                }
            }
            // order_by clause(s)
            if(array_key_exists('order_by', $args[0])){
                $o = $args[0]['order_by'];
                $oCt = count($o);
                for($oo = 0; $oo < $oCt; ++$oo){
                    $this->db->order_by($o[$oo]['field'], ($o[$oo]['direction']) ? $o[$oo]['direction'] : null);
                }
            }
            // join clause(s)
            if(array_key_exists('join', $args[0])){
                $j = $args[0]['join'];
                $jCt = count($j);
                for($jj = 0; $jj < $jCt; ++$jj){
                    $this->db->join($j[$jj]['table'], $j[$jj]['on'], $j[$jj]['direction']);
                }
            }
            // limit
            if(array_key_exists('limit', $args[0])){
                $this->db->limit($args[0]['limit']['limit'], $args[0]['limit']['offset']);
            }
        }

        // validate our filters
        private function validateFilters($args){
            return (!array_key_exists('where', $args[0]) || 
                       !array_key_exists('or_where', $args[0]) || 
                       !array_key_exists('where_in', $args[0]) || 
                       !array_key_exists('or_where_in', $args[0]) || 
                       !array_key_exists('where_not_in', $args[0]) || 
                       !array_key_exists('or_where_not_in', $args[0]) || 
                       !array_key_exists('like', $args[0]) || 
                       !array_key_exists('or_like', $args[0]) || 
                       !array_key_exists('not_like', $args[0]) || 
                       !array_key_exists('or_not_like', $args[0]));
        }

    }

?>
\$\endgroup\$
3
  • \$\begingroup\$ I guess no comments or answers is a good thing? \$\endgroup\$
    – Kevin
    Apr 11, 2014 at 19:46
  • \$\begingroup\$ pick out a portion of the code that you think could use some improvement and post that as a question. where is too much code here to be reviewed in whole. \$\endgroup\$
    – Malachi
    May 5, 2014 at 18:04
  • \$\begingroup\$ Looking for the comments in this class as a whole. To break it out into seperate chunked out questions is not conducive to the class... \$\endgroup\$
    – Kevin
    May 5, 2014 at 18:09

1 Answer 1

1
\$\begingroup\$

The only thing that I can see at a quick glance is that you need to give your variables more meaningful names so that when you come back on these later you will know exactly what their purpose in life is.

I will take another look later.

\$\endgroup\$
2
  • \$\begingroup\$ appreciate it. I know it's long, and I apologize for that, but it's got a big job... ;) \$\endgroup\$
    – Kevin
    May 5, 2014 at 18:24
  • \$\begingroup\$ @o7thWebDesign, sorry I didn't get a chance last night. \$\endgroup\$
    – Malachi
    May 6, 2014 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.