Completed
Push — master ( a3c126...11274f )
by Pavel
9s
created

ColumnLink   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 11
Bugs 3 Features 0
Metric Value
wmc 14
c 11
b 3
f 0
lcom 1
cbo 2
dl 0
loc 171
rs 10

8 Methods

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