Completed
Push — master ( ea0b5b...2eb559 )
by Martin
02:52
created

ColumnLink::setDataAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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
	 * @var array
58
	 */
59
	protected $parameters = [];
60
61
62
	/**
63
	 * @param DataGrid $grid
64
	 * @param string $key
65
	 * @param string $column
66
	 * @param string $name
67
	 * @param string $href
68
	 * @param array  $params
69
	 */
70
	public function __construct(DataGrid $grid, $key, $column, $name, $href, $params)
71
	{
72
		parent::__construct($grid, $key, $column, $name);
73
74
		$this->href   = $href;
75
		$this->params = $params;
76
	}
77
78
79
	/**
80
	 * Render row item into template
81
	 * @param  Row   $row
82
	 * @return mixed
83
	 */
84
	public function render(Row $row)
85
	{
86
		/**
87
		 * Renderer function may be used
88
		 */
89
		try {
90
			return $this->useRenderer($row);
91
		} catch (DataGridColumnRendererException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
92
			/**
93
			 * Do not use renderer
94
			 */
95
		}
96
97
		$value = parent::render($row);
98
99
		if (!$value && !$this->icon) {
100
			return NULL;
101
		}
102
103
		$a = Html::el('a')
104
			->href($this->createLink(
105
				$this->grid,
106
				$this->href,
107
				$this->getItemParams($row, $this->params) + $this->parameters
108
			));
109
			
110
		if (!empty($this->data_attributes)) {
111
			foreach ($this->data_attributes as $key => $attr_value) {
112
				$a->data($key, $attr_value);
113
			}
114
		}
115
116
		if ($this->open_in_new_tab) {
117
			$a->addAttributes(['target' => '_blank']);
118
		}
119
120
		if ($this->title) { $a->title($this->title); }
121
		if ($this->class) { $a->class($this->class); }
122
		
123
		$element = $a;
124
125
		if ($this->icon) {
126
			$a->addHtml(Html::el('span')->class(DataGrid::$icon_prefix . $this->icon));
127
128
			if (strlen($value)) {
129
				$a->addHtml('&nbsp;');
130
			}
131
		}
132
133
		if ($this->isTemplateEscaped()) {
134
			$a->addText($value);
135
		} else {
136
			$a->addHtml($value);
137
		}
138
139
		return $element;
140
	}
141
142
143
	/**
144
	 * Add parameters to link
145
	 * @param array $parameters
146
	 * @return static
147
	 */
148
	public function addParameters(array $parameters)
149
	{
150
		$this->parameters = $parameters;
151
152
		return $this;
153
	}
154
155
156
	/**
157
	 * Set icon before simple link
158
	 * @param string      $icon
159
	 * @return ColumnLink
160
	 */
161
	public function setIcon($icon = NULL)
162
	{
163
		$this->icon = $icon;
164
165
		return $this;
166
	}
167
168
169
	/**
170
	 * Setting data attributes
171
	 * @param string $key
172
	 * @param mixed  $value
173
	 * @return static
174
	 */
175
	public function setDataAttribute($key, $value)
176
	{
177
		$this->data_attributes[$key] = $value;
178
179
		return $this;
180
	}
181
182
183
	/**
184
	 * Set attribute title
185
	 * @param string $title
186
	 */
187
	public function setTitle($title)
188
	{
189
		$this->title = $title;
190
191
		return $this;
192
	}
193
194
195
	/**
196
	 * Get attribute title
197
	 */
198
	public function getTitle()
199
	{
200
		return $this->title;
201
	}
202
203
204
	/**
205
	 * Set attribute class
206
	 * @param string $class
207
	 */
208
	public function setClass($class)
209
	{
210
		$this->class = $class;
211
212
		return $this;
213
	}
214
215
216
	/**
217
	 * Get attribute class
218
	 */
219
	public function getClass()
220
	{
221
		return $this->class;
222
	}
223
224
	/**
225
	 * Open link in new window/tab?
226
	 * @return boolean
227
	 */
228
	public function isOpenInNewTab()
229
	{
230
		return $this->open_in_new_tab;
231
	}
232
233
	/**
234
	 * Set link to open in new tab/window or not
235
	 * @param boolean $open_in_new_tab
236
	 */
237
	public function setOpenInNewTab($open_in_new_tab)
238
	{
239
		$this->open_in_new_tab = $open_in_new_tab;
240
	}
241
242
243
244
245
}
246