xref: /webtrees/app/Module/HtmlBlockModule.php (revision 5f2ae57322ecd2d23660294256ca8376fd889348)
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
51*5f2ae573SGreg Roach     * @param string   $ctype
52727f238cSGreg Roach     * @param string[] $cfg
5376692c8bSGreg Roach     *
5476692c8bSGreg Roach     * @return string
5576692c8bSGreg Roach     */
56*5f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
57c1010edaSGreg Roach    {
58c02d9576SGreg Roach        $title          = $this->getBlockSetting($block_id, 'title', '');
59c02d9576SGreg Roach        $content        = $this->getBlockSetting($block_id, 'html', '');
60e2a378d3SGreg Roach        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
61e2a378d3SGreg Roach        $languages      = $this->getBlockSetting($block_id, 'languages');
628c2e8227SGreg Roach
638c2e8227SGreg Roach        // Only show this block for certain languages
648c2e8227SGreg Roach        if ($languages && !in_array(WT_LOCALE, explode(',', $languages))) {
658c2e8227SGreg Roach            return '';
668c2e8227SGreg Roach        }
678c2e8227SGreg Roach
68e490cd80SGreg Roach        $stats = new Stats($tree);
698c2e8227SGreg Roach
708c2e8227SGreg Roach        /*
718c2e8227SGreg Roach        * Retrieve text, process embedded variables
728c2e8227SGreg Roach        */
738c2e8227SGreg Roach        $title   = $stats->embedTags($title);
749c6524dcSGreg Roach        $content = $stats->embedTags($content);
758c2e8227SGreg Roach
76c02d9576SGreg Roach        if ($show_timestamp === '1') {
77acf76a54SGreg Roach            $content .= '<br>' . FunctionsDate::formatTimestamp((int) $this->getBlockSetting($block_id, 'timestamp', (string) WT_TIMESTAMP) + WT_TIMESTAMP_OFFSET);
788c2e8227SGreg Roach        }
798c2e8227SGreg Roach
80*5f2ae573SGreg Roach        if ($ctype) {
81e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
82c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
83c1010edaSGreg Roach                    'block_id' => $block_id,
84aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
85c1010edaSGreg Roach                ]);
86397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
87c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
88c1010edaSGreg Roach                    'block_id' => $block_id,
89aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
90c1010edaSGreg Roach                ]);
919c6524dcSGreg Roach            } else {
929c6524dcSGreg Roach                $config_url = '';
939c6524dcSGreg Roach            }
949c6524dcSGreg Roach
95147e99aaSGreg Roach            return view('modules/block-template', [
969c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
979c6524dcSGreg Roach                'id'         => $block_id,
989c6524dcSGreg Roach                'config_url' => $config_url,
999c6524dcSGreg Roach                'title'      => $title,
1009c6524dcSGreg Roach                'content'    => $content,
1019c6524dcSGreg Roach            ]);
1028c2e8227SGreg Roach        }
103b2ce94c6SRico Sonntag
104b2ce94c6SRico Sonntag        return $content;
1058c2e8227SGreg Roach    }
1068c2e8227SGreg Roach
1078c2e8227SGreg Roach    /** {@inheritdoc} */
108c1010edaSGreg Roach    public function loadAjax(): bool
109c1010edaSGreg Roach    {
1108c2e8227SGreg Roach        return false;
1118c2e8227SGreg Roach    }
1128c2e8227SGreg Roach
1138c2e8227SGreg Roach    /** {@inheritdoc} */
114c1010edaSGreg Roach    public function isUserBlock(): bool
115c1010edaSGreg Roach    {
1168c2e8227SGreg Roach        return true;
1178c2e8227SGreg Roach    }
1188c2e8227SGreg Roach
1198c2e8227SGreg Roach    /** {@inheritdoc} */
120c1010edaSGreg Roach    public function isGedcomBlock(): bool
121c1010edaSGreg Roach    {
1228c2e8227SGreg Roach        return true;
1238c2e8227SGreg Roach    }
1248c2e8227SGreg Roach
12576692c8bSGreg Roach    /**
126a45f9889SGreg Roach     * Update the configuration for a block.
127a45f9889SGreg Roach     *
128a45f9889SGreg Roach     * @param Request $request
129a45f9889SGreg Roach     * @param int     $block_id
130a45f9889SGreg Roach     *
131a45f9889SGreg Roach     * @return void
132a45f9889SGreg Roach     */
133a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
134a45f9889SGreg Roach    {
135a45f9889SGreg Roach        $languages = (array) $request->get('lang');
136a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'title', $request->get('title', ''));
137a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'html', $request->get('html', ''));
138a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'show_timestamp', $request->get('show_timestamp', ''));
139a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'timestamp', $request->get('timestamp', ''));
140a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'languages', implode(',', $languages));
141a45f9889SGreg Roach    }
142a45f9889SGreg Roach
143a45f9889SGreg Roach    /**
14476692c8bSGreg Roach     * An HTML form to edit block settings
14576692c8bSGreg Roach     *
146e490cd80SGreg Roach     * @param Tree $tree
14776692c8bSGreg Roach     * @param int  $block_id
148a9430be8SGreg Roach     *
149a9430be8SGreg Roach     * @return void
15076692c8bSGreg Roach     */
151a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
152c1010edaSGreg Roach    {
15313abd6f3SGreg Roach        $templates = [
154147e99aaSGreg Roach            I18N::translate('Keyword examples')      => view('modules/html/template-keywords', []),
155147e99aaSGreg Roach            I18N::translate('Narrative description') => view('modules/html/template-narrative', []),
156147e99aaSGreg Roach            I18N::translate('Statistics')            => view('modules/html/template-statistics', []),
15713abd6f3SGreg Roach        ];
1588c2e8227SGreg Roach
159abb263adSGreg Roach        $title          = $this->getBlockSetting($block_id, 'title', '');
160abb263adSGreg Roach        $html           = $this->getBlockSetting($block_id, 'html', '');
161e2a378d3SGreg Roach        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
162e2a378d3SGreg Roach        $languages      = explode(',', $this->getBlockSetting($block_id, 'languages'));
163c385536dSGreg Roach        $all_trees      = Tree::getNameList();
1648c2e8227SGreg Roach
165147e99aaSGreg Roach        echo view('modules/html/config', [
166c385536dSGreg Roach            'all_trees'      => $all_trees,
167c385536dSGreg Roach            'html'           => $html,
168c385536dSGreg Roach            'languages'      => $languages,
169c385536dSGreg Roach            'show_timestamp' => $show_timestamp,
170c385536dSGreg Roach            'templates'      => $templates,
171c385536dSGreg Roach            'title'          => $title,
172c385536dSGreg Roach        ]);
1738c2e8227SGreg Roach    }
1748c2e8227SGreg Roach}
175