18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 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 158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 168c2e8227SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 2176692c8bSGreg Roach 224459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 2450d6f48cSGreg Roachuse Fisharebest\Webtrees\Services\HtmlService; 25*1e653452SGreg Roachuse Fisharebest\Webtrees\Services\TreeService; 269219296aSGreg Roachuse Fisharebest\Webtrees\Statistics; 270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree; 281e7a7a28SGreg Roachuse Illuminate\Support\Str; 296ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 308c2e8227SGreg Roach 318c2e8227SGreg Roach/** 328c2e8227SGreg Roach * Class HtmlBlockModule 338c2e8227SGreg Roach */ 3437eb8894SGreg Roachclass HtmlBlockModule extends AbstractModule implements ModuleBlockInterface 35c1010edaSGreg Roach{ 3649a243cbSGreg Roach use ModuleBlockTrait; 3749a243cbSGreg Roach 3850d6f48cSGreg Roach /** @var HtmlService */ 3950d6f48cSGreg Roach private $html_service; 4050d6f48cSGreg Roach 41*1e653452SGreg Roach /** @var TreeService */ 42*1e653452SGreg Roach private $tree_service; 43*1e653452SGreg Roach 4450d6f48cSGreg Roach /** 4550d6f48cSGreg Roach * HtmlBlockModule bootstrap. 4650d6f48cSGreg Roach * 4750d6f48cSGreg Roach * @param HtmlService $html_service 48*1e653452SGreg Roach * @param TreeService $tree_service 4950d6f48cSGreg Roach */ 50*1e653452SGreg Roach public function __construct(HtmlService $html_service, TreeService $tree_service) 5150d6f48cSGreg Roach { 5250d6f48cSGreg Roach $this->html_service = $html_service; 53*1e653452SGreg Roach $this->tree_service = $tree_service; 5450d6f48cSGreg Roach } 5550d6f48cSGreg Roach 56961ec755SGreg Roach /** 570cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 58961ec755SGreg Roach * 59961ec755SGreg Roach * @return string 60961ec755SGreg Roach */ 6149a243cbSGreg Roach public function title(): string 62c1010edaSGreg Roach { 63bbb76c12SGreg Roach /* I18N: Name of a module */ 64bbb76c12SGreg Roach return I18N::translate('HTML'); 658c2e8227SGreg Roach } 668c2e8227SGreg Roach 67961ec755SGreg Roach /** 68961ec755SGreg Roach * A sentence describing what this module does. 69961ec755SGreg Roach * 70961ec755SGreg Roach * @return string 71961ec755SGreg Roach */ 7249a243cbSGreg Roach public function description(): string 73c1010edaSGreg Roach { 74bbb76c12SGreg Roach /* I18N: Description of the “HTML” module */ 75bbb76c12SGreg Roach return I18N::translate('Add your own text and graphics.'); 768c2e8227SGreg Roach } 778c2e8227SGreg Roach 7876692c8bSGreg Roach /** 7976692c8bSGreg Roach * Generate the HTML content of this block. 8076692c8bSGreg Roach * 81e490cd80SGreg Roach * @param Tree $tree 8276692c8bSGreg Roach * @param int $block_id 833caaa4d2SGreg Roach * @param string $context 843caaa4d2SGreg Roach * @param string[] $config 8576692c8bSGreg Roach * 8676692c8bSGreg Roach * @return string 8776692c8bSGreg Roach */ 883caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 89c1010edaSGreg Roach { 90cab242e7SGreg Roach $statistics = app(Statistics::class); 91bf57b580SGreg Roach 9250d6f48cSGreg Roach $title = $this->getBlockSetting($block_id, 'title'); 9350d6f48cSGreg Roach $content = $this->getBlockSetting($block_id, 'html'); 9450d6f48cSGreg Roach $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp'); 95e2a378d3SGreg Roach $languages = $this->getBlockSetting($block_id, 'languages'); 968c2e8227SGreg Roach 978c2e8227SGreg Roach // Only show this block for certain languages 9822d65e5aSGreg Roach if ($languages && !in_array(WT_LOCALE, explode(',', $languages), true)) { 998c2e8227SGreg Roach return ''; 1008c2e8227SGreg Roach } 1018c2e8227SGreg Roach 102ca52a408SGreg Roach // Retrieve text, process embedded variables 103bf57b580SGreg Roach $title = $statistics->embedTags($title); 104bf57b580SGreg Roach $content = $statistics->embedTags($content); 1058c2e8227SGreg Roach 1064459dc9aSGreg Roach $block_timestamp = (int) $this->getBlockSetting($block_id, 'timestamp', (string) Carbon::now()->unix()); 107ca52a408SGreg Roach 108ad98d39dSGreg Roach if ($show_timestamp === '1') { 1094459dc9aSGreg Roach $content .= '<br>' . view('components/datetime', ['timestamp' => Carbon::createFromTimestamp($block_timestamp)]); 1108c2e8227SGreg Roach } 1118c2e8227SGreg Roach 1123caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 113147e99aaSGreg Roach return view('modules/block-template', [ 1141e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1159c6524dcSGreg Roach 'id' => $block_id, 1163caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 1179c6524dcSGreg Roach 'title' => $title, 1189c6524dcSGreg Roach 'content' => $content, 1199c6524dcSGreg Roach ]); 1208c2e8227SGreg Roach } 121b2ce94c6SRico Sonntag 122b2ce94c6SRico Sonntag return $content; 1238c2e8227SGreg Roach } 1248c2e8227SGreg Roach 12550d6f48cSGreg Roach /** 12650d6f48cSGreg Roach * Should this block load asynchronously using AJAX? 12750d6f48cSGreg Roach * 1283caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 12950d6f48cSGreg Roach * 13050d6f48cSGreg Roach * @return bool 13150d6f48cSGreg Roach */ 132c1010edaSGreg Roach public function loadAjax(): bool 133c1010edaSGreg Roach { 1348c2e8227SGreg Roach return false; 1358c2e8227SGreg Roach } 1368c2e8227SGreg Roach 13750d6f48cSGreg Roach /** 13850d6f48cSGreg Roach * Can this block be shown on the user’s home page? 13950d6f48cSGreg Roach * 14050d6f48cSGreg Roach * @return bool 14150d6f48cSGreg Roach */ 142c1010edaSGreg Roach public function isUserBlock(): bool 143c1010edaSGreg Roach { 1448c2e8227SGreg Roach return true; 1458c2e8227SGreg Roach } 1468c2e8227SGreg Roach 14750d6f48cSGreg Roach /** 14850d6f48cSGreg Roach * Can this block be shown on the tree’s home page? 14950d6f48cSGreg Roach * 15050d6f48cSGreg Roach * @return bool 15150d6f48cSGreg Roach */ 15263276d8fSGreg Roach public function isTreeBlock(): bool 153c1010edaSGreg Roach { 1548c2e8227SGreg Roach return true; 1558c2e8227SGreg Roach } 1568c2e8227SGreg Roach 15776692c8bSGreg Roach /** 158a45f9889SGreg Roach * Update the configuration for a block. 159a45f9889SGreg Roach * 1606ccdf4f0SGreg Roach * @param ServerRequestInterface $request 161a45f9889SGreg Roach * @param int $block_id 162a45f9889SGreg Roach * 163a45f9889SGreg Roach * @return void 164a45f9889SGreg Roach */ 1656ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 166a45f9889SGreg Roach { 167e106ff43SGreg Roach $params = $request->getParsedBody(); 168e106ff43SGreg Roach 16950d6f48cSGreg Roach $title = $this->html_service->sanitize($params['title']); 17050d6f48cSGreg Roach $html = $this->html_service->sanitize($params['html']); 17150d6f48cSGreg Roach 172e106ff43SGreg Roach $languages = $params['lang'] ?? []; 17350d6f48cSGreg Roach 17450d6f48cSGreg Roach $this->setBlockSetting($block_id, 'title', $title); 17550d6f48cSGreg Roach $this->setBlockSetting($block_id, 'html', $html); 176e106ff43SGreg Roach $this->setBlockSetting($block_id, 'show_timestamp', $params['show_timestamp']); 1774459dc9aSGreg Roach $this->setBlockSetting($block_id, 'timestamp', (string) Carbon::now()->unix()); 178a45f9889SGreg Roach $this->setBlockSetting($block_id, 'languages', implode(',', $languages)); 179a45f9889SGreg Roach } 180a45f9889SGreg Roach 181a45f9889SGreg Roach /** 18276692c8bSGreg Roach * An HTML form to edit block settings 18376692c8bSGreg Roach * 184e490cd80SGreg Roach * @param Tree $tree 18576692c8bSGreg Roach * @param int $block_id 186a9430be8SGreg Roach * 1873caaa4d2SGreg Roach * @return string 18876692c8bSGreg Roach */ 1893caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 190c1010edaSGreg Roach { 19150d6f48cSGreg Roach $title = $this->getBlockSetting($block_id, 'title'); 19250d6f48cSGreg Roach $html = $this->getBlockSetting($block_id, 'html'); 193e2a378d3SGreg Roach $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0'); 194e2a378d3SGreg Roach $languages = explode(',', $this->getBlockSetting($block_id, 'languages')); 195*1e653452SGreg Roach $all_trees = $this->tree_service->titles(); 1968c2e8227SGreg Roach 197b6c326d8SGreg Roach $templates = [ 198b6c326d8SGreg Roach $html => I18N::translate('Custom'), 199b6c326d8SGreg Roach view('modules/html/template-keywords') => I18N::translate('Keyword examples'), 200b6c326d8SGreg Roach view('modules/html/template-narrative') => I18N::translate('Narrative description'), 201b6c326d8SGreg Roach view('modules/html/template-statistics') => I18N::translate('Statistics'), 202b6c326d8SGreg Roach ]; 203b6c326d8SGreg Roach 2043caaa4d2SGreg Roach return view('modules/html/config', [ 205c385536dSGreg Roach 'all_trees' => $all_trees, 206c385536dSGreg Roach 'html' => $html, 207c385536dSGreg Roach 'languages' => $languages, 208c385536dSGreg Roach 'show_timestamp' => $show_timestamp, 209c385536dSGreg Roach 'templates' => $templates, 210c385536dSGreg Roach 'title' => $title, 211c385536dSGreg Roach ]); 2128c2e8227SGreg Roach } 2138c2e8227SGreg Roach} 214