-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Awards.php
160 lines (139 loc) · 4.04 KB
/
Awards.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
<?php
declare(strict_types=1);
use Slim\App;
use Slim\Http\Request;
use WebGarden\Termite\Http\Middleware\DetermineCurrentPlugin;
use WebGarden\Termite\TermitePlugin;
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
} else {
require_once __DIR__ . '/../../vendor/autoload.php';
}
final class AwardsPlugin extends TermitePlugin
{
/**
* @var array
*/
protected $emojis = [
0x1F44D => '👍',
0x1F44E => '👎',
];
/**
* @var array|null
*/
protected $votes = null;
public function register()
{
parent::register();
$this->version = '1.0.3';
$this->author = 'Andrzej Kupczyk';
$this->contact = '[email protected]';
$this->url = 'https://github.com/andrzejkupczyk/mantisbt-awards';
}
public function hooks(): array
{
return [
'EVENT_BUGNOTE_DELETED' => 'handleBugnoteDeleted',
'EVENT_LAYOUT_RESOURCES' => 'handleLayoutResources',
'EVENT_REST_API_ROUTES' => 'handleRestApiRoutes',
'EVENT_VIEW_BUGNOTE' => 'handleViewBugnote',
'EVENT_VIEW_BUGNOTES_START' => 'handleViewBugnotesStart',
];
}
public function schema(): array
{
return [
['CreateTableSQL', [
plugin_table('bugnote_vote'),
'id I UNSIGNED PRIMARY NOTNULL AUTOINCREMENT,
bugnote_id I UNSIGNED NOTNULL,
voter_id I UNSIGNED NOTNULL,
emoji I UNSIGNED NOTNULL',
['mysql' => 'ENGINE=MyISAM DEFAULT CHARSET=utf8', 'pgsql' => 'WITHOUT OIDS'],
]],
['CreateIndexSQL', [
'idx_bugnote_vote_bugnote_id',
plugin_table('bugnote_vote'),
'bugnote_id',
]],
['CreateIndexSQL', [
'idx_bugnote_vote_voter_id_emoji',
plugin_table('bugnote_vote'),
'voter_id,emoji',
]],
];
}
public function config(): array
{
return [
'emojis' => $this->emojis,
];
}
/**
* @return void
*/
public function displayBugnoteAwards(int $bugnoteId)
{
$votes = is_array($this->votes)
? $this->votes
: fetch_bugnote_votes($bugnoteId);
include __DIR__ . '/components/list.php';
}
/**
* @return void
*/
public function handleBugnoteDeleted(string $event, int $bugId, int $bugnoteId)
{
reject_bugnote_votes($bugnoteId);
}
/**
* @return void
*/
public function handleLayoutResources()
{
if (is_page_name('view.php')) {
printf("\t<script src=\"%s\"></script>\n", plugin_file('htmx.min.js'));
}
}
/**
* @param array{app: \Slim\App} $payload
*
* @return void
*/
public function handleRestApiRoutes(string $event, array $payload)
{
$plugin = $this;
$payload['app']->group(plugin_route_group(), function (App $app) use ($plugin) {
$app->post('/votes', [$plugin, 'castVote']);
})->add(new DetermineCurrentPlugin());;
}
/**
* @param string $event
* @param int $bugId
* @param int $bugnoteId
* @param bool $isPrivate
*
* @return void
*/
public function handleViewBugnote(string $event, int $bugId, int $bugnoteId, bool $isPrivate)
{
$this->displayBugnoteAwards($bugnoteId);
}
/**
* @param \BugnoteData[] $bugnotes
*
* @return void
*/
public function handleViewBugnotesStart(string $event, int $bugId, array $bugnotes)
{
$this->votes = fetch_bugnote_votes(array_column($bugnotes, 'id'));
}
public function castVote(Request $request)
{
$parameters = array_map('intval', $request->getParams(['bugnote_id', 'emoji']));
if (!current_user_is_anonymous()) {
cast_bugnote_vote(...$parameters);
}
$this->displayBugnoteAwards($parameters['bugnote_id']);
}
}