Skip to content

Instantly share code, notes, and snippets.

@anunay
Created February 10, 2014 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anunay/8922776 to your computer and use it in GitHub Desktop.
Save anunay/8922776 to your computer and use it in GitHub Desktop.
Search Site Model for FuelCMS
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Search_site extends MY_Model {
protected $_tables;
function __construct()
{
$CI =& get_instance();
$CI->config->module_load(FUEL_FOLDER, 'fuel', TRUE);
$this->_tables = $CI->config->item('tables', 'fuel');
parent::__construct();
}
function search($keywords){
$keywords = ($keywords != '') ? '%' . $this->db->escape_str($keywords) . '%' : '%';
// search page variables
$select_params_b = array();
$select_params_b[] = "'' AS permalink";
$select_params_b[] = "'' AS title";
$select_params_b[] = $this->_tables['fuel_pagevars'] . ".page_id AS page_id";
$select_params_b[] = $this->_tables['fuel_pages'] . ".location AS location";
$select_params_b[] = $this->_tables['fuel_pages'] . ".last_modified AS sort_date";
$from_params_b = array();
$from_params_b[] = $this->_tables['fuel_pages'];
$from_params_b[] = $this->_tables['fuel_pagevars'];
$where_params_b = array();
$where_params_b[] = $this->_tables['fuel_pages'] . ".id = " . $this->_tables['fuel_pagevars'] . ".page_id";
$where_params_b[] = $this->_tables['fuel_pages'] . ".published = 'yes'";
$where_params_b[] = $this->_tables['fuel_pages'] . ".location != 'sitemap.xml'";
$where_params_b[] = $this->_tables['fuel_pages'] . ".location != 'search'";
$where_params_b[] = $this->_tables['fuel_pagevars'] . ".active = 'yes'";
$where_params_b[] = $this->_tables['fuel_pagevars'] . ".name != 'copy'";
$where_params_b[] = $this->_tables['fuel_pagevars'] . ".name != 'section_header'";
$where_params_b[] = $this->_tables['fuel_pagevars'] . ".value LIKE '" . $keywords . "'";
$sql_b = "SELECT " . implode(', ', $select_params_b) .
" FROM " . implode(', ', $from_params_b) .
" WHERE " . implode(' AND ', $where_params_b) .
" GROUP BY page_id";
// echo $sql_b;
// union queries
$sql = "UNION (" . $sql_b . ") ORDER BY sort_date desc";
return $this->db->query($sql_b)->result_array();
}
function search2($keywords)
{
# Key words
$keywords = explode(' ', $keywords);
# Short cuts
$pages = $this->_tables['fuel_pages'];
$page_vars = $this->_tables['fuel_pagevars'];
# Select
$select = array(
$page_vars.'.page_id AS page_id',
$pages.'.location AS location',
$pages.'.last_modified AS sort_date',
'fpv.value AS title',
'fpv1.value AS body'
);
# From
$from = array($pages, $page_vars);
# Where
$where = array(
$pages.'.id = '.$page_vars.'.page_id',
$pages.'.published = "yes"',
$pages.'.location != "sitemap.xml"',
$pages.'.location != "search"',
$page_vars.'.active = "yes"',
$page_vars.'.name != "copy"',
$page_vars.'.name = "body"'
);
# Build like
$str = '(';
foreach ($keywords as $keyword)
{
$str .= $page_vars.'.value LIKE "%'.$this->db->escape_str($keyword).'%" OR ';
}
# Remove last OR
$where[] = substr($str, 0, -4).')';
$sql = "SELECT ".implode(', ', $select).
" FROM ".implode(', ', $from).
" JOIN ".$page_vars." fpv ON (fpv.page_id = ".$page_vars.".page_id AND fpv.name = 'h1')".
" JOIN ".$page_vars." fpv1 ON (fpv1.page_id = ".$page_vars.".page_id AND fpv1.name = 'body')".
" WHERE ".implode(' AND ', $where);
echo $sql;
return $this->db->query($sql)->result_array();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment