-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvents.php
More file actions
135 lines (119 loc) · 4.56 KB
/
Events.php
File metadata and controls
135 lines (119 loc) · 4.56 KB
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
<?php
/**
* Menu Manager
* @link https://github.com/cuzy-app/menu-manager
* @license https://github.com/cuzy-app/menu-manager/blob/master/docs/LICENCE.md
* @author [Marc FARRE](https://marc.fun) for [CUZY.APP](https://www.cuzy.app)
*/
namespace humhub\modules\menuManager;
use Exception;
use humhub\helpers\ControllerHelper;
use humhub\libs\WidgetCreateEvent;
use humhub\modules\classifiedSpace\Module;
use humhub\modules\enterpriseTheme\widgets\Chooser;
use humhub\modules\menuManager\widgets\SpaceChooser;
use humhub\modules\ui\menu\MenuLink;
use humhub\widgets\TopMenu;
use Yii;
use yii\base\Event;
use yii\base\WidgetEvent;
use yii\helpers\Url;
class Events
{
/**
* TopMenu init event callback
*
* @param Event $event
* @see TopMenu
*/
public static function onTopMenuInit($event)
{
/** @var TopMenu $menu */
$menu = $event->sender;
/** @var Module $module */
$module = Yii::$app->getModule('menu-manager');
$configuration = $module->getConfiguration();
$homeMenuEntryConfig = $configuration->getMenuEntryConfig('topMenuHome');
if ($homeMenuEntryConfig->display()) {
$menu->addEntry(new MenuLink([
'id' => $homeMenuEntryConfig->id,
'label' => Yii::t('MenuManagerModule.custom', $homeMenuEntryConfig->label) ?: Yii::t('yii', 'Home'),
'url' => Url::home(),
'icon' => $homeMenuEntryConfig->icon ?: 'home',
'sortOrder' => $homeMenuEntryConfig->sortOrder ?: 50,
'isActive' => Url::home() === Url::current() || ControllerHelper::isActivePath('homepage', 'index', 'index'),
'isVisible' => true,
]));
}
}
/**
* TopMenu init event callback
*
* @param Event $event
* @see TopMenu
*/
public static function onTopMenuBeforeRun($event)
{
/** @var TopMenu $menu */
$menu = $event->sender;
/** @var Module $module */
$module = Yii::$app->getModule('menu-manager');
$configuration = $module->getConfiguration();
foreach ($configuration->availableTopMenuAttributes as $attribute) {
if (
$attribute === 'topMenuHome' // See onTopMenuInit()
|| $attribute === 'topMenuSpaceChooser' // See onSpaceChooserBeforeRun(
) {
continue;
}
$menuEntryConfig = $configuration->getMenuEntryConfig($attribute);
if ($attribute === 'topMenuCalendar') { // TODO: add an ID to the Calendar module top menu entry
$entry = $menu->getEntryByUrl(\humhub\modules\calendar\helpers\Url::toGlobalCalendar());
} elseif ($attribute === 'topMenuJitsiMeet') { // TODO: add an ID to the Jitsi Meet module top menu entry
$entry = $menu->getEntryByUrl(['/jitsi-meet/room']);
} else {
/** @var MenuLink $entry */
$entry = $menu->getEntryById($menuEntryConfig->id);
}
if (!$entry) {
continue;
}
if (!$menuEntryConfig->display()) {
$menu->removeEntry($entry);
} else {
if ($menuEntryConfig->icon) {
try {
$entry->setIcon($menuEntryConfig->icon);
} catch (Exception) {
}
}
if ($menuEntryConfig->label) {
$entry->setLabel(Yii::t('MenuManagerModule.custom', $menuEntryConfig->label));
}
$sortOrder = (int)$menuEntryConfig->sortOrder;
if ($sortOrder && $sortOrder >= 1 && $sortOrder <= 10000) {
$entry->setSortOrder($menuEntryConfig->sortOrder);
}
}
}
}
public static function onSpaceChooserBeforeRun(WidgetEvent $event)
{
/** @var Module $module */
$module = Yii::$app->getModule('menu-manager');
$configuration = $module->getConfiguration();
$mySpaceMenuEntryConfig = $configuration->getMenuEntryConfig('topMenuSpaceChooser');
if (!$mySpaceMenuEntryConfig->display()) {
$event->isValid = false;
}
}
public static function onSpaceChooserCreate(WidgetCreateEvent $event)
{
// No change to Enterprise Theme Space Chooser
if ($event->config['class'] === Chooser::class) {
return;
}
// Use Menu Manager Space Chooser to change the name and the icon
$event->config['class'] = SpaceChooser::class;
}
}