1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
<?php
/**
* This class represents the action menu used to group actions.
*
* @author Jan-Hendrik Willms <tleilax+studip@gmail.com>
* @license GPL2 or any later version
* @since Stud.IP 3.5
*/
class ActionMenu
{
const TEMPLATE_FILE_SINGLE = 'shared/action-menu-single.php';
const TEMPLATE_FILE_MULTIPLE = 'shared/action-menu.php';
const RENDERING_MODE_ICONS = 'icons';
const RENDERING_MODE_MENU = 'menu';
/**
* Returns an instance.
*
* @return ActionMenu
*/
public static function get()
{
return new self();
}
private $actions = [];
private $attributes = [];
private $condition_all = null;
private $condition = true;
/**
* @var string $context The context for the action menu.
*/
protected $context = '';
/**
* @var string|null $rendering_mode The forced rendering mode
*/
protected $rendering_mode = null;
/**
* Private constructur.
*
* @see ActionMenu::get()
*/
private function __construct()
{
$this->addCSSClass('action-menu');
}
/**
* Set condition for the next added item. If condition is false,
* the item will not be added.
*
* @param bool $state State of the condition
* @return ActionMenu instance to allow chaining
*/
public function condition($state)
{
$this->condition = (bool)$state;
return $this;
}
/**
* Set condition for all the next added items. If condition is false,
* no items will be added.
*
* @param bool $state State of the condition
* @return ActionMenu instance to allow chaining
*/
public function conditionAll($state)
{
$this->condition_all = $state;
return $this;
}
/**
* Checks the condition. Takes global and local (conditionAll() &
* condition()) conditions into account.
*
* @return bool indicating whether the condition is met or not
*/
protected function checkCondition()
{
$result = $this->condition;
if ($this->condition_all !== null) {
$result = $result && $this->condition_all;
}
$this->condition = true;
return $result;
}
/**
* Returns the number of action menu items (not counting separators).
*
* @return int count
*/
protected function countActions(): int
{
return count(array_filter($this->actions, function($action) {
return $action['type'] !== 'separator';
}));
}
/**
* Adds a link to the list of actions.
*
* @param String|StudipLink $url Link target, eithe as string or
* Stud.IP link. In the latter case,
* all other parameters are ignored.
* @param String|array $label Textual representation of the link
* @param mixed $icon Optional icon (as Icon object)
* @param array $attributes Optional attributes to add to the <a> tag
* @param mixed $index Optional index to access this link (remove for example) afterwards
* @param mixed $before Optional index to insert this link before the link with given index.
* @return ActionMenu instance to allow chaining
*/
public function addLink($url, $label = "", Icon $icon = null, array $attributes = [], $index = null, $before = null)
{
if ($this->checkCondition()) {
if ($url instanceof StudipLink) {
$action = [
'type' => 'link',
'link' => $url->link,
'icon' => $url->icon,
'label' => $url->title,
'attributes' => $url->attributes,
];
} else {
$action = [
'type' => 'link',
'link' => $url,
'icon' => $icon,
'label' => $label,
'attributes' => $attributes,
];
}
$index = $index ?: md5($action['link'] . json_encode($action['attributes']));
$action['index'] = $index;
//now insert it possibly at the desired position:
$before_key = null;
if ($before) {
$before_key = $this->getKeyForIndex($before);
if ($before_key !== null) {
array_splice($this->actions, $before_key, 0, $action);
return $this;
}
}
$current_key = $this->getKeyForIndex($index);
if ($current_key !== null) {
$this->actions[$current_key] = $action;
return $this;
}
$this->removeLink($index);
$this->actions[] = $action;
}
return $this;
}
/**
* Tries to remove the link with the given index and returns true on success (else false)
* @param $index : the index of the link. If the link had no special
* index it's md5($url.json_encode($ttributes)).
* @return bool : true if link was removed, false if index didn't exist
*/
public function removeLink($index)
{
$key = $this->getKeyForIndex($index);
if ($key !== null) {
unset($this->actions[$key]);
return true;
}
return false;
}
/**
* Adds a button to the list of actions.
*
* @param String $name Button name
* @param String $label Textual representation of the name
* @param mixed $icon Optional icon (as Icon object)
* @param array $attributes Optional attributes to add to the <a> tag
* @return ActionMenu instance to allow chaining
*/
public function addButton($name, $label, Icon $icon = null, array $attributes = [])
{
if ($this->checkCondition()) {
$this->actions[] = [
'type' => 'button',
'name' => $name,
'icon' => $icon,
'label' => $label,
'attributes' => $attributes,
];
}
return $this;
}
/**
* Adds a MultiPersonSearch object to the list of actions.
*
* @param MultiPersonSearch $mp MultiPersonSearch object
* @return ActionMenu instance to allow chaining
*/
public function addMultiPersonSearch(MultiPersonSearch $mp)
{
if ($this->checkCondition()) {
$this->actions[] = [
'type' => 'multi-person-search',
'object' => $mp,
];
}
return $this;
}
/**
* Adds a separator line to the list of actions.
*
* @return ActionMenu instance to allow chaining
*/
public function addSeparator(): ActionMenu
{
if ($this->checkCondition()) {
$this->actions[] = [
'type' => 'separator',
];
}
return $this;
}
/**
* Adds a css classs to the root element in html.
*
* @param string $class Name of the css class
* @return ActionMenu instance to allow chaining
*/
public function addCSSClass($class)
{
$this->addAttribute('class', $class, true);
return $this;
}
/**
* Adds an attribute to the root element in html.
*
* @param string $key Name of the attribute
* @param string $value Value of the attribute
* @param boolean $append Whether a current value should be append or not.
*/
public function addAttribute($key, $value, $append = false)
{
if (isset($this->attributes[$key]) && $append) {
$this->attributes[$key] .= " {$value}";
} else {
$this->attributes[$key] = $value;
}
}
/**
* Renders the action menu. If no item was added, an empty string will
* be returned. If a single item was added, the item itself will be
* displayed. Otherwise the whole menu will be rendered.
*
* @return String containing the html representation of the action menu
*/
public function render()
{
if ($this->countActions() === 0) {
return '';
}
$template_file = $this->getRenderingMode() === self::RENDERING_MODE_ICONS
? self::TEMPLATE_FILE_SINGLE
: self::TEMPLATE_FILE_MULTIPLE;
;
$template = $GLOBALS['template_factory']->open($template_file);
$template->actions = array_map(function ($action) {
$disabled = isset($action['attributes']['disabled'])
&& $action['attributes']['disabled'] !== false;
if ($disabled && $action['icon']) {
$action['icon'] = $action['icon']->copyWithRole(Icon::ROLE_INACTIVE);
}
return $action;
}, $this->actions);
$template->action_menu_title = $this->generateTitle();
$template->attributes = $this->attributes;
return $template->render();
}
/**
* Magic method to render the menu as a string.
*
* @return String containing the html representation of the action menu
* @see ActionMenu::render()
*/
public function __toString()
{
return $this->render();
}
protected function getKeyForIndex($index)
{
foreach ($this->actions as $key => $value) {
if ($value['index'] === $index) {
return $key;
}
}
return null;
}
/**
* Sets the context for the menu.
*
* @param string $context The context to be set.
*
* @return ActionMenu The action menu instance (to allow chaining).
*/
public function setContext(string $context)
{
$this->context = $context;
return $this;
}
/**
* Forces an explicit rendering mode.
*
* @param string|null $mode The desired rendering mode or null for automatic detection
* @return ActionMenu The action menu instance to allow chaining
* @throws Exception
*/
public function setRenderingMode(?string $mode): ActionMenu
{
if (
$mode !== null
&& $mode !== self::RENDERING_MODE_ICONS
&& $mode !== self::RENDERING_MODE_MENU
) {
throw new Exception("Invalid rendering mode '{$mode}'");
}
$this->rendering_mode = $mode;
return $this;
}
/**
* Returns the rendering mode for this action menu. This is set by either
* calling setRenderingMode or automatically determined by the configured
* threshold.
*
* @return string
*/
public function getRenderingMode(): string
{
$rendering_mode = $this->rendering_mode;
if ($rendering_mode === null) {
$rendering_mode = $this->countActions() <= Config::get()->ACTION_MENU_THRESHOLD
? self::RENDERING_MODE_ICONS
: self::RENDERING_MODE_MENU;
}
return $rendering_mode;
}
/**
* Generates the title of the action menu, including its context, if the context has been set.
*
* @return string The title of the action menu.
*/
public function generateTitle() : string
{
if ($this->context) {
return sprintf(
_('Aktionsmenü für %s'),
$this->context
);
} else {
return _('Aktionsmenü');
}
}
}
|