xref: /webtrees/app/Module/RecentChangesModule.php (revision 73df4c545c87012519818766e1a8eb02272a59b5)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2016 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\Database;
20use Fisharebest\Webtrees\Filter;
21use Fisharebest\Webtrees\Functions\FunctionsEdit;
22use Fisharebest\Webtrees\GedcomRecord;
23use Fisharebest\Webtrees\GedcomTag;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Individual;
26use Fisharebest\Webtrees\Theme;
27use Fisharebest\Webtrees\Tree;
28use Rhumsaa\Uuid\Uuid;
29
30/**
31 * Class RecentChangesModule
32 */
33class RecentChangesModule extends AbstractModule implements ModuleBlockInterface {
34	const DEFAULT_BLOCK      = '1';
35	const DEFAULT_DAYS       = 7;
36	const DEFAULT_HIDE_EMPTY = '0';
37	const DEFAULT_SHOW_USER  = '1';
38	const DEFAULT_SORT_STYLE = 'date_desc';
39	const DEFAULT_INFO_STYLE = 'table';
40	const MAX_DAYS           = 90;
41
42	/** {@inheritdoc} */
43	public function getTitle() {
44		return /* I18N: Name of a module */ I18N::translate('Recent changes');
45	}
46
47	/** {@inheritdoc} */
48	public function getDescription() {
49		return /* I18N: Description of the “Recent changes” module */ I18N::translate('A list of records that have been updated recently.');
50	}
51
52	/** {@inheritdoc} */
53	public function getBlock($block_id, $template = true, $cfg = array()) {
54		global $ctype, $WT_TREE;
55
56		$days       = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
57		$infoStyle  = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE);
58		$sortStyle  = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE);
59		$show_user  = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER);
60		$block      = $this->getBlockSetting($block_id, 'block', self::DEFAULT_BLOCK);
61		$hide_empty = $this->getBlockSetting($block_id, 'hide_empty', self::DEFAULT_HIDE_EMPTY);
62
63		foreach (array('days', 'infoStyle', 'sortStyle', 'hide_empty', 'show_user', 'block') as $name) {
64			if (array_key_exists($name, $cfg)) {
65				$$name = $cfg[$name];
66			}
67		}
68
69		$records = $this->getRecentChanges($WT_TREE, WT_CLIENT_JD - $days);
70
71		if (empty($records) && $hide_empty) {
72			return '';
73		}
74
75		// Print block header
76		$id    = $this->getName() . $block_id;
77		$class = $this->getName() . '_block';
78
79		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
80			$title = '<a class="icon-admin" title="' . I18N::translate('Preferences') . '" href="block_edit.php?block_id=' . $block_id . '&amp;ged=' . $WT_TREE->getNameHtml() . '&amp;ctype=' . $ctype . '"></a>';
81		} else {
82			$title = '';
83		}
84		$title .= /* I18N: title for list of recent changes */ I18N::plural('Changes in the last %s day', 'Changes in the last %s days', $days, I18N::number($days));
85
86		$content = '';
87		// Print block content
88		if (count($records) == 0) {
89			$content .= I18N::plural('There have been no changes within the last %s day.', 'There have been no changes within the last %s days.', $days, I18N::number($days));
90		} else {
91			switch ($infoStyle) {
92			case 'list':
93				$content .= $this->changesList($records, $sortStyle, $show_user);
94				break;
95			case 'table':
96				$content .= $this->changesTable($records, $sortStyle, $show_user);
97				break;
98			}
99		}
100
101		if ($template) {
102			if ($block) {
103				$class .= ' small_inner_block';
104			}
105
106			return Theme::theme()->formatBlock($id, $title, $class, $content);
107		} else {
108			return $content;
109		}
110	}
111
112	/** {@inheritdoc} */
113	public function loadAjax() {
114		return true;
115	}
116
117	/** {@inheritdoc} */
118	public function isUserBlock() {
119		return true;
120	}
121
122	/** {@inheritdoc} */
123	public function isGedcomBlock() {
124		return true;
125	}
126
127	/** {@inheritdoc} */
128	public function configureBlock($block_id) {
129		if (Filter::postBool('save') && Filter::checkCsrf()) {
130			$this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, self::MAX_DAYS));
131			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table'));
132			$this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'name|date_asc|date_desc'));
133			$this->setBlockSetting($block_id, 'show_user', Filter::postBool('show_user'));
134			$this->setBlockSetting($block_id, 'hide_empty', Filter::postBool('hide_empty'));
135			$this->setBlockSetting($block_id, 'block', Filter::postBool('block'));
136		}
137
138		$days       = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
139		$infoStyle  = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE);
140		$sortStyle  = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE);
141		$show_user  = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER);
142		$block      = $this->getBlockSetting($block_id, 'block', self::DEFAULT_BLOCK);
143		$hide_empty = $this->getBlockSetting($block_id, 'hide_empty', self::DEFAULT_HIDE_EMPTY);
144
145		echo '<tr><td class="descriptionbox wrap width33">';
146		echo I18N::translate('Number of days to show');
147		echo '</td><td class="optionbox">';
148		echo '<input type="text" name="days" size="2" value="', $days, '">';
149		echo ' <em>', I18N::plural('maximum %s day', 'maximum %s days', I18N::number(self::MAX_DAYS), I18N::number(self::MAX_DAYS)), '</em>';
150		echo '</td></tr>';
151
152		echo '<tr><td class="descriptionbox wrap width33">';
153		echo I18N::translate('Presentation style');
154		echo '</td><td class="optionbox">';
155		echo FunctionsEdit::selectEditControl('infoStyle', array('list' => I18N::translate('list'), 'table' => I18N::translate('table')), null, $infoStyle, '');
156		echo '</td></tr>';
157
158		echo '<tr><td class="descriptionbox wrap width33">';
159		echo I18N::translate('Sort order');
160		echo '</td><td class="optionbox">';
161		echo FunctionsEdit::selectEditControl('sortStyle', array(
162			'name'      => /* I18N: An option in a list-box */ I18N::translate('sort by name'),
163			'date_asc'  => /* I18N: An option in a list-box */ I18N::translate('sort by date, oldest first'),
164			'date_desc' => /* I18N: An option in a list-box */ I18N::translate('sort by date, newest first'),
165		), null, $sortStyle, '');
166		echo '</td></tr>';
167
168		echo '<tr><td class="descriptionbox wrap width33">';
169		echo /* I18N: label for a yes/no option */ I18N::translate('Show the user who made the change');
170		echo '</td><td class="optionbox">';
171		echo FunctionsEdit::editFieldYesNo('show_user', $show_user);
172		echo '</td></tr>';
173
174		echo '<tr><td class="descriptionbox wrap width33">';
175		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
176		echo '</td><td class="optionbox">';
177		echo FunctionsEdit::editFieldYesNo('block', $block);
178		echo '</td></tr>';
179
180		echo '<tr><td class="descriptionbox wrap width33">';
181		echo I18N::translate('Should this block be hidden when it is empty');
182		echo '</td><td class="optionbox">';
183		echo FunctionsEdit::editFieldYesNo('hide_empty', $hide_empty);
184		echo '</td></tr>';
185		echo '<tr><td colspan="2" class="optionbox wrap">';
186		echo '<span class="error">', I18N::translate('If you hide an empty block, you will not be able to change its configuration until it becomes visible by no longer being empty.'), '</span>';
187		echo '</td></tr>';
188	}
189
190	/**
191	 * Find records that have changed since a given julian day
192	 *
193	 * @param Tree $tree Changes for which tree
194	 * @param int  $jd   Julian day
195	 *
196	 * @return GedcomRecord[] List of records with changes
197	 */
198	private function getRecentChanges(Tree $tree, $jd) {
199		$sql =
200			"SELECT d_gid FROM `##dates`" .
201			" WHERE d_fact='CHAN' AND d_julianday1 >= :jd AND d_file = :tree_id";
202
203		$vars = array(
204			'jd'      => $jd,
205			'tree_id' => $tree->getTreeId(),
206		);
207
208		$xrefs = Database::prepare($sql)->execute($vars)->fetchOneColumn();
209
210		$records = array();
211		foreach ($xrefs as $xref) {
212			$record = GedcomRecord::getInstance($xref, $tree);
213			if ($record->canShow()) {
214				$records[] = $record;
215			}
216		}
217
218		return $records;
219	}
220
221	/**
222	 * Format a table of events
223	 *
224	 * @param GedcomRecord[] $records
225	 * @param string         $sort
226	 * @param bool           $show_user
227	 *
228	 * @return string
229	 */
230	private function changesList(array $records, $sort, $show_user) {
231		switch ($sort) {
232		case 'name':
233			uasort($records, array('self', 'sortByNameAndChangeDate'));
234			break;
235		case 'date_asc':
236			uasort($records, array('self', 'sortByChangeDateAndName'));
237			$records = array_reverse($records);
238			break;
239		case 'date_desc':
240			uasort($records, array('self', 'sortByChangeDateAndName'));
241		}
242
243		$html = '';
244		foreach ($records as $record) {
245			$html .= '<a href="' . $record->getHtmlUrl() . '" class="list_item name2">' . $record->getFullName() . '</a>';
246			$html .= '<div class="indent" style="margin-bottom: 5px;">';
247			if ($record instanceof Individual) {
248				if ($record->getAddName()) {
249					$html .= '<a href="' . $record->getHtmlUrl() . '" class="list_item">' . $record->getAddName() . '</a>';
250				}
251			}
252			if ($show_user) {
253				$html .= /* I18N: [a record was] Changed on <date/time> by <user> */
254					I18N::translate('Changed on %1$s by %2$s', $record->lastChangeTimestamp(), Filter::escapeHtml($record->lastChangeUser()));
255			} else {
256				$html .= /* I18N: [a record was] Changed on <date/time> */
257					I18N::translate('Changed on %1$s', $record->lastChangeTimestamp());
258			}
259			$html .= '</div>';
260		}
261
262		return $html;
263	}
264
265	/**
266	 * Format a table of events
267	 *
268	 * @param GedcomRecord[] $records
269	 * @param string         $sort
270	 * @param bool           $show_user
271	 *
272	 * @return string
273	 */
274	private function changesTable($records, $sort, $show_user) {
275		global $controller;
276
277		$table_id = 'table-chan-' . Uuid::uuid4(); // lists requires a unique ID in case there are multiple lists per page
278
279		switch ($sort) {
280		case 'name':
281		default:
282			$aaSorting = "[2,'asc'], [4,'desc']";
283			break;
284		case 'date_asc':
285			$aaSorting = "[4,'asc'], [2,'asc']";
286			break;
287		case 'date_desc':
288			$aaSorting = "[4,'desc'], [2,'asc']";
289			break;
290		}
291
292		$html = '';
293		$controller
294			->addExternalJavascript(WT_JQUERY_DATATABLES_JS_URL)
295			->addInlineJavascript('
296				jQuery.fn.dataTableExt.oSort["unicode-asc" ]=function(a,b) {return a.replace(/<[^<]*>/, "").localeCompare(b.replace(/<[^<]*>/, ""))};
297				jQuery.fn.dataTableExt.oSort["unicode-desc"]=function(a,b) {return b.replace(/<[^<]*>/, "").localeCompare(a.replace(/<[^<]*>/, ""))};
298				jQuery("#' . $table_id . '").dataTable({
299					dom: \'t\',
300					paging: false,
301					autoWidth:false,
302					lengthChange: false,
303					filter: false,
304					' . I18N::datatablesI18N() . ',
305					jQueryUI: true,
306					sorting: [' . $aaSorting . '],
307					columns: [
308						/* 0-Type */     { sortable: false, class: "center" },
309						/* 1-Record */   { dataSort: 2 },
310						/* 2-SORTNAME */ { type: "unicode" },
311						/* 3-Change */   { dataSort: 4 },
312						/* 4-DATE */     null
313						' . ($show_user ? ',/* 5-By */      null' : '') . '
314					]
315				});
316			');
317
318		$html .= '<table id="' . $table_id . '" class="width100">';
319		$html .= '<thead><tr>';
320		$html .= '<th></th>';
321		$html .= '<th>' . I18N::translate('Record') . '</th>';
322		$html .= '<th hidden>SORTNAME</th>';
323		$html .= '<th>' . GedcomTag::getLabel('CHAN') . '</th>';
324		$html .= '<th hidden>DATE</th>';
325		if ($show_user) {
326			$html .= '<th>' . GedcomTag::getLabel('_WT_USER') . '</th>';
327		}
328		$html .= '</tr></thead><tbody>';
329
330		foreach ($records as $record) {
331			$html .= '<tr><td>';
332			switch ($record::RECORD_TYPE) {
333			case 'INDI':
334				$icon = $record->getSexImage('small');
335				break;
336			case 'FAM':
337				$icon = '<i class="icon-button_family"></i>';
338				break;
339			case 'OBJE':
340				$icon = '<i class="icon-button_media"></i>';
341				break;
342			case 'NOTE':
343				$icon = '<i class="icon-button_note"></i>';
344				break;
345			case 'SOUR':
346				$icon = '<i class="icon-button_source"></i>';
347				break;
348			case 'REPO':
349				$icon = '<i class="icon-button_repository"></i>';
350				break;
351			default:
352				$icon = '&nbsp;';
353				break;
354			}
355			$html .= '<a href="' . $record->getHtmlUrl() . '">' . $icon . '</a>';
356			$html .= '</td>';
357			$name = $record->getFullName();
358			$html .= '<td class="wrap">';
359			$html .= '<a href="' . $record->getHtmlUrl() . '">' . $name . '</a>';
360			if ($record instanceof Individual) {
361				$addname = $record->getAddName();
362				if ($addname) {
363					$html .= '<div class="indent"><a href="' . $record->getHtmlUrl() . '">' . $addname . '</a></div>';
364				}
365			}
366			$html .= '</td>';
367			$html .= '<td hidden>' . $record->getSortName() . '</td>';
368			$html .= '<td class="wrap">' . $record->lastChangeTimestamp() . '</td>';
369			$html .= '<td hidden>' . $record->lastChangeTimestamp(true) . '</td>';
370			if ($show_user) {
371				$html .= '<td class="wrap">' . Filter::escapeHtml($record->lastChangeUser()) . '</td>';
372			}
373			$html .= '</tr>';
374		}
375
376		$html .= '</tbody></table>';
377
378		return $html;
379	}
380
381	/**
382	 * Sort the records by (1) last change date and (2) name
383	 *
384	 * @param GedcomRecord $a
385	 * @param GedcomRecord $b
386	 *
387	 * @return int
388	 */
389	private static function sortByChangeDateAndName(GedcomRecord $a, GedcomRecord $b) {
390		return $b->lastChangeTimestamp(true) - $a->lastChangeTimestamp(true) ?: GedcomRecord::compare($a, $b);
391	}
392
393	/**
394	 * Sort the records by (1) name and (2) last change date
395	 *
396	 * @param GedcomRecord $a
397	 * @param GedcomRecord $b
398	 *
399	 * @return int
400	 */
401	private static function sortByNameAndChangeDate(GedcomRecord $a, GedcomRecord $b) {
402		return GedcomRecord::compare($a, $b) ?: $b->lastChangeTimestamp(true) - $a->lastChangeTimestamp(true);
403	}
404}
405