-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangeLanguageWithURL.php
More file actions
179 lines (152 loc) · 5.51 KB
/
Copy pathChangeLanguageWithURL.php
File metadata and controls
179 lines (152 loc) · 5.51 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2025 webtrees development team
* <http://webtrees.net>
*
* ChangeLanguageWithURL (webtrees custom module):
* Copyright (C) 2025 Markus Hemprich
* <http://www.familienforschung-hemprich.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*
* ChangeLanguageWithURL
*
* A webtrees 2.1/2.2 custom module to change the webtrees language
* by URL requests with the language provided as an URL parameter.
*
*/
declare(strict_types=1);
namespace Jefferson49\Webtrees\Module\ChangeLanguageWithURL;
use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Module\AbstractModule;
use Fisharebest\Webtrees\Module\ModuleCustomInterface;
use Fisharebest\Webtrees\Session;
use Fisharebest\Webtrees\Site;
use Fisharebest\Webtrees\View;
use Fisharebest\Webtrees\Webtrees;
use Jefferson49\Webtrees\Helpers\Functions;
use Jefferson49\Webtrees\Module\ModuleCustomTrait;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class ChangeLanguageWithURL extends AbstractModule implements ModuleCustomInterface, MiddlewareInterface {
use ModuleCustomTrait;
//Custom module version
public const CUSTOM_VERSION = '1.2.2';
//Github repository
public const GITHUB_REPO = 'Jefferson49/ChangeLanguageWithURL';
//Author of custom module
public const CUSTOM_AUTHOR = 'Markus Hemprich';
/**
* Initialization.
*
* @return void
*/
public function boot(): void
{
//Register this class in the webtrees container
//This allows to access the module instance from other places, e.g. views/scripts (->assetUrl)
Functions::registerInContainer(self::class, $this);
// Register a namespace for our views.
View::registerNamespace(self::viewsNamespace(), $this->resourcesFolder() . 'views/');
}
/**
* How should this module be identified in the control panel, etc.?
*
* @return string
*/
public function title(): string
{
return 'ChangeLanguageWithURL module';
}
/**
* Show error message in the front end
*
* @return ResponseInterface
*/
private function showErrorMessage(string $text): ResponseInterface
{
return $this->viewResponse($this->name() . '::error', [
'title' => 'Error',
'tree' => null,
'text' => I18N::translate('Custom module') . ': ' . $this->name() . '<br><b>'. $text . '</b>',
]);
}
/**
* Whether a language tag belongs to an active language
*
* @param string $language_tag
*
* @return bool
*/
public static function isActiveLanguage(string $language_tag): bool
{
if (version_compare(Webtrees::VERSION, '2.2.6', '>')) {
return array_key_exists($language_tag, I18N::activeLanguages());
}
$language_found = false;
foreach (I18N::activeLocales() as $locale) {
if ($locale->languageTag() === $language_tag) {
$language_found = true;
break;
}
}
return $language_found;
}
/**
* Code here is executed before and after we process the request/response.
* We can block access by throwing an exception.
*
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
*
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$params = $request->getQueryParams();
$language = $params['language'] ?? '';
$language_after_signout = $params['language_after_signout'] ?? '';
if ($language_after_signout === 'reset') {
Site::setPreference('LANGUAGE_AFTER_SIGNOUT', '');
}
elseif ($language_after_signout !== '') {
if (self::isActiveLanguage($language_after_signout)) {
Site::setPreference('LANGUAGE_AFTER_SIGNOUT', $language_after_signout);
}
else {
//Show error message
return $this->showErrorMessage(I18N::translate('Requested language tag not found') . ': ' . $language_after_signout);
}
}
//Set language after sign out
if(!Auth::check() && Site::getPreference('LANGUAGE_AFTER_SIGNOUT') !== '') {
Session::put('language', Site::getPreference('LANGUAGE_AFTER_SIGNOUT'));
}
//Set language
if ($language !== '') {
if (self::isActiveLanguage($language)) {
I18N::init($language);
Session::put('language', $language);
}
else {
//Show error message
return $this->showErrorMessage(I18N::translate('Requested language tag not found') . ': ' . $language);
}
}
return $handler->handle($request);
}
}