Advertisement
Guest User

Untitled

a guest
Mar 4th, 2014
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.82 KB | None | 0 0
  1. <?php if(!defined('BASEPATH')) { header('Status: 404 Not Found'); exit(); }
  2.  
  3. class MY_Model extends CI_Model {
  4.     public $_select = array();
  5.     public $_join = array();
  6.     public $_where = array();
  7.     public $_like = array();
  8.     public $_limit = NULL;
  9.     public $_offset = NULL;
  10.     public $_order_by = NULL;
  11.     public $_order = NULL;
  12.     public $_group_by = NULL;
  13.     public $_having = array();
  14.    
  15.     protected $_hooks;
  16.     protected $response = NULL;
  17.    
  18.     public function __construct()
  19.     {
  20.         parent::__construct();
  21.  
  22.         if ( ! empty($this->models))
  23.         {
  24.             foreach ($this->models as $model)
  25.             {
  26.                 $this->load->model($model);
  27.             }
  28.         }
  29.     }
  30.    
  31.     public function select($select)
  32.     {
  33.         $this->trigger_events('select');
  34.  
  35.         $this->_select[] = $select;
  36.  
  37.         return $this;
  38.     }
  39.    
  40.     public function join($join, $on, $type = NULL)
  41.     {
  42.         $this->trigger_events('join');
  43.  
  44.         if (!is_array($join))
  45.         {
  46.             $join = array($join, $on, $type);
  47.         }
  48.  
  49.         array_push($this->_join, $join);
  50.  
  51.         return $this;
  52.     }
  53.    
  54.     public function where($where, $value = NULL)
  55.     {
  56.         $this->trigger_events('where');
  57.  
  58.         if (!is_array($where))
  59.         {
  60.             $where = array($where => $value);
  61.         }
  62.  
  63.         array_push($this->_where, $where);
  64.  
  65.         return $this;
  66.     }
  67.  
  68.     public function like($like, $value = NULL)
  69.     {
  70.         $this->trigger_events('like');
  71.  
  72.         if (!is_array($like))
  73.         {
  74.             $like = array($like => $value);
  75.         }
  76.  
  77.         array_push($this->_like, $like);
  78.  
  79.         return $this;
  80.     }
  81.    
  82.     public function limit($limit, $offset = NULL)
  83.     {
  84.         $this->trigger_events('limit');
  85.         $this->_limit = $limit;
  86.        
  87.         if($offset != NULL)
  88.         {  
  89.             $this->trigger_events('offset');
  90.             $this->_offset = $offset;
  91.         }
  92.  
  93.         return $this;
  94.     }
  95.  
  96.     public function order_by($by, $order='desc')
  97.     {
  98.         $this->trigger_events('order_by');
  99.  
  100.         $this->_order_by = $by;
  101.         $this->_order    = $order;
  102.  
  103.         return $this;
  104.     }
  105.  
  106.     public function group_by($by)
  107.     {
  108.         $this->trigger_events('group_by');
  109.  
  110.         $this->_group_by = $by;
  111.  
  112.         return $this;
  113.     }
  114.  
  115.     public function having($having, $value = NULL)
  116.     {
  117.         $this->trigger_events('having');
  118.  
  119.         if (!is_array($having))
  120.         {
  121.             $having = array($having => $value);
  122.         }
  123.  
  124.         array_push($this->_having, $having);
  125.  
  126.         return $this;
  127.     }
  128.  
  129.     public function row()
  130.     {
  131.         $this->trigger_events('row');
  132.  
  133.         $row = $this->response->row();
  134.         $this->response->free_result();
  135.  
  136.         return $row;
  137.     }
  138.  
  139.     public function result()
  140.     {
  141.         $this->trigger_events('result');
  142.  
  143.         $result = $this->response->result();
  144.         $this->response->free_result();
  145.  
  146.         return $result;
  147.     }
  148.  
  149.     public function num_rows()
  150.     {
  151.         $this->trigger_events(array('num_rows'));
  152.  
  153.         $result = $this->response->num_rows();
  154.         $this->response->free_result();
  155.  
  156.         return $result;
  157.     }
  158.    
  159.     public function get_list() {
  160.         if (isset($this->_select) && !empty($this->_select))
  161.         {
  162.             foreach ($this->_select as $select)
  163.             {
  164.                 $this->db->select($select);
  165.             }
  166.  
  167.             $this->_select = array();
  168.         }
  169.         else
  170.         {
  171.             $this->db->select($this->_default_select);
  172.         }
  173.        
  174.         if (isset($this->_join) && !empty($this->_join))
  175.         {
  176.             foreach ($this->_join as $join)
  177.             {
  178.                 $this->db->join($join[0], $join[1], $join[2]);
  179.             }
  180.  
  181.             $this->_join = array();
  182.         }
  183.         else
  184.         {
  185.             $this->db->join($this->_default_join[0], $this->_default_join[1], $this->_default_join[2]);
  186.         }
  187.  
  188.         $this->trigger_events('extra_where');
  189.  
  190.         if (isset($this->_where) && !empty($this->_where))
  191.         {
  192.             foreach ($this->_where as $where)
  193.             {
  194.                 $this->db->where($where);
  195.             }
  196.  
  197.             $this->_where = array();
  198.         }
  199.  
  200.         if (isset($this->_like) && !empty($this->_like))
  201.         {
  202.             foreach ($this->_like as $like)
  203.             {
  204.                 $this->db->or_like($like);
  205.             }
  206.  
  207.             $this->_like = array();
  208.         }
  209.  
  210.         if (isset($this->_limit) && isset($this->_offset))
  211.         {
  212.             $this->db->limit($this->_limit, $this->_offset);
  213.  
  214.             $this->_limit  = NULL;
  215.             $this->_offset = NULL;
  216.         }
  217.         else if (isset($this->_limit))
  218.         {
  219.             $this->db->limit($this->_limit);
  220.  
  221.             $this->_limit  = NULL;
  222.         }
  223.  
  224.         if (isset($this->_order_by) && isset($this->_order))
  225.         {
  226.             $this->db->order_by($this->_order_by, $this->_order);
  227.  
  228.             $this->_order    = NULL;
  229.             $this->_order_by = NULL;
  230.         }
  231.  
  232.         if (isset($this->_group_by))
  233.         {
  234.             $this->db->group_by($this->_group_by);
  235.  
  236.             $this->_group_by = NULL;
  237.         }
  238.  
  239.         if (isset($this->_having) && !empty($this->_having))
  240.         {
  241.             foreach ($this->_having as $having)
  242.             {
  243.                 $this->db->having($having);
  244.             }
  245.  
  246.             $this->_having = array();
  247.         }
  248.        
  249.         $this->response = $this->db->get($this->table);
  250.        
  251.         return $this;
  252.     }
  253.    
  254.     /**
  255.      * Hooks
  256.      **/
  257.     public function set_hook($event, $name, $class, $method, $arguments)
  258.     {
  259.         $this->_hooks->{$event}[$name] = new stdClass;
  260.         $this->_hooks->{$event}[$name]->class     = $class;
  261.         $this->_hooks->{$event}[$name]->method    = $method;
  262.         $this->_hooks->{$event}[$name]->arguments = $arguments;
  263.     }
  264.  
  265.     public function remove_hook($event, $name)
  266.     {
  267.         if (isset($this->_hooks->{$event}[$name]))
  268.         {
  269.             unset($this->_hooks->{$event}[$name]);
  270.         }
  271.     }
  272.  
  273.     public function remove_hooks($event)
  274.     {
  275.         if (isset($this->_hooks->$event))
  276.         {
  277.             unset($this->_hooks->$event);
  278.         }
  279.     }
  280.  
  281.     protected function _call_hook($event, $name)
  282.     {
  283.         if (isset($this->_hooks->{$event}[$name]) && method_exists($this->_hooks->{$event}[$name]->class, $this->_hooks->{$event}[$name]->method))
  284.         {
  285.             $hook = $this->_hooks->{$event}[$name];
  286.  
  287.             return call_user_func_array(array($hook->class, $hook->method), $hook->arguments);
  288.         }
  289.  
  290.         return FALSE;
  291.     }
  292.  
  293.     public function trigger_events($events)
  294.     {
  295.         if (is_array($events) && !empty($events))
  296.         {
  297.             foreach ($events as $event)
  298.             {
  299.                 $this->trigger_events($event);
  300.             }
  301.         }
  302.         else
  303.         {
  304.             if (isset($this->_hooks->$events) && !empty($this->_hooks->$events))
  305.             {
  306.                 foreach ($this->_hooks->$events as $name => $hook)
  307.                 {
  308.                     $this->_call_hook($events, $name);
  309.                 }
  310.             }
  311.         }
  312.     }
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement