forked from staxDB/humhub-modules-bookmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvents.php
122 lines (108 loc) · 3.77 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2015 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\bookmark;
use humhub\modules\bookmark\models\forms\ConfigForm;
use humhub\modules\bookmark\permissions\ViewBookmarkStream;
use Yii;
use yii\helpers\Url;
use yii\base\BaseObject;
use humhub\modules\bookmark\models\Bookmark;
/**
* Events provides callbacks to handle events.
*
* @author luke
*/
class Events extends BaseObject
{
/**
* On build of the TopMenu, check if module is enabled
* When enabled add a menu item
*
* @param type $event
*/
public static function onTopMenuInit($event)
{
// Do not show if not logged in as it is not possible to have bookmarks as a guest
if (Yii::$app->user->isGuest)
return;
// Is Module enabled on this workspace?
$event->sender->addItem([
'label' => Yii::t('BookmarkModule.base', 'Bookmarks'),
'id' => 'bookmark',
'icon' => '<i class="fa fa-bookmark"></i>',
'url' => Url::toRoute('/bookmark/index'),
'sortOrder' => 100,
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'bookmark'),
]);
}
public static function onProfileMenuInit($event)
{
$user = $event->sender->user;
if ($user->isModuleEnabled('bookmark')) {
if ($user->permissionManager->can(new ViewBookmarkStream())) {
$event->sender->addItem([
'label' => Yii::t('BookmarkModule.base', 'Bookmarks'),
'url' => $user->createUrl('/bookmark/profile/show'),
'icon' => '<i class="fa fa-bookmark"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'bookmark'),
]);
}
}
}
/**
* On User delete, also delete all bookmarks
*/
public static function onUserDelete($event)
{
foreach (Bookmark::findAll(['created_by' => $event->sender->id]) as $bookmark) {
$bookmark->delete();
}
return true;
}
/**
* On content delete, also delete all bookmarks
*/
public static function onActiveRecordDelete($event)
{
$record = $event->sender;
if ($record->hasAttribute('id')) {
foreach (Bookmark::findAll(['object_id' => $record->id, 'object_model' => $record->className()]) as $bookmark) {
$bookmark->delete();
}
}
}
/**
* Callback to validate module database records.
*/
public static function onIntegrityCheck($event)
{
$integrityController = $event->sender;
$integrityController->showTestHeadline("Bookmark (" . Bookmark::find()->count() . " entries)");
foreach (Bookmark::find()->all() as $bookmark) {
if ($bookmark->source === null) {
if ($integrityController->showFix("Deleting bookmark id " . $bookmark->id . " without existing target!")) {
$bookmark->delete();
}
}
// User doesn't exists
if ($bookmark->user === null) {
if ($integrityController->showFix("Deleting bookmark id " . $bookmark->id . " without existing user!")) {
$bookmark->delete();
}
}
}
}
/**
* On initalizing the wall entry controls also add the bookmark link widget
*
* @param type $event
*/
public static function onWallEntryLinksInit($event)
{
$event->sender->addWidget(widgets\BookmarkLink::class, ['object' => $event->sender->object], ['sortOrder' => ConfigForm::instantiate()->sortOrder]);
}
}