18d6560c4SGreg Roach<?php 23976b470SGreg Roach 38d6560c4SGreg Roach/** 48d6560c4SGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 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; 246ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 256ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 268d6560c4SGreg Roach 278d6560c4SGreg Roach/** 288d6560c4SGreg Roach * Class CustomCssJsModule - add CSS and JS to every page 298d6560c4SGreg Roach */ 308d6560c4SGreg Roachclass CustomCssJsModule extends AbstractModule implements ModuleConfigInterface, ModuleGlobalInterface 318d6560c4SGreg Roach{ 328d6560c4SGreg Roach use ModuleConfigTrait; 338d6560c4SGreg Roach use ModuleGlobalTrait; 348d6560c4SGreg Roach 358d6560c4SGreg Roach /** 368d6560c4SGreg Roach * A sentence describing what this module does. 378d6560c4SGreg Roach * 388d6560c4SGreg Roach * @return string 398d6560c4SGreg Roach */ 408d6560c4SGreg Roach public function description(): string 418d6560c4SGreg Roach { 428d6560c4SGreg Roach /* I18N: Description of the “CSS and JS” module. */ 438d6560c4SGreg Roach return I18N::translate('Add styling and scripts to every page.'); 448d6560c4SGreg Roach } 458d6560c4SGreg Roach 468d6560c4SGreg Roach /** 478d6560c4SGreg Roach * Should this module be enabled when it is first installed? 488d6560c4SGreg Roach * 498d6560c4SGreg Roach * @return bool 508d6560c4SGreg Roach */ 518d6560c4SGreg Roach public function isEnabledByDefault(): bool 528d6560c4SGreg Roach { 538d6560c4SGreg Roach return false; 548d6560c4SGreg Roach } 558d6560c4SGreg Roach 568d6560c4SGreg Roach /** 578d6560c4SGreg Roach * Show a form to edit the user CSS and JS. 588d6560c4SGreg Roach * 5957ab2231SGreg Roach * @param ServerRequestInterface $request 6057ab2231SGreg Roach * 616ccdf4f0SGreg Roach * @return ResponseInterface 628d6560c4SGreg Roach */ 63*a09ea7ccSGreg Roach public function getAdminAction(/** @scrutinizer ignore-unused */ ServerRequestInterface $request): ResponseInterface 648d6560c4SGreg Roach { 658d6560c4SGreg Roach $this->layout = 'layouts/administration'; 668d6560c4SGreg Roach 678d6560c4SGreg Roach return $this->viewResponse('modules/custom-css-js/edit', [ 688d6560c4SGreg Roach 'title' => $this->title(), 698d6560c4SGreg Roach 'head' => $this->getPreference('head'), 708d6560c4SGreg Roach 'body' => $this->getPreference('body'), 718d6560c4SGreg Roach ]); 728d6560c4SGreg Roach } 738d6560c4SGreg Roach 748d6560c4SGreg Roach /** 758d6560c4SGreg Roach * How should this module be identified in the control panel, etc.? 768d6560c4SGreg Roach * 778d6560c4SGreg Roach * @return string 788d6560c4SGreg Roach */ 798d6560c4SGreg Roach public function title(): string 808d6560c4SGreg Roach { 818d6560c4SGreg Roach /* I18N: Name of a module. */ 828d6560c4SGreg Roach return I18N::translate('CSS and JS'); 838d6560c4SGreg Roach } 848d6560c4SGreg Roach 858d6560c4SGreg Roach /** 868d6560c4SGreg Roach * Save the user CSS and JS. 878d6560c4SGreg Roach * 886ccdf4f0SGreg Roach * @param ServerRequestInterface $request 898d6560c4SGreg Roach * 906ccdf4f0SGreg Roach * @return ResponseInterface 918d6560c4SGreg Roach */ 926ccdf4f0SGreg Roach public function postAdminAction(ServerRequestInterface $request): ResponseInterface 938d6560c4SGreg Roach { 94b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 95b46c87bdSGreg Roach 96b46c87bdSGreg Roach $this->setPreference('body', $params['body']); 97b46c87bdSGreg Roach $this->setPreference('head', $params['head']); 988d6560c4SGreg Roach 998d6560c4SGreg Roach $message = I18N::translate('The preferences for the module “%s” have been updated.', $this->title()); 1008d6560c4SGreg Roach FlashMessages::addMessage($message, 'success'); 1018d6560c4SGreg Roach 1026ccdf4f0SGreg Roach return redirect($this->getConfigLink()); 1038d6560c4SGreg Roach } 1048d6560c4SGreg Roach 1058d6560c4SGreg Roach /** 1068d6560c4SGreg Roach * Raw content, to be added at the end of the <body> element. 1078d6560c4SGreg Roach * Typically, this will be <script> elements. 1088d6560c4SGreg Roach * 1098d6560c4SGreg Roach * @return string 1108d6560c4SGreg Roach */ 1118d6560c4SGreg Roach public function bodyContent(): string 1128d6560c4SGreg Roach { 1138d6560c4SGreg Roach return $this->getPreference('body'); 1148d6560c4SGreg Roach } 1158d6560c4SGreg Roach 1168d6560c4SGreg Roach /** 1178d6560c4SGreg Roach * Raw content, to be added at the end of the <head> element. 1188d6560c4SGreg Roach * Typically, this will be <link> and <meta> elements. 1198d6560c4SGreg Roach * 1208d6560c4SGreg Roach * @return string 1218d6560c4SGreg Roach */ 1228d6560c4SGreg Roach public function headContent(): string 1238d6560c4SGreg Roach { 1248d6560c4SGreg Roach return $this->getPreference('head'); 1258d6560c4SGreg Roach } 1268d6560c4SGreg Roach} 127