xref: /webtrees/app/Module/HtmlBlockModule.php (revision 397e599a3131684b8a335ebfddcfe63828b9f51b)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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\Functions\FunctionsDate;
22use Fisharebest\Webtrees\Functions\FunctionsEdit;
23use Fisharebest\Webtrees\Html;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Site;
26use Fisharebest\Webtrees\Stats;
27use Fisharebest\Webtrees\Tree;
28
29/**
30 * Class HtmlBlockModule
31 */
32class HtmlBlockModule extends AbstractModule implements ModuleBlockInterface {
33	/** {@inheritdoc} */
34	public function getTitle() {
35		return /* I18N: Name of a module */
36			I18N::translate('HTML');
37	}
38
39	/** {@inheritdoc} */
40	public function getDescription() {
41		return /* I18N: Description of the “HTML” module */
42			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 = []): string {
55		global $ctype, $WT_TREE;
56
57		$title          = $this->getBlockSetting($block_id, 'title', '');
58		$content        = $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		$title   = $stats->embedTags($title);
97		$content = $stats->embedTags($content);
98
99		if ($show_timestamp === '1') {
100			$content .= '<br>' . FunctionsDate::formatTimestamp($this->getBlockSetting($block_id, 'timestamp', WT_TIMESTAMP) + WT_TIMESTAMP_OFFSET);
101		}
102
103		if ($template) {
104			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE)) {
105				$config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
106			} elseif ($ctype === 'user' && Auth::check()) {
107				$config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
108			} else {
109				$config_url = '';
110			}
111
112			return view('blocks/template', [
113				'block'      => str_replace('_', '-', $this->getName()),
114				'id'         => $block_id,
115				'config_url' => $config_url,
116				'title'      => $title,
117				'content'    => $content,
118			]);
119		} else {
120			return $content;
121		}
122	}
123
124	/** {@inheritdoc} */
125	public function loadAjax(): bool {
126		return false;
127	}
128
129	/** {@inheritdoc} */
130	public function isUserBlock(): bool {
131		return true;
132	}
133
134	/** {@inheritdoc} */
135	public function isGedcomBlock(): bool {
136		return true;
137	}
138
139	/**
140	 * An HTML form to edit block settings
141	 *
142	 * @param int $block_id
143	 *
144	 * @return void
145	 */
146	public function configureBlock($block_id) {
147		global $WT_TREE;
148
149		if (Filter::postBool('save') && Filter::checkCsrf()) {
150			$languages = Filter::postArray('lang');
151			$this->setBlockSetting($block_id, 'gedcom', Filter::post('gedcom'));
152			$this->setBlockSetting($block_id, 'title', Filter::post('title'));
153			$this->setBlockSetting($block_id, 'html', Filter::post('html'));
154			$this->setBlockSetting($block_id, 'show_timestamp', Filter::postBool('show_timestamp'));
155			$this->setBlockSetting($block_id, 'timestamp', Filter::post('timestamp'));
156			$this->setBlockSetting($block_id, 'languages', implode(',', $languages));
157		}
158
159		$templates = [
160			I18N::translate('Keyword examples') => '#getAllTagsTable#',
161
162			I18N::translate('Narrative description') => /* 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#.'),
163
164			I18N::translate('Statistics') => '<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					<div class="row">
168						<div class="col col-sm-4">
169							<table class="table wt-facts-table">
170								<tr>
171									<th scope="row">' . I18N::translate('Individuals') . '</th>
172									<td>#totalIndividuals#</td>
173								</tr>
174								<tr>
175									<th scope="row">' . I18N::translate('Males') . '</th>
176									<td>#totalSexMales#<br>#totalSexMalesPercentage#</td>
177								</tr>
178								<tr>
179									<th scope="row">' . I18N::translate('Females') . '</th>
180									<td>#totalSexFemales#<br>#totalSexFemalesPercentage#</td>
181								</tr>
182								<tr>
183									<th scope="row">' . I18N::translate('Total surnames') . '</th>
184									<td>#totalSurnames#</td>
185								</tr>
186								<tr>
187									<th scope="row">' . I18N::translate('Families') . '</th>
188									<td>#totalFamilies#</td>
189								</tr>
190								<tr>
191									<th scope="row">' . I18N::translate('Sources') . '</th>
192									<td>#totalSources#</td>
193								</tr>
194								<tr>
195									<th scope="row">' . I18N::translate('Media objects') . '</th>
196									<td>#totalMedia#</td>
197								</tr>
198								<tr>
199									<th scope="row">' . I18N::translate('Repositories') . '</th>
200									<td>#totalRepositories#</td>
201								</tr>
202								<tr>
203									<th scope="row">' . I18N::translate('Events') . '</th>
204									<td>#totalEvents#</td>
205								</tr>
206								<tr>
207									<th scope="row">' . I18N::translate('Users') . '</th>
208									<td>#totalUsers#</td>
209								</tr>
210							</table>
211						</div>
212
213						<div class="col col-sm-8">
214							<table class="table wt-facts-table">
215								<tr>
216									<th scope="row">' . I18N::translate('Earliest birth') . '</th>
217									<td>#firstBirth#</td>
218								</tr>
219								<tr>
220									<th scope="row">' . I18N::translate('Latest birth') . '</th>
221									<td>#lastBirth#</td>
222								</tr>
223								<tr>
224									<th scope="row">' . I18N::translate('Earliest death') . '</th>
225									<td>#firstDeath#</td>
226								</tr>
227								<tr>
228									<th scope="row">' . I18N::translate('Latest death') . '</th>
229									<td>#lastDeath#</td>
230								</tr>
231								<tr>
232									<th scope="row">' . I18N::translate('Individual who lived the longest') . '</th>
233									<td>#longestLife#</td>
234								</tr>
235								<tr>
236									<th scope="row">' . I18N::translate('Average age at death') . '</th>
237									<td>#averageLifespan#</td>
238								</tr>
239								<tr>
240									<th scope="row">' . I18N::translate('Family with the most children') . '</th>
241									<td>#largestFamilySize#<br>#largestFamily#</td>
242								</tr>
243								<tr>
244									<th scope="row">' . I18N::translate('Average number of children per family') . '</th>
245									<td>#averageChildren#</td>
246								</tr>
247							</table>
248						</div>
249					</div>
250					<br>
251					<span style="font-weight: bold;">' . I18N::translate('Most common surnames') . '</span>
252					<br>
253					#commonSurnames#
254				</div>',
255		];
256
257		$title          = $this->getBlockSetting($block_id, 'title', '');
258		$html           = $this->getBlockSetting($block_id, 'html', '');
259		$gedcom         = $this->getBlockSetting($block_id, 'gedcom', '__current__');
260		$show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
261		$languages      = explode(',', $this->getBlockSetting($block_id, 'languages'));
262
263		?>
264		<div class="row form-group">
265			<label class="col-sm-3 col-form-label" for="title">
266				<?= I18N::translate('Title') ?>
267			</label>
268			<div class="col-sm-9">
269				<input class="form-control" type="text" id="title" name="title" value="<?= e($title) ?>">
270			</div>
271		</div>
272
273		<div class="row form-group">
274			<label class="col-sm-3 col-form-label" for="template">
275				<?= I18N::translate('Templates') ?>
276			</label>
277			<div class="col-sm-9">
278				<?= 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']) ?>
279				<p class="small text-muted">
280					<?= 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.') ?>
281				</p>
282			</div>
283		</div>
284
285		<div class="row form-group">
286			<label class="col-sm-3 col-form-label" for="gedcom">
287				<?= I18N::translate('Family tree') ?>
288			</label>
289			<div class="col-sm-9">
290				<?= Bootstrap4::select(['__current__' => I18N::translate('Current'), '__default__' => I18N::translate('Default')] + Tree::getNameList(), $gedcom, ['id' => 'gedcom', 'name' => 'gedcom']) ?>
291			</div>
292		</div>
293
294		<div class="row form-group">
295			<label class="col-sm-3 col-form-label" for="html">
296				<?= I18N::translate('Content') ?>
297			</label>
298			<div class="col-sm-9">
299				<p>
300					<?= 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.') ?>
301				</p>
302			</div>
303		</div>
304
305		<div class="row form-group">
306			<textarea name="html" id="html" class="html-edit" rows="10"><?= e($html) ?></textarea>
307		</div>
308
309		<fieldset class="form-group">
310			<div class="row">
311				<legend class="form-control-legend col-sm-3">
312					<?= I18N::translate('Show the date and time of update') ?>
313				</legend>
314				<div class="col-sm-9">
315					<?= Bootstrap4::radioButtons('show_timestamp', FunctionsEdit::optionsNoYes(), $show_timestamp, true) ?>
316				</div>
317			</div>
318		</fieldset>
319
320		<fieldset class="form-group">
321			<div class="row">
322				<legend class="form-control-legend col-sm-3">
323					<?= I18N::translate('Show this block for which languages') ?>
324				</legend>
325				<div class="col-sm-9">
326					<?= FunctionsEdit::editLanguageCheckboxes('lang', $languages) ?>
327				</div>
328			</div>
329		</fieldset>
330
331		<?php
332	}
333}
334