Completed
Push — master ( fe6898...6997a4 )
by Pavel
20:55
created

ColumnLink::render()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 26
Code Lines 13

Duplication

Lines 9
Ratio 34.62 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 9
loc 26
rs 8.439
cc 6
eloc 13
nc 10
nop 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
16
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 DataGrid
31
	 */
32
	protected $grid;
33
34
	/**
35
	 * @var array
36
	 */
37
	protected $params;
38
39
	/**
40
	 * @var string
41
	 */
42
	protected $href;
43
44
45
	/**
46
	 * @param DataGrid $grid
47
	 * @param string $column
48
	 * @param string $name
49
	 * @param string $href
50
	 * @param array $params
51
	 */
52
	public function __construct(DataGrid $grid, $column, $name, $href, $params)
53
	{
54
		parent::__construct($column, $name);
55
56
		$this->href   = $href;
57
		$this->grid   = $grid;
58
		$this->params = $params;
59
	}
60
61
62
	/**
63
	 * Render row item into template
64
	 * @param  Row   $row
65
	 * @return mixed
66
	 */
67
	public function render(Row $row)
68
	{
69
		/**
70
		 * Renderer function may be used
71
		 */
72 View Code Duplication
		if ($renderer = $this->getRenderer()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
			if (!$renderer->getConditionCallback()) {
74
				return call_user_func_array($renderer->getCallback(), [$row->getItem()]);
75
			}
76
77
			if (call_user_func_array($renderer->getConditionCallback(), [$row->getItem()])) {
78
				return call_user_func_array($renderer->getCallback(), [$row->getItem()]);
79
			}
80
		}
81
82
		$value = parent::render($row);
83
84
		$a = Html::el('a')
85
			->href($this->createLink($this->href, $this->getItemParams($row)))
86
			->setText($value);
87
88
		if ($this->title) { $a->title($this->title); }
89
		if ($this->class) { $a->class($this->class); }
90
91
		return $a;
92
	}
93
94
95
	/**
96
	 * Set attribute title
97
	 * @param string $title
98
	 */
99
	public function setTitle($title)
100
	{
101
		$this->title = $title;
102
103
		return $this;
104
	}
105
106
107
	/**
108
	 * Get attribute title
109
	 */
110
	public function getTitle()
111
	{
112
		return $this->title;
113
	}
114
115
116
	/**
117
	 * Set attribute class
118
	 * @param string $class
119
	 */
120
	public function setClass($class)
121
	{
122
		$this->class = $class;
123
124
		return $this;
125
	}
126
127
128
	/**
129
	 * Get attribute class
130
	 */
131
	public function getClass()
132
	{
133
		return $this->class;
134
	}
135
136
	/**
137
	 * Get item params (E.g. action may be called id => $item->id, name => $item->name, ...)
138
	 * @param  Row   $row
139
	 * @return array
140
	 */
141 View Code Duplication
	protected function getItemParams(Row $row)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
	{
143
		$return = [];
144
145
		foreach ($this->params as $param_name => $param) {
146
			$return[is_string($param_name) ? $param_name : $param] = $row->getValue($param);
147
		}
148
149
		return $return;
150
	}
151
152
}
153