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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Firing a custom search form and displaying results


Go to End


3 Posts   1872 Views

Avatar
Fraser

Community Member, 48 Posts

27 May 2012 at 4:32pm

Edited: 27/05/2012 4:34pm

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.

Avatar
Fraser

Community Member, 48 Posts

28 May 2012 at 10:16am

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.

Avatar
Fraser

Community Member, 48 Posts

28 May 2012 at 10:20am

Again.. solved.

I had to change the line

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

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