@@ -23,7 +23,7 @@ |
||
23 | 23 | |
24 | 24 | /** |
25 | 25 | * Action constructor. |
26 | - * @param string|Closure $url |
|
26 | + * @param string $url |
|
27 | 27 | * @param string $content |
28 | 28 | * @param string $method |
29 | 29 | */ |
@@ -6,63 +6,63 @@ |
||
6 | 6 | |
7 | 7 | class Action |
8 | 8 | { |
9 | - /** |
|
10 | - * @var string |
|
11 | - */ |
|
12 | - protected $url; |
|
9 | + /** |
|
10 | + * @var string |
|
11 | + */ |
|
12 | + protected $url; |
|
13 | 13 | |
14 | - /** |
|
15 | - * @var string |
|
16 | - */ |
|
17 | - protected $content; |
|
14 | + /** |
|
15 | + * @var string |
|
16 | + */ |
|
17 | + protected $content; |
|
18 | 18 | |
19 | - /** |
|
20 | - * @var string |
|
21 | - */ |
|
22 | - protected $method; |
|
19 | + /** |
|
20 | + * @var string |
|
21 | + */ |
|
22 | + protected $method; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Action constructor. |
|
26 | - * @param string|Closure $url |
|
27 | - * @param string $content |
|
28 | - * @param string $method |
|
29 | - */ |
|
30 | - public function __construct($url, string $content, string $method = 'GET') |
|
31 | - { |
|
32 | - $this->url = $url; |
|
24 | + /** |
|
25 | + * Action constructor. |
|
26 | + * @param string|Closure $url |
|
27 | + * @param string $content |
|
28 | + * @param string $method |
|
29 | + */ |
|
30 | + public function __construct($url, string $content, string $method = 'GET') |
|
31 | + { |
|
32 | + $this->url = $url; |
|
33 | 33 | |
34 | - $this->content = $content; |
|
34 | + $this->content = $content; |
|
35 | 35 | |
36 | - $this->method = $method; |
|
37 | - } |
|
36 | + $this->method = $method; |
|
37 | + } |
|
38 | 38 | |
39 | - protected function buildUrl($row) |
|
40 | - { |
|
41 | - if ($this->url instanceof Closure) { |
|
42 | - return call_user_func($this->url, $row); |
|
43 | - } |
|
39 | + protected function buildUrl($row) |
|
40 | + { |
|
41 | + if ($this->url instanceof Closure) { |
|
42 | + return call_user_func($this->url, $row); |
|
43 | + } |
|
44 | 44 | |
45 | - return preg_replace_callback('/\{([\w\_]+)\}/', function($match) use ($row) { |
|
45 | + return preg_replace_callback('/\{([\w\_]+)\}/', function($match) use ($row) { |
|
46 | 46 | |
47 | - $match = $match[1]; |
|
47 | + $match = $match[1]; |
|
48 | 48 | |
49 | - if (isset($row->$match)) { |
|
50 | - return $row->$match; |
|
49 | + if (isset($row->$match)) { |
|
50 | + return $row->$match; |
|
51 | 51 | |
52 | - } elseif (isset($row[$match])) { |
|
53 | - return $row[$match]; |
|
54 | - } |
|
52 | + } elseif (isset($row[$match])) { |
|
53 | + return $row[$match]; |
|
54 | + } |
|
55 | 55 | |
56 | - return ''; |
|
57 | - }, $this->url); |
|
58 | - } |
|
56 | + return ''; |
|
57 | + }, $this->url); |
|
58 | + } |
|
59 | 59 | |
60 | - public function render($row) |
|
61 | - { |
|
62 | - return view('woo_gridview::columns.action', [ |
|
63 | - 'url' => $this->buildUrl($row), |
|
64 | - 'content' => $this->content, |
|
65 | - 'method' => $this->method, |
|
66 | - ])->render(); |
|
67 | - } |
|
60 | + public function render($row) |
|
61 | + { |
|
62 | + return view('woo_gridview::columns.action', [ |
|
63 | + 'url' => $this->buildUrl($row), |
|
64 | + 'content' => $this->content, |
|
65 | + 'method' => $this->method, |
|
66 | + ])->render(); |
|
67 | + } |
|
68 | 68 | } |
69 | 69 | \ 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 (is_string($columnOptions['value']) && 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 (is_string($columnOptions['value']) && 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 |
@@ -20,132 +20,132 @@ |
||
20 | 20 | |
21 | 21 | class GridViewHelper |
22 | 22 | { |
23 | - /** |
|
24 | - * A list of grid aliases |
|
25 | - * @var array |
|
26 | - */ |
|
27 | - private static $aliases = [ |
|
28 | - 'column' => [ |
|
29 | - 'attribute' => AttributeColumn::class, |
|
30 | - 'raw' => CallbackColumn::class, |
|
31 | - 'callback' => CallbackColumn::class, |
|
32 | - 'actions' => ActionsColumn::class, |
|
33 | - 'view' => ViewColumn::class, |
|
34 | - ], |
|
35 | - 'formatter' => [ |
|
36 | - 'email' => EmailFormatter::class, |
|
37 | - 'image' => ImageFormatter::class, |
|
38 | - 'text' => TextFormatter::class, |
|
39 | - 'url' => UrlFormatter::class, |
|
40 | - ], |
|
41 | - 'filter' => [ |
|
42 | - 'text' => TextFilter::class, |
|
43 | - 'dropdown' => DropdownFilter::class, |
|
44 | - ], |
|
45 | - 'renderer' => [ |
|
46 | - 'default' => DefaultRenderer::class, |
|
47 | - ], |
|
48 | - 'action' => [ |
|
49 | - 'delete' => DeleteAction::class, |
|
50 | - 'update' => EditAction::class, |
|
51 | - 'edit' => EditAction::class, |
|
52 | - 'show' => ShowAction::class, |
|
53 | - 'view' => ShowAction::class, |
|
54 | - ] |
|
55 | - ]; |
|
56 | - |
|
57 | - private function __construct() {} |
|
58 | - |
|
59 | - /** |
|
60 | - * Useful in case you want to register a new alias for your project |
|
61 | - * @param string $context |
|
62 | - * @param string $alias |
|
63 | - * @param string $aliasTo |
|
64 | - */ |
|
65 | - public static function registerAlias(string $context, string $alias, string $aliasTo) |
|
66 | - { |
|
67 | - self::$aliases[$context][$alias] = $aliasTo; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Allows to resolve class name by its alias |
|
72 | - * @param string $context |
|
73 | - * @param string $alias |
|
74 | - * @return mixed |
|
75 | - */ |
|
76 | - public static function resolveAlias(string $context, string $alias) |
|
77 | - { |
|
78 | - return self::$aliases[$context][$alias] ?? $alias; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Allows to convert options array to html string |
|
83 | - * @param array $htmlOptions |
|
84 | - * @param array $context - context is variables, which are allowed to use when property value calculated dynamically |
|
85 | - * @return string |
|
86 | - */ |
|
87 | - public static function htmlOptionsToString(array $htmlOptions, array $context = []) : string |
|
88 | - { |
|
89 | - if (empty($htmlOptions)) { |
|
90 | - return ''; |
|
91 | - } |
|
92 | - |
|
93 | - $out = []; |
|
94 | - |
|
95 | - foreach ($htmlOptions as $k => $v) { |
|
96 | - |
|
97 | - if ($v instanceof \Closure) { |
|
98 | - $v = call_user_func_array($v, $context); |
|
99 | - } |
|
100 | - |
|
101 | - $out[] = htmlentities($k) . '="' . htmlentities($v, ENT_COMPAT) . '"'; |
|
102 | - } |
|
103 | - |
|
104 | - return implode(' ', $out); |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Allows to make column title by it key or attribute name |
|
109 | - * @param string|int $key |
|
110 | - * @return string |
|
111 | - */ |
|
112 | - public static function columnTitle($key) : string |
|
113 | - { |
|
114 | - if (is_numeric($key)) { |
|
115 | - return 'Column'; |
|
116 | - } |
|
117 | - |
|
118 | - return ucwords( |
|
119 | - trim( |
|
120 | - preg_replace_callback( |
|
121 | - '/([A-Z]|_|\.)/', |
|
122 | - function($word) { |
|
123 | - $word = $word[0]; |
|
124 | - |
|
125 | - if ($word == '_' || $word == '.') { |
|
126 | - return ' '; |
|
127 | - } |
|
128 | - |
|
129 | - return ' ' . strtolower($word); |
|
130 | - }, |
|
131 | - $key |
|
132 | - ) |
|
133 | - ) |
|
134 | - ); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * Helper for internal purposes |
|
139 | - * @param $id |
|
140 | - * @param $component |
|
141 | - * @return string |
|
142 | - */ |
|
143 | - public static function gridIdFormatter($id, $component) |
|
144 | - { |
|
145 | - if ($id == 0) { |
|
146 | - return $component; |
|
147 | - } |
|
148 | - |
|
149 | - return 'grid[' . $id . '][' . $component . ']'; |
|
150 | - } |
|
23 | + /** |
|
24 | + * A list of grid aliases |
|
25 | + * @var array |
|
26 | + */ |
|
27 | + private static $aliases = [ |
|
28 | + 'column' => [ |
|
29 | + 'attribute' => AttributeColumn::class, |
|
30 | + 'raw' => CallbackColumn::class, |
|
31 | + 'callback' => CallbackColumn::class, |
|
32 | + 'actions' => ActionsColumn::class, |
|
33 | + 'view' => ViewColumn::class, |
|
34 | + ], |
|
35 | + 'formatter' => [ |
|
36 | + 'email' => EmailFormatter::class, |
|
37 | + 'image' => ImageFormatter::class, |
|
38 | + 'text' => TextFormatter::class, |
|
39 | + 'url' => UrlFormatter::class, |
|
40 | + ], |
|
41 | + 'filter' => [ |
|
42 | + 'text' => TextFilter::class, |
|
43 | + 'dropdown' => DropdownFilter::class, |
|
44 | + ], |
|
45 | + 'renderer' => [ |
|
46 | + 'default' => DefaultRenderer::class, |
|
47 | + ], |
|
48 | + 'action' => [ |
|
49 | + 'delete' => DeleteAction::class, |
|
50 | + 'update' => EditAction::class, |
|
51 | + 'edit' => EditAction::class, |
|
52 | + 'show' => ShowAction::class, |
|
53 | + 'view' => ShowAction::class, |
|
54 | + ] |
|
55 | + ]; |
|
56 | + |
|
57 | + private function __construct() {} |
|
58 | + |
|
59 | + /** |
|
60 | + * Useful in case you want to register a new alias for your project |
|
61 | + * @param string $context |
|
62 | + * @param string $alias |
|
63 | + * @param string $aliasTo |
|
64 | + */ |
|
65 | + public static function registerAlias(string $context, string $alias, string $aliasTo) |
|
66 | + { |
|
67 | + self::$aliases[$context][$alias] = $aliasTo; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Allows to resolve class name by its alias |
|
72 | + * @param string $context |
|
73 | + * @param string $alias |
|
74 | + * @return mixed |
|
75 | + */ |
|
76 | + public static function resolveAlias(string $context, string $alias) |
|
77 | + { |
|
78 | + return self::$aliases[$context][$alias] ?? $alias; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Allows to convert options array to html string |
|
83 | + * @param array $htmlOptions |
|
84 | + * @param array $context - context is variables, which are allowed to use when property value calculated dynamically |
|
85 | + * @return string |
|
86 | + */ |
|
87 | + public static function htmlOptionsToString(array $htmlOptions, array $context = []) : string |
|
88 | + { |
|
89 | + if (empty($htmlOptions)) { |
|
90 | + return ''; |
|
91 | + } |
|
92 | + |
|
93 | + $out = []; |
|
94 | + |
|
95 | + foreach ($htmlOptions as $k => $v) { |
|
96 | + |
|
97 | + if ($v instanceof \Closure) { |
|
98 | + $v = call_user_func_array($v, $context); |
|
99 | + } |
|
100 | + |
|
101 | + $out[] = htmlentities($k) . '="' . htmlentities($v, ENT_COMPAT) . '"'; |
|
102 | + } |
|
103 | + |
|
104 | + return implode(' ', $out); |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Allows to make column title by it key or attribute name |
|
109 | + * @param string|int $key |
|
110 | + * @return string |
|
111 | + */ |
|
112 | + public static function columnTitle($key) : string |
|
113 | + { |
|
114 | + if (is_numeric($key)) { |
|
115 | + return 'Column'; |
|
116 | + } |
|
117 | + |
|
118 | + return ucwords( |
|
119 | + trim( |
|
120 | + preg_replace_callback( |
|
121 | + '/([A-Z]|_|\.)/', |
|
122 | + function($word) { |
|
123 | + $word = $word[0]; |
|
124 | + |
|
125 | + if ($word == '_' || $word == '.') { |
|
126 | + return ' '; |
|
127 | + } |
|
128 | + |
|
129 | + return ' ' . strtolower($word); |
|
130 | + }, |
|
131 | + $key |
|
132 | + ) |
|
133 | + ) |
|
134 | + ); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * Helper for internal purposes |
|
139 | + * @param $id |
|
140 | + * @param $component |
|
141 | + * @return string |
|
142 | + */ |
|
143 | + public static function gridIdFormatter($id, $component) |
|
144 | + { |
|
145 | + if ($id == 0) { |
|
146 | + return $component; |
|
147 | + } |
|
148 | + |
|
149 | + return 'grid[' . $id . '][' . $component . ']'; |
|
150 | + } |
|
151 | 151 | } |
152 | 152 | \ No newline at end of file |
@@ -9,146 +9,146 @@ |
||
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 |
|
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 | - protected function buildFilter() |
|
70 | - { |
|
71 | - if (is_null($this->filter) || is_object($this->filter)) { |
|
72 | - return; |
|
73 | - } |
|
74 | - |
|
75 | - if (is_string($this->filter)) { |
|
76 | - $this->filter = [ |
|
77 | - 'class' => $this->filter, |
|
78 | - 'name' => $this->value, |
|
79 | - ]; |
|
80 | - } |
|
81 | - |
|
82 | - if (empty($this->filter['class'])) { |
|
83 | - $this->filter['class'] = TextFilter::class; |
|
84 | - } |
|
85 | - |
|
86 | - if (empty($this->filter['name'])) { |
|
87 | - $this->filter['name'] = $this->value; |
|
88 | - } |
|
89 | - |
|
90 | - $className = GridViewHelper::resolveAlias('filter', $this->filter['class']); |
|
91 | - $this->filter = new $className($this->filter); |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * @return array |
|
96 | - */ |
|
97 | - protected function configTests(): array |
|
98 | - { |
|
99 | - return [ |
|
100 | - 'title' => 'string', |
|
101 | - 'value' => 'any', |
|
102 | - 'headerHtmlOptions' => 'array', |
|
103 | - 'contentHtmlOptions' => 'array', |
|
104 | - 'formatters' => 'array', |
|
105 | - 'emptyValue' => 'string', |
|
106 | - 'sortable' => 'bool', |
|
107 | - 'filter' => BaseFilter::class . '|null', |
|
108 | - ]; |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Formatted header html options |
|
113 | - * @return string |
|
114 | - */ |
|
115 | - public function compileHeaderHtmlOptions() : string |
|
116 | - { |
|
117 | - return GridViewHelper::htmlOptionsToString($this->headerHtmlOptions); |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Formatted content html options |
|
122 | - * @param array $context |
|
123 | - * @return string |
|
124 | - */ |
|
125 | - public function compileContentHtmlOptions(array $context) : string |
|
126 | - { |
|
127 | - return GridViewHelper::htmlOptionsToString($this->contentHtmlOptions, $context); |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Render column value for row |
|
132 | - * @param array|object $row |
|
133 | - * @return string|mixed |
|
134 | - */ |
|
135 | - protected abstract function _renderValue($row); |
|
136 | - |
|
137 | - /** |
|
138 | - * Renders column content |
|
139 | - * @param $row |
|
140 | - * @return string |
|
141 | - */ |
|
142 | - public function renderValue($row) |
|
143 | - { |
|
144 | - $value = $this->_renderValue($row); |
|
145 | - |
|
146 | - foreach ($this->formatters as $formatter) { |
|
147 | - $className = GridViewHelper::resolveAlias('formatter', $formatter); |
|
148 | - $value = (new $className)->format($value); |
|
149 | - } |
|
150 | - |
|
151 | - return $value; |
|
152 | - } |
|
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 |
|
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 | + protected function buildFilter() |
|
70 | + { |
|
71 | + if (is_null($this->filter) || is_object($this->filter)) { |
|
72 | + return; |
|
73 | + } |
|
74 | + |
|
75 | + if (is_string($this->filter)) { |
|
76 | + $this->filter = [ |
|
77 | + 'class' => $this->filter, |
|
78 | + 'name' => $this->value, |
|
79 | + ]; |
|
80 | + } |
|
81 | + |
|
82 | + if (empty($this->filter['class'])) { |
|
83 | + $this->filter['class'] = TextFilter::class; |
|
84 | + } |
|
85 | + |
|
86 | + if (empty($this->filter['name'])) { |
|
87 | + $this->filter['name'] = $this->value; |
|
88 | + } |
|
89 | + |
|
90 | + $className = GridViewHelper::resolveAlias('filter', $this->filter['class']); |
|
91 | + $this->filter = new $className($this->filter); |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * @return array |
|
96 | + */ |
|
97 | + protected function configTests(): array |
|
98 | + { |
|
99 | + return [ |
|
100 | + 'title' => 'string', |
|
101 | + 'value' => 'any', |
|
102 | + 'headerHtmlOptions' => 'array', |
|
103 | + 'contentHtmlOptions' => 'array', |
|
104 | + 'formatters' => 'array', |
|
105 | + 'emptyValue' => 'string', |
|
106 | + 'sortable' => 'bool', |
|
107 | + 'filter' => BaseFilter::class . '|null', |
|
108 | + ]; |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Formatted header html options |
|
113 | + * @return string |
|
114 | + */ |
|
115 | + public function compileHeaderHtmlOptions() : string |
|
116 | + { |
|
117 | + return GridViewHelper::htmlOptionsToString($this->headerHtmlOptions); |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Formatted content html options |
|
122 | + * @param array $context |
|
123 | + * @return string |
|
124 | + */ |
|
125 | + public function compileContentHtmlOptions(array $context) : string |
|
126 | + { |
|
127 | + return GridViewHelper::htmlOptionsToString($this->contentHtmlOptions, $context); |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Render column value for row |
|
132 | + * @param array|object $row |
|
133 | + * @return string|mixed |
|
134 | + */ |
|
135 | + protected abstract function _renderValue($row); |
|
136 | + |
|
137 | + /** |
|
138 | + * Renders column content |
|
139 | + * @param $row |
|
140 | + * @return string |
|
141 | + */ |
|
142 | + public function renderValue($row) |
|
143 | + { |
|
144 | + $value = $this->_renderValue($row); |
|
145 | + |
|
146 | + foreach ($this->formatters as $formatter) { |
|
147 | + $className = GridViewHelper::resolveAlias('formatter', $formatter); |
|
148 | + $value = (new $className)->format($value); |
|
149 | + } |
|
150 | + |
|
151 | + return $value; |
|
152 | + } |
|
153 | 153 | |
154 | 154 | } |
155 | 155 | \ No newline at end of file |
@@ -6,87 +6,87 @@ |
||
6 | 6 | |
7 | 7 | class ActionsColumn extends BaseColumn |
8 | 8 | { |
9 | - /** |
|
10 | - * By default is empty for this column |
|
11 | - * @var string |
|
12 | - */ |
|
13 | - public $title = ''; |
|
14 | - |
|
15 | - /** |
|
16 | - * Value contains short codes for actions |
|
17 | - * @var array |
|
18 | - */ |
|
19 | - public $value = []; |
|
20 | - |
|
21 | - /** |
|
22 | - * @var bool |
|
23 | - */ |
|
24 | - public $sortable = false; |
|
25 | - |
|
26 | - /** |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - public $formatters = []; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var string |
|
33 | - */ |
|
34 | - public $delimiter = ' '; |
|
35 | - |
|
36 | - /** |
|
37 | - * @var null |
|
38 | - */ |
|
39 | - public $filter = null; |
|
40 | - |
|
41 | - public function __construct(array $config) |
|
42 | - { |
|
43 | - parent::__construct($config); |
|
44 | - $this->buildActions(); |
|
45 | - } |
|
46 | - |
|
47 | - protected function buildActions() |
|
48 | - { |
|
49 | - foreach ($this->value as &$action) { |
|
50 | - if (is_object($action)) { |
|
51 | - continue; |
|
52 | - } |
|
53 | - |
|
54 | - if (is_string($action)) { |
|
55 | - $tmp = explode(':', $action); |
|
56 | - |
|
57 | - $action = [ |
|
58 | - 'class' => array_shift($tmp), |
|
59 | - 'args' => $tmp, |
|
60 | - ]; |
|
61 | - } |
|
62 | - |
|
63 | - $className = GridViewHelper::resolveAlias('action', $action['class']); |
|
64 | - $action = new $className(...$action['args']); |
|
65 | - } |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * @return array |
|
70 | - */ |
|
71 | - protected function configTests(): array |
|
72 | - { |
|
73 | - return array_merge(parent::configTests(), [ |
|
74 | - 'value' => 'array', |
|
75 | - 'delimiter' => 'string', |
|
76 | - ]); |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * @inheritdoc |
|
81 | - */ |
|
82 | - public function _renderValue($row) |
|
83 | - { |
|
84 | - $result = []; |
|
85 | - |
|
86 | - foreach ($this->value as $action) { |
|
87 | - $result[] = $action->render($row); |
|
88 | - } |
|
89 | - |
|
90 | - return implode($this->delimiter, $result); |
|
91 | - } |
|
9 | + /** |
|
10 | + * By default is empty for this column |
|
11 | + * @var string |
|
12 | + */ |
|
13 | + public $title = ''; |
|
14 | + |
|
15 | + /** |
|
16 | + * Value contains short codes for actions |
|
17 | + * @var array |
|
18 | + */ |
|
19 | + public $value = []; |
|
20 | + |
|
21 | + /** |
|
22 | + * @var bool |
|
23 | + */ |
|
24 | + public $sortable = false; |
|
25 | + |
|
26 | + /** |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + public $formatters = []; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var string |
|
33 | + */ |
|
34 | + public $delimiter = ' '; |
|
35 | + |
|
36 | + /** |
|
37 | + * @var null |
|
38 | + */ |
|
39 | + public $filter = null; |
|
40 | + |
|
41 | + public function __construct(array $config) |
|
42 | + { |
|
43 | + parent::__construct($config); |
|
44 | + $this->buildActions(); |
|
45 | + } |
|
46 | + |
|
47 | + protected function buildActions() |
|
48 | + { |
|
49 | + foreach ($this->value as &$action) { |
|
50 | + if (is_object($action)) { |
|
51 | + continue; |
|
52 | + } |
|
53 | + |
|
54 | + if (is_string($action)) { |
|
55 | + $tmp = explode(':', $action); |
|
56 | + |
|
57 | + $action = [ |
|
58 | + 'class' => array_shift($tmp), |
|
59 | + 'args' => $tmp, |
|
60 | + ]; |
|
61 | + } |
|
62 | + |
|
63 | + $className = GridViewHelper::resolveAlias('action', $action['class']); |
|
64 | + $action = new $className(...$action['args']); |
|
65 | + } |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * @return array |
|
70 | + */ |
|
71 | + protected function configTests(): array |
|
72 | + { |
|
73 | + return array_merge(parent::configTests(), [ |
|
74 | + 'value' => 'array', |
|
75 | + 'delimiter' => 'string', |
|
76 | + ]); |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * @inheritdoc |
|
81 | + */ |
|
82 | + public function _renderValue($row) |
|
83 | + { |
|
84 | + $result = []; |
|
85 | + |
|
86 | + foreach ($this->value as $action) { |
|
87 | + $result[] = $action->render($row); |
|
88 | + } |
|
89 | + |
|
90 | + return implode($this->delimiter, $result); |
|
91 | + } |
|
92 | 92 | } |
93 | 93 | \ No newline at end of file |
@@ -4,8 +4,8 @@ |
||
4 | 4 | |
5 | 5 | class DeleteAction extends Action |
6 | 6 | { |
7 | - public function __construct(string $url, string $content = '<i class="fa fa-trash-alt"></i>') |
|
8 | - { |
|
9 | - parent::__construct($url, $content, 'DELETE'); |
|
10 | - } |
|
7 | + public function __construct(string $url, string $content = '<i class="fa fa-trash-alt"></i>') |
|
8 | + { |
|
9 | + parent::__construct($url, $content, 'DELETE'); |
|
10 | + } |
|
11 | 11 | } |
12 | 12 | \ No newline at end of file |
@@ -6,8 +6,8 @@ |
||
6 | 6 | |
7 | 7 | class ShowAction extends Action |
8 | 8 | { |
9 | - public function __construct(string $url, string $content = '<i class="fa fa-eye"></i>') |
|
10 | - { |
|
11 | - parent::__construct($url, $content, 'GET'); |
|
12 | - } |
|
9 | + public function __construct(string $url, string $content = '<i class="fa fa-eye"></i>') |
|
10 | + { |
|
11 | + parent::__construct($url, $content, 'GET'); |
|
12 | + } |
|
13 | 13 | } |
14 | 14 | \ 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, string $content = '<i class="fa fa-edit"></i>') |
|
10 | - { |
|
11 | - parent::__construct($url, $content, 'GET'); |
|
12 | - } |
|
9 | + public function __construct(string $url, string $content = '<i class="fa fa-edit"></i>') |
|
10 | + { |
|
11 | + parent::__construct($url, $content, 'GET'); |
|
12 | + } |
|
13 | 13 | } |
14 | 14 | \ No newline at end of file |
@@ -8,6 +8,6 @@ |
||
8 | 8 | { |
9 | 9 | public function __construct(string $url, string $content = '<i class="fa fa-edit"></i>') |
10 | 10 | { |
11 | - parent::__construct($url, $content, 'GET'); |
|
11 | + parent::__construct($url, $content, 'GET'); |
|
12 | 12 | } |
13 | 13 | } |
14 | 14 | \ No newline at end of file |