-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathToDoLists.php
181 lines (152 loc) · 4.94 KB
/
ToDoLists.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
<?php
declare(strict_types=1);
use Slim\App;
use WebGarden\Termite\TermitePlugin;
use WebGarden\ToDoLists\Database\TasksRepository;
use WebGarden\ToDoLists\Http\Controller;
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
} else {
require_once __DIR__ . '/../../vendor/autoload.php';
}
class ToDoListsPlugin extends TermitePlugin
{
const VERSION = '3.0.2';
/**
* @var \WebGarden\ToDoLists\Http\Controller
*/
protected $controller;
/**
* @var \WebGarden\ToDoLists\Database\TasksRepository
*/
protected $tasks;
public function register()
{
parent::register();
$this->page = 'config';
$this->version = self::VERSION;
$this->author = 'Andrzej Kupczyk';
$this->contact = '[email protected]';
$this->url = 'https://github.com/andrzejkupczyk/mantis-todolists';
}
public function init()
{
$this->controller = new Controller();
$this->tasks = new TasksRepository();
}
public function config(): array
{
return [
'manage_threshold' => DEVELOPER,
'view_threshold' => REPORTER,
];
}
public function events(): array
{
return [
'EVENT_TODOLISTS_REQUEST_HANDLED' => EVENT_TYPE_EXECUTE,
'EVENT_TODOLISTS_TASK_CREATED' => EVENT_TYPE_EXECUTE,
'EVENT_TODOLISTS_TASK_UPDATED' => EVENT_TYPE_EXECUTE,
];
}
public function hooks(): array
{
$events = [
'EVENT_BUG_DELETED' => 'deleteTasks',
'EVENT_REST_API_ROUTES' => 'routes',
'EVENT_TODOLISTS_REQUEST_HANDLED' => 'displayTasks',
'EVENT_TODOLISTS_TASK_CREATED' => 'addLogEntry',
'EVENT_TODOLISTS_TASK_UPDATED' => 'addLogEntry',
];
if (is_page_name('view.php') || is_page_name('bug_reminder')) {
$events += [
'EVENT_LAYOUT_PAGE_FOOTER' => 'scripts',
'EVENT_LAYOUT_RESOURCES' => 'styles',
'EVENT_VIEW_BUG_DETAILS' => 'displayTasks',
];
}
return $events;
}
/**
* @see https://www.mantisbt.org/wiki/doku.php/mantisbt:plugins_overview#schema_management
* @see https://adodb.org/dokuwiki/doku.php?id=v5:dictionary:dictionary_index
*/
public function schema(): array
{
$tableName = plugin_table(TasksRepository::TABLE_NAME);
return [
['CreateTableSQL', [
$tableName,
'id I UNSIGNED PRIMARY NOTNULL AUTOINCREMENT,
bug_id I UNSIGNED NOTNULL DEFAULT \'0\',
description C(120) NOTNULL DEFAULT \'\',
finished I2 DEFAULT \'0\'',
['mysql' => 'ENGINE=MyISAM DEFAULT CHARSET=utf8', 'pgsql' => 'WITHOUT OIDS'],
]],
['CreateIndexSQL', [
'idx_tasks_bug_id',
$tableName,
'bug_id',
]],
];
}
/**
* @return void
*/
public function deleteTasks(string $event, int $bugId)
{
$this->tasks->deleteAssociatedToBug($bugId);
}
/**
* @return void
*/
public function displayTasks(string $event, int $bugId)
{
$tasks = $this->tasks->findByBug($bugId);
$canManage = $this->canManage();
if ($event === 'EVENT_VIEW_BUG_DETAILS') {
include_once 'pages/partials/todolist.php';
} else {
header("HX-Trigger: $event");
include_once 'pages/partials/list_items.php';
}
}
/**
* @return void
*/
public function addLogEntry(string $event, array $data)
{
$fieldName = strtolower(str_replace('EVENT_TODOLISTS_', '', $event));
history_log_event_direct($data['bug_id'], plugin_lang_get_defaulted($fieldName), '', $data['description']);
}
public function styles(): string
{
return '<link rel="stylesheet" type="text/css" href="' . plugin_file('todolists.css') . '" />';
}
public function scripts(): string
{
return '<script type="text/javascript" src="' . plugin_file('htmx.min.js') . '"></script>' .
'<script type="text/javascript" src="' . plugin_file('todolists.min.js') . '"></script>';
}
/**
* @param array{app: \Slim\App} $payload
*
* @return void
*/
public function routes(string $event, array $payload)
{
if (!$this->canManage()) {
return;
}
$plugin = $this;
$payload['app']->group(plugin_route_group(), function (App $app) use ($plugin) {
$app->post('/tasks', [$plugin->controller, 'create']);
$app->put('/tasks', [$plugin->controller, 'update']);
$app->delete('/tasks', [$plugin->controller, 'delete']);
});
}
private function canManage(): bool
{
return access_has_project_level((int) plugin_config_get('manage_threshold'));
}
}