xref: /webtrees/app/Module/HtmlBlockModule.php (revision b6c326d8b8798b83b744c4d4a669df5aa9f3e0c2)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
214459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
239219296aSGreg Roachuse Fisharebest\Webtrees\Statistics;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
251e7a7a28SGreg Roachuse Illuminate\Support\Str;
26a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
278c2e8227SGreg Roach
288c2e8227SGreg Roach/**
298c2e8227SGreg Roach * Class HtmlBlockModule
308c2e8227SGreg Roach */
3137eb8894SGreg Roachclass HtmlBlockModule extends AbstractModule implements ModuleBlockInterface
32c1010edaSGreg Roach{
3349a243cbSGreg Roach    use ModuleBlockTrait;
3449a243cbSGreg Roach
35961ec755SGreg Roach    /**
360cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
37961ec755SGreg Roach     *
38961ec755SGreg Roach     * @return string
39961ec755SGreg Roach     */
4049a243cbSGreg Roach    public function title(): string
41c1010edaSGreg Roach    {
42bbb76c12SGreg Roach        /* I18N: Name of a module */
43bbb76c12SGreg Roach        return I18N::translate('HTML');
448c2e8227SGreg Roach    }
458c2e8227SGreg Roach
46961ec755SGreg Roach    /**
47961ec755SGreg Roach     * A sentence describing what this module does.
48961ec755SGreg Roach     *
49961ec755SGreg Roach     * @return string
50961ec755SGreg Roach     */
5149a243cbSGreg Roach    public function description(): string
52c1010edaSGreg Roach    {
53bbb76c12SGreg Roach        /* I18N: Description of the “HTML” module */
54bbb76c12SGreg Roach        return I18N::translate('Add your own text and graphics.');
558c2e8227SGreg Roach    }
568c2e8227SGreg Roach
5776692c8bSGreg Roach    /**
5876692c8bSGreg Roach     * Generate the HTML content of this block.
5976692c8bSGreg Roach     *
60e490cd80SGreg Roach     * @param Tree     $tree
6176692c8bSGreg Roach     * @param int      $block_id
625f2ae573SGreg Roach     * @param string   $ctype
63727f238cSGreg Roach     * @param string[] $cfg
6476692c8bSGreg Roach     *
6576692c8bSGreg Roach     * @return string
6676692c8bSGreg Roach     */
675f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
68c1010edaSGreg Roach    {
69cab242e7SGreg Roach        $statistics = app(Statistics::class);
70bf57b580SGreg Roach
71c02d9576SGreg Roach        $title          = $this->getBlockSetting($block_id, 'title', '');
72c02d9576SGreg Roach        $content        = $this->getBlockSetting($block_id, 'html', '');
73e2a378d3SGreg Roach        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
74e2a378d3SGreg Roach        $languages      = $this->getBlockSetting($block_id, 'languages');
758c2e8227SGreg Roach
768c2e8227SGreg Roach        // Only show this block for certain languages
778c2e8227SGreg Roach        if ($languages && !in_array(WT_LOCALE, explode(',', $languages))) {
788c2e8227SGreg Roach            return '';
798c2e8227SGreg Roach        }
808c2e8227SGreg Roach
81ca52a408SGreg Roach        // Retrieve text, process embedded variables
82bf57b580SGreg Roach        $title   = $statistics->embedTags($title);
83bf57b580SGreg Roach        $content = $statistics->embedTags($content);
848c2e8227SGreg Roach
854459dc9aSGreg Roach        $block_timestamp = (int) $this->getBlockSetting($block_id, 'timestamp', (string) Carbon::now()->unix());
86ca52a408SGreg Roach
87ad98d39dSGreg Roach        if ($show_timestamp === '1') {
884459dc9aSGreg Roach            $content .= '<br>' . view('components/datetime', ['timestamp' => Carbon::createFromTimestamp($block_timestamp)]);
898c2e8227SGreg Roach        }
908c2e8227SGreg Roach
916a8879feSGreg Roach        if ($ctype !== '') {
92e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
93c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
94c1010edaSGreg Roach                    'block_id' => $block_id,
95aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
96c1010edaSGreg Roach                ]);
97397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
98c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
99c1010edaSGreg Roach                    'block_id' => $block_id,
100aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
101c1010edaSGreg Roach                ]);
1029c6524dcSGreg Roach            } else {
1039c6524dcSGreg Roach                $config_url = '';
1049c6524dcSGreg Roach            }
1059c6524dcSGreg Roach
106147e99aaSGreg Roach            return view('modules/block-template', [
1071e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1089c6524dcSGreg Roach                'id'         => $block_id,
1099c6524dcSGreg Roach                'config_url' => $config_url,
1109c6524dcSGreg Roach                'title'      => $title,
1119c6524dcSGreg Roach                'content'    => $content,
1129c6524dcSGreg Roach            ]);
1138c2e8227SGreg Roach        }
114b2ce94c6SRico Sonntag
115b2ce94c6SRico Sonntag        return $content;
1168c2e8227SGreg Roach    }
1178c2e8227SGreg Roach
1188c2e8227SGreg Roach    /** {@inheritdoc} */
119c1010edaSGreg Roach    public function loadAjax(): bool
120c1010edaSGreg Roach    {
1218c2e8227SGreg Roach        return false;
1228c2e8227SGreg Roach    }
1238c2e8227SGreg Roach
1248c2e8227SGreg Roach    /** {@inheritdoc} */
125c1010edaSGreg Roach    public function isUserBlock(): bool
126c1010edaSGreg Roach    {
1278c2e8227SGreg Roach        return true;
1288c2e8227SGreg Roach    }
1298c2e8227SGreg Roach
1308c2e8227SGreg Roach    /** {@inheritdoc} */
13163276d8fSGreg Roach    public function isTreeBlock(): bool
132c1010edaSGreg Roach    {
1338c2e8227SGreg Roach        return true;
1348c2e8227SGreg Roach    }
1358c2e8227SGreg Roach
13676692c8bSGreg Roach    /**
137a45f9889SGreg Roach     * Update the configuration for a block.
138a45f9889SGreg Roach     *
139a45f9889SGreg Roach     * @param Request $request
140a45f9889SGreg Roach     * @param int     $block_id
141a45f9889SGreg Roach     *
142a45f9889SGreg Roach     * @return void
143a45f9889SGreg Roach     */
144e364afe4SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id): void
145a45f9889SGreg Roach    {
146a45f9889SGreg Roach        $languages = (array) $request->get('lang');
147a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'title', $request->get('title', ''));
148a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'html', $request->get('html', ''));
149a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'show_timestamp', $request->get('show_timestamp', ''));
1504459dc9aSGreg Roach        $this->setBlockSetting($block_id, 'timestamp', (string) Carbon::now()->unix());
151a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'languages', implode(',', $languages));
152a45f9889SGreg Roach    }
153a45f9889SGreg Roach
154a45f9889SGreg Roach    /**
15576692c8bSGreg Roach     * An HTML form to edit block settings
15676692c8bSGreg Roach     *
157e490cd80SGreg Roach     * @param Tree $tree
15876692c8bSGreg Roach     * @param int  $block_id
159a9430be8SGreg Roach     *
160a9430be8SGreg Roach     * @return void
16176692c8bSGreg Roach     */
162e364afe4SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): void
163c1010edaSGreg Roach    {
164abb263adSGreg Roach        $title          = $this->getBlockSetting($block_id, 'title', '');
165abb263adSGreg Roach        $html           = $this->getBlockSetting($block_id, 'html', '');
166e2a378d3SGreg Roach        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
167e2a378d3SGreg Roach        $languages      = explode(',', $this->getBlockSetting($block_id, 'languages'));
168c385536dSGreg Roach        $all_trees      = Tree::getNameList();
1698c2e8227SGreg Roach
170*b6c326d8SGreg Roach        $templates = [
171*b6c326d8SGreg Roach            $html                                    => I18N::translate('Custom'),
172*b6c326d8SGreg Roach            view('modules/html/template-keywords')   => I18N::translate('Keyword examples'),
173*b6c326d8SGreg Roach            view('modules/html/template-narrative')  => I18N::translate('Narrative description'),
174*b6c326d8SGreg Roach            view('modules/html/template-statistics') => I18N::translate('Statistics'),
175*b6c326d8SGreg Roach        ];
176*b6c326d8SGreg Roach
177147e99aaSGreg Roach        echo view('modules/html/config', [
178c385536dSGreg Roach            'all_trees'      => $all_trees,
179c385536dSGreg Roach            'html'           => $html,
180c385536dSGreg Roach            'languages'      => $languages,
181c385536dSGreg Roach            'show_timestamp' => $show_timestamp,
182c385536dSGreg Roach            'templates'      => $templates,
183c385536dSGreg Roach            'title'          => $title,
184c385536dSGreg Roach        ]);
1858c2e8227SGreg Roach    }
1868c2e8227SGreg Roach}
187