xref: /webtrees/app/Module/HtmlBlockModule.php (revision 9af6b024736711ef85eba12979344b0241b8b348)
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\Carbon;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Statistics;
24use Fisharebest\Webtrees\Tree;
25use Illuminate\Support\Str;
26use Symfony\Component\HttpFoundation\Request;
27
28/**
29 * Class HtmlBlockModule
30 */
31class HtmlBlockModule extends AbstractModule implements ModuleBlockInterface
32{
33    use ModuleBlockTrait;
34
35    /**
36     * How should this module be identified in the control panel, etc.?
37     *
38     * @return string
39     */
40    public function title(): string
41    {
42        /* I18N: Name of a module */
43        return I18N::translate('HTML');
44    }
45
46    /**
47     * A sentence describing what this module does.
48     *
49     * @return string
50     */
51    public function description(): string
52    {
53        /* I18N: Description of the “HTML” module */
54        return I18N::translate('Add your own text and graphics.');
55    }
56
57    /**
58     * Generate the HTML content of this block.
59     *
60     * @param Tree     $tree
61     * @param int      $block_id
62     * @param string   $ctype
63     * @param string[] $cfg
64     *
65     * @return string
66     */
67    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
68    {
69        $statistics = app(Statistics::class);
70
71        $title          = $this->getBlockSetting($block_id, 'title', '');
72        $content        = $this->getBlockSetting($block_id, 'html', '');
73        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
74        $languages      = $this->getBlockSetting($block_id, 'languages');
75
76        // Only show this block for certain languages
77        if ($languages && !in_array(WT_LOCALE, explode(',', $languages))) {
78            return '';
79        }
80
81        // Retrieve text, process embedded variables
82        $title   = $statistics->embedTags($title);
83        $content = $statistics->embedTags($content);
84
85        $block_timestamp = (int) $this->getBlockSetting($block_id, 'timestamp', (string) Carbon::now()->unix());
86
87        if ($show_timestamp === '1') {
88            $content .= '<br>' . view('components/datetime', ['timestamp' => Carbon::createFromTimestamp($block_timestamp)]);
89        }
90
91        if ($ctype !== '') {
92            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
93                $config_url = route('tree-page-block-edit', [
94                    'block_id' => $block_id,
95                    'ged'      => $tree->name(),
96                ]);
97            } elseif ($ctype === 'user' && Auth::check()) {
98                $config_url = route('user-page-block-edit', [
99                    'block_id' => $block_id,
100                    'ged'      => $tree->name(),
101                ]);
102            } else {
103                $config_url = '';
104            }
105
106            return view('modules/block-template', [
107                'block'      => Str::kebab($this->name()),
108                'id'         => $block_id,
109                'config_url' => $config_url,
110                'title'      => $title,
111                'content'    => $content,
112            ]);
113        }
114
115        return $content;
116    }
117
118    /** {@inheritdoc} */
119    public function loadAjax(): bool
120    {
121        return false;
122    }
123
124    /** {@inheritdoc} */
125    public function isUserBlock(): bool
126    {
127        return true;
128    }
129
130    /** {@inheritdoc} */
131    public function isTreeBlock(): bool
132    {
133        return true;
134    }
135
136    /**
137     * Update the configuration for a block.
138     *
139     * @param Request $request
140     * @param int     $block_id
141     *
142     * @return void
143     */
144    public function saveBlockConfiguration(Request $request, int $block_id): void
145    {
146        $languages = (array) $request->get('lang');
147        $this->setBlockSetting($block_id, 'title', $request->get('title', ''));
148        $this->setBlockSetting($block_id, 'html', $request->get('html', ''));
149        $this->setBlockSetting($block_id, 'show_timestamp', $request->get('show_timestamp', ''));
150        $this->setBlockSetting($block_id, 'timestamp', (string) Carbon::now()->unix());
151        $this->setBlockSetting($block_id, 'languages', implode(',', $languages));
152    }
153
154    /**
155     * An HTML form to edit block settings
156     *
157     * @param Tree $tree
158     * @param int  $block_id
159     *
160     * @return void
161     */
162    public function editBlockConfiguration(Tree $tree, int $block_id): void
163    {
164        $templates = [
165            I18N::translate('Keyword examples')      => view('modules/html/template-keywords', []),
166            I18N::translate('Narrative description') => view('modules/html/template-narrative', []),
167            I18N::translate('Statistics')            => view('modules/html/template-statistics', []),
168        ];
169
170        $title          = $this->getBlockSetting($block_id, 'title', '');
171        $html           = $this->getBlockSetting($block_id, 'html', '');
172        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
173        $languages      = explode(',', $this->getBlockSetting($block_id, 'languages'));
174        $all_trees      = Tree::getNameList();
175
176        echo view('modules/html/config', [
177            'all_trees'      => $all_trees,
178            'html'           => $html,
179            'languages'      => $languages,
180            'show_timestamp' => $show_timestamp,
181            'templates'      => $templates,
182            'title'          => $title,
183        ]);
184    }
185}
186