xref: /webtrees/app/Module/HtmlBlockModule.php (revision 94026f200c17dcfccf296678dc90ea88b14f6246)
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\Functions\FunctionsDate;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Stats;
24use Fisharebest\Webtrees\Tree;
25use Symfony\Component\HttpFoundation\Request;
26
27/**
28 * Class HtmlBlockModule
29 */
30class HtmlBlockModule extends AbstractModule implements ModuleBlockInterface
31{
32    /** {@inheritdoc} */
33    public function getTitle(): string
34    {
35        /* I18N: Name of a module */
36        return I18N::translate('HTML');
37    }
38
39    /** {@inheritdoc} */
40    public function getDescription(): string
41    {
42        /* I18N: Description of the “HTML” module */
43        return I18N::translate('Add your own text and graphics.');
44    }
45
46    /**
47     * Generate the HTML content of this block.
48     *
49     * @param Tree     $tree
50     * @param int      $block_id
51     * @param string   $ctype
52     * @param string[] $cfg
53     *
54     * @return string
55     */
56    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
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', (string) WT_TIMESTAMP) + WT_TIMESTAMP_OFFSET);
78        }
79
80        if ($ctype !== '') {
81            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
82                $config_url = route('tree-page-block-edit', [
83                    'block_id' => $block_id,
84                    'ged'      => $tree->name(),
85                ]);
86            } elseif ($ctype === 'user' && Auth::check()) {
87                $config_url = route('user-page-block-edit', [
88                    'block_id' => $block_id,
89                    'ged'      => $tree->name(),
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        }
103
104        return $content;
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