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