Created
July 23, 2012 19:25
-
-
Save litzinger/3165638 to your computer and use it in GitHub Desktop.
Wild card search for possible related entries. Good for 404 pages.
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 | |
$plugin_info = array( | |
'pi_name' => 'Suggested Results', | |
'pi_version' => '1.0', | |
'pi_author' => 'Brian Litzinger', | |
'pi_author_url' => '', | |
'pi_description' => 'Looks at the URL segments and tries to find suggested alternate pages.', | |
'pi_usage' => Suggested::_usage() | |
); | |
class Suggested | |
{ | |
function __construct() | |
{ | |
$this->EE =& get_instance(); | |
} | |
function entries() | |
{ | |
// set some default tag parameters checking if any were passed manually through the tag first | |
// the tag params are used by the Weblog object and affect it's output | |
$this->EE->TMPL->tagparams["channel"] = $this->EE->TMPL->fetch_param('channel', ''); | |
$this->EE->TMPL->tagparams["disable"] = $this->EE->TMPL->fetch_param('disable', 'member_data|pagination|trackbacks'); | |
$this->EE->TMPL->tagparams["dynamic"] = $this->EE->TMPL->fetch_param('dynamic', 'no'); | |
$this->EE->TMPL->tagparams["rdf"] = $this->EE->TMPL->fetch_param('rdf', 'off'); | |
$this->EE->TMPL->tagparams["limit"] = $this->EE->TMPL->fetch_param('limit', '100'); | |
$debug = $this->EE->TMPL->fetch_param('debug', 'no'); | |
$keywords = $this->EE->uri->segments; | |
// Load the Channel class | |
if(!class_exists('Channel')) | |
{ | |
require PATH_MOD.'channel/mod.channel'.EXT; | |
} | |
$c = new Channel; | |
$c->initialize(); | |
$disabled = explode('/', $this->EE->TMPL->tagparams["disable"]); | |
if ( ! in_array('custom_fields', $disabled)) | |
{ | |
$c->fetch_custom_channel_fields(); | |
} | |
if ( ! in_array('member_data', $disabled)) | |
{ | |
$c->fetch_custom_member_fields(); | |
} | |
if ( ! in_array('pagination', $disabled)) | |
{ | |
$c->pagination->get_template(); | |
} | |
$save_cache = FALSE; | |
if ($this->EE->config->item('enable_sql_caching') == 'y') | |
{ | |
if (FALSE == ($c->sql = $c->fetch_cache())) | |
{ | |
$save_cache = TRUE; | |
} | |
else | |
{ | |
if ($this->EE->TMPL->fetch_param('dynamic') != 'no') | |
{ | |
if (preg_match("#(^|\/)C(\d+)#", $c->query_string, $match) OR in_array($c->reserved_cat_segment, explode("/", $c->query_string))) | |
{ | |
$c->cat_request = TRUE; | |
} | |
} | |
} | |
if (FALSE !== ($cache = $c->fetch_cache('pagination_count'))) | |
{ | |
if (FALSE !== ($c->fetch_cache('field_pagination'))) | |
{ | |
if (FALSE !== ($pg_query = $c->fetch_cache('pagination_query'))) | |
{ | |
$c->pagination->paginate = TRUE; | |
$c->pagination->field_pagination = TRUE; | |
$c->pagination->cfields = $c->cfields; | |
$c->pagination->build(trim($cache), $c->sql, $this->EE->db->query(trim($pg_query))); | |
} | |
} | |
else | |
{ | |
$c->pagination->cfields = $c->cfields; | |
$c->pagination->build(trim($cache), $c->sql); | |
} | |
} | |
} | |
if ($c->sql == '') | |
{ | |
$c->build_sql_query(); | |
} | |
if ($c->sql == '') | |
{ | |
return $this->EE->TMPL->no_results(); | |
} | |
if ($save_cache == TRUE) | |
{ | |
$c->save_cache($c->sql); | |
} | |
$c->query = $this->EE->db->query($c->sql); | |
if ($c->query->num_rows() == 0) | |
{ | |
return $this->EE->TMPL->no_results(); | |
} | |
// custom select sql | |
$select_sql = "FROM"; | |
// custom the join sql | |
$join_sql = "AS t"; | |
// custom where sql | |
$where_sql = "WHERE "; | |
foreach($keywords as $keyword) | |
{ | |
$keyword = $this->EE->db->escape_str($keyword); | |
$where_sql .= "t.url_title LIKE '%{$keyword}%' OR "; | |
} | |
$where_sql = substr($where_sql, 0, -4) . ' AND '; | |
// modify Channel sql with custom | |
$sql = str_replace(array("FROM", "AS t", "WHERE"), array($select_sql, $join_sql, $where_sql), $c->sql); | |
// $sql = str_replace('SELECT', 'SELECT distinct(t.url_title),', $sql); | |
// $sql = str_replace('ORDER BY', 'GROUP BY t.entry_id ORDER BY', $sql); | |
// run sql | |
if($debug == "yes") | |
{ | |
echo "<pre>". $sql ."</pre>"; | |
} | |
$c->query = $this->EE->db->query($sql); | |
// again if there are no results fire the no_results method | |
if($c->query->num_rows == 0) | |
{ | |
return $this->EE->TMPL->no_results(); | |
} | |
$this->EE->load->library('typography'); | |
$this->EE->typography->initialize(array( | |
'convert_curly' => FALSE | |
)); | |
if ($c->enable['categories'] == TRUE) | |
{ | |
$c->fetch_categories(); | |
} | |
$c->parse_channel_entries(); | |
if ($c->enable['pagination'] == TRUE) | |
{ | |
$c->return_data = $c->pagination->render($c->return_data); | |
} | |
// Does the tag contain "related entries" that we need to parse out? | |
if (count($this->EE->TMPL->related_data) > 0 && count($c->related_entries) > 0) | |
{ | |
$c->parse_related_entries(); | |
} | |
if (count($this->EE->TMPL->reverse_related_data) > 0 && count($c->reverse_related_entries) > 0) | |
{ | |
$c->parse_reverse_related_entries(); | |
} | |
// finally return the parsed template tagdata | |
return $c->return_data; | |
} | |
function _usage() { | |
return <<<END | |
{exp:suggested:searchn} | |
{if no_results} | |
{!-- do nothing and die gracefully --} | |
{/if} | |
{if count == 1} | |
<h4>Perhaps you were looking for...</h4> | |
{/if} | |
{title} | |
{/exp:suggested:search} | |
END; | |
} | |
} // class Fulltext\ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment