Completed
Pull Request — master (#153)
by
unknown
02:36
created

ColumnLink::setDataAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
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
	/**
52
	 * @param DataGrid $grid
53
	 * @param string $key
54
	 * @param string $column
55
	 * @param string $name
56
	 * @param string $href
57
	 * @param array  $params
58
	 */
59
	public function __construct(DataGrid $grid, $key, $column, $name, $href, $params)
60
	{
61
		parent::__construct($grid, $key, $column, $name);
62
63
		$this->href   = $href;
64
		$this->params = $params;
65
	}
66
67
68
	/**
69
	 * Render row item into template
70
	 * @param  Row   $row
71
	 * @return mixed
72
	 */
73
	public function render(Row $row)
74
	{
75
		/**
76
		 * Renderer function may be used
77
		 */
78
		try {
79
			return $this->useRenderer($row);
80
		} catch (DataGridColumnRendererException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
81
			/**
82
			 * Do not use renderer
83
			 */
84
		}
85
86
		$value = parent::render($row);
87
88
		$a = Html::el('a')
89
			->href($this->createLink($this->href, $this->getItemParams($row, $this->params)))
90
			->setText($value);
91
			
92
		$a->addAttributes($this->data_attributes);
93
94
		if ($this->title) { $a->title($this->title); }
95
		if ($this->class) { $a->class($this->class); }
96
		
97
		$element = $a;
98
		
99
		if($this->icon != NULL) {
100
			$emptyEl = Html::el('span');
101
102
			$span = Html::el('span')->addAttributes(array("class" => $this->icon));
103
			$span->setHtml("&nbsp");
104
105
			$emptyEl->add($span);
106
			$emptyEl->add($a);
107
108
			$element = $emptyEl;
109
		}
110
111
		return $element;
112
	}
113
114
115
	/**
116
	 * Set icon before simple link
117
	 * @param string $class
118
	 * @return ColumnLink
119
	 */
120
	public function setIcon($class = NULL)
121
	{
122
		$this->icon = $class;
123
124
		return $this;
125
	}
126
127
128
	/**
129
	 * Setting data attributes
130
	 * @param string $key
131
	 * @param mixed $value
132
	 * @return static
133
	 */
134
	public function setDataAttribute($key, $value)
135
	{
136
		$this->data_attributes[$key] = $value;
137
138
		return $this;
139
	}
140
141
142
	/**
143
	 * Set attribute title
144
	 * @param string $title
145
	 */
146
	public function setTitle($title)
147
	{
148
		$this->title = $title;
149
150
		return $this;
151
	}
152
153
154
	/**
155
	 * Get attribute title
156
	 */
157
	public function getTitle()
158
	{
159
		return $this->title;
160
	}
161
162
163
	/**
164
	 * Set attribute class
165
	 * @param string $class
166
	 */
167
	public function setClass($class)
168
	{
169
		$this->class = $class;
170
171
		return $this;
172
	}
173
174
175
	/**
176
	 * Get attribute class
177
	 */
178
	public function getClass()
179
	{
180
		return $this->class;
181
	}
182
183
}
184