Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed a bug in searching.

  • Loading branch information...
commit ddcbc675c12fc70c9db92f9c24b364dfe6ef8d62 1 parent 5866a1f
@ericbarnes ericbarnes authored
View
109 upload/system/68kb/modules/search/controllers/search.php
@@ -21,84 +21,84 @@
* @link http://68kb.com/user_guide/
*
*/
-class Search extends Front_Controller
+class Search extends Front_Controller
{
-
+
/**
* Constructor
*
* @return void
*/
- public function __construct()
+ public function __construct()
{
parent::__construct();
-
+
log_message('debug', 'Search Controller Initialized');
-
+
// Load the needed libs
$this->load->model('search_model');
$this->load->model('categories/categories_model');
$this->load->model('kb/articles_model');
$this->load->helper(array('form', 'url', 'html', 'date'));
}
-
+
// ------------------------------------------------------------------------
-
+
/**
* Show the search form.
*/
- public function index()
+ public function index()
{
$this->events->trigger('search/index');
-
+
$this->benchmark->mark('get_cats_for_select_start');
-
+
$this->load->library('categories/categories_library');
$cats = $this->categories_model->get_categories();
$this->categories_library->category_tree($cats);
$data['cats'] = $this->categories_library->get_categories();
-
+
$this->benchmark->mark('get_cats_for_select_end');
-
+
// Set parent breadcrumb
$this->template->set_breadcrumb(lang('lang_search'), 'search');
-
+
$this->template->build('search', $data);
}
-
+
// ------------------------------------------------------------------------
-
+
/**
* Perform the search and store it.
*/
public function do_search()
{
$this->load->model('kb/articles_model');
-
+
$this->load->library('pagination');
-
+
$options = array();
-
+
// Setup the categories first.
if ($category = $this->input->post('category'))
{
$category = (int) $category;
-
- // Set up the parent cat.
+
+ // Set up the parent cat.
$options['category'] = $category;
-
+
// Now any children
$this->load->library('categories/categories_library');
$this->categories_library->clear_ids();
$this->categories_library->get_child_ids($category);
$search_cats = $this->categories_library->get_ids();
-
+
if ( ! empty($search_cats))
{
$options['article_category'] = $search_cats;
}
}
-
+
// Now keywords
if ($keywords = $this->input->post('keywords'))
{
@@ -111,25 +111,25 @@ public function do_search()
$this->session->set_flashdata('error', lang('lang_no_results'));
redirect('search/no_results');
}
-
+
// Bing Bang Done.. Off to search results we go!
redirect('search/results/'.$hash);
}
-
+
// ------------------------------------------------------------------------
-
+
/**
* Filter results from category page.
*/
public function do_filter()
- {
+ {
// Make sure we have a category
if ( ! $category = $this->input->post('category'))
{
$this->session->set_flashdata('error', lang('lang_no_results'));
redirect('search/no_results');
}
-
+
// Make sure we have some fields or else no filtering can be done.
if ($this->events->active('fields'))
{
@@ -143,19 +143,19 @@ public function do_filter()
// Remove any previous filter session data
$this->search_model->remove_session();
-
- // Here we assign the whole post global.
- // This is secured in the filter listings
+
+ // Here we assign the whole post global.
+ // This is secured in the filter listings
// because it checks the posted data matches
// searchable fields.
$options = $_POST;
-
+
if ( ! $hash = $hash = $this->search_model->filter_listings($options, $category))
{
$this->session->set_flashdata('error', lang('lang_no_results'));
redirect('search/no_results');
}
-
+
// Get the category uri
$this->load->model('categories/categories_model');
if ( ! $cat_data = $this->categories_model->get_cat($category))
@@ -163,18 +163,18 @@ public function do_filter()
$this->session->set_flashdata('error', 'Category not found');
redirect('search/no_results');
}
-
+
$uri = $cat_data['cat_uri'];
redirect('categories/'.$uri.'/'.$hash);
}
-
+
// ------------------------------------------------------------------------
-
+
/**
- * Show the search results.
- *
- * Must have a valid md5 hash.
+ * Show the search results.
+ *
+ * Must have a valid md5 hash.
*/
public function results($hash = '')
{
@@ -184,10 +184,10 @@ public function results($hash = '')
$this->session->set_flashdata('message', 'Hash Error');
redirect('search/no_results');
}
-
+
// Parse out old searchs
$this->search_model->clean_search_results();
-
+
// Confirm we have a search result
if ( ! $search = $this->search_model->get_search($hash))
{
@@ -197,7 +197,7 @@ public function results($hash = '')
$search_where = unserialize(base64_decode($search['search_keywords']));
$search_total = $search['search_total'];
-
+
// Setup pagination
$this->load->library('pagination');
$config['total_rows'] = $search_total;
@@ -205,20 +205,21 @@ public function results($hash = '')
$config['num_links'] = 5;
$data['paging'] = $this->pagination->get_pagination($config['total_rows'], $config['per_page']);
$offset = $this->pagination->offset;
-
+
// Do the query
$this->db->from('articles')
->join('article_fields', 'article_id = article_field_id', 'inner')
+ ->join('article2cat', 'article_id = article_id_rel', 'right')
->where($search_where)
->limit($config['per_page'], $offset);
-
+
$query = $this->db->get();
-
- if ($query->num_rows() == 0)
+
+ if ($query->num_rows() == 0)
{
return FALSE;
}
-
+
$data['articles'] = $query->result_array();
// Get any other thing they may want.
$i = 0;
@@ -228,20 +229,20 @@ public function results($hash = '')
$data['articles'][$i]['article_url'] = site_url('article/'.$row['article_uri']);
$i++;
}
-
+
$this->template->set_breadcrumb(lang('lang_search'), 'search');
-
+
// Finally pass it to the template.
$this->template->build('results', $data);
}
-
+
// ------------------------------------------------------------------------
-
+
public function no_results()
{
// Set parent breadcrumb
$this->template->set_breadcrumb(lang('lang_search'), 'search');
-
+
$data = array();
if ($this->session->flashdata('message'))
{
@@ -257,9 +258,9 @@ public function no_results()
$data['link'] = 'javascript:history.go(-1)';
$data['link_text'] = lang('lang_search_again');
}
-
+
$this->template->build('message', $data);
}
}
/* End of file search.php */
-/* Location: ./upload/includes/68kb/modules/search/controllers/search.php */
+/* Location: ./upload/includes/68kb/modules/search/controllers/search.php */
View
214 upload/system/68kb/modules/search/libraries/Search_parser.php
@@ -22,17 +22,17 @@
*
*/
class Search_parser
-{
+{
private $_ci;
-
+
private $_data = array();
-
+
function __construct($data = array())
{
$this->_ci =& get_instance();
$this->_data = $data;
}
-
+
// ------------------------------------------------------------------------
/**
@@ -46,16 +46,16 @@ function form($data = '')
{
// Set the default options
$defaults = array(
- 'show_categories' => 'no',
+ 'show_categories' => 'no',
'class' => 'search_form',
'id' => '',
'style' => ''
);
$options = $this->_ci->settings->get_params($data['attributes'], $defaults);
-
+
$this->_ci->load->helper('form');
-
+
// Should we load up the categories
$cats = '';
if ($options['show_categories'] == 'yes')
@@ -64,207 +64,27 @@ function form($data = '')
$cats = $this->_ci->categories_model->get_categories();
$this->_ci->categories_library->category_tree($cats);
$cats = $this->_ci->categories_library->get_categories();
-
+
$cat_options['0'] = lang('lang_search_all');
foreach($cats as $row)
{
$indent = ($row['cat_parent'] != 0) ? repeater(' » ', $row['depth']) : '';
- $cat_options[$row['cat_id']] = $indent.$row['cat_name'];
- }
- $cats = form_dropdown('category', $cat_options, 'class="category"');
+ $cat_options[$row['cat_id']] = $indent.$row['cat_name'];
+ }
+ $cats = form_dropdown('category', $cat_options, 'id="cats" class="category"');
}
-
+
$attributes = array('class' => $options['class'], 'id' => $options['id'], 'style' => $options['style']);
-
+
$output = form_open('search/do_search', $attributes);
-
+
$output .= str_replace('{kb:cats}', $cats, $data['content']);
-
+
$output .= form_close();
-
- return $output;
-
-
-
- }
-
- // ------------------------------------------------------------------------
-
- /**
- * Listings Helper
- *
- * @param array
- * @return mixed
- */
- function get()
- {
- $this->_ci->benchmark->mark('get_listings_start');
-
- // Set the default options
- $defaults = array('limit' => 10, 'owner' => '', 'category' => '', 'class' => '', 'extra_field' => '', 'sort_order' => 'random', 'sort_column' => 'listing_title', 'cache' => 0, 'exclude' => '');
-
- $options = $this->_ci->settings->get_params($this->_data['attributes'], $defaults);
-
- // Do the listings query
- $this->_ci->db->from('listings')
- ->join('listing_status', 'listing_status = status_id', 'inner')
- ->join('listing_fields', 'listing_id = listing_field_id', 'inner')
- ->join('users', 'user_id = listing_owner_id', 'left');
-
- $where = 'status_show_listing = "y" AND listing_expiration > '. time();
-
- // Call any hooks and add them to the where clause.
- if ($this->_ci->events->active_hook('get_listings_where'))
- {
- $where .= $this->_ci->events->trigger('get_listings_where');
- }
-
- $this->_ci->db->where($where);
-
- // Searching for a single users listings
- if ($options['owner'] != '' && is_numeric($options['owner']))
- {
- $this->_ci->db->where('listing_owner_id', (int) $options['owner']);
- }
-
- // Searching off a listings class
- if ($options['class'] != '')
- {
- $this->_ci->db->like('listing_class', $options['class']);
- }
-
- // Searching in a specific category
- if ($options['category'] !== '' && is_numeric($options['category'])) // Single category
- {
- $this->_ci->db->where('listing_category', (int) $options['category']);
- }
- elseif ($options['category'] !== '' && strpos($options['category'], ',') !== FALSE) // Passing multiple categories (1,2,3) Must have a comma as seperator
- {
- $this->_ci->db->where_in('listing_category', $options['category']);
- }
-
- // Check for excluded listings
- if ($options['exclude'] != '')
- {
- $exclude = explode(',', $options['exclude']);
- $this->_ci->db->where_not_in('listing_id', $exclude);
- }
-
- // Extra Field Searching
- if ($options['extra_field'] !== '' && strpos($options['extra_field'], '|') !== FALSE) // Must have a pipe as a seperator
- {
- $extra_field = explode('|', $options['extra_field']);
-
- // See if they used the field name or the table name
- if (strpos($extra_field[0], 'extra_field_') === FALSE)
- {
- $extra_field[0] = 'extra_field_'.$extra_field[0];
- }
-
- $this->_ci->db->where($extra_field[0], $extra_field[1]);
- }
-
- // Allowed order by. This prevents some one from using something to produce an invalid query.
- $allowed_order_by = array('asc', 'desc', 'random');
-
- $sort_order = 'asc';
- if ( ! in_array(strtolower($options['sort_order']), $allowed_order_by))
- {
- $sort_order = 'desc';
- }
-
- // This sets the allowed order by clauses. Prevents invalid query.
- $listing_cols = $this->_ci->db->list_fields('listings');
- $fields_cols = $this->_ci->db->list_fields('listing_fields');
- $allowed_sort = array_merge($listing_cols, $fields_cols);
-
- // Order By
- if (in_array($options['sort_column'], $allowed_sort))
- {
- $this->_ci->db->order_by($options['sort_column'], $sort_order);
- $this->_ci->db->limit($options['limit']);
- }
- else
- {
- $this->_ci->db->order_by('listing_title', $sort_order);
- $this->_ci->db->limit($options['limit']);
- }
-
- $query = $this->_ci->db->get();
-
- // Log the query in case some one wants to see it.
- log_message('debug', 'Listing Plugin Query: '. $this->_ci->db->last_query());
-
- // no records so we can't continue
- if ($query->num_rows() == 0)
- {
- return array();
- }
-
- $data = $query->result_array();
-
- // load img helper
- $this->_ci->load->helper('html');
-
- // Get any other thing they may want.
- $i = 0;
- foreach ($data AS $row)
- {
- $this->_ci->db->select('image_thumb')->from('listings_images')->where('image_listing_id', $row['listing_id'])->limit(1);
- $img_query = $this->_ci->db->get();
-
- if ($img_query->num_rows() > 0)
- {
- $img_data = $img_query->row_array();
- $image_properties = array(
- 'src' => $this->_ci->config->item('listing_image_path') . $img_data['image_thumb'],
- 'alt' => $row['listing_title'],
- 'class' => 'listing_image',
- );
- }
- else
- {
- $image_properties = array(
- 'src' => 'themes/'.$this->_ci->settings->get_setting('site_theme') . '/images/nophoto.gif',
- 'alt' => $row['listing_title'],
- 'class' => 'listing_image',
- );
- }
-
- $data[$i]['listing_images'] = $img_query->num_rows();
- $data[$i]['listing_upload_image'] = img($image_properties);
- $img_query->free_result();
-
- // Generate the URL
- $data[$i]['listing_url'] = site_url('listings/'.$row['listing_id'].'/'.$row['listing_uri']);
- $i++;
- }
-
- $query->free_result();
-
- $this->_ci->benchmark->mark('get_listings_end');
-
- return $data;
- }
-
- // ------------------------------------------------------------------------
-
- /**
- * Get a single param
- *
- * @param string - The array key
- * @return mixed - The value
- */
- private function _get_param($key)
- {
- if (isset($this->_data['attributes'][$key]))
- {
- return $this->_data['attributes'][$key];
- }
- return FALSE;
+ return $output;
}
}
/* End of file Search_parser.php */
-/* Location: ./upload/system/68kb/modules/search/libraries/Search_parser.php */
+/* Location: ./upload/system/68kb/modules/search/libraries/Search_parser.php */
View
130 upload/system/68kb/modules/search/models/search_model.php
@@ -29,31 +29,31 @@ class Search_model extends CI_Model
*
* @return void
*/
- public function __construct()
+ public function __construct()
{
parent::__construct();
log_message('debug', 'Search_model Initialized');
}
-
+
// ------------------------------------------------------------------------
-
+
/**
* Store Search Results
*
* @param string - The where clause
* @param int - The total results.
*/
- public function store_search_results($keywords, $total)
+ public function store_search_results($keywords, $total)
{
$this->load->helper('string');
-
+
$hash = random_string('unique');
-
+
// First delete any old searches by the user
$this->db->where('search_user_id', $this->session->userdata('user_id'));
$this->db->where('search_ip', $this->input->ip_address());
$this->db->delete('search');
-
+
$data = array(
'search_id' => $hash,
'search_date' => time(),
@@ -62,14 +62,14 @@ public function store_search_results($keywords, $total)
'search_total' => $total,
'search_ip' => $this->input->ip_address()
);
-
+
$this->db->insert('search', $data);
-
+
return $hash;
}
-
+
// ------------------------------------------------------------------------
-
+
private function _save_keywords($keywords)
{
$data = array(
@@ -78,12 +78,12 @@ private function _save_keywords($keywords)
'searchlog_term' => $keywords,
'searchlog_ip' => $this->input->ip_address()
);
-
+
$this->db->insert('searchlog', $data);
}
-
+
// ------------------------------------------------------------------------
-
+
/**
* Delete old search data.
*
@@ -93,13 +93,13 @@ public function clean_search_results()
{
// Set the expiration to one hour
$expire = strtotime("-1 hour");
-
+
$this->db->where('search_date <', $expire);
- $this->db->delete('search');
+ $this->db->delete('search');
}
-
+
// ------------------------------------------------------------------------
-
+
/**
* Remove the search session data
*/
@@ -108,7 +108,7 @@ public function remove_session()
if ($sess_data = get_cookie($this->config->item('sess_cookie_name'), TRUE))
{
$sess_data = unserialize($sess_data);
- foreach ($sess_data as $key => $value)
+ foreach ($sess_data as $key => $value)
{
if (strpos($key, 'extra_field_') !== FALSE)
{
@@ -117,19 +117,19 @@ public function remove_session()
}
}
}
-
+
// ------------------------------------------------------------------------
-
+
/**
* Get any extra fields that can be searched
- *
+ *
* @param int Category id
* @return array
*/
public function get_fields($cat_id)
{
$cat_id = (int) $cat_id;
-
+
$this->db->select('field_id,field_type,field_internal,field_validation')
->distinct()
->from('fields')
@@ -137,9 +137,9 @@ public function get_fields($cat_id)
->where('rel_cat_id', $cat_id)
->where('field_table', 'listing_fields')
->where('field_search', 'yes');
-
+
$query = $this->db->get();
-
+
if ($query->num_rows() == 0)
{
return FALSE;
@@ -148,12 +148,12 @@ public function get_fields($cat_id)
$data = $query->result_array();
$query->free_result();
-
+
return $data;
}
-
+
// ------------------------------------------------------------------------
-
+
/**
* Get a stored search
*
@@ -164,21 +164,21 @@ public function get_search($hash)
$this->db->from('search')
->where('search_id', $hash)
->where('search_ip', $this->input->ip_address());
-
+
$query = $this->db->get();
-
- if ($query->num_rows() == 0)
+
+ if ($query->num_rows() == 0)
{
return FALSE;
}
-
+
$data = $query->row_array();
-
+
return $data;
}
-
+
// ------------------------------------------------------------------------
-
+
/**
* Filter the listings by options
*
@@ -195,23 +195,23 @@ public function filter_listings($search_options = array())
$fields = $this->get_fields($category);
}
}
-
+
$this->db->from('articles')
->join('article_fields', 'article_id = article_field_id', 'inner');
-
+
$where = 'article_display = "y"';
-
+
if (isset($search_options['category']) OR isset($search_options['article_category']))
{
$this->db->join('article2cat', 'article_id = article_id_rel', 'right');
}
-
+
// Call any hooks and add them to the where clause.
if ($this->events->active_hook('get_articles_where'))
{
$where .= $this->events->trigger('get_articles_where', $search_options);
}
-
+
// Validate that the search_options can be searched.
if ( ! empty($search_options))
{
@@ -221,26 +221,26 @@ public function filter_listings($search_options = array())
{
$field_name = $extra['field_internal'];
$listing_field = 'extra_field_'.$field_name;
-
+
// Check if it is a min/max field
if ($extra['field_type'] == 'price' OR $extra['field_type'] == 'range' OR strpos($extra['field_validation'], 'numeric') !== FALSE)
{
$min = 0;
-
+
if (isset($search_options[$field_name.'_min']) && is_numeric($search_options[$field_name.'_min']))
{
$min = (int) $search_options[$field_name.'_min'];
$this->session->set_userdata($listing_field.'_min', $min);
}
-
+
$max = 999999999;
-
+
if (isset($search_options[$field_name.'_max']) && is_numeric($search_options[$field_name.'_max']))
{
$max = (int) $search_options[$field_name.'_max'];
$this->session->set_userdata($listing_field.'_max', $max);
}
-
+
if ($min > $max OR $max == 0)
{
$max = 999999999;
@@ -251,23 +251,23 @@ public function filter_listings($search_options = array())
elseif ($extra['field_type'] == 'date') // Searching dates
{
$min = strtotime("-10 years");
-
+
if (isset($search_options[$field_name.'_min']) && $search_options[$field_name.'_min'] != 'min')
{
$value = $search_options[$field_name.'_min'] .' 00:00:00';
$min = human_to_unix($value);
$this->session->set_userdata($listing_field.'_min', $search_options[$field_name.'_min']);
}
-
+
$max = strtotime("+10 years");
-
+
if (isset($search_options[$field_name.'_max']) && $search_options[$field_name.'_max'] != 'max')
{
$value = $search_options[$field_name.'_max'] .' 00:00:00';
$max = human_to_unix($value);
$this->session->set_userdata($listing_field.'_max', $search_options[$field_name.'_max']);
}
-
+
if ($min > $max)
{
$max = strtotime("+10 years");
@@ -283,28 +283,28 @@ public function filter_listings($search_options = array())
}
}
}
-
+
if (isset($search_options['keywords'])) // Keyword searching
{
$keywords = $this->security->xss_clean($search_options['keywords']);
$keywords = strip_tags(stripslashes($keywords));
$words_array = explode(" ", $keywords);
-
+
foreach ($words_array as $trimm)
{
$where .= " AND (
- article_title LIKE '%".$this->db->escape_like_str($trimm)."%' OR
- article_short_desc LIKE '%".$this->db->escape_like_str($trimm)."%' OR
+ article_title LIKE '%".$this->db->escape_like_str($trimm)."%' OR
+ article_short_desc LIKE '%".$this->db->escape_like_str($trimm)."%' OR
article_description LIKE '%".$this->db->escape_like_str($trimm)."%'
)";
}
}
-
+
if (isset($search_options['article_category']))
{
if (is_array($search_options['article_category']))
{
- $this->db->where_in('category_id_rel', $search_options['article_category']);
+ $where .= ' AND category_id_rel IN('.implode($search_options['article_category'],',').')';
}
}
elseif (isset($category))
@@ -315,31 +315,31 @@ public function filter_listings($search_options = array())
}
}
}
-
+
$this->db->where($where);
-
+
$query = $this->db->get();
-
- if ($query->num_rows() == 0)
+
+ if ($query->num_rows() == 0)
{
return FALSE;
}
-
+
$data = $query->result_array();
-
+
// Save the keywords
if (isset($search_options['keywords']))
{
$this->_save_keywords($search_options['keywords']);
}
-
+
// Save the search
$hash = $this->store_search_results(base64_encode(serialize($where)), $query->num_rows());
-
+
$query->free_result();
-
+
return $hash;
}
}
/* End of file search_model.php */
-/* Location: ./upload/includes/68kb/modules/search/models/search_model.php */
+/* Location: ./upload/includes/68kb/modules/search/models/search_model.php */
Please sign in to comment.
Something went wrong with that request. Please try again.