xref: /webtrees/app/Module/HtmlBlockModule.php (revision f84aae5427fb2f153517c370d8fed75c42973b3b)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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 */
16namespace Fisharebest\Webtrees\Module;
17
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Filter;
20use Fisharebest\Webtrees\Functions\FunctionsDate;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Site;
23use Fisharebest\Webtrees\Stats;
24use Fisharebest\Webtrees\Tree;
25
26/**
27 * Class HtmlBlockModule
28 */
29class HtmlBlockModule extends AbstractModule implements ModuleBlockInterface {
30	/** {@inheritdoc} */
31	public function getTitle() {
32		return /* I18N: Name of a module */
33			I18N::translate('HTML');
34	}
35
36	/** {@inheritdoc} */
37	public function getDescription() {
38		return /* I18N: Description of the “HTML” module */
39			I18N::translate('Add your own text and graphics.');
40	}
41
42	/**
43	 * Generate the HTML content of this block.
44	 *
45	 * @param Tree     $tree
46	 * @param int      $block_id
47	 * @param bool     $template
48	 * @param string[] $cfg
49	 *
50	 * @return string
51	 */
52	public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string {
53		global $ctype;
54
55		$title          = $this->getBlockSetting($block_id, 'title', '');
56		$content        = $this->getBlockSetting($block_id, 'html', '');
57		$show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
58		$languages      = $this->getBlockSetting($block_id, 'languages');
59
60		// Only show this block for certain languages
61		if ($languages && !in_array(WT_LOCALE, explode(',', $languages))) {
62			return '';
63		}
64
65		$stats = new Stats($tree);
66
67		/*
68		* Retrieve text, process embedded variables
69		*/
70		$title   = $stats->embedTags($title);
71		$content = $stats->embedTags($content);
72
73		if ($show_timestamp === '1') {
74			$content .= '<br>' . FunctionsDate::formatTimestamp((int) $this->getBlockSetting($block_id, 'timestamp', WT_TIMESTAMP) + WT_TIMESTAMP_OFFSET);
75		}
76
77		if ($template) {
78			if ($ctype === 'gedcom' && Auth::isManager($tree)) {
79				$config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]);
80			} elseif ($ctype === 'user' && Auth::check()) {
81				$config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]);
82			} else {
83				$config_url = '';
84			}
85
86			return view('blocks/template', [
87				'block'      => str_replace('_', '-', $this->getName()),
88				'id'         => $block_id,
89				'config_url' => $config_url,
90				'title'      => $title,
91				'content'    => $content,
92			]);
93		} else {
94			return $content;
95		}
96	}
97
98	/** {@inheritdoc} */
99	public function loadAjax(): bool {
100		return false;
101	}
102
103	/** {@inheritdoc} */
104	public function isUserBlock(): bool {
105		return true;
106	}
107
108	/** {@inheritdoc} */
109	public function isGedcomBlock(): bool {
110		return true;
111	}
112
113	/**
114	 * An HTML form to edit block settings
115	 *
116	 * @param Tree $tree
117	 * @param int  $block_id
118	 *
119	 * @return void
120	 */
121	public function configureBlock(Tree $tree, int $block_id) {
122		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
123			$languages = Filter::postArray('lang');
124			$this->setBlockSetting($block_id, 'title', Filter::post('title'));
125			$this->setBlockSetting($block_id, 'html', Filter::post('html'));
126			$this->setBlockSetting($block_id, 'show_timestamp', Filter::postBool('show_timestamp'));
127			$this->setBlockSetting($block_id, 'timestamp', Filter::post('timestamp'));
128			$this->setBlockSetting($block_id, 'languages', implode(',', $languages));
129
130			return;
131		}
132
133		$templates = [
134			I18N::translate('Keyword examples')      => view('blocks/html-template-keywords', []),
135			I18N::translate('Narrative description') => view('blocks/html-template-narrative', []),
136			I18N::translate('Statistics')            => view('blocks/html-template-statistics', []),
137		];
138
139		$title          = $this->getBlockSetting($block_id, 'title', '');
140		$html           = $this->getBlockSetting($block_id, 'html', '');
141		$show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
142		$languages      = explode(',', $this->getBlockSetting($block_id, 'languages'));
143		$all_trees      = Tree::getNameList();
144
145		echo view('blocks/html-config', [
146			'all_trees'       => $all_trees,
147			'html'            => $html,
148			'languages'       => $languages,
149			'show_timestamp'  => $show_timestamp,
150			'templates'       => $templates,
151			'title'           => $title,
152		]);
153	}
154}
155