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