Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Form Questions

How to display Search Results on the same URL


Reply

18 Posts   5922 Views

Avatar
Martijn

6 September 2010 at 11:02pm Community Member, 271 Posts

Not extensively tested, but this creates a SearchForm on Page with an Extension and displays the searchresults with a Controller without a Page class.

http://www.sspaste.com/paste/show/4c84cb3f4d2e1

Avatar
losqualo

7 September 2010 at 12:45am Community Member, 17 Posts

Apologies if I'm being particularly dense here... does that all go into _config.php as seems to be implied?

If so, why? Seems to run counter to everything I've read thus far if that's the case.

And why is the fulltext search engine enabling commented out?.. that also seems counter-intuitive.

If not, then how does it break down in terms of files and where they go... I'd be assuming as php files in mysite/code... that is if it isn't just in the _config.php file.

That said, once I understand the placement/breakdown I can see some things in here that help in other areas of understanding, so thanks for that too!

Cheers

Rob

Avatar
Martijn

7 September 2010 at 4:17am (Last edited: 7 September 2010 4:25am), Community Member, 271 Posts

No, sorry if I was not clear.

This goes in you _config.php file :

//Enable search
FulltextSearchable::enable();
//Add extensions
Object::add_extension('Page_Controller','SearchFormExtension');
//Set director to SearchPage_Controller
Director::addRules(10, array(
   SearchPage_Controller::getURLSegment().'//$Action/$ID/$OtherID' => 'SearchPage_Controller'
));

This goes in themes/yourtheme/Layout/SearchPage.ss:

<% if SearchResults %>
   <div class="searchresults">
   <% control SearchResults %>
      <div>
         <h5>$Title</h5>
         <p>$Content.Summary</p>
         <a class="readMoreLink right" href="$Link" title="$Title">Read more</a>
         <div class="clear"></div>   
      </div>
   <% end_control %>
   </div>
<% else %>
<p>No results found</p>
<% end_if %>

And this goes in mysite/code/SearchFormExtension.php:

<?php
/**
* Add a SearchForm to Page_Controller
*/
class SearchFormExtension extends Extension{
   
   public static $allowed_actions = array(
      'SearchForm',
      'results'
   );
   
   function SearchForm() {
      
      $searchText = $this->owner->request->getVar('Search') ? $this->owner->request->getVar('Search') : 'Search';
      
      $fields = new FieldSet(
         new TextField("Search", "", $searchText)
      );
      $actions = new FieldSet(
         new FormAction('results', 'Search!')
      );
      
      return new SearchForm(Controller::curr(), "SearchForm", $fields, $actions);
   }    
   
   function results($data = NULL, $form = NULL){
      if($data && $form){
         Director::redirect(Director::baseURL().SearchPage_Controller::getURLSegment()."/?Search=".$form->getSearchQuery());
      }
   }
}

/**
* Pageless controller to display the searchresults
*/

class SearchPage_Controller extends Page_Controller{
   
   public static $url_segment = 'searchresults';
   
   function init(){
      parent::init();
   }
   
   function getURLSegment(){
      return self::$url_segment;   
   }
   
   function setURLSegment($string = 'searchresults'){
      self::$url_segment = $string;   
   }
   
   function SearchResults(){
      if($this->request->getVar('Search')){
         $form = SearchFormExtension::SearchForm();
         return $form->getResults();
      }
}

function Link(){
      return $this->getURLSegment();
}
}

Edit: I attached a zipfile with the needed files in a seperate modulefolder.
This should work as well.

Attached Files
Avatar
bunheng

4 August 2011 at 4:13pm Community Member, 78 Posts

Hi,

Is it possible to return the the following:

1. Page title for SearchPage.
2. Total results match.
3. And the search query.

Thanks
Bunheng

Avatar
martimiz

5 August 2011 at 2:53am (Last edited: 5 August 2011 2:54am), Forum Moderator, 1091 Posts

@ Yitter: I still think you're on the right track using $form->setFormAction. I can confirm the missing &action_results=Search in IE, I recall that IE has trouble returning the name of the input 'submit'. But that doesn't really matter, as long as you're not using it...

I'll add this example to all the different solutions offered in this thread: a simple search in a basic page. It's almost like the basic SilverStripe search, but for me it works on IE6789... I stripped it bare for the sake of a clean example:

- first I created a page called 'searchresults' in the SiteTree. Then in my Page_Controller:

protected $Query;

function SearchForm() {
   $searchText = isset($this->Query) ? $this->Query : 'Search...';
   $fields = new FieldSet(
      new TextField("Search", "", $searchText)
   );
   $actions = new FieldSet(
      new FormAction('results', 'Search')
   );
   $form = new SearchForm($this, "SearchForm", $fields, $actions);
   $form->setFormAction(Director::baseURL().'searchresults/SearchForm');
   return $form;
}

function results($data, $form){
   $Query = (isset($data['Search']))? $data['Search'] : '' ;
   $sqlQuery = Convert::raw2sql($Query);
   $Title = "Search results for '" . substr($Query, 0, 20) . "' ";

   $Results = DataObject::get( ... );
   $searchdata = array(
      'ProductList' => $Results,
      'MenuTitle' => $Title
   );
   $this->Query = $Query;
   return $this->customise($searchdata)->renderWith(array('PageResults', 'Page'));
}

@bunheng: sure you can, add all the info you want to the $searchdata array (like I did with the MenuTitle), and make sure your template uses them..

Avatar
bunheng

5 August 2011 at 1:16pm Community Member, 78 Posts

Hi martimiz,

Sorry to disturb you again, I would like to know where I can create the page searchresults, is it a new page class in mysite/code? or create it via CMS Admin.

Thanks
Bunheng

Avatar
martimiz

10 August 2011 at 12:22am (Last edited: 10 August 2011 12:22am), Forum Moderator, 1091 Posts

Hi again

I'm sorry, I missed your question untill right now :-(

Anyway: I meant create a new page in the CMS SiteTree. Title: searchresults

Avatar
NickJacobs

22 August 2011 at 3:13am Community Member, 144 Posts

Hi @Martijn
I setup your SearchFormExtension, and it works great.....but once I log out of SS I cannot log back in. I get an unknown 500 server error..

if I comment out the 'Object::add_extension('Page_Controller','SearchFormExtension');' I can get back in, bu then the search obviously doesn't work..

any ideas why this would be happening?