xref: /webtrees/app/Module/HtmlBlockModule.php (revision e364afe4ae4e316fc4ebd53eccbaff2d29e419a5)
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;
25a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class HtmlBlockModule
298c2e8227SGreg Roach */
3037eb8894SGreg Roachclass HtmlBlockModule extends AbstractModule implements ModuleBlockInterface
31c1010edaSGreg Roach{
3249a243cbSGreg Roach    use ModuleBlockTrait;
3349a243cbSGreg Roach
34961ec755SGreg Roach    /**
350cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
36961ec755SGreg Roach     *
37961ec755SGreg Roach     * @return string
38961ec755SGreg Roach     */
3949a243cbSGreg Roach    public function title(): string
40c1010edaSGreg Roach    {
41bbb76c12SGreg Roach        /* I18N: Name of a module */
42bbb76c12SGreg Roach        return I18N::translate('HTML');
438c2e8227SGreg Roach    }
448c2e8227SGreg Roach
45961ec755SGreg Roach    /**
46961ec755SGreg Roach     * A sentence describing what this module does.
47961ec755SGreg Roach     *
48961ec755SGreg Roach     * @return string
49961ec755SGreg Roach     */
5049a243cbSGreg Roach    public function description(): string
51c1010edaSGreg Roach    {
52bbb76c12SGreg Roach        /* I18N: Description of the “HTML” module */
53bbb76c12SGreg Roach        return I18N::translate('Add your own text and graphics.');
548c2e8227SGreg Roach    }
558c2e8227SGreg Roach
5676692c8bSGreg Roach    /**
5776692c8bSGreg Roach     * Generate the HTML content of this block.
5876692c8bSGreg Roach     *
59e490cd80SGreg Roach     * @param Tree     $tree
6076692c8bSGreg Roach     * @param int      $block_id
615f2ae573SGreg Roach     * @param string   $ctype
62727f238cSGreg Roach     * @param string[] $cfg
6376692c8bSGreg Roach     *
6476692c8bSGreg Roach     * @return string
6576692c8bSGreg Roach     */
665f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
67c1010edaSGreg Roach    {
68cab242e7SGreg Roach        $statistics = app(Statistics::class);
69bf57b580SGreg Roach
70c02d9576SGreg Roach        $title          = $this->getBlockSetting($block_id, 'title', '');
71c02d9576SGreg Roach        $content        = $this->getBlockSetting($block_id, 'html', '');
72e2a378d3SGreg Roach        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
73e2a378d3SGreg Roach        $languages      = $this->getBlockSetting($block_id, 'languages');
748c2e8227SGreg Roach
758c2e8227SGreg Roach        // Only show this block for certain languages
768c2e8227SGreg Roach        if ($languages && !in_array(WT_LOCALE, explode(',', $languages))) {
778c2e8227SGreg Roach            return '';
788c2e8227SGreg Roach        }
798c2e8227SGreg Roach
80ca52a408SGreg Roach        // Retrieve text, process embedded variables
81bf57b580SGreg Roach        $title   = $statistics->embedTags($title);
82bf57b580SGreg Roach        $content = $statistics->embedTags($content);
838c2e8227SGreg Roach
844459dc9aSGreg Roach        $block_timestamp = (int) $this->getBlockSetting($block_id, 'timestamp', (string) Carbon::now()->unix());
85ca52a408SGreg Roach
86ad98d39dSGreg Roach        if ($show_timestamp === '1') {
874459dc9aSGreg Roach            $content .= '<br>' . view('components/datetime', ['timestamp' => Carbon::createFromTimestamp($block_timestamp)]);
888c2e8227SGreg Roach        }
898c2e8227SGreg Roach
906a8879feSGreg Roach        if ($ctype !== '') {
91e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
92c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
93c1010edaSGreg Roach                    'block_id' => $block_id,
94aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
95c1010edaSGreg Roach                ]);
96397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
97c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
98c1010edaSGreg Roach                    'block_id' => $block_id,
99aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
100c1010edaSGreg Roach                ]);
1019c6524dcSGreg Roach            } else {
1029c6524dcSGreg Roach                $config_url = '';
1039c6524dcSGreg Roach            }
1049c6524dcSGreg Roach
105147e99aaSGreg Roach            return view('modules/block-template', [
10626684e68SGreg Roach                'block'      => str_replace('_', '-', $this->name()),
1079c6524dcSGreg Roach                'id'         => $block_id,
1089c6524dcSGreg Roach                'config_url' => $config_url,
1099c6524dcSGreg Roach                'title'      => $title,
1109c6524dcSGreg Roach                'content'    => $content,
1119c6524dcSGreg Roach            ]);
1128c2e8227SGreg Roach        }
113b2ce94c6SRico Sonntag
114b2ce94c6SRico Sonntag        return $content;
1158c2e8227SGreg Roach    }
1168c2e8227SGreg Roach
1178c2e8227SGreg Roach    /** {@inheritdoc} */
118c1010edaSGreg Roach    public function loadAjax(): bool
119c1010edaSGreg Roach    {
1208c2e8227SGreg Roach        return false;
1218c2e8227SGreg Roach    }
1228c2e8227SGreg Roach
1238c2e8227SGreg Roach    /** {@inheritdoc} */
124c1010edaSGreg Roach    public function isUserBlock(): bool
125c1010edaSGreg Roach    {
1268c2e8227SGreg Roach        return true;
1278c2e8227SGreg Roach    }
1288c2e8227SGreg Roach
1298c2e8227SGreg Roach    /** {@inheritdoc} */
13063276d8fSGreg Roach    public function isTreeBlock(): bool
131c1010edaSGreg Roach    {
1328c2e8227SGreg Roach        return true;
1338c2e8227SGreg Roach    }
1348c2e8227SGreg Roach
13576692c8bSGreg Roach    /**
136a45f9889SGreg Roach     * Update the configuration for a block.
137a45f9889SGreg Roach     *
138a45f9889SGreg Roach     * @param Request $request
139a45f9889SGreg Roach     * @param int     $block_id
140a45f9889SGreg Roach     *
141a45f9889SGreg Roach     * @return void
142a45f9889SGreg Roach     */
143*e364afe4SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id): void
144a45f9889SGreg Roach    {
145a45f9889SGreg Roach        $languages = (array) $request->get('lang');
146a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'title', $request->get('title', ''));
147a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'html', $request->get('html', ''));
148a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'show_timestamp', $request->get('show_timestamp', ''));
1494459dc9aSGreg Roach        $this->setBlockSetting($block_id, 'timestamp', (string) Carbon::now()->unix());
150a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'languages', implode(',', $languages));
151a45f9889SGreg Roach    }
152a45f9889SGreg Roach
153a45f9889SGreg Roach    /**
15476692c8bSGreg Roach     * An HTML form to edit block settings
15576692c8bSGreg Roach     *
156e490cd80SGreg Roach     * @param Tree $tree
15776692c8bSGreg Roach     * @param int  $block_id
158a9430be8SGreg Roach     *
159a9430be8SGreg Roach     * @return void
16076692c8bSGreg Roach     */
161*e364afe4SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): void
162c1010edaSGreg Roach    {
16313abd6f3SGreg Roach        $templates = [
164147e99aaSGreg Roach            I18N::translate('Keyword examples')      => view('modules/html/template-keywords', []),
165147e99aaSGreg Roach            I18N::translate('Narrative description') => view('modules/html/template-narrative', []),
166147e99aaSGreg Roach            I18N::translate('Statistics')            => view('modules/html/template-statistics', []),
16713abd6f3SGreg Roach        ];
1688c2e8227SGreg Roach
169abb263adSGreg Roach        $title          = $this->getBlockSetting($block_id, 'title', '');
170abb263adSGreg Roach        $html           = $this->getBlockSetting($block_id, 'html', '');
171e2a378d3SGreg Roach        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
172e2a378d3SGreg Roach        $languages      = explode(',', $this->getBlockSetting($block_id, 'languages'));
173c385536dSGreg Roach        $all_trees      = Tree::getNameList();
1748c2e8227SGreg Roach
175147e99aaSGreg Roach        echo view('modules/html/config', [
176c385536dSGreg Roach            'all_trees'      => $all_trees,
177c385536dSGreg Roach            'html'           => $html,
178c385536dSGreg Roach            'languages'      => $languages,
179c385536dSGreg Roach            'show_timestamp' => $show_timestamp,
180c385536dSGreg Roach            'templates'      => $templates,
181c385536dSGreg Roach            'title'          => $title,
182c385536dSGreg Roach        ]);
1838c2e8227SGreg Roach    }
1848c2e8227SGreg Roach}
185