Completed
Push — master ( 392d04...e48b3d )
by Pavel
02:16
created

MultiAction::getActions()   A

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\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
		$this->actions[$key] = $action;
113
114
		return $this;
115
	}
116
117
118
	/**
119
	 * @return Action[]
120
	 */
121
	public function getActions()
122
	{
123
		return $this->actions;
124
	}
125
126
127
	/**
128
	 * @param  string $key
129
	 * @return Action
130
	 */
131 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...
132
	{
133
		if (!isset($this->actions[$key])) {
134
			throw new DataGridException(
135
				"There is no action at key [$key] defined for MultiAction."
136
			);
137
		}
138
139
		return $this->actions[$key];
140
	}
141
142
143
	/**
144
	 * Column can have variables that will be passed to custom template scope
145
	 * @return array
146
	 */
147
	public function getTemplateVariables()
148
	{
149
		return array_merge($this->template_variables, [
150
			'multi_action' => $this
151
		]);
152
	}
153
154
}
155