xref: /webtrees/app/Module/HtmlBlockModule.php (revision ba5cd25e4897e5ca3268036ab8a43da7979eff40)
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 = []) {
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) {
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() {
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') => '#getAllTagsTable#',
158
159			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#.'),
160
161			I18N::translate('Statistics') => '<div class="gedcom_stats">
162				<span style="font-weight: bold;"><a href="index.php?command=gedcom">#gedcomTitle#</a></span><br>
163				' . I18N::translate('This family tree was last updated on %s.', '#gedcomUpdated#') . '
164					<div class="row">
165						<div class="col col-sm-4">
166							<table class="table wt-facts-table">
167								<tr>
168									<th scope="row">' . I18N::translate('Individuals') . '</th>
169									<td>#totalIndividuals#</td>
170								</tr>
171								<tr>
172									<th scope="row">' . I18N::translate('Males') . '</th>
173									<td>#totalSexMales#<br>#totalSexMalesPercentage#</td>
174								</tr>
175								<tr>
176									<th scope="row">' . I18N::translate('Females') . '</th>
177									<td>#totalSexFemales#<br>#totalSexFemalesPercentage#</td>
178								</tr>
179								<tr>
180									<th scope="row">' . I18N::translate('Total surnames') . '</th>
181									<td>#totalSurnames#</td>
182								</tr>
183								<tr>
184									<th scope="row">' . I18N::translate('Families') . '</th>
185									<td>#totalFamilies#</td>
186								</tr>
187								<tr>
188									<th scope="row">' . I18N::translate('Sources') . '</th>
189									<td>#totalSources#</td>
190								</tr>
191								<tr>
192									<th scope="row">' . I18N::translate('Media objects') . '</th>
193									<td>#totalMedia#</td>
194								</tr>
195								<tr>
196									<th scope="row">' . I18N::translate('Repositories') . '</th>
197									<td>#totalRepositories#</td>
198								</tr>
199								<tr>
200									<th scope="row">' . I18N::translate('Events') . '</th>
201									<td>#totalEvents#</td>
202								</tr>
203								<tr>
204									<th scope="row">' . I18N::translate('Users') . '</th>
205									<td>#totalUsers#</td>
206								</tr>
207							</table>
208						</div>
209
210						<div class="col col-sm-8">
211							<table class="table wt-facts-table">
212								<tr>
213									<th scope="row">' . I18N::translate('Earliest birth') . '</th>
214									<td>#firstBirth#</td>
215								</tr>
216								<tr>
217									<th scope="row">' . I18N::translate('Latest birth') . '</th>
218									<td>#lastBirth#</td>
219								</tr>
220								<tr>
221									<th scope="row">' . I18N::translate('Earliest death') . '</th>
222									<td>#firstDeath#</td>
223								</tr>
224								<tr>
225									<th scope="row">' . I18N::translate('Latest death') . '</th>
226									<td>#lastDeath#</td>
227								</tr>
228								<tr>
229									<th scope="row">' . I18N::translate('Individual who lived the longest') . '</th>
230									<td>#longestLife#</td>
231								</tr>
232								<tr>
233									<th scope="row">' . I18N::translate('Average age at death') . '</th>
234									<td>#averageLifespan#</td>
235								</tr>
236								<tr>
237									<th scope="row">' . I18N::translate('Family with the most children') . '</th>
238									<td>#largestFamilySize#<br>#largestFamily#</td>
239								</tr>
240								<tr>
241									<th scope="row">' . I18N::translate('Average number of children per family') . '</th>
242									<td>#averageChildren#</td>
243								</tr>
244							</table>
245						</div>
246					</div>
247					<br>
248					<span style="font-weight: bold;">' . I18N::translate('Most common surnames') . '</span>
249					<br>
250					#commonSurnames#
251				</div>',
252		];
253
254		$title          = $this->getBlockSetting($block_id, 'title');
255		$html           = $this->getBlockSetting($block_id, 'html');
256		$gedcom         = $this->getBlockSetting($block_id, 'gedcom', '__current__');
257		$show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
258		$languages      = explode(',', $this->getBlockSetting($block_id, 'languages'));
259
260		?>
261		<div class="row form-group">
262			<label class="col-sm-3 col-form-label" for="title">
263				<?= I18N::translate('Title') ?>
264			</label>
265			<div class="col-sm-9">
266				<input class="form-control" type="text" id="title" name="title" value="<?= Html::escape($title) ?>">
267			</div>
268		</div>
269
270		<div class="row form-group">
271			<label class="col-sm-3 col-form-label" for="template">
272				<?= I18N::translate('Templates') ?>
273			</label>
274			<div class="col-sm-9">
275				<?= 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']) ?>
276				<p class="small text-muted">
277					<?= 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.') ?>
278				</p>
279			</div>
280		</div>
281
282		<div class="row form-group">
283			<label class="col-sm-3 col-form-label" for="gedcom">
284				<?= I18N::translate('Family tree') ?>
285			</label>
286			<div class="col-sm-9">
287				<?= Bootstrap4::select(['__current__' => I18N::translate('Current'), '__default__' => I18N::translate('Default')] + Tree::getNameList(), $gedcom, ['id' => 'gedcom', 'name' => 'gedcom']) ?>
288			</div>
289		</div>
290
291		<div class="row form-group">
292			<label class="col-sm-3 col-form-label" for="html">
293				<?= I18N::translate('Content') ?>
294			</label>
295			<div class="col-sm-9">
296				<p>
297					<?= 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.') ?>
298				</p>
299			</div>
300		</div>
301
302		<div class="row form-group">
303			<textarea name="html" id="html" class="html-edit" rows="10"><?= Html::escape($html) ?></textarea>
304		</div>
305
306		<fieldset class="form-group">
307			<div class="row">
308				<legend class="form-control-legend col-sm-3">
309					<?= I18N::translate('Show the date and time of update') ?>
310				</legend>
311				<div class="col-sm-9">
312					<?= Bootstrap4::radioButtons('show_timestamp', FunctionsEdit::optionsNoYes(), $show_timestamp, true) ?>
313				</div>
314			</div>
315		</fieldset>
316
317		<fieldset class="form-group">
318			<div class="row">
319				<legend class="form-control-legend col-sm-3">
320					<?= I18N::translate('Show this block for which languages') ?>
321				</legend>
322				<div class="col-sm-9">
323					<?= FunctionsEdit::editLanguageCheckboxes('lang', $languages) ?>
324				</div>
325			</div>
326		</fieldset>
327
328		<?php
329	}
330}
331