xref: /webtrees/app/Module/HtmlBlockModule.php (revision 6b1381a306f9b3ddbe167a71b6a607d5ab7a609d)
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\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    /**
36     * How should this module be identified in the control panel, etc.?
37     *
38     * @return string
39     */
40    public function title(): string
41    {
42        /* I18N: Name of a module */
43        return I18N::translate('HTML');
44    }
45
46    /**
47     * A sentence describing what this module does.
48     *
49     * @return string
50     */
51    public function description(): string
52    {
53        /* I18N: Description of the “HTML” module */
54        return I18N::translate('Add your own text and graphics.');
55    }
56
57    /**
58     * Generate the HTML content of this block.
59     *
60     * @param Tree     $tree
61     * @param int      $block_id
62     * @param string   $ctype
63     * @param string[] $cfg
64     *
65     * @return string
66     */
67    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
68    {
69        $statistics = app(Statistics::class);
70
71        $title          = $this->getBlockSetting($block_id, 'title', '');
72        $content        = $this->getBlockSetting($block_id, 'html', '');
73        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
74        $languages      = $this->getBlockSetting($block_id, 'languages');
75
76        // Only show this block for certain languages
77        if ($languages && !in_array(WT_LOCALE, explode(',', $languages), true)) {
78            return '';
79        }
80
81        // Retrieve text, process embedded variables
82        $title   = $statistics->embedTags($title);
83        $content = $statistics->embedTags($content);
84
85        $block_timestamp = (int) $this->getBlockSetting($block_id, 'timestamp', (string) Carbon::now()->unix());
86
87        if ($show_timestamp === '1') {
88            $content .= '<br>' . view('components/datetime', ['timestamp' => Carbon::createFromTimestamp($block_timestamp)]);
89        }
90
91        if ($ctype !== '') {
92            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
93                $config_url = route('tree-page-block-edit', [
94                    'block_id' => $block_id,
95                    'ged'      => $tree->name(),
96                ]);
97            } elseif ($ctype === 'user' && Auth::check()) {
98                $config_url = route('user-page-block-edit', [
99                    'block_id' => $block_id,
100                    'ged'      => $tree->name(),
101                ]);
102            } else {
103                $config_url = '';
104            }
105
106            return view('modules/block-template', [
107                'block'      => Str::kebab($this->name()),
108                'id'         => $block_id,
109                'config_url' => $config_url,
110                'title'      => $title,
111                'content'    => $content,
112            ]);
113        }
114
115        return $content;
116    }
117
118    /** {@inheritdoc} */
119    public function loadAjax(): bool
120    {
121        return false;
122    }
123
124    /** {@inheritdoc} */
125    public function isUserBlock(): bool
126    {
127        return true;
128    }
129
130    /** {@inheritdoc} */
131    public function isTreeBlock(): bool
132    {
133        return true;
134    }
135
136    /**
137     * Update the configuration for a block.
138     *
139     * @param ServerRequestInterface $request
140     * @param int     $block_id
141     *
142     * @return void
143     */
144    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
145    {
146        $params = $request->getParsedBody();
147
148        $languages = $params['lang'] ?? [];
149        $this->setBlockSetting($block_id, 'title', $params['title']);
150        $this->setBlockSetting($block_id, 'html', $params['html']);
151        $this->setBlockSetting($block_id, 'show_timestamp', $params['show_timestamp']);
152        $this->setBlockSetting($block_id, 'timestamp', (string) Carbon::now()->unix());
153        $this->setBlockSetting($block_id, 'languages', implode(',', $languages));
154    }
155
156    /**
157     * An HTML form to edit block settings
158     *
159     * @param Tree $tree
160     * @param int  $block_id
161     *
162     * @return void
163     */
164    public function editBlockConfiguration(Tree $tree, int $block_id): void
165    {
166        $title          = $this->getBlockSetting($block_id, 'title', '');
167        $html           = $this->getBlockSetting($block_id, 'html', '');
168        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
169        $languages      = explode(',', $this->getBlockSetting($block_id, 'languages'));
170        $all_trees      = Tree::getNameList();
171
172        $templates = [
173            $html                                    => I18N::translate('Custom'),
174            view('modules/html/template-keywords')   => I18N::translate('Keyword examples'),
175            view('modules/html/template-narrative')  => I18N::translate('Narrative description'),
176            view('modules/html/template-statistics') => I18N::translate('Statistics'),
177        ];
178
179        echo view('modules/html/config', [
180            'all_trees'      => $all_trees,
181            'html'           => $html,
182            'languages'      => $languages,
183            'show_timestamp' => $show_timestamp,
184            'templates'      => $templates,
185            'title'          => $title,
186        ]);
187    }
188}
189