-
Notifications
You must be signed in to change notification settings - Fork 1k
/
KununuBridge.php
156 lines (134 loc) · 4.58 KB
/
KununuBridge.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
<?php
class KununuBridge extends BridgeAbstract
{
const MAINTAINER = 'logmanoriginal';
const NAME = 'Kununu Bridge';
const URI = 'https://www.kununu.com/';
const CACHE_TIMEOUT = 86400; // 24h
const DESCRIPTION = 'Returns the latest reviews for a company and site of your choice.';
const PARAMETERS = [
'global' => [
'site' => [
'name' => 'Site',
'type' => 'list',
'title' => 'Select your site',
'values' => [
'Austria' => 'at',
'Germany' => 'de',
'Switzerland' => 'ch'
],
'exampleValue' => 'de',
],
'include_ratings' => [
'name' => 'Include ratings',
'type' => 'checkbox',
'title' => 'Activate to include ratings in the feed'
],
'limit' => [
'name' => 'Limit',
'type' => 'number',
'defaultValue' => 3,
'title' => "Maximum number of items to return in the feed.\n0 = unlimited"
]
],
[
'company' => [
'name' => 'Company',
'required' => true,
'exampleValue' => 'kununu',
'title' => 'Insert company name (i.e. Kununu) or URI path (i.e. kununu)'
]
]
];
private $companyName = '';
public function getURI()
{
if (!is_null($this->getInput('company')) && !is_null($this->getInput('site'))) {
$company = $this->fixCompanyName($this->getInput('company'));
$site = $this->getInput('site');
return sprintf('%s%s/%s', self::URI, $site, $company);
}
return parent::getURI();
}
public function getName()
{
if (!is_null($this->getInput('company'))) {
$company = $this->fixCompanyName($this->getInput('company'));
return ($this->companyName ?: $company) . ' - ' . self::NAME;
}
return parent::getName();
}
public function getIcon()
{
return 'https://www.kununu.com/favicon-196x196.png';
}
public function collectData()
{
$full = $this->getInput('full');
// Load page
$json = json_decode(getContents($this->getAPI()), true);
$this->companyName = $json['common']['name'];
$baseURI = $this->getURI() . '/bewertung/';
$limit = $this->getInput('limit') ?: 0;
// Go through all articles
foreach ($json['reviews'] as $review) {
$item = [];
$item['author'] = $review['position'] . ' / ' . $review['department'];
$item['timestamp'] = $review['createdAt'];
$item['title'] = $review['roundedScore'] . ' : ' . $review['title'];
$item['uri'] = $baseURI . $review['uuid'];
$item['content'] = $this->extractArticleDescription($review);
$this->items[] = $item;
if ($limit > 0 && count($this->items) >= $limit) {
break;
}
}
}
/**
* Returns JSON API url
*/
private function getAPI()
{
$company = $this->fixCompanyName($this->getInput('company'));
$site = $this->getInput('site');
return self::URI . 'middlewares/profiles/' .
$site . '/' . $company .
'/reviews?reviewType=employees&urlParams=sort=newest&sort=newest&page=1';
}
/*
* Returns a fixed version of the provided company name
*/
private function fixCompanyName($company)
{
$company = trim($company);
$company = str_replace(' ', '-', $company);
$company = strtolower($company);
$umlauts = ['/ä/','/ö/','/ü/','/Ä/','/Ö/','/Ü/','/ß/'];
$replace = ['ae','oe','ue','Ae','Oe','Ue','ss'];
return preg_replace($umlauts, $replace, $company);
}
/**
* Returns the description from a given article
*/
private function extractArticleDescription($json)
{
$retVal = '';
foreach ($json['texts'] as $text) {
$retVal .= '<h4>' . $text['id'] . '</h4><p>' . $text['text'] . '</p>';
}
if ($this->getInput('include_ratings') && !empty($json['ratings'])) {
$retVal .= (empty($retVal) ? '' : '<hr>') . '<table>';
foreach ($json['ratings'] as $rating) {
$retVal .= <<<EOD
<tr>
<td>{$rating['id']}
<td>{$rating['roundedScore']}
<td>{$rating['text']}
</tr>
EOD;
}
$retVal .= '</table>';
}
return $retVal;
}
}