-
Notifications
You must be signed in to change notification settings - Fork 3
/
Events.php
executable file
·218 lines (187 loc) · 7.6 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
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
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*
*/
namespace humhub\modules\task;
//use humhub\modules\task\integration\calendar\TaskCalendar;
use humhub\modules\notification\models\Notification;
use humhub\modules\task\jobs\SendReminder;
use humhub\modules\task\models\SnippetModuleSettings;
use humhub\modules\task\models\Task;
use humhub\modules\task\models\TaskItem;
use humhub\modules\task\models\TaskReminder;
use humhub\modules\task\integration\calendar\TaskCalendar;
use humhub\modules\task\models\TaskUser;
use humhub\modules\task\widgets\MyTasks;
use Yii;
use yii\base\Object;
/* @var $user \humhub\modules\user\models\User */
/**
* Created by PhpStorm.
* User: davidborn
* Date: 14.09.2017
* Time: 12:12
*/
class Events extends Object
{
/**
* @param $event \humhub\modules\calendar\interfaces\CalendarItemTypesEvent
* @return mixed
*/
public static function onGetCalendarItemTypes($event)
{
$contentContainer = $event->contentContainer;
if(!$contentContainer || $contentContainer->isModuleEnabled('task')) {
TaskCalendar::addItemTypes($event);
}
}
/**
* @param $event \humhub\modules\calendar\interfaces\CalendarItemsEvent;
*/
public static function onFindCalendarItems($event)
{
$contentContainer = $event->contentContainer;
if(!$contentContainer || $contentContainer->isModuleEnabled('task')) {
TaskCalendar::addItems($event);
}
}
public static function onDashboardSidebarInit($event)
{
if (Yii::$app->user->isGuest) {
return;
}
$settings = SnippetModuleSettings::instantiate();
if ($settings->showMyTasksSnippet()) {
$event->sender->addWidget(MyTasks::className(), ['limit' => $settings->myTasksSnippetMaxItems], ['sortOrder' => $settings->myTasksSnippetSortOrder]);
}
}
public static function onSpaceSidebarInit($event)
{
if (Yii::$app->user->isGuest) {
return;
}
$space = $event->sender->space;
$settings = SnippetModuleSettings::instantiate();
if ($space->isModuleEnabled('task')) {
if ($settings->showMyTasksSnippetSpace()) {
$event->sender->addWidget(MyTasks::className(), ['limit' => $settings->myTasksSnippetMaxItems], ['sortOrder' => $settings->myTasksSnippetSortOrder]);
}
}
}
public static function onSpaceMenuInit($event)
{
/* @var $space \humhub\modules\space\models\Space */
$space = $event->sender->space;
if ($space->isModuleEnabled('task') && $space->isMember()) {
$event->sender->addItem([
'label' => Yii::t('TaskModule.base', 'Tasks'),
'group' => 'modules',
'url' => $space->createUrl('//task/index'),
'icon' => '<i class="fa fa-tasks"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'task'),
]);
}
}
/**
* Callback to validate module database records.
*
* @param Event $event
* @throws \Exception
*/
public static function onIntegrityCheck($event)
{
$integrityController = $event->sender;
$integrityController->showTestHeadline("Tasks Module - Entries (" . Task::find()->count() . " entries)");
// check for taskItems without task
foreach (TaskItem::find()->all() as $taskItem) {
if ($taskItem->task === null) {
if ($integrityController->showFix("Deleting task item id " . $taskItem->id . " without existing task!")) {
$taskItem->delete();
}
}
}
// check for task responsible users without task or existing user
foreach (TaskUser::find()->all() as $taskUser) {
if ($taskUser->task === null) {
if ($integrityController->showFix("Deleting task user user id " . $taskUser->id . " without existing task!")) {
$taskUser->delete();
}
}
if ($taskUser->user === null) {
if ($integrityController->showFix("Deleting task user user id " . $taskUser->id . " without existing user!")) {
$taskUser->delete();
}
}
}
// // check for task responsible users without task or existing user
// foreach (TaskResponsible::find()->all() as $taskResponsible) {
// if ($taskResponsible->task === null) {
// if ($integrityController->showFix("Deleting task responsible user id " . $taskResponsible->id . " without existing task!")) {
// $taskResponsible->delete();
// }
// }
// if ($taskResponsible->user === null) {
// if ($integrityController->showFix("Deleting task responsible user id " . $taskResponsible->id . " without existing user!")) {
// $taskResponsible->delete();
// }
// }
// }
// // check for task assigned users without task or existing user
// foreach (TaskAssigned::find()->all() as $taskAssigned) {
// if ($taskAssigned->task === null) {
// if ($integrityController->showFix("Deleting task assigned user id " . $taskAssigned->id . " without existing task!")) {
// $taskAssigned->delete();
// }
// }
// if ($taskAssigned->user === null) {
// if ($integrityController->showFix("Deleting task assigned user id " . $taskAssigned->id . " without existing user!")) {
// $taskAssigned->delete();
// }
// }
// }
// check for task reminders without task
foreach (TaskReminder::find()->all() as $taskReminder) {
if ($taskReminder->task === null) {
if ($integrityController->showFix("Deleting task reminder id " . $taskReminder->id . " without existing task!")) {
$taskReminder->delete();
}
}
}
}
/**
* Handle what happens, when user left space.
*
* @param $event
* @throws \Exception
* @throws \yii\base\Exception
* @throws \yii\db\StaleObjectException
*/
public static function onMemberRemoved ($event)
{
$tasks = Task::find()->contentContainer($event->space)->all();
if (!empty($tasks)) {
foreach ($tasks as $task) {
$taskUser = $task->getTaskAssigned()->where(['task_user.user_id' => $event->user->id])->all();
foreach ($taskUser as $user) {
$user->delete();
}
// remove notifications
// $event->sender->className()
// $event->sender->getPrimaryKey()
$notifications = Notification::find()->where(['source_class' => Task::className(), 'source_pk' => $task->id, 'space_id' => $event->space->id])->all();
foreach ($notifications as $notification) {
$notification->delete();
}
}
}
}
public static function onCronRun($event)
{
if (Yii::$app->controller->action->id == 'hourly') {
Yii::$app->queue->push( new SendReminder());
}
}
}