Completed
Push — master ( e22553...3e6748 )
by Pavel
02:36
created

ToolbarButton::renderButton()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 24
rs 8.6845
c 1
b 0
f 0
cc 4
eloc 12
nc 8
nop 0
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\Toolbar;
10
11
use Nette\Utils\Html;
12
use Ublaboo\DataGrid\DataGrid;
13
use Ublaboo\DataGrid\Traits;
14
15
class ToolbarButton
16
{
17
18
	use Traits\TButton;
19
	use Traits\TLink;
20
21
	/**
22
	 * @var DataGrid
23
	 */
24
	protected $grid;
25
26
	/**
27
	 * @var string
28
	 */
29
	protected $href;
30
31
	/**
32
	 * @var array
33
	 */
34
	protected $params;
35
36
	/**
37
	 * @var array
38
	 */
39
	protected $attributes = [];
40
41
42
	/**
43
	 * @param DataGrid $grid
44
	 * @param string   $href
45
	 * @param string   $text
46
	 * @param array    $params
47
	 */
48
	public function __construct(DataGrid $grid, $href, $text, $params = [])
49
	{
50
		$this->grid = $grid;
51
		$this->href = $href;
52
		$this->text = $text;
53
		$this->params = $params;
54
	}
55
56
57
	/**
58
	 * Render toolbar button
59
	 * @return Html
60
	 */
61
	public function renderButton()
62
	{
63
		$link = $this->createLink($this->grid, $this->href, $this->params);
64
65
		$a = Html::el('a')->href($link);
66
67
		$this->tryAddIcon($a, $this->getIcon(), $this->getText());
68
69
		if (!empty($this->attributes)) {
70
			$a->addAttributes($this->attributes);
71
		}
72
73
		$a->addText($this->grid->getTranslator()->translate($this->text));
74
75
		if ($this->title) {
76
			$a->title($this->grid->getTranslator()->translate($this->title));
77
		}
78
79
		if ($this->class) {
80
			$a->class($this->class);
81
		}
82
83
		return $a;
84
	}
85
86
87
	/**
88
	 * @param array $attrs
89
	 * @return static
90
	 */
91
	public function addAttributes(array $attrs)
92
	{
93
		$this->attributes = $this->attributes + $attrs;
94
95
		return $this;
96
	}
97
98
}
99