1 | <?php |
||
2 | |||
3 | namespace Arbory\Base\Admin\Filter; |
||
4 | |||
5 | use Closure; |
||
6 | use Arbory\Base\Html\Html; |
||
7 | use Arbory\Base\Admin\Widgets\Button; |
||
8 | use Arbory\Base\Admin\Widgets\Link; |
||
9 | use Arbory\Base\Html\Elements\Content; |
||
10 | use Arbory\Base\Html\Elements\Element; |
||
11 | |||
12 | class Renderer |
||
13 | { |
||
14 | /** |
||
15 | * @var FilterManager |
||
16 | */ |
||
17 | protected $manager; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $action; |
||
23 | |||
24 | /** |
||
25 | * @return FilterManager |
||
26 | */ |
||
27 | public function getManager(): FilterManager |
||
28 | { |
||
29 | return $this->manager; |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @param FilterManager $manager |
||
34 | * @return Renderer |
||
35 | */ |
||
36 | public function setManager(FilterManager $manager): self |
||
37 | { |
||
38 | $this->manager = $manager; |
||
39 | |||
40 | return $this; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @return Element |
||
45 | */ |
||
46 | protected function filterHeader(): Element |
||
47 | { |
||
48 | return Html::div([ |
||
49 | Html::h2(trans('arbory::filter.filter')), |
||
50 | Button::create() |
||
51 | ->type('button', 'js-filter-trigger') |
||
52 | ->withIcon('delete_outline') |
||
53 | ->iconOnly(), |
||
54 | ])->addClass('title-block'); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @return Button |
||
59 | */ |
||
60 | protected function filterButton(): Button |
||
61 | { |
||
62 | return Button::create() |
||
63 | ->type('submit', 'full-width') |
||
64 | ->title(trans('arbory::filter.apply')); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @return Link |
||
69 | */ |
||
70 | protected function saveButton(): Link |
||
71 | { |
||
72 | $linkUrl = $this->getManager()->getModule()->url('dialog', ['dialog' => 'save_filter']); |
||
73 | |||
74 | return Link::create($linkUrl) |
||
75 | ->asButton('full-width') |
||
76 | ->asAjaxbox() |
||
77 | ->title(trans('arbory::filter.save_as')); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @return Link |
||
82 | */ |
||
83 | protected function resetButton(): Link |
||
84 | { |
||
85 | return Link::create(\Request::url()) |
||
86 | ->asButton('full-width reset secondary') |
||
87 | ->title(trans('arbory::filter.reset')); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @return Content |
||
92 | */ |
||
93 | protected function renderFilters(): Content |
||
94 | { |
||
95 | return (new Content($this->manager->getFilters())) |
||
96 | ->map(Closure::fromCallable([$this, 'renderFilter'])); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @return string |
||
101 | */ |
||
102 | public function getAction(): string |
||
103 | { |
||
104 | return $this->action; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param string $action |
||
109 | * @return Renderer |
||
110 | */ |
||
111 | public function setAction(string $action): self |
||
112 | { |
||
113 | $this->action = $action; |
||
114 | |||
115 | return $this; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param FilterItem $filterItem |
||
120 | * @return Element |
||
121 | */ |
||
122 | protected function renderFilter(FilterItem $filterItem) |
||
123 | { |
||
124 | $isOpen = $filterItem->isOpen(); |
||
125 | |||
126 | if ($isOpen === null) { |
||
127 | $isOpen = ! $filterItem->getType()->isEmpty(); |
||
128 | } |
||
129 | |||
130 | return Html::div([ |
||
131 | Html::div([ |
||
132 | Html::h3($filterItem->getTitle()), |
||
133 | Button::create() |
||
134 | ->withIcon($isOpen ? 'add' : 'remove') |
||
135 | ->iconOnly() |
||
136 | ->withoutBackground(), |
||
137 | ])->addClass('js-accordion-trigger heading'), |
||
138 | Html::div([ |
||
139 | $filterItem->getType()->render($filterItem), |
||
140 | ])->addClass('body'.(! $isOpen ? ' hidden' : '')), |
||
141 | ])->addClass('accordion'); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @return Content|string |
||
146 | */ |
||
147 | public function render() |
||
148 | { |
||
149 | return new Content([ |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
150 | Html::form([ |
||
151 | $this->filterHeader(), |
||
152 | $this->renderFilters(), |
||
153 | $this->filterButton(), |
||
154 | $this->saveButton(), |
||
155 | $this->resetButton(), |
||
156 | ])->addClass('form-filter') |
||
157 | ->addAttributes(['id' => 'grid-filter']) |
||
158 | ->addAttributes(['action' => $this->action]) |
||
159 | ->addAttributes(['method' => 'get']), |
||
160 | ]); |
||
161 | } |
||
162 | } |
||
163 |