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