xref: /webtrees/app/Module/HtmlBlockModule.php (revision 4cb8d1f0eb760216ff1402f3415ef3e289b4d8b3)
1<?php
2namespace Fisharebest\Webtrees;
3
4/**
5 * webtrees: online genealogy
6 * Copyright (C) 2015 webtrees development team
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/**
20 * Class HtmlBlockModule
21 */
22class HtmlBlockModule extends Module implements ModuleBlockInterface {
23	/** {@inheritdoc} */
24	public function getTitle() {
25		return /* I18N: Name of a module */ I18N::translate('HTML');
26	}
27
28	/** {@inheritdoc} */
29	public function getDescription() {
30		return /* I18N: Description of the “HTML” module */ I18N::translate('Add your own text and graphics.');
31	}
32
33	/** {@inheritdoc} */
34	public function getBlock($block_id, $template = true, $cfg = null) {
35		global $ctype, $WT_TREE;
36
37		$title          = get_block_setting($block_id, 'title');
38		$html           = get_block_setting($block_id, 'html');
39		$gedcom         = get_block_setting($block_id, 'gedcom');
40		$show_timestamp = get_block_setting($block_id, 'show_timestamp', '0');
41		$languages      = get_block_setting($block_id, 'languages');
42
43		// Only show this block for certain languages
44		if ($languages && !in_array(WT_LOCALE, explode(',', $languages))) {
45			return '';
46		}
47
48		/*
49		 * Select GEDCOM
50		 */
51		switch ($gedcom) {
52		case '__current__':
53			$stats = new Stats($WT_TREE);
54			break;
55		case '__default__':
56			$tree_id = Tree::getIdFromName(Site::getPreference('DEFAULT_GEDCOM'));
57			if ($tree_id) {
58				$stats = new Stats(Tree::findById($tree_id));
59			} else {
60				$stats = new Stats($WT_TREE);
61			}
62			break;
63		default:
64			$tree_id = Tree::getIdFromName($gedcom);
65			if ($tree_id) {
66				$stats = new Stats(Tree::findById($tree_id));
67			} else {
68				$stats = new Stats($WT_TREE);
69			}
70			break;
71		}
72
73		/*
74		* Retrieve text, process embedded variables
75		*/
76		if (strpos($title, '#') !== false || strpos($html, '#') !== false) {
77			$title = $stats->embedTags($title);
78			$html  = $stats->embedTags($html);
79		}
80
81		/*
82		* Start Of Output
83		*/
84		$id = $this->getName() . $block_id;
85		$class = $this->getName() . '_block';
86		if ($ctype === 'gedcom' && WT_USER_GEDCOM_ADMIN || $ctype === 'user' && Auth::check()) {
87			$title = '<i class="icon-admin" title="' . I18N::translate('Configure') . '" onclick="modalDialog(\'block_edit.php?block_id=' . $block_id . '\', \'' . $this->getTitle() . '\');"></i>' . $title;
88		}
89
90		$content = $html;
91
92		if ($show_timestamp) {
93			$content .= '<br>' . format_timestamp(get_block_setting($block_id, 'timestamp', WT_TIMESTAMP));
94		}
95
96		if ($template) {
97			return Theme::theme()->formatBlock($id, $title, $class, $content);
98		} else {
99			return $content;
100		}
101	}
102
103	/** {@inheritdoc} */
104	public function loadAjax() {
105		return false;
106	}
107
108	/** {@inheritdoc} */
109	public function isUserBlock() {
110		return true;
111	}
112
113	/** {@inheritdoc} */
114	public function isGedcomBlock() {
115		return true;
116	}
117
118	/** {@inheritdoc} */
119	public function configureBlock($block_id) {
120		if (Filter::postBool('save') && Filter::checkCsrf()) {
121			set_block_setting($block_id, 'gedcom', Filter::post('gedcom'));
122			set_block_setting($block_id, 'title', Filter::post('title'));
123			set_block_setting($block_id, 'html', Filter::post('html'));
124			set_block_setting($block_id, 'show_timestamp', Filter::postBool('show_timestamp'));
125			set_block_setting($block_id, 'timestamp', Filter::post('timestamp'));
126			$languages = Filter::postArray('lang', null, array_keys(I18N::installed_languages()));
127			set_block_setting($block_id, 'languages', implode(',', $languages));
128		}
129
130		$templates = array(
131			I18N::translate('Keyword examples')=>
132			'#getAllTagsTable#',
133
134			I18N::translate('Narrative description')=>
135			/* 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#.'),
136
137			I18N::translate('Statistics')=>
138			'<div class="gedcom_stats">
139				<span style="font-weight: bold;"><a href="index.php?command=gedcom">#gedcomTitle#</a></span><br>
140				' . I18N::translate('This family tree was last updated on %s.', '#gedcomUpdated#') . '
141				<table id="keywords">
142					<tr>
143						<td valign="top" class="width20">
144							<table cellspacing="1" cellpadding="0">
145								<tr>
146									<td class="facts_label">'. I18N::translate('Individuals') . '</td>
147									<td class="facts_value" align="right"><a href="indilist.php?surname_sublist=no">#totalIndividuals#</a></td>
148								</tr>
149								<tr>
150									<td class="facts_label">'. I18N::translate('Males') . '</td>
151									<td class="facts_value" align="right">#totalSexMales#<br>#totalSexMalesPercentage#</td>
152								</tr>
153								<tr>
154									<td class="facts_label">'. I18N::translate('Females') . '</td>
155									<td class="facts_value" align="right">#totalSexFemales#<br>#totalSexFemalesPercentage#</td>
156								</tr>
157								<tr>
158									<td class="facts_label">'. I18N::translate('Total surnames') . '</td>
159									<td class="facts_value" align="right"><a href="indilist.php?show_all=yes&amp;surname_sublist=yes&amp;ged='.WT_GEDURL . '">#totalSurnames#</a></td>
160								</tr>
161								<tr>
162									<td class="facts_label">'. I18N::translate('Families') . '</td>
163									<td class="facts_value" align="right"><a href="famlist.php?ged='.WT_GEDURL . '">#totalFamilies#</a></td>
164								</tr>
165								<tr>
166									<td class="facts_label">'. I18N::translate('Sources') . '</td>
167									<td class="facts_value" align="right"><a href="sourcelist.php?ged='.WT_GEDURL . '">#totalSources#</a></td>
168								</tr>
169								<tr>
170									<td class="facts_label">'. I18N::translate('Media objects') . '</td>
171									<td class="facts_value" align="right"><a href="medialist.php?ged='.WT_GEDURL . '">#totalMedia#</a></td>
172								</tr>
173								<tr>
174									<td class="facts_label">'. I18N::translate('Repositories') . '</td>
175									<td class="facts_value" align="right"><a href="repolist.php?ged='.WT_GEDURL . '">#totalRepositories#</a></td>
176								</tr>
177								<tr>
178									<td class="facts_label">'. I18N::translate('Total events') . '</td>
179									<td class="facts_value" align="right">#totalEvents#</td>
180								</tr>
181								<tr>
182									<td class="facts_label">'. I18N::translate('Total users') . '</td>
183									<td class="facts_value" align="right">#totalUsers#</td>
184								</tr>
185							</table>
186						</td>
187						<td><br></td>
188						<td valign="top">
189							<table cellspacing="1" cellpadding="0" border="0">
190								<tr>
191									<td class="facts_label">'. I18N::translate('Earliest birth year') . '</td>
192									<td class="facts_value" align="right">#firstBirthYear#</td>
193									<td class="facts_value">#firstBirth#</td>
194								</tr>
195								<tr>
196									<td class="facts_label">'. I18N::translate('Latest birth year') . '</td>
197									<td class="facts_value" align="right">#lastBirthYear#</td>
198									<td class="facts_value">#lastBirth#</td>
199								</tr>
200								<tr>
201									<td class="facts_label">'. I18N::translate('Earliest death year') . '</td>
202									<td class="facts_value" align="right">#firstDeathYear#</td>
203									<td class="facts_value">#firstDeath#</td>
204								</tr>
205								<tr>
206									<td class="facts_label">'. I18N::translate('Latest death year') . '</td>
207									<td class="facts_value" align="right">#lastDeathYear#</td>
208									<td class="facts_value">#lastDeath#</td>
209								</tr>
210								<tr>
211									<td class="facts_label">'. I18N::translate('Individual who lived the longest') . '</td>
212									<td class="facts_value" align="right">#longestLifeAge#</td>
213									<td class="facts_value">#longestLife#</td>
214								</tr>
215								<tr>
216									<td class="facts_label">'. I18N::translate('Average age at death') . '</td>
217									<td class="facts_value" align="right">#averageLifespan#</td>
218									<td class="facts_value"></td>
219								</tr>
220								<tr>
221									<td class="facts_label">'. I18N::translate('Family with the most children') . '</td>
222									<td class="facts_value" align="right">#largestFamilySize#</td>
223									<td class="facts_value">#largestFamily#</td>
224								</tr>
225								<tr>
226									<td class="facts_label">'. I18N::translate('Average number of children per family') . '</td>
227									<td class="facts_value" align="right">#averageChildren#</td>
228									<td class="facts_value"></td>
229								</tr>
230							</table>
231						</td>
232					</tr>
233				</table><br>
234				<span style="font-weight: bold;">' . I18N::translate('Most common surnames') . '</span><br>
235				#commonSurnames#
236			</div>'
237		);
238
239		$title          = get_block_setting($block_id, 'title');
240		$html           = get_block_setting($block_id, 'html');
241		$gedcom         = get_block_setting($block_id, 'gedcom');
242		$show_timestamp = get_block_setting($block_id, 'show_timestamp', '0');
243		$languages      = explode(',', get_block_setting($block_id, 'languages'));
244
245		echo '<tr><td class="descriptionbox wrap">',
246			WT_Gedcom_Tag::getLabel('TITL'),
247			'</td><td class="optionbox"><input type="text" name="title" size="30" value="', Filter::escapeHtml($title), '"></td></tr>';
248
249		// templates
250		echo '<tr><td class="descriptionbox wrap">',
251			I18N::translate('Templates'),
252			'</td><td class="optionbox wrap">';
253		// The CK editor needs lots of help to load/save data :-(
254		if (Module::getModuleByName('ckeditor')) {
255			$ckeditor_onchange = 'CKEDITOR.instances.html.setData(document.block.html.value);';
256		} else {
257			$ckeditor_onchange = '';
258		}
259		echo '<select name="template" onchange="document.block.html.value=document.block.template.options[document.block.template.selectedIndex].value;', $ckeditor_onchange, '">';
260		echo '<option value="', Filter::escapeHtml($html), '">', I18N::translate('Custom'), '</option>';
261		foreach ($templates as $title=>$template) {
262			echo '<option value="', Filter::escapeHtml($template), '">', $title, '</option>';
263		}
264		echo '</select>';
265		if (!$html) {
266			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>';
267		}
268		echo '</td></tr>';
269
270		if (count(Tree::getAll()) > 1) {
271			if ($gedcom == '__current__') {$sel_current = 'selected'; } else {$sel_current = ''; }
272			if ($gedcom == '__default__') {$sel_default = 'selected'; } else {$sel_default = ''; }
273			echo '<tr><td class="descriptionbox wrap">',
274				I18N::translate('Family tree'),
275				'</td><td class="optionbox">',
276				'<select name="gedcom">',
277				'<option value="__current__" ', $sel_current, '>', I18N::translate('Current'), '</option>',
278				'<option value="__default__" ', $sel_default, '>', I18N::translate('Default'), '</option>';
279			foreach (Tree::getAll() as $tree) {
280				if ($tree->getName() === $gedcom) {$sel = 'selected'; } else {$sel = ''; }
281				echo '<option value="', $tree->getNameHtml(), '" ', $sel, ' dir="auto">', $tree->getTitleHtml(), '</option>';
282			}
283			echo '</select>';
284			echo '</td></tr>';
285		}
286
287		// html
288		echo '<tr><td colspan="2" class="descriptionbox">',
289			I18N::translate('Content');
290		if (!$html) {
291			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>';
292		}
293		echo
294			'</td></tr><tr>',
295			'<td colspan="2" class="optionbox">';
296		echo '<textarea name="html" class="html-edit" rows="10" style="width:98%;">', Filter::escapeHtml($html), '</textarea>';
297		echo '</td></tr>';
298
299		echo '<tr><td class="descriptionbox wrap">';
300		echo I18N::translate('Show the date and time of update');
301		echo '</td><td class="optionbox">';
302		echo edit_field_yes_no('show_timestamp', $show_timestamp);
303		echo '<input type="hidden" name="timestamp" value="', WT_TIMESTAMP, '">';
304		echo '</td></tr>';
305
306		echo '<tr><td class="descriptionbox wrap">';
307		echo I18N::translate('Show this block for which languages?');
308		echo '</td><td class="optionbox">';
309		echo edit_language_checkboxes('lang', $languages);
310		echo '</td></tr>';
311	}
312}
313