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