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