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