xref: /webtrees/app/Module/HtmlBlockModule.php (revision e490cd80ad2d75f9f6adf9b41698ad3f3f058276)
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 */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
203d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDate;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Site;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Stats;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
258c2e8227SGreg Roach
268c2e8227SGreg Roach/**
278c2e8227SGreg Roach * Class HtmlBlockModule
288c2e8227SGreg Roach */
29e2a378d3SGreg Roachclass HtmlBlockModule extends AbstractModule implements ModuleBlockInterface {
308c2e8227SGreg Roach	/** {@inheritdoc} */
318c2e8227SGreg Roach	public function getTitle() {
329c6524dcSGreg Roach		return /* I18N: Name of a module */
339c6524dcSGreg Roach			I18N::translate('HTML');
348c2e8227SGreg Roach	}
358c2e8227SGreg Roach
368c2e8227SGreg Roach	/** {@inheritdoc} */
378c2e8227SGreg Roach	public function getDescription() {
389c6524dcSGreg Roach		return /* I18N: Description of the “HTML” module */
399c6524dcSGreg Roach			I18N::translate('Add your own text and graphics.');
408c2e8227SGreg Roach	}
418c2e8227SGreg Roach
4276692c8bSGreg Roach	/**
4376692c8bSGreg Roach	 * Generate the HTML content of this block.
4476692c8bSGreg Roach	 *
45*e490cd80SGreg Roach	 * @param Tree     $tree
4676692c8bSGreg Roach	 * @param int      $block_id
4776692c8bSGreg Roach	 * @param bool     $template
48727f238cSGreg Roach	 * @param string[] $cfg
4976692c8bSGreg Roach	 *
5076692c8bSGreg Roach	 * @return string
5176692c8bSGreg Roach	 */
52*e490cd80SGreg Roach	public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string {
53*e490cd80SGreg Roach		global $ctype;
548c2e8227SGreg Roach
55c02d9576SGreg Roach		$title          = $this->getBlockSetting($block_id, 'title', '');
56c02d9576SGreg Roach		$content        = $this->getBlockSetting($block_id, 'html', '');
57e2a378d3SGreg Roach		$gedcom         = $this->getBlockSetting($block_id, 'gedcom');
58e2a378d3SGreg Roach		$show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
59e2a378d3SGreg Roach		$languages      = $this->getBlockSetting($block_id, 'languages');
608c2e8227SGreg Roach
618c2e8227SGreg Roach		// Only show this block for certain languages
628c2e8227SGreg Roach		if ($languages && !in_array(WT_LOCALE, explode(',', $languages))) {
638c2e8227SGreg Roach			return '';
648c2e8227SGreg Roach		}
658c2e8227SGreg Roach
668c2e8227SGreg Roach		/*
678c2e8227SGreg Roach		 * Select GEDCOM
688c2e8227SGreg Roach		 */
698c2e8227SGreg Roach		switch ($gedcom) {
708c2e8227SGreg Roach			case '__current__':
71*e490cd80SGreg Roach				$stats = new Stats($tree);
728c2e8227SGreg Roach				break;
738c2e8227SGreg Roach			case '__default__':
74*e490cd80SGreg Roach				$tree = Tree::findByName(Site::getPreference('DEFAULT_GEDCOM')) ?? $tree;
75ef2fd529SGreg Roach				$stats = new Stats($tree);
768c2e8227SGreg Roach				break;
778c2e8227SGreg Roach			default:
78ef2fd529SGreg Roach				$tree = Tree::findByName($gedcom);
79ef2fd529SGreg Roach				if ($tree) {
80ef2fd529SGreg Roach					$stats = new Stats($tree);
818c2e8227SGreg Roach				} else {
82*e490cd80SGreg Roach					$stats = new Stats($tree);
838c2e8227SGreg Roach				}
848c2e8227SGreg Roach				break;
858c2e8227SGreg Roach		}
868c2e8227SGreg Roach
878c2e8227SGreg Roach		/*
888c2e8227SGreg Roach		* Retrieve text, process embedded variables
898c2e8227SGreg Roach		*/
908c2e8227SGreg Roach		$title   = $stats->embedTags($title);
919c6524dcSGreg Roach		$content = $stats->embedTags($content);
928c2e8227SGreg Roach
93c02d9576SGreg Roach		if ($show_timestamp === '1') {
94*e490cd80SGreg Roach			$content .= '<br>' . FunctionsDate::formatTimestamp((int) $this->getBlockSetting($block_id, 'timestamp', WT_TIMESTAMP) + WT_TIMESTAMP_OFFSET);
958c2e8227SGreg Roach		}
968c2e8227SGreg Roach
978c2e8227SGreg Roach		if ($template) {
98*e490cd80SGreg Roach			if ($ctype === 'gedcom' && Auth::isManager($tree)) {
99*e490cd80SGreg Roach				$config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]);
100397e599aSGreg Roach			} elseif ($ctype === 'user' && Auth::check()) {
101*e490cd80SGreg Roach				$config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]);
1029c6524dcSGreg Roach			} else {
1039c6524dcSGreg Roach				$config_url = '';
1049c6524dcSGreg Roach			}
1059c6524dcSGreg Roach
106225e381fSGreg Roach			return view('blocks/template', [
1079c6524dcSGreg Roach				'block'      => str_replace('_', '-', $this->getName()),
1089c6524dcSGreg Roach				'id'         => $block_id,
1099c6524dcSGreg Roach				'config_url' => $config_url,
1109c6524dcSGreg Roach				'title'      => $title,
1119c6524dcSGreg Roach				'content'    => $content,
1129c6524dcSGreg Roach			]);
1138c2e8227SGreg Roach		} else {
1148c2e8227SGreg Roach			return $content;
1158c2e8227SGreg Roach		}
1168c2e8227SGreg Roach	}
1178c2e8227SGreg Roach
1188c2e8227SGreg Roach	/** {@inheritdoc} */
119a9430be8SGreg Roach	public function loadAjax(): bool {
1208c2e8227SGreg Roach		return false;
1218c2e8227SGreg Roach	}
1228c2e8227SGreg Roach
1238c2e8227SGreg Roach	/** {@inheritdoc} */
124a9430be8SGreg Roach	public function isUserBlock(): bool {
1258c2e8227SGreg Roach		return true;
1268c2e8227SGreg Roach	}
1278c2e8227SGreg Roach
1288c2e8227SGreg Roach	/** {@inheritdoc} */
129a9430be8SGreg Roach	public function isGedcomBlock(): bool {
1308c2e8227SGreg Roach		return true;
1318c2e8227SGreg Roach	}
1328c2e8227SGreg Roach
13376692c8bSGreg Roach	/**
13476692c8bSGreg Roach	 * An HTML form to edit block settings
13576692c8bSGreg Roach	 *
136*e490cd80SGreg Roach	 * @param Tree $tree
13776692c8bSGreg Roach	 * @param int  $block_id
138a9430be8SGreg Roach	 *
139a9430be8SGreg Roach	 * @return void
14076692c8bSGreg Roach	 */
141*e490cd80SGreg Roach	public function configureBlock(Tree $tree, int $block_id) {
142c385536dSGreg Roach		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
143c999a340SGreg Roach			$languages = Filter::postArray('lang');
144e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'gedcom', Filter::post('gedcom'));
145e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'title', Filter::post('title'));
146e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'html', Filter::post('html'));
147e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'show_timestamp', Filter::postBool('show_timestamp'));
148e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'timestamp', Filter::post('timestamp'));
149e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'languages', implode(',', $languages));
150c385536dSGreg Roach
151c385536dSGreg Roach			return;
1528c2e8227SGreg Roach		}
1538c2e8227SGreg Roach
15413abd6f3SGreg Roach		$templates = [
155c385536dSGreg Roach			I18N::translate('Keyword examples')      => view('blocks/html-template-keywords', []),
156c385536dSGreg Roach			I18N::translate('Narrative description') => view('blocks/html-template-narrative', []),
157c385536dSGreg Roach			I18N::translate('Statistics')            => view('blocks/html-template-statistics', []),
15813abd6f3SGreg Roach		];
1598c2e8227SGreg Roach
160abb263adSGreg Roach		$title          = $this->getBlockSetting($block_id, 'title', '');
161abb263adSGreg Roach		$html           = $this->getBlockSetting($block_id, 'html', '');
16215d603e7SGreg Roach		$gedcom         = $this->getBlockSetting($block_id, 'gedcom', '__current__');
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
167c385536dSGreg Roach		echo view('blocks/html-config', [
168c385536dSGreg Roach			'all_trees'       => $all_trees,
169c385536dSGreg Roach			'gedcom'          => $gedcom,
170c385536dSGreg Roach			'html'            => $html,
171c385536dSGreg Roach			'languages'       => $languages,
172c385536dSGreg Roach			'show_timestamp'  => $show_timestamp,
173c385536dSGreg Roach			'templates'       => $templates,
174c385536dSGreg Roach			'title'           => $title,
175c385536dSGreg Roach		]);
1768c2e8227SGreg Roach	}
1778c2e8227SGreg Roach}
178