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->getName()); |
|
|
|
|
65
|
|
|
|
66
|
|
|
if (!empty($this->attributes)) { |
67
|
|
|
$button->addAttributes($this->attributes); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$button->addText($this->grid->getTranslator()->translate($this->name)); |
71
|
|
|
|
72
|
|
|
if ($this->hasCaret()) { |
73
|
|
|
$button->addHtml(' '); |
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
|
|
|
public function getAction($key) |
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
|
|
|
|