|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Encore\Admin\Grid\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
trait HasElementNames |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Grid name. |
|
9
|
|
|
* |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $name; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* HTML element names. |
|
16
|
|
|
* |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $elementNames = [ |
|
20
|
|
|
'grid_row' => 'grid-row', |
|
21
|
|
|
'grid_select_all' => 'grid-select-all', |
|
22
|
|
|
'grid_per_page' => 'grid-per-pager', |
|
23
|
|
|
'grid_batch' => 'grid-batch', |
|
24
|
|
|
'export_selected' => 'export-selected', |
|
25
|
|
|
'selected_rows' => 'selectedRows', |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Set name to grid. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $name |
|
32
|
|
|
* |
|
33
|
|
|
* @return $this |
|
34
|
|
|
*/ |
|
35
|
|
|
public function setName($name) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->name = $name; |
|
38
|
|
|
|
|
39
|
|
|
$this->model()->setPerPageName("{$name}_{$this->model()->getPerPageName()}"); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
$this->getFilter()->setName($name); |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get name of grid. |
|
48
|
|
|
* |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getName() |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->name; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getGridRowName() |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->elementNameWithPrefix('grid_row'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getSelectAllName() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->elementNameWithPrefix('grid_select_all'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getPerPageName() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->elementNameWithPrefix('grid_per_page'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getGridBatchName() |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->elementNameWithPrefix('grid_batch'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getExportSelectedName() |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->elementNameWithPrefix('export_selected'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getSelectedRowsName() |
|
100
|
|
|
{ |
|
101
|
|
|
$elementName = $this->elementNames['selected_rows']; |
|
102
|
|
|
|
|
103
|
|
|
if ($this->name) { |
|
104
|
|
|
return sprintf('%s%s', $this->name, ucfirst($elementName)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $elementName; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function elementNameWithPrefix($name) |
|
114
|
|
|
{ |
|
115
|
|
|
$elementName = $this->elementNames[$name]; |
|
116
|
|
|
|
|
117
|
|
|
if ($this->name) { |
|
118
|
|
|
return sprintf('%s-%s', $this->name, $elementName); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $elementName; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.