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