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