-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEsekPosts.php
66 lines (51 loc) · 1.51 KB
/
EsekPosts.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
<?php
require_once(__DIR__ . '/EsekClient.php');
class EsekPosts
{
const TAG_NAME = 'Posts';
// gets run on the `ParserFirstCallInit` hook
public static function onParserFirstCallInit(Parser $parser)
{
// When the parser sees the <Posts> tag, it executes renderPosts
$parser->setHook(self::TAG_NAME, [self::class, 'renderPosts']);
}
public static function renderPosts($input, array $args, Parser $parser, PPFrame $frame)
{
// if no STIL-ID, don't return anything
if (empty($input)) {
return '';
}
$username = htmlspecialchars($input);
$postsTable = self::fetchPosts($username);
return $postsTable;
}
private static function fetchPosts(string $username)
{
$client = new EsekClient();
$posts = $client->getPostsForUser($username);
if (count($posts) == 0) {
return "<p>Denna användaren har inte haft några poster ännu...</p>";
}
$table = self::createTableFromPosts($posts);
return $table;
}
private static function createTableFromPosts(array $posts)
{
$table = "<table class=\"wikitable esek-table esek-posts\">";
$table .= "<tr>";
$table .= "<th>Postnamn</th>";
$table .= "<th>Start</th>";
$table .= "<th>Slut</th>";
$table .= "</tr>";
foreach ($posts as $post) {
$post_url = "/index.php/" . $post['name'];
$table .= "<tr>";
$table .= "<td><a href=\"$post_url\">" . $post['name'] . "</a></td>";
$table .= "<td>" . $post['start'] . "</td>";
$table .= "<td>" . $post['end'] . "</td>";
$table .= "</tr>";
}
$table .= "</table>";
return $table;
}
}