@@ -9,57 +9,57 @@ |
||
9 | 9 | |
10 | 10 | class AttributeColumn extends BaseColumn |
11 | 11 | { |
12 | - public $formatters = ['text']; |
|
12 | + public $formatters = ['text']; |
|
13 | 13 | |
14 | - public $filter = TextFilter::class; |
|
14 | + public $filter = TextFilter::class; |
|
15 | 15 | |
16 | - /** |
|
17 | - * AttributeColumn constructor. |
|
18 | - * @param $config |
|
19 | - * @throws GridViewConfigException |
|
20 | - */ |
|
21 | - public function __construct($config) |
|
22 | - { |
|
23 | - parent::__construct($config); |
|
16 | + /** |
|
17 | + * AttributeColumn constructor. |
|
18 | + * @param $config |
|
19 | + * @throws GridViewConfigException |
|
20 | + */ |
|
21 | + public function __construct($config) |
|
22 | + { |
|
23 | + parent::__construct($config); |
|
24 | 24 | |
25 | - if (empty($this->title)) { |
|
26 | - $this->title = GridViewHelper::columnTitle($this->value); |
|
27 | - } |
|
28 | - } |
|
25 | + if (empty($this->title)) { |
|
26 | + $this->title = GridViewHelper::columnTitle($this->value); |
|
27 | + } |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * @return array |
|
32 | - */ |
|
33 | - protected function configTests(): array |
|
34 | - { |
|
35 | - return array_merge(parent::configTests(), [ |
|
36 | - 'value' => 'string', |
|
37 | - ]); |
|
38 | - } |
|
30 | + /** |
|
31 | + * @return array |
|
32 | + */ |
|
33 | + protected function configTests(): array |
|
34 | + { |
|
35 | + return array_merge(parent::configTests(), [ |
|
36 | + 'value' => 'string', |
|
37 | + ]); |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * @inheritdoc |
|
42 | - */ |
|
43 | - public function _renderValue($row) |
|
44 | - { |
|
45 | - $path = $row; |
|
46 | - $exp = explode('.', $this->value); |
|
40 | + /** |
|
41 | + * @inheritdoc |
|
42 | + */ |
|
43 | + public function _renderValue($row) |
|
44 | + { |
|
45 | + $path = $row; |
|
46 | + $exp = explode('.', $this->value); |
|
47 | 47 | |
48 | - /** |
|
49 | - * Extract dots notation (column.sub.sub) |
|
50 | - */ |
|
51 | - foreach ($exp as $part) { |
|
48 | + /** |
|
49 | + * Extract dots notation (column.sub.sub) |
|
50 | + */ |
|
51 | + foreach ($exp as $part) { |
|
52 | 52 | |
53 | - if (isset($path[$part])) { |
|
54 | - $path = $path[$part]; |
|
53 | + if (isset($path[$part])) { |
|
54 | + $path = $path[$part]; |
|
55 | 55 | |
56 | - } elseif (isset($path->$part)) { |
|
57 | - $path = $path[$part]; |
|
58 | - } else { |
|
59 | - return $this->emptyValue; |
|
60 | - } |
|
61 | - } |
|
56 | + } elseif (isset($path->$part)) { |
|
57 | + $path = $path[$part]; |
|
58 | + } else { |
|
59 | + return $this->emptyValue; |
|
60 | + } |
|
61 | + } |
|
62 | 62 | |
63 | - return $path; |
|
64 | - } |
|
63 | + return $path; |
|
64 | + } |
|
65 | 65 | } |
66 | 66 | \ No newline at end of file |
@@ -4,26 +4,26 @@ |
||
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 | - * @return array |
|
14 | - */ |
|
15 | - protected function configTests(): array |
|
16 | - { |
|
17 | - return array_merge(parent::configTests(), [ |
|
18 | - 'value' => 'closure', |
|
19 | - ]); |
|
20 | - } |
|
12 | + /** |
|
13 | + * @return array |
|
14 | + */ |
|
15 | + protected function configTests(): array |
|
16 | + { |
|
17 | + return array_merge(parent::configTests(), [ |
|
18 | + 'value' => 'closure', |
|
19 | + ]); |
|
20 | + } |
|
21 | 21 | |
22 | - /** |
|
23 | - * @inheritdoc |
|
24 | - */ |
|
25 | - public function _renderValue($row) |
|
26 | - { |
|
27 | - return call_user_func($this->value, $row); |
|
28 | - } |
|
22 | + /** |
|
23 | + * @inheritdoc |
|
24 | + */ |
|
25 | + public function _renderValue($row) |
|
26 | + { |
|
27 | + return call_user_func($this->value, $row); |
|
28 | + } |
|
29 | 29 | } |
30 | 30 | \ No newline at end of file |
@@ -6,8 +6,8 @@ |
||
6 | 6 | |
7 | 7 | class EditAction extends Action |
8 | 8 | { |
9 | - public function __construct(string $url) |
|
10 | - { |
|
11 | - parent::__construct($url, '<i class="fa fa-"', $method); |
|
12 | - } |
|
9 | + public function __construct(string $url) |
|
10 | + { |
|
11 | + parent::__construct($url, '<i class="fa fa-"', $method); |
|
12 | + } |
|
13 | 13 | } |
14 | 14 | \ No newline at end of file |
@@ -4,36 +4,36 @@ |
||
4 | 4 | |
5 | 5 | class Action |
6 | 6 | { |
7 | - /** |
|
8 | - * @var string |
|
9 | - */ |
|
10 | - protected $url; |
|
11 | - |
|
12 | - /** |
|
13 | - * @var string |
|
14 | - */ |
|
15 | - protected $content; |
|
16 | - |
|
17 | - /** |
|
18 | - * @var string |
|
19 | - */ |
|
20 | - protected $method; |
|
21 | - |
|
22 | - public function __construct(string $url, string $content, string $method = 'GET') |
|
23 | - { |
|
24 | - $this->url = $url; |
|
25 | - |
|
26 | - $this->content = $content; |
|
27 | - |
|
28 | - $this->method = $method; |
|
29 | - } |
|
30 | - |
|
31 | - public function render() |
|
32 | - { |
|
33 | - return view('woo_gridview::columns.action', [ |
|
34 | - 'url' => $this->url, |
|
35 | - 'content' => $this->content, |
|
36 | - 'method' => $this->method, |
|
37 | - ])->render(); |
|
38 | - } |
|
7 | + /** |
|
8 | + * @var string |
|
9 | + */ |
|
10 | + protected $url; |
|
11 | + |
|
12 | + /** |
|
13 | + * @var string |
|
14 | + */ |
|
15 | + protected $content; |
|
16 | + |
|
17 | + /** |
|
18 | + * @var string |
|
19 | + */ |
|
20 | + protected $method; |
|
21 | + |
|
22 | + public function __construct(string $url, string $content, string $method = 'GET') |
|
23 | + { |
|
24 | + $this->url = $url; |
|
25 | + |
|
26 | + $this->content = $content; |
|
27 | + |
|
28 | + $this->method = $method; |
|
29 | + } |
|
30 | + |
|
31 | + public function render() |
|
32 | + { |
|
33 | + return view('woo_gridview::columns.action', [ |
|
34 | + 'url' => $this->url, |
|
35 | + 'content' => $this->content, |
|
36 | + 'method' => $this->method, |
|
37 | + ])->render(); |
|
38 | + } |
|
39 | 39 | } |
40 | 40 | \ No newline at end of file |
@@ -7,16 +7,16 @@ |
||
7 | 7 | |
8 | 8 | class ViewColumn extends BaseColumn |
9 | 9 | { |
10 | - public $formatters = []; |
|
10 | + public $formatters = []; |
|
11 | 11 | |
12 | - /** |
|
13 | - * Render column value for row |
|
14 | - * @param array|object $row |
|
15 | - * @return string|mixed |
|
16 | - * @throws Throwable |
|
17 | - */ |
|
18 | - protected function _renderValue($row) |
|
19 | - { |
|
20 | - return view($this->value, $row)->render(); |
|
21 | - } |
|
12 | + /** |
|
13 | + * Render column value for row |
|
14 | + * @param array|object $row |
|
15 | + * @return string|mixed |
|
16 | + * @throws Throwable |
|
17 | + */ |
|
18 | + protected function _renderValue($row) |
|
19 | + { |
|
20 | + return view($this->value, $row)->render(); |
|
21 | + } |
|
22 | 22 | } |
23 | 23 | \ No newline at end of file |
@@ -9,140 +9,140 @@ |
||
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 array |
|
33 | - */ |
|
34 | - public $headerHtmlOptions = []; |
|
35 | - |
|
36 | - /** |
|
37 | - * @var array |
|
38 | - */ |
|
39 | - public $contentHtmlOptions = []; |
|
40 | - |
|
41 | - /** |
|
42 | - * @var array - allowed: raw, url, email, text, image |
|
43 | - */ |
|
44 | - public $formatters = []; |
|
45 | - |
|
46 | - /** |
|
47 | - * Value when column is empty |
|
48 | - * @var string |
|
49 | - */ |
|
50 | - public $emptyValue = '-'; |
|
51 | - |
|
52 | - /** |
|
53 | - * BaseColumn constructor. |
|
54 | - * @param array $config |
|
55 | - * @throws \Woo\GridView\Exceptions\GridViewConfigException |
|
56 | - */ |
|
57 | - public function __construct(array $config) |
|
58 | - { |
|
59 | - $this->loadConfig($config); |
|
60 | - |
|
61 | - $this->buildFilter(); |
|
62 | - } |
|
63 | - |
|
64 | - protected function buildFilter() |
|
65 | - { |
|
66 | - if (is_null($this->filter) || is_object($this->filter)) { |
|
67 | - return; |
|
68 | - } |
|
69 | - |
|
70 | - if (is_string($this->filter)) { |
|
71 | - $this->filter = [ |
|
72 | - 'class' => $this->filter, |
|
73 | - 'name' => $this->value, |
|
74 | - ]; |
|
75 | - } |
|
76 | - |
|
77 | - if (empty($this->filter['class'])) { |
|
78 | - $this->filter['class'] = TextFilter::class; |
|
79 | - } |
|
80 | - |
|
81 | - if (empty($this->filter['name'])) { |
|
82 | - $this->filter['name'] = $this->value; |
|
83 | - } |
|
84 | - |
|
85 | - $className = GridViewHelper::resolveAlias('filter', $this->filter['class']); |
|
86 | - $this->filter = new $className($this->filter); |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * @return array |
|
91 | - */ |
|
92 | - protected function configTests(): array |
|
93 | - { |
|
94 | - return [ |
|
95 | - 'title' => 'string', |
|
96 | - 'value' => 'any', |
|
97 | - 'headerHtmlOptions' => 'array', |
|
98 | - 'contentHtmlOptions' => 'array', |
|
99 | - 'formatters' => 'array', |
|
100 | - 'emptyValue' => 'string', |
|
101 | - 'filter' => BaseFilter::class . '|null', |
|
102 | - ]; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Formatted header html options |
|
107 | - * @return string |
|
108 | - */ |
|
109 | - public function compileHeaderHtmlOptions() : string |
|
110 | - { |
|
111 | - return GridViewHelper::htmlOptionsToString($this->headerHtmlOptions); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Formatted content html options |
|
116 | - * @param array $context |
|
117 | - * @return string |
|
118 | - */ |
|
119 | - public function compileContentHtmlOptions(array $context) : string |
|
120 | - { |
|
121 | - return GridViewHelper::htmlOptionsToString($this->contentHtmlOptions, $context); |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Render column value for row |
|
126 | - * @param array|object $row |
|
127 | - * @return string|mixed |
|
128 | - */ |
|
129 | - protected abstract function _renderValue($row); |
|
130 | - |
|
131 | - /** |
|
132 | - * Renders column content |
|
133 | - * @param $row |
|
134 | - * @return string |
|
135 | - */ |
|
136 | - public function renderValue($row) |
|
137 | - { |
|
138 | - $value = $this->_renderValue($row); |
|
139 | - |
|
140 | - foreach ($this->formatters as $formatter) { |
|
141 | - $className = GridViewHelper::resolveAlias('formatter', $formatter); |
|
142 | - $value = (new $className)->format($value); |
|
143 | - } |
|
144 | - |
|
145 | - return $value; |
|
146 | - } |
|
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 array |
|
33 | + */ |
|
34 | + public $headerHtmlOptions = []; |
|
35 | + |
|
36 | + /** |
|
37 | + * @var array |
|
38 | + */ |
|
39 | + public $contentHtmlOptions = []; |
|
40 | + |
|
41 | + /** |
|
42 | + * @var array - allowed: raw, url, email, text, image |
|
43 | + */ |
|
44 | + public $formatters = []; |
|
45 | + |
|
46 | + /** |
|
47 | + * Value when column is empty |
|
48 | + * @var string |
|
49 | + */ |
|
50 | + public $emptyValue = '-'; |
|
51 | + |
|
52 | + /** |
|
53 | + * BaseColumn constructor. |
|
54 | + * @param array $config |
|
55 | + * @throws \Woo\GridView\Exceptions\GridViewConfigException |
|
56 | + */ |
|
57 | + public function __construct(array $config) |
|
58 | + { |
|
59 | + $this->loadConfig($config); |
|
60 | + |
|
61 | + $this->buildFilter(); |
|
62 | + } |
|
63 | + |
|
64 | + protected function buildFilter() |
|
65 | + { |
|
66 | + if (is_null($this->filter) || is_object($this->filter)) { |
|
67 | + return; |
|
68 | + } |
|
69 | + |
|
70 | + if (is_string($this->filter)) { |
|
71 | + $this->filter = [ |
|
72 | + 'class' => $this->filter, |
|
73 | + 'name' => $this->value, |
|
74 | + ]; |
|
75 | + } |
|
76 | + |
|
77 | + if (empty($this->filter['class'])) { |
|
78 | + $this->filter['class'] = TextFilter::class; |
|
79 | + } |
|
80 | + |
|
81 | + if (empty($this->filter['name'])) { |
|
82 | + $this->filter['name'] = $this->value; |
|
83 | + } |
|
84 | + |
|
85 | + $className = GridViewHelper::resolveAlias('filter', $this->filter['class']); |
|
86 | + $this->filter = new $className($this->filter); |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * @return array |
|
91 | + */ |
|
92 | + protected function configTests(): array |
|
93 | + { |
|
94 | + return [ |
|
95 | + 'title' => 'string', |
|
96 | + 'value' => 'any', |
|
97 | + 'headerHtmlOptions' => 'array', |
|
98 | + 'contentHtmlOptions' => 'array', |
|
99 | + 'formatters' => 'array', |
|
100 | + 'emptyValue' => 'string', |
|
101 | + 'filter' => BaseFilter::class . '|null', |
|
102 | + ]; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Formatted header html options |
|
107 | + * @return string |
|
108 | + */ |
|
109 | + public function compileHeaderHtmlOptions() : string |
|
110 | + { |
|
111 | + return GridViewHelper::htmlOptionsToString($this->headerHtmlOptions); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Formatted content html options |
|
116 | + * @param array $context |
|
117 | + * @return string |
|
118 | + */ |
|
119 | + public function compileContentHtmlOptions(array $context) : string |
|
120 | + { |
|
121 | + return GridViewHelper::htmlOptionsToString($this->contentHtmlOptions, $context); |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Render column value for row |
|
126 | + * @param array|object $row |
|
127 | + * @return string|mixed |
|
128 | + */ |
|
129 | + protected abstract function _renderValue($row); |
|
130 | + |
|
131 | + /** |
|
132 | + * Renders column content |
|
133 | + * @param $row |
|
134 | + * @return string |
|
135 | + */ |
|
136 | + public function renderValue($row) |
|
137 | + { |
|
138 | + $value = $this->_renderValue($row); |
|
139 | + |
|
140 | + foreach ($this->formatters as $formatter) { |
|
141 | + $className = GridViewHelper::resolveAlias('formatter', $formatter); |
|
142 | + $value = (new $className)->format($value); |
|
143 | + } |
|
144 | + |
|
145 | + return $value; |
|
146 | + } |
|
147 | 147 | |
148 | 148 | } |
149 | 149 | \ No newline at end of file |
@@ -15,212 +15,212 @@ |
||
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 | - |
|
124 | - $this->pagination = new LengthAwarePaginator( |
|
125 | - $this->dataProvider->getData( |
|
126 | - $this->request->filters, |
|
127 | - $this->request->sortColumn ?? '', |
|
128 | - $this->request->sortOrder ?? 'DESC', |
|
129 | - $this->request->page, $this->rowsPerPage |
|
130 | - ), |
|
131 | - $this->dataProvider->getCount(), |
|
132 | - $this->rowsPerPage, |
|
133 | - $this->request->page |
|
134 | - ); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @return array |
|
139 | - */ |
|
140 | - protected function configTests(): array |
|
141 | - { |
|
142 | - return [ |
|
143 | - 'dataProvider' => BaseDataProvider::class, |
|
144 | - 'columns' => 'array', |
|
145 | - 'renderer' => BaseRenderer::class, |
|
146 | - 'rowsPerPage' => 'int', |
|
147 | - 'tableHtmlOptions' => 'array', |
|
148 | - 'showFilters' => 'boolean', |
|
149 | - ]; |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Build columns into objects |
|
154 | - */ |
|
155 | - protected function buildColumns() |
|
156 | - { |
|
157 | - foreach ($this->columns as $key => &$columnOptions) { |
|
158 | - |
|
159 | - /** |
|
160 | - * In case of when column is already build |
|
161 | - */ |
|
162 | - if (is_object($columnOptions)) { |
|
163 | - continue; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * When only attribute name/value passed |
|
168 | - */ |
|
169 | - if (is_string($columnOptions)) { |
|
170 | - $columnOptions = [ |
|
171 | - 'value' => $columnOptions, |
|
172 | - ]; |
|
173 | - } |
|
174 | - |
|
175 | - if ($columnOptions instanceof Closure) { |
|
176 | - $columnOptions = [ |
|
177 | - 'class' => CallbackColumn::class, |
|
178 | - 'value' => $columnOptions, |
|
179 | - 'title' => GridViewHelper::columnTitle($key), |
|
180 | - ]; |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * Inline column declaration detector |
|
185 | - */ |
|
186 | - if (strpos($columnOptions['value'], 'view:') === 0) { |
|
187 | - $columnOptions['class'] = 'view'; |
|
188 | - $columnOptions['value'] = str_replace('view:', '', $columnOptions['value']); |
|
189 | - } |
|
190 | - |
|
191 | - $columnOptions = array_merge($this->columnOptions, $columnOptions); |
|
192 | - |
|
193 | - $className = GridViewHelper::resolveAlias('column', $columnOptions['class']); |
|
194 | - $columnOptions = new $className($columnOptions); |
|
195 | - } |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * Draws widget and return html code |
|
200 | - * @return string |
|
201 | - */ |
|
202 | - public function render() |
|
203 | - { |
|
204 | - return $this->renderer->render($this); |
|
205 | - } |
|
206 | - |
|
207 | - public function getPagination() |
|
208 | - { |
|
209 | - return $this->pagination; |
|
210 | - } |
|
211 | - |
|
212 | - public function getRequest() |
|
213 | - { |
|
214 | - return $this->request; |
|
215 | - } |
|
216 | - |
|
217 | - public function getId() |
|
218 | - { |
|
219 | - return $this->id; |
|
220 | - } |
|
221 | - |
|
222 | - public function compileTableHtmlOptions() |
|
223 | - { |
|
224 | - return GridViewHelper::htmlOptionsToString($this->tableHtmlOptions); |
|
225 | - } |
|
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 | + |
|
124 | + $this->pagination = new LengthAwarePaginator( |
|
125 | + $this->dataProvider->getData( |
|
126 | + $this->request->filters, |
|
127 | + $this->request->sortColumn ?? '', |
|
128 | + $this->request->sortOrder ?? 'DESC', |
|
129 | + $this->request->page, $this->rowsPerPage |
|
130 | + ), |
|
131 | + $this->dataProvider->getCount(), |
|
132 | + $this->rowsPerPage, |
|
133 | + $this->request->page |
|
134 | + ); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @return array |
|
139 | + */ |
|
140 | + protected function configTests(): array |
|
141 | + { |
|
142 | + return [ |
|
143 | + 'dataProvider' => BaseDataProvider::class, |
|
144 | + 'columns' => 'array', |
|
145 | + 'renderer' => BaseRenderer::class, |
|
146 | + 'rowsPerPage' => 'int', |
|
147 | + 'tableHtmlOptions' => 'array', |
|
148 | + 'showFilters' => 'boolean', |
|
149 | + ]; |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Build columns into objects |
|
154 | + */ |
|
155 | + protected function buildColumns() |
|
156 | + { |
|
157 | + foreach ($this->columns as $key => &$columnOptions) { |
|
158 | + |
|
159 | + /** |
|
160 | + * In case of when column is already build |
|
161 | + */ |
|
162 | + if (is_object($columnOptions)) { |
|
163 | + continue; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * When only attribute name/value passed |
|
168 | + */ |
|
169 | + if (is_string($columnOptions)) { |
|
170 | + $columnOptions = [ |
|
171 | + 'value' => $columnOptions, |
|
172 | + ]; |
|
173 | + } |
|
174 | + |
|
175 | + if ($columnOptions instanceof Closure) { |
|
176 | + $columnOptions = [ |
|
177 | + 'class' => CallbackColumn::class, |
|
178 | + 'value' => $columnOptions, |
|
179 | + 'title' => GridViewHelper::columnTitle($key), |
|
180 | + ]; |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * Inline column declaration detector |
|
185 | + */ |
|
186 | + if (strpos($columnOptions['value'], 'view:') === 0) { |
|
187 | + $columnOptions['class'] = 'view'; |
|
188 | + $columnOptions['value'] = str_replace('view:', '', $columnOptions['value']); |
|
189 | + } |
|
190 | + |
|
191 | + $columnOptions = array_merge($this->columnOptions, $columnOptions); |
|
192 | + |
|
193 | + $className = GridViewHelper::resolveAlias('column', $columnOptions['class']); |
|
194 | + $columnOptions = new $className($columnOptions); |
|
195 | + } |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * Draws widget and return html code |
|
200 | + * @return string |
|
201 | + */ |
|
202 | + public function render() |
|
203 | + { |
|
204 | + return $this->renderer->render($this); |
|
205 | + } |
|
206 | + |
|
207 | + public function getPagination() |
|
208 | + { |
|
209 | + return $this->pagination; |
|
210 | + } |
|
211 | + |
|
212 | + public function getRequest() |
|
213 | + { |
|
214 | + return $this->request; |
|
215 | + } |
|
216 | + |
|
217 | + public function getId() |
|
218 | + { |
|
219 | + return $this->id; |
|
220 | + } |
|
221 | + |
|
222 | + public function compileTableHtmlOptions() |
|
223 | + { |
|
224 | + return GridViewHelper::htmlOptionsToString($this->tableHtmlOptions); |
|
225 | + } |
|
226 | 226 | } |
227 | 227 | \ No newline at end of file |
@@ -17,125 +17,125 @@ |
||
17 | 17 | |
18 | 18 | class GridViewHelper |
19 | 19 | { |
20 | - /** |
|
21 | - * A list of grid aliases |
|
22 | - * @var array |
|
23 | - */ |
|
24 | - private static $aliases = [ |
|
25 | - 'column' => [ |
|
26 | - 'attribute' => AttributeColumn::class, |
|
27 | - 'raw' => CallbackColumn::class, |
|
28 | - 'callback' => CallbackColumn::class, |
|
29 | - 'actions' => ActionsColumn::class, |
|
30 | - 'view' => ViewColumn::class, |
|
31 | - ], |
|
32 | - 'formatter' => [ |
|
33 | - 'email' => EmailFormatter::class, |
|
34 | - 'image' => ImageFormatter::class, |
|
35 | - 'text' => TextFormatter::class, |
|
36 | - 'url' => UrlFormatter::class, |
|
37 | - ], |
|
38 | - 'filter' => [ |
|
39 | - 'text' => TextFilter::class, |
|
40 | - 'dropdown' => DropdownFilter::class, |
|
41 | - ], |
|
42 | - 'renderer' => [ |
|
43 | - 'default' => DefaultRenderer::class, |
|
44 | - ] |
|
45 | - ]; |
|
46 | - |
|
47 | - private function __construct() {} |
|
48 | - |
|
49 | - /** |
|
50 | - * Useful in case you want to register a new alias for your project |
|
51 | - * @param string $context |
|
52 | - * @param string $alias |
|
53 | - * @param string $aliasTo |
|
54 | - */ |
|
55 | - public static function registerAlias(string $context, string $alias, string $aliasTo) |
|
56 | - { |
|
57 | - self::$aliases[$context][$alias] = $aliasTo; |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Allows to resolve class name by its alias |
|
62 | - * @param string $context |
|
63 | - * @param string $alias |
|
64 | - * @return mixed |
|
65 | - */ |
|
66 | - public static function resolveAlias(string $context, string $alias) |
|
67 | - { |
|
68 | - return self::$aliases[$context][$alias] ?? $alias; |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Allows to convert options array to html string |
|
73 | - * @param array $htmlOptions |
|
74 | - * @param array $context - context is variables, which are allowed to use when property value calculated dynamically |
|
75 | - * @return string |
|
76 | - */ |
|
77 | - public static function htmlOptionsToString(array $htmlOptions, array $context = []) : string |
|
78 | - { |
|
79 | - if (empty($htmlOptions)) { |
|
80 | - return ''; |
|
81 | - } |
|
82 | - |
|
83 | - $out = []; |
|
84 | - |
|
85 | - foreach ($htmlOptions as $k => $v) { |
|
86 | - |
|
87 | - if ($v instanceof \Closure) { |
|
88 | - $v = call_user_func_array($v, $context); |
|
89 | - } |
|
90 | - |
|
91 | - $out[] = htmlentities($k) . '="' . htmlentities($v, ENT_COMPAT) . '"'; |
|
92 | - } |
|
93 | - |
|
94 | - return implode(' ', $out); |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Allows to make column title by it key or attribute name |
|
99 | - * @param string|int $key |
|
100 | - * @return string |
|
101 | - */ |
|
102 | - public static function columnTitle($key) : string |
|
103 | - { |
|
104 | - if (is_numeric($key)) { |
|
105 | - return 'Column'; |
|
106 | - } |
|
107 | - |
|
108 | - return ucwords( |
|
109 | - trim( |
|
110 | - preg_replace_callback( |
|
111 | - '/([A-Z]|_|\.)/', |
|
112 | - function($word) { |
|
113 | - $word = $word[0]; |
|
114 | - |
|
115 | - if ($word == '_' || $word == '.') { |
|
116 | - return ' '; |
|
117 | - } |
|
118 | - |
|
119 | - return ' ' . strtolower($word); |
|
120 | - }, |
|
121 | - $key |
|
122 | - ) |
|
123 | - ) |
|
124 | - ); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Helper for internal purposes |
|
129 | - * @param $id |
|
130 | - * @param $component |
|
131 | - * @return string |
|
132 | - */ |
|
133 | - public static function gridIdFormatter($id, $component) |
|
134 | - { |
|
135 | - if ($id == 0) { |
|
136 | - return $component; |
|
137 | - } |
|
138 | - |
|
139 | - return 'grid[' . $id . '][' . $component . ']'; |
|
140 | - } |
|
20 | + /** |
|
21 | + * A list of grid aliases |
|
22 | + * @var array |
|
23 | + */ |
|
24 | + private static $aliases = [ |
|
25 | + 'column' => [ |
|
26 | + 'attribute' => AttributeColumn::class, |
|
27 | + 'raw' => CallbackColumn::class, |
|
28 | + 'callback' => CallbackColumn::class, |
|
29 | + 'actions' => ActionsColumn::class, |
|
30 | + 'view' => ViewColumn::class, |
|
31 | + ], |
|
32 | + 'formatter' => [ |
|
33 | + 'email' => EmailFormatter::class, |
|
34 | + 'image' => ImageFormatter::class, |
|
35 | + 'text' => TextFormatter::class, |
|
36 | + 'url' => UrlFormatter::class, |
|
37 | + ], |
|
38 | + 'filter' => [ |
|
39 | + 'text' => TextFilter::class, |
|
40 | + 'dropdown' => DropdownFilter::class, |
|
41 | + ], |
|
42 | + 'renderer' => [ |
|
43 | + 'default' => DefaultRenderer::class, |
|
44 | + ] |
|
45 | + ]; |
|
46 | + |
|
47 | + private function __construct() {} |
|
48 | + |
|
49 | + /** |
|
50 | + * Useful in case you want to register a new alias for your project |
|
51 | + * @param string $context |
|
52 | + * @param string $alias |
|
53 | + * @param string $aliasTo |
|
54 | + */ |
|
55 | + public static function registerAlias(string $context, string $alias, string $aliasTo) |
|
56 | + { |
|
57 | + self::$aliases[$context][$alias] = $aliasTo; |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Allows to resolve class name by its alias |
|
62 | + * @param string $context |
|
63 | + * @param string $alias |
|
64 | + * @return mixed |
|
65 | + */ |
|
66 | + public static function resolveAlias(string $context, string $alias) |
|
67 | + { |
|
68 | + return self::$aliases[$context][$alias] ?? $alias; |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Allows to convert options array to html string |
|
73 | + * @param array $htmlOptions |
|
74 | + * @param array $context - context is variables, which are allowed to use when property value calculated dynamically |
|
75 | + * @return string |
|
76 | + */ |
|
77 | + public static function htmlOptionsToString(array $htmlOptions, array $context = []) : string |
|
78 | + { |
|
79 | + if (empty($htmlOptions)) { |
|
80 | + return ''; |
|
81 | + } |
|
82 | + |
|
83 | + $out = []; |
|
84 | + |
|
85 | + foreach ($htmlOptions as $k => $v) { |
|
86 | + |
|
87 | + if ($v instanceof \Closure) { |
|
88 | + $v = call_user_func_array($v, $context); |
|
89 | + } |
|
90 | + |
|
91 | + $out[] = htmlentities($k) . '="' . htmlentities($v, ENT_COMPAT) . '"'; |
|
92 | + } |
|
93 | + |
|
94 | + return implode(' ', $out); |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Allows to make column title by it key or attribute name |
|
99 | + * @param string|int $key |
|
100 | + * @return string |
|
101 | + */ |
|
102 | + public static function columnTitle($key) : string |
|
103 | + { |
|
104 | + if (is_numeric($key)) { |
|
105 | + return 'Column'; |
|
106 | + } |
|
107 | + |
|
108 | + return ucwords( |
|
109 | + trim( |
|
110 | + preg_replace_callback( |
|
111 | + '/([A-Z]|_|\.)/', |
|
112 | + function($word) { |
|
113 | + $word = $word[0]; |
|
114 | + |
|
115 | + if ($word == '_' || $word == '.') { |
|
116 | + return ' '; |
|
117 | + } |
|
118 | + |
|
119 | + return ' ' . strtolower($word); |
|
120 | + }, |
|
121 | + $key |
|
122 | + ) |
|
123 | + ) |
|
124 | + ); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Helper for internal purposes |
|
129 | + * @param $id |
|
130 | + * @param $component |
|
131 | + * @return string |
|
132 | + */ |
|
133 | + public static function gridIdFormatter($id, $component) |
|
134 | + { |
|
135 | + if ($id == 0) { |
|
136 | + return $component; |
|
137 | + } |
|
138 | + |
|
139 | + return 'grid[' . $id . '][' . $component . ']'; |
|
140 | + } |
|
141 | 141 | } |
142 | 142 | \ No newline at end of file |
@@ -9,32 +9,32 @@ |
||
9 | 9 | |
10 | 10 | abstract class BaseRenderer |
11 | 11 | { |
12 | - use Configurable; |
|
12 | + use Configurable; |
|
13 | 13 | |
14 | - /** |
|
15 | - * @var GridView |
|
16 | - */ |
|
17 | - public $gridView; |
|
14 | + /** |
|
15 | + * @var GridView |
|
16 | + */ |
|
17 | + public $gridView; |
|
18 | 18 | |
19 | - /** |
|
20 | - * BaseRenderer constructor. |
|
21 | - * @param $config |
|
22 | - * @throws GridViewConfigException |
|
23 | - */ |
|
24 | - public function __construct($config) |
|
25 | - { |
|
26 | - $this->loadConfig($config); |
|
27 | - } |
|
19 | + /** |
|
20 | + * BaseRenderer constructor. |
|
21 | + * @param $config |
|
22 | + * @throws GridViewConfigException |
|
23 | + */ |
|
24 | + public function __construct($config) |
|
25 | + { |
|
26 | + $this->loadConfig($config); |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * @return array |
|
31 | - */ |
|
32 | - protected function configTests(): array |
|
33 | - { |
|
34 | - return [ |
|
35 | - 'gridView' => GridView::class, |
|
36 | - ]; |
|
37 | - } |
|
29 | + /** |
|
30 | + * @return array |
|
31 | + */ |
|
32 | + protected function configTests(): array |
|
33 | + { |
|
34 | + return [ |
|
35 | + 'gridView' => GridView::class, |
|
36 | + ]; |
|
37 | + } |
|
38 | 38 | |
39 | - public abstract function render() : string; |
|
39 | + public abstract function render() : string; |
|
40 | 40 | } |
41 | 41 | \ No newline at end of file |