How to do include a category search option for CGBlogs

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
applejack
Power Poster
Power Poster
Posts: 1015
Joined: Fri Mar 30, 2007 2:28 am
Location: London

How to do include a category search option for CGBlogs

Post by applejack »

This is how to do include a category search option alongside a free text search using CGBlogs module as implemented here http://www.commercialriskeurope.com/

Javascript code for the submission form

Code: Select all


function CheckForm() {

with(self.document.searchform) {
	  
	if (catselect.selectedIndex==0)
	{
		cntnt01modules.value = "";
	}
	submit();
	
}
}


Search template. The categories are the same as those set up as categories in the CGBlog. If you wanted these automated you could using the Browsecat template to populate the categories in the select box.

Code: Select all


{$startform|replace:'<form':'<form onsubmit="return CheckForm(this);" name="searchform"'}

<select id="catselect" onchange="form.{$search_actionid}passthru_CGBlog_category.value=this.options[this.selectedIndex].value">
<option value="">Search Site</option>
<option value="Association News">Association News</option>
<option value="Comment">Comment</option>
<option value="Education">Education</option>
<option value="In And Out">In & Out</option>
<option value="Industry Profiles">Industry Profiles</option>
<option value="Latest News">Latest News</option>
<option value="Results">Results</option>
<option value="Roundtables">Roundtables</option>
</select>

<input type="text" class="text-input" id="{$search_actionid}searchinput" name="{$search_actionid}searchinput" size="20" maxlength="50" value="{$searchtext}" {$hogan} /><span class="searchbutt"><input type="image" name="myclicker" src="/uploads/images/site/butt-search-lilac.gif" /></span><br />
{if isset($hidden)}{$hidden}{/if}

{$endform}


Search Results template

Code: Select all


{if $smarty.post.cntnt01passthru_CGBlog_category}
<h3>Search results for "{$phrase}"  in {$smarty.post.cntnt01passthru_CGBlog_category}</h3>
{else}
<h3>Search results for "{$phrase}"</h3>
{/if}



{if $itemcount > 0}
<ol style="margin-left:18px;">
{counter start="0" assign="mycounter"}
{foreach from=$results item=entry}
{if $smarty.post.cntnt01passthru_CGBlog_category}
{if $entry->title == "Calguys Blog Module"}
{counter assign="mycounter"}
  <li><a href="{$entry->url}">{$entry->urltxt}</a></li>
{/if}
{else}
<li><a href="{$entry->url}">{$entry->urltxt}</a></li>
{/if}
  {/foreach}
</ol>

{if $smarty.post.cntnt01passthru_CGBlog_category && $mycounter == 0}
<p><strong>{$noresultsfound}</strong></p>
{/if}

{else}
  <p><strong>{$noresultsfound}</strong></p>
{/if}


Code to add to CGBlog.module.php insert above function SearchResult

Code: Select all


// CODE FOR CATEGORY SEARCH
	
    function SearchResultWithParams($returnid,$articleid,$attr='',$params=array())
    {
      $result = array();
		
      if ($attr == 'cgblog')
	{
	  $db =& $this->GetDb();
	  $now = $db->DbTimeStamp(time());
	  $zerotime = $db->DbTimeStamp(1);
	  $q = "SELECT cgblog_title FROM ".cms_db_prefix()."module_cgblog cgb ";
      $where = " WHERE cgblog_id = ? ";
	  if(!empty($params['category']))
	  {
	        $where .= " AND c.name = '".$params['category']."' ";
		$q .= " LEFT JOIN ".cms_db_prefix()."module_cgblog_blog_categories bc ON (bc.blog_id = cgb.cgblog_id)";
		$q .= " LEFT JOIN ".cms_db_prefix()."module_cgblog_categories c ON (bc.category_id = c.id )";
	  }
	  if( $this->GetPreference('expired_searchable',1) == 0 )
	    {
	      // make sure we don't return expired articles.
	      // if we don't want em to.
	      $where .= " AND ((".$db->IfNull('end_time',$zerotime)." = ".$zerotime.") OR (end_time > $now))";
	    }
	  $dbresult = $db->Execute( $q.$where, array( $articleid ) );
	  //echo $db->sql;
	  while($dbresult && $row=$dbresult->FetchRow()) 
		{

	      //0 position is the prefix displayed in the list results.
	      $result[0] = $this->GetFriendlyName();
		
	      //1 position is the title
	      $result[1] = $row['cgblog_title'];

	      //2 position is the URL to the title.
	      
	      $aliased_title = munge_string_to_url($row['cgblog_title']);
	      $prettyurl = $this->GetPreference('urlprefix','cgblog').'/' . $articleid.'/'.$returnid."/$aliased_title";
	      $result[2] = $this->CreateLink('cntnt01', 'detail', $returnid, '', array('articleid' => $articleid) ,'', true, false, '', true, $prettyurl);
	    }
	}
		
      return $result;
    }	

Tag in the HTML code

Code: Select all


{search resultpage='search-results' passthru_CGBlog_category='' search_method='post' searchtext=''}



Enjoy...
Last edited by applejack on Tue Apr 27, 2010 11:57 pm, edited 1 time in total.

Website Design & Production
http://www.applejack.co.uk
User avatar
zounars
Forum Members
Forum Members
Posts: 41
Joined: Tue Jun 20, 2006 11:38 am
Location: Italy

Re: How to do include a category search option for CGBlogs

Post by zounars »

Hello applejack,

I would like to know if this method can be use to include a category search option for News module?
I don't want to use CGBlogs as my articles have to be posted only in one category.

Thanks for your help.
applejack
Power Poster
Power Poster
Posts: 1015
Joined: Fri Mar 30, 2007 2:28 am
Location: London

Re: How to do include a category search option for CGBlogs

Post by applejack »

No idea you will have to try it.
Post Reply

Return to “Tips and Tricks”