xref: /webtrees/app/Module/RecentChangesModule.php (revision ce92b752bc949d88e324abf0e2284c2bcf2ded7c)
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
253			// The timestamp may be missing or private.
254			$timestamp = $record->lastChangeTimestamp();
255			if ($timestamp !== '') {
256				if ($show_user) {
257					$html .= /* I18N: [a record was] Changed on <date/time> by <user> */
258						I18N::translate('Changed on %1$s by %2$s', $timestamp, Filter::escapeHtml($record->lastChangeUser()));
259				} else {
260					$html .= /* I18N: [a record was] Changed on <date/time> */
261						I18N::translate('Changed on %1$s', $timestamp);
262				}
263			}
264			$html .= '</div>';
265		}
266
267		return $html;
268	}
269
270	/**
271	 * Format a table of events
272	 *
273	 * @param GedcomRecord[] $records
274	 * @param string         $sort
275	 * @param bool           $show_user
276	 *
277	 * @return string
278	 */
279	private function changesTable($records, $sort, $show_user) {
280		global $controller;
281
282		$table_id = 'table-chan-' . Uuid::uuid4(); // lists requires a unique ID in case there are multiple lists per page
283
284		switch ($sort) {
285		case 'name':
286		default:
287			$aaSorting = "[1,'asc'], [2,'desc']";
288			break;
289		case 'date_asc':
290			$aaSorting = "[2,'asc'], [1,'asc']";
291			break;
292		case 'date_desc':
293			$aaSorting = "[2,'desc'], [1,'asc']";
294			break;
295		}
296
297		$html = '';
298		$controller
299			->addExternalJavascript(WT_JQUERY_DATATABLES_JS_URL)
300			->addInlineJavascript('
301				jQuery("#' . $table_id . '").dataTable({
302					dom: \'t\',
303					paging: false,
304					autoWidth:false,
305					lengthChange: false,
306					filter: false,
307					' . I18N::datatablesI18N() . ',
308					jQueryUI: true,
309					sorting: [' . $aaSorting . '],
310					columns: [
311						{ sortable: false, class: "center" },
312						null,
313						null,
314						{ visible: ' . ($show_user ? 'true' : 'false') . ' }
315					]
316				});
317			');
318
319		$html .= '<table id="' . $table_id . '" class="width100">';
320		$html .= '<thead><tr>';
321		$html .= '<th></th>';
322		$html .= '<th>' . I18N::translate('Record') . '</th>';
323		$html .= '<th>' . GedcomTag::getLabel('CHAN') . '</th>';
324		$html .= '<th>' . GedcomTag::getLabel('_WT_USER') . '</th>';
325		$html .= '</tr></thead><tbody>';
326
327		foreach ($records as $record) {
328			$html .= '<tr><td>';
329			switch ($record::RECORD_TYPE) {
330			case 'INDI':
331				$html .= $record->getSexImage('small');
332				break;
333			case 'FAM':
334				$html .= '<i class="icon-button_family"></i>';
335				break;
336			case 'OBJE':
337				$html .= '<i class="icon-button_media"></i>';
338				break;
339			case 'NOTE':
340				$html .= '<i class="icon-button_note"></i>';
341				break;
342			case 'SOUR':
343				$html .= '<i class="icon-button_source"></i>';
344				break;
345			case 'REPO':
346				$html .= '<i class="icon-button_repository"></i>';
347				break;
348			}
349			$html .= '</td>';
350			$html .= '<td data-sort="' . Filter::escapeHtml($record->getSortName()) . '">';
351			$html .= '<a href="' . $record->getHtmlUrl() . '">' . $record->getFullName() . '</a>';
352			$addname = $record->getAddName();
353			if ($addname) {
354				$html .= '<div class="indent"><a href="' . $record->getHtmlUrl() . '">' . $addname . '</a></div>';
355			}
356			$html .= '</td>';
357			$html .= '<td data-sort="' . $record->lastChangeTimestamp(true) . '">' . $record->lastChangeTimestamp() . '</td>';
358			$html .= '<td>' . Filter::escapeHtml($record->lastChangeUser()) . '</td>';
359			$html .= '</tr>';
360		}
361
362		$html .= '</tbody></table>';
363
364		return $html;
365	}
366
367	/**
368	 * Sort the records by (1) last change date and (2) name
369	 *
370	 * @param GedcomRecord $a
371	 * @param GedcomRecord $b
372	 *
373	 * @return int
374	 */
375	private static function sortByChangeDateAndName(GedcomRecord $a, GedcomRecord $b) {
376		return $b->lastChangeTimestamp(true) - $a->lastChangeTimestamp(true) ?: GedcomRecord::compare($a, $b);
377	}
378
379	/**
380	 * Sort the records by (1) name and (2) last change date
381	 *
382	 * @param GedcomRecord $a
383	 * @param GedcomRecord $b
384	 *
385	 * @return int
386	 */
387	private static function sortByNameAndChangeDate(GedcomRecord $a, GedcomRecord $b) {
388		return GedcomRecord::compare($a, $b) ?: $b->lastChangeTimestamp(true) - $a->lastChangeTimestamp(true);
389	}
390}
391