@@ -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 | } |
@@ -9,170 +9,170 @@ |
||
9 | 9 | |
10 | 10 | abstract class BaseColumn |
11 | 11 | { |
12 | - use Configurable; |
|
13 | - |
|
14 | - /** |
|
15 | - * Column title |
|
16 | - * @var string |
|
17 | - */ |
|
18 | - public $title = ''; |
|
19 | - |
|
20 | - /** |
|
21 | - * Column value. Could be an attribute, |
|
22 | - * @var string|mixed |
|
23 | - */ |
|
24 | - public $value = ''; |
|
25 | - |
|
26 | - /** |
|
27 | - * @var BaseFilter |
|
28 | - */ |
|
29 | - public $filter; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var boolean|string |
|
33 | - */ |
|
34 | - public $sortable = true; |
|
35 | - |
|
36 | - /** |
|
37 | - * @var array |
|
38 | - */ |
|
39 | - public $headerHtmlOptions = []; |
|
40 | - |
|
41 | - /** |
|
42 | - * @var array |
|
43 | - */ |
|
44 | - public $contentHtmlOptions = []; |
|
45 | - |
|
46 | - /** |
|
47 | - * @var array - allowed: raw, url, email, text, image |
|
48 | - */ |
|
49 | - public $formatters = ['text']; |
|
50 | - |
|
51 | - /** |
|
52 | - * Value when column is empty |
|
53 | - * @var string |
|
54 | - */ |
|
55 | - public $emptyValue = '-'; |
|
56 | - |
|
57 | - /** |
|
58 | - * BaseColumn constructor. |
|
59 | - * @param array $config |
|
60 | - * @throws \Woo\GridView\Exceptions\GridViewConfigException |
|
61 | - */ |
|
62 | - public function __construct(array $config) |
|
63 | - { |
|
64 | - $this->loadConfig($config); |
|
65 | - |
|
66 | - $this->buildFilter(); |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * Allows to get sortable column's name |
|
71 | - */ |
|
72 | - public function getSortableName() |
|
73 | - { |
|
74 | - if ($this->sortable === false) { |
|
75 | - return false; |
|
76 | - } |
|
77 | - |
|
78 | - if (!is_bool($this->sortable)) { |
|
79 | - return $this->sortable; |
|
80 | - } |
|
81 | - |
|
82 | - if (is_scalar($this->value)) { |
|
83 | - return $this->value; |
|
84 | - } |
|
85 | - |
|
86 | - return false; |
|
87 | - } |
|
88 | - |
|
89 | - protected function buildFilter() |
|
90 | - { |
|
91 | - if (is_null($this->filter) || is_object($this->filter)) { |
|
92 | - return; |
|
93 | - } |
|
94 | - |
|
95 | - if (is_string($this->filter)) { |
|
96 | - $this->filter = [ |
|
97 | - 'class' => $this->filter, |
|
98 | - 'name' => $this->value, |
|
99 | - ]; |
|
100 | - } |
|
101 | - |
|
102 | - if (empty($this->filter['class'])) { |
|
103 | - $this->filter['class'] = TextFilter::class; |
|
104 | - } |
|
105 | - |
|
106 | - if (empty($this->filter['name'])) { |
|
107 | - $this->filter['name'] = $this->value; |
|
108 | - } |
|
109 | - |
|
110 | - $className = GridViewHelper::resolveAlias('filter', $this->filter['class']); |
|
111 | - $this->filter = new $className($this->filter); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @return array |
|
116 | - */ |
|
117 | - protected function configTests(): array |
|
118 | - { |
|
119 | - return [ |
|
120 | - 'title' => 'string', |
|
121 | - 'value' => 'any', |
|
122 | - 'headerHtmlOptions' => 'array', |
|
123 | - 'contentHtmlOptions' => 'array', |
|
124 | - 'formatters' => 'array', |
|
125 | - 'emptyValue' => 'string', |
|
126 | - 'sortable' => 'any', |
|
127 | - 'filter' => BaseFilter::class . '|null', |
|
128 | - ]; |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Formatted header html options |
|
133 | - * @return string |
|
134 | - */ |
|
135 | - public function compileHeaderHtmlOptions() : string |
|
136 | - { |
|
137 | - return GridViewHelper::htmlOptionsToString($this->headerHtmlOptions); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Formatted content html options |
|
142 | - * @param array $context |
|
143 | - * @return string |
|
144 | - */ |
|
145 | - public function compileContentHtmlOptions(array $context) : string |
|
146 | - { |
|
147 | - return GridViewHelper::htmlOptionsToString($this->contentHtmlOptions, $context); |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Render column value for row |
|
152 | - * @param array|object $row |
|
153 | - * @return string|mixed |
|
154 | - */ |
|
155 | - protected abstract function _renderValue($row); |
|
156 | - |
|
157 | - /** |
|
158 | - * Renders column content |
|
159 | - * @param $row |
|
160 | - * @return string |
|
161 | - */ |
|
162 | - public function renderValue($row) |
|
163 | - { |
|
164 | - // QueryBuilder returns objects instead of arrays |
|
165 | - if (!is_array($row)){ |
|
166 | - $row = (array)$row; |
|
167 | - } |
|
168 | - $value = $this->_renderValue($row); |
|
169 | - |
|
170 | - foreach ($this->formatters as $formatter) { |
|
171 | - $className = GridViewHelper::resolveAlias('formatter', $formatter); |
|
172 | - $value = (new $className)->format($value); |
|
173 | - } |
|
174 | - |
|
175 | - return $value; |
|
176 | - } |
|
12 | + use Configurable; |
|
13 | + |
|
14 | + /** |
|
15 | + * Column title |
|
16 | + * @var string |
|
17 | + */ |
|
18 | + public $title = ''; |
|
19 | + |
|
20 | + /** |
|
21 | + * Column value. Could be an attribute, |
|
22 | + * @var string|mixed |
|
23 | + */ |
|
24 | + public $value = ''; |
|
25 | + |
|
26 | + /** |
|
27 | + * @var BaseFilter |
|
28 | + */ |
|
29 | + public $filter; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var boolean|string |
|
33 | + */ |
|
34 | + public $sortable = true; |
|
35 | + |
|
36 | + /** |
|
37 | + * @var array |
|
38 | + */ |
|
39 | + public $headerHtmlOptions = []; |
|
40 | + |
|
41 | + /** |
|
42 | + * @var array |
|
43 | + */ |
|
44 | + public $contentHtmlOptions = []; |
|
45 | + |
|
46 | + /** |
|
47 | + * @var array - allowed: raw, url, email, text, image |
|
48 | + */ |
|
49 | + public $formatters = ['text']; |
|
50 | + |
|
51 | + /** |
|
52 | + * Value when column is empty |
|
53 | + * @var string |
|
54 | + */ |
|
55 | + public $emptyValue = '-'; |
|
56 | + |
|
57 | + /** |
|
58 | + * BaseColumn constructor. |
|
59 | + * @param array $config |
|
60 | + * @throws \Woo\GridView\Exceptions\GridViewConfigException |
|
61 | + */ |
|
62 | + public function __construct(array $config) |
|
63 | + { |
|
64 | + $this->loadConfig($config); |
|
65 | + |
|
66 | + $this->buildFilter(); |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * Allows to get sortable column's name |
|
71 | + */ |
|
72 | + public function getSortableName() |
|
73 | + { |
|
74 | + if ($this->sortable === false) { |
|
75 | + return false; |
|
76 | + } |
|
77 | + |
|
78 | + if (!is_bool($this->sortable)) { |
|
79 | + return $this->sortable; |
|
80 | + } |
|
81 | + |
|
82 | + if (is_scalar($this->value)) { |
|
83 | + return $this->value; |
|
84 | + } |
|
85 | + |
|
86 | + return false; |
|
87 | + } |
|
88 | + |
|
89 | + protected function buildFilter() |
|
90 | + { |
|
91 | + if (is_null($this->filter) || is_object($this->filter)) { |
|
92 | + return; |
|
93 | + } |
|
94 | + |
|
95 | + if (is_string($this->filter)) { |
|
96 | + $this->filter = [ |
|
97 | + 'class' => $this->filter, |
|
98 | + 'name' => $this->value, |
|
99 | + ]; |
|
100 | + } |
|
101 | + |
|
102 | + if (empty($this->filter['class'])) { |
|
103 | + $this->filter['class'] = TextFilter::class; |
|
104 | + } |
|
105 | + |
|
106 | + if (empty($this->filter['name'])) { |
|
107 | + $this->filter['name'] = $this->value; |
|
108 | + } |
|
109 | + |
|
110 | + $className = GridViewHelper::resolveAlias('filter', $this->filter['class']); |
|
111 | + $this->filter = new $className($this->filter); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @return array |
|
116 | + */ |
|
117 | + protected function configTests(): array |
|
118 | + { |
|
119 | + return [ |
|
120 | + 'title' => 'string', |
|
121 | + 'value' => 'any', |
|
122 | + 'headerHtmlOptions' => 'array', |
|
123 | + 'contentHtmlOptions' => 'array', |
|
124 | + 'formatters' => 'array', |
|
125 | + 'emptyValue' => 'string', |
|
126 | + 'sortable' => 'any', |
|
127 | + 'filter' => BaseFilter::class . '|null', |
|
128 | + ]; |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Formatted header html options |
|
133 | + * @return string |
|
134 | + */ |
|
135 | + public function compileHeaderHtmlOptions() : string |
|
136 | + { |
|
137 | + return GridViewHelper::htmlOptionsToString($this->headerHtmlOptions); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Formatted content html options |
|
142 | + * @param array $context |
|
143 | + * @return string |
|
144 | + */ |
|
145 | + public function compileContentHtmlOptions(array $context) : string |
|
146 | + { |
|
147 | + return GridViewHelper::htmlOptionsToString($this->contentHtmlOptions, $context); |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Render column value for row |
|
152 | + * @param array|object $row |
|
153 | + * @return string|mixed |
|
154 | + */ |
|
155 | + protected abstract function _renderValue($row); |
|
156 | + |
|
157 | + /** |
|
158 | + * Renders column content |
|
159 | + * @param $row |
|
160 | + * @return string |
|
161 | + */ |
|
162 | + public function renderValue($row) |
|
163 | + { |
|
164 | + // QueryBuilder returns objects instead of arrays |
|
165 | + if (!is_array($row)){ |
|
166 | + $row = (array)$row; |
|
167 | + } |
|
168 | + $value = $this->_renderValue($row); |
|
169 | + |
|
170 | + foreach ($this->formatters as $formatter) { |
|
171 | + $className = GridViewHelper::resolveAlias('formatter', $formatter); |
|
172 | + $value = (new $className)->format($value); |
|
173 | + } |
|
174 | + |
|
175 | + return $value; |
|
176 | + } |
|
177 | 177 | |
178 | 178 | } |
@@ -162,8 +162,8 @@ |
||
162 | 162 | public function renderValue($row) |
163 | 163 | { |
164 | 164 | // QueryBuilder returns objects instead of arrays |
165 | - if (!is_array($row)){ |
|
166 | - $row = (array)$row; |
|
165 | + if (!is_array($row)) { |
|
166 | + $row = (array) $row; |
|
167 | 167 | } |
168 | 168 | $value = $this->_renderValue($row); |
169 | 169 |