Created
April 26, 2014 17:27
-
-
Save escapeboy/11325935 to your computer and use it in GitHub Desktop.
CodeIgniter Event Library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
| /** | |
| * Logs Library | |
| * Loging some events from user activity | |
| * @author Nikola Katsarov | |
| * @website http://katsarov.biz | |
| */ | |
| class Events { | |
| public $event_table = 'event_log'; | |
| private function first_record() | |
| { | |
| $CI =& get_instance(); | |
| $CI->db->select_max('guest_id', 'last_guest_id'); | |
| $q = $CI->db->get($this->event_table); | |
| $last_record = $q->row()->last_guest_id; | |
| $CI->db->set('user_action', 'generate_guest_id'); | |
| $CI->db->set('action_date', 'NOW()', false); | |
| $CI->db->set('guest_id', $last_record + 1); | |
| $CI->db->set('ip_address', $CI->session->userdata('ip_address')); | |
| $CI->db->insert($this->event_table); | |
| $guest = $this->load('`id` = '.$CI->db->insert_id()); | |
| return $guest->guest_id; | |
| } | |
| public function save($action='unknown', $action_data=array()) | |
| { | |
| $CI =& get_instance(); | |
| if(!$CI->session->userdata('guest_id')){ | |
| $CI->session->set_userdata('guest_id', $this->first_record()); | |
| } | |
| $CI->db->set('user_action', $action); | |
| $CI->db->set('action_date', 'NOW()', false ); | |
| $CI->db->set('guest_id', $CI->session->userdata('guest_id')); | |
| $CI->db->set('ip_address', $CI->session->userdata('ip_address')); | |
| if($action_data){ | |
| $CI->db->set('action_data', '\''.json_encode($action_data).'\'', false); | |
| } | |
| if(!$event = $this->load('`guest_id` =\''.$CI->session->userdata('guest_id').'\' AND `user_action`=\''.$action.'\'')){ | |
| $CI->db->insert($this->event_table); | |
| }else{ | |
| $CI->db->where('id', $event->id); | |
| $CI->db->update($this->event_table); | |
| } | |
| } | |
| public function load($where='') | |
| { | |
| $CI =& get_instance(); | |
| if($where){ | |
| $CI->db->where($where); | |
| } | |
| $q = $CI->db->get($this->event_table); | |
| return $q->row(); | |
| } | |
| public function get_events($where='', $limit=false, $group=false) | |
| { | |
| $CI =& get_instance(); | |
| if($where){ | |
| $CI->db->where($where); | |
| } | |
| if($limit){ | |
| $CI->db->limit($limit); | |
| } | |
| if($group){ | |
| $CI->db->group_by('guest_id'); | |
| } | |
| $q = $CI->db->get($this->event_table); | |
| return $q->result(); | |
| } | |
| public function update_event($id, $data=array()) | |
| { | |
| $CI =& get_instance(); | |
| $CI->db->where('id', $id); | |
| $CI->db->update($this->event_table, $data); | |
| } | |
| public function delete_event($id) | |
| { | |
| $CI =& get_instance(); | |
| $CI->db->where('id', $id); | |
| $CI->db->delete($this->event_table); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment