xref: /webtrees/app/Module/HtmlBlockModule.php (revision a743d8a2f9a2ec24120e80ab37b2b699bcfc2694)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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
224459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
2450d6f48cSGreg Roachuse Fisharebest\Webtrees\Services\HtmlService;
259219296aSGreg Roachuse Fisharebest\Webtrees\Statistics;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
271e7a7a28SGreg Roachuse Illuminate\Support\Str;
286ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
298c2e8227SGreg Roach
308c2e8227SGreg Roach/**
318c2e8227SGreg Roach * Class HtmlBlockModule
328c2e8227SGreg Roach */
3337eb8894SGreg Roachclass HtmlBlockModule extends AbstractModule implements ModuleBlockInterface
34c1010edaSGreg Roach{
3549a243cbSGreg Roach    use ModuleBlockTrait;
3649a243cbSGreg Roach
3750d6f48cSGreg Roach    /** @var HtmlService */
3850d6f48cSGreg Roach    private $html_service;
3950d6f48cSGreg Roach
4050d6f48cSGreg Roach    /**
4150d6f48cSGreg Roach     * HtmlBlockModule bootstrap.
4250d6f48cSGreg Roach     *
4350d6f48cSGreg Roach     * @param HtmlService $html_service
4450d6f48cSGreg Roach     */
457c2c99faSGreg Roach    public function __construct(HtmlService $html_service)
4650d6f48cSGreg Roach    {
4750d6f48cSGreg Roach        $this->html_service = $html_service;
4850d6f48cSGreg Roach    }
4950d6f48cSGreg Roach
50961ec755SGreg Roach    /**
510cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
52961ec755SGreg Roach     *
53961ec755SGreg Roach     * @return string
54961ec755SGreg Roach     */
5549a243cbSGreg Roach    public function title(): string
56c1010edaSGreg Roach    {
57bbb76c12SGreg Roach        /* I18N: Name of a module */
58bbb76c12SGreg Roach        return I18N::translate('HTML');
598c2e8227SGreg Roach    }
608c2e8227SGreg Roach
61961ec755SGreg Roach    /**
62961ec755SGreg Roach     * A sentence describing what this module does.
63961ec755SGreg Roach     *
64961ec755SGreg Roach     * @return string
65961ec755SGreg Roach     */
6649a243cbSGreg Roach    public function description(): string
67c1010edaSGreg Roach    {
68bbb76c12SGreg Roach        /* I18N: Description of the “HTML” module */
69bbb76c12SGreg Roach        return I18N::translate('Add your own text and graphics.');
708c2e8227SGreg Roach    }
718c2e8227SGreg Roach
7276692c8bSGreg Roach    /**
7376692c8bSGreg Roach     * Generate the HTML content of this block.
7476692c8bSGreg Roach     *
75e490cd80SGreg Roach     * @param Tree     $tree
7676692c8bSGreg Roach     * @param int      $block_id
773caaa4d2SGreg Roach     * @param string   $context
783caaa4d2SGreg Roach     * @param string[] $config
7976692c8bSGreg Roach     *
8076692c8bSGreg Roach     * @return string
8176692c8bSGreg Roach     */
823caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
83c1010edaSGreg Roach    {
84cab242e7SGreg Roach        $statistics = app(Statistics::class);
85bf57b580SGreg Roach
8650d6f48cSGreg Roach        $title          = $this->getBlockSetting($block_id, 'title');
8750d6f48cSGreg Roach        $content        = $this->getBlockSetting($block_id, 'html');
8850d6f48cSGreg Roach        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp');
89e2a378d3SGreg Roach        $languages      = $this->getBlockSetting($block_id, 'languages');
908c2e8227SGreg Roach
918c2e8227SGreg Roach        // Only show this block for certain languages
9265cf5706SGreg Roach        if ($languages && !in_array(I18N::languageTag(), explode(',', $languages), true)) {
938c2e8227SGreg Roach            return '';
948c2e8227SGreg Roach        }
958c2e8227SGreg Roach
96ca52a408SGreg Roach        // Retrieve text, process embedded variables
97bf57b580SGreg Roach        $title   = $statistics->embedTags($title);
98bf57b580SGreg Roach        $content = $statistics->embedTags($content);
998c2e8227SGreg Roach
1004459dc9aSGreg Roach        $block_timestamp = (int) $this->getBlockSetting($block_id, 'timestamp', (string) Carbon::now()->unix());
101ca52a408SGreg Roach
102ad98d39dSGreg Roach        if ($show_timestamp === '1') {
1034459dc9aSGreg Roach            $content .= '<br>' . view('components/datetime', ['timestamp' => Carbon::createFromTimestamp($block_timestamp)]);
1048c2e8227SGreg Roach        }
1058c2e8227SGreg Roach
1063caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
107147e99aaSGreg Roach            return view('modules/block-template', [
1081e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1099c6524dcSGreg Roach                'id'         => $block_id,
1103caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
111*a743d8a2SGreg Roach                'title'      => e($title),
1129c6524dcSGreg Roach                'content'    => $content,
1139c6524dcSGreg Roach            ]);
1148c2e8227SGreg Roach        }
115b2ce94c6SRico Sonntag
116b2ce94c6SRico Sonntag        return $content;
1178c2e8227SGreg Roach    }
1188c2e8227SGreg Roach
11950d6f48cSGreg Roach    /**
12050d6f48cSGreg Roach     * Should this block load asynchronously using AJAX?
12150d6f48cSGreg Roach     *
1223caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
12350d6f48cSGreg Roach     *
12450d6f48cSGreg Roach     * @return bool
12550d6f48cSGreg Roach     */
126c1010edaSGreg Roach    public function loadAjax(): bool
127c1010edaSGreg Roach    {
1288c2e8227SGreg Roach        return false;
1298c2e8227SGreg Roach    }
1308c2e8227SGreg Roach
13150d6f48cSGreg Roach    /**
13250d6f48cSGreg Roach     * Can this block be shown on the user’s home page?
13350d6f48cSGreg Roach     *
13450d6f48cSGreg Roach     * @return bool
13550d6f48cSGreg Roach     */
136c1010edaSGreg Roach    public function isUserBlock(): bool
137c1010edaSGreg Roach    {
1388c2e8227SGreg Roach        return true;
1398c2e8227SGreg Roach    }
1408c2e8227SGreg Roach
14150d6f48cSGreg Roach    /**
14250d6f48cSGreg Roach     * Can this block be shown on the tree’s home page?
14350d6f48cSGreg Roach     *
14450d6f48cSGreg Roach     * @return bool
14550d6f48cSGreg Roach     */
14663276d8fSGreg Roach    public function isTreeBlock(): bool
147c1010edaSGreg Roach    {
1488c2e8227SGreg Roach        return true;
1498c2e8227SGreg Roach    }
1508c2e8227SGreg Roach
15176692c8bSGreg Roach    /**
152a45f9889SGreg Roach     * Update the configuration for a block.
153a45f9889SGreg Roach     *
1546ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
155a45f9889SGreg Roach     * @param int     $block_id
156a45f9889SGreg Roach     *
157a45f9889SGreg Roach     * @return void
158a45f9889SGreg Roach     */
1596ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
160a45f9889SGreg Roach    {
161b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
162e106ff43SGreg Roach
16350d6f48cSGreg Roach        $title = $this->html_service->sanitize($params['title']);
16450d6f48cSGreg Roach        $html  = $this->html_service->sanitize($params['html']);
16550d6f48cSGreg Roach
166e73fb82fSGreg Roach        $languages = $params['languages'] ?? [];
16750d6f48cSGreg Roach
16850d6f48cSGreg Roach        $this->setBlockSetting($block_id, 'title', $title);
16950d6f48cSGreg Roach        $this->setBlockSetting($block_id, 'html', $html);
170e106ff43SGreg Roach        $this->setBlockSetting($block_id, 'show_timestamp', $params['show_timestamp']);
1714459dc9aSGreg Roach        $this->setBlockSetting($block_id, 'timestamp', (string) Carbon::now()->unix());
172a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'languages', implode(',', $languages));
173a45f9889SGreg Roach    }
174a45f9889SGreg Roach
175a45f9889SGreg Roach    /**
17676692c8bSGreg Roach     * An HTML form to edit block settings
17776692c8bSGreg Roach     *
178e490cd80SGreg Roach     * @param Tree $tree
17976692c8bSGreg Roach     * @param int  $block_id
180a9430be8SGreg Roach     *
1813caaa4d2SGreg Roach     * @return string
18276692c8bSGreg Roach     */
1833caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
184c1010edaSGreg Roach    {
18550d6f48cSGreg Roach        $title          = $this->getBlockSetting($block_id, 'title');
18650d6f48cSGreg Roach        $html           = $this->getBlockSetting($block_id, 'html');
187e2a378d3SGreg Roach        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
188e2a378d3SGreg Roach        $languages      = explode(',', $this->getBlockSetting($block_id, 'languages'));
1898c2e8227SGreg Roach
190b6c326d8SGreg Roach        $templates = [
191b6c326d8SGreg Roach            $html                                    => I18N::translate('Custom'),
192b6c326d8SGreg Roach            view('modules/html/template-keywords')   => I18N::translate('Keyword examples'),
193b6c326d8SGreg Roach            view('modules/html/template-narrative')  => I18N::translate('Narrative description'),
194fd303cbfSGreg Roach            view('modules/html/template-statistics', ['tree' => $tree]) => I18N::translate('Statistics'),
195b6c326d8SGreg Roach        ];
196b6c326d8SGreg Roach
1973caaa4d2SGreg Roach        return view('modules/html/config', [
198c385536dSGreg Roach            'html'           => $html,
199c385536dSGreg Roach            'languages'      => $languages,
200c385536dSGreg Roach            'show_timestamp' => $show_timestamp,
201c385536dSGreg Roach            'templates'      => $templates,
202c385536dSGreg Roach            'title'          => $title,
203c385536dSGreg Roach        ]);
2048c2e8227SGreg Roach    }
2058c2e8227SGreg Roach}
206