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