xref: /webtrees/app/Module/CustomCssJsModule.php (revision ac5f8ed16774e57c5f89faca528d55bc4a3971e1)
18d6560c4SGreg Roach<?php
28d6560c4SGreg Roach/**
38d6560c4SGreg Roach * webtrees: online genealogy
48d6560c4SGreg Roach * Copyright (C) 2019 webtrees development team
58d6560c4SGreg Roach * This program is free software: you can redistribute it and/or modify
68d6560c4SGreg Roach * it under the terms of the GNU General Public License as published by
78d6560c4SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88d6560c4SGreg Roach * (at your option) any later version.
98d6560c4SGreg Roach * This program is distributed in the hope that it will be useful,
108d6560c4SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118d6560c4SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128d6560c4SGreg Roach * GNU General Public License for more details.
138d6560c4SGreg Roach * You should have received a copy of the GNU General Public License
148d6560c4SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158d6560c4SGreg Roach */
168d6560c4SGreg Roachdeclare(strict_types=1);
178d6560c4SGreg Roach
188d6560c4SGreg Roachnamespace Fisharebest\Webtrees\Module;
198d6560c4SGreg Roach
208d6560c4SGreg Roachuse Fisharebest\Webtrees\FlashMessages;
218d6560c4SGreg Roachuse Fisharebest\Webtrees\I18N;
226ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
236ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
248d6560c4SGreg Roach
258d6560c4SGreg Roach/**
268d6560c4SGreg Roach * Class CustomCssJsModule - add CSS and JS to every page
278d6560c4SGreg Roach */
288d6560c4SGreg Roachclass CustomCssJsModule extends AbstractModule implements ModuleConfigInterface, ModuleGlobalInterface
298d6560c4SGreg Roach{
308d6560c4SGreg Roach    use ModuleConfigTrait;
318d6560c4SGreg Roach    use ModuleGlobalTrait;
328d6560c4SGreg Roach
338d6560c4SGreg Roach    /**
348d6560c4SGreg Roach     * A sentence describing what this module does.
358d6560c4SGreg Roach     *
368d6560c4SGreg Roach     * @return string
378d6560c4SGreg Roach     */
388d6560c4SGreg Roach    public function description(): string
398d6560c4SGreg Roach    {
408d6560c4SGreg Roach        /* I18N: Description of the “CSS and JS” module. */
418d6560c4SGreg Roach        return I18N::translate('Add styling and scripts to every page.');
428d6560c4SGreg Roach    }
438d6560c4SGreg Roach
448d6560c4SGreg Roach    /**
458d6560c4SGreg Roach     * Should this module be enabled when it is first installed?
468d6560c4SGreg Roach     *
478d6560c4SGreg Roach     * @return bool
488d6560c4SGreg Roach     */
498d6560c4SGreg Roach    public function isEnabledByDefault(): bool
508d6560c4SGreg Roach    {
518d6560c4SGreg Roach        return false;
528d6560c4SGreg Roach    }
538d6560c4SGreg Roach
548d6560c4SGreg Roach    /**
558d6560c4SGreg Roach     * Show a form to edit the user CSS and JS.
568d6560c4SGreg Roach     *
576ccdf4f0SGreg Roach     * @return ResponseInterface
588d6560c4SGreg Roach     */
596ccdf4f0SGreg Roach    public function getAdminAction(): 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    {
90*ac5f8ed1SGreg Roach        $this->setPreference('body', $request->getParsedBody()['body']);
91*ac5f8ed1SGreg Roach        $this->setPreference('head', $request->getParsedBody()['head']);
928d6560c4SGreg Roach
938d6560c4SGreg Roach        $message = I18N::translate('The preferences for the module “%s” have been updated.', $this->title());
948d6560c4SGreg Roach        FlashMessages::addMessage($message, 'success');
958d6560c4SGreg Roach
966ccdf4f0SGreg Roach        return redirect($this->getConfigLink());
978d6560c4SGreg Roach    }
988d6560c4SGreg Roach
998d6560c4SGreg Roach    /**
1008d6560c4SGreg Roach     * Raw content, to be added at the end of the <body> element.
1018d6560c4SGreg Roach     * Typically, this will be <script> elements.
1028d6560c4SGreg Roach     *
1038d6560c4SGreg Roach     * @return string
1048d6560c4SGreg Roach     */
1058d6560c4SGreg Roach    public function bodyContent(): string
1068d6560c4SGreg Roach    {
1078d6560c4SGreg Roach        return $this->getPreference('body');
1088d6560c4SGreg Roach    }
1098d6560c4SGreg Roach
1108d6560c4SGreg Roach    /**
1118d6560c4SGreg Roach     * Raw content, to be added at the end of the <head> element.
1128d6560c4SGreg Roach     * Typically, this will be <link> and <meta> elements.
1138d6560c4SGreg Roach     *
1148d6560c4SGreg Roach     * @return string
1158d6560c4SGreg Roach     */
1168d6560c4SGreg Roach    public function headContent(): string
1178d6560c4SGreg Roach    {
1188d6560c4SGreg Roach        return $this->getPreference('head');
1198d6560c4SGreg Roach    }
1208d6560c4SGreg Roach}
121