xref: /webtrees/app/Module/HtmlBlockModule.php (revision 10e0649788c8d7d4974d81c048ca2b225df8f22e)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
55bfc6897SGreg 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;
274c718a92SGreg Roachuse Fisharebest\Webtrees\Validator;
281e7a7a28SGreg Roachuse Illuminate\Support\Str;
296ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
308c2e8227SGreg Roach
31*10e06497SGreg 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
64961ec755SGreg Roach    /**
65961ec755SGreg Roach     * A sentence describing what this module does.
66961ec755SGreg Roach     *
67961ec755SGreg Roach     * @return string
68961ec755SGreg Roach     */
6949a243cbSGreg Roach    public function description(): string
70c1010edaSGreg Roach    {
71bbb76c12SGreg Roach        /* I18N: Description of the “HTML” module */
72bbb76c12SGreg Roach        return I18N::translate('Add your own text and graphics.');
738c2e8227SGreg Roach    }
748c2e8227SGreg Roach
7576692c8bSGreg Roach    /**
7676692c8bSGreg Roach     * Generate the HTML content of this block.
7776692c8bSGreg Roach     *
78e490cd80SGreg Roach     * @param Tree                 $tree
7976692c8bSGreg Roach     * @param int                  $block_id
803caaa4d2SGreg Roach     * @param string               $context
8176d39c55SGreg Roach     * @param array<string,string> $config
8276692c8bSGreg Roach     *
8376692c8bSGreg Roach     * @return string
8476692c8bSGreg Roach     */
853caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
86c1010edaSGreg Roach    {
87cab242e7SGreg Roach        $statistics = app(Statistics::class);
88bf57b580SGreg Roach
8950d6f48cSGreg Roach        $title          = $this->getBlockSetting($block_id, 'title');
9050d6f48cSGreg Roach        $content        = $this->getBlockSetting($block_id, 'html');
9150d6f48cSGreg Roach        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp');
92e2a378d3SGreg Roach        $languages      = $this->getBlockSetting($block_id, 'languages');
938c2e8227SGreg Roach
948c2e8227SGreg Roach        // Only show this block for certain languages
9565cf5706SGreg Roach        if ($languages && !in_array(I18N::languageTag(), explode(',', $languages), true)) {
968c2e8227SGreg Roach            return '';
978c2e8227SGreg Roach        }
988c2e8227SGreg Roach
99ca52a408SGreg Roach        // Retrieve text, process embedded variables
100bf57b580SGreg Roach        $title   = $statistics->embedTags($title);
101bf57b580SGreg Roach        $content = $statistics->embedTags($content);
1028c2e8227SGreg Roach
103d97083feSGreg Roach        $block_timestamp = (int) $this->getBlockSetting($block_id, 'timestamp', (string) time());
104ca52a408SGreg Roach
105ad98d39dSGreg Roach        if ($show_timestamp === '1') {
106d97083feSGreg Roach            $content .= '<br>' . view('components/datetime', ['timestamp' => Registry::timestampFactory()->make($block_timestamp)]);
1078c2e8227SGreg Roach        }
1088c2e8227SGreg Roach
1093caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
110147e99aaSGreg Roach            return view('modules/block-template', [
1111e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1129c6524dcSGreg Roach                'id'         => $block_id,
1133caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
1144c718a92SGreg Roach                'title'      => e(strip_tags($title)),
1159c6524dcSGreg Roach                'content'    => $content,
1169c6524dcSGreg Roach            ]);
1178c2e8227SGreg Roach        }
118b2ce94c6SRico Sonntag
119b2ce94c6SRico Sonntag        return $content;
1208c2e8227SGreg Roach    }
1218c2e8227SGreg Roach
12250d6f48cSGreg Roach    /**
12350d6f48cSGreg Roach     * Should this block load asynchronously using AJAX?
12450d6f48cSGreg Roach     *
1253caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
12650d6f48cSGreg Roach     *
12750d6f48cSGreg Roach     * @return bool
12850d6f48cSGreg Roach     */
129c1010edaSGreg Roach    public function loadAjax(): bool
130c1010edaSGreg Roach    {
1318c2e8227SGreg Roach        return false;
1328c2e8227SGreg Roach    }
1338c2e8227SGreg Roach
13450d6f48cSGreg Roach    /**
13550d6f48cSGreg Roach     * Can this block be shown on the user’s home page?
13650d6f48cSGreg Roach     *
13750d6f48cSGreg Roach     * @return bool
13850d6f48cSGreg Roach     */
139c1010edaSGreg Roach    public function isUserBlock(): bool
140c1010edaSGreg Roach    {
1418c2e8227SGreg Roach        return true;
1428c2e8227SGreg Roach    }
1438c2e8227SGreg Roach
14450d6f48cSGreg Roach    /**
14550d6f48cSGreg Roach     * Can this block be shown on the tree’s home page?
14650d6f48cSGreg Roach     *
14750d6f48cSGreg Roach     * @return bool
14850d6f48cSGreg Roach     */
14963276d8fSGreg Roach    public function isTreeBlock(): bool
150c1010edaSGreg Roach    {
1518c2e8227SGreg Roach        return true;
1528c2e8227SGreg Roach    }
1538c2e8227SGreg Roach
15476692c8bSGreg Roach    /**
155a45f9889SGreg Roach     * Update the configuration for a block.
156a45f9889SGreg Roach     *
1576ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
158a45f9889SGreg Roach     * @param int                    $block_id
159a45f9889SGreg Roach     *
160a45f9889SGreg Roach     * @return void
161a45f9889SGreg Roach     */
1626ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
163a45f9889SGreg Roach    {
1644c718a92SGreg Roach        $title          = Validator::parsedBody($request)->string('title');
1654c718a92SGreg Roach        $html           = Validator::parsedBody($request)->string('html');
1664c718a92SGreg Roach        $show_timestamp = Validator::parsedBody($request)->boolean('show_timestamp');
1674c718a92SGreg Roach        $languages      = Validator::parsedBody($request)->array('languages');
16850d6f48cSGreg Roach
16950d6f48cSGreg Roach        $this->setBlockSetting($block_id, 'title', $title);
1704c718a92SGreg Roach        $this->setBlockSetting($block_id, 'html', $this->html_service->sanitize($html));
1714c718a92SGreg Roach        $this->setBlockSetting($block_id, 'show_timestamp', (string) $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