xref: /webtrees/app/Module/HtmlBlockModule.php (revision 13abd6f3a37322f885d85df150e105d27ad81f8d)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2016 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\Functions\FunctionsEdit;
22use Fisharebest\Webtrees\GedcomTag;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Module;
25use Fisharebest\Webtrees\Site;
26use Fisharebest\Webtrees\Stats;
27use Fisharebest\Webtrees\Theme;
28use Fisharebest\Webtrees\Tree;
29
30/**
31 * Class HtmlBlockModule
32 */
33class HtmlBlockModule extends AbstractModule implements ModuleBlockInterface {
34	/** {@inheritdoc} */
35	public function getTitle() {
36		return /* I18N: Name of a module */ I18N::translate('HTML');
37	}
38
39	/** {@inheritdoc} */
40	public function getDescription() {
41		return /* I18N: Description of the “HTML” module */ I18N::translate('Add your own text and graphics.');
42	}
43
44	/**
45	 * Generate the HTML content of this block.
46	 *
47	 * @param int      $block_id
48	 * @param bool     $template
49	 * @param string[] $cfg
50	 *
51	 * @return string
52	 */
53	public function getBlock($block_id, $template = true, $cfg = []) {
54		global $ctype, $WT_TREE;
55
56		$title          = $this->getBlockSetting($block_id, 'title');
57		$html           = $this->getBlockSetting($block_id, 'html');
58		$gedcom         = $this->getBlockSetting($block_id, 'gedcom');
59		$show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
60		$languages      = $this->getBlockSetting($block_id, 'languages');
61
62		// Only show this block for certain languages
63		if ($languages && !in_array(WT_LOCALE, explode(',', $languages))) {
64			return '';
65		}
66
67		/*
68		 * Select GEDCOM
69		 */
70		switch ($gedcom) {
71		case '__current__':
72			$stats = new Stats($WT_TREE);
73			break;
74		case '__default__':
75			$tree = Tree::findByName(Site::getPreference('DEFAULT_GEDCOM'));
76			if ($tree) {
77				$stats = new Stats($tree);
78			} else {
79				$stats = new Stats($WT_TREE);
80			}
81			break;
82		default:
83			$tree = Tree::findByName($gedcom);
84			if ($tree) {
85				$stats = new Stats($tree);
86			} else {
87				$stats = new Stats($WT_TREE);
88			}
89			break;
90		}
91
92		/*
93		* Retrieve text, process embedded variables
94		*/
95		if (strpos($title, '#') !== false || strpos($html, '#') !== false) {
96			$title = $stats->embedTags($title);
97			$html  = $stats->embedTags($html);
98		}
99
100		/*
101		* Start Of Output
102		*/
103		$id    = $this->getName() . $block_id;
104		$class = $this->getName() . '_block';
105		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
106			$title = '<a class="icon-admin" title="' . I18N::translate('Preferences') . '" href="block_edit.php?block_id=' . $block_id . '&amp;ged=' . $WT_TREE->getNameHtml() . '&amp;ctype=' . $ctype . '"></a>' . $title;
107		}
108
109		$content = $html;
110
111		if ($show_timestamp) {
112			$content .= '<br>' . FunctionsDate::formatTimestamp($this->getBlockSetting($block_id, 'timestamp', WT_TIMESTAMP) + WT_TIMESTAMP_OFFSET);
113		}
114
115		if ($template) {
116			return Theme::theme()->formatBlock($id, $title, $class, $content);
117		} else {
118			return $content;
119		}
120	}
121
122	/** {@inheritdoc} */
123	public function loadAjax() {
124		return false;
125	}
126
127	/** {@inheritdoc} */
128	public function isUserBlock() {
129		return true;
130	}
131
132	/** {@inheritdoc} */
133	public function isGedcomBlock() {
134		return true;
135	}
136
137	/**
138	 * An HTML form to edit block settings
139	 *
140	 * @param int $block_id
141	 */
142	public function configureBlock($block_id) {
143		global $WT_TREE;
144
145		if (Filter::postBool('save') && Filter::checkCsrf()) {
146			$languages = Filter::postArray('lang');
147			$this->setBlockSetting($block_id, 'gedcom', Filter::post('gedcom'));
148			$this->setBlockSetting($block_id, 'title', Filter::post('title'));
149			$this->setBlockSetting($block_id, 'html', Filter::post('html'));
150			$this->setBlockSetting($block_id, 'show_timestamp', Filter::postBool('show_timestamp'));
151			$this->setBlockSetting($block_id, 'timestamp', Filter::post('timestamp'));
152			$this->setBlockSetting($block_id, 'languages', implode(',', $languages));
153		}
154
155		$templates = [
156			I18N::translate('Keyword examples') =>
157			'#getAllTagsTable#',
158
159			I18N::translate('Narrative description') =>
160			/* I18N: do not translate the #keywords# */ I18N::translate('This family tree was last updated on #gedcomUpdated#. There are #totalSurnames# surnames in this family tree. The earliest recorded event is the #firstEventType# of #firstEventName# in #firstEventYear#. The most recent event is the #lastEventType# of #lastEventName# in #lastEventYear#.<br><br>If you have any comments or feedback please contact #contactWebmaster#.'),
161
162			I18N::translate('Statistics') =>
163			'<div class="gedcom_stats">
164				<span style="font-weight: bold;"><a href="index.php?command=gedcom">#gedcomTitle#</a></span><br>
165				' . I18N::translate('This family tree was last updated on %s.', '#gedcomUpdated#') . '
166				<table id="keywords">
167					<tr>
168						<td class="width20">
169							<table cellspacing="1" cellpadding="0">
170								<tr>
171									<td class="facts_label">' . I18N::translate('Individuals') . '</td>
172									<td class="facts_value"><a href="indilist.php?surname_sublist=no">#totalIndividuals#</a></td>
173								</tr>
174								<tr>
175									<td class="facts_label">' . I18N::translate('Males') . '</td>
176									<td class="facts_value">#totalSexMales#<br>#totalSexMalesPercentage#</td>
177								</tr>
178								<tr>
179									<td class="facts_label">' . I18N::translate('Females') . '</td>
180									<td class="facts_value">#totalSexFemales#<br>#totalSexFemalesPercentage#</td>
181								</tr>
182								<tr>
183									<td class="facts_label">' . I18N::translate('Total surnames') . '</td>
184									<td class="facts_value"><a href="indilist.php?show_all=yes&amp;surname_sublist=yes&amp;ged=' . $WT_TREE->getNameUrl() . '">#totalSurnames#</a></td>
185								</tr>
186								<tr>
187									<td class="facts_label">' . I18N::translate('Families') . '</td>
188									<td class="facts_value"><a href="famlist.php?ged=' . $WT_TREE->getNameUrl() . '">#totalFamilies#</a></td>
189								</tr>
190								<tr>
191									<td class="facts_label">' . I18N::translate('Sources') . '</td>
192									<td class="facts_value"><a href="sourcelist.php?ged=' . $WT_TREE->getNameUrl() . '">#totalSources#</a></td>
193								</tr>
194								<tr>
195									<td class="facts_label">' . I18N::translate('Media objects') . '</td>
196									<td class="facts_value"><a href="medialist.php?ged=' . $WT_TREE->getNameUrl() . '">#totalMedia#</a></td>
197								</tr>
198								<tr>
199									<td class="facts_label">' . I18N::translate('Repositories') . '</td>
200									<td class="facts_value"><a href="repolist.php?ged=' . $WT_TREE->getNameUrl() . '">#totalRepositories#</a></td>
201								</tr>
202								<tr>
203									<td class="facts_label">' . I18N::translate('Total events') . '</td>
204									<td class="facts_value">#totalEvents#</td>
205								</tr>
206								<tr>
207									<td class="facts_label">' . I18N::translate('Total users') . '</td>
208									<td class="facts_value">#totalUsers#</td>
209								</tr>
210							</table>
211						</td>
212						<td><br></td>
213						<td>
214							<table cellspacing="1" cellpadding="0" border="0">
215								<tr>
216									<td class="facts_label">' . I18N::translate('Earliest birth year') . '</td>
217									<td class="facts_value">#firstBirthYear#</td>
218									<td class="facts_value">#firstBirth#</td>
219								</tr>
220								<tr>
221									<td class="facts_label">' . I18N::translate('Latest birth year') . '</td>
222									<td class="facts_value">#lastBirthYear#</td>
223									<td class="facts_value">#lastBirth#</td>
224								</tr>
225								<tr>
226									<td class="facts_label">' . I18N::translate('Earliest death year') . '</td>
227									<td class="facts_value">#firstDeathYear#</td>
228									<td class="facts_value">#firstDeath#</td>
229								</tr>
230								<tr>
231									<td class="facts_label">' . I18N::translate('Latest death year') . '</td>
232									<td class="facts_value">#lastDeathYear#</td>
233									<td class="facts_value">#lastDeath#</td>
234								</tr>
235								<tr>
236									<td class="facts_label">' . I18N::translate('Individual who lived the longest') . '</td>
237									<td class="facts_value">#longestLifeAge#</td>
238									<td class="facts_value">#longestLife#</td>
239								</tr>
240								<tr>
241									<td class="facts_label">' . I18N::translate('Average age at death') . '</td>
242									<td class="facts_value">#averageLifespan#</td>
243									<td class="facts_value"></td>
244								</tr>
245								<tr>
246									<td class="facts_label">' . I18N::translate('Family with the most children') . '</td>
247									<td class="facts_value">#largestFamilySize#</td>
248									<td class="facts_value">#largestFamily#</td>
249								</tr>
250								<tr>
251									<td class="facts_label">' . I18N::translate('Average number of children per family') . '</td>
252									<td class="facts_value">#averageChildren#</td>
253									<td class="facts_value"></td>
254								</tr>
255							</table>
256						</td>
257					</tr>
258				</table><br>
259				<span style="font-weight: bold;">' . I18N::translate('Most common surnames') . '</span><br>
260				#commonSurnames#
261			</div>',
262		];
263
264		$title          = $this->getBlockSetting($block_id, 'title');
265		$html           = $this->getBlockSetting($block_id, 'html');
266		$gedcom         = $this->getBlockSetting($block_id, 'gedcom');
267		$show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
268		$languages      = explode(',', $this->getBlockSetting($block_id, 'languages'));
269
270		echo '<tr><td class="descriptionbox wrap">',
271			GedcomTag::getLabel('TITL'),
272			'</td><td class="optionbox"><input type="text" name="title" size="30" value="', Filter::escapeHtml($title), '"></td></tr>';
273
274		// templates
275		echo '<tr><td class="descriptionbox wrap">',
276			I18N::translate('Templates'),
277			'</td><td class="optionbox wrap">';
278		// The CK editor needs lots of help to load/save data :-(
279		if (Module::getModuleByName('ckeditor')) {
280			$ckeditor_onchange = 'CKEDITOR.instances.html.setData(document.block.html.value);';
281		} else {
282			$ckeditor_onchange = '';
283		}
284		echo '<select name="template" onchange="document.block.html.value=document.block.template.options[document.block.template.selectedIndex].value;', $ckeditor_onchange, '">';
285		echo '<option value="', Filter::escapeHtml($html), '">', I18N::translate('Custom'), '</option>';
286		foreach ($templates as $title => $template) {
287			echo '<option value="', Filter::escapeHtml($template), '">', $title, '</option>';
288		}
289		echo '</select>';
290		if (!$html) {
291			echo '<p>', I18N::translate('To assist you in getting started with this block, we have created several standard templates. When you select one of these templates, the text area will contain a copy that you can then alter to suit your site’s requirements.'), '</p>';
292		}
293		echo '</td></tr>';
294
295		if (count(Tree::getAll()) > 1) {
296			if ($gedcom == '__current__') {$sel_current = 'selected'; } else {$sel_current = ''; }
297			if ($gedcom == '__default__') {$sel_default = 'selected'; } else {$sel_default = ''; }
298			echo '<tr><td class="descriptionbox wrap">',
299				I18N::translate('Family tree'),
300				'</td><td class="optionbox">',
301				'<select name="gedcom">',
302				'<option value="__current__" ', $sel_current, '>', I18N::translate('Current'), '</option>',
303				'<option value="__default__" ', $sel_default, '>', I18N::translate('Default'), '</option>';
304			foreach (Tree::getAll() as $tree) {
305				if ($tree->getName() === $gedcom) {$sel = 'selected'; } else {$sel = ''; }
306				echo '<option value="', $tree->getNameHtml(), '" ', $sel, '>', $tree->getTitleHtml(), '</option>';
307			}
308			echo '</select>';
309			echo '</td></tr>';
310		}
311
312		// html
313		echo '<tr><td colspan="2" class="descriptionbox">',
314			I18N::translate('Content');
315		if (!$html) {
316			echo '<p>', I18N::translate('As well as using the toolbar to apply HTML formatting, you can insert database fields which are updated automatically. These special fields are marked with <b>#</b> characters. For example <b>#totalFamilies#</b> will be replaced with the actual number of families in the database. Advanced users may wish to apply CSS classes to their text, so that the formatting matches the currently selected theme.'), '</p>';
317		}
318		echo
319			'</td></tr><tr>',
320			'<td colspan="2" class="optionbox">';
321		echo '<textarea name="html" class="html-edit" rows="10" style="width:98%;">', Filter::escapeHtml($html), '</textarea>';
322		echo '</td></tr>';
323
324		echo '<tr><td class="descriptionbox wrap">';
325		echo I18N::translate('Show the date and time of update');
326		echo '</td><td class="optionbox">';
327		echo FunctionsEdit::editFieldYesNo('show_timestamp', $show_timestamp);
328		echo '<input type="hidden" name="timestamp" value="', WT_TIMESTAMP, '">';
329		echo '</td></tr>';
330
331		echo '<tr><td class="descriptionbox wrap">';
332		echo I18N::translate('Show this block for which languages');
333		echo '</td><td class="optionbox">';
334		echo FunctionsEdit::editLanguageCheckboxes('lang', $languages);
335		echo '</td></tr>';
336	}
337}
338