xref: /webtrees/app/Module/CustomCssJsModule.php (revision fd6c003f26d8770d21ea893811f0fc20a190c323)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Module;
20
21use Fisharebest\Webtrees\FlashMessages;
22use Fisharebest\Webtrees\I18N;
23use Psr\Http\Message\ResponseInterface;
24use Psr\Http\Message\ServerRequestInterface;
25
26/**
27 * Class CustomCssJsModule - add CSS and JS to every page
28 */
29class CustomCssJsModule extends AbstractModule implements ModuleConfigInterface, ModuleGlobalInterface
30{
31    use ModuleConfigTrait;
32    use ModuleGlobalTrait;
33
34    /**
35     * A sentence describing what this module does.
36     *
37     * @return string
38     */
39    public function description(): string
40    {
41        /* I18N: Description of the “CSS and JS” module. */
42        return I18N::translate('Add styling and scripts to every page.');
43    }
44
45    /**
46     * Should this module be enabled when it is first installed?
47     *
48     * @return bool
49     */
50    public function isEnabledByDefault(): bool
51    {
52        return false;
53    }
54
55    /**
56     * Show a form to edit the user CSS and JS.
57     *
58     * @param ServerRequestInterface $request
59     *
60     * @return ResponseInterface
61     */
62    public function getAdminAction(ServerRequestInterface $request): ResponseInterface
63    {
64        $this->layout = 'layouts/administration';
65
66        return $this->viewResponse('modules/custom-css-js/edit', [
67            'title' => $this->title(),
68            'head'  => $this->getPreference('head'),
69            'body'  => $this->getPreference('body'),
70        ]);
71    }
72
73    /**
74     * How should this module be identified in the control panel, etc.?
75     *
76     * @return string
77     */
78    public function title(): string
79    {
80        /* I18N: Name of a module. */
81        return I18N::translate('CSS and JS');
82    }
83
84    /**
85     * Save the user CSS and JS.
86     *
87     * @param ServerRequestInterface $request
88     *
89     * @return ResponseInterface
90     */
91    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
92    {
93        $this->setPreference('body', $request->getParsedBody()['body']);
94        $this->setPreference('head', $request->getParsedBody()['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