Jump to:

23483 Posts in 18976 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Firing a custom search form and displaying results

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 689 Views
  • Fraser
    Avatar
    Community Member
    46 Posts

    Firing a custom search form and displaying results Link to this post

    I have a form on my homepage which i initiate with:

    function DealSearchForm() {
       $regions = DataObject::get("RegionPage");
       $fields = new FieldSet(
        new DropdownField('Region', 'Specify Region', $regions->map("ID", "Title", "Any")),
        new DropdownField('Resort', 'Specify Resort', array(''=>'Any'))
        );
       
        // Create actions
        $actions = new FieldSet(
        new FormAction('doDealSearch', 'Search')
        );

       return new Form($this, 'DealSearchForm', $fields, $actions);
    }

    The regions and resorts are populated with javascript via the following jSON

    function getRegionsJson(){
       $regionsArray = array();
       $regions = DataObject::get("RegionPage");

       foreach($regions as $region) {
          $regionsArray[$region->ID] = array();
          
          foreach($region->Children() as $resort) {
             $regionsArray[$region->ID][$resort->ID] = $resort->MenuTitle;
          }
       }
       return json_encode($regionsArray);
       }

    The regions and resorts are essentially just page types. All good so far.

    Now, I need to actually perform the search and display the results based on the regions (if any has been selected. If not show all) and resorts (again, if one has been selected, if not, show all in the region) but am at a complete loss where to even start with this.

    I'm very new to silverstripe and OOP, having only done procedural programming previous to this so please treat me like an idiot in any responses to make things as clear as possible including which files to add various snippets of code to.

    Many thanks.

  • Fraser
    Avatar
    Community Member
    46 Posts

    Re: Firing a custom search form and displaying results Link to this post

    Update.

    I have managed to take care of performing the search and outputting the results using:

    function DealSearchForm() {
       $regions = DataObject::get("RegionPage");
       $fields = new FieldSet(
        new DropdownField('Region', 'Specify Region', $regions->map("ID", "Title", "Any")),
        new DropdownField('Resort', 'Specify Resort', array(''=>'Any')),
        );
       
        // Create actions
        $actions = new FieldSet(
        new FormAction('doDealSearch', 'Search')
        );
          
       return new Form($this, 'DealSearchForm', $fields, $actions);
    }

    public function doDealSearch($data, $form) {
          $results = $this->getResults($data);
          $customised = $this->customise(array(
                'Results' => $results
          ));
          
          if(Director::is_ajax()) {
             return $customised->renderWith('RideSearchResults');
          }
          return $customised->renderWith(array('SearchResults', 'RegionPage'));
       }
       
       public function getResults($searchCriteria = array()) {
          $start = ($this->request->requestVar('start')) ? (int)$this->request->requestVar('start') : 0;
          $limit = 20;
          
          $where = '"SiteTree"."ClassName" = \'ResortPage\'';
          $sort   = '';
          $join   = '';
          
          if(isset($searchCriteria['Region']) && $searchCriteria['Region'] !=''){
             $where .= ' AND "SiteTree"."parentID" = ' . Convert::raw2sql($searchCriteria['Region']);
          }
          
          if (isset($searchCriteria['Resort']) && $searchCriteria['Resort'] !=''){
             $where .= ' AND "SiteTree"."ID" = "' . Convert::raw2sql($searchCriteria['Resort']) . '"';
          }
          
          
          $total = DataObject::get('SiteTree', $where, $sort, $join);
       
          return new DataObjectSet;
       }
       
       function Results() {
          return $this->getResults();
       }

    However the results are coming back (into SearchResults.ss url: /home/DealSearchForm) unstyled with just the template content. ie not as part of page.ss.

    What am I doing wrong here. I created a page class for Search results in SearchResultsPage.php.

  • Fraser
    Avatar
    Community Member
    46 Posts

    Re: Firing a custom search form and displaying results Link to this post

    Again.. solved.

    I had to change the line

    return $customised->renderWith(array('SearchResults', 'RegionPage'));


    to

    return $customised->renderWith(array('SearchResults', 'Page'));

    689 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.