Skip to content

Instantly share code, notes, and snippets.

@hailwood
Created September 10, 2015 23:32
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/b799b7ed8179b8a88e3c to your computer and use it in GitHub Desktop.
Save hailwood/b799b7ed8179b8a88e3c to your computer and use it in GitHub Desktop.
<?php
/**
* @package shop
*/
class BrandFilterForm extends Form {
protected $currentFilter = [];
public function __construct($controller, $name = "BrandFilterForm") {
$this->currentFilter = Session::get('BrandFilter');
parent::__construct(
$controller,
$name,
$this->getFormFields(),
$this->getFormActions()
);
$this->addExtraClass("brand-filter-form");
$this->extend('updateBrandFilterForm');
$this->setLegend('Brands');
$this->disableSecurityToken();
}
public function filterbrands($data, $form) {
$filters = $data['Filters'];
$this->currentFilter = $filters;
if (empty($filters)) {
Session::clear('BrandFilter');
} else {
Session::set('BrandFilter', $filters);
}
return $this->controller->redirectBack();
}
public function reset($data, $form) {
Session::clear('BrandFilter');
$this->currentFilter = [];
return $this->controller->redirectBack();
}
/**
* @return FieldList Fields for this form.
*/
protected function getFormFields() {
$fields = new FieldList();
$fields->push(
(new CheckboxSetField('Filters', '', ProductBrand::get('ProductBrand', 'Displayed=1')->map()->toArray(), $this->currentFilter))
->setFieldHolderTemplate('BrandFilterCheckboxSetHolder')
);
return $fields;
}
/**
* @return FieldList Actions for this form.
*/
protected function getFormActions() {
$fieldList = new FieldList(
FormAction::create('filterbrands', 'Filter')
->setUseButtonTag(true)
->addExtraClass('btn btn-main')
->setButtonContent('Filter')
);
if (!empty($this->currentFilter)) {
$removeAction = new FormAction('removefilter', 'Clear');
$removeAction->setUseButtonTag(true);
$removeAction->addExtraClass('btn btn-compare pull-right');
$removeAction->setButtonContent('Clear');
$fieldList->push($removeAction);
}
return $fieldList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment