1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Pfilsx\DataGrid\Grid\Columns; |
5
|
|
|
|
6
|
|
|
use Exception; |
7
|
|
|
use Pfilsx\DataGrid\DataGridException; |
8
|
|
|
use Pfilsx\DataGrid\Grid\Items\DataGridItemInterface; |
9
|
|
|
|
10
|
|
|
class ActionColumn extends AbstractColumn |
11
|
|
|
{ |
12
|
|
|
protected $buttonsTemplate = '{show} {edit} {delete}'; |
13
|
|
|
|
14
|
|
|
protected $buttons = []; |
15
|
|
|
|
16
|
|
|
protected $urlGenerator; |
17
|
|
|
|
18
|
|
|
protected $pathPrefix; |
19
|
|
|
|
20
|
|
|
protected $identifier; |
21
|
|
|
|
22
|
|
|
protected $buttonsVisibility = [ |
23
|
|
|
'show' => true, |
24
|
|
|
'edit' => true, |
25
|
|
|
'delete' => true |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
public function getHeadContent() |
29
|
|
|
{ |
30
|
|
|
return ''; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function hasFilter() |
34
|
|
|
{ |
35
|
|
|
return false; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getFilterContent() |
39
|
|
|
{ |
40
|
|
|
return null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getCellContent($entity) |
44
|
|
|
{ |
45
|
|
|
return preg_replace_callback('/\{(\w+)\}/', function ($matches) use ($entity) { |
46
|
|
|
if ($this->isButtonVisible($matches[1])) { |
47
|
|
|
if (array_key_exists($matches[1], $this->buttons) && is_callable($this->buttons[$matches[1]])) { |
48
|
|
|
return call_user_func_array($this->buttons[$matches[1]], [ |
49
|
|
|
$entity, |
50
|
|
|
$this->generateUrl($entity, $matches[1]) |
51
|
|
|
]); |
52
|
|
|
} |
53
|
|
|
return $this->template->renderBlock('action_button', [ |
54
|
|
|
'url' => $this->generateUrl($entity, $matches[1]), |
55
|
|
|
'action' => $matches[1], |
56
|
|
|
'entity' => $entity, |
57
|
|
|
'identifier' => $this->identifier |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
return ''; |
61
|
|
|
}, $this->buttonsTemplate); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param DataGridItemInterface $item |
66
|
|
|
* @param string $action |
67
|
|
|
* @return mixed|string |
68
|
|
|
* @throws Exception |
69
|
|
|
*/ |
70
|
|
|
protected function generateUrl($item, $action) |
71
|
|
|
{ |
72
|
|
|
if (is_callable($this->urlGenerator)) { |
73
|
|
|
return call_user_func_array($this->urlGenerator, [$item, $action, $this->container->getRouter()]); |
74
|
|
|
} elseif (!empty($this->identifier) && $item->has($this->identifier)) { |
75
|
|
|
return $this->container->getRouter()->generate($this->pathPrefix . $action, [ |
76
|
|
|
'id' => $item->get($this->identifier) |
77
|
|
|
]); |
78
|
|
|
} elseif ($item->hasIdentifier()) { |
79
|
|
|
return $this->container->getRouter()->generate($this->pathPrefix . $action, [ |
80
|
|
|
'id' => $item->get($item->getIdentifier()) |
81
|
|
|
]); |
82
|
|
|
} else { |
83
|
|
|
throw new DataGridException('Could not generate url for action: ' . $action); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getButtonsTemplate(): string |
91
|
|
|
{ |
92
|
|
|
return $this->buttonsTemplate; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $buttonsTemplate |
97
|
|
|
*/ |
98
|
|
|
protected function setButtonsTemplate(string $buttonsTemplate): void |
99
|
|
|
{ |
100
|
|
|
$this->buttonsTemplate = $buttonsTemplate; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public function getButtons(): array |
107
|
|
|
{ |
108
|
|
|
return $this->buttons; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param array $buttons |
113
|
|
|
*/ |
114
|
|
|
protected function setButtons(array $buttons): void |
115
|
|
|
{ |
116
|
|
|
$this->buttons = $buttons; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return mixed |
121
|
|
|
*/ |
122
|
|
|
public function getUrlGenerator() |
123
|
|
|
{ |
124
|
|
|
return $this->urlGenerator; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param mixed $urlGenerator |
129
|
|
|
*/ |
130
|
|
|
protected function setUrlGenerator(callable $urlGenerator): void |
131
|
|
|
{ |
132
|
|
|
$this->urlGenerator = $urlGenerator; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return mixed |
137
|
|
|
*/ |
138
|
|
|
public function getPathPrefix() |
139
|
|
|
{ |
140
|
|
|
return $this->pathPrefix; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param mixed $pathPrefix |
145
|
|
|
*/ |
146
|
|
|
protected function setPathPrefix($pathPrefix): void |
147
|
|
|
{ |
148
|
|
|
$this->pathPrefix = $pathPrefix; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string $name |
153
|
|
|
* @return bool |
154
|
|
|
*/ |
155
|
|
|
public function isButtonVisible(string $name): bool |
156
|
|
|
{ |
157
|
|
|
return !array_key_exists($name, $this->buttonsVisibility) || $this->buttonsVisibility[$name]; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param array $buttonsVisibility |
162
|
|
|
*/ |
163
|
|
|
public function setButtonsVisibility(array $buttonsVisibility): void |
164
|
|
|
{ |
165
|
|
|
$this->buttonsVisibility = array_merge($this->buttonsVisibility, $buttonsVisibility); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
protected function setIdentifier(string $identifier) |
169
|
|
|
{ |
170
|
|
|
$this->identifier = $identifier; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|