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 2290a2f718SGreg Roachuse Fisharebest\Localization\Locale\LocaleInterface; 234459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 2550d6f48cSGreg Roachuse Fisharebest\Webtrees\Services\HtmlService; 261e653452SGreg Roachuse Fisharebest\Webtrees\Services\TreeService; 279219296aSGreg Roachuse Fisharebest\Webtrees\Statistics; 280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree; 291e7a7a28SGreg Roachuse Illuminate\Support\Str; 306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 318c2e8227SGreg Roach 3290a2f718SGreg Roachuse function assert; 3390a2f718SGreg Roach 348c2e8227SGreg Roach/** 358c2e8227SGreg Roach * Class HtmlBlockModule 368c2e8227SGreg Roach */ 3737eb8894SGreg Roachclass HtmlBlockModule extends AbstractModule implements ModuleBlockInterface 38c1010edaSGreg Roach{ 3949a243cbSGreg Roach use ModuleBlockTrait; 4049a243cbSGreg Roach 4150d6f48cSGreg Roach /** @var HtmlService */ 4250d6f48cSGreg Roach private $html_service; 4350d6f48cSGreg Roach 441e653452SGreg Roach /** @var TreeService */ 451e653452SGreg Roach private $tree_service; 461e653452SGreg Roach 4750d6f48cSGreg Roach /** 4850d6f48cSGreg Roach * HtmlBlockModule bootstrap. 4950d6f48cSGreg Roach * 5050d6f48cSGreg Roach * @param HtmlService $html_service 511e653452SGreg Roach * @param TreeService $tree_service 5250d6f48cSGreg Roach */ 531e653452SGreg Roach public function __construct(HtmlService $html_service, TreeService $tree_service) 5450d6f48cSGreg Roach { 5550d6f48cSGreg Roach $this->html_service = $html_service; 561e653452SGreg Roach $this->tree_service = $tree_service; 5750d6f48cSGreg Roach } 5850d6f48cSGreg Roach 59961ec755SGreg Roach /** 600cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 61961ec755SGreg Roach * 62961ec755SGreg Roach * @return string 63961ec755SGreg Roach */ 6449a243cbSGreg Roach public function title(): string 65c1010edaSGreg Roach { 66bbb76c12SGreg Roach /* I18N: Name of a module */ 67bbb76c12SGreg Roach return I18N::translate('HTML'); 688c2e8227SGreg Roach } 698c2e8227SGreg Roach 70961ec755SGreg Roach /** 71961ec755SGreg Roach * A sentence describing what this module does. 72961ec755SGreg Roach * 73961ec755SGreg Roach * @return string 74961ec755SGreg Roach */ 7549a243cbSGreg Roach public function description(): string 76c1010edaSGreg Roach { 77bbb76c12SGreg Roach /* I18N: Description of the “HTML” module */ 78bbb76c12SGreg Roach return I18N::translate('Add your own text and graphics.'); 798c2e8227SGreg Roach } 808c2e8227SGreg Roach 8176692c8bSGreg Roach /** 8276692c8bSGreg Roach * Generate the HTML content of this block. 8376692c8bSGreg Roach * 84e490cd80SGreg Roach * @param Tree $tree 8576692c8bSGreg Roach * @param int $block_id 863caaa4d2SGreg Roach * @param string $context 873caaa4d2SGreg Roach * @param string[] $config 8876692c8bSGreg Roach * 8976692c8bSGreg Roach * @return string 9076692c8bSGreg Roach */ 913caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 92c1010edaSGreg Roach { 9390a2f718SGreg Roach $locale = app(ServerRequestInterface::class)->getAttribute('locale'); 9490a2f718SGreg Roach assert($locale instanceof LocaleInterface); 9590a2f718SGreg Roach 96cab242e7SGreg Roach $statistics = app(Statistics::class); 97bf57b580SGreg Roach 9850d6f48cSGreg Roach $title = $this->getBlockSetting($block_id, 'title'); 9950d6f48cSGreg Roach $content = $this->getBlockSetting($block_id, 'html'); 10050d6f48cSGreg Roach $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp'); 101e2a378d3SGreg Roach $languages = $this->getBlockSetting($block_id, 'languages'); 1028c2e8227SGreg Roach 1038c2e8227SGreg Roach // Only show this block for certain languages 10490a2f718SGreg Roach if ($languages && !in_array($locale->languageTag(), explode(',', $languages), true)) { 1058c2e8227SGreg Roach return ''; 1068c2e8227SGreg Roach } 1078c2e8227SGreg Roach 108ca52a408SGreg Roach // Retrieve text, process embedded variables 109bf57b580SGreg Roach $title = $statistics->embedTags($title); 110bf57b580SGreg Roach $content = $statistics->embedTags($content); 1118c2e8227SGreg Roach 1124459dc9aSGreg Roach $block_timestamp = (int) $this->getBlockSetting($block_id, 'timestamp', (string) Carbon::now()->unix()); 113ca52a408SGreg Roach 114ad98d39dSGreg Roach if ($show_timestamp === '1') { 1154459dc9aSGreg Roach $content .= '<br>' . view('components/datetime', ['timestamp' => Carbon::createFromTimestamp($block_timestamp)]); 1168c2e8227SGreg Roach } 1178c2e8227SGreg Roach 1183caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 119147e99aaSGreg Roach return view('modules/block-template', [ 1201e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1219c6524dcSGreg Roach 'id' => $block_id, 1223caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 1239c6524dcSGreg Roach 'title' => $title, 1249c6524dcSGreg Roach 'content' => $content, 1259c6524dcSGreg Roach ]); 1268c2e8227SGreg Roach } 127b2ce94c6SRico Sonntag 128b2ce94c6SRico Sonntag return $content; 1298c2e8227SGreg Roach } 1308c2e8227SGreg Roach 13150d6f48cSGreg Roach /** 13250d6f48cSGreg Roach * Should this block load asynchronously using AJAX? 13350d6f48cSGreg Roach * 1343caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 13550d6f48cSGreg Roach * 13650d6f48cSGreg Roach * @return bool 13750d6f48cSGreg Roach */ 138c1010edaSGreg Roach public function loadAjax(): bool 139c1010edaSGreg Roach { 1408c2e8227SGreg Roach return false; 1418c2e8227SGreg Roach } 1428c2e8227SGreg Roach 14350d6f48cSGreg Roach /** 14450d6f48cSGreg Roach * Can this block be shown on the user’s home page? 14550d6f48cSGreg Roach * 14650d6f48cSGreg Roach * @return bool 14750d6f48cSGreg Roach */ 148c1010edaSGreg Roach public function isUserBlock(): bool 149c1010edaSGreg Roach { 1508c2e8227SGreg Roach return true; 1518c2e8227SGreg Roach } 1528c2e8227SGreg Roach 15350d6f48cSGreg Roach /** 15450d6f48cSGreg Roach * Can this block be shown on the tree’s home page? 15550d6f48cSGreg Roach * 15650d6f48cSGreg Roach * @return bool 15750d6f48cSGreg Roach */ 15863276d8fSGreg Roach public function isTreeBlock(): bool 159c1010edaSGreg Roach { 1608c2e8227SGreg Roach return true; 1618c2e8227SGreg Roach } 1628c2e8227SGreg Roach 16376692c8bSGreg Roach /** 164a45f9889SGreg Roach * Update the configuration for a block. 165a45f9889SGreg Roach * 1666ccdf4f0SGreg Roach * @param ServerRequestInterface $request 167a45f9889SGreg Roach * @param int $block_id 168a45f9889SGreg Roach * 169a45f9889SGreg Roach * @return void 170a45f9889SGreg Roach */ 1716ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 172a45f9889SGreg Roach { 173e106ff43SGreg Roach $params = $request->getParsedBody(); 174e106ff43SGreg Roach 17550d6f48cSGreg Roach $title = $this->html_service->sanitize($params['title']); 17650d6f48cSGreg Roach $html = $this->html_service->sanitize($params['html']); 17750d6f48cSGreg Roach 178e106ff43SGreg Roach $languages = $params['lang'] ?? []; 17950d6f48cSGreg Roach 18050d6f48cSGreg Roach $this->setBlockSetting($block_id, 'title', $title); 18150d6f48cSGreg Roach $this->setBlockSetting($block_id, 'html', $html); 182e106ff43SGreg Roach $this->setBlockSetting($block_id, 'show_timestamp', $params['show_timestamp']); 1834459dc9aSGreg Roach $this->setBlockSetting($block_id, 'timestamp', (string) Carbon::now()->unix()); 184a45f9889SGreg Roach $this->setBlockSetting($block_id, 'languages', implode(',', $languages)); 185a45f9889SGreg Roach } 186a45f9889SGreg Roach 187a45f9889SGreg Roach /** 18876692c8bSGreg Roach * An HTML form to edit block settings 18976692c8bSGreg Roach * 190e490cd80SGreg Roach * @param Tree $tree 19176692c8bSGreg Roach * @param int $block_id 192a9430be8SGreg Roach * 1933caaa4d2SGreg Roach * @return string 19476692c8bSGreg Roach */ 1953caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 196c1010edaSGreg Roach { 19750d6f48cSGreg Roach $title = $this->getBlockSetting($block_id, 'title'); 19850d6f48cSGreg Roach $html = $this->getBlockSetting($block_id, 'html'); 199e2a378d3SGreg Roach $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0'); 200e2a378d3SGreg Roach $languages = explode(',', $this->getBlockSetting($block_id, 'languages')); 2011e653452SGreg Roach $all_trees = $this->tree_service->titles(); 2028c2e8227SGreg Roach 203b6c326d8SGreg Roach $templates = [ 204b6c326d8SGreg Roach $html => I18N::translate('Custom'), 205b6c326d8SGreg Roach view('modules/html/template-keywords') => I18N::translate('Keyword examples'), 206b6c326d8SGreg Roach view('modules/html/template-narrative') => I18N::translate('Narrative description'), 207*fd303cbfSGreg Roach view('modules/html/template-statistics', ['tree' => $tree]) => I18N::translate('Statistics'), 208b6c326d8SGreg Roach ]; 209b6c326d8SGreg Roach 2103caaa4d2SGreg Roach return view('modules/html/config', [ 211c385536dSGreg Roach 'all_trees' => $all_trees, 212c385536dSGreg Roach 'html' => $html, 213c385536dSGreg Roach 'languages' => $languages, 214c385536dSGreg Roach 'show_timestamp' => $show_timestamp, 215c385536dSGreg Roach 'templates' => $templates, 216c385536dSGreg Roach 'title' => $title, 217c385536dSGreg Roach ]); 2188c2e8227SGreg Roach } 2198c2e8227SGreg Roach} 220