forked from humhub/mail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Events.php
104 lines (86 loc) · 2.89 KB
/
Events.php
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
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2015 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\mail;
use humhub\modules\mail\permissions\StartConversation;
use humhub\modules\user\models\User;
use Yii;
use yii\helpers\Url;
use humhub\modules\mail\models\MessageEntry;
use humhub\modules\mail\models\UserMessage;
use humhub\modules\mail\widgets\NewMessageButton;
use humhub\modules\mail\widgets\Notifications;
use humhub\modules\mail\permissions\SendMail;
use humhub\modules\mail\models\Config;
/**
* Description of Events
*
* @author luke
*/
class Events
{
/**
* On User delete, also delete all comments
*
* @param type $event
*/
public static function onUserDelete($event)
{
foreach (MessageEntry::findAll(array('user_id' => $event->sender->id)) as $messageEntry) {
$messageEntry->delete();
}
foreach (UserMessage::findAll(array('user_id' => $event->sender->id)) as $userMessage) {
$userMessage->message->leave($event->sender->id);
}
return true;
}
/**
* On build of the TopMenu, check if module is enabled
* When enabled add a menu item
*
* @param type $event
*/
public static function onTopMenuInit($event)
{
if (Yii::$app->user->isGuest) {
return;
}
$showInTopNav = false;
// Workaround for module update problem
if (method_exists(Config::getModule(), 'showInTopNav')) {
$showInTopNav = Config::getModule()->showInTopNav();
}
if(!Config::getModule()->showInTopNav()){
$event->sender->addItem([
'label' => Yii::t('MailModule.base', 'Messages'),
'url' => Url::to(['/mail/mail/index']),
'icon' => '<i class="fa fa-envelope"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'mail'),
'sortOrder' => 300,
]);
}
}
public static function onNotificationAddonInit($event)
{
if (Yii::$app->user->isGuest) {
return;
}
$event->sender->addWidget(Notifications::className(), [], ['sortOrder' => 90]);
}
public static function onProfileHeaderControlsInit($event)
{
/** @var User $profileContainer */
$profileContainer = $event->sender->user;
if($profileContainer->isCurrentUser() || !Yii::$app->user->can(StartConversation::class)) {
return;
}
// Is the current logged user allowed to send mails to profile user?
if(!Yii::$app->user->isAdmin() && !$profileContainer->can(SendMail::class)) {
return;
}
$event->sender->addWidget(NewMessageButton::class, ['guid' => $event->sender->user->guid, 'size' => null, 'icon' => null], ['sortOrder' => 90]);
}
}