xref: /webtrees/app/Module/CustomCssJsModule.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\FlashMessages;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Validator;
25use Psr\Http\Message\ResponseInterface;
26use Psr\Http\Message\ServerRequestInterface;
27
28/**
29 * Class CustomCssJsModule - add CSS and JS to every page
30 */
31class CustomCssJsModule extends AbstractModule implements ModuleConfigInterface, ModuleGlobalInterface
32{
33    use ModuleConfigTrait;
34    use ModuleGlobalTrait;
35
36    public function description(): string
37    {
38        /* I18N: Description of the “CSS and JS” module. */
39        return I18N::translate('Add styling and scripts to every page.');
40    }
41
42    /**
43     * Should this module be enabled when it is first installed?
44     *
45     * @return bool
46     */
47    public function isEnabledByDefault(): bool
48    {
49        return false;
50    }
51
52    /**
53     * Show a form to edit the user CSS and JS.
54     *
55     * @param ServerRequestInterface $request
56     *
57     * @return ResponseInterface
58     */
59    public function getAdminAction(ServerRequestInterface $request): ResponseInterface
60    {
61        $this->layout = 'layouts/administration';
62
63        return $this->viewResponse('modules/custom-css-js/edit', [
64            'title' => $this->title(),
65            'head'  => $this->getPreference('head'),
66            'body'  => $this->getPreference('body'),
67        ]);
68    }
69
70    /**
71     * How should this module be identified in the control panel, etc.?
72     *
73     * @return string
74     */
75    public function title(): string
76    {
77        /* I18N: Name of a module. */
78        return I18N::translate('CSS and JS');
79    }
80
81    /**
82     * Save the user CSS and JS.
83     *
84     * @param ServerRequestInterface $request
85     *
86     * @return ResponseInterface
87     */
88    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
89    {
90        $body = Validator::parsedBody($request)->string('body');
91        $head = Validator::parsedBody($request)->string('head');
92
93        $this->setPreference('body', $body);
94        $this->setPreference('head', $head);
95
96        $message = I18N::translate('The preferences for the module “%s” have been updated.', $this->title());
97        FlashMessages::addMessage($message, 'success');
98
99        return redirect($this->getConfigLink());
100    }
101
102    /**
103     * Raw content, to be added at the end of the <body> element.
104     * Typically, this will be <script> elements.
105     *
106     * @return string
107     */
108    public function bodyContent(): string
109    {
110        return $this->getPreference('body');
111    }
112
113    /**
114     * Raw content, to be added at the end of the <head> element.
115     * Typically, this will be <link> and <meta> elements.
116     *
117     * @return string
118     */
119    public function headContent(): string
120    {
121        return $this->getPreference('head');
122    }
123}
124