@@ -4,8 +4,8 @@ |
||
4 | 4 | |
5 | 5 | class RawFormatter implements IFormatter |
6 | 6 | { |
7 | - public function format($value): string |
|
8 | - { |
|
9 | - return $value; |
|
10 | - } |
|
7 | + public function format($value): string |
|
8 | + { |
|
9 | + return $value; |
|
10 | + } |
|
11 | 11 | } |
12 | 12 | \ No newline at end of file |
@@ -4,31 +4,31 @@ |
||
4 | 4 | |
5 | 5 | class CallbackColumn extends BaseColumn |
6 | 6 | { |
7 | - /** |
|
8 | - * @var string |
|
9 | - */ |
|
10 | - public $formatters = ['raw']; |
|
7 | + /** |
|
8 | + * @var string |
|
9 | + */ |
|
10 | + public $formatters = ['raw']; |
|
11 | 11 | |
12 | - /** |
|
13 | - * @var bool |
|
14 | - */ |
|
15 | - public $sortable = false; |
|
12 | + /** |
|
13 | + * @var bool |
|
14 | + */ |
|
15 | + public $sortable = false; |
|
16 | 16 | |
17 | - /** |
|
18 | - * @return array |
|
19 | - */ |
|
20 | - protected function configTests(): array |
|
21 | - { |
|
22 | - return array_merge(parent::configTests(), [ |
|
23 | - 'value' => 'closure', |
|
24 | - ]); |
|
25 | - } |
|
17 | + /** |
|
18 | + * @return array |
|
19 | + */ |
|
20 | + protected function configTests(): array |
|
21 | + { |
|
22 | + return array_merge(parent::configTests(), [ |
|
23 | + 'value' => 'closure', |
|
24 | + ]); |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * @inheritdoc |
|
29 | - */ |
|
30 | - public function _renderValue($row) |
|
31 | - { |
|
32 | - return call_user_func($this->value, $row); |
|
33 | - } |
|
27 | + /** |
|
28 | + * @inheritdoc |
|
29 | + */ |
|
30 | + public function _renderValue($row) |
|
31 | + { |
|
32 | + return call_user_func($this->value, $row); |
|
33 | + } |
|
34 | 34 | } |
35 | 35 | \ No newline at end of file |
@@ -9,166 +9,166 @@ |
||
9 | 9 | |
10 | 10 | abstract class BaseColumn |
11 | 11 | { |
12 | - use Configurable; |
|
13 | - |
|
14 | - /** |
|
15 | - * Column title |
|
16 | - * @var string |
|
17 | - */ |
|
18 | - public $title = ''; |
|
19 | - |
|
20 | - /** |
|
21 | - * Column value. Could be an attribute, |
|
22 | - * @var string|mixed |
|
23 | - */ |
|
24 | - public $value = ''; |
|
25 | - |
|
26 | - /** |
|
27 | - * @var BaseFilter |
|
28 | - */ |
|
29 | - public $filter; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var boolean|string |
|
33 | - */ |
|
34 | - public $sortable = true; |
|
35 | - |
|
36 | - /** |
|
37 | - * @var array |
|
38 | - */ |
|
39 | - public $headerHtmlOptions = []; |
|
40 | - |
|
41 | - /** |
|
42 | - * @var array |
|
43 | - */ |
|
44 | - public $contentHtmlOptions = []; |
|
45 | - |
|
46 | - /** |
|
47 | - * @var array - allowed: raw, url, email, text, image |
|
48 | - */ |
|
49 | - public $formatters = ['text']; |
|
50 | - |
|
51 | - /** |
|
52 | - * Value when column is empty |
|
53 | - * @var string |
|
54 | - */ |
|
55 | - public $emptyValue = '-'; |
|
56 | - |
|
57 | - /** |
|
58 | - * BaseColumn constructor. |
|
59 | - * @param array $config |
|
60 | - * @throws \Woo\GridView\Exceptions\GridViewConfigException |
|
61 | - */ |
|
62 | - public function __construct(array $config) |
|
63 | - { |
|
64 | - $this->loadConfig($config); |
|
65 | - |
|
66 | - $this->buildFilter(); |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * Allows to get sortable column's name |
|
71 | - */ |
|
72 | - public function getSortableName() |
|
73 | - { |
|
74 | - if ($this->sortable === false) { |
|
75 | - return false; |
|
76 | - } |
|
77 | - |
|
78 | - if (!is_bool($this->sortable)) { |
|
79 | - return $this->sortable; |
|
80 | - } |
|
81 | - |
|
82 | - if (is_scalar($this->value)) { |
|
83 | - return $this->value; |
|
84 | - } |
|
85 | - |
|
86 | - return false; |
|
87 | - } |
|
88 | - |
|
89 | - protected function buildFilter() |
|
90 | - { |
|
91 | - if (is_null($this->filter) || is_object($this->filter)) { |
|
92 | - return; |
|
93 | - } |
|
94 | - |
|
95 | - if (is_string($this->filter)) { |
|
96 | - $this->filter = [ |
|
97 | - 'class' => $this->filter, |
|
98 | - 'name' => $this->value, |
|
99 | - ]; |
|
100 | - } |
|
101 | - |
|
102 | - if (empty($this->filter['class'])) { |
|
103 | - $this->filter['class'] = TextFilter::class; |
|
104 | - } |
|
105 | - |
|
106 | - if (empty($this->filter['name'])) { |
|
107 | - $this->filter['name'] = $this->value; |
|
108 | - } |
|
109 | - |
|
110 | - $className = GridViewHelper::resolveAlias('filter', $this->filter['class']); |
|
111 | - $this->filter = new $className($this->filter); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @return array |
|
116 | - */ |
|
117 | - protected function configTests(): array |
|
118 | - { |
|
119 | - return [ |
|
120 | - 'title' => 'string', |
|
121 | - 'value' => 'any', |
|
122 | - 'headerHtmlOptions' => 'array', |
|
123 | - 'contentHtmlOptions' => 'array', |
|
124 | - 'formatters' => 'array', |
|
125 | - 'emptyValue' => 'string', |
|
126 | - 'sortable' => 'any', |
|
127 | - 'filter' => BaseFilter::class . '|null', |
|
128 | - ]; |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Formatted header html options |
|
133 | - * @return string |
|
134 | - */ |
|
135 | - public function compileHeaderHtmlOptions() : string |
|
136 | - { |
|
137 | - return GridViewHelper::htmlOptionsToString($this->headerHtmlOptions); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Formatted content html options |
|
142 | - * @param array $context |
|
143 | - * @return string |
|
144 | - */ |
|
145 | - public function compileContentHtmlOptions(array $context) : string |
|
146 | - { |
|
147 | - return GridViewHelper::htmlOptionsToString($this->contentHtmlOptions, $context); |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Render column value for row |
|
152 | - * @param array|object $row |
|
153 | - * @return string|mixed |
|
154 | - */ |
|
155 | - protected abstract function _renderValue($row); |
|
156 | - |
|
157 | - /** |
|
158 | - * Renders column content |
|
159 | - * @param $row |
|
160 | - * @return string |
|
161 | - */ |
|
162 | - public function renderValue($row) |
|
163 | - { |
|
164 | - $value = $this->_renderValue($row); |
|
165 | - |
|
166 | - foreach ($this->formatters as $formatter) { |
|
167 | - $className = GridViewHelper::resolveAlias('formatter', $formatter); |
|
168 | - $value = (new $className)->format($value); |
|
169 | - } |
|
170 | - |
|
171 | - return $value; |
|
172 | - } |
|
12 | + use Configurable; |
|
13 | + |
|
14 | + /** |
|
15 | + * Column title |
|
16 | + * @var string |
|
17 | + */ |
|
18 | + public $title = ''; |
|
19 | + |
|
20 | + /** |
|
21 | + * Column value. Could be an attribute, |
|
22 | + * @var string|mixed |
|
23 | + */ |
|
24 | + public $value = ''; |
|
25 | + |
|
26 | + /** |
|
27 | + * @var BaseFilter |
|
28 | + */ |
|
29 | + public $filter; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var boolean|string |
|
33 | + */ |
|
34 | + public $sortable = true; |
|
35 | + |
|
36 | + /** |
|
37 | + * @var array |
|
38 | + */ |
|
39 | + public $headerHtmlOptions = []; |
|
40 | + |
|
41 | + /** |
|
42 | + * @var array |
|
43 | + */ |
|
44 | + public $contentHtmlOptions = []; |
|
45 | + |
|
46 | + /** |
|
47 | + * @var array - allowed: raw, url, email, text, image |
|
48 | + */ |
|
49 | + public $formatters = ['text']; |
|
50 | + |
|
51 | + /** |
|
52 | + * Value when column is empty |
|
53 | + * @var string |
|
54 | + */ |
|
55 | + public $emptyValue = '-'; |
|
56 | + |
|
57 | + /** |
|
58 | + * BaseColumn constructor. |
|
59 | + * @param array $config |
|
60 | + * @throws \Woo\GridView\Exceptions\GridViewConfigException |
|
61 | + */ |
|
62 | + public function __construct(array $config) |
|
63 | + { |
|
64 | + $this->loadConfig($config); |
|
65 | + |
|
66 | + $this->buildFilter(); |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * Allows to get sortable column's name |
|
71 | + */ |
|
72 | + public function getSortableName() |
|
73 | + { |
|
74 | + if ($this->sortable === false) { |
|
75 | + return false; |
|
76 | + } |
|
77 | + |
|
78 | + if (!is_bool($this->sortable)) { |
|
79 | + return $this->sortable; |
|
80 | + } |
|
81 | + |
|
82 | + if (is_scalar($this->value)) { |
|
83 | + return $this->value; |
|
84 | + } |
|
85 | + |
|
86 | + return false; |
|
87 | + } |
|
88 | + |
|
89 | + protected function buildFilter() |
|
90 | + { |
|
91 | + if (is_null($this->filter) || is_object($this->filter)) { |
|
92 | + return; |
|
93 | + } |
|
94 | + |
|
95 | + if (is_string($this->filter)) { |
|
96 | + $this->filter = [ |
|
97 | + 'class' => $this->filter, |
|
98 | + 'name' => $this->value, |
|
99 | + ]; |
|
100 | + } |
|
101 | + |
|
102 | + if (empty($this->filter['class'])) { |
|
103 | + $this->filter['class'] = TextFilter::class; |
|
104 | + } |
|
105 | + |
|
106 | + if (empty($this->filter['name'])) { |
|
107 | + $this->filter['name'] = $this->value; |
|
108 | + } |
|
109 | + |
|
110 | + $className = GridViewHelper::resolveAlias('filter', $this->filter['class']); |
|
111 | + $this->filter = new $className($this->filter); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @return array |
|
116 | + */ |
|
117 | + protected function configTests(): array |
|
118 | + { |
|
119 | + return [ |
|
120 | + 'title' => 'string', |
|
121 | + 'value' => 'any', |
|
122 | + 'headerHtmlOptions' => 'array', |
|
123 | + 'contentHtmlOptions' => 'array', |
|
124 | + 'formatters' => 'array', |
|
125 | + 'emptyValue' => 'string', |
|
126 | + 'sortable' => 'any', |
|
127 | + 'filter' => BaseFilter::class . '|null', |
|
128 | + ]; |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Formatted header html options |
|
133 | + * @return string |
|
134 | + */ |
|
135 | + public function compileHeaderHtmlOptions() : string |
|
136 | + { |
|
137 | + return GridViewHelper::htmlOptionsToString($this->headerHtmlOptions); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Formatted content html options |
|
142 | + * @param array $context |
|
143 | + * @return string |
|
144 | + */ |
|
145 | + public function compileContentHtmlOptions(array $context) : string |
|
146 | + { |
|
147 | + return GridViewHelper::htmlOptionsToString($this->contentHtmlOptions, $context); |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Render column value for row |
|
152 | + * @param array|object $row |
|
153 | + * @return string|mixed |
|
154 | + */ |
|
155 | + protected abstract function _renderValue($row); |
|
156 | + |
|
157 | + /** |
|
158 | + * Renders column content |
|
159 | + * @param $row |
|
160 | + * @return string |
|
161 | + */ |
|
162 | + public function renderValue($row) |
|
163 | + { |
|
164 | + $value = $this->_renderValue($row); |
|
165 | + |
|
166 | + foreach ($this->formatters as $formatter) { |
|
167 | + $className = GridViewHelper::resolveAlias('formatter', $formatter); |
|
168 | + $value = (new $className)->format($value); |
|
169 | + } |
|
170 | + |
|
171 | + return $value; |
|
172 | + } |
|
173 | 173 | |
174 | 174 | } |
@@ -15,232 +15,232 @@ |
||
15 | 15 | |
16 | 16 | class GridView |
17 | 17 | { |
18 | - use Configurable; |
|
19 | - |
|
20 | - /** |
|
21 | - * Counter for ids |
|
22 | - * @var int |
|
23 | - */ |
|
24 | - private static $counter = 0; |
|
25 | - |
|
26 | - /** |
|
27 | - * Grid id (used for request handling, for |
|
28 | - * @var int |
|
29 | - */ |
|
30 | - private $id; |
|
31 | - |
|
32 | - /** |
|
33 | - * DataProvider provides gridview with the data for representation |
|
34 | - * @var BaseDataProvider |
|
35 | - */ |
|
36 | - public $dataProvider; |
|
37 | - |
|
38 | - /** |
|
39 | - * Columns config. You may specify array or GridColumn instance |
|
40 | - * @var BaseColumn[] |
|
41 | - */ |
|
42 | - public $columns = []; |
|
43 | - |
|
44 | - /** |
|
45 | - * Common options for all columns, will be appended to all columns configs |
|
46 | - * @var array |
|
47 | - */ |
|
48 | - public $columnOptions = [ |
|
49 | - 'class' => AttributeColumn::class, |
|
50 | - ]; |
|
51 | - |
|
52 | - /** |
|
53 | - * Renders the final UI |
|
54 | - * @var string|BaseRenderer |
|
55 | - */ |
|
56 | - public $renderer = DefaultRenderer::class; |
|
57 | - |
|
58 | - /** |
|
59 | - * Allows to pass some options into renderer/customize rendered behavior |
|
60 | - * @var array |
|
61 | - */ |
|
62 | - public $rendererOptions = []; |
|
63 | - |
|
64 | - /** |
|
65 | - * Controls amount of data per page |
|
66 | - * @var int |
|
67 | - */ |
|
68 | - public $rowsPerPage = 25; |
|
69 | - |
|
70 | - /** |
|
71 | - * Allows to tune the <table> tag with html options |
|
72 | - * @var array |
|
73 | - */ |
|
74 | - public $tableHtmlOptions = [ |
|
75 | - 'class' => 'table table-bordered gridview-table', |
|
76 | - ]; |
|
77 | - |
|
78 | - /** |
|
79 | - * Indicate if filters will be shown or not |
|
80 | - * @var bool |
|
81 | - */ |
|
82 | - public $showFilters = true; |
|
83 | - |
|
84 | - /** |
|
85 | - * Flags allow to change standalone vue to. If false, GridView component should be included in your root Vue instance |
|
86 | - * @var bool |
|
87 | - */ |
|
88 | - public $standaloneVue = true; |
|
89 | - |
|
90 | - /** |
|
91 | - * List of additinal params which'll be added to filter requests |
|
92 | - * @var array |
|
93 | - */ |
|
94 | - public $additionalRequestParams = []; |
|
95 | - |
|
96 | - /** |
|
97 | - * @var Paginator |
|
98 | - */ |
|
99 | - protected $pagination; |
|
100 | - |
|
101 | - /** |
|
102 | - * @var GridViewRequest |
|
103 | - */ |
|
104 | - protected $request; |
|
105 | - |
|
106 | - /** |
|
107 | - * GridView constructor. |
|
108 | - * @param array $config |
|
109 | - * @throws Exceptions\GridViewConfigException |
|
110 | - */ |
|
111 | - public function __construct(array $config) |
|
112 | - { |
|
113 | - $this->id = self::$counter++; |
|
114 | - |
|
115 | - $this->loadConfig($config); |
|
116 | - |
|
117 | - /** |
|
118 | - * Making renderer |
|
119 | - */ |
|
120 | - if (!is_object($this->renderer)) { |
|
121 | - $className = GridViewHelper::resolveAlias('renderer', $this->renderer); |
|
122 | - $this->renderer = new $className(array_merge( |
|
123 | - $this->rendererOptions, [ |
|
124 | - 'gridView' => $this, |
|
125 | - ] |
|
126 | - )); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Build columns from config |
|
131 | - */ |
|
132 | - $this->buildColumns(); |
|
133 | - |
|
134 | - $this->request = GridViewRequest::parse($this->id); |
|
135 | - $this->request->perPage = $this->rowsPerPage; |
|
136 | - |
|
137 | - $this->pagination = new LengthAwarePaginator( |
|
138 | - $this->dataProvider->getData($this->request), |
|
139 | - $this->dataProvider->getCount($this->request), |
|
140 | - $this->rowsPerPage, |
|
141 | - $this->request->page |
|
142 | - ); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @return array |
|
147 | - */ |
|
148 | - protected function configTests(): array |
|
149 | - { |
|
150 | - return [ |
|
151 | - 'dataProvider' => BaseDataProvider::class, |
|
152 | - 'columns' => 'array', |
|
153 | - 'renderer' => BaseRenderer::class, |
|
154 | - 'rowsPerPage' => 'int', |
|
155 | - 'tableHtmlOptions' => 'array', |
|
156 | - 'showFilters' => 'boolean', |
|
157 | - ]; |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * Build columns into objects |
|
162 | - */ |
|
163 | - protected function buildColumns() |
|
164 | - { |
|
165 | - foreach ($this->columns as $key => &$columnOptions) { |
|
166 | - |
|
167 | - /** |
|
168 | - * In case of when column is already build |
|
169 | - */ |
|
170 | - if (is_object($columnOptions)) { |
|
171 | - continue; |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * When only attribute name/value passed |
|
176 | - */ |
|
177 | - if (is_string($columnOptions)) { |
|
178 | - $columnOptions = [ |
|
179 | - 'value' => $columnOptions, |
|
180 | - ]; |
|
181 | - } |
|
182 | - |
|
183 | - if ($columnOptions instanceof Closure) { |
|
184 | - $columnOptions = [ |
|
185 | - 'class' => CallbackColumn::class, |
|
186 | - 'value' => $columnOptions, |
|
187 | - 'title' => GridViewHelper::columnTitle($key), |
|
188 | - ]; |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Inline column declaration detector |
|
193 | - */ |
|
194 | - if (is_string($columnOptions['value']) && strpos($columnOptions['value'], 'view:') === 0) { |
|
195 | - $columnOptions['class'] = 'view'; |
|
196 | - $columnOptions['value'] = str_replace('view:', '', $columnOptions['value']); |
|
197 | - } |
|
198 | - |
|
199 | - $columnOptions = array_merge($this->columnOptions, $columnOptions); |
|
200 | - |
|
201 | - $className = GridViewHelper::resolveAlias('column', $columnOptions['class']); |
|
202 | - $columnOptions = new $className($columnOptions); |
|
203 | - } |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * Draws widget and return html code |
|
208 | - * @return string |
|
209 | - */ |
|
210 | - public function render() |
|
211 | - { |
|
212 | - return $this->renderer->render($this); |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * @return LengthAwarePaginator|Paginator |
|
217 | - */ |
|
218 | - public function getPagination() |
|
219 | - { |
|
220 | - return $this->pagination; |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * @return GridViewRequest |
|
225 | - */ |
|
226 | - public function getRequest() |
|
227 | - { |
|
228 | - return $this->request; |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * @return int |
|
233 | - */ |
|
234 | - public function getId() : int |
|
235 | - { |
|
236 | - return $this->id; |
|
237 | - } |
|
238 | - |
|
239 | - /** |
|
240 | - * @return string |
|
241 | - */ |
|
242 | - public function compileTableHtmlOptions() : string |
|
243 | - { |
|
244 | - return GridViewHelper::htmlOptionsToString($this->tableHtmlOptions); |
|
245 | - } |
|
18 | + use Configurable; |
|
19 | + |
|
20 | + /** |
|
21 | + * Counter for ids |
|
22 | + * @var int |
|
23 | + */ |
|
24 | + private static $counter = 0; |
|
25 | + |
|
26 | + /** |
|
27 | + * Grid id (used for request handling, for |
|
28 | + * @var int |
|
29 | + */ |
|
30 | + private $id; |
|
31 | + |
|
32 | + /** |
|
33 | + * DataProvider provides gridview with the data for representation |
|
34 | + * @var BaseDataProvider |
|
35 | + */ |
|
36 | + public $dataProvider; |
|
37 | + |
|
38 | + /** |
|
39 | + * Columns config. You may specify array or GridColumn instance |
|
40 | + * @var BaseColumn[] |
|
41 | + */ |
|
42 | + public $columns = []; |
|
43 | + |
|
44 | + /** |
|
45 | + * Common options for all columns, will be appended to all columns configs |
|
46 | + * @var array |
|
47 | + */ |
|
48 | + public $columnOptions = [ |
|
49 | + 'class' => AttributeColumn::class, |
|
50 | + ]; |
|
51 | + |
|
52 | + /** |
|
53 | + * Renders the final UI |
|
54 | + * @var string|BaseRenderer |
|
55 | + */ |
|
56 | + public $renderer = DefaultRenderer::class; |
|
57 | + |
|
58 | + /** |
|
59 | + * Allows to pass some options into renderer/customize rendered behavior |
|
60 | + * @var array |
|
61 | + */ |
|
62 | + public $rendererOptions = []; |
|
63 | + |
|
64 | + /** |
|
65 | + * Controls amount of data per page |
|
66 | + * @var int |
|
67 | + */ |
|
68 | + public $rowsPerPage = 25; |
|
69 | + |
|
70 | + /** |
|
71 | + * Allows to tune the <table> tag with html options |
|
72 | + * @var array |
|
73 | + */ |
|
74 | + public $tableHtmlOptions = [ |
|
75 | + 'class' => 'table table-bordered gridview-table', |
|
76 | + ]; |
|
77 | + |
|
78 | + /** |
|
79 | + * Indicate if filters will be shown or not |
|
80 | + * @var bool |
|
81 | + */ |
|
82 | + public $showFilters = true; |
|
83 | + |
|
84 | + /** |
|
85 | + * Flags allow to change standalone vue to. If false, GridView component should be included in your root Vue instance |
|
86 | + * @var bool |
|
87 | + */ |
|
88 | + public $standaloneVue = true; |
|
89 | + |
|
90 | + /** |
|
91 | + * List of additinal params which'll be added to filter requests |
|
92 | + * @var array |
|
93 | + */ |
|
94 | + public $additionalRequestParams = []; |
|
95 | + |
|
96 | + /** |
|
97 | + * @var Paginator |
|
98 | + */ |
|
99 | + protected $pagination; |
|
100 | + |
|
101 | + /** |
|
102 | + * @var GridViewRequest |
|
103 | + */ |
|
104 | + protected $request; |
|
105 | + |
|
106 | + /** |
|
107 | + * GridView constructor. |
|
108 | + * @param array $config |
|
109 | + * @throws Exceptions\GridViewConfigException |
|
110 | + */ |
|
111 | + public function __construct(array $config) |
|
112 | + { |
|
113 | + $this->id = self::$counter++; |
|
114 | + |
|
115 | + $this->loadConfig($config); |
|
116 | + |
|
117 | + /** |
|
118 | + * Making renderer |
|
119 | + */ |
|
120 | + if (!is_object($this->renderer)) { |
|
121 | + $className = GridViewHelper::resolveAlias('renderer', $this->renderer); |
|
122 | + $this->renderer = new $className(array_merge( |
|
123 | + $this->rendererOptions, [ |
|
124 | + 'gridView' => $this, |
|
125 | + ] |
|
126 | + )); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Build columns from config |
|
131 | + */ |
|
132 | + $this->buildColumns(); |
|
133 | + |
|
134 | + $this->request = GridViewRequest::parse($this->id); |
|
135 | + $this->request->perPage = $this->rowsPerPage; |
|
136 | + |
|
137 | + $this->pagination = new LengthAwarePaginator( |
|
138 | + $this->dataProvider->getData($this->request), |
|
139 | + $this->dataProvider->getCount($this->request), |
|
140 | + $this->rowsPerPage, |
|
141 | + $this->request->page |
|
142 | + ); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @return array |
|
147 | + */ |
|
148 | + protected function configTests(): array |
|
149 | + { |
|
150 | + return [ |
|
151 | + 'dataProvider' => BaseDataProvider::class, |
|
152 | + 'columns' => 'array', |
|
153 | + 'renderer' => BaseRenderer::class, |
|
154 | + 'rowsPerPage' => 'int', |
|
155 | + 'tableHtmlOptions' => 'array', |
|
156 | + 'showFilters' => 'boolean', |
|
157 | + ]; |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * Build columns into objects |
|
162 | + */ |
|
163 | + protected function buildColumns() |
|
164 | + { |
|
165 | + foreach ($this->columns as $key => &$columnOptions) { |
|
166 | + |
|
167 | + /** |
|
168 | + * In case of when column is already build |
|
169 | + */ |
|
170 | + if (is_object($columnOptions)) { |
|
171 | + continue; |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * When only attribute name/value passed |
|
176 | + */ |
|
177 | + if (is_string($columnOptions)) { |
|
178 | + $columnOptions = [ |
|
179 | + 'value' => $columnOptions, |
|
180 | + ]; |
|
181 | + } |
|
182 | + |
|
183 | + if ($columnOptions instanceof Closure) { |
|
184 | + $columnOptions = [ |
|
185 | + 'class' => CallbackColumn::class, |
|
186 | + 'value' => $columnOptions, |
|
187 | + 'title' => GridViewHelper::columnTitle($key), |
|
188 | + ]; |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Inline column declaration detector |
|
193 | + */ |
|
194 | + if (is_string($columnOptions['value']) && strpos($columnOptions['value'], 'view:') === 0) { |
|
195 | + $columnOptions['class'] = 'view'; |
|
196 | + $columnOptions['value'] = str_replace('view:', '', $columnOptions['value']); |
|
197 | + } |
|
198 | + |
|
199 | + $columnOptions = array_merge($this->columnOptions, $columnOptions); |
|
200 | + |
|
201 | + $className = GridViewHelper::resolveAlias('column', $columnOptions['class']); |
|
202 | + $columnOptions = new $className($columnOptions); |
|
203 | + } |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * Draws widget and return html code |
|
208 | + * @return string |
|
209 | + */ |
|
210 | + public function render() |
|
211 | + { |
|
212 | + return $this->renderer->render($this); |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * @return LengthAwarePaginator|Paginator |
|
217 | + */ |
|
218 | + public function getPagination() |
|
219 | + { |
|
220 | + return $this->pagination; |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * @return GridViewRequest |
|
225 | + */ |
|
226 | + public function getRequest() |
|
227 | + { |
|
228 | + return $this->request; |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * @return int |
|
233 | + */ |
|
234 | + public function getId() : int |
|
235 | + { |
|
236 | + return $this->id; |
|
237 | + } |
|
238 | + |
|
239 | + /** |
|
240 | + * @return string |
|
241 | + */ |
|
242 | + public function compileTableHtmlOptions() : string |
|
243 | + { |
|
244 | + return GridViewHelper::htmlOptionsToString($this->tableHtmlOptions); |
|
245 | + } |
|
246 | 246 | } |
@@ -6,58 +6,58 @@ |
||
6 | 6 | |
7 | 7 | abstract class BaseDataProvider |
8 | 8 | { |
9 | - /** |
|
10 | - * true means that all request filters are accepted, checks by default comparing (like) |
|
11 | - * false means that filtering is not enabled in this dataprovider |
|
12 | - * array should contain array of fields, available for filtering. |
|
13 | - * if key not specified, value should be a name of field, otherwise key - field name, |
|
14 | - * value - comparing type (=, like) or a callable function |
|
15 | - * @var bool|array |
|
16 | - */ |
|
17 | - protected $filters = false; |
|
18 | - |
|
19 | - /** |
|
20 | - * true means that all request sortings are accepted, checks by default sorting |
|
21 | - * false means that ordering is not enabled in this dataprovider |
|
22 | - * array should contain array of fields, available for filtering. |
|
23 | - * value should be a string |
|
24 | - * @var bool|array |
|
25 | - */ |
|
26 | - protected $ordering = false; |
|
27 | - |
|
28 | - /** |
|
29 | - * Should return total amount of rows |
|
30 | - * @param GridViewRequest $request |
|
31 | - * @return int |
|
32 | - */ |
|
33 | - abstract public function getCount(GridViewRequest $request) : int; |
|
34 | - |
|
35 | - /** |
|
36 | - * Should return a list of data for current page |
|
37 | - * @param GridViewRequest $request |
|
38 | - * @return mixed |
|
39 | - */ |
|
40 | - abstract public function getData(GridViewRequest $request); |
|
41 | - |
|
42 | - /** |
|
43 | - * Allows to set a list of fields, available for filtering |
|
44 | - * @param array|boolean $filters |
|
45 | - * @return $this |
|
46 | - */ |
|
47 | - public function setFilters($filters) |
|
48 | - { |
|
49 | - $this->filters = $filters; |
|
50 | - return $this; |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * Allows to set a list of ordering fields |
|
55 | - * @param array|boolean $ordering |
|
56 | - * @return $this |
|
57 | - */ |
|
58 | - public function setOrdering($ordering) |
|
59 | - { |
|
60 | - $this->ordering = $ordering; |
|
61 | - return $this; |
|
62 | - } |
|
9 | + /** |
|
10 | + * true means that all request filters are accepted, checks by default comparing (like) |
|
11 | + * false means that filtering is not enabled in this dataprovider |
|
12 | + * array should contain array of fields, available for filtering. |
|
13 | + * if key not specified, value should be a name of field, otherwise key - field name, |
|
14 | + * value - comparing type (=, like) or a callable function |
|
15 | + * @var bool|array |
|
16 | + */ |
|
17 | + protected $filters = false; |
|
18 | + |
|
19 | + /** |
|
20 | + * true means that all request sortings are accepted, checks by default sorting |
|
21 | + * false means that ordering is not enabled in this dataprovider |
|
22 | + * array should contain array of fields, available for filtering. |
|
23 | + * value should be a string |
|
24 | + * @var bool|array |
|
25 | + */ |
|
26 | + protected $ordering = false; |
|
27 | + |
|
28 | + /** |
|
29 | + * Should return total amount of rows |
|
30 | + * @param GridViewRequest $request |
|
31 | + * @return int |
|
32 | + */ |
|
33 | + abstract public function getCount(GridViewRequest $request) : int; |
|
34 | + |
|
35 | + /** |
|
36 | + * Should return a list of data for current page |
|
37 | + * @param GridViewRequest $request |
|
38 | + * @return mixed |
|
39 | + */ |
|
40 | + abstract public function getData(GridViewRequest $request); |
|
41 | + |
|
42 | + /** |
|
43 | + * Allows to set a list of fields, available for filtering |
|
44 | + * @param array|boolean $filters |
|
45 | + * @return $this |
|
46 | + */ |
|
47 | + public function setFilters($filters) |
|
48 | + { |
|
49 | + $this->filters = $filters; |
|
50 | + return $this; |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * Allows to set a list of ordering fields |
|
55 | + * @param array|boolean $ordering |
|
56 | + * @return $this |
|
57 | + */ |
|
58 | + public function setOrdering($ordering) |
|
59 | + { |
|
60 | + $this->ordering = $ordering; |
|
61 | + return $this; |
|
62 | + } |
|
63 | 63 | } |
@@ -8,96 +8,96 @@ |
||
8 | 8 | |
9 | 9 | class EloquentDataProvider extends BaseDataProvider |
10 | 10 | { |
11 | - protected $filters = true; |
|
12 | - protected $ordering = true; |
|
13 | - |
|
14 | - protected $query; |
|
15 | - |
|
16 | - /** |
|
17 | - * EloquentDataProvider constructor. |
|
18 | - * @param Builder $query |
|
19 | - */ |
|
20 | - public function __construct(Builder $query) |
|
21 | - { |
|
22 | - $this->query = clone $query; |
|
23 | - } |
|
24 | - |
|
25 | - /** |
|
26 | - * Applies filter to a column |
|
27 | - * @param \Closure| $filter |
|
28 | - * @param string $fieldName |
|
29 | - * @param Builder $query |
|
30 | - * @param mixed $value |
|
31 | - * @return void |
|
32 | - */ |
|
33 | - private function applyFilter($filter, string $fieldName, Builder $query, $value) |
|
34 | - { |
|
35 | - if (is_callable($filter)) { |
|
36 | - $filter($query, $value); |
|
37 | - return; |
|
38 | - } |
|
39 | - |
|
40 | - switch ($filter) { |
|
41 | - case '=': |
|
42 | - $query->where($fieldName, '=', $value); |
|
43 | - break; |
|
44 | - |
|
45 | - case 'like': |
|
46 | - $query->where($fieldName, 'LIKE', '%' . $value . '%'); |
|
47 | - break; |
|
48 | - |
|
49 | - default: |
|
50 | - throw new GridViewConfigException('Unknown filter type: ' . $filter); |
|
51 | - } |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * @param GridViewRequest $request |
|
56 | - * @return Builder |
|
57 | - */ |
|
58 | - protected function baseQuery(GridViewRequest $request) |
|
59 | - { |
|
60 | - $query = clone $this->query; |
|
61 | - |
|
62 | - if ($this->filters !== false) { |
|
63 | - foreach ($request->filters as $field => $value) { |
|
64 | - if ($this->filters === true || in_array($field, $this->filters)) { |
|
65 | - $query->where($field, 'LIKE', '%' . $value . '%'); |
|
66 | - |
|
67 | - } elseif (!empty($this->filters[$field])) { |
|
68 | - $this->applyFilter($this->filters[$field], $field, $query, $value); |
|
69 | - } |
|
70 | - } |
|
71 | - } |
|
72 | - |
|
73 | - if ($request->sortColumn && ($this->ordering === true || in_array($request->sortColumn, $this->ordering))) { |
|
74 | - $query->reorder($request->sortColumn, $request->sortOrder); |
|
75 | - } |
|
76 | - |
|
77 | - return $query; |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * @inheritdoc |
|
82 | - */ |
|
83 | - public function getCount(GridViewRequest $request) : int |
|
84 | - { |
|
85 | - return $this->baseQuery($request)->count(); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * @inheritdoc |
|
90 | - */ |
|
91 | - public function getData(GridViewRequest $request) |
|
92 | - { |
|
93 | - $query = $this->baseQuery($request); |
|
94 | - |
|
95 | - if ($request->perPage == 0) { |
|
96 | - return $query->get(); |
|
97 | - } |
|
98 | - |
|
99 | - return $query->offset(($request->page - 1) * $request->perPage) |
|
100 | - ->limit($request->perPage) |
|
101 | - ->get(); |
|
102 | - } |
|
11 | + protected $filters = true; |
|
12 | + protected $ordering = true; |
|
13 | + |
|
14 | + protected $query; |
|
15 | + |
|
16 | + /** |
|
17 | + * EloquentDataProvider constructor. |
|
18 | + * @param Builder $query |
|
19 | + */ |
|
20 | + public function __construct(Builder $query) |
|
21 | + { |
|
22 | + $this->query = clone $query; |
|
23 | + } |
|
24 | + |
|
25 | + /** |
|
26 | + * Applies filter to a column |
|
27 | + * @param \Closure| $filter |
|
28 | + * @param string $fieldName |
|
29 | + * @param Builder $query |
|
30 | + * @param mixed $value |
|
31 | + * @return void |
|
32 | + */ |
|
33 | + private function applyFilter($filter, string $fieldName, Builder $query, $value) |
|
34 | + { |
|
35 | + if (is_callable($filter)) { |
|
36 | + $filter($query, $value); |
|
37 | + return; |
|
38 | + } |
|
39 | + |
|
40 | + switch ($filter) { |
|
41 | + case '=': |
|
42 | + $query->where($fieldName, '=', $value); |
|
43 | + break; |
|
44 | + |
|
45 | + case 'like': |
|
46 | + $query->where($fieldName, 'LIKE', '%' . $value . '%'); |
|
47 | + break; |
|
48 | + |
|
49 | + default: |
|
50 | + throw new GridViewConfigException('Unknown filter type: ' . $filter); |
|
51 | + } |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * @param GridViewRequest $request |
|
56 | + * @return Builder |
|
57 | + */ |
|
58 | + protected function baseQuery(GridViewRequest $request) |
|
59 | + { |
|
60 | + $query = clone $this->query; |
|
61 | + |
|
62 | + if ($this->filters !== false) { |
|
63 | + foreach ($request->filters as $field => $value) { |
|
64 | + if ($this->filters === true || in_array($field, $this->filters)) { |
|
65 | + $query->where($field, 'LIKE', '%' . $value . '%'); |
|
66 | + |
|
67 | + } elseif (!empty($this->filters[$field])) { |
|
68 | + $this->applyFilter($this->filters[$field], $field, $query, $value); |
|
69 | + } |
|
70 | + } |
|
71 | + } |
|
72 | + |
|
73 | + if ($request->sortColumn && ($this->ordering === true || in_array($request->sortColumn, $this->ordering))) { |
|
74 | + $query->reorder($request->sortColumn, $request->sortOrder); |
|
75 | + } |
|
76 | + |
|
77 | + return $query; |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * @inheritdoc |
|
82 | + */ |
|
83 | + public function getCount(GridViewRequest $request) : int |
|
84 | + { |
|
85 | + return $this->baseQuery($request)->count(); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * @inheritdoc |
|
90 | + */ |
|
91 | + public function getData(GridViewRequest $request) |
|
92 | + { |
|
93 | + $query = $this->baseQuery($request); |
|
94 | + |
|
95 | + if ($request->perPage == 0) { |
|
96 | + return $query->get(); |
|
97 | + } |
|
98 | + |
|
99 | + return $query->offset(($request->page - 1) * $request->perPage) |
|
100 | + ->limit($request->perPage) |
|
101 | + ->get(); |
|
102 | + } |
|
103 | 103 | } |
@@ -7,25 +7,25 @@ |
||
7 | 7 | |
8 | 8 | abstract class BaseFilter |
9 | 9 | { |
10 | - use Configurable; |
|
10 | + use Configurable; |
|
11 | 11 | |
12 | - public $name; |
|
13 | - public $cssClass = 'form-control'; |
|
12 | + public $name; |
|
13 | + public $cssClass = 'form-control'; |
|
14 | 14 | |
15 | - public function __construct(array $config) |
|
16 | - { |
|
17 | - if (!empty($config['cssClass'])) { |
|
18 | - $config['cssClass'] .= ' '.$this->cssClass; |
|
19 | - } |
|
20 | - $this->loadConfig($config); |
|
21 | - } |
|
15 | + public function __construct(array $config) |
|
16 | + { |
|
17 | + if (!empty($config['cssClass'])) { |
|
18 | + $config['cssClass'] .= ' '.$this->cssClass; |
|
19 | + } |
|
20 | + $this->loadConfig($config); |
|
21 | + } |
|
22 | 22 | |
23 | - protected function configTests(): array |
|
24 | - { |
|
25 | - return [ |
|
26 | - 'name' => 'string', |
|
27 | - ]; |
|
28 | - } |
|
23 | + protected function configTests(): array |
|
24 | + { |
|
25 | + return [ |
|
26 | + 'name' => 'string', |
|
27 | + ]; |
|
28 | + } |
|
29 | 29 | |
30 | - abstract public function render(GridView $grid) : string; |
|
30 | + abstract public function render(GridView $grid) : string; |
|
31 | 31 | } |
@@ -15,7 +15,7 @@ |
||
15 | 15 | public function __construct(array $config) |
16 | 16 | { |
17 | 17 | if (!empty($config['cssClass'])) { |
18 | - $config['cssClass'] .= ' '.$this->cssClass; |
|
18 | + $config['cssClass'] .= ' ' . $this->cssClass; |
|
19 | 19 | } |
20 | 20 | $this->loadConfig($config); |
21 | 21 | } |
@@ -6,12 +6,12 @@ |
||
6 | 6 | |
7 | 7 | class TextFilter extends BaseFilter |
8 | 8 | { |
9 | - public function render(GridView $grid): string |
|
10 | - { |
|
11 | - return view('woo_gridview::filters.text-filter', [ |
|
12 | - 'name' => $this->name, |
|
13 | - 'value' => $grid->getRequest()->filters[$this->name] ?? '', |
|
14 | - 'cssClass' => $this->cssClass, |
|
15 | - ]); |
|
16 | - } |
|
9 | + public function render(GridView $grid): string |
|
10 | + { |
|
11 | + return view('woo_gridview::filters.text-filter', [ |
|
12 | + 'name' => $this->name, |
|
13 | + 'value' => $grid->getRequest()->filters[$this->name] ?? '', |
|
14 | + 'cssClass' => $this->cssClass, |
|
15 | + ]); |
|
16 | + } |
|
17 | 17 | } |
@@ -6,25 +6,25 @@ |
||
6 | 6 | |
7 | 7 | class DropdownFilter extends BaseFilter |
8 | 8 | { |
9 | - /** |
|
10 | - * @var array |
|
11 | - */ |
|
12 | - public $items; |
|
9 | + /** |
|
10 | + * @var array |
|
11 | + */ |
|
12 | + public $items; |
|
13 | 13 | |
14 | - protected function configTests(): array |
|
15 | - { |
|
16 | - return array_merge(parent::configTests(), [ |
|
17 | - 'items' => 'array', |
|
18 | - ]); |
|
19 | - } |
|
14 | + protected function configTests(): array |
|
15 | + { |
|
16 | + return array_merge(parent::configTests(), [ |
|
17 | + 'items' => 'array', |
|
18 | + ]); |
|
19 | + } |
|
20 | 20 | |
21 | - public function render(GridView $grid): string |
|
22 | - { |
|
23 | - return view('woo_gridview::filters.dropdown-filter', [ |
|
24 | - 'name' => $this->name, |
|
25 | - 'value' => $grid->getRequest()->filters[$this->name] ?? '', |
|
26 | - 'items' => $this->items, |
|
27 | - 'cssClass' => $this->cssClass, |
|
28 | - ]); |
|
29 | - } |
|
21 | + public function render(GridView $grid): string |
|
22 | + { |
|
23 | + return view('woo_gridview::filters.dropdown-filter', [ |
|
24 | + 'name' => $this->name, |
|
25 | + 'value' => $grid->getRequest()->filters[$this->name] ?? '', |
|
26 | + 'items' => $this->items, |
|
27 | + 'cssClass' => $this->cssClass, |
|
28 | + ]); |
|
29 | + } |
|
30 | 30 | } |