Completed
Push — master ( e48b3d...de0724 )
by Pavel
02:17
created

MultiAction::getAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
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\Traits;
15
16
class MultiAction extends Column
17
{
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
	/**
44
	 * @param DataGrid $grid
45
	 */
46
	public function __construct(DataGrid $grid, $name)
47
	{
48
		$this->grid = $grid;
49
		$this->name = $name;
50
51
		$this->setTemplate(__DIR__ . '/../templates/column_multi_action.latte');
52
	}
53
54
55
	/**
56
	 * @return Html
57
	 */
58
	public function renderButton()
59
	{
60
		$button = Html::el('button')
61
			->type('button')
62
			->data('toggle', 'dropdown');
63
64
		$this->tryAddIcon($button, $this->getIcon(), $this->getText());
65
66
		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...
67
			$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...
68
		}
69
70
		$button->addText($this->grid->getTranslator()->translate($this->name));
71
72
		if ($this->hasCaret()) {
73
			$button->addHtml('&nbsp;');
74
			$button->addHtml('<i class="caret"></i>');
75
		}
76
77
		if ($this->getTitle()) {
78
			$button->title($this->grid->getTranslator()->translate($this->getTitle()));
79
		}
80
81
		if ($this->getClass()) {
82
			$button->class($this->getClass() . ' dropdown-toggle');
83
		}
84
85
		return $button;
86
	}
87
88
89
	/**
90
	 * @param string     $key
91
	 * @param string     $name
92
	 * @param string     $href
93
	 * @param array|null $params
94
	 * @return static
95
	 */
96
	public function addAction($key, $name, $href = NULL, array $params = NULL)
97
	{
98
		if (isset($this->actions[$key])) {
99
			throw new DataGridException(
100
				"There is already action at key [$key] defined for MultiAction."
101
			);
102
		}
103
104
		$href = $href ?: $key;
105
106
		if (NULL === $params) {
107
			$params = [$this->grid->getPrimaryKey()];
108
		}
109
110
		$action = new Action($this->grid, $href, $name, $params);
111
112
		$action->setClass('');
113
114
		$this->actions[$key] = $action;
115
116
		return $this;
117
	}
118
119
120
	/**
121
	 * @return Action[]
122
	 */
123
	public function getActions()
124
	{
125
		return $this->actions;
126
	}
127
128
129
	/**
130
	 * @param  string $key
131
	 * @return Action
132
	 */
133 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...
134
	{
135
		if (!isset($this->actions[$key])) {
136
			throw new DataGridException(
137
				"There is no action at key [$key] defined for MultiAction."
138
			);
139
		}
140
141
		return $this->actions[$key];
142
	}
143
144
145
	/**
146
	 * Column can have variables that will be passed to custom template scope
147
	 * @return array
148
	 */
149
	public function getTemplateVariables()
150
	{
151
		return array_merge($this->template_variables, [
152
			'multi_action' => $this
153
		]);
154
	}
155
156
}
157