xref: /webtrees/app/Module/HtmlBlockModule.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 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;
274c718a92SGreg Roachuse Fisharebest\Webtrees\Validator;
281e7a7a28SGreg Roachuse Illuminate\Support\Str;
296ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
308c2e8227SGreg Roach
3110e06497SGreg Roachuse function in_array;
32d97083feSGreg Roachuse function time;
33d97083feSGreg Roach
348c2e8227SGreg Roach/**
358c2e8227SGreg Roach * Class HtmlBlockModule
368c2e8227SGreg Roach */
3737eb8894SGreg Roachclass HtmlBlockModule extends AbstractModule implements ModuleBlockInterface
38c1010edaSGreg Roach{
3949a243cbSGreg Roach    use ModuleBlockTrait;
4049a243cbSGreg Roach
4143f2f523SGreg Roach    private HtmlService $html_service;
4250d6f48cSGreg Roach
4350d6f48cSGreg Roach    /**
4450d6f48cSGreg Roach     * HtmlBlockModule bootstrap.
4550d6f48cSGreg Roach     *
4650d6f48cSGreg Roach     * @param HtmlService $html_service
4750d6f48cSGreg Roach     */
487c2c99faSGreg Roach    public function __construct(HtmlService $html_service)
4950d6f48cSGreg Roach    {
5050d6f48cSGreg Roach        $this->html_service = $html_service;
5150d6f48cSGreg Roach    }
5250d6f48cSGreg Roach
53961ec755SGreg Roach    /**
540cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
55961ec755SGreg Roach     *
56961ec755SGreg Roach     * @return string
57961ec755SGreg Roach     */
5849a243cbSGreg Roach    public function title(): string
59c1010edaSGreg Roach    {
60bbb76c12SGreg Roach        /* I18N: Name of a module */
61bbb76c12SGreg Roach        return I18N::translate('HTML');
628c2e8227SGreg Roach    }
638c2e8227SGreg Roach
6449a243cbSGreg Roach    public function description(): string
65c1010edaSGreg Roach    {
66bbb76c12SGreg Roach        /* I18N: Description of the “HTML” module */
67bbb76c12SGreg Roach        return I18N::translate('Add your own text and graphics.');
688c2e8227SGreg Roach    }
698c2e8227SGreg Roach
7076692c8bSGreg Roach    /**
7176692c8bSGreg Roach     * Generate the HTML content of this block.
7276692c8bSGreg Roach     *
73e490cd80SGreg Roach     * @param Tree                 $tree
7476692c8bSGreg Roach     * @param int                  $block_id
753caaa4d2SGreg Roach     * @param string               $context
7676d39c55SGreg Roach     * @param array<string,string> $config
7776692c8bSGreg Roach     *
7876692c8bSGreg Roach     * @return string
7976692c8bSGreg Roach     */
803caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
81c1010edaSGreg Roach    {
82*d35568b4SGreg Roach        $statistics = Registry::container()->get(Statistics::class);
83bf57b580SGreg Roach
8450d6f48cSGreg Roach        $title          = $this->getBlockSetting($block_id, 'title');
8550d6f48cSGreg Roach        $content        = $this->getBlockSetting($block_id, 'html');
8650d6f48cSGreg Roach        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp');
87e2a378d3SGreg Roach        $languages      = $this->getBlockSetting($block_id, 'languages');
888c2e8227SGreg Roach
898c2e8227SGreg Roach        // Only show this block for certain languages
9065cf5706SGreg Roach        if ($languages && !in_array(I18N::languageTag(), explode(',', $languages), true)) {
918c2e8227SGreg Roach            return '';
928c2e8227SGreg Roach        }
938c2e8227SGreg Roach
94ca52a408SGreg Roach        // Retrieve text, process embedded variables
95bf57b580SGreg Roach        $title   = $statistics->embedTags($title);
96bf57b580SGreg Roach        $content = $statistics->embedTags($content);
978c2e8227SGreg Roach
98d97083feSGreg Roach        $block_timestamp = (int) $this->getBlockSetting($block_id, 'timestamp', (string) time());
99ca52a408SGreg Roach
100ad98d39dSGreg Roach        if ($show_timestamp === '1') {
101d97083feSGreg Roach            $content .= '<br>' . view('components/datetime', ['timestamp' => Registry::timestampFactory()->make($block_timestamp)]);
1028c2e8227SGreg Roach        }
1038c2e8227SGreg Roach
1043caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
105147e99aaSGreg Roach            return view('modules/block-template', [
1061e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1079c6524dcSGreg Roach                'id'         => $block_id,
1083caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
1094c718a92SGreg Roach                'title'      => e(strip_tags($title)),
1109c6524dcSGreg Roach                'content'    => $content,
1119c6524dcSGreg Roach            ]);
1128c2e8227SGreg Roach        }
113b2ce94c6SRico Sonntag
114b2ce94c6SRico Sonntag        return $content;
1158c2e8227SGreg Roach    }
1168c2e8227SGreg Roach
11750d6f48cSGreg Roach    /**
11850d6f48cSGreg Roach     * Should this block load asynchronously using AJAX?
11950d6f48cSGreg Roach     *
1203caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
12150d6f48cSGreg Roach     *
12250d6f48cSGreg Roach     * @return bool
12350d6f48cSGreg Roach     */
124c1010edaSGreg Roach    public function loadAjax(): bool
125c1010edaSGreg Roach    {
1268c2e8227SGreg Roach        return false;
1278c2e8227SGreg Roach    }
1288c2e8227SGreg Roach
12950d6f48cSGreg Roach    /**
13050d6f48cSGreg Roach     * Can this block be shown on the user’s home page?
13150d6f48cSGreg Roach     *
13250d6f48cSGreg Roach     * @return bool
13350d6f48cSGreg Roach     */
134c1010edaSGreg Roach    public function isUserBlock(): bool
135c1010edaSGreg Roach    {
1368c2e8227SGreg Roach        return true;
1378c2e8227SGreg Roach    }
1388c2e8227SGreg Roach
13950d6f48cSGreg Roach    /**
14050d6f48cSGreg Roach     * Can this block be shown on the tree’s home page?
14150d6f48cSGreg Roach     *
14250d6f48cSGreg Roach     * @return bool
14350d6f48cSGreg Roach     */
14463276d8fSGreg Roach    public function isTreeBlock(): bool
145c1010edaSGreg Roach    {
1468c2e8227SGreg Roach        return true;
1478c2e8227SGreg Roach    }
1488c2e8227SGreg Roach
14976692c8bSGreg Roach    /**
150a45f9889SGreg Roach     * Update the configuration for a block.
151a45f9889SGreg Roach     *
1526ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
153a45f9889SGreg Roach     * @param int                    $block_id
154a45f9889SGreg Roach     *
155a45f9889SGreg Roach     * @return void
156a45f9889SGreg Roach     */
1576ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
158a45f9889SGreg Roach    {
1594c718a92SGreg Roach        $title          = Validator::parsedBody($request)->string('title');
1604c718a92SGreg Roach        $html           = Validator::parsedBody($request)->string('html');
1614c718a92SGreg Roach        $show_timestamp = Validator::parsedBody($request)->boolean('show_timestamp');
1624c718a92SGreg Roach        $languages      = Validator::parsedBody($request)->array('languages');
16350d6f48cSGreg Roach
16450d6f48cSGreg Roach        $this->setBlockSetting($block_id, 'title', $title);
1654c718a92SGreg Roach        $this->setBlockSetting($block_id, 'html', $this->html_service->sanitize($html));
1664c718a92SGreg Roach        $this->setBlockSetting($block_id, 'show_timestamp', (string) $show_timestamp);
167d97083feSGreg Roach        $this->setBlockSetting($block_id, 'timestamp', (string) time());
168a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'languages', implode(',', $languages));
169a45f9889SGreg Roach    }
170a45f9889SGreg Roach
171a45f9889SGreg Roach    /**
17276692c8bSGreg Roach     * An HTML form to edit block settings
17376692c8bSGreg Roach     *
174e490cd80SGreg Roach     * @param Tree $tree
17576692c8bSGreg Roach     * @param int  $block_id
176a9430be8SGreg Roach     *
1773caaa4d2SGreg Roach     * @return string
17876692c8bSGreg Roach     */
1793caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
180c1010edaSGreg Roach    {
18150d6f48cSGreg Roach        $title          = $this->getBlockSetting($block_id, 'title');
18250d6f48cSGreg Roach        $html           = $this->getBlockSetting($block_id, 'html');
183e2a378d3SGreg Roach        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
184e2a378d3SGreg Roach        $languages      = explode(',', $this->getBlockSetting($block_id, 'languages'));
1858c2e8227SGreg Roach
186b6c326d8SGreg Roach        $templates = [
187b6c326d8SGreg Roach            $html                                                       => I18N::translate('Custom'),
188b6c326d8SGreg Roach            view('modules/html/template-keywords')                      => I18N::translate('Keyword examples'),
189b6c326d8SGreg Roach            view('modules/html/template-narrative')                     => I18N::translate('Narrative description'),
190fd303cbfSGreg Roach            view('modules/html/template-statistics', ['tree' => $tree]) => I18N::translate('Statistics'),
191b6c326d8SGreg Roach        ];
192b6c326d8SGreg Roach
1933caaa4d2SGreg Roach        return view('modules/html/config', [
194c385536dSGreg Roach            'html'           => $html,
195c385536dSGreg Roach            'languages'      => $languages,
196c385536dSGreg Roach            'show_timestamp' => $show_timestamp,
197c385536dSGreg Roach            'templates'      => $templates,
198c385536dSGreg Roach            'title'          => $title,
199c385536dSGreg Roach        ]);
2008c2e8227SGreg Roach    }
2018c2e8227SGreg Roach}
202