Completed
Push — master ( 257df0...011c2c )
by Pavel
07:32
created

MultiAction::addAction()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 11
nc 5
nop 4
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
20
	use Traits\TButtonTryAddIcon;
21
	use Traits\TButtonIcon;
22
	use Traits\TButtonClass;
23
	use Traits\TButtonTitle;
24
	use Traits\TButtonText;
25
	use Traits\TButtonCaret;
26
	use Traits\TLink;
27
28
	/**
29
	 * @var DataGrid
30
	 */
31
	protected $grid;
32
33
	/**
34
	 * @var string
35
	 */
36
	protected $name;
37
38
	/**
39
	 * @var array
40
	 */
41
	protected $actions = [];
42
43
	/**
44
	 * @var callable[]
45
	 */
46
	private $rowConditions = [];
47
48
49
	/**
50
	 * @param DataGrid $grid
51
	 */
52
	public function __construct(DataGrid $grid, $name)
53
	{
54
		$this->grid = $grid;
55
		$this->name = $name;
56
57
		$this->setTemplate(__DIR__ . '/../templates/column_multi_action.latte');
58
	}
59
60
61
	/**
62
	 * @return Html
63
	 */
64
	public function renderButton()
65
	{
66
		$button = Html::el('button')
67
			->type('button')
68
			->data('toggle', 'dropdown');
69
70
		$this->tryAddIcon($button, $this->getIcon(), $this->getName());
71
72
		if (!empty($this->attributes)) {
0 ignored issues
show
Documentation introduced by
The property attributes does not exist on object<Ublaboo\DataGrid\Column\MultiAction>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
73
			$button->addAttributes($this->attributes);
0 ignored issues
show
Documentation introduced by
The property attributes does not exist on object<Ublaboo\DataGrid\Column\MultiAction>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
74
		}
75
76
		$button->addText($this->grid->getTranslator()->translate($this->name));
77
78
		if ($this->hasCaret()) {
79
			$button->addHtml('&nbsp;');
80
			$button->addHtml('<i class="caret"></i>');
81
		}
82
83
		if ($this->getTitle()) {
84
			$button->title($this->grid->getTranslator()->translate($this->getTitle()));
85
		}
86
87
		if ($this->getClass()) {
88
			$button->class($this->getClass() . ' dropdown-toggle');
89
		}
90
91
		return $button;
92
	}
93
94
95
	/**
96
	 * @param string     $key
97
	 * @param string     $name
98
	 * @param string     $href
99
	 * @param array|null $params
100
	 * @return static
101
	 */
102
	public function addAction($key, $name, $href = NULL, array $params = NULL)
103
	{
104
		if (isset($this->actions[$key])) {
105
			throw new DataGridException(
106
				"There is already action at key [$key] defined for MultiAction."
107
			);
108
		}
109
110
		$href = $href ?: $key;
111
112
		if (NULL === $params) {
113
			$params = [$this->grid->getPrimaryKey()];
114
		}
115
116
		$action = new Action($this->grid, $href, $name, $params);
117
118
		$action->setClass('');
119
120
		$this->actions[$key] = $action;
121
122
		return $this;
123
	}
124
125
126
	/**
127
	 * @return Action[]
128
	 */
129
	public function getActions()
130
	{
131
		return $this->actions;
132
	}
133
134
135
	/**
136
	 * @param  string $key
137
	 * @return Action
138
	 */
139
	public function getAction($key)
140
	{
141
		if (!isset($this->actions[$key])) {
142
			throw new DataGridException(
143
				"There is no action at key [$key] defined for MultiAction."
144
			);
145
		}
146
147
		return $this->actions[$key];
148
	}
149
150
151
	/**
152
	 * Column can have variables that will be passed to custom template scope
153
	 * @return array
154
	 */
155
	public function getTemplateVariables()
156
	{
157
		return array_merge($this->template_variables, [
158
			'multi_action' => $this
159
		]);
160
	}
161
162
163
	/**
164
	 * @param string $actionKey
165
	 * @param callable $rowCondition
166
	 * @return void
167
	 */
168
	public function setRowCondition($actionKey, callable $rowCondition)
169
	{
170
		$this->rowConditions[$actionKey] = $rowCondition;
171
	}
172
173
174
	/**
175
	 * @param  string $actionKey
176
	 * @param  Row    $row
177
	 * @return bool
178
	 */
179
	public function testRowCondition($actionKey, Row $row)
180
	{
181
		if (!isset($this->rowConditions[$actionKey])) {
182
			return true;
183
		}
184
185
		return (bool) call_user_func($this->rowConditions[$actionKey], $row->getItem());
186
	}
187
}
188