21294 Posts in 5734 Topics by 2602 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 286 Views |
-
Firing a custom search form and displaying results

27 May 2012 at 4:32pm Last edited: 27 May 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.
-
Re: Firing a custom search form and displaying results

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.
-
Re: Firing a custom search form and displaying results

28 May 2012 at 10:20am
Again.. solved.
I had to change the line
return $customised->renderWith(array('SearchResults', 'RegionPage'));
toreturn $customised->renderWith(array('SearchResults', 'Page'));
| 286 Views | ||
|
Page:
1
|
Go to Top |

