-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPrimerTheme.php
314 lines (273 loc) · 9.45 KB
/
PrimerTheme.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
declare(strict_types=1);
namespace Primer;
use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\FlashMessages;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Menu;
use Fisharebest\Webtrees\Session;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\View;
use Fisharebest\Webtrees\Module\MinimalTheme;
use Fisharebest\Webtrees\Module\ModuleConfigInterface;
use Fisharebest\Webtrees\Module\ModuleConfigTrait;
use Fisharebest\Webtrees\Module\ModuleCustomInterface;
use Fisharebest\Webtrees\Module\ModuleCustomTrait;
use Fisharebest\Webtrees\Module\ModuleGlobalInterface;
use Fisharebest\Webtrees\Module\ModuleGlobalTrait;
use Fisharebest\Webtrees\Validator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class PrimerTheme extends MinimalTheme implements ModuleCustomInterface, ModuleGlobalInterface, ModuleConfigInterface {
use ModuleConfigTrait;
use ModuleCustomTrait;
use ModuleGlobalTrait;
public const CUSTOM_AUTHOR = 'Fredrik Ekdahl';
public const CUSTOM_VERSION = '1.3.0';
public const GITHUB_REPO = 'ekdahl/webtrees-primer-theme';
public const CUSTOM_SUPPORT_URL = 'https://github.com/ekdahl/webtrees-primer-theme';
public const CUSTOM_LATEST_VERSION = 'https://raw.githubusercontent.com/' . self::GITHUB_REPO . '/main/latest-version.txt';
/**
* @return string
*/
public function title(): string
{
return 'Primer';
}
/**
* {@inheritDoc}
* @see \Fisharebest\Webtrees\Module\ModuleCustomInterface::customModuleAuthorName()
*/
public function customModuleAuthorName(): string
{
return self::CUSTOM_AUTHOR;
}
/**
* {@inheritDoc}
* @see \Fisharebest\Webtrees\Module\ModuleCustomInterface::customModuleVersion()
*/
public function customModuleVersion(): string
{
return self::CUSTOM_VERSION;
}
/**
* {@inheritDoc}
* @see \Fisharebest\Webtrees\Module\ModuleCustomInterface::customModuleSupportUrl()
*/
public function customModuleSupportUrl(): string
{
return self::CUSTOM_SUPPORT_URL;
}
/**
* {@inheritDoc}
* @see \Fisharebest\Webtrees\Module\ModuleCustomInterface::customModuleLatestVersionUrl()
*/
public function customModuleLatestVersionUrl(): string
{
return self::CUSTOM_LATEST_VERSION;
}
/**
* Generate a list of items for the user menu.
*
* @param Tree|null $tree
*
* @return array<Menu>
*/
public function userMenu(?Tree $tree): array
{
return array_filter([
$this->menuPendingChanges($tree),
$this->menuMyPages($tree),
$this->menuThemes(),
$this->menuPalette(),
$this->menuLanguages(),
$this->menuLogin(),
$this->menuLogout(),
]);
}
/**
* Bootstrap the module
*/
public function boot(): void
{
// Register a namespace for our views.
View::registerNamespace($this->name(), $this->resourcesFolder() . 'views/');
}
/**
* Where does this module store its resources
*
* @return string
*/
public function resourcesFolder(): string
{
return __DIR__ . '/resources/';
}
/**
* Raw content, to be added at the end of the <head> element.
* Typically, this will be <link> and <meta> elements.
* we use it to load the special font files
*
* @return string
*/
public function headContent(): string
{
return
'<style>
@font-face {
font-family: \'Font Awesome 6 Free\';
src: url("' . $this->assetUrl('fonts/fa-solid-900.woff2') . '") format("woff2");
font-style: normal;
font-weight: 900;
font-display: block;
}
@font-face {
font-family: \'icomoon\';
src: url("' . $this->assetUrl('fonts/icomoon.woff') . '") format("woff");
font-style: normal;
font-weight: normal;
font-display: block;
}
</style>';
}
/**
* Add our own stylesheet to the existing stylesheets.
*
* @return array<string>
*/
public function stylesheets(): array
{
$stylesheets = parent::stylesheets();
$stylesheets[] = $this->assetUrl('css/theme.css');
$stylesheets[] = $this->assetUrl('css/charts.css');
$stylesheets[] = $this->assetUrl('css/icons.css');
$stylesheets[] = $this->assetUrl('css/primer/base/size/size.css');
$stylesheets[] = $this->assetUrl('css/primer/base/typography/typography.css');
$stylesheets[] = $this->assetUrl('css/primer/functional/size/border.css');
$stylesheets[] = $this->assetUrl('css/primer/functional/size/size.css');
$stylesheets[] = $this->assetUrl('css/primer/functional/themes/' . $this->palette() . '.css');
$stylesheets[] = $this->assetUrl('css/primer/functional/typography/typography.css');
$stylesheets[] = $this->assetUrl('css/primer/primitives/temp-typography-tokens.css');
$stylesheets[] = $this->assetUrl('css/site-logo-' . $this->getPreference('logo', 'people') . '.css');
return $stylesheets;
}
/**
* Create a menu of palette options
*
* @return Menu
*/
protected function menuPalette(): Menu
{
/* I18N: A colour scheme */
$menu = new Menu(I18N::translate('Palette'), '#', 'menu-color');
$palette = $this->palette();
foreach ($this->palettes() as $palette_id => $palette_name) {
$url = route('module', ['module' => $this->name(), 'action' => 'Palette', 'palette' => $palette_id]);
$submenu = new Menu(
$palette_name,
'#',
'menu-primer-' . $palette_id . ($palette === $palette_id ? ' active' : ''),
[
'data-wt-post-url' => $url,
]
);
$menu->addSubmenu($submenu);
}
return $menu;
}
/**
* Switch to a new palette
*
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
public function postPaletteAction(ServerRequestInterface $request): ResponseInterface
{
$user = Validator::attributes($request)->user();
$palette = Validator::queryParams($request)->isInArrayKeys($this->palettes())->string('palette');
$user->setPreference('primer-palette', $palette);
Session::put('primer-palette', $palette);
return response();
}
/**
* @return array<string>
*/
private function palettes(): array
{
$palettes = [
'dark' => I18N::translate('Dark default'),
'dark-dimmed' => I18N::translate('Dark dimmed'),
'dark-high-contrast' => I18N::translate('Dark high contrast'),
'light' => I18N::translate('Light default'),
'light-high-contrast' => I18N::translate('Light high contrast'),
];
return $palettes;
}
/**
* @return string
*/
private function palette(): string
{
// If we are logged in, use our preference
$palette = Auth::user()->getPreference('primer-palette');
// If not logged in or no preference, use one we selected earlier in the session.
if ($palette === '') {
$palette = Session::get('primer-palette');
$palette = is_string($palette) ? $palette : '';
}
// We haven't selected one this session? Use the default
if ($palette === '') {
$palette = $this->getPreference('palette', 'dark');
}
return $palette;
}
/**
* @return array<string>
*/
private function logos(): array
{
$logos = [
'people' => I18N::translate('People'),
'webtrees' => I18N::translate('webtrees'),
];
return $logos;
}
/**
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
public function getAdminAction(): ResponseInterface
{
if (Session::get('theme') !== $this->name()) {
// We need to register the namespace for this view because the boot didn't run
View::registerNamespace($this->name(), $this->resourcesFolder() . 'views/');
}
$this->layout = 'layouts/administration';
return $this->viewResponse($this->name() . '::settings', [
'palette' => $this->getPreference('palette', 'dark'),
'palettes' => $this->palettes(),
'title' => $this->title(),
'logo' => $this->getPreference('logo', 'people'),
'logos' => $this->logos(),
]);
}
/**
* Save the user preference.
*
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
public function postAdminAction(ServerRequestInterface $request): ResponseInterface
{
$params = (array) $request->getParsedBody();
if ($params['save'] === '1') {
$this->setPreference('palette', $params['palette']);
$this->setPreference('logo', $params['logo']);
$message = I18N::translate('The preferences for the module “%s” have been updated.', $this->title());
FlashMessages::addMessage($message, 'success');
}
return redirect($this->getConfigLink());
}
};