18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 5*5bfc6897SGreg 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; 271e7a7a28SGreg Roachuse Illuminate\Support\Str; 286ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 298c2e8227SGreg Roach 30d97083feSGreg Roachuse function time; 31d97083feSGreg Roach 328c2e8227SGreg Roach/** 338c2e8227SGreg Roach * Class HtmlBlockModule 348c2e8227SGreg Roach */ 3537eb8894SGreg Roachclass HtmlBlockModule extends AbstractModule implements ModuleBlockInterface 36c1010edaSGreg Roach{ 3749a243cbSGreg Roach use ModuleBlockTrait; 3849a243cbSGreg Roach 3943f2f523SGreg Roach private HtmlService $html_service; 4050d6f48cSGreg Roach 4150d6f48cSGreg Roach /** 4250d6f48cSGreg Roach * HtmlBlockModule bootstrap. 4350d6f48cSGreg Roach * 4450d6f48cSGreg Roach * @param HtmlService $html_service 4550d6f48cSGreg Roach */ 467c2c99faSGreg Roach public function __construct(HtmlService $html_service) 4750d6f48cSGreg Roach { 4850d6f48cSGreg Roach $this->html_service = $html_service; 4950d6f48cSGreg Roach } 5050d6f48cSGreg Roach 51961ec755SGreg Roach /** 520cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 53961ec755SGreg Roach * 54961ec755SGreg Roach * @return string 55961ec755SGreg Roach */ 5649a243cbSGreg Roach public function title(): string 57c1010edaSGreg Roach { 58bbb76c12SGreg Roach /* I18N: Name of a module */ 59bbb76c12SGreg Roach return I18N::translate('HTML'); 608c2e8227SGreg Roach } 618c2e8227SGreg Roach 62961ec755SGreg Roach /** 63961ec755SGreg Roach * A sentence describing what this module does. 64961ec755SGreg Roach * 65961ec755SGreg Roach * @return string 66961ec755SGreg Roach */ 6749a243cbSGreg Roach public function description(): string 68c1010edaSGreg Roach { 69bbb76c12SGreg Roach /* I18N: Description of the “HTML” module */ 70bbb76c12SGreg Roach return I18N::translate('Add your own text and graphics.'); 718c2e8227SGreg Roach } 728c2e8227SGreg Roach 7376692c8bSGreg Roach /** 7476692c8bSGreg Roach * Generate the HTML content of this block. 7576692c8bSGreg Roach * 76e490cd80SGreg Roach * @param Tree $tree 7776692c8bSGreg Roach * @param int $block_id 783caaa4d2SGreg Roach * @param string $context 7976d39c55SGreg Roach * @param array<string,string> $config 8076692c8bSGreg Roach * 8176692c8bSGreg Roach * @return string 8276692c8bSGreg Roach */ 833caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 84c1010edaSGreg Roach { 85cab242e7SGreg Roach $statistics = app(Statistics::class); 86bf57b580SGreg Roach 8750d6f48cSGreg Roach $title = $this->getBlockSetting($block_id, 'title'); 8850d6f48cSGreg Roach $content = $this->getBlockSetting($block_id, 'html'); 8950d6f48cSGreg Roach $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp'); 90e2a378d3SGreg Roach $languages = $this->getBlockSetting($block_id, 'languages'); 918c2e8227SGreg Roach 928c2e8227SGreg Roach // Only show this block for certain languages 9365cf5706SGreg Roach if ($languages && !in_array(I18N::languageTag(), explode(',', $languages), true)) { 948c2e8227SGreg Roach return ''; 958c2e8227SGreg Roach } 968c2e8227SGreg Roach 97ca52a408SGreg Roach // Retrieve text, process embedded variables 98bf57b580SGreg Roach $title = $statistics->embedTags($title); 99bf57b580SGreg Roach $content = $statistics->embedTags($content); 1008c2e8227SGreg Roach 101d97083feSGreg Roach $block_timestamp = (int) $this->getBlockSetting($block_id, 'timestamp', (string) time()); 102ca52a408SGreg Roach 103ad98d39dSGreg Roach if ($show_timestamp === '1') { 104d97083feSGreg Roach $content .= '<br>' . view('components/datetime', ['timestamp' => Registry::timestampFactory()->make($block_timestamp)]); 1058c2e8227SGreg Roach } 1068c2e8227SGreg Roach 1073caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 108147e99aaSGreg Roach return view('modules/block-template', [ 1091e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1109c6524dcSGreg Roach 'id' => $block_id, 1113caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 112a743d8a2SGreg Roach 'title' => e($title), 1139c6524dcSGreg Roach 'content' => $content, 1149c6524dcSGreg Roach ]); 1158c2e8227SGreg Roach } 116b2ce94c6SRico Sonntag 117b2ce94c6SRico Sonntag return $content; 1188c2e8227SGreg Roach } 1198c2e8227SGreg Roach 12050d6f48cSGreg Roach /** 12150d6f48cSGreg Roach * Should this block load asynchronously using AJAX? 12250d6f48cSGreg Roach * 1233caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 12450d6f48cSGreg Roach * 12550d6f48cSGreg Roach * @return bool 12650d6f48cSGreg Roach */ 127c1010edaSGreg Roach public function loadAjax(): bool 128c1010edaSGreg Roach { 1298c2e8227SGreg Roach return false; 1308c2e8227SGreg Roach } 1318c2e8227SGreg Roach 13250d6f48cSGreg Roach /** 13350d6f48cSGreg Roach * Can this block be shown on the user’s home page? 13450d6f48cSGreg Roach * 13550d6f48cSGreg Roach * @return bool 13650d6f48cSGreg Roach */ 137c1010edaSGreg Roach public function isUserBlock(): bool 138c1010edaSGreg Roach { 1398c2e8227SGreg Roach return true; 1408c2e8227SGreg Roach } 1418c2e8227SGreg Roach 14250d6f48cSGreg Roach /** 14350d6f48cSGreg Roach * Can this block be shown on the tree’s home page? 14450d6f48cSGreg Roach * 14550d6f48cSGreg Roach * @return bool 14650d6f48cSGreg Roach */ 14763276d8fSGreg Roach public function isTreeBlock(): bool 148c1010edaSGreg Roach { 1498c2e8227SGreg Roach return true; 1508c2e8227SGreg Roach } 1518c2e8227SGreg Roach 15276692c8bSGreg Roach /** 153a45f9889SGreg Roach * Update the configuration for a block. 154a45f9889SGreg Roach * 1556ccdf4f0SGreg Roach * @param ServerRequestInterface $request 156a45f9889SGreg Roach * @param int $block_id 157a45f9889SGreg Roach * 158a45f9889SGreg Roach * @return void 159a45f9889SGreg Roach */ 1606ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 161a45f9889SGreg Roach { 162b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 163e106ff43SGreg Roach 16450d6f48cSGreg Roach $title = $this->html_service->sanitize($params['title']); 16550d6f48cSGreg Roach $html = $this->html_service->sanitize($params['html']); 16650d6f48cSGreg Roach 167e73fb82fSGreg Roach $languages = $params['languages'] ?? []; 16850d6f48cSGreg Roach 16950d6f48cSGreg Roach $this->setBlockSetting($block_id, 'title', $title); 17050d6f48cSGreg Roach $this->setBlockSetting($block_id, 'html', $html); 171e106ff43SGreg Roach $this->setBlockSetting($block_id, 'show_timestamp', $params['show_timestamp']); 172d97083feSGreg Roach $this->setBlockSetting($block_id, 'timestamp', (string) time()); 173a45f9889SGreg Roach $this->setBlockSetting($block_id, 'languages', implode(',', $languages)); 174a45f9889SGreg Roach } 175a45f9889SGreg Roach 176a45f9889SGreg Roach /** 17776692c8bSGreg Roach * An HTML form to edit block settings 17876692c8bSGreg Roach * 179e490cd80SGreg Roach * @param Tree $tree 18076692c8bSGreg Roach * @param int $block_id 181a9430be8SGreg Roach * 1823caaa4d2SGreg Roach * @return string 18376692c8bSGreg Roach */ 1843caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 185c1010edaSGreg Roach { 18650d6f48cSGreg Roach $title = $this->getBlockSetting($block_id, 'title'); 18750d6f48cSGreg Roach $html = $this->getBlockSetting($block_id, 'html'); 188e2a378d3SGreg Roach $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0'); 189e2a378d3SGreg Roach $languages = explode(',', $this->getBlockSetting($block_id, 'languages')); 1908c2e8227SGreg Roach 191b6c326d8SGreg Roach $templates = [ 192b6c326d8SGreg Roach $html => I18N::translate('Custom'), 193b6c326d8SGreg Roach view('modules/html/template-keywords') => I18N::translate('Keyword examples'), 194b6c326d8SGreg Roach view('modules/html/template-narrative') => I18N::translate('Narrative description'), 195fd303cbfSGreg Roach view('modules/html/template-statistics', ['tree' => $tree]) => I18N::translate('Statistics'), 196b6c326d8SGreg Roach ]; 197b6c326d8SGreg Roach 1983caaa4d2SGreg Roach return view('modules/html/config', [ 199c385536dSGreg Roach 'html' => $html, 200c385536dSGreg Roach 'languages' => $languages, 201c385536dSGreg Roach 'show_timestamp' => $show_timestamp, 202c385536dSGreg Roach 'templates' => $templates, 203c385536dSGreg Roach 'title' => $title, 204c385536dSGreg Roach ]); 2058c2e8227SGreg Roach } 2068c2e8227SGreg Roach} 207