@@ -7,77 +7,77 @@ |
||
7 | 7 | |
8 | 8 | class GridViewRequest |
9 | 9 | { |
10 | - use Configurable; |
|
11 | - |
|
12 | - public $page; |
|
13 | - |
|
14 | - public $sortColumn; |
|
15 | - |
|
16 | - public $sortOrder; |
|
17 | - |
|
18 | - public $perPage; |
|
19 | - |
|
20 | - public $filters = []; |
|
21 | - |
|
22 | - private function __construct($config) |
|
23 | - { |
|
24 | - $this->loadConfig($config); |
|
25 | - } |
|
26 | - |
|
27 | - private static function gridField(int $gridId, string $field) |
|
28 | - { |
|
29 | - return $gridId == 0 ? $field : 'grid.' . $gridId . '.' . $field; |
|
30 | - } |
|
31 | - |
|
32 | - /** |
|
33 | - * Allows to parse request information and making request instance |
|
34 | - * @param int $gridId |
|
35 | - * @return GridViewRequest |
|
36 | - */ |
|
37 | - public static function parse(int $gridId) |
|
38 | - { |
|
39 | - $request = Request::instance(); |
|
40 | - |
|
41 | - $page = $request->input(self::gridField($gridId, 'page'), 1); |
|
42 | - $sortColumn = $request->input(self::gridField($gridId, 'sort'), ''); |
|
43 | - $sortOrder = $request->input(self::gridField($gridId, 'order'), 'DESC'); |
|
44 | - $filters = $request->input(self::gridField($gridId, 'filters'), []); |
|
45 | - |
|
46 | - if ($page <= 0) { |
|
47 | - $page = 1; |
|
48 | - } |
|
49 | - |
|
50 | - if (!is_string($sortColumn)) { |
|
51 | - $sortColumn = ''; |
|
52 | - } |
|
53 | - |
|
54 | - if (!in_array($sortOrder, ['ASC', 'DESC'])) { |
|
55 | - $sortOrder = 'DESC'; |
|
56 | - } |
|
57 | - |
|
58 | - if (!is_array($filters)) { |
|
59 | - $filters = []; |
|
60 | - } |
|
61 | - |
|
62 | - return new GridViewRequest([ |
|
63 | - 'page' => $page, |
|
64 | - 'sortColumn' => $sortColumn, |
|
65 | - 'sortOrder' => $sortOrder, |
|
66 | - 'filters' => $filters, |
|
67 | - ]); |
|
68 | - } |
|
69 | - |
|
70 | - public function getFilterValue(string $name) |
|
71 | - { |
|
72 | - return $this->filters[$name] ?? ''; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * Should specify tests |
|
77 | - * @return array |
|
78 | - */ |
|
79 | - protected function configTests(): array |
|
80 | - { |
|
81 | - return []; |
|
82 | - } |
|
10 | + use Configurable; |
|
11 | + |
|
12 | + public $page; |
|
13 | + |
|
14 | + public $sortColumn; |
|
15 | + |
|
16 | + public $sortOrder; |
|
17 | + |
|
18 | + public $perPage; |
|
19 | + |
|
20 | + public $filters = []; |
|
21 | + |
|
22 | + private function __construct($config) |
|
23 | + { |
|
24 | + $this->loadConfig($config); |
|
25 | + } |
|
26 | + |
|
27 | + private static function gridField(int $gridId, string $field) |
|
28 | + { |
|
29 | + return $gridId == 0 ? $field : 'grid.' . $gridId . '.' . $field; |
|
30 | + } |
|
31 | + |
|
32 | + /** |
|
33 | + * Allows to parse request information and making request instance |
|
34 | + * @param int $gridId |
|
35 | + * @return GridViewRequest |
|
36 | + */ |
|
37 | + public static function parse(int $gridId) |
|
38 | + { |
|
39 | + $request = Request::instance(); |
|
40 | + |
|
41 | + $page = $request->input(self::gridField($gridId, 'page'), 1); |
|
42 | + $sortColumn = $request->input(self::gridField($gridId, 'sort'), ''); |
|
43 | + $sortOrder = $request->input(self::gridField($gridId, 'order'), 'DESC'); |
|
44 | + $filters = $request->input(self::gridField($gridId, 'filters'), []); |
|
45 | + |
|
46 | + if ($page <= 0) { |
|
47 | + $page = 1; |
|
48 | + } |
|
49 | + |
|
50 | + if (!is_string($sortColumn)) { |
|
51 | + $sortColumn = ''; |
|
52 | + } |
|
53 | + |
|
54 | + if (!in_array($sortOrder, ['ASC', 'DESC'])) { |
|
55 | + $sortOrder = 'DESC'; |
|
56 | + } |
|
57 | + |
|
58 | + if (!is_array($filters)) { |
|
59 | + $filters = []; |
|
60 | + } |
|
61 | + |
|
62 | + return new GridViewRequest([ |
|
63 | + 'page' => $page, |
|
64 | + 'sortColumn' => $sortColumn, |
|
65 | + 'sortOrder' => $sortOrder, |
|
66 | + 'filters' => $filters, |
|
67 | + ]); |
|
68 | + } |
|
69 | + |
|
70 | + public function getFilterValue(string $name) |
|
71 | + { |
|
72 | + return $this->filters[$name] ?? ''; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Should specify tests |
|
77 | + * @return array |
|
78 | + */ |
|
79 | + protected function configTests(): array |
|
80 | + { |
|
81 | + return []; |
|
82 | + } |
|
83 | 83 | } |
84 | 84 | \ No newline at end of file |
@@ -7,57 +7,57 @@ |
||
7 | 7 | |
8 | 8 | class EloquentDataProvider extends BaseDataProvider |
9 | 9 | { |
10 | - protected $query; |
|
11 | - |
|
12 | - /** |
|
13 | - * EloquentDataProvider constructor. |
|
14 | - * @param Builder $query |
|
15 | - */ |
|
16 | - public function __construct(Builder $query) |
|
17 | - { |
|
18 | - $this->query = clone $query; |
|
19 | - } |
|
20 | - |
|
21 | - /** |
|
22 | - * @param GridViewRequest $request |
|
23 | - * @return Builder |
|
24 | - */ |
|
25 | - protected function baseQuery(GridViewRequest $request) |
|
26 | - { |
|
27 | - $query = clone $this->query; |
|
28 | - |
|
29 | - foreach ($request->filters as $field => $value) { |
|
30 | - $query->where($field, 'LIKE', '%' . $value . '%'); |
|
31 | - } |
|
32 | - |
|
33 | - if ($request->sortColumn) { |
|
34 | - $query->orderBy($request->sortColumn, $request->sortOrder); |
|
35 | - } |
|
36 | - |
|
37 | - return $query; |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * @inheritdoc |
|
42 | - */ |
|
43 | - public function getCount(GridViewRequest $request) : int |
|
44 | - { |
|
45 | - return $this->baseQuery($request)->count(); |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * @inheritdoc |
|
50 | - */ |
|
51 | - public function getData(GridViewRequest $request) |
|
52 | - { |
|
53 | - $query = $this->baseQuery($request); |
|
54 | - |
|
55 | - if ($request->perPage == 0) { |
|
56 | - return $query->get(); |
|
57 | - } |
|
58 | - |
|
59 | - return $query->offset(($request->page - 1) * $request->perPage) |
|
60 | - ->limit($request->perPage) |
|
61 | - ->get(); |
|
62 | - } |
|
10 | + protected $query; |
|
11 | + |
|
12 | + /** |
|
13 | + * EloquentDataProvider constructor. |
|
14 | + * @param Builder $query |
|
15 | + */ |
|
16 | + public function __construct(Builder $query) |
|
17 | + { |
|
18 | + $this->query = clone $query; |
|
19 | + } |
|
20 | + |
|
21 | + /** |
|
22 | + * @param GridViewRequest $request |
|
23 | + * @return Builder |
|
24 | + */ |
|
25 | + protected function baseQuery(GridViewRequest $request) |
|
26 | + { |
|
27 | + $query = clone $this->query; |
|
28 | + |
|
29 | + foreach ($request->filters as $field => $value) { |
|
30 | + $query->where($field, 'LIKE', '%' . $value . '%'); |
|
31 | + } |
|
32 | + |
|
33 | + if ($request->sortColumn) { |
|
34 | + $query->orderBy($request->sortColumn, $request->sortOrder); |
|
35 | + } |
|
36 | + |
|
37 | + return $query; |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * @inheritdoc |
|
42 | + */ |
|
43 | + public function getCount(GridViewRequest $request) : int |
|
44 | + { |
|
45 | + return $this->baseQuery($request)->count(); |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * @inheritdoc |
|
50 | + */ |
|
51 | + public function getData(GridViewRequest $request) |
|
52 | + { |
|
53 | + $query = $this->baseQuery($request); |
|
54 | + |
|
55 | + if ($request->perPage == 0) { |
|
56 | + return $query->get(); |
|
57 | + } |
|
58 | + |
|
59 | + return $query->offset(($request->page - 1) * $request->perPage) |
|
60 | + ->limit($request->perPage) |
|
61 | + ->get(); |
|
62 | + } |
|
63 | 63 | } |
64 | 64 | \ No newline at end of file |
@@ -6,71 +6,71 @@ |
||
6 | 6 | |
7 | 7 | class ArrayDataProvider extends BaseDataProvider |
8 | 8 | { |
9 | - /** |
|
10 | - * @var array |
|
11 | - */ |
|
12 | - private $data; |
|
9 | + /** |
|
10 | + * @var array |
|
11 | + */ |
|
12 | + private $data; |
|
13 | 13 | |
14 | - public function __construct(array $data) |
|
15 | - { |
|
16 | - $this->data = $data; |
|
17 | - } |
|
14 | + public function __construct(array $data) |
|
15 | + { |
|
16 | + $this->data = $data; |
|
17 | + } |
|
18 | 18 | |
19 | - /** |
|
20 | - * @param GridViewRequest $request |
|
21 | - * @return array |
|
22 | - */ |
|
23 | - protected function processData(GridViewRequest $request) : array |
|
24 | - { |
|
25 | - if (empty($this->data)) { |
|
26 | - return []; |
|
27 | - } |
|
19 | + /** |
|
20 | + * @param GridViewRequest $request |
|
21 | + * @return array |
|
22 | + */ |
|
23 | + protected function processData(GridViewRequest $request) : array |
|
24 | + { |
|
25 | + if (empty($this->data)) { |
|
26 | + return []; |
|
27 | + } |
|
28 | 28 | |
29 | - $tmp = collect($this->data); |
|
29 | + $tmp = collect($this->data); |
|
30 | 30 | |
31 | - if (!empty($request->filters)) { |
|
32 | - $tmp->filter(function($item) use ($request) { |
|
33 | - foreach ($request->filters as $filterKey => $filterValue) { |
|
31 | + if (!empty($request->filters)) { |
|
32 | + $tmp->filter(function($item) use ($request) { |
|
33 | + foreach ($request->filters as $filterKey => $filterValue) { |
|
34 | 34 | |
35 | - if (!isset($item[$filterKey])) { |
|
36 | - return false; |
|
37 | - } |
|
35 | + if (!isset($item[$filterKey])) { |
|
36 | + return false; |
|
37 | + } |
|
38 | 38 | |
39 | - if (strpos($item[$filterKey], $filterValue) === false) { |
|
40 | - return false; |
|
41 | - } |
|
42 | - } |
|
39 | + if (strpos($item[$filterKey], $filterValue) === false) { |
|
40 | + return false; |
|
41 | + } |
|
42 | + } |
|
43 | 43 | |
44 | - return true; |
|
45 | - }); |
|
46 | - } |
|
44 | + return true; |
|
45 | + }); |
|
46 | + } |
|
47 | 47 | |
48 | - if (!empty($request->sortColumn)) { |
|
49 | - $tmp = $tmp->sortBy( |
|
50 | - $request->sortColumn, |
|
51 | - $request->sortOrder == 'DESC' ? SORT_DESC : SORT_ASC |
|
52 | - ); |
|
53 | - } |
|
48 | + if (!empty($request->sortColumn)) { |
|
49 | + $tmp = $tmp->sortBy( |
|
50 | + $request->sortColumn, |
|
51 | + $request->sortOrder == 'DESC' ? SORT_DESC : SORT_ASC |
|
52 | + ); |
|
53 | + } |
|
54 | 54 | |
55 | - return $tmp; |
|
56 | - } |
|
55 | + return $tmp; |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * @inheritdoc |
|
60 | - */ |
|
61 | - public function getCount(GridViewRequest $request) : int |
|
62 | - { |
|
63 | - return count($this->processData($request)); |
|
64 | - } |
|
58 | + /** |
|
59 | + * @inheritdoc |
|
60 | + */ |
|
61 | + public function getCount(GridViewRequest $request) : int |
|
62 | + { |
|
63 | + return count($this->processData($request)); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * @inheritdoc |
|
68 | - */ |
|
69 | - public function getData(GridViewRequest $request) |
|
70 | - { |
|
71 | - return array_splice( |
|
72 | - $this->processData($request), |
|
73 | - ($request->page -1) * $request->perPage, $request->perPage |
|
74 | - ); |
|
75 | - } |
|
66 | + /** |
|
67 | + * @inheritdoc |
|
68 | + */ |
|
69 | + public function getData(GridViewRequest $request) |
|
70 | + { |
|
71 | + return array_splice( |
|
72 | + $this->processData($request), |
|
73 | + ($request->page -1) * $request->perPage, $request->perPage |
|
74 | + ); |
|
75 | + } |
|
76 | 76 | } |
77 | 77 | \ No newline at end of file |
@@ -70,7 +70,7 @@ |
||
70 | 70 | { |
71 | 71 | return array_splice( |
72 | 72 | $this->processData($request), |
73 | - ($request->page -1) * $request->perPage, $request->perPage |
|
73 | + ($request->page - 1) * $request->perPage, $request->perPage |
|
74 | 74 | ); |
75 | 75 | } |
76 | 76 | } |
77 | 77 | \ No newline at end of file |
@@ -6,17 +6,17 @@ |
||
6 | 6 | |
7 | 7 | abstract class BaseDataProvider |
8 | 8 | { |
9 | - /** |
|
10 | - * Should return total amount of rows |
|
11 | - * @param GridViewRequest $request |
|
12 | - * @return int |
|
13 | - */ |
|
14 | - abstract public function getCount(GridViewRequest $request) : int; |
|
9 | + /** |
|
10 | + * Should return total amount of rows |
|
11 | + * @param GridViewRequest $request |
|
12 | + * @return int |
|
13 | + */ |
|
14 | + abstract public function getCount(GridViewRequest $request) : int; |
|
15 | 15 | |
16 | - /** |
|
17 | - * Should return a list of data for current page |
|
18 | - * @param GridViewRequest $request |
|
19 | - * @return mixed |
|
20 | - */ |
|
21 | - abstract public function getData(GridViewRequest $request); |
|
16 | + /** |
|
17 | + * Should return a list of data for current page |
|
18 | + * @param GridViewRequest $request |
|
19 | + * @return mixed |
|
20 | + */ |
|
21 | + abstract public function getData(GridViewRequest $request); |
|
22 | 22 | } |
23 | 23 | \ No newline at end of file |
@@ -15,208 +15,208 @@ |
||
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 | - * @var Paginator |
|
86 | - */ |
|
87 | - protected $pagination; |
|
88 | - |
|
89 | - /** |
|
90 | - * @var GridViewRequest |
|
91 | - */ |
|
92 | - protected $request; |
|
93 | - |
|
94 | - /** |
|
95 | - * GridView constructor. |
|
96 | - * @param array $config |
|
97 | - * @throws Exceptions\GridViewConfigException |
|
98 | - */ |
|
99 | - public function __construct(array $config) |
|
100 | - { |
|
101 | - $this->id = self::$counter++; |
|
102 | - |
|
103 | - $this->loadConfig($config); |
|
104 | - |
|
105 | - /** |
|
106 | - * Making renderer |
|
107 | - */ |
|
108 | - if (!is_object($this->renderer)) { |
|
109 | - $className = GridViewHelper::resolveAlias('renderer', $this->renderer); |
|
110 | - $this->renderer = new $className(array_merge( |
|
111 | - $this->rendererOptions, [ |
|
112 | - 'gridView' => $this, |
|
113 | - ] |
|
114 | - )); |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Build columns from config |
|
119 | - */ |
|
120 | - $this->buildColumns(); |
|
121 | - |
|
122 | - $this->request = GridViewRequest::parse($this->id); |
|
123 | - $this->request->perPage = $this->rowsPerPage; |
|
124 | - |
|
125 | - $this->pagination = new LengthAwarePaginator( |
|
126 | - $this->dataProvider->getData($this->request), |
|
127 | - $this->dataProvider->getCount($this->request), |
|
128 | - $this->rowsPerPage, |
|
129 | - $this->request->page |
|
130 | - ); |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * @return array |
|
135 | - */ |
|
136 | - protected function configTests(): array |
|
137 | - { |
|
138 | - return [ |
|
139 | - 'dataProvider' => BaseDataProvider::class, |
|
140 | - 'columns' => 'array', |
|
141 | - 'renderer' => BaseRenderer::class, |
|
142 | - 'rowsPerPage' => 'int', |
|
143 | - 'tableHtmlOptions' => 'array', |
|
144 | - 'showFilters' => 'boolean', |
|
145 | - ]; |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * Build columns into objects |
|
150 | - */ |
|
151 | - protected function buildColumns() |
|
152 | - { |
|
153 | - foreach ($this->columns as $key => &$columnOptions) { |
|
154 | - |
|
155 | - /** |
|
156 | - * In case of when column is already build |
|
157 | - */ |
|
158 | - if (is_object($columnOptions)) { |
|
159 | - continue; |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * When only attribute name/value passed |
|
164 | - */ |
|
165 | - if (is_string($columnOptions)) { |
|
166 | - $columnOptions = [ |
|
167 | - 'value' => $columnOptions, |
|
168 | - ]; |
|
169 | - } |
|
170 | - |
|
171 | - if ($columnOptions instanceof Closure) { |
|
172 | - $columnOptions = [ |
|
173 | - 'class' => CallbackColumn::class, |
|
174 | - 'value' => $columnOptions, |
|
175 | - 'title' => GridViewHelper::columnTitle($key), |
|
176 | - ]; |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Inline column declaration detector |
|
181 | - */ |
|
182 | - if (is_string($columnOptions['value']) && strpos($columnOptions['value'], 'view:') === 0) { |
|
183 | - $columnOptions['class'] = 'view'; |
|
184 | - $columnOptions['value'] = str_replace('view:', '', $columnOptions['value']); |
|
185 | - } |
|
186 | - |
|
187 | - $columnOptions = array_merge($this->columnOptions, $columnOptions); |
|
188 | - |
|
189 | - $className = GridViewHelper::resolveAlias('column', $columnOptions['class']); |
|
190 | - $columnOptions = new $className($columnOptions); |
|
191 | - } |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Draws widget and return html code |
|
196 | - * @return string |
|
197 | - */ |
|
198 | - public function render() |
|
199 | - { |
|
200 | - return $this->renderer->render($this); |
|
201 | - } |
|
202 | - |
|
203 | - public function getPagination() |
|
204 | - { |
|
205 | - return $this->pagination; |
|
206 | - } |
|
207 | - |
|
208 | - public function getRequest() |
|
209 | - { |
|
210 | - return $this->request; |
|
211 | - } |
|
212 | - |
|
213 | - public function getId() |
|
214 | - { |
|
215 | - return $this->id; |
|
216 | - } |
|
217 | - |
|
218 | - public function compileTableHtmlOptions() |
|
219 | - { |
|
220 | - return GridViewHelper::htmlOptionsToString($this->tableHtmlOptions); |
|
221 | - } |
|
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 | + * @var Paginator |
|
86 | + */ |
|
87 | + protected $pagination; |
|
88 | + |
|
89 | + /** |
|
90 | + * @var GridViewRequest |
|
91 | + */ |
|
92 | + protected $request; |
|
93 | + |
|
94 | + /** |
|
95 | + * GridView constructor. |
|
96 | + * @param array $config |
|
97 | + * @throws Exceptions\GridViewConfigException |
|
98 | + */ |
|
99 | + public function __construct(array $config) |
|
100 | + { |
|
101 | + $this->id = self::$counter++; |
|
102 | + |
|
103 | + $this->loadConfig($config); |
|
104 | + |
|
105 | + /** |
|
106 | + * Making renderer |
|
107 | + */ |
|
108 | + if (!is_object($this->renderer)) { |
|
109 | + $className = GridViewHelper::resolveAlias('renderer', $this->renderer); |
|
110 | + $this->renderer = new $className(array_merge( |
|
111 | + $this->rendererOptions, [ |
|
112 | + 'gridView' => $this, |
|
113 | + ] |
|
114 | + )); |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Build columns from config |
|
119 | + */ |
|
120 | + $this->buildColumns(); |
|
121 | + |
|
122 | + $this->request = GridViewRequest::parse($this->id); |
|
123 | + $this->request->perPage = $this->rowsPerPage; |
|
124 | + |
|
125 | + $this->pagination = new LengthAwarePaginator( |
|
126 | + $this->dataProvider->getData($this->request), |
|
127 | + $this->dataProvider->getCount($this->request), |
|
128 | + $this->rowsPerPage, |
|
129 | + $this->request->page |
|
130 | + ); |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * @return array |
|
135 | + */ |
|
136 | + protected function configTests(): array |
|
137 | + { |
|
138 | + return [ |
|
139 | + 'dataProvider' => BaseDataProvider::class, |
|
140 | + 'columns' => 'array', |
|
141 | + 'renderer' => BaseRenderer::class, |
|
142 | + 'rowsPerPage' => 'int', |
|
143 | + 'tableHtmlOptions' => 'array', |
|
144 | + 'showFilters' => 'boolean', |
|
145 | + ]; |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * Build columns into objects |
|
150 | + */ |
|
151 | + protected function buildColumns() |
|
152 | + { |
|
153 | + foreach ($this->columns as $key => &$columnOptions) { |
|
154 | + |
|
155 | + /** |
|
156 | + * In case of when column is already build |
|
157 | + */ |
|
158 | + if (is_object($columnOptions)) { |
|
159 | + continue; |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * When only attribute name/value passed |
|
164 | + */ |
|
165 | + if (is_string($columnOptions)) { |
|
166 | + $columnOptions = [ |
|
167 | + 'value' => $columnOptions, |
|
168 | + ]; |
|
169 | + } |
|
170 | + |
|
171 | + if ($columnOptions instanceof Closure) { |
|
172 | + $columnOptions = [ |
|
173 | + 'class' => CallbackColumn::class, |
|
174 | + 'value' => $columnOptions, |
|
175 | + 'title' => GridViewHelper::columnTitle($key), |
|
176 | + ]; |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Inline column declaration detector |
|
181 | + */ |
|
182 | + if (is_string($columnOptions['value']) && strpos($columnOptions['value'], 'view:') === 0) { |
|
183 | + $columnOptions['class'] = 'view'; |
|
184 | + $columnOptions['value'] = str_replace('view:', '', $columnOptions['value']); |
|
185 | + } |
|
186 | + |
|
187 | + $columnOptions = array_merge($this->columnOptions, $columnOptions); |
|
188 | + |
|
189 | + $className = GridViewHelper::resolveAlias('column', $columnOptions['class']); |
|
190 | + $columnOptions = new $className($columnOptions); |
|
191 | + } |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Draws widget and return html code |
|
196 | + * @return string |
|
197 | + */ |
|
198 | + public function render() |
|
199 | + { |
|
200 | + return $this->renderer->render($this); |
|
201 | + } |
|
202 | + |
|
203 | + public function getPagination() |
|
204 | + { |
|
205 | + return $this->pagination; |
|
206 | + } |
|
207 | + |
|
208 | + public function getRequest() |
|
209 | + { |
|
210 | + return $this->request; |
|
211 | + } |
|
212 | + |
|
213 | + public function getId() |
|
214 | + { |
|
215 | + return $this->id; |
|
216 | + } |
|
217 | + |
|
218 | + public function compileTableHtmlOptions() |
|
219 | + { |
|
220 | + return GridViewHelper::htmlOptionsToString($this->tableHtmlOptions); |
|
221 | + } |
|
222 | 222 | } |
223 | 223 | \ No newline at end of file |