@@ -7,25 +7,25 @@ |
||
7 | 7 | |
8 | 8 | abstract class BaseFilter |
9 | 9 | { |
10 | - use Configurable; |
|
10 | + use Configurable; |
|
11 | 11 | |
12 | - public $name; |
|
13 | - public $cssClass = 'form-control'; |
|
12 | + public $name; |
|
13 | + public $cssClass = 'form-control'; |
|
14 | 14 | |
15 | - public function __construct(array $config) |
|
16 | - { |
|
17 | - if (!empty($config['cssClass'])) { |
|
18 | - $config['cssClass'] .= ' '.$this->cssClass; |
|
19 | - } |
|
20 | - $this->loadConfig($config); |
|
21 | - } |
|
15 | + public function __construct(array $config) |
|
16 | + { |
|
17 | + if (!empty($config['cssClass'])) { |
|
18 | + $config['cssClass'] .= ' '.$this->cssClass; |
|
19 | + } |
|
20 | + $this->loadConfig($config); |
|
21 | + } |
|
22 | 22 | |
23 | - protected function configTests(): array |
|
24 | - { |
|
25 | - return [ |
|
26 | - 'name' => 'string', |
|
27 | - ]; |
|
28 | - } |
|
23 | + protected function configTests(): array |
|
24 | + { |
|
25 | + return [ |
|
26 | + 'name' => 'string', |
|
27 | + ]; |
|
28 | + } |
|
29 | 29 | |
30 | - abstract public function render(GridView $grid) : string; |
|
30 | + abstract public function render(GridView $grid) : string; |
|
31 | 31 | } |
@@ -15,7 +15,7 @@ |
||
15 | 15 | public function __construct(array $config) |
16 | 16 | { |
17 | 17 | if (!empty($config['cssClass'])) { |
18 | - $config['cssClass'] .= ' '.$this->cssClass; |
|
18 | + $config['cssClass'] .= ' ' . $this->cssClass; |
|
19 | 19 | } |
20 | 20 | $this->loadConfig($config); |
21 | 21 | } |
@@ -6,12 +6,12 @@ |
||
6 | 6 | |
7 | 7 | class TextFilter extends BaseFilter |
8 | 8 | { |
9 | - public function render(GridView $grid): string |
|
10 | - { |
|
11 | - return view('woo_gridview::filters.text-filter', [ |
|
12 | - 'name' => $this->name, |
|
13 | - 'value' => $grid->getRequest()->filters[$this->name] ?? '', |
|
14 | - 'cssClass' => $this->cssClass, |
|
15 | - ]); |
|
16 | - } |
|
9 | + public function render(GridView $grid): string |
|
10 | + { |
|
11 | + return view('woo_gridview::filters.text-filter', [ |
|
12 | + 'name' => $this->name, |
|
13 | + 'value' => $grid->getRequest()->filters[$this->name] ?? '', |
|
14 | + 'cssClass' => $this->cssClass, |
|
15 | + ]); |
|
16 | + } |
|
17 | 17 | } |
@@ -6,25 +6,25 @@ |
||
6 | 6 | |
7 | 7 | class DropdownFilter extends BaseFilter |
8 | 8 | { |
9 | - /** |
|
10 | - * @var array |
|
11 | - */ |
|
12 | - public $items; |
|
9 | + /** |
|
10 | + * @var array |
|
11 | + */ |
|
12 | + public $items; |
|
13 | 13 | |
14 | - protected function configTests(): array |
|
15 | - { |
|
16 | - return array_merge(parent::configTests(), [ |
|
17 | - 'items' => 'array', |
|
18 | - ]); |
|
19 | - } |
|
14 | + protected function configTests(): array |
|
15 | + { |
|
16 | + return array_merge(parent::configTests(), [ |
|
17 | + 'items' => 'array', |
|
18 | + ]); |
|
19 | + } |
|
20 | 20 | |
21 | - public function render(GridView $grid): string |
|
22 | - { |
|
23 | - return view('woo_gridview::filters.dropdown-filter', [ |
|
24 | - 'name' => $this->name, |
|
25 | - 'value' => $grid->getRequest()->filters[$this->name] ?? '', |
|
26 | - 'items' => $this->items, |
|
27 | - 'cssClass' => $this->cssClass, |
|
28 | - ]); |
|
29 | - } |
|
21 | + public function render(GridView $grid): string |
|
22 | + { |
|
23 | + return view('woo_gridview::filters.dropdown-filter', [ |
|
24 | + 'name' => $this->name, |
|
25 | + 'value' => $grid->getRequest()->filters[$this->name] ?? '', |
|
26 | + 'items' => $this->items, |
|
27 | + 'cssClass' => $this->cssClass, |
|
28 | + ]); |
|
29 | + } |
|
30 | 30 | } |
@@ -7,22 +7,22 @@ |
||
7 | 7 | |
8 | 8 | class DefaultRenderer extends BaseRenderer |
9 | 9 | { |
10 | - /** |
|
11 | - * @return string |
|
12 | - * @throws \Throwable |
|
13 | - */ |
|
14 | - public function render(): string |
|
15 | - { |
|
16 | - $filters = []; |
|
17 | - foreach ($this->gridView->columns as $column) { |
|
18 | - if ($column->filter) { |
|
19 | - $filters[$column->filter->name] = $this->gridView->getRequest()->getFilterValue($column->filter->name); |
|
20 | - } |
|
21 | - } |
|
10 | + /** |
|
11 | + * @return string |
|
12 | + * @throws \Throwable |
|
13 | + */ |
|
14 | + public function render(): string |
|
15 | + { |
|
16 | + $filters = []; |
|
17 | + foreach ($this->gridView->columns as $column) { |
|
18 | + if ($column->filter) { |
|
19 | + $filters[$column->filter->name] = $this->gridView->getRequest()->getFilterValue($column->filter->name); |
|
20 | + } |
|
21 | + } |
|
22 | 22 | |
23 | - return view('woo_gridview::renderers.default', [ |
|
24 | - 'grid' => $this->gridView, |
|
25 | - 'filters' => $filters, |
|
26 | - ])->render(); |
|
27 | - } |
|
23 | + return view('woo_gridview::renderers.default', [ |
|
24 | + 'grid' => $this->gridView, |
|
25 | + 'filters' => $filters, |
|
26 | + ])->render(); |
|
27 | + } |
|
28 | 28 | } |
@@ -8,101 +8,101 @@ |
||
8 | 8 | |
9 | 9 | class QueryDataProvider extends BaseDataProvider |
10 | 10 | { |
11 | - protected $filters = true; |
|
12 | - protected $ordering = true; |
|
13 | - |
|
14 | - protected $query; |
|
15 | - |
|
16 | - /** |
|
17 | - * EloquentDataProvider constructor. |
|
18 | - * @param Builder $query |
|
19 | - */ |
|
20 | - public function __construct(Builder $query) |
|
21 | - { |
|
22 | - $this->query = clone $query; |
|
23 | - } |
|
24 | - |
|
25 | - /** |
|
26 | - * Applies filter to a column |
|
27 | - * @param \Closure| $filter |
|
28 | - * @param string $fieldName |
|
29 | - * @param Builder $query |
|
30 | - * @param mixed $value |
|
31 | - * @return void |
|
32 | - */ |
|
33 | - private function applyFilter($filter, string $fieldName, Builder $query, $value) |
|
34 | - { |
|
35 | - if (is_callable($filter)) { |
|
36 | - $filter($query, $value); |
|
37 | - return; |
|
38 | - } |
|
39 | - |
|
40 | - switch ($filter) { |
|
41 | - case '=': |
|
42 | - case '<>': |
|
43 | - case '>=': |
|
44 | - case '>': |
|
45 | - case '<=': |
|
46 | - case '<': |
|
47 | - $query->where($fieldName, $filter, $value); |
|
48 | - break; |
|
49 | - |
|
50 | - case 'like': |
|
51 | - $query->where($fieldName, 'LIKE', '%' . $value . '%'); |
|
52 | - break; |
|
53 | - |
|
54 | - default: |
|
55 | - throw new GridViewConfigException('Unknown filter type: ' . $filter); |
|
56 | - } |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * @param GridViewRequest $request |
|
61 | - * @return Builder |
|
62 | - */ |
|
63 | - protected function baseQuery(GridViewRequest $request) |
|
64 | - { |
|
65 | - $query = clone $this->query; |
|
66 | - |
|
67 | - if ($this->filters !== false) { |
|
68 | - foreach ($request->filters as $field => $value) { |
|
69 | - if ($this->filters === true || in_array($field, $this->filters)) { |
|
70 | - $query->where($field, 'LIKE', '%' . $value . '%'); |
|
71 | - |
|
72 | - } elseif (!empty($this->filters[$field])) { |
|
73 | - $this->applyFilter($this->filters[$field], $field, $query, $value); |
|
74 | - } |
|
75 | - } |
|
76 | - } |
|
77 | - |
|
78 | - if ($request->sortColumn && ($this->ordering === true || in_array($request->sortColumn, $this->ordering))) { |
|
79 | - $query->reorder($request->sortColumn, $request->sortOrder); |
|
80 | - } |
|
81 | - |
|
82 | - return $query; |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * @inheritdoc |
|
87 | - */ |
|
88 | - public function getCount(GridViewRequest $request) : int |
|
89 | - { |
|
90 | - return $this->baseQuery($request)->count(); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @inheritdoc |
|
95 | - */ |
|
96 | - public function getData(GridViewRequest $request) |
|
97 | - { |
|
98 | - $query = $this->baseQuery($request); |
|
99 | - |
|
100 | - if ($request->perPage == 0) { |
|
101 | - return $query->get()->all(); |
|
102 | - } |
|
103 | - |
|
104 | - return $query->offset(($request->page - 1) * $request->perPage) |
|
105 | - ->limit($request->perPage) |
|
106 | - ->get()->all(); |
|
107 | - } |
|
11 | + protected $filters = true; |
|
12 | + protected $ordering = true; |
|
13 | + |
|
14 | + protected $query; |
|
15 | + |
|
16 | + /** |
|
17 | + * EloquentDataProvider constructor. |
|
18 | + * @param Builder $query |
|
19 | + */ |
|
20 | + public function __construct(Builder $query) |
|
21 | + { |
|
22 | + $this->query = clone $query; |
|
23 | + } |
|
24 | + |
|
25 | + /** |
|
26 | + * Applies filter to a column |
|
27 | + * @param \Closure| $filter |
|
28 | + * @param string $fieldName |
|
29 | + * @param Builder $query |
|
30 | + * @param mixed $value |
|
31 | + * @return void |
|
32 | + */ |
|
33 | + private function applyFilter($filter, string $fieldName, Builder $query, $value) |
|
34 | + { |
|
35 | + if (is_callable($filter)) { |
|
36 | + $filter($query, $value); |
|
37 | + return; |
|
38 | + } |
|
39 | + |
|
40 | + switch ($filter) { |
|
41 | + case '=': |
|
42 | + case '<>': |
|
43 | + case '>=': |
|
44 | + case '>': |
|
45 | + case '<=': |
|
46 | + case '<': |
|
47 | + $query->where($fieldName, $filter, $value); |
|
48 | + break; |
|
49 | + |
|
50 | + case 'like': |
|
51 | + $query->where($fieldName, 'LIKE', '%' . $value . '%'); |
|
52 | + break; |
|
53 | + |
|
54 | + default: |
|
55 | + throw new GridViewConfigException('Unknown filter type: ' . $filter); |
|
56 | + } |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * @param GridViewRequest $request |
|
61 | + * @return Builder |
|
62 | + */ |
|
63 | + protected function baseQuery(GridViewRequest $request) |
|
64 | + { |
|
65 | + $query = clone $this->query; |
|
66 | + |
|
67 | + if ($this->filters !== false) { |
|
68 | + foreach ($request->filters as $field => $value) { |
|
69 | + if ($this->filters === true || in_array($field, $this->filters)) { |
|
70 | + $query->where($field, 'LIKE', '%' . $value . '%'); |
|
71 | + |
|
72 | + } elseif (!empty($this->filters[$field])) { |
|
73 | + $this->applyFilter($this->filters[$field], $field, $query, $value); |
|
74 | + } |
|
75 | + } |
|
76 | + } |
|
77 | + |
|
78 | + if ($request->sortColumn && ($this->ordering === true || in_array($request->sortColumn, $this->ordering))) { |
|
79 | + $query->reorder($request->sortColumn, $request->sortOrder); |
|
80 | + } |
|
81 | + |
|
82 | + return $query; |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * @inheritdoc |
|
87 | + */ |
|
88 | + public function getCount(GridViewRequest $request) : int |
|
89 | + { |
|
90 | + return $this->baseQuery($request)->count(); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @inheritdoc |
|
95 | + */ |
|
96 | + public function getData(GridViewRequest $request) |
|
97 | + { |
|
98 | + $query = $this->baseQuery($request); |
|
99 | + |
|
100 | + if ($request->perPage == 0) { |
|
101 | + return $query->get()->all(); |
|
102 | + } |
|
103 | + |
|
104 | + return $query->offset(($request->page - 1) * $request->perPage) |
|
105 | + ->limit($request->perPage) |
|
106 | + ->get()->all(); |
|
107 | + } |
|
108 | 108 | } |
@@ -8,101 +8,101 @@ |
||
8 | 8 | |
9 | 9 | class EloquentDataProvider extends BaseDataProvider |
10 | 10 | { |
11 | - protected $filters = true; |
|
12 | - protected $ordering = true; |
|
13 | - |
|
14 | - protected $query; |
|
15 | - |
|
16 | - /** |
|
17 | - * EloquentDataProvider constructor. |
|
18 | - * @param Builder $query |
|
19 | - */ |
|
20 | - public function __construct(Builder $query) |
|
21 | - { |
|
22 | - $this->query = clone $query; |
|
23 | - } |
|
24 | - |
|
25 | - /** |
|
26 | - * Applies filter to a column |
|
27 | - * @param \Closure| $filter |
|
28 | - * @param string $fieldName |
|
29 | - * @param Builder $query |
|
30 | - * @param mixed $value |
|
31 | - * @return void |
|
32 | - */ |
|
33 | - private function applyFilter($filter, string $fieldName, Builder $query, $value) |
|
34 | - { |
|
35 | - if (is_callable($filter)) { |
|
36 | - $filter($query, $value); |
|
37 | - return; |
|
38 | - } |
|
39 | - |
|
40 | - switch ($filter) { |
|
41 | - case '=': |
|
42 | - case '<>': |
|
43 | - case '>=': |
|
44 | - case '>': |
|
45 | - case '<=': |
|
46 | - case '<': |
|
47 | - $query->where($fieldName, $filter, $value); |
|
48 | - break; |
|
49 | - |
|
50 | - case 'like': |
|
51 | - $query->where($fieldName, 'LIKE', '%' . $value . '%'); |
|
52 | - break; |
|
53 | - |
|
54 | - default: |
|
55 | - throw new GridViewConfigException('Unknown filter type: ' . $filter); |
|
56 | - } |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * @param GridViewRequest $request |
|
61 | - * @return Builder |
|
62 | - */ |
|
63 | - protected function baseQuery(GridViewRequest $request) |
|
64 | - { |
|
65 | - $query = clone $this->query; |
|
66 | - |
|
67 | - if ($this->filters !== false) { |
|
68 | - foreach ($request->filters as $field => $value) { |
|
69 | - if ($this->filters === true || in_array($field, $this->filters)) { |
|
70 | - $query->where($field, 'LIKE', '%' . $value . '%'); |
|
71 | - |
|
72 | - } elseif (!empty($this->filters[$field])) { |
|
73 | - $this->applyFilter($this->filters[$field], $field, $query, $value); |
|
74 | - } |
|
75 | - } |
|
76 | - } |
|
77 | - |
|
78 | - if ($request->sortColumn && ($this->ordering === true || in_array($request->sortColumn, $this->ordering))) { |
|
79 | - $query->reorder($request->sortColumn, $request->sortOrder); |
|
80 | - } |
|
81 | - |
|
82 | - return $query; |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * @inheritdoc |
|
87 | - */ |
|
88 | - public function getCount(GridViewRequest $request) : int |
|
89 | - { |
|
90 | - return $this->baseQuery($request)->count(); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @inheritdoc |
|
95 | - */ |
|
96 | - public function getData(GridViewRequest $request) |
|
97 | - { |
|
98 | - $query = $this->baseQuery($request); |
|
99 | - |
|
100 | - if ($request->perPage == 0) { |
|
101 | - return $query->get(); |
|
102 | - } |
|
103 | - |
|
104 | - return $query->offset(($request->page - 1) * $request->perPage) |
|
105 | - ->limit($request->perPage) |
|
106 | - ->get(); |
|
107 | - } |
|
11 | + protected $filters = true; |
|
12 | + protected $ordering = true; |
|
13 | + |
|
14 | + protected $query; |
|
15 | + |
|
16 | + /** |
|
17 | + * EloquentDataProvider constructor. |
|
18 | + * @param Builder $query |
|
19 | + */ |
|
20 | + public function __construct(Builder $query) |
|
21 | + { |
|
22 | + $this->query = clone $query; |
|
23 | + } |
|
24 | + |
|
25 | + /** |
|
26 | + * Applies filter to a column |
|
27 | + * @param \Closure| $filter |
|
28 | + * @param string $fieldName |
|
29 | + * @param Builder $query |
|
30 | + * @param mixed $value |
|
31 | + * @return void |
|
32 | + */ |
|
33 | + private function applyFilter($filter, string $fieldName, Builder $query, $value) |
|
34 | + { |
|
35 | + if (is_callable($filter)) { |
|
36 | + $filter($query, $value); |
|
37 | + return; |
|
38 | + } |
|
39 | + |
|
40 | + switch ($filter) { |
|
41 | + case '=': |
|
42 | + case '<>': |
|
43 | + case '>=': |
|
44 | + case '>': |
|
45 | + case '<=': |
|
46 | + case '<': |
|
47 | + $query->where($fieldName, $filter, $value); |
|
48 | + break; |
|
49 | + |
|
50 | + case 'like': |
|
51 | + $query->where($fieldName, 'LIKE', '%' . $value . '%'); |
|
52 | + break; |
|
53 | + |
|
54 | + default: |
|
55 | + throw new GridViewConfigException('Unknown filter type: ' . $filter); |
|
56 | + } |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * @param GridViewRequest $request |
|
61 | + * @return Builder |
|
62 | + */ |
|
63 | + protected function baseQuery(GridViewRequest $request) |
|
64 | + { |
|
65 | + $query = clone $this->query; |
|
66 | + |
|
67 | + if ($this->filters !== false) { |
|
68 | + foreach ($request->filters as $field => $value) { |
|
69 | + if ($this->filters === true || in_array($field, $this->filters)) { |
|
70 | + $query->where($field, 'LIKE', '%' . $value . '%'); |
|
71 | + |
|
72 | + } elseif (!empty($this->filters[$field])) { |
|
73 | + $this->applyFilter($this->filters[$field], $field, $query, $value); |
|
74 | + } |
|
75 | + } |
|
76 | + } |
|
77 | + |
|
78 | + if ($request->sortColumn && ($this->ordering === true || in_array($request->sortColumn, $this->ordering))) { |
|
79 | + $query->reorder($request->sortColumn, $request->sortOrder); |
|
80 | + } |
|
81 | + |
|
82 | + return $query; |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * @inheritdoc |
|
87 | + */ |
|
88 | + public function getCount(GridViewRequest $request) : int |
|
89 | + { |
|
90 | + return $this->baseQuery($request)->count(); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @inheritdoc |
|
95 | + */ |
|
96 | + public function getData(GridViewRequest $request) |
|
97 | + { |
|
98 | + $query = $this->baseQuery($request); |
|
99 | + |
|
100 | + if ($request->perPage == 0) { |
|
101 | + return $query->get(); |
|
102 | + } |
|
103 | + |
|
104 | + return $query->offset(($request->page - 1) * $request->perPage) |
|
105 | + ->limit($request->perPage) |
|
106 | + ->get(); |
|
107 | + } |
|
108 | 108 | } |
@@ -25,146 +25,146 @@ |
||
25 | 25 | |
26 | 26 | class GridViewHelper |
27 | 27 | { |
28 | - /** |
|
29 | - * A list of grid aliases |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - private static $aliases = [ |
|
33 | - 'column' => [ |
|
34 | - 'attribute' => AttributeColumn::class, |
|
35 | - 'raw' => CallbackColumn::class, |
|
36 | - 'callback' => CallbackColumn::class, |
|
37 | - 'actions' => ActionsColumn::class, |
|
38 | - 'view' => ViewColumn::class, |
|
39 | - ], |
|
40 | - 'formatter' => [ |
|
41 | - 'email' => EmailFormatter::class, |
|
42 | - 'image' => ImageFormatter::class, |
|
43 | - 'text' => TextFormatter::class, |
|
44 | - 'url' => UrlFormatter::class, |
|
45 | - 'raw' => RawFormatter::class, |
|
46 | - 'boolean' => BooleanFormatter::class, |
|
47 | - 'currency' => CurrencyFormatter::class, |
|
48 | - ], |
|
49 | - 'filter' => [ |
|
50 | - 'text' => TextFilter::class, |
|
51 | - 'dropdown' => DropdownFilter::class, |
|
52 | - ], |
|
53 | - 'renderer' => [ |
|
54 | - 'default' => DefaultRenderer::class, |
|
55 | - ], |
|
56 | - 'action' => [ |
|
57 | - 'delete' => DeleteAction::class, |
|
58 | - 'update' => EditAction::class, |
|
59 | - 'edit' => EditAction::class, |
|
60 | - 'show' => ShowAction::class, |
|
61 | - 'view' => ShowAction::class, |
|
62 | - 'action' => Action::class, |
|
63 | - ] |
|
64 | - ]; |
|
65 | - |
|
66 | - private function __construct() {} |
|
67 | - |
|
68 | - /** |
|
69 | - * Useful in case you want to register a new alias for your project |
|
70 | - * @param string $context |
|
71 | - * @param string $alias |
|
72 | - * @param string $aliasTo |
|
73 | - */ |
|
74 | - public static function registerAlias(string $context, string $alias, string $aliasTo) |
|
75 | - { |
|
76 | - self::$aliases[$context][$alias] = $aliasTo; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Allows to resolve class name by its alias |
|
81 | - * @param string $context |
|
82 | - * @param string $alias |
|
83 | - * @return mixed |
|
84 | - */ |
|
85 | - public static function resolveAlias(string $context, string $alias) |
|
86 | - { |
|
87 | - return self::$aliases[$context][$alias] ?? $alias; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Allows to convert options array to html string |
|
92 | - * @param array $htmlOptions |
|
93 | - * @param array $context - context is variables, which are allowed to use when property value calculated dynamically |
|
94 | - * @return string |
|
95 | - */ |
|
96 | - public static function htmlOptionsToString(array $htmlOptions, array $context = []) : string |
|
97 | - { |
|
98 | - if (empty($htmlOptions)) { |
|
99 | - return ''; |
|
100 | - } |
|
101 | - |
|
102 | - $out = []; |
|
103 | - |
|
104 | - foreach ($htmlOptions as $k => $v) { |
|
105 | - |
|
106 | - if ($v instanceof \Closure) { |
|
107 | - $v = call_user_func_array($v, $context); |
|
108 | - } |
|
109 | - |
|
110 | - $out[] = htmlentities($k) . '="' . htmlentities($v, ENT_COMPAT) . '"'; |
|
111 | - } |
|
112 | - |
|
113 | - return implode(' ', $out); |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Allows to make column title by it key or attribute name |
|
118 | - * @param string|int $key |
|
119 | - * @return string |
|
120 | - */ |
|
121 | - public static function columnTitle($key) : string |
|
122 | - { |
|
123 | - if (is_numeric($key)) { |
|
124 | - return 'Column'; |
|
125 | - } |
|
126 | - |
|
127 | - return ucwords( |
|
128 | - trim( |
|
129 | - preg_replace_callback( |
|
130 | - '/([A-Z]|_|\.)/', |
|
131 | - function($word) { |
|
132 | - $word = $word[0]; |
|
133 | - |
|
134 | - if ($word == '_' || $word == '.') { |
|
135 | - return ' '; |
|
136 | - } |
|
137 | - |
|
138 | - return ' ' . strtolower($word); |
|
139 | - }, |
|
140 | - $key |
|
141 | - ) |
|
142 | - ) |
|
143 | - ); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Helper for internal purposes |
|
148 | - * @param $id |
|
149 | - * @param $component |
|
150 | - * @return string |
|
151 | - */ |
|
152 | - public static function gridIdFormatter($id, $component) |
|
153 | - { |
|
154 | - if ($id == 0) { |
|
155 | - return $component; |
|
156 | - } |
|
157 | - |
|
158 | - return 'grid[' . $id . '][' . $component . ']'; |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Generates page url with all requested params from request |
|
163 | - * @param $gridId |
|
164 | - * @param $page |
|
165 | - */ |
|
166 | - public static function pageUrl($gridId, $page) |
|
167 | - { |
|
168 | - return url()->current() . '?' . Arr::query([\Woo\GridView\GridViewHelper::gridIdFormatter($gridId, 'page') => $page] + request()->query()); |
|
169 | - } |
|
28 | + /** |
|
29 | + * A list of grid aliases |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + private static $aliases = [ |
|
33 | + 'column' => [ |
|
34 | + 'attribute' => AttributeColumn::class, |
|
35 | + 'raw' => CallbackColumn::class, |
|
36 | + 'callback' => CallbackColumn::class, |
|
37 | + 'actions' => ActionsColumn::class, |
|
38 | + 'view' => ViewColumn::class, |
|
39 | + ], |
|
40 | + 'formatter' => [ |
|
41 | + 'email' => EmailFormatter::class, |
|
42 | + 'image' => ImageFormatter::class, |
|
43 | + 'text' => TextFormatter::class, |
|
44 | + 'url' => UrlFormatter::class, |
|
45 | + 'raw' => RawFormatter::class, |
|
46 | + 'boolean' => BooleanFormatter::class, |
|
47 | + 'currency' => CurrencyFormatter::class, |
|
48 | + ], |
|
49 | + 'filter' => [ |
|
50 | + 'text' => TextFilter::class, |
|
51 | + 'dropdown' => DropdownFilter::class, |
|
52 | + ], |
|
53 | + 'renderer' => [ |
|
54 | + 'default' => DefaultRenderer::class, |
|
55 | + ], |
|
56 | + 'action' => [ |
|
57 | + 'delete' => DeleteAction::class, |
|
58 | + 'update' => EditAction::class, |
|
59 | + 'edit' => EditAction::class, |
|
60 | + 'show' => ShowAction::class, |
|
61 | + 'view' => ShowAction::class, |
|
62 | + 'action' => Action::class, |
|
63 | + ] |
|
64 | + ]; |
|
65 | + |
|
66 | + private function __construct() {} |
|
67 | + |
|
68 | + /** |
|
69 | + * Useful in case you want to register a new alias for your project |
|
70 | + * @param string $context |
|
71 | + * @param string $alias |
|
72 | + * @param string $aliasTo |
|
73 | + */ |
|
74 | + public static function registerAlias(string $context, string $alias, string $aliasTo) |
|
75 | + { |
|
76 | + self::$aliases[$context][$alias] = $aliasTo; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Allows to resolve class name by its alias |
|
81 | + * @param string $context |
|
82 | + * @param string $alias |
|
83 | + * @return mixed |
|
84 | + */ |
|
85 | + public static function resolveAlias(string $context, string $alias) |
|
86 | + { |
|
87 | + return self::$aliases[$context][$alias] ?? $alias; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Allows to convert options array to html string |
|
92 | + * @param array $htmlOptions |
|
93 | + * @param array $context - context is variables, which are allowed to use when property value calculated dynamically |
|
94 | + * @return string |
|
95 | + */ |
|
96 | + public static function htmlOptionsToString(array $htmlOptions, array $context = []) : string |
|
97 | + { |
|
98 | + if (empty($htmlOptions)) { |
|
99 | + return ''; |
|
100 | + } |
|
101 | + |
|
102 | + $out = []; |
|
103 | + |
|
104 | + foreach ($htmlOptions as $k => $v) { |
|
105 | + |
|
106 | + if ($v instanceof \Closure) { |
|
107 | + $v = call_user_func_array($v, $context); |
|
108 | + } |
|
109 | + |
|
110 | + $out[] = htmlentities($k) . '="' . htmlentities($v, ENT_COMPAT) . '"'; |
|
111 | + } |
|
112 | + |
|
113 | + return implode(' ', $out); |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Allows to make column title by it key or attribute name |
|
118 | + * @param string|int $key |
|
119 | + * @return string |
|
120 | + */ |
|
121 | + public static function columnTitle($key) : string |
|
122 | + { |
|
123 | + if (is_numeric($key)) { |
|
124 | + return 'Column'; |
|
125 | + } |
|
126 | + |
|
127 | + return ucwords( |
|
128 | + trim( |
|
129 | + preg_replace_callback( |
|
130 | + '/([A-Z]|_|\.)/', |
|
131 | + function($word) { |
|
132 | + $word = $word[0]; |
|
133 | + |
|
134 | + if ($word == '_' || $word == '.') { |
|
135 | + return ' '; |
|
136 | + } |
|
137 | + |
|
138 | + return ' ' . strtolower($word); |
|
139 | + }, |
|
140 | + $key |
|
141 | + ) |
|
142 | + ) |
|
143 | + ); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Helper for internal purposes |
|
148 | + * @param $id |
|
149 | + * @param $component |
|
150 | + * @return string |
|
151 | + */ |
|
152 | + public static function gridIdFormatter($id, $component) |
|
153 | + { |
|
154 | + if ($id == 0) { |
|
155 | + return $component; |
|
156 | + } |
|
157 | + |
|
158 | + return 'grid[' . $id . '][' . $component . ']'; |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Generates page url with all requested params from request |
|
163 | + * @param $gridId |
|
164 | + * @param $page |
|
165 | + */ |
|
166 | + public static function pageUrl($gridId, $page) |
|
167 | + { |
|
168 | + return url()->current() . '?' . Arr::query([\Woo\GridView\GridViewHelper::gridIdFormatter($gridId, 'page') => $page] + request()->query()); |
|
169 | + } |
|
170 | 170 | } |
@@ -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 = (array)$row; |
|
46 | - $exp = explode('.', $this->value); |
|
40 | + /** |
|
41 | + * @inheritdoc |
|
42 | + */ |
|
43 | + public function _renderValue($row) |
|
44 | + { |
|
45 | + $path = (array)$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 | } |
@@ -42,7 +42,7 @@ |
||
42 | 42 | */ |
43 | 43 | public function _renderValue($row) |
44 | 44 | { |
45 | - $path = (array)$row; |
|
45 | + $path = (array) $row; |
|
46 | 46 | $exp = explode('.', $this->value); |
47 | 47 | |
48 | 48 | /** |