MultiAction::getActions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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\Column;
10
11
use Nette\Utils\Html;
12
use Ublaboo\DataGrid\DataGrid;
13
use Ublaboo\DataGrid\Exception\DataGridException;
14
use Ublaboo\DataGrid\Row;
15
use Ublaboo\DataGrid\Traits;
16
17
class MultiAction extends Column
18
{
19
	use Traits\TButtonTryAddIcon;
20
	use Traits\TButtonIcon;
21
	use Traits\TButtonClass;
22
	use Traits\TButtonTitle;
23
	use Traits\TButtonText;
24
	use Traits\TButtonCaret;
25
	use Traits\TLink;
26
27
	/**
28
	 * @var DataGrid
29
	 */
30
	protected $grid;
31
32
	/**
33
	 * @var string
34
	 */
35
	protected $name;
36
37
	/**
38
	 * @var array
39
	 */
40
	protected $actions = [];
41
42
	/**
43
	 * @var callable[]
44
	 */
45
	private $rowConditions = [];
46
47
48
	/**
49
	 * @param DataGrid $grid
50
	 */
51
	public function __construct(DataGrid $grid, $name)
52
	{
53
		$this->grid = $grid;
54
		$this->name = $name;
55
56
		$this->setTemplate(__DIR__ . '/../templates/column_multi_action.latte');
57
	}
58
59
60
	/**
61
	 * @return Html
62
	 */
63
	public function renderButton()
64
	{
65
		$button = Html::el('button')
66
			->type('button')
67
			->data('toggle', 'dropdown');
68
69
		$this->tryAddIcon($button, $this->getIcon(), $this->getName());
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
70
71
		if (!empty($this->attributes)) {
72
			$button->addAttributes($this->attributes);
73
		}
74
75
		$button->addText($this->grid->getTranslator()->translate($this->name));
76
77
		if ($this->hasCaret()) {
78
			$button->addHtml('&nbsp;');
79
			$button->addHtml('<i class="caret"></i>');
80
		}
81
82
		if ($this->getTitle()) {
83
			$button->title($this->grid->getTranslator()->translate($this->getTitle()));
84
		}
85
86
		if ($this->getClass()) {
87
			$button->class($this->getClass() . ' dropdown-toggle');
88
		}
89
90
		return $button;
91
	}
92
93
94
	/**
95
	 * @param string     $key
96
	 * @param string     $name
97
	 * @param string     $href
98
	 * @param array|null $params
99
	 * @return static
100
	 */
101
	public function addAction($key, $name, $href = null, array $params = null)
102
	{
103
		if (isset($this->actions[$key])) {
104
			throw new DataGridException(
105
				"There is already action at key [$key] defined for MultiAction."
106
			);
107
		}
108
109
		$href = $href ?: $key;
110
111
		if ($params === null) {
112
			$params = [$this->grid->getPrimaryKey()];
113
		}
114
115
		$action = new Action($this->grid, $href, $name, $params);
116
117
		$action->setClass('');
118
119
		$this->actions[$key] = $action;
120
121
		return $this;
122
	}
123
124
125
	/**
126
	 * @return Action[]
127
	 */
128
	public function getActions()
129
	{
130
		return $this->actions;
131
	}
132
133
134
	/**
135
	 * @param  string $key
136
	 * @return Action
137
	 */
138 View Code Duplication
	public function getAction($key)
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...
139
	{
140
		if (!isset($this->actions[$key])) {
141
			throw new DataGridException(
142
				"There is no action at key [$key] defined for MultiAction."
143
			);
144
		}
145
146
		return $this->actions[$key];
147
	}
148
149
150
	/**
151
	 * Column can have variables that will be passed to custom template scope
152
	 * @return array
153
	 */
154
	public function getTemplateVariables()
155
	{
156
		return array_merge($this->template_variables, [
157
			'multi_action' => $this,
158
		]);
159
	}
160
161
162
	/**
163
	 * @param string $actionKey
164
	 * @param callable $rowCondition
165
	 * @return void
166
	 */
167
	public function setRowCondition($actionKey, callable $rowCondition)
168
	{
169
		$this->rowConditions[$actionKey] = $rowCondition;
170
	}
171
172
173
	/**
174
	 * @param  string $actionKey
175
	 * @param  Row    $row
176
	 * @return bool
177
	 */
178
	public function testRowCondition($actionKey, Row $row)
179
	{
180
		if (!isset($this->rowConditions[$actionKey])) {
181
			return true;
182
		}
183
184
		return (bool) call_user_func($this->rowConditions[$actionKey], $row->getItem());
185
	}
186
}
187