xref: /webtrees/app/Module/CustomCssJsModule.php (revision 02086832f6c85aab8102c1a336d227314e040441)
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 Symfony\Component\HttpFoundation\RedirectResponse;
23use Symfony\Component\HttpFoundation\Request;
24use Symfony\Component\HttpFoundation\Response;
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     * @return Response
59     */
60    public function getAdminAction(): Response
61    {
62        $this->layout = 'layouts/administration';
63
64        return $this->viewResponse('modules/custom-css-js/edit', [
65            'title' => $this->title(),
66            'head'  => $this->getPreference('head'),
67            'body'  => $this->getPreference('body'),
68        ]);
69    }
70
71    /**
72     * How should this module be identified in the control panel, etc.?
73     *
74     * @return string
75     */
76    public function title(): string
77    {
78        /* I18N: Name of a module. */
79        return I18N::translate('CSS and JS');
80    }
81
82    /**
83     * Save the user CSS and JS.
84     *
85     * @param Request $request
86     *
87     * @return RedirectResponse
88     */
89    public function postAdminAction(Request $request): RedirectResponse
90    {
91        $this->setPreference('body', $request->get('body', ''));
92        $this->setPreference('head', $request->get('head', ''));
93
94        $message = I18N::translate('The preferences for the module “%s” have been updated.', $this->title());
95        FlashMessages::addMessage($message, 'success');
96
97        return new RedirectResponse($this->getConfigLink());
98    }
99
100    /**
101     * Raw content, to be added at the end of the <body> element.
102     * Typically, this will be <script> elements.
103     *
104     * @return string
105     */
106    public function bodyContent(): string
107    {
108        return $this->getPreference('body');
109    }
110
111    /**
112     * Raw content, to be added at the end of the <head> element.
113     * Typically, this will be <link> and <meta> elements.
114     *
115     * @return string
116     */
117    public function headContent(): string
118    {
119        return $this->getPreference('head');
120    }
121}
122