1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\Webtrees\I18N; 23use Fisharebest\Webtrees\Registry; 24use Fisharebest\Webtrees\Services\HtmlService; 25use Fisharebest\Webtrees\Statistics; 26use Fisharebest\Webtrees\Tree; 27use Fisharebest\Webtrees\Validator; 28use Illuminate\Support\Str; 29use Psr\Http\Message\ServerRequestInterface; 30 31use function in_array; 32use function time; 33 34/** 35 * Class HtmlBlockModule 36 */ 37class HtmlBlockModule extends AbstractModule implements ModuleBlockInterface 38{ 39 use ModuleBlockTrait; 40 41 private HtmlService $html_service; 42 43 /** 44 * HtmlBlockModule bootstrap. 45 * 46 * @param HtmlService $html_service 47 */ 48 public function __construct(HtmlService $html_service) 49 { 50 $this->html_service = $html_service; 51 } 52 53 /** 54 * How should this module be identified in the control panel, etc.? 55 * 56 * @return string 57 */ 58 public function title(): string 59 { 60 /* I18N: Name of a module */ 61 return I18N::translate('HTML'); 62 } 63 64 public function description(): string 65 { 66 /* I18N: Description of the “HTML” module */ 67 return I18N::translate('Add your own text and graphics.'); 68 } 69 70 /** 71 * Generate the HTML content of this block. 72 * 73 * @param Tree $tree 74 * @param int $block_id 75 * @param string $context 76 * @param array<string,string> $config 77 * 78 * @return string 79 */ 80 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 81 { 82 $statistics = Registry::container()->get(Statistics::class); 83 84 $title = $this->getBlockSetting($block_id, 'title'); 85 $content = $this->getBlockSetting($block_id, 'html'); 86 $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp'); 87 $languages = $this->getBlockSetting($block_id, 'languages'); 88 89 // Only show this block for certain languages 90 if ($languages && !in_array(I18N::languageTag(), explode(',', $languages), true)) { 91 return ''; 92 } 93 94 // Retrieve text, process embedded variables 95 $title = $statistics->embedTags($title); 96 $content = $statistics->embedTags($content); 97 98 $block_timestamp = (int) $this->getBlockSetting($block_id, 'timestamp', (string) time()); 99 100 if ($show_timestamp === '1') { 101 $content .= '<br>' . view('components/datetime', ['timestamp' => Registry::timestampFactory()->make($block_timestamp)]); 102 } 103 104 if ($context !== self::CONTEXT_EMBED) { 105 return view('modules/block-template', [ 106 'block' => Str::kebab($this->name()), 107 'id' => $block_id, 108 'config_url' => $this->configUrl($tree, $context, $block_id), 109 'title' => e(strip_tags($title)), 110 'content' => $content, 111 ]); 112 } 113 114 return $content; 115 } 116 117 /** 118 * Should this block load asynchronously using AJAX? 119 * 120 * Simple blocks are faster in-line, more complex ones can be loaded later. 121 * 122 * @return bool 123 */ 124 public function loadAjax(): bool 125 { 126 return false; 127 } 128 129 /** 130 * Can this block be shown on the user’s home page? 131 * 132 * @return bool 133 */ 134 public function isUserBlock(): bool 135 { 136 return true; 137 } 138 139 /** 140 * Can this block be shown on the tree’s home page? 141 * 142 * @return bool 143 */ 144 public function isTreeBlock(): bool 145 { 146 return true; 147 } 148 149 /** 150 * Update the configuration for a block. 151 * 152 * @param ServerRequestInterface $request 153 * @param int $block_id 154 * 155 * @return void 156 */ 157 public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 158 { 159 $title = Validator::parsedBody($request)->string('title'); 160 $html = Validator::parsedBody($request)->string('html'); 161 $show_timestamp = Validator::parsedBody($request)->boolean('show_timestamp'); 162 $languages = Validator::parsedBody($request)->array('languages'); 163 164 $this->setBlockSetting($block_id, 'title', $title); 165 $this->setBlockSetting($block_id, 'html', $this->html_service->sanitize($html)); 166 $this->setBlockSetting($block_id, 'show_timestamp', (string) $show_timestamp); 167 $this->setBlockSetting($block_id, 'timestamp', (string) time()); 168 $this->setBlockSetting($block_id, 'languages', implode(',', $languages)); 169 } 170 171 /** 172 * An HTML form to edit block settings 173 * 174 * @param Tree $tree 175 * @param int $block_id 176 * 177 * @return string 178 */ 179 public function editBlockConfiguration(Tree $tree, int $block_id): string 180 { 181 $title = $this->getBlockSetting($block_id, 'title'); 182 $html = $this->getBlockSetting($block_id, 'html'); 183 $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0'); 184 $languages = explode(',', $this->getBlockSetting($block_id, 'languages')); 185 186 $templates = [ 187 $html => I18N::translate('Custom'), 188 view('modules/html/template-keywords') => I18N::translate('Keyword examples'), 189 view('modules/html/template-narrative') => I18N::translate('Narrative description'), 190 view('modules/html/template-statistics', ['tree' => $tree]) => I18N::translate('Statistics'), 191 ]; 192 193 return view('modules/html/config', [ 194 'html' => $html, 195 'languages' => $languages, 196 'show_timestamp' => $show_timestamp, 197 'templates' => $templates, 198 'title' => $title, 199 ]); 200 } 201} 202