ColumnLink   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 234
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 60.42%

Importance

Changes 0
Metric Value
wmc 22
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 234
rs 10
ccs 29
cts 48
cp 0.6042

11 Methods

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