18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 55bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team 68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 98c2e8227SGreg Roach * (at your option) any later version. 108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 138c2e8227SGreg Roach * GNU General Public License for more details. 148c2e8227SGreg 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/>. 168c2e8227SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 2176692c8bSGreg Roach 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 23d97083feSGreg Roachuse Fisharebest\Webtrees\Registry; 2450d6f48cSGreg Roachuse Fisharebest\Webtrees\Services\HtmlService; 259219296aSGreg Roachuse Fisharebest\Webtrees\Statistics; 260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree; 27*4c718a92SGreg Roachuse Fisharebest\Webtrees\Validator; 281e7a7a28SGreg Roachuse Illuminate\Support\Str; 296ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 308c2e8227SGreg Roach 31d97083feSGreg Roachuse function time; 32d97083feSGreg Roach 338c2e8227SGreg Roach/** 348c2e8227SGreg Roach * Class HtmlBlockModule 358c2e8227SGreg Roach */ 3637eb8894SGreg Roachclass HtmlBlockModule extends AbstractModule implements ModuleBlockInterface 37c1010edaSGreg Roach{ 3849a243cbSGreg Roach use ModuleBlockTrait; 3949a243cbSGreg Roach 4043f2f523SGreg Roach private HtmlService $html_service; 4150d6f48cSGreg Roach 4250d6f48cSGreg Roach /** 4350d6f48cSGreg Roach * HtmlBlockModule bootstrap. 4450d6f48cSGreg Roach * 4550d6f48cSGreg Roach * @param HtmlService $html_service 4650d6f48cSGreg Roach */ 477c2c99faSGreg Roach public function __construct(HtmlService $html_service) 4850d6f48cSGreg Roach { 4950d6f48cSGreg Roach $this->html_service = $html_service; 5050d6f48cSGreg Roach } 5150d6f48cSGreg Roach 52961ec755SGreg Roach /** 530cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 54961ec755SGreg Roach * 55961ec755SGreg Roach * @return string 56961ec755SGreg Roach */ 5749a243cbSGreg Roach public function title(): string 58c1010edaSGreg Roach { 59bbb76c12SGreg Roach /* I18N: Name of a module */ 60bbb76c12SGreg Roach return I18N::translate('HTML'); 618c2e8227SGreg Roach } 628c2e8227SGreg Roach 63961ec755SGreg Roach /** 64961ec755SGreg Roach * A sentence describing what this module does. 65961ec755SGreg Roach * 66961ec755SGreg Roach * @return string 67961ec755SGreg Roach */ 6849a243cbSGreg Roach public function description(): string 69c1010edaSGreg Roach { 70bbb76c12SGreg Roach /* I18N: Description of the “HTML” module */ 71bbb76c12SGreg Roach return I18N::translate('Add your own text and graphics.'); 728c2e8227SGreg Roach } 738c2e8227SGreg Roach 7476692c8bSGreg Roach /** 7576692c8bSGreg Roach * Generate the HTML content of this block. 7676692c8bSGreg Roach * 77e490cd80SGreg Roach * @param Tree $tree 7876692c8bSGreg Roach * @param int $block_id 793caaa4d2SGreg Roach * @param string $context 8076d39c55SGreg Roach * @param array<string,string> $config 8176692c8bSGreg Roach * 8276692c8bSGreg Roach * @return string 8376692c8bSGreg Roach */ 843caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 85c1010edaSGreg Roach { 86cab242e7SGreg Roach $statistics = app(Statistics::class); 87bf57b580SGreg Roach 8850d6f48cSGreg Roach $title = $this->getBlockSetting($block_id, 'title'); 8950d6f48cSGreg Roach $content = $this->getBlockSetting($block_id, 'html'); 9050d6f48cSGreg Roach $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp'); 91e2a378d3SGreg Roach $languages = $this->getBlockSetting($block_id, 'languages'); 928c2e8227SGreg Roach 938c2e8227SGreg Roach // Only show this block for certain languages 9465cf5706SGreg Roach if ($languages && !in_array(I18N::languageTag(), explode(',', $languages), true)) { 958c2e8227SGreg Roach return ''; 968c2e8227SGreg Roach } 978c2e8227SGreg Roach 98ca52a408SGreg Roach // Retrieve text, process embedded variables 99bf57b580SGreg Roach $title = $statistics->embedTags($title); 100bf57b580SGreg Roach $content = $statistics->embedTags($content); 1018c2e8227SGreg Roach 102d97083feSGreg Roach $block_timestamp = (int) $this->getBlockSetting($block_id, 'timestamp', (string) time()); 103ca52a408SGreg Roach 104ad98d39dSGreg Roach if ($show_timestamp === '1') { 105d97083feSGreg Roach $content .= '<br>' . view('components/datetime', ['timestamp' => Registry::timestampFactory()->make($block_timestamp)]); 1068c2e8227SGreg Roach } 1078c2e8227SGreg Roach 1083caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 109147e99aaSGreg Roach return view('modules/block-template', [ 1101e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1119c6524dcSGreg Roach 'id' => $block_id, 1123caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 113*4c718a92SGreg Roach 'title' => e(strip_tags($title)), 1149c6524dcSGreg Roach 'content' => $content, 1159c6524dcSGreg Roach ]); 1168c2e8227SGreg Roach } 117b2ce94c6SRico Sonntag 118b2ce94c6SRico Sonntag return $content; 1198c2e8227SGreg Roach } 1208c2e8227SGreg Roach 12150d6f48cSGreg Roach /** 12250d6f48cSGreg Roach * Should this block load asynchronously using AJAX? 12350d6f48cSGreg Roach * 1243caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 12550d6f48cSGreg Roach * 12650d6f48cSGreg Roach * @return bool 12750d6f48cSGreg Roach */ 128c1010edaSGreg Roach public function loadAjax(): bool 129c1010edaSGreg Roach { 1308c2e8227SGreg Roach return false; 1318c2e8227SGreg Roach } 1328c2e8227SGreg Roach 13350d6f48cSGreg Roach /** 13450d6f48cSGreg Roach * Can this block be shown on the user’s home page? 13550d6f48cSGreg Roach * 13650d6f48cSGreg Roach * @return bool 13750d6f48cSGreg Roach */ 138c1010edaSGreg Roach public function isUserBlock(): bool 139c1010edaSGreg Roach { 1408c2e8227SGreg Roach return true; 1418c2e8227SGreg Roach } 1428c2e8227SGreg Roach 14350d6f48cSGreg Roach /** 14450d6f48cSGreg Roach * Can this block be shown on the tree’s home page? 14550d6f48cSGreg Roach * 14650d6f48cSGreg Roach * @return bool 14750d6f48cSGreg Roach */ 14863276d8fSGreg Roach public function isTreeBlock(): bool 149c1010edaSGreg Roach { 1508c2e8227SGreg Roach return true; 1518c2e8227SGreg Roach } 1528c2e8227SGreg Roach 15376692c8bSGreg Roach /** 154a45f9889SGreg Roach * Update the configuration for a block. 155a45f9889SGreg Roach * 1566ccdf4f0SGreg Roach * @param ServerRequestInterface $request 157a45f9889SGreg Roach * @param int $block_id 158a45f9889SGreg Roach * 159a45f9889SGreg Roach * @return void 160a45f9889SGreg Roach */ 1616ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 162a45f9889SGreg Roach { 163*4c718a92SGreg Roach $title = Validator::parsedBody($request)->string('title'); 164*4c718a92SGreg Roach $html = Validator::parsedBody($request)->string('html'); 165*4c718a92SGreg Roach $show_timestamp = Validator::parsedBody($request)->boolean('show_timestamp'); 166*4c718a92SGreg Roach $languages = Validator::parsedBody($request)->array('languages'); 16750d6f48cSGreg Roach 16850d6f48cSGreg Roach $this->setBlockSetting($block_id, 'title', $title); 169*4c718a92SGreg Roach $this->setBlockSetting($block_id, 'html', $this->html_service->sanitize($html)); 170*4c718a92SGreg Roach $this->setBlockSetting($block_id, 'show_timestamp', (string) $show_timestamp); 171d97083feSGreg Roach $this->setBlockSetting($block_id, 'timestamp', (string) time()); 172a45f9889SGreg Roach $this->setBlockSetting($block_id, 'languages', implode(',', $languages)); 173a45f9889SGreg Roach } 174a45f9889SGreg Roach 175a45f9889SGreg Roach /** 17676692c8bSGreg Roach * An HTML form to edit block settings 17776692c8bSGreg Roach * 178e490cd80SGreg Roach * @param Tree $tree 17976692c8bSGreg Roach * @param int $block_id 180a9430be8SGreg Roach * 1813caaa4d2SGreg Roach * @return string 18276692c8bSGreg Roach */ 1833caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 184c1010edaSGreg Roach { 18550d6f48cSGreg Roach $title = $this->getBlockSetting($block_id, 'title'); 18650d6f48cSGreg Roach $html = $this->getBlockSetting($block_id, 'html'); 187e2a378d3SGreg Roach $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0'); 188e2a378d3SGreg Roach $languages = explode(',', $this->getBlockSetting($block_id, 'languages')); 1898c2e8227SGreg Roach 190b6c326d8SGreg Roach $templates = [ 191b6c326d8SGreg Roach $html => I18N::translate('Custom'), 192b6c326d8SGreg Roach view('modules/html/template-keywords') => I18N::translate('Keyword examples'), 193b6c326d8SGreg Roach view('modules/html/template-narrative') => I18N::translate('Narrative description'), 194fd303cbfSGreg Roach view('modules/html/template-statistics', ['tree' => $tree]) => I18N::translate('Statistics'), 195b6c326d8SGreg Roach ]; 196b6c326d8SGreg Roach 1973caaa4d2SGreg Roach return view('modules/html/config', [ 198c385536dSGreg Roach 'html' => $html, 199c385536dSGreg Roach 'languages' => $languages, 200c385536dSGreg Roach 'show_timestamp' => $show_timestamp, 201c385536dSGreg Roach 'templates' => $templates, 202c385536dSGreg Roach 'title' => $title, 203c385536dSGreg Roach ]); 2048c2e8227SGreg Roach } 2058c2e8227SGreg Roach} 206