xref: /webtrees/app/Module/HtmlBlockModule.php (revision 6fd4f6e3d5a2c64c159e88e10883a1ccab4d4a42)
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\Statistics;
24use Fisharebest\Webtrees\Tree;
25use Symfony\Component\HttpFoundation\Request;
26
27/**
28 * Class HtmlBlockModule
29 */
30class HtmlBlockModule extends AbstractModule implements ModuleBlockInterface
31{
32    use ModuleBlockTrait;
33
34    /**
35     * @var Statistics
36     */
37    private $statistics;
38
39    /**
40     * TopGivenNamesModule constructor.
41     *
42     * @param Statistics $statistics
43     */
44    public function __construct(Statistics $statistics)
45    {
46        $this->statistics = $statistics;
47    }
48
49    /**
50     * How should this module be labelled on tabs, menus, 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        $title          = $this->getBlockSetting($block_id, 'title', '');
84        $content        = $this->getBlockSetting($block_id, 'html', '');
85        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
86        $languages      = $this->getBlockSetting($block_id, 'languages');
87
88        // Only show this block for certain languages
89        if ($languages && !in_array(WT_LOCALE, explode(',', $languages))) {
90            return '';
91        }
92
93        /*
94        * Retrieve text, process embedded variables
95        */
96        $title   = $this->statistics->embedTags($title);
97        $content = $this->statistics->embedTags($content);
98
99        if ($show_timestamp === '1') {
100            $content .= '<br>' . FunctionsDate::formatTimestamp((int) $this->getBlockSetting($block_id, 'timestamp', (string) WT_TIMESTAMP));
101        }
102
103        if ($ctype !== '') {
104            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
105                $config_url = route('tree-page-block-edit', [
106                    'block_id' => $block_id,
107                    'ged'      => $tree->name(),
108                ]);
109            } elseif ($ctype === 'user' && Auth::check()) {
110                $config_url = route('user-page-block-edit', [
111                    'block_id' => $block_id,
112                    'ged'      => $tree->name(),
113                ]);
114            } else {
115                $config_url = '';
116            }
117
118            return view('modules/block-template', [
119                'block'      => str_replace('_', '-', $this->name()),
120                'id'         => $block_id,
121                'config_url' => $config_url,
122                'title'      => $title,
123                'content'    => $content,
124            ]);
125        }
126
127        return $content;
128    }
129
130    /** {@inheritdoc} */
131    public function loadAjax(): bool
132    {
133        return false;
134    }
135
136    /** {@inheritdoc} */
137    public function isUserBlock(): bool
138    {
139        return true;
140    }
141
142    /** {@inheritdoc} */
143    public function isTreeBlock(): bool
144    {
145        return true;
146    }
147
148    /**
149     * Update the configuration for a block.
150     *
151     * @param Request $request
152     * @param int     $block_id
153     *
154     * @return void
155     */
156    public function saveBlockConfiguration(Request $request, int $block_id)
157    {
158        $languages = (array) $request->get('lang');
159        $this->setBlockSetting($block_id, 'title', $request->get('title', ''));
160        $this->setBlockSetting($block_id, 'html', $request->get('html', ''));
161        $this->setBlockSetting($block_id, 'show_timestamp', $request->get('show_timestamp', ''));
162        $this->setBlockSetting($block_id, 'timestamp', $request->get('timestamp', ''));
163        $this->setBlockSetting($block_id, 'languages', implode(',', $languages));
164    }
165
166    /**
167     * An HTML form to edit block settings
168     *
169     * @param Tree $tree
170     * @param int  $block_id
171     *
172     * @return void
173     */
174    public function editBlockConfiguration(Tree $tree, int $block_id)
175    {
176        $templates = [
177            I18N::translate('Keyword examples')      => view('modules/html/template-keywords', []),
178            I18N::translate('Narrative description') => view('modules/html/template-narrative', []),
179            I18N::translate('Statistics')            => view('modules/html/template-statistics', []),
180        ];
181
182        $title          = $this->getBlockSetting($block_id, 'title', '');
183        $html           = $this->getBlockSetting($block_id, 'html', '');
184        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
185        $languages      = explode(',', $this->getBlockSetting($block_id, 'languages'));
186        $all_trees      = Tree::getNameList();
187
188        echo view('modules/html/config', [
189            'all_trees'      => $all_trees,
190            'html'           => $html,
191            'languages'      => $languages,
192            'show_timestamp' => $show_timestamp,
193            'templates'      => $templates,
194            'title'          => $title,
195        ]);
196    }
197}
198