Skip to content

Instantly share code, notes, and snippets.

@hailwood
Created September 21, 2015 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hailwood/57f8b98e7fbacc2ff693 to your computer and use it in GitHub Desktop.
Save hailwood/57f8b98e7fbacc2ff693 to your computer and use it in GitHub Desktop.
<?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