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