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 /** 65 * A sentence describing what this module does. 66 * 67 * @return string 68 */ 69 public function description(): string 70 { 71 /* I18N: Description of the “HTML” module */ 72 return I18N::translate('Add your own text and graphics.'); 73 } 74 75 /** 76 * Generate the HTML content of this block. 77 * 78 * @param Tree $tree 79 * @param int $block_id 80 * @param string $context 81 * @param array<string,string> $config 82 * 83 * @return string 84 */ 85 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 86 { 87 $statistics = Registry::container()->get(Statistics::class); 88 89 $title = $this->getBlockSetting($block_id, 'title'); 90 $content = $this->getBlockSetting($block_id, 'html'); 91 $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp'); 92 $languages = $this->getBlockSetting($block_id, 'languages'); 93 94 // Only show this block for certain languages 95 if ($languages && !in_array(I18N::languageTag(), explode(',', $languages), true)) { 96 return ''; 97 } 98 99 // Retrieve text, process embedded variables 100 $title = $statistics->embedTags($title); 101 $content = $statistics->embedTags($content); 102 103 $block_timestamp = (int) $this->getBlockSetting($block_id, 'timestamp', (string) time()); 104 105 if ($show_timestamp === '1') { 106 $content .= '<br>' . view('components/datetime', ['timestamp' => Registry::timestampFactory()->make($block_timestamp)]); 107 } 108 109 if ($context !== self::CONTEXT_EMBED) { 110 return view('modules/block-template', [ 111 'block' => Str::kebab($this->name()), 112 'id' => $block_id, 113 'config_url' => $this->configUrl($tree, $context, $block_id), 114 'title' => e(strip_tags($title)), 115 'content' => $content, 116 ]); 117 } 118 119 return $content; 120 } 121 122 /** 123 * Should this block load asynchronously using AJAX? 124 * 125 * Simple blocks are faster in-line, more complex ones can be loaded later. 126 * 127 * @return bool 128 */ 129 public function loadAjax(): bool 130 { 131 return false; 132 } 133 134 /** 135 * Can this block be shown on the user’s home page? 136 * 137 * @return bool 138 */ 139 public function isUserBlock(): bool 140 { 141 return true; 142 } 143 144 /** 145 * Can this block be shown on the tree’s home page? 146 * 147 * @return bool 148 */ 149 public function isTreeBlock(): bool 150 { 151 return true; 152 } 153 154 /** 155 * Update the configuration for a block. 156 * 157 * @param ServerRequestInterface $request 158 * @param int $block_id 159 * 160 * @return void 161 */ 162 public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 163 { 164 $title = Validator::parsedBody($request)->string('title'); 165 $html = Validator::parsedBody($request)->string('html'); 166 $show_timestamp = Validator::parsedBody($request)->boolean('show_timestamp'); 167 $languages = Validator::parsedBody($request)->array('languages'); 168 169 $this->setBlockSetting($block_id, 'title', $title); 170 $this->setBlockSetting($block_id, 'html', $this->html_service->sanitize($html)); 171 $this->setBlockSetting($block_id, 'show_timestamp', (string) $show_timestamp); 172 $this->setBlockSetting($block_id, 'timestamp', (string) time()); 173 $this->setBlockSetting($block_id, 'languages', implode(',', $languages)); 174 } 175 176 /** 177 * An HTML form to edit block settings 178 * 179 * @param Tree $tree 180 * @param int $block_id 181 * 182 * @return string 183 */ 184 public function editBlockConfiguration(Tree $tree, int $block_id): string 185 { 186 $title = $this->getBlockSetting($block_id, 'title'); 187 $html = $this->getBlockSetting($block_id, 'html'); 188 $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0'); 189 $languages = explode(',', $this->getBlockSetting($block_id, 'languages')); 190 191 $templates = [ 192 $html => I18N::translate('Custom'), 193 view('modules/html/template-keywords') => I18N::translate('Keyword examples'), 194 view('modules/html/template-narrative') => I18N::translate('Narrative description'), 195 view('modules/html/template-statistics', ['tree' => $tree]) => I18N::translate('Statistics'), 196 ]; 197 198 return view('modules/html/config', [ 199 'html' => $html, 200 'languages' => $languages, 201 'show_timestamp' => $show_timestamp, 202 'templates' => $templates, 203 'title' => $title, 204 ]); 205 } 206} 207