xref: /webtrees/app/Module/HtmlBlockModule.php (revision aa6f03bb51be5a55a78fe8aed18753116689a6d3)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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;
213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDate;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Stats;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
25a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class HtmlBlockModule
298c2e8227SGreg Roach */
30c1010edaSGreg Roachclass HtmlBlockModule extends AbstractModule implements ModuleBlockInterface
31c1010edaSGreg Roach{
328c2e8227SGreg Roach    /** {@inheritdoc} */
338f53f488SRico Sonntag    public function getTitle(): string
34c1010edaSGreg Roach    {
35bbb76c12SGreg Roach        /* I18N: Name of a module */
36bbb76c12SGreg Roach        return I18N::translate('HTML');
378c2e8227SGreg Roach    }
388c2e8227SGreg Roach
398c2e8227SGreg Roach    /** {@inheritdoc} */
408f53f488SRico Sonntag    public function getDescription(): string
41c1010edaSGreg Roach    {
42bbb76c12SGreg Roach        /* I18N: Description of the “HTML” module */
43bbb76c12SGreg Roach        return I18N::translate('Add your own text and graphics.');
448c2e8227SGreg Roach    }
458c2e8227SGreg Roach
4676692c8bSGreg Roach    /**
4776692c8bSGreg Roach     * Generate the HTML content of this block.
4876692c8bSGreg Roach     *
49e490cd80SGreg Roach     * @param Tree     $tree
5076692c8bSGreg Roach     * @param int      $block_id
5176692c8bSGreg Roach     * @param bool     $template
52727f238cSGreg Roach     * @param string[] $cfg
5376692c8bSGreg Roach     *
5476692c8bSGreg Roach     * @return string
5576692c8bSGreg Roach     */
56c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
57c1010edaSGreg Roach    {
58e490cd80SGreg Roach        global $ctype;
598c2e8227SGreg Roach
60c02d9576SGreg Roach        $title          = $this->getBlockSetting($block_id, 'title', '');
61c02d9576SGreg Roach        $content        = $this->getBlockSetting($block_id, 'html', '');
62e2a378d3SGreg Roach        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
63e2a378d3SGreg Roach        $languages      = $this->getBlockSetting($block_id, 'languages');
648c2e8227SGreg Roach
658c2e8227SGreg Roach        // Only show this block for certain languages
668c2e8227SGreg Roach        if ($languages && !in_array(WT_LOCALE, explode(',', $languages))) {
678c2e8227SGreg Roach            return '';
688c2e8227SGreg Roach        }
698c2e8227SGreg Roach
70e490cd80SGreg Roach        $stats = new Stats($tree);
718c2e8227SGreg Roach
728c2e8227SGreg Roach        /*
738c2e8227SGreg Roach        * Retrieve text, process embedded variables
748c2e8227SGreg Roach        */
758c2e8227SGreg Roach        $title   = $stats->embedTags($title);
769c6524dcSGreg Roach        $content = $stats->embedTags($content);
778c2e8227SGreg Roach
78c02d9576SGreg Roach        if ($show_timestamp === '1') {
79acf76a54SGreg Roach            $content .= '<br>' . FunctionsDate::formatTimestamp((int) $this->getBlockSetting($block_id, 'timestamp', (string) WT_TIMESTAMP) + WT_TIMESTAMP_OFFSET);
808c2e8227SGreg Roach        }
818c2e8227SGreg Roach
828c2e8227SGreg Roach        if ($template) {
83e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
84c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
85c1010edaSGreg Roach                    'block_id' => $block_id,
86*aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
87c1010edaSGreg Roach                ]);
88397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
89c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
90c1010edaSGreg Roach                    'block_id' => $block_id,
91*aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
92c1010edaSGreg Roach                ]);
939c6524dcSGreg Roach            } else {
949c6524dcSGreg Roach                $config_url = '';
959c6524dcSGreg Roach            }
969c6524dcSGreg Roach
97147e99aaSGreg Roach            return view('modules/block-template', [
989c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
999c6524dcSGreg Roach                'id'         => $block_id,
1009c6524dcSGreg Roach                'config_url' => $config_url,
1019c6524dcSGreg Roach                'title'      => $title,
1029c6524dcSGreg Roach                'content'    => $content,
1039c6524dcSGreg Roach            ]);
1048c2e8227SGreg Roach        }
105b2ce94c6SRico Sonntag
106b2ce94c6SRico Sonntag        return $content;
1078c2e8227SGreg Roach    }
1088c2e8227SGreg Roach
1098c2e8227SGreg Roach    /** {@inheritdoc} */
110c1010edaSGreg Roach    public function loadAjax(): bool
111c1010edaSGreg Roach    {
1128c2e8227SGreg Roach        return false;
1138c2e8227SGreg Roach    }
1148c2e8227SGreg Roach
1158c2e8227SGreg Roach    /** {@inheritdoc} */
116c1010edaSGreg Roach    public function isUserBlock(): bool
117c1010edaSGreg Roach    {
1188c2e8227SGreg Roach        return true;
1198c2e8227SGreg Roach    }
1208c2e8227SGreg Roach
1218c2e8227SGreg Roach    /** {@inheritdoc} */
122c1010edaSGreg Roach    public function isGedcomBlock(): bool
123c1010edaSGreg Roach    {
1248c2e8227SGreg Roach        return true;
1258c2e8227SGreg Roach    }
1268c2e8227SGreg Roach
12776692c8bSGreg Roach    /**
128a45f9889SGreg Roach     * Update the configuration for a block.
129a45f9889SGreg Roach     *
130a45f9889SGreg Roach     * @param Request $request
131a45f9889SGreg Roach     * @param int     $block_id
132a45f9889SGreg Roach     *
133a45f9889SGreg Roach     * @return void
134a45f9889SGreg Roach     */
135a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
136a45f9889SGreg Roach    {
137a45f9889SGreg Roach        $languages = (array) $request->get('lang');
138a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'title', $request->get('title', ''));
139a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'html', $request->get('html', ''));
140a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'show_timestamp', $request->get('show_timestamp', ''));
141a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'timestamp', $request->get('timestamp', ''));
142a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'languages', implode(',', $languages));
143a45f9889SGreg Roach    }
144a45f9889SGreg Roach
145a45f9889SGreg Roach    /**
14676692c8bSGreg Roach     * An HTML form to edit block settings
14776692c8bSGreg Roach     *
148e490cd80SGreg Roach     * @param Tree $tree
14976692c8bSGreg Roach     * @param int  $block_id
150a9430be8SGreg Roach     *
151a9430be8SGreg Roach     * @return void
15276692c8bSGreg Roach     */
153a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
154c1010edaSGreg Roach    {
15513abd6f3SGreg Roach        $templates = [
156147e99aaSGreg Roach            I18N::translate('Keyword examples')      => view('modules/html/template-keywords', []),
157147e99aaSGreg Roach            I18N::translate('Narrative description') => view('modules/html/template-narrative', []),
158147e99aaSGreg Roach            I18N::translate('Statistics')            => view('modules/html/template-statistics', []),
15913abd6f3SGreg Roach        ];
1608c2e8227SGreg Roach
161abb263adSGreg Roach        $title          = $this->getBlockSetting($block_id, 'title', '');
162abb263adSGreg Roach        $html           = $this->getBlockSetting($block_id, 'html', '');
163e2a378d3SGreg Roach        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
164e2a378d3SGreg Roach        $languages      = explode(',', $this->getBlockSetting($block_id, 'languages'));
165c385536dSGreg Roach        $all_trees      = Tree::getNameList();
1668c2e8227SGreg Roach
167147e99aaSGreg Roach        echo view('modules/html/config', [
168c385536dSGreg Roach            'all_trees'      => $all_trees,
169c385536dSGreg Roach            'html'           => $html,
170c385536dSGreg Roach            'languages'      => $languages,
171c385536dSGreg Roach            'show_timestamp' => $show_timestamp,
172c385536dSGreg Roach            'templates'      => $templates,
173c385536dSGreg Roach            'title'          => $title,
174c385536dSGreg Roach        ]);
1758c2e8227SGreg Roach    }
1768c2e8227SGreg Roach}
177