forked from niwanetwork/niwa-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_api.php
143 lines (131 loc) · 3.24 KB
/
_api.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
<?php
/**
* This file pulls in member/affiliate data from the json files
* and defines helper functions that may be used throughout the website
*/
class NiwaDataHelper
{
/**
* JSON object of member wikis
*
* @var object
*/
protected $memberWikis;
/**
* JSON object of affiliate wikis
*
* @var object
*/
protected $affiliates;
const URL_REPLACE_STRING = "$1";
function __construct()
{
$this->memberWikis = json_decode(
file_get_contents("data/members.json")
);
$this->affiliates = json_decode(
file_get_contents("data/affiliates.json")
);
}
/**
* Return all member wikis or if language code given,
* all wikis for that language code.
*
* @param string $languageCode
* @return object
*/
public function getMemberWikis($languageCode = null)
{
if ($languageCode) {
return $this->memberWikis->{$languageCode};
}
return $this->memberWikis;
}
/**
* Return the affiliates
*
* @return object
*/
public function getAffiliates()
{
return $this->affiliates;
}
/**
* Returns a link for the given Wiki interwiki url and page
*
* @param string $url Interwiki URL
* @param string $page Wiki page
* @return string
*/
public function getWikiLink($url, $page = "")
{
return str_replace(self::URL_REPLACE_STRING, $page, $url);
}
/**
* Returns a given wiki's mainpage
*
* @param object $wiki
* @return string
*/
public function getWikiMainpage($wiki)
{
return $this->getWikiLink($wiki->url, $wiki->mainpage);
}
/**
* Generates a link for the member or affiliate
*
* @param string $url The anchor tag href
* @param string $text The anchor tag display text
* @return string
*/
protected function generateMemberLink($url, $text)
{
return "<a class='member-wiki-link' href='{$url}'>{$text}</a>";
}
/**
* Generates the html string for Links with error checking for wikis that do not have
* one of the options.
*
* Requires a individual wiki array from the api.
*
* @param object $member
* @return string $links
*/
public function generateMemberLinks($member)
{
// Check if wiki mainpage is specified or if we should just use the URL as-is
if (isset($member->mainpage)) {
$links = $this->generateMemberLink(
$this->getWikiMainpage($member),
$member->title
);
} else {
$links = $this->generateMemberLink(
$member->url,
$member->title
);
}
if (isset($member->site)) {
$links .= $this->generateMemberLink($member->site, $member->siteName);
};
if (isset($member->forums)) {
$links .= $this->generateMemberLink($member->forums, "Forums");
};
if (isset($member->chat)) {
$links .= $this->generateMemberLink($member->chat, "Chat");
};
if (isset($member->discord)) {
$links .= $this->generateMemberLink($member->discord, "Discord");
};
if (isset($member->twitter)) {
$links .= $this->generateMemberLink($member->twitter, "Twitter");
};
if (isset($member->twitch)) {
$links .= $this->generateMemberLink($member->twitch, "Twitch");
};
if (isset($member->facebook)) {
$links .= $this->generateMemberLink($member->facebook, "Facebook");
};
return $links;
}
}