xref: /webtrees/app/Module/HtmlBlockModule.php (revision 816b6e467bbd615761680bed6ea4ecb16fe66602)
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\Html;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Site;
27use Fisharebest\Webtrees\Stats;
28use Fisharebest\Webtrees\Theme;
29use Fisharebest\Webtrees\Tree;
30
31/**
32 * Class HtmlBlockModule
33 */
34class HtmlBlockModule extends AbstractModule implements ModuleBlockInterface {
35	/** {@inheritdoc} */
36	public function getTitle() {
37		return /* I18N: Name of a module */ I18N::translate('HTML');
38	}
39
40	/** {@inheritdoc} */
41	public function getDescription() {
42		return /* I18N: Description of the “HTML” module */ I18N::translate('Add your own text and graphics.');
43	}
44
45	/**
46	 * Generate the HTML content of this block.
47	 *
48	 * @param int      $block_id
49	 * @param bool     $template
50	 * @param string[] $cfg
51	 *
52	 * @return string
53	 */
54	public function getBlock($block_id, $template = true, $cfg = []) {
55		global $ctype, $WT_TREE;
56
57		$title          = $this->getBlockSetting($block_id, 'title');
58		$html           = $this->getBlockSetting($block_id, 'html');
59		$gedcom         = $this->getBlockSetting($block_id, 'gedcom');
60		$show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
61		$languages      = $this->getBlockSetting($block_id, 'languages');
62
63		// Only show this block for certain languages
64		if ($languages && !in_array(WT_LOCALE, explode(',', $languages))) {
65			return '';
66		}
67
68		/*
69		 * Select GEDCOM
70		 */
71		switch ($gedcom) {
72		case '__current__':
73			$stats = new Stats($WT_TREE);
74			break;
75		case '__default__':
76			$tree = Tree::findByName(Site::getPreference('DEFAULT_GEDCOM'));
77			if ($tree) {
78				$stats = new Stats($tree);
79			} else {
80				$stats = new Stats($WT_TREE);
81			}
82			break;
83		default:
84			$tree = Tree::findByName($gedcom);
85			if ($tree) {
86				$stats = new Stats($tree);
87			} else {
88				$stats = new Stats($WT_TREE);
89			}
90			break;
91		}
92
93		/*
94		* Retrieve text, process embedded variables
95		*/
96		if (strpos($title, '#') !== false || strpos($html, '#') !== false) {
97			$title = $stats->embedTags($title);
98			$html  = $stats->embedTags($html);
99		}
100
101		/*
102		* Start Of Output
103		*/
104		$id    = $this->getName() . $block_id;
105		$class = $this->getName() . '_block';
106		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
107			$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]) . ' ' . $title;
108		}
109
110		$content = $html;
111
112		if ($show_timestamp) {
113			$content .= '<br>' . FunctionsDate::formatTimestamp($this->getBlockSetting($block_id, 'timestamp', WT_TIMESTAMP) + WT_TIMESTAMP_OFFSET);
114		}
115
116		if ($template) {
117			return Theme::theme()->formatBlock($id, $title, $class, $content);
118		} else {
119			return $content;
120		}
121	}
122
123	/** {@inheritdoc} */
124	public function loadAjax() {
125		return false;
126	}
127
128	/** {@inheritdoc} */
129	public function isUserBlock() {
130		return true;
131	}
132
133	/** {@inheritdoc} */
134	public function isGedcomBlock() {
135		return true;
136	}
137
138	/**
139	 * An HTML form to edit block settings
140	 *
141	 * @param int $block_id
142	 */
143	public function configureBlock($block_id) {
144		global $WT_TREE;
145
146		if (Filter::postBool('save') && Filter::checkCsrf()) {
147			$languages = Filter::postArray('lang');
148			$this->setBlockSetting($block_id, 'gedcom', Filter::post('gedcom'));
149			$this->setBlockSetting($block_id, 'title', Filter::post('title'));
150			$this->setBlockSetting($block_id, 'html', Filter::post('html'));
151			$this->setBlockSetting($block_id, 'show_timestamp', Filter::postBool('show_timestamp'));
152			$this->setBlockSetting($block_id, 'timestamp', Filter::post('timestamp'));
153			$this->setBlockSetting($block_id, 'languages', implode(',', $languages));
154		}
155
156		$templates = [
157			I18N::translate('Keyword examples') =>
158			'#getAllTagsTable#',
159
160			I18N::translate('Narrative description') =>
161			/* 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#.'),
162
163			I18N::translate('Statistics') =>
164			'<div class="gedcom_stats">
165				<span style="font-weight: bold;"><a href="index.php?command=gedcom">#gedcomTitle#</a></span><br>
166				' . I18N::translate('This family tree was last updated on %s.', '#gedcomUpdated#') . '
167				<table id="keywords">
168					<tr>
169						<td class="width20">
170							<table cellspacing="1" cellpadding="0">
171								<tr>
172									<td class="facts_label">' . I18N::translate('Individuals') . '</td>
173									<td class="facts_value"><a href="indilist.php?surname_sublist=no">#totalIndividuals#</a></td>
174								</tr>
175								<tr>
176									<td class="facts_label">' . I18N::translate('Males') . '</td>
177									<td class="facts_value">#totalSexMales#<br>#totalSexMalesPercentage#</td>
178								</tr>
179								<tr>
180									<td class="facts_label">' . I18N::translate('Females') . '</td>
181									<td class="facts_value">#totalSexFemales#<br>#totalSexFemalesPercentage#</td>
182								</tr>
183								<tr>
184									<td class="facts_label">' . I18N::translate('Total surnames') . '</td>
185									<td class="facts_value"><a href="indilist.php?show_all=yes&amp;surname_sublist=yes&amp;ged=' . $WT_TREE->getNameUrl() . '">#totalSurnames#</a></td>
186								</tr>
187								<tr>
188									<td class="facts_label">' . I18N::translate('Families') . '</td>
189									<td class="facts_value"><a href="famlist.php?ged=' . $WT_TREE->getNameUrl() . '">#totalFamilies#</a></td>
190								</tr>
191								<tr>
192									<td class="facts_label">' . I18N::translate('Sources') . '</td>
193									<td class="facts_value"><a href="sourcelist.php?ged=' . $WT_TREE->getNameUrl() . '">#totalSources#</a></td>
194								</tr>
195								<tr>
196									<td class="facts_label">' . I18N::translate('Media objects') . '</td>
197									<td class="facts_value"><a href="medialist.php?ged=' . $WT_TREE->getNameUrl() . '">#totalMedia#</a></td>
198								</tr>
199								<tr>
200									<td class="facts_label">' . I18N::translate('Repositories') . '</td>
201									<td class="facts_value"><a href="repolist.php?ged=' . $WT_TREE->getNameUrl() . '">#totalRepositories#</a></td>
202								</tr>
203								<tr>
204									<td class="facts_label">' . I18N::translate('Total events') . '</td>
205									<td class="facts_value">#totalEvents#</td>
206								</tr>
207								<tr>
208									<td class="facts_label">' . I18N::translate('Total users') . '</td>
209									<td class="facts_value">#totalUsers#</td>
210								</tr>
211							</table>
212						</td>
213						<td><br></td>
214						<td>
215							<table cellspacing="1" cellpadding="0" border="0">
216								<tr>
217									<td class="facts_label">' . I18N::translate('Earliest birth year') . '</td>
218									<td class="facts_value">#firstBirthYear#</td>
219									<td class="facts_value">#firstBirth#</td>
220								</tr>
221								<tr>
222									<td class="facts_label">' . I18N::translate('Latest birth year') . '</td>
223									<td class="facts_value">#lastBirthYear#</td>
224									<td class="facts_value">#lastBirth#</td>
225								</tr>
226								<tr>
227									<td class="facts_label">' . I18N::translate('Earliest death year') . '</td>
228									<td class="facts_value">#firstDeathYear#</td>
229									<td class="facts_value">#firstDeath#</td>
230								</tr>
231								<tr>
232									<td class="facts_label">' . I18N::translate('Latest death year') . '</td>
233									<td class="facts_value">#lastDeathYear#</td>
234									<td class="facts_value">#lastDeath#</td>
235								</tr>
236								<tr>
237									<td class="facts_label">' . I18N::translate('Individual who lived the longest') . '</td>
238									<td class="facts_value">#longestLifeAge#</td>
239									<td class="facts_value">#longestLife#</td>
240								</tr>
241								<tr>
242									<td class="facts_label">' . I18N::translate('Average age at death') . '</td>
243									<td class="facts_value">#averageLifespan#</td>
244									<td class="facts_value"></td>
245								</tr>
246								<tr>
247									<td class="facts_label">' . I18N::translate('Family with the most children') . '</td>
248									<td class="facts_value">#largestFamilySize#</td>
249									<td class="facts_value">#largestFamily#</td>
250								</tr>
251								<tr>
252									<td class="facts_label">' . I18N::translate('Average number of children per family') . '</td>
253									<td class="facts_value">#averageChildren#</td>
254									<td class="facts_value"></td>
255								</tr>
256							</table>
257						</td>
258					</tr>
259				</table><br>
260				<span style="font-weight: bold;">' . I18N::translate('Most common surnames') . '</span><br>
261				#commonSurnames#
262			</div>',
263		];
264
265		$title          = $this->getBlockSetting($block_id, 'title');
266		$html           = $this->getBlockSetting($block_id, 'html');
267		$gedcom         = $this->getBlockSetting($block_id, 'gedcom', '__current__');
268		$show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
269		$languages      = explode(',', $this->getBlockSetting($block_id, 'languages'));
270
271		?>
272		<div class="row form-group">
273			<label class="col-sm-3 col-form-label" for="title">
274				<?= I18N::translate('Title') ?>
275			</label>
276			<div class="col-sm-9">
277				<input class="form-control" type="text" id="title" name="title" value="<?= Html::escape($title) ?>">
278			</div>
279		</div>
280
281		<div class="row form-group">
282			<label class="col-sm-3 col-form-label" for="template">
283				<?= I18N::translate('Templates') ?>
284			</label>
285			<div class="col-sm-9">
286				<?= 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']) ?>
287				<p class="small text-muted">
288					<?= 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.') ?>
289				</p>
290			</div>
291		</div>
292
293		<div class="row form-group">
294			<label class="col-sm-3 col-form-label" for="gedcom">
295				<?= I18N::translate('Family tree') ?>
296			</label>
297			<div class="col-sm-9">
298				<?= Bootstrap4::select(['__current__' => I18N::translate('Current'), '__default__' => I18N::translate('Default')] + Tree::getNameList(), $gedcom, ['id' => 'gedcom', 'name' => 'gedcom']) ?>
299			</div>
300		</div>
301
302		<div class="row form-group">
303			<label class="col-sm-3 col-form-label" for="html">
304				<?= I18N::translate('Content') ?>
305			</label>
306			<div class="col-sm-9">
307				<p>
308					<?= 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.') ?>
309				</p>
310			</div>
311		</div>
312
313		<div class="row form-group">
314			<textarea name="html" id="html" class="html-edit" rows="10"><?= Html::escape($html) ?></textarea>
315		</div>
316
317		<fieldset class="form-group">
318			<div class="row">
319				<legend class="form-control-legend col-sm-3">
320					<?= I18N::translate('Show the date and time of update') ?>
321				</legend>
322				<div class="col-sm-9">
323					<?= Bootstrap4::radioButtons('timestamp', FunctionsEdit::optionsNoYes(), $show_timestamp, true) ?>
324				</div>
325			</div>
326		</fieldset>
327
328		<fieldset class="form-group">
329			<div class="row">
330				<legend class="form-control-legend col-sm-3">
331					<?= I18N::translate('Show this block for which languages') ?>
332				</legend>
333				<div class="col-sm-9">
334					<?= FunctionsEdit::editLanguageCheckboxes('lang', $languages) ?>
335				</div>
336			</div>
337		</fieldset>
338
339		<?php
340	}
341}
342