xref: /webtrees/app/Module/HtmlBlockModule.php (revision 9353052e08ef3eb4cf05871a926f9ec2c45ba53b)
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 AbstractModule 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          = $this->getBlockSetting($block_id, 'title');
38		$html           = $this->getBlockSetting($block_id, 'html');
39		$gedcom         = $this->getBlockSetting($block_id, 'gedcom');
40		$show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
41		$languages      = $this->getBlockSetting($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 = Tree::findByName(Site::getPreference('DEFAULT_GEDCOM'));
57			if ($tree) {
58				$stats = new Stats($tree);
59			} else {
60				$stats = new Stats($WT_TREE);
61			}
62			break;
63		default:
64			$tree = Tree::findByName($gedcom);
65			if ($tree) {
66				$stats = new Stats($tree);
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' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
87			$title = '<a class="icon-admin" title="' . I18N::translate('Configure') . '" href="block_edit.php?block_id=' . $block_id . '&amp;ged=' . $WT_TREE->getNameHtml() . '&amp;ctype=' . $ctype . '"></a>';
88		}
89
90		$content = $html;
91
92		if ($show_timestamp) {
93			$content .= '<br>' . format_timestamp($this->getBlockSetting($block_id, 'timestamp', WT_TIMESTAMP) + WT_TIMESTAMP_OFFSET);
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		global $WT_TREE;
121
122		if (Filter::postBool('save') && Filter::checkCsrf()) {
123			$languages = Filter::postArray('lang');
124			$this->setBlockSetting($block_id, 'gedcom', Filter::post('gedcom'));
125			$this->setBlockSetting($block_id, 'title', Filter::post('title'));
126			$this->setBlockSetting($block_id, 'html', Filter::post('html'));
127			$this->setBlockSetting($block_id, 'show_timestamp', Filter::postBool('show_timestamp'));
128			$this->setBlockSetting($block_id, 'timestamp', Filter::post('timestamp'));
129			$this->setBlockSetting($block_id, 'languages', implode(',', $languages));
130		}
131
132		$templates = array(
133			I18N::translate('Keyword examples')=>
134			'#getAllTagsTable#',
135
136			I18N::translate('Narrative description')=>
137			/* 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#.'),
138
139			I18N::translate('Statistics')=>
140			'<div class="gedcom_stats">
141				<span style="font-weight: bold;"><a href="index.php?command=gedcom">#gedcomTitle#</a></span><br>
142				' . I18N::translate('This family tree was last updated on %s.', '#gedcomUpdated#') . '
143				<table id="keywords">
144					<tr>
145						<td valign="top" class="width20">
146							<table cellspacing="1" cellpadding="0">
147								<tr>
148									<td class="facts_label">'. I18N::translate('Individuals') . '</td>
149									<td class="facts_value" align="right"><a href="indilist.php?surname_sublist=no">#totalIndividuals#</a></td>
150								</tr>
151								<tr>
152									<td class="facts_label">'. I18N::translate('Males') . '</td>
153									<td class="facts_value" align="right">#totalSexMales#<br>#totalSexMalesPercentage#</td>
154								</tr>
155								<tr>
156									<td class="facts_label">'. I18N::translate('Females') . '</td>
157									<td class="facts_value" align="right">#totalSexFemales#<br>#totalSexFemalesPercentage#</td>
158								</tr>
159								<tr>
160									<td class="facts_label">'. I18N::translate('Total surnames') . '</td>
161									<td class="facts_value" align="right"><a href="indilist.php?show_all=yes&amp;surname_sublist=yes&amp;ged='. $WT_TREE->getNameUrl() . '">#totalSurnames#</a></td>
162								</tr>
163								<tr>
164									<td class="facts_label">'. I18N::translate('Families') . '</td>
165									<td class="facts_value" align="right"><a href="famlist.php?ged='. $WT_TREE->getNameUrl() . '">#totalFamilies#</a></td>
166								</tr>
167								<tr>
168									<td class="facts_label">'. I18N::translate('Sources') . '</td>
169									<td class="facts_value" align="right"><a href="sourcelist.php?ged='. $WT_TREE->getNameUrl() . '">#totalSources#</a></td>
170								</tr>
171								<tr>
172									<td class="facts_label">'. I18N::translate('Media objects') . '</td>
173									<td class="facts_value" align="right"><a href="medialist.php?ged='. $WT_TREE->getNameUrl() . '">#totalMedia#</a></td>
174								</tr>
175								<tr>
176									<td class="facts_label">'. I18N::translate('Repositories') . '</td>
177									<td class="facts_value" align="right"><a href="repolist.php?ged='. $WT_TREE->getNameUrl() . '">#totalRepositories#</a></td>
178								</tr>
179								<tr>
180									<td class="facts_label">'. I18N::translate('Total events') . '</td>
181									<td class="facts_value" align="right">#totalEvents#</td>
182								</tr>
183								<tr>
184									<td class="facts_label">'. I18N::translate('Total users') . '</td>
185									<td class="facts_value" align="right">#totalUsers#</td>
186								</tr>
187							</table>
188						</td>
189						<td><br></td>
190						<td valign="top">
191							<table cellspacing="1" cellpadding="0" border="0">
192								<tr>
193									<td class="facts_label">'. I18N::translate('Earliest birth year') . '</td>
194									<td class="facts_value" align="right">#firstBirthYear#</td>
195									<td class="facts_value">#firstBirth#</td>
196								</tr>
197								<tr>
198									<td class="facts_label">'. I18N::translate('Latest birth year') . '</td>
199									<td class="facts_value" align="right">#lastBirthYear#</td>
200									<td class="facts_value">#lastBirth#</td>
201								</tr>
202								<tr>
203									<td class="facts_label">'. I18N::translate('Earliest death year') . '</td>
204									<td class="facts_value" align="right">#firstDeathYear#</td>
205									<td class="facts_value">#firstDeath#</td>
206								</tr>
207								<tr>
208									<td class="facts_label">'. I18N::translate('Latest death year') . '</td>
209									<td class="facts_value" align="right">#lastDeathYear#</td>
210									<td class="facts_value">#lastDeath#</td>
211								</tr>
212								<tr>
213									<td class="facts_label">'. I18N::translate('Individual who lived the longest') . '</td>
214									<td class="facts_value" align="right">#longestLifeAge#</td>
215									<td class="facts_value">#longestLife#</td>
216								</tr>
217								<tr>
218									<td class="facts_label">'. I18N::translate('Average age at death') . '</td>
219									<td class="facts_value" align="right">#averageLifespan#</td>
220									<td class="facts_value"></td>
221								</tr>
222								<tr>
223									<td class="facts_label">'. I18N::translate('Family with the most children') . '</td>
224									<td class="facts_value" align="right">#largestFamilySize#</td>
225									<td class="facts_value">#largestFamily#</td>
226								</tr>
227								<tr>
228									<td class="facts_label">'. I18N::translate('Average number of children per family') . '</td>
229									<td class="facts_value" align="right">#averageChildren#</td>
230									<td class="facts_value"></td>
231								</tr>
232							</table>
233						</td>
234					</tr>
235				</table><br>
236				<span style="font-weight: bold;">' . I18N::translate('Most common surnames') . '</span><br>
237				#commonSurnames#
238			</div>'
239		);
240
241		$title          = $this->getBlockSetting($block_id, 'title');
242		$html           = $this->getBlockSetting($block_id, 'html');
243		$gedcom         = $this->getBlockSetting($block_id, 'gedcom');
244		$show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
245		$languages      = explode(',', $this->getBlockSetting($block_id, 'languages'));
246
247		echo '<tr><td class="descriptionbox wrap">',
248			GedcomTag::getLabel('TITL'),
249			'</td><td class="optionbox"><input type="text" name="title" size="30" value="', Filter::escapeHtml($title), '"></td></tr>';
250
251		// templates
252		echo '<tr><td class="descriptionbox wrap">',
253			I18N::translate('Templates'),
254			'</td><td class="optionbox wrap">';
255		// The CK editor needs lots of help to load/save data :-(
256		if (Module::getModuleByName('ckeditor')) {
257			$ckeditor_onchange = 'CKEDITOR.instances.html.setData(document.block.html.value);';
258		} else {
259			$ckeditor_onchange = '';
260		}
261		echo '<select name="template" onchange="document.block.html.value=document.block.template.options[document.block.template.selectedIndex].value;', $ckeditor_onchange, '">';
262		echo '<option value="', Filter::escapeHtml($html), '">', I18N::translate('Custom'), '</option>';
263		foreach ($templates as $title=>$template) {
264			echo '<option value="', Filter::escapeHtml($template), '">', $title, '</option>';
265		}
266		echo '</select>';
267		if (!$html) {
268			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>';
269		}
270		echo '</td></tr>';
271
272		if (count(Tree::getAll()) > 1) {
273			if ($gedcom == '__current__') {$sel_current = 'selected'; } else {$sel_current = ''; }
274			if ($gedcom == '__default__') {$sel_default = 'selected'; } else {$sel_default = ''; }
275			echo '<tr><td class="descriptionbox wrap">',
276				I18N::translate('Family tree'),
277				'</td><td class="optionbox">',
278				'<select name="gedcom">',
279				'<option value="__current__" ', $sel_current, '>', I18N::translate('Current'), '</option>',
280				'<option value="__default__" ', $sel_default, '>', I18N::translate('Default'), '</option>';
281			foreach (Tree::getAll() as $tree) {
282				if ($tree->getName() === $gedcom) {$sel = 'selected'; } else {$sel = ''; }
283				echo '<option value="', $tree->getNameHtml(), '" ', $sel, '>', $tree->getTitleHtml(), '</option>';
284			}
285			echo '</select>';
286			echo '</td></tr>';
287		}
288
289		// html
290		echo '<tr><td colspan="2" class="descriptionbox">',
291			I18N::translate('Content');
292		if (!$html) {
293			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>';
294		}
295		echo
296			'</td></tr><tr>',
297			'<td colspan="2" class="optionbox">';
298		echo '<textarea name="html" class="html-edit" rows="10" style="width:98%;">', Filter::escapeHtml($html), '</textarea>';
299		echo '</td></tr>';
300
301		echo '<tr><td class="descriptionbox wrap">';
302		echo I18N::translate('Show the date and time of update');
303		echo '</td><td class="optionbox">';
304		echo edit_field_yes_no('show_timestamp', $show_timestamp);
305		echo '<input type="hidden" name="timestamp" value="', WT_TIMESTAMP, '">';
306		echo '</td></tr>';
307
308		echo '<tr><td class="descriptionbox wrap">';
309		echo I18N::translate('Show this block for which languages?');
310		echo '</td><td class="optionbox">';
311		echo edit_language_checkboxes('lang', $languages);
312		echo '</td></tr>';
313	}
314}
315