xref: /webtrees/app/Module/HtmlBlockModule.php (revision 8262180855f3e1018b62e890a9924a637531d647)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 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\Bootstrap4;
20use Fisharebest\Webtrees\Filter;
21use Fisharebest\Webtrees\FontAwesome;
22use Fisharebest\Webtrees\Functions\FunctionsDate;
23use Fisharebest\Webtrees\Functions\FunctionsEdit;
24use Fisharebest\Webtrees\I18N;
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 = FontAwesome::linkIcon('preferences', I18N::translate('Preferences'), ['class' => 'btn btn-link', 'href' => 'block_edit.php?block_id=' . $block_id . '&ged=' . $WT_TREE->getNameHtml() . '&ctype=' . $ctype]) . ' ';
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', '__current__');
267		$show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
268		$languages      = explode(',', $this->getBlockSetting($block_id, 'languages'));
269
270		?>
271		<div class="row form-group">
272			<label class="col-sm-3 col-form-label" for="title">
273				<?= I18N::translate('Title') ?>
274			</label>
275			<div class="col-sm-9">
276				<input class="form-control" type="text" id="title" name="title" value="<?= Filter::escapeHtml($title) ?>">
277			</div>
278		</div>
279
280		<div class="row form-group">
281			<label class="col-sm-3 col-form-label" for="template">
282				<?= I18N::translate('Templates') ?>
283			</label>
284			<div class="col-sm-9">
285				<?= Bootstrap4::select([$html => I18N::translate('Custom')] + array_flip($templates), '', ['onchange' => 'this.form.html.value=this.options[this.selectedIndex].value; CKEDITOR.instances.html.setData(document.block.html.value);', 'id' => 'template']) ?>
286				<p class="small text-muted">
287					<?= 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.') ?>
288				</p>
289			</div>
290		</div>
291
292		<div class="row form-group">
293			<label class="col-sm-3 col-form-label" for="gedcom">
294				<?= I18N::translate('Family tree') ?>
295			</label>
296			<div class="col-sm-9">
297				<?= Bootstrap4::select(['__current__' => I18N::translate('Current'), '__default__' => I18N::translate('Default')] + Tree::getNameList(), $gedcom, ['id' => 'gedcom', 'name' => 'gedcom']) ?>
298			</div>
299		</div>
300
301		<div class="row form-group">
302			<label class="col-sm-3 col-form-label" for="html">
303				<?= I18N::translate('Content') ?>
304			</label>
305			<div class="col-sm-9">
306				<p>
307					<?= 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.') ?>
308				</p>
309			</div>
310		</div>
311
312		<div class="row form-group">
313			<textarea name="html" id="html" class="html-edit" rows="10"><?= Filter::escapeHtml($html) ?></textarea>
314		</div>
315
316		<fieldset class="form-group">
317			<div class="row">
318				<legend class="form-control-legend col-sm-3">
319					<?= I18N::translate('Show the date and time of update') ?>
320				</legend>
321				<div class="col-sm-9">
322					<?= Bootstrap4::radioButtons('timestamp', FunctionsEdit::optionsNoYes(), $show_timestamp, true) ?>
323				</div>
324			</div>
325		</fieldset>
326
327		<fieldset class="form-group">
328			<div class="row">
329				<legend class="form-control-legend col-sm-3">
330					<?= I18N::translate('Show this block for which languages') ?>
331				</legend>
332				<div class="col-sm-9">
333					<?= FunctionsEdit::editLanguageCheckboxes('lang', $languages) ?>
334				</div>
335			</div>
336		</fieldset>
337
338		<?php
339	}
340}
341