. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Module; use Fisharebest\Webtrees\Auth; use Fisharebest\Webtrees\Carbon; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Services\HtmlService; use Fisharebest\Webtrees\Statistics; use Fisharebest\Webtrees\Tree; use Illuminate\Support\Str; use Psr\Http\Message\ServerRequestInterface; /** * Class HtmlBlockModule */ class HtmlBlockModule extends AbstractModule implements ModuleBlockInterface { use ModuleBlockTrait; /** @var HtmlService */ private $html_service; /** * HtmlBlockModule bootstrap. * * @param HtmlService $html_service */ public function boot(HtmlService $html_service) { $this->html_service = $html_service; } /** * How should this module be identified in the control panel, etc.? * * @return string */ public function title(): string { /* I18N: Name of a module */ return I18N::translate('HTML'); } /** * A sentence describing what this module does. * * @return string */ public function description(): string { /* I18N: Description of the “HTML” module */ return I18N::translate('Add your own text and graphics.'); } /** * Generate the HTML content of this block. * * @param Tree $tree * @param int $block_id * @param string $ctype * @param string[] $cfg * * @return string */ public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string { $statistics = app(Statistics::class); $title = $this->getBlockSetting($block_id, 'title'); $content = $this->getBlockSetting($block_id, 'html'); $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp'); $languages = $this->getBlockSetting($block_id, 'languages'); // Only show this block for certain languages if ($languages && !in_array(WT_LOCALE, explode(',', $languages), true)) { return ''; } // Retrieve text, process embedded variables $title = $statistics->embedTags($title); $content = $statistics->embedTags($content); $block_timestamp = (int) $this->getBlockSetting($block_id, 'timestamp', (string) Carbon::now()->unix()); if ($show_timestamp === '1') { $content .= '
' . view('components/datetime', ['timestamp' => Carbon::createFromTimestamp($block_timestamp)]); } if ($ctype !== '') { if ($ctype === 'gedcom' && Auth::isManager($tree)) { $config_url = route('tree-page-block-edit', [ 'block_id' => $block_id, 'ged' => $tree->name(), ]); } elseif ($ctype === 'user' && Auth::check()) { $config_url = route('user-page-block-edit', [ 'block_id' => $block_id, 'ged' => $tree->name(), ]); } else { $config_url = ''; } return view('modules/block-template', [ 'block' => Str::kebab($this->name()), 'id' => $block_id, 'config_url' => $config_url, 'title' => $title, 'content' => $content, ]); } return $content; } /** * Should this block load asynchronously using AJAX? * * Simple blocks are faster in-line, more comples ones * can be loaded later. * * @return bool */ public function loadAjax(): bool { return false; } /** * Can this block be shown on the user’s home page? * * @return bool */ public function isUserBlock(): bool { return true; } /** * Can this block be shown on the tree’s home page? * * @return bool */ public function isTreeBlock(): bool { return true; } /** * Update the configuration for a block. * * @param ServerRequestInterface $request * @param int $block_id * * @return void */ public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void { $params = $request->getParsedBody(); $title = $this->html_service->sanitize($params['title']); $html = $this->html_service->sanitize($params['html']); $languages = $params['lang'] ?? []; $this->setBlockSetting($block_id, 'title', $title); $this->setBlockSetting($block_id, 'html', $html); $this->setBlockSetting($block_id, 'show_timestamp', $params['show_timestamp']); $this->setBlockSetting($block_id, 'timestamp', (string) Carbon::now()->unix()); $this->setBlockSetting($block_id, 'languages', implode(',', $languages)); } /** * An HTML form to edit block settings * * @param Tree $tree * @param int $block_id * * @return void */ public function editBlockConfiguration(Tree $tree, int $block_id): void { $title = $this->getBlockSetting($block_id, 'title'); $html = $this->getBlockSetting($block_id, 'html'); $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0'); $languages = explode(',', $this->getBlockSetting($block_id, 'languages')); $all_trees = Tree::getNameList(); $templates = [ $html => I18N::translate('Custom'), view('modules/html/template-keywords') => I18N::translate('Keyword examples'), view('modules/html/template-narrative') => I18N::translate('Narrative description'), view('modules/html/template-statistics') => I18N::translate('Statistics'), ]; echo view('modules/html/config', [ 'all_trees' => $all_trees, 'html' => $html, 'languages' => $languages, 'show_timestamp' => $show_timestamp, 'templates' => $templates, 'title' => $title, ]); } }