Skip to content
This repository was archived by the owner on Sep 19, 2022. It is now read-only.

Commit 1767f8f

Browse files
authored
Translatable columns (#127)
Allow translation of multi-language attributes
1 parent e5d27e1 commit 1767f8f

File tree

5 files changed

+110
-55
lines changed

5 files changed

+110
-55
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file.
1414
- ListOfSps
1515
- Don't show the description by default
1616
- Added required attribute 'listOfSps.serviceNameAttr' !!!
17+
- Add translation for multi-languages attributes
1718

1819
## [v3.9.0]
1920
#### Added

config-templates/module_perun.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@
182182
''
183183
],
184184

185+
/**
186+
* Specify list of facility attributes which have translations.
187+
* If an attribute is not included in listOfSps.attributesDefinitions, it will be added.
188+
* Defaults to an empty array.
189+
*/
190+
//'listOfSps.multilingualAttributes' => [],
191+
185192
/**
186193
********************************************
187194
* Part of configuration for Warning on DS *

lib/ListOfSps.php

Lines changed: 80 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,23 @@ public static function sortByName($a, $b)
88
return strcmp(strtolower($a['facility']->getName()), strtolower($b['facility']->getName()));
99
}
1010

11-
public static function getClass($attribute)
11+
public static function getClass($type)
1212
{
13-
if ($attribute['type'] === 'java.lang.String' || $attribute['type'] === 'java.lang.LargeString') {
14-
return 'string';
15-
} elseif ($attribute['type'] === 'java.lang.Integer') {
16-
return 'integer';
17-
} elseif ($attribute['type'] === 'java.lang.Boolean') {
18-
return 'boolean';
19-
} elseif ($attribute['type'] === 'java.util.ArrayList' || $attribute['type'] === 'java.util.LargeArrayList') {
20-
return 'array';
21-
} elseif ($attribute['type'] === 'java.util.LinkedHashMap') {
22-
return 'map';
23-
} else {
24-
return '';
13+
switch ($type) {
14+
case 'java.lang.String':
15+
case 'java.lang.LargeString':
16+
return 'string';
17+
case 'java.lang.Integer':
18+
return 'integer';
19+
case 'java.lang.Boolean':
20+
return 'boolean';
21+
case 'java.util.ArrayList':
22+
case 'java.util.LargeArrayList':
23+
return 'array';
24+
case 'java.util.LinkedHashMap':
25+
return 'map';
26+
default:
27+
return '';
2528
}
2629
}
2730

@@ -34,44 +37,77 @@ public static function printServiceName($name, $loginURL = null)
3437
return "<a class='customLink' href='" . htmlspecialchars($loginURL) . "'>" . htmlspecialchars($name) . "</a>";
3538
}
3639

37-
public static function printAttributeValue($attribute, $service, $attr)
40+
public static function printAttributeValue($type, $value)
3841
{
39-
$value = $attribute['value'];
40-
if (empty($value) && $attribute['type'] !== 'java.lang.Boolean') {
42+
if (empty($value) && $type !== 'java.lang.Boolean') {
4143
return "<td class='center'>&horbar;</td>";
4244
}
43-
$string = '';
44-
if ($attribute['type'] === 'java.lang.String' || $attribute['type'] === 'java.lang.LargeString') {
45-
if (filter_var($value, FILTER_VALIDATE_URL)) {
46-
$string = '<a class="customLink" href="' . $value . '">' . $value . '</a>';
47-
} else {
48-
$string = $value;
49-
}
50-
} elseif ($attribute['type'] === 'java.lang.Integer') {
51-
$string = $value;
52-
} elseif ($attribute['type'] === 'java.lang.Boolean') {
53-
if ($value !== null && $value) {
54-
$string = '&#x2714;';
55-
} else {
56-
$string = '&#x2715;';
57-
}
58-
} elseif ($attribute['type'] === 'java.util.ArrayList' || $attribute['type'] === 'java.lang.LargeArrayList') {
59-
$string = '<ul>';
60-
foreach ($value as $v) {
61-
$string .= '<li>' . $v . '</li>';
62-
}
63-
$string .= '</ul>';
64-
} elseif ($attribute['type'] === 'java.util.LinkedHashMap') {
65-
$string = '<ul>';
66-
foreach ($value as $k => $v) {
67-
$string .= '<li>' . $k . ' &rarr; ' . $v . '</li>';
68-
}
69-
$string .= '</ul>';
45+
46+
switch ($type) {
47+
case 'java.lang.String':
48+
case 'java.lang.LargeString':
49+
if (filter_var($value, FILTER_VALIDATE_URL)) {
50+
$string = '<a class="customLink" href="' . htmlspecialchars($value) . '">'
51+
. htmlspecialchars($value) . '</a>';
52+
} else {
53+
$string = htmlspecialchars($value);
54+
}
55+
break;
56+
case 'java.lang.Integer':
57+
$string = htmlspecialchars($value);
58+
break;
59+
case 'java.lang.Boolean':
60+
if ($value !== null && $value) {
61+
$string = '&#x2714;';
62+
} else {
63+
$string = '&#x2715;';
64+
}
65+
break;
66+
case 'java.util.ArrayList':
67+
case 'java.lang.LargeArrayList':
68+
$string = '<ul>';
69+
foreach ($value as $v) {
70+
$string .= '<li>' . htmlspecialchars($v) . '</li>';
71+
}
72+
$string .= '</ul>';
73+
break;
74+
case 'java.util.LinkedHashMap':
75+
$string = '<ul>';
76+
foreach ($value as $k => $v) {
77+
$string .= '<li>' . htmlspecialchars($k) . ' &rarr; ' . htmlspecialchars($v) . '</li>';
78+
}
79+
$string .= '</ul>';
80+
break;
81+
default:
82+
$string = '';
7083
}
7184
if (!empty($string)) {
72-
return '<td class="' . self::getClass($service['facilityAttributes'][$attr]) . '">' . $string . '</td>';
85+
return '<td class="' . self::getClass($type) . '">' . $string . '</td>';
7386
} else {
7487
return '<td/>';
7588
}
7689
}
90+
91+
public static function getPreferredTranslation($translations, $language = 'en')
92+
{
93+
if (is_string($translations)) {
94+
return $translations;
95+
}
96+
97+
if (isset($translations[$language])) {
98+
return $translations[$language];
99+
}
100+
101+
if (isset($translations['en'])) {
102+
return $translations['en'];
103+
}
104+
105+
if (count($translations) > 0) {
106+
$languages = array_keys($translations);
107+
return $translations[$languages[0]];
108+
}
109+
110+
// we don't have anything to return
111+
throw new \Exception('Nothing to return from translation.');
112+
}
77113
}

templates/listOfSps-tpl.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@
9797
<?php
9898
foreach ($attributesToShow as $attr) {
9999
if (!empty($samlServices)) {
100-
echo "<th class='" .
101-
ListOfSps::getClass(array_values($samlServices)[0]['facilityAttributes'][$attr]) .
102-
"'>" . array_values($samlServices)[0]['facilityAttributes'][$attr]['displayName']
103-
. "</th>";
100+
echo "<th class='" . ListOfSps::getClass(
101+
array_values($samlServices)[0]['facilityAttributes'][$attr]['type']
102+
) . "'>" . array_values($samlServices)[0]['facilityAttributes'][$attr]['displayName']
103+
. "</th>";
104104
}
105105
}
106106
?>
@@ -116,20 +116,24 @@
116116
}
117117
echo '<tr>';
118118
echo '<td>'
119-
. ListOfSps::printServiceName($service['name']['value'], $service['loginURL']['value'] ?? null)
119+
. ListOfSps::printServiceName(
120+
ListOfSps::getPreferredTranslation($service['name']['value'], $this->getLanguage()),
121+
$service['loginURL']['value'] ?? null
122+
)
120123
. '</td>';
121124
if (array_key_exists($service['facility']->getID(), $samlServices)) {
122125
echo '<td>' . $this->t('{perun:listOfSps:saml}') . '</td>';
123126
} else {
124127
echo '<td>' . $this->t('{perun:listOfSps:oidc}') . '</td>';
125128
}
126129
foreach ($attributesToShow as $attr) {
127-
$value = ListOfSps::printAttributeValue(
128-
$service['facilityAttributes'][$attr],
129-
$service,
130-
$attr
131-
);
132-
echo $value;
130+
$type = $service['facilityAttributes'][$attr]['type'];
131+
$value = $service['facilityAttributes'][$attr]['value'];
132+
if ($value !== null && in_array($attr, $this->data['multilingualAttributes'])) {
133+
$type = 'java.lang.String';
134+
$value = ListOfSps::getPreferredTranslation($value, $this->getLanguage());
135+
}
136+
echo ListOfSps::printAttributeValue($type, $value);
133137
}
134138
}
135139
echo '</tr>';

www/listOfSps.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
const CONFIG_FILE_NAME = 'module_perun.php';
1010
const PROXY_IDENTIFIER = 'listOfSps.proxyIdentifier';
1111
const ATTRIBUTES_DEFINITIONS = 'listOfSps.attributesDefinitions';
12+
const MULTILINGUAL_ATTRIBUTES = 'listOfSps.multilingualAttributes';
1213
const SHOW_OIDC_SERVICES = 'listOfSps.showOIDCServices';
1314

1415
const PERUN_SERVICE_NAME_ATTR_NAME = 'listOfSps.serviceNameAttr';
@@ -45,6 +46,11 @@
4546
. ATTRIBUTES_DEFINITIONS . '\'.'
4647
);
4748
}
49+
$multilingualAttributes = $conf->getArray(MULTILINGUAL_ATTRIBUTES, []);
50+
$attributesDefinitions = array_merge(
51+
$attributesDefinitions,
52+
array_diff($multilingualAttributes, $attributesDefinitions)
53+
);
4854

4955
$showOIDCServices = $conf->getBoolean(SHOW_OIDC_SERVICES, false);
5056
$perunSaml2EntityIdAttr = $conf->getString(PERUN_SAML2_ENTITY_ID_ATTR_NAME);
@@ -195,6 +201,7 @@
195201
$t = new Template($config, 'perun:listOfSps-tpl.php');
196202
$t->data['statistics'] = $statistics;
197203
$t->data['attributesToShow'] = $attributesToShow;
204+
$t->data['multilingualAttributes'] = $multilingualAttributes;
198205
$t->data['samlServices'] = $samlServices;
199206
$t->data['oidcServices'] = $oidcServices;
200207
$t->data['allServices'] = $allServices;

0 commit comments

Comments
 (0)