forked from humhub/tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Events.php
265 lines (227 loc) · 9.13 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
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
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2018 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*
*/
namespace humhub\modules\tasks;
use humhub\modules\infoscreen\helpers\Url;
use humhub\modules\tasks\helpers\TaskListUrl;
use humhub\modules\tasks\helpers\TaskUrl;
use humhub\modules\user\models\User;
use Yii;
use humhub\modules\notification\models\Notification;
use humhub\modules\tasks\jobs\SendReminder;
use humhub\modules\tasks\models\SnippetModuleSettings;
use humhub\modules\tasks\models\Task;
use humhub\modules\tasks\models\checklist\TaskItem;
use humhub\modules\tasks\models\scheduling\TaskReminder;
use humhub\modules\tasks\integration\calendar\TaskCalendar;
use humhub\modules\tasks\widgets\MyTasks;
use humhub\modules\tasks\models\user\TaskUser;
use yii\db\Expression;
/* @var $user \humhub\modules\user\models\User */
/**
* Created by PhpStorm.
* User: davidborn
* Date: 14.09.2017
* Time: 12:12
*/
class Events
{
public static function onTopMenuInit($event)
{
/* @var $module Module */
$module = Yii::$app->getModule('tasks');
if(!(int) $module->settings->get('showGlobalMenuItem', 1)) {
return;
}
// Is Module enabled on this workspace?
$event->sender->addItem([
'label' => Yii::t('TasksModule.base', 'Tasks'),
'id' => 'tasks-global',
'icon' => '<i class="fa fa-tasks"></i>',
'url' => TaskUrl::globalView(),
'sortOrder' => $module->settings->get('menuSortOrder', 500),
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'tasks' && Yii::$app->controller->id == 'global'),
]);
}
/**
* @param $event \humhub\modules\calendar\interfaces\CalendarItemTypesEvent
* @return mixed
*/
public static function onGetCalendarItemTypes($event)
{
$contentContainer = $event->contentContainer;
if(!$contentContainer || $contentContainer->isModuleEnabled('tasks')) {
TaskCalendar::addItemTypes($event);
}
}
/**
* @param $event \humhub\modules\calendar\interfaces\CalendarItemsEvent;
*/
public static function onFindCalendarItems($event)
{
$contentContainer = $event->contentContainer;
if(!$contentContainer || $contentContainer->isModuleEnabled('tasks')) {
TaskCalendar::addItems($event);
}
}
public static function onDashboardSidebarInit($event)
{
if (Yii::$app->user->isGuest) {
return;
}
$settings = SnippetModuleSettings::instantiate();
if ($settings->showMyTasksSnippet()) {
$event->sender->addWidget(MyTasks::class, ['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('tasks')) {
if ($settings->showMyTasksSnippetSpace()) {
$event->sender->addWidget(MyTasks::class, [
'contentContainer' => $space,
'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('tasks')) {
$event->sender->addItem([
'label' => Yii::t('TasksModule.base', 'Tasks'),
'group' => 'modules',
'url' => TaskListUrl::taskListRoot($space),
'icon' => '<i class="fa fa-tasks"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'tasks'),
]);
}
}
public static function onProfileMenuInit($event)
{
/* @var $user User */
$user = $event->sender->user;
if ($user->isModuleEnabled('tasks')) {
$event->sender->addItem([
'label' => Yii::t('TasksModule.base', 'Tasks'),
'url' => TaskListUrl::taskListRoot($user),
'icon' => '<i class="fa fa-tasks"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'tasks'),
]);
}
}
/**
* Callback to validate module database records.
*
* @param Event $event
* @throws \Exception
* @throws \Throwable
*/
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();
}
}
}
$integrityController->showTestHeadline("Tasks Module (" . Task::find()->count() . " entries)");
foreach (Task::find()->all() as $task) {
if ($task->task_list_id != null && !$task->list) {
if ($integrityController->showFix("Reset task list for task" . $task->id . " with invalid task_list_setting!")) {
$task->updateAttributes(['task_list_id' => new Expression('NULL')]);
}
}
}
// // 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
* @throws \Throwable
*/
public static function onMemberRemoved ($event)
{
$tasks = Task::find()->contentContainer($event->space)->all();
if (!empty($tasks)) {
foreach ($tasks as $task) {
$taskUser = $task->getAssignedTaskUsers()->where(['task_user.user_id' => $event->user->id])->all();
foreach ($taskUser as $user) {
$user->delete();
}
$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)
{
Yii::$app->queue->push(new SendReminder());
}
}