Repository navigation
Expand file tree
/
Copy pathPlugin.php
More file actions
167 lines (141 loc) · 6.92 KB
/
Copy pathPlugin.php
File metadata and controls
167 lines (141 loc) · 6.92 KB
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
<?php
namespace Kanboard\Plugin\NotifyMe;
use Kanboard\Core\Plugin\Base;
use Kanboard\Core\Security\AuthenticationManager;
use Kanboard\Core\Security\Role;
use Kanboard\Core\Translator;
use Kanboard\Model\CommentModel;
use Kanboard\Model\ProjectFileModel;
use Kanboard\Model\SubtaskModel;
use Kanboard\Model\TaskFileModel;
use Kanboard\Model\TaskLinkModel;
use Kanboard\Model\TaskModel;
class Plugin extends Base
{
public function onStartup()
{
Translator::load($this->languageModel->getCurrentLanguage(), __DIR__ . '/Locale');
}
/**
* Vacation mode pauses task emails to a user until the next login. Core's
* userNotificationModel is replaced so the pause covers Kanboard's own
* emails whichever plugin sends them, and the web notification still runs.
*/
private function initializeVacationMode()
{
$this->container['notifyMeVacation'] = function ($c) {
return new \Kanboard\Plugin\NotifyMe\Model\Vacation($c);
};
try {
$this->container['userNotificationModel'] = function ($c) {
return new \Kanboard\Plugin\NotifyMe\Model\UserNotificationModel($c);
};
} catch (\Exception $e) {
$this->logger->error('NotifyMe: userNotificationModel already in use, vacation mode covers NotifyMe emails only: '.$e->getMessage());
}
// Core has no hook after a settings save, and configModel is already frozen when plugins
// load. So when a request posts the limit, trim after the response is sent, to whatever
// value the database holds then (an unauthorized or failed save leaves it unchanged).
if (isset($_POST['notifyme_max_unread'])) {
register_shutdown_function(function () {
try {
// Only an admin can save this setting, so only an admin's post trims.
if (! $this->userSession->isLogged() || ! $this->userSession->isAdmin()) {
return;
}
$options = $this->configModel->getAll();
$this->userNotificationModel->trimAllUnread(isset($options['notifyme_max_unread']) ? $options['notifyme_max_unread'] : \Kanboard\Plugin\NotifyMe\Model\UserNotificationModel::UNREAD_DEFAULT);
} catch (\Throwable $e) {
$this->logger->error('NotifyMe: trim after settings save failed: '.$e->getMessage());
}
});
}
$this->applicationAccessMap->add('VacationController', '*', Role::APP_USER);
$this->projectAccessMap->add('ProjectVacationController', '*', Role::PROJECT_MANAGER);
$this->template->hook->attach('template:config:application', 'NotifyMe:config/unread_limit');
$this->template->hook->attach('template:user:sidebar:actions', 'NotifyMe:vacation/user_sidebar');
$this->template->hook->attach('template:project:sidebar', 'NotifyMe:vacation/project_sidebar');
// Fires on every login, including a remembered session and before any
// two-factor code is checked.
$this->dispatcher->addListener(AuthenticationManager::EVENT_SUCCESS, function () {
$userId = (int) $this->userSession->getId();
$setters = $userId > 0 ? $this->notifyMeVacation->getSetters($userId) : array();
if ($userId > 0 && $this->notifyMeVacation->clearAll($userId)) {
$this->flash->success(t('Vacation mode turned off.'));
$user = $this->userModel->getById($userId);
if (!empty($user)) {
$this->notifyMeVacation->sendNotice($user, array(), array(), false);
// The manager who set a project flag hears that it is off, too.
foreach ($setters as $projectId => $setterId) {
$project = $this->projectModel->getById($projectId);
$setter = $this->userModel->getById($setterId);
if (!empty($project) && !empty($setter)) {
$this->notifyMeVacation->sendNotice($user, $project, $setter, false, false);
}
}
}
}
});
}
public function initialize()
{
// Register as a shared service (one instance reused across all hooks).
$this->container['notifyMeAction'] = function ($c) {
return new \Kanboard\Plugin\NotifyMe\Action\NotifyMeAction($c);
};
$this->initializeVacationMode();
$action = $this->container['notifyMeAction'];
$events = array(
TaskModel::EVENT_CREATE => 'onTaskCreate',
TaskModel::EVENT_UPDATE => 'onTaskUpdate',
TaskModel::EVENT_CLOSE => 'onTaskClose',
TaskModel::EVENT_OPEN => 'onTaskOpen',
TaskModel::EVENT_MOVE_COLUMN => 'onTaskMoveColumn',
TaskModel::EVENT_MOVE_SWIMLANE => 'onTaskMoveSwimlane',
TaskModel::EVENT_ASSIGNEE_CHANGE => 'onTaskAssigneeChange',
SubtaskModel::EVENT_CREATE => 'onSubtaskCreate',
SubtaskModel::EVENT_UPDATE => 'onSubtaskUpdate',
SubtaskModel::EVENT_DELETE => 'onSubtaskDelete',
CommentModel::EVENT_CREATE => 'onCommentCreate',
CommentModel::EVENT_UPDATE => 'onCommentUpdate',
CommentModel::EVENT_DELETE => 'onCommentDelete',
TaskFileModel::EVENT_CREATE => 'onTaskFileCreate',
TaskFileModel::EVENT_DESTROY => 'onTaskFileDestroy',
TaskLinkModel::EVENT_CREATE_UPDATE => 'onTaskLinkCreateUpdate',
TaskLinkModel::EVENT_DELETE => 'onTaskLinkDelete',
ProjectFileModel::EVENT_CREATE => 'onProjectFileCreate',
ProjectFileModel::EVENT_DESTROY => 'onProjectFileDestroy',
AuthenticationManager::EVENT_FAILURE => 'onAuthFailure',
'wikipage.create' => 'onWikiPageCreate',
'wikipage.update' => 'onWikiPageUpdate',
'wikipage.delete' => 'onWikiPageDelete',
);
foreach ($events as $eventName => $method) {
$this->dispatcher->addListener($eventName, array($action, $method));
}
}
public function getPluginName()
{
return 'NotifyMe';
}
public function getPluginDescription()
{
return t('Email and Notifications menu notifications for your own actions, and failed login alerts to the account owner');
}
public function getPluginAuthor()
{
return 'Christefano Reyes';
}
public function getPluginVersion()
{
return '1.2.1';
}
public function getPluginHomepage()
{
return 'https://github.com/christefano/NotifyMe';
}
public function getCompatibleVersion()
{
return '>=1.2.20';
}
}