xref: /webtrees/app/Module/CustomCssJsModule.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
18d6560c4SGreg Roach<?php
23976b470SGreg Roach
38d6560c4SGreg Roach/**
48d6560c4SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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;
24748dbe15SGreg 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    public function description(): string
378d6560c4SGreg Roach    {
388d6560c4SGreg Roach        /* I18N: Description of the “CSS and JS” module. */
398d6560c4SGreg Roach        return I18N::translate('Add styling and scripts to every page.');
408d6560c4SGreg Roach    }
418d6560c4SGreg Roach
428d6560c4SGreg Roach    /**
438d6560c4SGreg Roach     * Should this module be enabled when it is first installed?
448d6560c4SGreg Roach     *
458d6560c4SGreg Roach     * @return bool
468d6560c4SGreg Roach     */
478d6560c4SGreg Roach    public function isEnabledByDefault(): bool
488d6560c4SGreg Roach    {
498d6560c4SGreg Roach        return false;
508d6560c4SGreg Roach    }
518d6560c4SGreg Roach
528d6560c4SGreg Roach    /**
538d6560c4SGreg Roach     * Show a form to edit the user CSS and JS.
548d6560c4SGreg Roach     *
5557ab2231SGreg Roach     * @param ServerRequestInterface $request
5657ab2231SGreg Roach     *
576ccdf4f0SGreg Roach     * @return ResponseInterface
588d6560c4SGreg Roach     */
5949528f2bSGreg Roach    public function getAdminAction(ServerRequestInterface $request): ResponseInterface
608d6560c4SGreg Roach    {
618d6560c4SGreg Roach        $this->layout = 'layouts/administration';
628d6560c4SGreg Roach
638d6560c4SGreg Roach        return $this->viewResponse('modules/custom-css-js/edit', [
648d6560c4SGreg Roach            'title' => $this->title(),
658d6560c4SGreg Roach            'head'  => $this->getPreference('head'),
668d6560c4SGreg Roach            'body'  => $this->getPreference('body'),
678d6560c4SGreg Roach        ]);
688d6560c4SGreg Roach    }
698d6560c4SGreg Roach
708d6560c4SGreg Roach    /**
718d6560c4SGreg Roach     * How should this module be identified in the control panel, etc.?
728d6560c4SGreg Roach     *
738d6560c4SGreg Roach     * @return string
748d6560c4SGreg Roach     */
758d6560c4SGreg Roach    public function title(): string
768d6560c4SGreg Roach    {
778d6560c4SGreg Roach        /* I18N: Name of a module. */
788d6560c4SGreg Roach        return I18N::translate('CSS and JS');
798d6560c4SGreg Roach    }
808d6560c4SGreg Roach
818d6560c4SGreg Roach    /**
828d6560c4SGreg Roach     * Save the user CSS and JS.
838d6560c4SGreg Roach     *
846ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
858d6560c4SGreg Roach     *
866ccdf4f0SGreg Roach     * @return ResponseInterface
878d6560c4SGreg Roach     */
886ccdf4f0SGreg Roach    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
898d6560c4SGreg Roach    {
90748dbe15SGreg Roach        $body = Validator::parsedBody($request)->string('body');
91748dbe15SGreg Roach        $head = Validator::parsedBody($request)->string('head');
92b46c87bdSGreg Roach
93748dbe15SGreg Roach        $this->setPreference('body', $body);
94748dbe15SGreg Roach        $this->setPreference('head', $head);
958d6560c4SGreg Roach
968d6560c4SGreg Roach        $message = I18N::translate('The preferences for the module “%s” have been updated.', $this->title());
978d6560c4SGreg Roach        FlashMessages::addMessage($message, 'success');
988d6560c4SGreg Roach
996ccdf4f0SGreg Roach        return redirect($this->getConfigLink());
1008d6560c4SGreg Roach    }
1018d6560c4SGreg Roach
1028d6560c4SGreg Roach    /**
1038d6560c4SGreg Roach     * Raw content, to be added at the end of the <body> element.
1048d6560c4SGreg Roach     * Typically, this will be <script> elements.
1058d6560c4SGreg Roach     *
1068d6560c4SGreg Roach     * @return string
1078d6560c4SGreg Roach     */
1088d6560c4SGreg Roach    public function bodyContent(): string
1098d6560c4SGreg Roach    {
1108d6560c4SGreg Roach        return $this->getPreference('body');
1118d6560c4SGreg Roach    }
1128d6560c4SGreg Roach
1138d6560c4SGreg Roach    /**
1148d6560c4SGreg Roach     * Raw content, to be added at the end of the <head> element.
1158d6560c4SGreg Roach     * Typically, this will be <link> and <meta> elements.
1168d6560c4SGreg Roach     *
1178d6560c4SGreg Roach     * @return string
1188d6560c4SGreg Roach     */
1198d6560c4SGreg Roach    public function headContent(): string
1208d6560c4SGreg Roach    {
1218d6560c4SGreg Roach        return $this->getPreference('head');
1228d6560c4SGreg Roach    }
1238d6560c4SGreg Roach}
124