Completed
Pull Request — master (#153)
by
unknown
02:47
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
		if ($this->title) { $a->title($this->title); }
93
		if ($this->class) { $a->class($this->class); }
94
		
95
		$element = $a;
96
		
97
		if($this->icon != NULL) {
98
			$emptyEl = Html::el('span');
99
100
			$span = Html::el('span')->addAttributes(array("class" => $this->icon));
101
			$span->setHtml("&nbsp");
102
103
			$emptyEl->add($span);
104
			$emptyEl->add($a);
105
106
			$element = $emptyEl;
107
		}
108
109
		return $element;
110
	}
111
112
113
	/**
114
	 * Set icon before simple link
115
	 * @param string $class
116
	 * @return ColumnLink
117
	 */
118
	public function setIcon($class = NULL)
119
	{
120
		$this->icon = $class;
121
122
		return $this;
123
	}
124
125
126
	/**
127
	 * Setting data attributes
128
	 * @param string $key
129
	 * @param mixed $value
130
	 * @return static
131
	 */
132
	public function setDataAttribute($key, $value)
133
	{
134
		$this->data_attributes[$key] = $value;
135
136
		return $this;
137
	}
138
139
140
	/**
141
	 * Set attribute title
142
	 * @param string $title
143
	 */
144
	public function setTitle($title)
145
	{
146
		$this->title = $title;
147
148
		return $this;
149
	}
150
151
152
	/**
153
	 * Get attribute title
154
	 */
155
	public function getTitle()
156
	{
157
		return $this->title;
158
	}
159
160
161
	/**
162
	 * Set attribute class
163
	 * @param string $class
164
	 */
165
	public function setClass($class)
166
	{
167
		$this->class = $class;
168
169
		return $this;
170
	}
171
172
173
	/**
174
	 * Get attribute class
175
	 */
176
	public function getClass()
177
	{
178
		return $this->class;
179
	}
180
181
}
182