1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) 2015 Giant.cz <[email protected]> |
5
|
|
|
* @author Pavel Janda <[email protected]> |
6
|
|
|
* @package Giant |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Ublaboo\DataGrid\Column; |
10
|
|
|
|
11
|
|
|
use Nette\Utils\Html; |
12
|
|
|
use Ublaboo\DataGrid\DataGrid; |
13
|
|
|
use Ublaboo\DataGrid\Row; |
14
|
|
|
use Ublaboo\DataGrid\Exception\DataGridHasToBeAttachedToPresenterComponentException; |
15
|
|
|
use Ublaboo\DataGrid\Exception\DataGridColumnRendererException; |
16
|
|
|
|
17
|
|
|
class ColumnLink extends Column |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $title; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $class; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $params; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $href; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $icon; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $data_attributes = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var bool |
52
|
|
|
*/ |
53
|
|
|
protected $open_in_new_tab = FALSE; |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param DataGrid $grid |
58
|
|
|
* @param string $key |
59
|
|
|
* @param string $column |
60
|
|
|
* @param string $name |
61
|
|
|
* @param string $href |
62
|
|
|
* @param array $params |
63
|
|
|
*/ |
64
|
|
|
public function __construct(DataGrid $grid, $key, $column, $name, $href, $params) |
65
|
|
|
{ |
66
|
|
|
parent::__construct($grid, $key, $column, $name); |
67
|
|
|
|
68
|
|
|
$this->href = $href; |
69
|
|
|
$this->params = $params; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Render row item into template |
75
|
|
|
* @param Row $row |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
|
|
public function render(Row $row) |
79
|
|
|
{ |
80
|
|
|
/** |
81
|
|
|
* Renderer function may be used |
82
|
|
|
*/ |
83
|
|
|
try { |
84
|
|
|
return $this->useRenderer($row); |
85
|
|
|
} catch (DataGridColumnRendererException $e) { |
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Do not use renderer |
88
|
|
|
*/ |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$value = parent::render($row); |
92
|
|
|
|
93
|
|
|
if (!$value && !$this->icon) { |
94
|
|
|
return NULL; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$a = Html::el('a') |
98
|
|
|
->href($this->createLink( |
99
|
|
|
$this->grid, |
100
|
|
|
$this->href, |
101
|
|
|
$this->getItemParams($row, $this->params) |
102
|
|
|
)); |
103
|
|
|
|
104
|
|
|
if (!empty($this->data_attributes)) { |
105
|
|
|
foreach ($this->data_attributes as $key => $attr_value) { |
106
|
|
|
$a->data($key, $attr_value); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ($this->open_in_new_tab) { |
111
|
|
|
$a->addAttributes(['target' => '_blank']); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ($this->title) { $a->title($this->title); } |
115
|
|
|
if ($this->class) { $a->class($this->class); } |
116
|
|
|
|
117
|
|
|
$element = $a; |
118
|
|
|
|
119
|
|
|
if ($this->icon) { |
120
|
|
|
$a->addHtml(Html::el('span')->class(DataGrid::$icon_prefix . $this->icon)); |
121
|
|
|
|
122
|
|
|
if (strlen($value)) { |
123
|
|
|
$a->addHtml(' '); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if ($this->isTemplateEscaped()) { |
128
|
|
|
$a->addText($value); |
129
|
|
|
} else { |
130
|
|
|
$a->addHtml($value); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $element; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Set icon before simple link |
139
|
|
|
* @param string $icon |
140
|
|
|
* @return ColumnLink |
141
|
|
|
*/ |
142
|
|
|
public function setIcon($icon = NULL) |
143
|
|
|
{ |
144
|
|
|
$this->icon = $icon; |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Setting data attributes |
152
|
|
|
* @param string $key |
153
|
|
|
* @param mixed $value |
154
|
|
|
* @return static |
155
|
|
|
*/ |
156
|
|
|
public function setDataAttribute($key, $value) |
157
|
|
|
{ |
158
|
|
|
$this->data_attributes[$key] = $value; |
159
|
|
|
|
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Set attribute title |
166
|
|
|
* @param string $title |
167
|
|
|
*/ |
168
|
|
|
public function setTitle($title) |
169
|
|
|
{ |
170
|
|
|
$this->title = $title; |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get attribute title |
178
|
|
|
*/ |
179
|
|
|
public function getTitle() |
180
|
|
|
{ |
181
|
|
|
return $this->title; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Set attribute class |
187
|
|
|
* @param string $class |
188
|
|
|
*/ |
189
|
|
|
public function setClass($class) |
190
|
|
|
{ |
191
|
|
|
$this->class = $class; |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Get attribute class |
199
|
|
|
*/ |
200
|
|
|
public function getClass() |
201
|
|
|
{ |
202
|
|
|
return $this->class; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Open link in new window/tab? |
207
|
|
|
* @return boolean |
208
|
|
|
*/ |
209
|
|
|
public function isOpenInNewTab() |
210
|
|
|
{ |
211
|
|
|
return $this->open_in_new_tab; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Set link to open in new tab/window or not |
216
|
|
|
* @param boolean $open_in_new_tab |
217
|
|
|
*/ |
218
|
|
|
public function setOpenInNewTab($open_in_new_tab) |
219
|
|
|
{ |
220
|
|
|
$this->open_in_new_tab = $open_in_new_tab; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
|
224
|
|
|
|
225
|
|
|
|
226
|
|
|
} |
227
|
|
|
|