xref: /webtrees/app/Module/HtmlBlockModule.php (revision 873953697c930fadbf3243d2b8c0029fd684da0e)
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\Functions\FunctionsDate;
20use Fisharebest\Webtrees\I18N;
21use Fisharebest\Webtrees\Stats;
22use Fisharebest\Webtrees\Tree;
23use Symfony\Component\HttpFoundation\Request;
24
25/**
26 * Class HtmlBlockModule
27 */
28class HtmlBlockModule extends AbstractModule implements ModuleBlockInterface
29{
30    /** {@inheritdoc} */
31    public function getTitle(): string
32    {
33        /* I18N: Name of a module */
34        return I18N::translate('HTML');
35    }
36
37    /** {@inheritdoc} */
38    public function getDescription(): string
39    {
40        /* I18N: Description of the “HTML” module */
41        return 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     * Update the configuration for a block.
127     *
128     * @param Request $request
129     * @param int     $block_id
130     *
131     * @return void
132     */
133    public function saveBlockConfiguration(Request $request, int $block_id)
134    {
135        $languages = (array) $request->get('lang');
136        $this->setBlockSetting($block_id, 'title', $request->get('title', ''));
137        $this->setBlockSetting($block_id, 'html', $request->get('html', ''));
138        $this->setBlockSetting($block_id, 'show_timestamp', $request->get('show_timestamp', ''));
139        $this->setBlockSetting($block_id, 'timestamp', $request->get('timestamp', ''));
140        $this->setBlockSetting($block_id, 'languages', implode(',', $languages));
141    }
142
143    /**
144     * An HTML form to edit block settings
145     *
146     * @param Tree $tree
147     * @param int  $block_id
148     *
149     * @return void
150     */
151    public function editBlockConfiguration(Tree $tree, int $block_id)
152    {
153        $templates = [
154            I18N::translate('Keyword examples')      => view('modules/html/template-keywords', []),
155            I18N::translate('Narrative description') => view('modules/html/template-narrative', []),
156            I18N::translate('Statistics')            => view('modules/html/template-statistics', []),
157        ];
158
159        $title          = $this->getBlockSetting($block_id, 'title', '');
160        $html           = $this->getBlockSetting($block_id, 'html', '');
161        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
162        $languages      = explode(',', $this->getBlockSetting($block_id, 'languages'));
163        $all_trees      = Tree::getNameList();
164
165        echo view('modules/html/config', [
166            'all_trees'      => $all_trees,
167            'html'           => $html,
168            'languages'      => $languages,
169            'show_timestamp' => $show_timestamp,
170            'templates'      => $templates,
171            'title'          => $title,
172        ]);
173    }
174}
175