xref: /webtrees/app/Module/HtmlBlockModule.php (revision 4a1f1d433794f86f893b59883147e57af4e94374)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\Carbon;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Services\HtmlService;
23use Fisharebest\Webtrees\Statistics;
24use Fisharebest\Webtrees\Tree;
25use Illuminate\Support\Str;
26use Psr\Http\Message\ServerRequestInterface;
27
28/**
29 * Class HtmlBlockModule
30 */
31class HtmlBlockModule extends AbstractModule implements ModuleBlockInterface
32{
33    use ModuleBlockTrait;
34
35    /** @var HtmlService */
36    private $html_service;
37
38    /**
39     * HtmlBlockModule bootstrap.
40     *
41     * @param HtmlService $html_service
42     */
43    public function boot(HtmlService $html_service)
44    {
45        $this->html_service = $html_service;
46    }
47
48    /**
49     * How should this module be identified in the control panel, etc.?
50     *
51     * @return string
52     */
53    public function title(): string
54    {
55        /* I18N: Name of a module */
56        return I18N::translate('HTML');
57    }
58
59    /**
60     * A sentence describing what this module does.
61     *
62     * @return string
63     */
64    public function description(): string
65    {
66        /* I18N: Description of the “HTML” module */
67        return I18N::translate('Add your own text and graphics.');
68    }
69
70    /**
71     * Generate the HTML content of this block.
72     *
73     * @param Tree     $tree
74     * @param int      $block_id
75     * @param string   $context
76     * @param string[] $config
77     *
78     * @return string
79     */
80    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
81    {
82        $statistics = app(Statistics::class);
83
84        $title          = $this->getBlockSetting($block_id, 'title');
85        $content        = $this->getBlockSetting($block_id, 'html');
86        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp');
87        $languages      = $this->getBlockSetting($block_id, 'languages');
88
89        // Only show this block for certain languages
90        if ($languages && !in_array(WT_LOCALE, explode(',', $languages), true)) {
91            return '';
92        }
93
94        // Retrieve text, process embedded variables
95        $title   = $statistics->embedTags($title);
96        $content = $statistics->embedTags($content);
97
98        $block_timestamp = (int) $this->getBlockSetting($block_id, 'timestamp', (string) Carbon::now()->unix());
99
100        if ($show_timestamp === '1') {
101            $content .= '<br>' . view('components/datetime', ['timestamp' => Carbon::createFromTimestamp($block_timestamp)]);
102        }
103
104        if ($context !== self::CONTEXT_EMBED) {
105            return view('modules/block-template', [
106                'block'      => Str::kebab($this->name()),
107                'id'         => $block_id,
108                'config_url' => $this->configUrl($tree, $context, $block_id),
109                'title'      => $title,
110                'content'    => $content,
111            ]);
112        }
113
114        return $content;
115    }
116
117    /**
118     * Should this block load asynchronously using AJAX?
119     *
120     * Simple blocks are faster in-line, more complex ones can be loaded later.
121     *
122     * @return bool
123     */
124    public function loadAjax(): bool
125    {
126        return false;
127    }
128
129    /**
130     * Can this block be shown on the user’s home page?
131     *
132     * @return bool
133     */
134    public function isUserBlock(): bool
135    {
136        return true;
137    }
138
139    /**
140     * Can this block be shown on the tree’s home page?
141     *
142     * @return bool
143     */
144    public function isTreeBlock(): bool
145    {
146        return true;
147    }
148
149    /**
150     * Update the configuration for a block.
151     *
152     * @param ServerRequestInterface $request
153     * @param int     $block_id
154     *
155     * @return void
156     */
157    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
158    {
159        $params = $request->getParsedBody();
160
161        $title = $this->html_service->sanitize($params['title']);
162        $html  = $this->html_service->sanitize($params['html']);
163
164        $languages = $params['lang'] ?? [];
165
166        $this->setBlockSetting($block_id, 'title', $title);
167        $this->setBlockSetting($block_id, 'html', $html);
168        $this->setBlockSetting($block_id, 'show_timestamp', $params['show_timestamp']);
169        $this->setBlockSetting($block_id, 'timestamp', (string) Carbon::now()->unix());
170        $this->setBlockSetting($block_id, 'languages', implode(',', $languages));
171    }
172
173    /**
174     * An HTML form to edit block settings
175     *
176     * @param Tree $tree
177     * @param int  $block_id
178     *
179     * @return string
180     */
181    public function editBlockConfiguration(Tree $tree, int $block_id): string
182    {
183        $title          = $this->getBlockSetting($block_id, 'title');
184        $html           = $this->getBlockSetting($block_id, 'html');
185        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
186        $languages      = explode(',', $this->getBlockSetting($block_id, 'languages'));
187        $all_trees      = Tree::getNameList();
188
189        $templates = [
190            $html                                    => I18N::translate('Custom'),
191            view('modules/html/template-keywords')   => I18N::translate('Keyword examples'),
192            view('modules/html/template-narrative')  => I18N::translate('Narrative description'),
193            view('modules/html/template-statistics') => I18N::translate('Statistics'),
194        ];
195
196        return view('modules/html/config', [
197            'all_trees'      => $all_trees,
198            'html'           => $html,
199            'languages'      => $languages,
200            'show_timestamp' => $show_timestamp,
201            'templates'      => $templates,
202            'title'          => $title,
203        ]);
204    }
205}
206