-
-
Save hailwood/57f8b98e7fbacc2ff693 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (class_exists("Widget")) { | |
/** | |
* Class BlogSearchWidget | |
* | |
* @method Blog Blog() | |
*/ | |
class BlogSearchWidget extends Widget { | |
private static $cmsTitle = "Blog Search"; | |
private static $description = "Displays a keyword search of blog posts."; | |
private static $title = "Search"; | |
private static $db = [ | |
'WidgetTitle' => 'Varchar' | |
]; | |
private static $has_one = array( | |
"Blog" => "Blog", | |
); | |
public function getCMSFields() { | |
$fields = FieldList::create(); | |
$fields->merge(array( | |
TextField::create("WidgetTitle", "Title"), | |
DropdownField::create("BlogID", "Blog", Blog::get()->map()) | |
)); | |
$this->extend("updateCMSFields", $fields); | |
return $fields; | |
} | |
public function Title() { | |
return $this->WidgetTitle ? $this->WidgetTitle : self::$title; | |
} | |
} | |
/** | |
* Class BlogSearchWidget_Controller | |
* | |
* @property BlogSearchWidget $widget | |
*/ | |
class BlogSearchWidget_Controller extends WidgetController { | |
private static $allowed_actions = [ | |
'BlogSearchForm', | |
'doSearch' | |
]; | |
public function BlogSearchForm() { | |
$searchParam = Controller::curr()->request->getVar('search'); | |
$form = new Form( | |
$this, | |
'BlogSearchForm', | |
new FieldList( | |
(new TextField('Query', '', $searchParam))->setAttribute('required', 'required') | |
), | |
new FieldList( | |
new FormAction('doSearch', 'Search') | |
) | |
); | |
$form->setTemplate('BlogSearchForm'); | |
$form->disableSecurityToken(); | |
$form->setFormMethod('get'); | |
return $form; | |
} | |
public function doSearch($data, $form) { | |
$query = $data['Query']; | |
$blog = $this->widget->Blog(); | |
return $this->redirect($blog->Link("?search=$query")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment