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