|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WebApp\Component; |
| 4 | + |
| 5 | +use TgI18n\I18N; |
| 6 | +use TgUtils\Request; |
| 7 | + |
| 8 | +class FilterPanel extends Div { |
| 9 | + |
| 10 | + protected static $filter; |
| 11 | + |
| 12 | + public function __construct($parent) { |
| 13 | + parent::__construct($parent); |
| 14 | + $this->addClass('filter-panel'); |
| 15 | + $this->form = new Form($this, 'filterForm'); |
| 16 | + $this->form->setAction(Request::getRequest()->originalPath); |
| 17 | + $this->addFilterSections(); |
| 18 | + $this->addButtons(); |
| 19 | + } |
| 20 | + |
| 21 | + public function addFilterCheckbox($parent, $groupId, $value, $label, $defaultChecked = FALSE) { |
| 22 | + $div = new Div($parent); |
| 23 | + $div->addClass('form-group'); |
| 24 | + $cb = new Checkbox($div, 'filter'.ucfirst($groupId).ucfirst($value), $groupId.':'.$value); |
| 25 | + $cb->setName('filter[]'); |
| 26 | + $cb->setLabel($label); |
| 27 | + $cb->setAnnotation('webapp/renderer', 'WebApp\\DefaultTheme\\PlainCheckboxRenderer'); |
| 28 | + if (self::hasFilter($groupId)) { |
| 29 | + $cb->setChecked(self::isFilterSet($groupId, $value)); |
| 30 | + } else { |
| 31 | + $cb->setChecked($defaultChecked); |
| 32 | + } |
| 33 | + return $cb; |
| 34 | + } |
| 35 | + |
| 36 | + public function addSection($label, $id) { |
| 37 | + $div = new Div($this->form); |
| 38 | + $div->addClass('filter-section'); |
| 39 | + $p = new Paragraph($div); |
| 40 | + $p->addClass('section-toggle'); |
| 41 | + $l = new Link($p, '#'.$id, strtoupper(I18N::_($label))); |
| 42 | + $l |
| 43 | + ->addClass('link-primary') |
| 44 | + ->setAttribute('data-toggle', 'collapse') |
| 45 | + ->setAttribute('role', 'button') |
| 46 | + ->setAttribute('aria-expanded', 'true') |
| 47 | + ->setAttribute('aria-controls', $id); |
| 48 | + |
| 49 | + $rc = new Div($div); |
| 50 | + $rc |
| 51 | + ->setId($id) |
| 52 | + ->addClass('section-content', 'collapse', 'show'); |
| 53 | + return $rc; |
| 54 | + } |
| 55 | + |
| 56 | + protected function addButtons() { |
| 57 | + $div = new Div($this->form); |
| 58 | + $div->addClass('filter-submit'); |
| 59 | + $request = Request::getRequest(); |
| 60 | + $search = $request->getParam('search'); |
| 61 | + new HiddenInput($div, 'search', $search); |
| 62 | + $btn = new SubmitButton($div, 'apply_label'); |
| 63 | + $btn->setAttribute('onclick', 'searchFilter.filterSubmit(this);return false'); |
| 64 | + } |
| 65 | + |
| 66 | + public static function getFilters($request = NULL) { |
| 67 | + if (self::$filter == NULL) { |
| 68 | + if ($request == NULL) $request = Request::getRequest(); |
| 69 | + self::$filter = array(); |
| 70 | + foreach ($request->getParam('filter', array()) AS $filter) { |
| 71 | + list($name, $value) = explode(':', $filter, 2); |
| 72 | + self::$filter[$name][] = $value; |
| 73 | + } |
| 74 | + } |
| 75 | + return self::$filter; |
| 76 | + } |
| 77 | + |
| 78 | + public static function getFilter($name, $request = NULL) { |
| 79 | + $filters = self::getFilters($request); |
| 80 | + if (isset($filters[$name])) return $filters[$name]; |
| 81 | + return array(); |
| 82 | + } |
| 83 | + |
| 84 | + public static function isFilterSet($name, $value, $request = NULL) { |
| 85 | + $filters = self::getFilter($name, $request); |
| 86 | + return in_array($value, $filters); |
| 87 | + } |
| 88 | + |
| 89 | + public static function hasFilter($name, $request = NULL) { |
| 90 | + $filters = self::getFilter($name, $request); |
| 91 | + return count($filters) > 0; |
| 92 | + } |
| 93 | + |
| 94 | +} |
| 95 | + |
0 commit comments