xref: /webtrees/app/Module/CustomCssJsModule.php (revision 2210ec835179836edcccab2d4e4acfcc5f6b4f74)
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    /**
37     * A sentence describing what this module does.
38     *
39     * @return string
40     */
41    public function description(): string
42    {
43        /* I18N: Description of the “CSS and JS” module. */
44        return I18N::translate('Add styling and scripts to every page.');
45    }
46
47    /**
48     * Should this module be enabled when it is first installed?
49     *
50     * @return bool
51     */
52    public function isEnabledByDefault(): bool
53    {
54        return false;
55    }
56
57    /**
58     * Show a form to edit the user CSS and JS.
59     *
60     * @param ServerRequestInterface $request
61     *
62     * @return ResponseInterface
63     */
64    public function getAdminAction(ServerRequestInterface $request): ResponseInterface
65    {
66        $this->layout = 'layouts/administration';
67
68        return $this->viewResponse('modules/custom-css-js/edit', [
69            'title' => $this->title(),
70            'head'  => $this->getPreference('head'),
71            'body'  => $this->getPreference('body'),
72        ]);
73    }
74
75    /**
76     * How should this module be identified in the control panel, etc.?
77     *
78     * @return string
79     */
80    public function title(): string
81    {
82        /* I18N: Name of a module. */
83        return I18N::translate('CSS and JS');
84    }
85
86    /**
87     * Save the user CSS and JS.
88     *
89     * @param ServerRequestInterface $request
90     *
91     * @return ResponseInterface
92     */
93    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
94    {
95        $body = Validator::parsedBody($request)->string('body');
96        $head = Validator::parsedBody($request)->string('head');
97
98        $this->setPreference('body', $body);
99        $this->setPreference('head', $head);
100
101        $message = I18N::translate('The preferences for the module “%s” have been updated.', $this->title());
102        FlashMessages::addMessage($message, 'success');
103
104        return redirect($this->getConfigLink());
105    }
106
107    /**
108     * Raw content, to be added at the end of the <body> element.
109     * Typically, this will be <script> elements.
110     *
111     * @return string
112     */
113    public function bodyContent(): string
114    {
115        return $this->getPreference('body');
116    }
117
118    /**
119     * Raw content, to be added at the end of the <head> element.
120     * Typically, this will be <link> and <meta> elements.
121     *
122     * @return string
123     */
124    public function headContent(): string
125    {
126        return $this->getPreference('head');
127    }
128}
129