xref: /webtrees/app/Module/HtmlBlockModule.php (revision d556820b1a7ded827b1d7cc803988e0ebd0451f7)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Registry;
24use Fisharebest\Webtrees\Services\HtmlService;
25use Fisharebest\Webtrees\Statistics;
26use Fisharebest\Webtrees\Tree;
27use Illuminate\Support\Str;
28use Psr\Http\Message\ServerRequestInterface;
29
30use function time;
31
32/**
33 * Class HtmlBlockModule
34 */
35class HtmlBlockModule extends AbstractModule implements ModuleBlockInterface
36{
37    use ModuleBlockTrait;
38
39    private HtmlService $html_service;
40
41    /**
42     * HtmlBlockModule bootstrap.
43     *
44     * @param HtmlService $html_service
45     */
46    public function __construct(HtmlService $html_service)
47    {
48        $this->html_service = $html_service;
49    }
50
51    /**
52     * How should this module be identified in the control panel, etc.?
53     *
54     * @return string
55     */
56    public function title(): string
57    {
58        /* I18N: Name of a module */
59        return I18N::translate('HTML');
60    }
61
62    /**
63     * A sentence describing what this module does.
64     *
65     * @return string
66     */
67    public function description(): string
68    {
69        /* I18N: Description of the “HTML” module */
70        return I18N::translate('Add your own text and graphics.');
71    }
72
73    /**
74     * Generate the HTML content of this block.
75     *
76     * @param Tree                 $tree
77     * @param int                  $block_id
78     * @param string               $context
79     * @param array<string,string> $config
80     *
81     * @return string
82     */
83    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
84    {
85        $statistics = app(Statistics::class);
86
87        $title          = $this->getBlockSetting($block_id, 'title');
88        $content        = $this->getBlockSetting($block_id, 'html');
89        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp');
90        $languages      = $this->getBlockSetting($block_id, 'languages');
91
92        // Only show this block for certain languages
93        if ($languages && !in_array(I18N::languageTag(), explode(',', $languages), true)) {
94            return '';
95        }
96
97        // Retrieve text, process embedded variables
98        $title   = $statistics->embedTags($title);
99        $content = $statistics->embedTags($content);
100
101        $block_timestamp = (int) $this->getBlockSetting($block_id, 'timestamp', (string) time());
102
103        if ($show_timestamp === '1') {
104            $content .= '<br>' . view('components/datetime', ['timestamp' => Registry::timestampFactory()->make($block_timestamp)]);
105        }
106
107        if ($context !== self::CONTEXT_EMBED) {
108            return view('modules/block-template', [
109                'block'      => Str::kebab($this->name()),
110                'id'         => $block_id,
111                'config_url' => $this->configUrl($tree, $context, $block_id),
112                'title'      => e($title),
113                'content'    => $content,
114            ]);
115        }
116
117        return $content;
118    }
119
120    /**
121     * Should this block load asynchronously using AJAX?
122     *
123     * Simple blocks are faster in-line, more complex ones can be loaded later.
124     *
125     * @return bool
126     */
127    public function loadAjax(): bool
128    {
129        return false;
130    }
131
132    /**
133     * Can this block be shown on the user’s home page?
134     *
135     * @return bool
136     */
137    public function isUserBlock(): bool
138    {
139        return true;
140    }
141
142    /**
143     * Can this block be shown on the tree’s home page?
144     *
145     * @return bool
146     */
147    public function isTreeBlock(): bool
148    {
149        return true;
150    }
151
152    /**
153     * Update the configuration for a block.
154     *
155     * @param ServerRequestInterface $request
156     * @param int     $block_id
157     *
158     * @return void
159     */
160    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
161    {
162        $params = (array) $request->getParsedBody();
163
164        $title = $this->html_service->sanitize($params['title']);
165        $html  = $this->html_service->sanitize($params['html']);
166
167        $languages = $params['languages'] ?? [];
168
169        $this->setBlockSetting($block_id, 'title', $title);
170        $this->setBlockSetting($block_id, 'html', $html);
171        $this->setBlockSetting($block_id, 'show_timestamp', $params['show_timestamp']);
172        $this->setBlockSetting($block_id, 'timestamp', (string) time());
173        $this->setBlockSetting($block_id, 'languages', implode(',', $languages));
174    }
175
176    /**
177     * An HTML form to edit block settings
178     *
179     * @param Tree $tree
180     * @param int  $block_id
181     *
182     * @return string
183     */
184    public function editBlockConfiguration(Tree $tree, int $block_id): string
185    {
186        $title          = $this->getBlockSetting($block_id, 'title');
187        $html           = $this->getBlockSetting($block_id, 'html');
188        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
189        $languages      = explode(',', $this->getBlockSetting($block_id, 'languages'));
190
191        $templates = [
192            $html                                    => I18N::translate('Custom'),
193            view('modules/html/template-keywords')   => I18N::translate('Keyword examples'),
194            view('modules/html/template-narrative')  => I18N::translate('Narrative description'),
195            view('modules/html/template-statistics', ['tree' => $tree]) => I18N::translate('Statistics'),
196        ];
197
198        return view('modules/html/config', [
199            'html'           => $html,
200            'languages'      => $languages,
201            'show_timestamp' => $show_timestamp,
202            'templates'      => $templates,
203            'title'          => $title,
204        ]);
205    }
206}
207