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