-
Notifications
You must be signed in to change notification settings - Fork 7
/
JustlightTheme.php
452 lines (392 loc) · 16 KB
/
JustlightTheme.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
declare(strict_types=1);
namespace JustCarmen\Webtrees\Module\JustlightTheme;
use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Menu;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\View;
use Fisharebest\Webtrees\Session;
use Fisharebest\Webtrees\FlashMessages;
use Psr\Http\Message\ResponseInterface;
use Fisharebest\Localization\Translation;
use Psr\Http\Message\ServerRequestInterface;
use Fisharebest\Webtrees\Module\MinimalTheme;
use Illuminate\Database\Capsule\Manager as DB;
use Fisharebest\Webtrees\Contracts\UserInterface;
use Fisharebest\Webtrees\Module\ModuleThemeTrait;
use Fisharebest\Webtrees\Module\ModuleConfigTrait;
use Fisharebest\Webtrees\Module\ModuleCustomTrait;
use Fisharebest\Webtrees\Module\ModuleFooterTrait;
use Fisharebest\Webtrees\Module\ModuleGlobalTrait;
use Fisharebest\Webtrees\Module\ModuleThemeInterface;
use Fisharebest\Webtrees\Module\ModuleConfigInterface;
use Fisharebest\Webtrees\Module\ModuleCustomInterface;
use Fisharebest\Webtrees\Module\ModuleFooterInterface;
use Fisharebest\Webtrees\Module\ModuleGlobalInterface;
class JustlightTheme extends MinimalTheme implements ModuleThemeInterface, ModuleCustomInterface, ModuleFooterInterface, ModuleGlobalInterface, ModuleConfigInterface
{
use ModuleThemeTrait;
use ModuleCustomTrait;
use ModuleFooterTrait;
use ModuleGlobalTrait;
use ModuleConfigTrait;
// Module constants
public const CUSTOM_AUTHOR = 'JustCarmen';
public const CUSTOM_VERSION = '2.2.8';
public const GITHUB_REPO = 'webtrees-theme-justlight';
public const AUTHOR_WEBSITE = 'justcarmen.nl';
public const CUSTOM_SUPPORT_URL = 'https://' . self::AUTHOR_WEBSITE . '/modules-webtrees-2/justlight-theme/';
/**
* {@inheritDoc}
* @see \Fisharebest\Webtrees\Module\AbstractModule::title()
*/
public function title(): string
{
return I18N::translate('JustLight');
}
/**
* {@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;
}
/**
* A URL that will provide the latest stable version of this module.
*
* @return string
*/
public function customModuleLatestVersionUrl(): string
{
return 'https://raw.githubusercontent.com/' . self::CUSTOM_AUTHOR . '/' . self::GITHUB_REPO . '/main/latest-version.txt';
}
/**
* {@inheritDoc}
* @see \Fisharebest\Webtrees\Module\ModuleCustomInterface::customModuleSupportUrl()
*/
public function customModuleSupportUrl(): string
{
return self::CUSTOM_SUPPORT_URL;
}
/**
* {@inheritDoc}
* @see \Fisharebest\Webtrees\Module\AbstractModule::boot()
*/
public function boot(): void
{
// Register a namespace for our views.
View::registerNamespace($this->name(), $this->resourcesFolder() . 'views/');
// Add all javascript used by this module in a view
View($this->name() . '::theme/script.js');
// Replace an existing view with our own version.
View::registerCustomView('::report-setup-page', $this->name() . '::report-setup-page');
View::registerCustomView('::icons/add', $this->name() . '::icons/add');
View::registerCustomView('::icons/anniversary', $this->name() . '::icons/anniversary');
View::registerCustomView('::icons/calendar', $this->name() . '::icons/calendar');
View::registerCustomView('::icons/delete', $this->name() . '::icons/delete');
View::registerCustomView('::icons/individual', $this->name() . '::icons/individual');
View::registerCustomView('::layouts/default', $this->name() . '::layouts/default');
View::registerCustomView('::modules/clippings/show', $this->name() . '::modules/clippings/show');
View::registerCustomView('::modules/faq/show', $this->name() . '::modules/faq/show');
View::registerCustomView('::modules/favorites/favorites', $this->name() . '::modules/favorites/favorites');
View::registerCustomView('::modules/gedcom_stats/statistics', $this->name() . '::modules/gedcom_stats/statistics');
View::registerCustomView('::modules/lightbox/tab', $this->name() . '::modules/lightbox/tab');
View::registerCustomView('::modules/stories/list', $this->name() . '::modules/stories/list');
View::registerCustomView('::modules/user-messages/user-messages', $this->name() . '::modules/user-messages/user-messages');
// specific views for the JustBlack palette
if ($this->palette() === 'justblack') {
View::registerCustomView('::statistics/other/charts/column', $this->name() . '::statistics/other/charts/column');
View::registerCustomView('::statistics/other/charts/combo', $this->name() . '::statistics/other/charts/combo');
View::registerCustomView('::statistics/other/charts/geo', $this->name() . '::statistics/other/charts/geo');
View::registerCustomView('::statistics/other/charts/pie', $this->name() . '::statistics/other/charts/pie');
}
}
/**
* {@inheritDoc}
* @see \Fisharebest\Webtrees\Module\AbstractModule::resourcesFolder()
*/
public function resourcesFolder(): string
{
return __DIR__ . '/resources/';
}
/**
* @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', [
'allow_switch' => $this->getPreference('allow-switch', '0'),
'palette' => $this->getPreference('palette', 'justlight'),
'palettes' => $this->palettes(),
'title' => $this->title()
]);
}
/**
* Save the user preference.
*
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
public function postAdminAction(ServerRequestInterface $request): ResponseInterface
{
$params = (array) $request->getParsedBody();
if ($params['save'] === '1') {
if ($params['allow-switch'] === '0') {
// remove any previous set users' palette choice.
DB::table('user_setting')->where('setting_name', '=', 'justlight-palette')->delete();
}
$this->setPreference('palette', $params['palette']);
$this->setPreference('allow-switch', $params['allow-switch']);
$message = I18N::translate('The preferences for the module “%s” have been updated.', $this->title());
FlashMessages::addMessage($message, 'success');
}
return redirect($this->getConfigLink());
}
/**
* 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: \'icomoon\';
src: url("' . $this->assetUrl('fonts/icomoon.eot') . '");
src: url("' . $this->assetUrl('fonts/icomoon.eot') . ' #iefix") format("embedded-opentype"),
url("' . $this->assetUrl('fonts/icomoon.ttf') . '") format("truetype"),
url("' . $this->assetUrl('fonts/icomoon.woff') . '") format("woff"),
url("' . $this->assetUrl('fonts/icomoon.svg') . ' #icomoon") format("svg");
font-weight: normal;
font-style: normal;
font-display: block;
}
@font-face {
font-family: \'Font Awesome 5 Free\';
src: url("' . $this->assetUrl('fonts/fa-solid-900.eot') . '");
src: url("' . $this->assetUrl('fonts/fa-solid-900.eot') . '?#iefix") format("embedded-opentype"),
url("' . $this->assetUrl('fonts/fa-solid-900.woff2') . '") format("woff2"),
url("' . $this->assetUrl('fonts/fa-solid-900.woff') . '") format("woff"),
url("' . $this->assetUrl('fonts/fa-solid-900.ttf') . '") format("truetype"),
url("' . $this->assetUrl('fonts/fa-solid-900.svg') . '#fontawesome") format("svg");
font-style: normal;
font-weight: 900;
font-display: block;
}
.fa,
.fas {
font-family: \'Font Awesome 5 Free\';
font-weight: 900;
}
</style>';
}
/**
* {@inheritDoc}
* @see \Fisharebest\Webtrees\Module\AbstractModule::stylesheets()
*/
public function stylesheets(): array
{
$color_scheme = isset($_COOKIE["JL_COLOR_SCHEME"]) ? $_COOKIE["JL_COLOR_SCHEME"] : false;
if ($color_scheme === false) $color_scheme = 'light'; // fallback
// Load the CSS for the correct color-scheme
if ($this->palette() === 'justauto') {
if ($color_scheme === 'dark') {
$stylesheet = $this->assetUrl('css/justblack.min.css');
} else {
$stylesheet = $this->assetUrl('css/justlight.min.css');
}
} else {
$stylesheet = $this->assetUrl('css/' . $this->palette() . '.min.css');
}
return [$stylesheet];
}
/**
* A footer, to be added at the bottom of every page.
*
* @param ServerRequestInterface $request
*
* @return string
*/
public function getFooter(ServerRequestInterface $request): string
{
if (Session::get('theme') === $this->name()) {
return view($this->name() . '::theme/footer-credits', [
'url' => 'https://' . self::AUTHOR_WEBSITE,
'text' => 'Design: ' . self::AUTHOR_WEBSITE,
]);
} else {
return "";
}
}
/**
* {@inheritDoc}
* @see \Fisharebest\Webtrees\Module\ModuleThemeInterface::stylesheets()
* Usage: FanChartModule and Statistics charts
*/
public function parameter($parameter_name)
{
$parameters1 = [
'chart-background-f' => 'fff0f5', // FanChart
'chart-background-m' => 'd7eaf9', // Fanchart
'chart-background-u' => 'f9f9f9', // Fanchart
'chart-box-x' => 260, // unused
'chart-box-y' => 85, // unused
'chart-font-color' => '212529', // FanChart
'chart-spacing-x' => 5, // unused
'chart-spacing-y' => 10, // unused
'compact-chart-box-x' => 240, // unused
'compact-chart-box-y' => 50, // unused
];
if ($this->palette() === 'justblack') {
$parameters2 = [
'distribution-chart-high-values' => 'd86400', // Statistics charts (palette primary color)
'distribution-chart-low-values' => 'ffca66', // Statistics charts (geo chart only, lighten 40%)
'distribution-chart-no-values' => 'dee2e6', // Statistics charts (sass jc-gray-300)
];
} else {
$parameters2 = [
'distribution-chart-high-values' => '337ab7', // Statistics charts (palette primary color)
'distribution-chart-low-values' => '99e0ff', // Statistics charts (geo chart only, lighten 40%)
'distribution-chart-no-values' => 'dee2e6', // Statistics charts ((sass jc-gray-300)
];
}
$parameters = array_merge($parameters1, $parameters2);
return $parameters[$parameter_name];
}
/**
* Generate a list of items for the user menu.
*
* @param Tree|null $tree
*
* @return Menu[]
*/
public function userMenu(?Tree $tree): array
{
if ($this->getPreference('allow-switch')) {
return array_filter([
$this->menuPendingChanges($tree),
$this->menuMyPages($tree),
$this->menuThemes(),
$this->menuPalette(),
$this->menuLanguages(),
$this->menuLogin(),
$this->menuLogout(),
]);
} else {
return parent::userMenu($tree);
}
}
/**
* Create a menu of palette options
*
* @return Menu
*/
protected function menuPalette(): Menu
{
/* I18N: A colour scheme */
$menu = new Menu(I18N::translate('Palette'), '#', 'menu-justlight');
$palette = $this->palette();
foreach ($this->palettes() as $palette_id => $palette_name) {
$url = route('module', ['module' => $this->name(), 'action' => 'Palette', 'palette' => $palette_id]);
if ($palette_id === 'justauto') {
$title = I18N::translate('Automatically chooses a palette based on Windows color mode');
} else {
$title = "";
}
$submenu = new Menu(
$palette_name,
'#',
'menu-justlight-' . $palette_id . ($palette === $palette_id ? ' active' : ''),
[
'data-wt-post-url' => $url,
'title' => $title
]
);
$menu->addSubmenu($submenu);
}
return $menu;
}
/**
* Switch to a new palette
*
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
public function postPaletteAction(ServerRequestInterface $request): ResponseInterface
{
$user = $request->getAttribute('user');
assert($user instanceof UserInterface);
$palette = $request->getQueryParams()['palette'];
assert(array_key_exists($palette, $this->palettes()));
$user->setPreference('justlight-palette', $palette);
Session::put('justlight-palette', $palette);
return response();
}
/**
* @return array<string>
*/
private function palettes(): array
{
$palettes = [
/* I18N: The name of a colour-scheme */
'justlight' => I18N::translate('JustLight'),
/* I18N: The name of a colour-scheme */
'justblack' => I18N::translate('JustBlack'),
/* I18N: The name of a colour-scheme */
'justauto' => I18N::translate('JustAuto')
];
uasort($palettes, I18N::comparator());
return $palettes;
}
/**
* @return string
*/
public function palette(): string
{
// If we are logged in, use our preference
$palette = Auth::user()->getPreference('justlight-palette', '');
// If not logged in or no preference, use one we selected earlier in the session.
if ($palette === '') {
$palette = Session::get('justlight-palette');
$palette = is_string($palette) ? $palette : '';
}
// We haven't selected one this session? Use the site default
if ($palette === '') {
$palette = $this->getPreference('palette', 'justlight');
}
return $palette;
}
/**
* Additional/updated translations.
*
* @param string $language
*
* @return array<string>
*/
public function customTranslations(string $language): array
{
$file = $this->resourcesFolder() . 'lang/' . $language . '.php';
return file_exists($file) ? (new Translation($file))->asArray() : [];
}
};