-
Notifications
You must be signed in to change notification settings - Fork 34
/
Events.php
331 lines (290 loc) · 12.8 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?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\content\components\ContentContainerActiveRecord;
use humhub\modules\rest\Module as RestModule;
use humhub\modules\space\models\Space;
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)
{
try {
/* @var $module Module */
$module = Yii::$app->getModule('tasks');
if (!$module->settings->get('showGlobalMenuItem', false) || Yii::$app->user->isGuest) {
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'),
]);
} catch (\Throwable $e) {
Yii::error($e);
}
}
/**
* @param $event \humhub\modules\calendar\interfaces\CalendarItemTypesEvent
* @return mixed
*/
public static function onGetCalendarItemTypes($event)
{
try {
/* @var ContentContainerActiveRecord $contentContainer */
$contentContainer = $event->contentContainer;
if (!$contentContainer || $contentContainer->moduleManager->isEnabled('tasks')) {
TaskCalendar::addItemTypes($event);
}
} catch (\Throwable $e) {
Yii::error($e);
}
}
/**
* @param $event \humhub\modules\calendar\interfaces\CalendarItemsEvent;
*/
public static function onFindCalendarItems($event)
{
try {
/* @var ContentContainerActiveRecord $contentContainer */
$contentContainer = $event->contentContainer;
if (!$contentContainer || $contentContainer->moduleManager->isEnabled('tasks')) {
TaskCalendar::addItems($event);
}
} catch (\Throwable $e) {
Yii::error($e);
}
}
public static function onDashboardSidebarInit($event)
{
try {
if (Yii::$app->user->isGuest) {
return;
}
$settings = SnippetModuleSettings::instantiate();
if ($settings->showMyTasksSnippet()) {
$event->sender->addWidget(MyTasks::class, ['limit' => $settings->myTasksSnippetMaxItems], ['sortOrder' => $settings->myTasksSnippetSortOrder]);
}
} catch (\Throwable $e) {
Yii::error($e);
}
}
public static function onSpaceSidebarInit($event)
{
try {
if (Yii::$app->user->isGuest) {
return;
}
/* @var $space Space */
$space = $event->sender->space;
if ($space->moduleManager->isEnabled('tasks') && $space->isMember()) {
$settings = SnippetModuleSettings::instantiate();
if ($settings->showMyTasksSnippetSpace()) {
$event->sender->addWidget(MyTasks::class, [
'contentContainer' => $space,
'limit' => $settings->myTasksSnippetMaxItems], ['sortOrder' => $settings->myTasksSnippetSortOrder]);
}
}
} catch (\Throwable $e) {
Yii::error($e);
}
}
public static function onSpaceMenuInit($event)
{
try {
/* @var $space Space */
$space = $event->sender->space;
if ($space->moduleManager->isEnabled('tasks') && $space->isMember()) {
$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'),
]);
}
} catch (\Throwable $e) {
Yii::error($e);
}
}
public static function onProfileMenuInit($event)
{
/* @var $user User */
try {
$user = $event->sender->user;
if ($user->moduleManager->isEnabled('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'),
]);
}
} catch (\Throwable $e) {
Yii::error($e);
}
}
/**
* 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()->each() as $taskItem) {
if ($taskItem->task === null) {
if ($integrityController->showFix("Deleting task item id " . $taskItem->id . " without existing task!")) {
$taskItem->hardDelete();
}
}
}
// check for task responsible users without task or existing user
foreach (TaskUser::find()->each() 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()->each() 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()->each() 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()->each() 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()->each() 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)
{
try {
$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();
}
}
}
} catch (\Throwable $e) {
Yii::error($e);
}
}
public static function onCronRun($event)
{
Yii::$app->queue->push(new SendReminder());
}
public static function onRestApiAddRules()
{
/* @var RestModule $restModule */
$restModule = Yii::$app->getModule('rest');
$restModule->addRules([
['pattern' => 'tasks/', 'route' => 'tasks/rest/tasks/find', 'verb' => ['GET', 'HEAD']],
['pattern' => 'tasks/container/<containerId:\d+>', 'route' => 'tasks/rest/tasks/find-by-container', 'verb' => ['GET', 'HEAD']],
['pattern' => 'tasks/container/<containerId:\d+>', 'route' => 'tasks/rest/tasks/delete-by-container', 'verb' => 'DELETE'],
//Task CRUD
['pattern' => 'tasks/container/<containerId:\d+>', 'route' => 'tasks/rest/tasks/create', 'verb' => 'POST'],
['pattern' => 'tasks/task/<id:\d+>', 'route' => 'tasks/rest/tasks/view', 'verb' => ['GET', 'HEAD']],
['pattern' => 'tasks/task/<id:\d+>', 'route' => 'tasks/rest/tasks/update', 'verb' => 'PUT'],
['pattern' => 'tasks/task/<id:\d+>', 'route' => 'tasks/rest/tasks/delete', 'verb' => 'DELETE'],
//Task management
['pattern' => 'tasks/task/<id:\d+>/processed', 'route' => 'tasks/rest/tasks/processed', 'verb' => 'PATCH'],
['pattern' => 'tasks/task/<id:\d+>/revert', 'route' => 'tasks/rest/tasks/revert', 'verb' => 'PATCH'],
['pattern' => 'tasks/task/<id:\d+>/upload-files', 'route' => 'tasks/rest/tasks/attach-files', 'verb' => 'POST'],
['pattern' => 'tasks/task/<id:\d+>/remove-file/<fileId:\d+>', 'route' => 'tasks/rest/tasks/remove-file', 'verb' => 'DELETE'],
//Task List CRUD
['pattern' => 'tasks/lists/container/<containerId:\d+>', 'route' => 'tasks/rest/task-list/index', 'verb' => 'GET'],
['pattern' => 'tasks/lists/container/<containerId:\d+>', 'route' => 'tasks/rest/task-list/create', 'verb' => 'POST'],
['pattern' => 'tasks/list/<id:\d+>', 'route' => 'tasks/rest/task-list/view', 'verb' => ['GET', 'HEAD']],
['pattern' => 'tasks/list/<id:\d+>', 'route' => 'tasks/rest/task-list/update', 'verb' => 'PUT'],
['pattern' => 'tasks/list/<id:\d+>', 'route' => 'tasks/rest/task-list/delete', 'verb' => 'DELETE'],
], 'tasks');
}
}