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