xref: /webtrees/app/Module/CustomCssJsModule.php (revision 748dbe155a6d19d66918ad136947fa23ee8f8469)
18d6560c4SGreg Roach<?php
23976b470SGreg Roach
38d6560c4SGreg Roach/**
48d6560c4SGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
68d6560c4SGreg Roach * This program is free software: you can redistribute it and/or modify
78d6560c4SGreg Roach * it under the terms of the GNU General Public License as published by
88d6560c4SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98d6560c4SGreg Roach * (at your option) any later version.
108d6560c4SGreg Roach * This program is distributed in the hope that it will be useful,
118d6560c4SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128d6560c4SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138d6560c4SGreg Roach * GNU General Public License for more details.
148d6560c4SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168d6560c4SGreg Roach */
17fcfa147eSGreg Roach
188d6560c4SGreg Roachdeclare(strict_types=1);
198d6560c4SGreg Roach
208d6560c4SGreg Roachnamespace Fisharebest\Webtrees\Module;
218d6560c4SGreg Roach
228d6560c4SGreg Roachuse Fisharebest\Webtrees\FlashMessages;
238d6560c4SGreg Roachuse Fisharebest\Webtrees\I18N;
24*748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator;
256ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
266ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
278d6560c4SGreg Roach
288d6560c4SGreg Roach/**
298d6560c4SGreg Roach * Class CustomCssJsModule - add CSS and JS to every page
308d6560c4SGreg Roach */
318d6560c4SGreg Roachclass CustomCssJsModule extends AbstractModule implements ModuleConfigInterface, ModuleGlobalInterface
328d6560c4SGreg Roach{
338d6560c4SGreg Roach    use ModuleConfigTrait;
348d6560c4SGreg Roach    use ModuleGlobalTrait;
358d6560c4SGreg Roach
368d6560c4SGreg Roach    /**
378d6560c4SGreg Roach     * A sentence describing what this module does.
388d6560c4SGreg Roach     *
398d6560c4SGreg Roach     * @return string
408d6560c4SGreg Roach     */
418d6560c4SGreg Roach    public function description(): string
428d6560c4SGreg Roach    {
438d6560c4SGreg Roach        /* I18N: Description of the “CSS and JS” module. */
448d6560c4SGreg Roach        return I18N::translate('Add styling and scripts to every page.');
458d6560c4SGreg Roach    }
468d6560c4SGreg Roach
478d6560c4SGreg Roach    /**
488d6560c4SGreg Roach     * Should this module be enabled when it is first installed?
498d6560c4SGreg Roach     *
508d6560c4SGreg Roach     * @return bool
518d6560c4SGreg Roach     */
528d6560c4SGreg Roach    public function isEnabledByDefault(): bool
538d6560c4SGreg Roach    {
548d6560c4SGreg Roach        return false;
558d6560c4SGreg Roach    }
568d6560c4SGreg Roach
578d6560c4SGreg Roach    /**
588d6560c4SGreg Roach     * Show a form to edit the user CSS and JS.
598d6560c4SGreg Roach     *
6057ab2231SGreg Roach     * @param ServerRequestInterface $request
6157ab2231SGreg Roach     *
626ccdf4f0SGreg Roach     * @return ResponseInterface
638d6560c4SGreg Roach     */
64a09ea7ccSGreg Roach    public function getAdminAction(/** @scrutinizer ignore-unused */ ServerRequestInterface $request): ResponseInterface
658d6560c4SGreg Roach    {
668d6560c4SGreg Roach        $this->layout = 'layouts/administration';
678d6560c4SGreg Roach
688d6560c4SGreg Roach        return $this->viewResponse('modules/custom-css-js/edit', [
698d6560c4SGreg Roach            'title' => $this->title(),
708d6560c4SGreg Roach            'head'  => $this->getPreference('head'),
718d6560c4SGreg Roach            'body'  => $this->getPreference('body'),
728d6560c4SGreg Roach        ]);
738d6560c4SGreg Roach    }
748d6560c4SGreg Roach
758d6560c4SGreg Roach    /**
768d6560c4SGreg Roach     * How should this module be identified in the control panel, etc.?
778d6560c4SGreg Roach     *
788d6560c4SGreg Roach     * @return string
798d6560c4SGreg Roach     */
808d6560c4SGreg Roach    public function title(): string
818d6560c4SGreg Roach    {
828d6560c4SGreg Roach        /* I18N: Name of a module. */
838d6560c4SGreg Roach        return I18N::translate('CSS and JS');
848d6560c4SGreg Roach    }
858d6560c4SGreg Roach
868d6560c4SGreg Roach    /**
878d6560c4SGreg Roach     * Save the user CSS and JS.
888d6560c4SGreg Roach     *
896ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
908d6560c4SGreg Roach     *
916ccdf4f0SGreg Roach     * @return ResponseInterface
928d6560c4SGreg Roach     */
936ccdf4f0SGreg Roach    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
948d6560c4SGreg Roach    {
95*748dbe15SGreg Roach        $body = Validator::parsedBody($request)->string('body');
96*748dbe15SGreg Roach        $head = Validator::parsedBody($request)->string('head');
97b46c87bdSGreg Roach
98*748dbe15SGreg Roach        $this->setPreference('body', $body);
99*748dbe15SGreg Roach        $this->setPreference('head', $head);
1008d6560c4SGreg Roach
1018d6560c4SGreg Roach        $message = I18N::translate('The preferences for the module “%s” have been updated.', $this->title());
1028d6560c4SGreg Roach        FlashMessages::addMessage($message, 'success');
1038d6560c4SGreg Roach
1046ccdf4f0SGreg Roach        return redirect($this->getConfigLink());
1058d6560c4SGreg Roach    }
1068d6560c4SGreg Roach
1078d6560c4SGreg Roach    /**
1088d6560c4SGreg Roach     * Raw content, to be added at the end of the <body> element.
1098d6560c4SGreg Roach     * Typically, this will be <script> elements.
1108d6560c4SGreg Roach     *
1118d6560c4SGreg Roach     * @return string
1128d6560c4SGreg Roach     */
1138d6560c4SGreg Roach    public function bodyContent(): string
1148d6560c4SGreg Roach    {
1158d6560c4SGreg Roach        return $this->getPreference('body');
1168d6560c4SGreg Roach    }
1178d6560c4SGreg Roach
1188d6560c4SGreg Roach    /**
1198d6560c4SGreg Roach     * Raw content, to be added at the end of the <head> element.
1208d6560c4SGreg Roach     * Typically, this will be <link> and <meta> elements.
1218d6560c4SGreg Roach     *
1228d6560c4SGreg Roach     * @return string
1238d6560c4SGreg Roach     */
1248d6560c4SGreg Roach    public function headContent(): string
1258d6560c4SGreg Roach    {
1268d6560c4SGreg Roach        return $this->getPreference('head');
1278d6560c4SGreg Roach    }
1288d6560c4SGreg Roach}
129