xref: /webtrees/app/Module/HtmlBlockModule.php (revision 43e2aec9e526d1a242c3e80ff940736aec157724)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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 */
16namespace Fisharebest\Webtrees\Module;
17
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Filter;
20use Fisharebest\Webtrees\Functions\FunctionsDate;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Stats;
23use Fisharebest\Webtrees\Tree;
24
25/**
26 * Class HtmlBlockModule
27 */
28class HtmlBlockModule extends AbstractModule implements ModuleBlockInterface
29{
30    /** {@inheritdoc} */
31    public function getTitle()
32    {
33        return /* I18N: Name of a module */
34            I18N::translate('HTML');
35    }
36
37    /** {@inheritdoc} */
38    public function getDescription()
39    {
40        return /* I18N: Description of the “HTML” module */
41            I18N::translate('Add your own text and graphics.');
42    }
43
44    /**
45     * Generate the HTML content of this block.
46     *
47     * @param Tree     $tree
48     * @param int      $block_id
49     * @param bool     $template
50     * @param string[] $cfg
51     *
52     * @return string
53     */
54    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
55    {
56        global $ctype;
57
58        $title          = $this->getBlockSetting($block_id, 'title', '');
59        $content        = $this->getBlockSetting($block_id, 'html', '');
60        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
61        $languages      = $this->getBlockSetting($block_id, 'languages');
62
63        // Only show this block for certain languages
64        if ($languages && !in_array(WT_LOCALE, explode(',', $languages))) {
65            return '';
66        }
67
68        $stats = new Stats($tree);
69
70        /*
71        * Retrieve text, process embedded variables
72        */
73        $title   = $stats->embedTags($title);
74        $content = $stats->embedTags($content);
75
76        if ($show_timestamp === '1') {
77            $content .= '<br>' . FunctionsDate::formatTimestamp((int)$this->getBlockSetting($block_id, 'timestamp', WT_TIMESTAMP) + WT_TIMESTAMP_OFFSET);
78        }
79
80        if ($template) {
81            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
82                $config_url = route('tree-page-block-edit', [
83                    'block_id' => $block_id,
84                    'ged'      => $tree->getName(),
85                ]);
86            } elseif ($ctype === 'user' && Auth::check()) {
87                $config_url = route('user-page-block-edit', [
88                    'block_id' => $block_id,
89                    'ged'      => $tree->getName(),
90                ]);
91            } else {
92                $config_url = '';
93            }
94
95            return view('modules/block-template', [
96                'block'      => str_replace('_', '-', $this->getName()),
97                'id'         => $block_id,
98                'config_url' => $config_url,
99                'title'      => $title,
100                'content'    => $content,
101            ]);
102        } else {
103            return $content;
104        }
105    }
106
107    /** {@inheritdoc} */
108    public function loadAjax(): bool
109    {
110        return false;
111    }
112
113    /** {@inheritdoc} */
114    public function isUserBlock(): bool
115    {
116        return true;
117    }
118
119    /** {@inheritdoc} */
120    public function isGedcomBlock(): bool
121    {
122        return true;
123    }
124
125    /**
126     * An HTML form to edit block settings
127     *
128     * @param Tree $tree
129     * @param int  $block_id
130     *
131     * @return void
132     */
133    public function configureBlock(Tree $tree, int $block_id)
134    {
135        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
136            $languages = Filter::postArray('lang');
137            $this->setBlockSetting($block_id, 'title', Filter::post('title'));
138            $this->setBlockSetting($block_id, 'html', Filter::post('html'));
139            $this->setBlockSetting($block_id, 'show_timestamp', Filter::postBool('show_timestamp'));
140            $this->setBlockSetting($block_id, 'timestamp', Filter::post('timestamp'));
141            $this->setBlockSetting($block_id, 'languages', implode(',', $languages));
142
143            return;
144        }
145
146        $templates = [
147            I18N::translate('Keyword examples')      => view('modules/html/template-keywords', []),
148            I18N::translate('Narrative description') => view('modules/html/template-narrative', []),
149            I18N::translate('Statistics')            => view('modules/html/template-statistics', []),
150        ];
151
152        $title          = $this->getBlockSetting($block_id, 'title', '');
153        $html           = $this->getBlockSetting($block_id, 'html', '');
154        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
155        $languages      = explode(',', $this->getBlockSetting($block_id, 'languages'));
156        $all_trees      = Tree::getNameList();
157
158        echo view('modules/html/config', [
159            'all_trees'      => $all_trees,
160            'html'           => $html,
161            'languages'      => $languages,
162            'show_timestamp' => $show_timestamp,
163            'templates'      => $templates,
164            'title'          => $title,
165        ]);
166    }
167}
168