1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Grid\Column; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Grid\Column; |
6
|
|
|
use Encore\Admin\Grid\Model; |
7
|
|
|
use Illuminate\Contracts\Support\Htmlable; |
8
|
|
|
use Illuminate\Contracts\Support\Renderable; |
9
|
|
|
|
10
|
|
|
trait HasHeader |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Filter |
14
|
|
|
*/ |
15
|
|
|
public $filter; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $headers = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Add contents to column header. |
24
|
|
|
* |
25
|
|
|
* @param string|Renderable|Htmlable $header |
26
|
|
|
* |
27
|
|
|
* @return $this |
28
|
|
|
*/ |
29
|
|
|
public function addHeader($header) |
30
|
|
|
{ |
31
|
|
|
if ($header instanceof Filter) { |
32
|
|
|
$header->setParent($this); |
33
|
|
|
$this->filter = $header; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$this->headers[] = $header; |
37
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Add a column sortable to column header. |
43
|
|
|
* |
44
|
|
|
* @param string $cast |
45
|
|
|
* |
46
|
|
|
* @return Column|string |
47
|
|
|
*/ |
48
|
|
|
protected function addSorter($cast = null) |
49
|
|
|
{ |
50
|
|
|
$sortName = $this->grid->model()->getSortName(); |
|
|
|
|
51
|
|
|
|
52
|
|
|
$sorter = new Sorter($sortName, $this->getName(), $cast); |
|
|
|
|
53
|
|
|
|
54
|
|
|
return $this->addHeader($sorter); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Add a help tooltip to column header. |
59
|
|
|
* |
60
|
|
|
* @param string $message |
61
|
|
|
* |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
protected function addHelp($message) |
65
|
|
|
{ |
66
|
|
|
return $this->addHeader(new Help($message)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Add a filter to column header. |
71
|
|
|
* |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
|
|
protected function addFilter($type = null, $formal = null) |
75
|
|
|
{ |
76
|
|
|
if (is_array($type)) { |
77
|
|
|
return $this->addHeader(new CheckFilter($type)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (is_null($type)) { |
81
|
|
|
$type = 'equal'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (in_array($type, ['equal', 'like', 'date', 'time', 'datetime'])) { |
85
|
|
|
return $this->addHeader(new InputFilter($type)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($type === 'range') { |
89
|
|
|
if (is_null($formal)) { |
90
|
|
|
$formal = 'equal'; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->addHeader(new RangeFilter($formal)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Add a binding based on filter to the model query. |
101
|
|
|
* |
102
|
|
|
* @param Model $model |
103
|
|
|
*/ |
104
|
|
|
public function bindFilterQuery(Model $model) |
105
|
|
|
{ |
106
|
|
|
if ($this->filter) { |
107
|
|
|
$this->filter->addBinding(request($this->getName()), $model); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Render Column header. |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
View Code Duplication |
public function renderHeader() |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
return collect($this->headers)->map(function ($item) { |
119
|
|
|
if ($item instanceof Renderable) { |
120
|
|
|
return $item->render(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ($item instanceof Htmlable) { |
124
|
|
|
return $item->toHtml(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return (string)$item; |
128
|
|
|
})->implode(''); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: