cxf_tables = array( 'categories' =>'forum_categories', 'forums' => 'forum_forums', 'topics' => 'forum_topics', 'posts' => 'forum_posts', 'users' => 'users' ); //$this->db->dbprefix($this->cxf_tables['categories']) //print_r($this->cxf_tables); } /******************** Categories ********************/ /* * Fetch All Categories */ function fetch_categories() { return $this->db->get($this->cxf_tables['categories']); } /* * Insert/Update a Category */ function insert_category($name=null, $priority=0, $id=null) { //If a name is NOT set if(!$name) { return null; } //Create the array $data = array('name' => $name, 'priority' => $priority); //Is it an update or what? if($id) { return $this->db->update($this->cxf_tables['categories'], $data, $id); } else { return $this->db->insert($this->cxf_tables['categories'], $data); } } /* * Delete a Category */ function delete_category($id=null) { if(!$id) { return null; } return $this->db->delete($this->cxf_tables['categories'], array('id' => $id)); } /******************** Forums ********************/ /* * Fetch All Forums * * @param $category ONLY from a given category * @return object */ function fetch_forums($category=null) { //Select the Cat, Forum, Desc, and Priority $this->db->select($this->db->dbprefix($this->cxf_tables['categories']) . '.name as category, ' . $this->db->dbprefix($this->cxf_tables['forums']). '.id, ' . $this->db->dbprefix($this->cxf_tables['forums']). '.name, ' . $this->db->dbprefix($this->cxf_tables['forums']). '.description, ' . $this->db->dbprefix($this->cxf_tables['forums']). '.priority, ' . 'COUNT('. $this->db->dbprefix($this->cxf_tables['topics']). '.id) as topics,' . $this->db->dbprefix($this->cxf_tables['topics']). '.title as topic_title, ' . $this->db->dbprefix($this->cxf_tables['topics']). '.id as topic_id, ' . $this->db->dbprefix($this->cxf_tables['users']). '.username as user' , FALSE); //Get the Categories table $this->db->join($this->cxf_tables['categories'], $this->cxf_tables['categories']. '.id = ' . $this->cxf_tables['forums']. '.category_id', 'left'); //Get the Topics Table $this->db->join($this->cxf_tables['topics'], $this->cxf_tables['forums']. '.id = ' . $this->cxf_tables['topics']. '.forum_id', 'left'); //Get the users Table $this->db->join($this->cxf_tables['users'], $this->cxf_tables['topics']. '.user_id = ' . $this->cxf_tables['users']. '.id', 'left'); //Sort by the Cat then the forum (priority) $this->db->order_by($this->cxf_tables['categories']. '.priority ASC, ' . $this->db->dbprefix($this->cxf_tables['forums']). '.priority ASC'); //Group by Forum ID $this->db->group_by($this->cxf_tables['forums']. '.id'); //If we only want forums from a certain category if($category) { $this->db->where('category_id', $category); } //Don't show forums that are linked to non-existent categories $this->db->where($this->cxf_tables['categories']. '.name IS NOT NULL'); //Fetch it $query = $this->db->get($this->cxf_tables['forums']); //If nothing was found if ($query->num_rows() < 1) { return; } return $query; } /* * Insert/Update a Forum */ /* $data = array( 'category_id' => 1, 'name' => 'Example', 'description' => 'about it', 'priority' => 0, ) */ function insert_forum($data, $id=null) { //If these are NOT set if(empty($data['category_id']) || empty($data['name'])) { return null; } //Set to ZERO if(empty($data['priority'])) { $data['priority'] = 0; } //Make sure an ID wasn't passed if(isset($data['id'])) { unset($data['id']); } //Is it an update or what? if($id) { return $this->db->update($this->cxf_tables['forums'], $data, $id); } else { return $this->db->insert($this->cxf_tables['forums'], $data); } } /* * Delete a Forum */ function delete_forum($id=null) { if(!$id) { return null; } return $this->db->delete($this->cxf_tables['forums'], array('id' => $id)); } /******************** Topics & Posts ********************/ /* * Search / Fetch Matching Posts or Topics */ /* Optional parameters $data = array( 'parent_id' => null, 'user_id' => null, 'status' => 1, 'order_by' => 'date desc', 'limit' => 10, 'offset' => 0, 'like' => array('column' => 'value'), 'count' => null ); */ //Fetch Topics function fetch_topics($data=null) { return $this->__fetch('topics', $data); } //Fetch Posts function fetch_posts($data=null) { return $this->__fetch('posts', $data); } /* * Find Topics/Posts that match the data given */ function __fetch($type='topics', $data=null) { //Order by Date Descending $this->db->order_by((!empty($data['order_by']) ? $data['order_by'] : 'date desc')); //If a parent ID is given (for a topic or post) if(!empty($data['parent_id'])) { $this->db->where(($type == 'topics' ? 'forum' : 'topic'). '_id', $data['parent_id']); } //If a user ID is given if(!empty($data['user_id'])) { $this->db->where('user_id', $data['user_id']); } //@todo change this so that it doesn't limit to ONLY one type of status at a time //If a status is given - else use ACTIVE $this->db->where('status', (isset($data['status']) ? $data['status'] : 1)); //If a limit is given $this->db->limit( (!empty($data['limit']) ? $data['limit'] : 10), (isset($data['offset']) ? $data['offset'] : 0) ); //Are we searching for rows ONLY like a certain thing? if(!empty($data['like']) && is_array($data['like'])) { foreach($data['like'] as $col => $value) { $this->db->or_like($col, $value); } } //Is this just a count or what? if(!empty($data['count'])) { $this->db->from($this->cxf_tables[$type]); return $this->db->count_all_results(); } //Fetch all topics/posts $query = $this->db->get($this->cxf_tables[$type]); //If a row is NOT found if ($query->num_rows() < 1) { return null; } return $query; } /* * Insert/Update a Topic/Post */ /* $data = array( 'parent_id' => 1, 'user_id' => 1, 'title' => 'Hello World', 'text' => 'My post/topic text', 'date' => 1111111111, 'status' => 1 */ //Alias to make like easier function insert_post($data=null, $id=null) { return $this->__insert('posts', $data, $id); } //Alias to make like easier function insert_topic($data=null, $id=null) { return $this->__insert('topics', $data, $id); } /* * Create a Topic/Post */ function __insert($type='posts', $data=null, $id=null) { //Make sure each item is present foreach(array('parent_id', 'user_id', 'title', 'text') as $key) { if(empty($data[$key])) { return null; } } //Make sure an ID wasn't passed if(isset($data['id'])) { unset($data['id']); } //If no time is given - use current time if(empty($data['date'])) { $data['date'] = time(); } //Check to see if status is set if(empty($data['status'])) { $data['status'] = 0; } //If an ID was given - then this is an update if($id) { return $this->db->update($this->cxf_tables[$type], $data, array('id' => $id)); //Else it is an insert } else { return $this->db->insert($this->cxf_tables[$type], $data); } } /* * Delete a Topic */ function delete_topic($id=null) { if(!$id) { return null; } return $this->db->delete($this->cxf_tables['topics'], array('id' => $id)); } /* * Delete a Post */ function delete_post($id=null) { if(!$id) { return null; } return $this->db->delete($this->cxf_tables['posts'], array('id' => $id)); } } ?>