Skip to content
Browse files

Adding category parameter

  • Loading branch information...
1 parent 88c06c2 commit 63a524053f77223350e0526c64d337ffd198dc17 @jamierumbelow committed Jun 9, 2011
Showing with 52 additions and 6 deletions.
  1. +33 −6 third_party/blog/libraries/blog.php
  2. +19 −0 third_party/blog/models/blog_model.php
View
39 third_party/blog/libraries/blog.php
@@ -315,6 +315,7 @@ public function entries($template_data) {
$paginate_once = $this->_param('paginate_once');
$per_page = $this->_param('per_page');
$pagination_segment = $this->_param('pagination_segment');
+ $category = $this->_param('category');
$category_segment = $this->_param('category_segment');
$category_url_title = $this->_param('category_url_title');
$excerpt_words = $this->_param('excerpt_words');
@@ -346,20 +347,20 @@ public function entries($template_data) {
||
($category_url_title)) {
if ($category_url_title) {
- $category = $this->mojo->blog_model->where('url_name', $category_url_title)->category();
+ $cat = $this->mojo->blog_model->where('url_name', $category_url_title)->category();
} else {
- $category = $this->mojo->blog_model->where('url_name', $this->mojo->uri->segment((int)$category_segment))->category();
+ $cat = $this->mojo->blog_model->where('url_name', $this->mojo->uri->segment((int)$category_segment))->category();
}
if ($category) {
- $this->mojo->blog_model->where('category_id', $category->id);
+ $this->mojo->blog_model->where('category_id', $cat->id);
$paginate = FALSE;
$cond['category_page'] = TRUE;
- $category_name = $category->name;
- $category_url_name = $category->url_name;
- $category_id = $category->id;
+ $category_name = $cat->name;
+ $category_url_name = $cat->url_name;
+ $category_id = $cat->id;
} else {
$category_name = '';
$category_url_name = '';
@@ -387,6 +388,32 @@ public function entries($template_data) {
$this->mojo->blog_model->where('status', 'published');
}
+ // Category
+ if ($category) {
+ // Get rid of 'not '
+ $not = FALSE;
+ $first = TRUE;
+ if (substr($category, 0, 4) == 'not ') { $category = substr($category, 4); $not = TRUE; }
+
+ // Multiple categories
+ $categories = explode('|', $category);
+
+ foreach ($categories as $category) {
+ $cat_id = $this->mojo->blog_model->isolate()->where('url_name', $category)->category()->id;
+ $this->mojo->blog_model->unisolate();
+
+ if ($not) {
+ $this->mojo->blog_model->where('category_id !=', $cat_id);
+ } else {
+ if ($first) {
+ $this->mojo->blog_model->where('category_id', $cat_id);
+ } else {
+ $this->mojo->blog_model->or_where('category_id', $cat_id);
+ }
+ }
+ }
+ }
+
// Orderby and sort
$orderby = ($orderby) ? $orderby : 'date';
$sort = ($sort) ? strtoupper($sort) : 'DESC';
View
19 third_party/blog/models/blog_model.php
@@ -12,6 +12,7 @@
class Blog_model extends CI_Model {
public $validation_errors = '';
+ private $_old_query_stuff = array();
public function __construct() {
parent::__construct();
@@ -252,6 +253,24 @@ public function categories_dropdown() {
return $dropdown;
}
+ public function isolate() {
+ // Cache the CodeIgniter ActiveRecord values
+ $ar_values = array(
+ 'ar_select', 'ar_distinct', 'ar_from', 'ar_join', 'ar_where', 'ar_like',
+ 'ar_groupby', 'ar_having', 'ar_limit', 'ar_offset', 'ar_order', 'ar_orderby',
+ 'ar_set', 'ar_wherein', 'ar_aliased_tables', 'ar_store_array'
+ );
+ foreach ($ar_values as $val) { $this->_old_query_stuff[$val] = $this->db->$val; $this->db->$val = null; }
+
+ // Return $this
+ return $this;
+ }
+
+ public function unisolate() {
+ // Restore the AR values
+ foreach ($this->_old_query_stuff as $key => $val) { $this->db->$key = $val; }
+ }
+
public function __call($method, $arguments) {
if (method_exists($this->db, $method)) {
call_user_func_array(array($this->db, $method), $arguments);

0 comments on commit 63a5240

Please sign in to comment.
Something went wrong with that request. Please try again.