xref: /webtrees/app/Module/CustomCssJsModule.php (revision 9b152ff9230017d2c03aa1bf603a98b18250446d)
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 */
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        $this->setPreference('body', $request->getParsedBody()['body']);
95        $this->setPreference('head', $request->getParsedBody()['head']);
96
97        $message = I18N::translate('The preferences for the module “%s” have been updated.', $this->title());
98        FlashMessages::addMessage($message, 'success');
99
100        return redirect($this->getConfigLink());
101    }
102
103    /**
104     * Raw content, to be added at the end of the <body> element.
105     * Typically, this will be <script> elements.
106     *
107     * @return string
108     */
109    public function bodyContent(): string
110    {
111        return $this->getPreference('body');
112    }
113
114    /**
115     * Raw content, to be added at the end of the <head> element.
116     * Typically, this will be <link> and <meta> elements.
117     *
118     * @return string
119     */
120    public function headContent(): string
121    {
122        return $this->getPreference('head');
123    }
124}
125