xref: /webtrees/app/Module/HtmlBlockModule.php (revision 6c21f8bee87860f3207f654266c478a05acfbcba)
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 Fisharebest\Webtrees\Validator;
28use Illuminate\Support\Str;
29use Psr\Http\Message\ServerRequestInterface;
30
31use function time;
32
33/**
34 * Class HtmlBlockModule
35 */
36class HtmlBlockModule extends AbstractModule implements ModuleBlockInterface
37{
38    use ModuleBlockTrait;
39
40    private HtmlService $html_service;
41
42    /**
43     * HtmlBlockModule bootstrap.
44     *
45     * @param HtmlService $html_service
46     */
47    public function __construct(HtmlService $html_service)
48    {
49        $this->html_service = $html_service;
50    }
51
52    /**
53     * How should this module be identified in the control panel, etc.?
54     *
55     * @return string
56     */
57    public function title(): string
58    {
59        /* I18N: Name of a module */
60        return I18N::translate('HTML');
61    }
62
63    /**
64     * A sentence describing what this module does.
65     *
66     * @return string
67     */
68    public function description(): string
69    {
70        /* I18N: Description of the “HTML” module */
71        return I18N::translate('Add your own text and graphics.');
72    }
73
74    /**
75     * Generate the HTML content of this block.
76     *
77     * @param Tree                 $tree
78     * @param int                  $block_id
79     * @param string               $context
80     * @param array<string,string> $config
81     *
82     * @return string
83     */
84    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
85    {
86        $statistics = app(Statistics::class);
87
88        $title          = $this->getBlockSetting($block_id, 'title');
89        $content        = $this->getBlockSetting($block_id, 'html');
90        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp');
91        $languages      = $this->getBlockSetting($block_id, 'languages');
92
93        // Only show this block for certain languages
94        if ($languages && !in_array(I18N::languageTag(), explode(',', $languages), true)) {
95            return '';
96        }
97
98        // Retrieve text, process embedded variables
99        $title   = $statistics->embedTags($title);
100        $content = $statistics->embedTags($content);
101
102        $block_timestamp = (int) $this->getBlockSetting($block_id, 'timestamp', (string) time());
103
104        if ($show_timestamp === '1') {
105            $content .= '<br>' . view('components/datetime', ['timestamp' => Registry::timestampFactory()->make($block_timestamp)]);
106        }
107
108        if ($context !== self::CONTEXT_EMBED) {
109            return view('modules/block-template', [
110                'block'      => Str::kebab($this->name()),
111                'id'         => $block_id,
112                'config_url' => $this->configUrl($tree, $context, $block_id),
113                'title'      => e(strip_tags($title)),
114                'content'    => $content,
115            ]);
116        }
117
118        return $content;
119    }
120
121    /**
122     * Should this block load asynchronously using AJAX?
123     *
124     * Simple blocks are faster in-line, more complex ones can be loaded later.
125     *
126     * @return bool
127     */
128    public function loadAjax(): bool
129    {
130        return false;
131    }
132
133    /**
134     * Can this block be shown on the user’s home page?
135     *
136     * @return bool
137     */
138    public function isUserBlock(): bool
139    {
140        return true;
141    }
142
143    /**
144     * Can this block be shown on the tree’s home page?
145     *
146     * @return bool
147     */
148    public function isTreeBlock(): bool
149    {
150        return true;
151    }
152
153    /**
154     * Update the configuration for a block.
155     *
156     * @param ServerRequestInterface $request
157     * @param int                    $block_id
158     *
159     * @return void
160     */
161    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
162    {
163        $title          = Validator::parsedBody($request)->string('title');
164        $html           = Validator::parsedBody($request)->string('html');
165        $show_timestamp = Validator::parsedBody($request)->boolean('show_timestamp');
166        $languages      = Validator::parsedBody($request)->array('languages');
167
168        $this->setBlockSetting($block_id, 'title', $title);
169        $this->setBlockSetting($block_id, 'html', $this->html_service->sanitize($html));
170        $this->setBlockSetting($block_id, 'show_timestamp', (string) $show_timestamp);
171        $this->setBlockSetting($block_id, 'timestamp', (string) time());
172        $this->setBlockSetting($block_id, 'languages', implode(',', $languages));
173    }
174
175    /**
176     * An HTML form to edit block settings
177     *
178     * @param Tree $tree
179     * @param int  $block_id
180     *
181     * @return string
182     */
183    public function editBlockConfiguration(Tree $tree, int $block_id): string
184    {
185        $title          = $this->getBlockSetting($block_id, 'title');
186        $html           = $this->getBlockSetting($block_id, 'html');
187        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
188        $languages      = explode(',', $this->getBlockSetting($block_id, 'languages'));
189
190        $templates = [
191            $html                                                       => I18N::translate('Custom'),
192            view('modules/html/template-keywords')                      => I18N::translate('Keyword examples'),
193            view('modules/html/template-narrative')                     => I18N::translate('Narrative description'),
194            view('modules/html/template-statistics', ['tree' => $tree]) => I18N::translate('Statistics'),
195        ];
196
197        return view('modules/html/config', [
198            'html'           => $html,
199            'languages'      => $languages,
200            'show_timestamp' => $show_timestamp,
201            'templates'      => $templates,
202            'title'          => $title,
203        ]);
204    }
205}
206