xref: /webtrees/app/Module/HtmlBlockModule.php (revision 9b93b7c342a543e7bd55b5a09d97ba0c39fc32b0)
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\Auth;
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   $ctype
77     * @param string[] $cfg
78     *
79     * @return string
80     */
81    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): 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 ($ctype !== '') {
106            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
107                $config_url = route('tree-page-block-edit', [
108                    'block_id' => $block_id,
109                    'ged'      => $tree->name(),
110                ]);
111            } elseif ($ctype === 'user' && Auth::check()) {
112                $config_url = route('user-page-block-edit', [
113                    'block_id' => $block_id,
114                    'ged'      => $tree->name(),
115                ]);
116            } else {
117                $config_url = '';
118            }
119
120            return view('modules/block-template', [
121                'block'      => Str::kebab($this->name()),
122                'id'         => $block_id,
123                'config_url' => $config_url,
124                'title'      => $title,
125                'content'    => $content,
126            ]);
127        }
128
129        return $content;
130    }
131
132    /**
133     * Should this block load asynchronously using AJAX?
134     *
135     * Simple blocks are faster in-line, more comples ones
136     * can be loaded later.
137     *
138     * @return bool
139     */
140    public function loadAjax(): bool
141    {
142        return false;
143    }
144
145    /**
146     * Can this block be shown on the user’s home page?
147     *
148     * @return bool
149     */
150    public function isUserBlock(): bool
151    {
152        return true;
153    }
154
155    /**
156     * Can this block be shown on the tree’s home page?
157     *
158     * @return bool
159     */
160    public function isTreeBlock(): bool
161    {
162        return true;
163    }
164
165    /**
166     * Update the configuration for a block.
167     *
168     * @param ServerRequestInterface $request
169     * @param int     $block_id
170     *
171     * @return void
172     */
173    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
174    {
175        $params = $request->getParsedBody();
176
177        $title = $this->html_service->sanitize($params['title']);
178        $html  = $this->html_service->sanitize($params['html']);
179
180        $languages = $params['lang'] ?? [];
181
182        $this->setBlockSetting($block_id, 'title', $title);
183        $this->setBlockSetting($block_id, 'html', $html);
184        $this->setBlockSetting($block_id, 'show_timestamp', $params['show_timestamp']);
185        $this->setBlockSetting($block_id, 'timestamp', (string) Carbon::now()->unix());
186        $this->setBlockSetting($block_id, 'languages', implode(',', $languages));
187    }
188
189    /**
190     * An HTML form to edit block settings
191     *
192     * @param Tree $tree
193     * @param int  $block_id
194     *
195     * @return void
196     */
197    public function editBlockConfiguration(Tree $tree, int $block_id): void
198    {
199        $title          = $this->getBlockSetting($block_id, 'title');
200        $html           = $this->getBlockSetting($block_id, 'html');
201        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
202        $languages      = explode(',', $this->getBlockSetting($block_id, 'languages'));
203        $all_trees      = Tree::getNameList();
204
205        $templates = [
206            $html                                    => I18N::translate('Custom'),
207            view('modules/html/template-keywords')   => I18N::translate('Keyword examples'),
208            view('modules/html/template-narrative')  => I18N::translate('Narrative description'),
209            view('modules/html/template-statistics') => I18N::translate('Statistics'),
210        ];
211
212        echo view('modules/html/config', [
213            'all_trees'      => $all_trees,
214            'html'           => $html,
215            'languages'      => $languages,
216            'show_timestamp' => $show_timestamp,
217            'templates'      => $templates,
218            'title'          => $title,
219        ]);
220    }
221}
222