xref: /webtrees/app/Module/RecentChangesModule.php (revision 911f5683b68340b4bad2bbca6e1ab7a3a15f46a7)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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\GedcomRecord;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Individual;
24use Fisharebest\Webtrees\Tree;
25
26/**
27 * Class RecentChangesModule
28 */
29class RecentChangesModule extends AbstractModule implements ModuleBlockInterface {
30	const DEFAULT_BLOCK      = '1';
31	const DEFAULT_DAYS       = 7;
32	const DEFAULT_HIDE_EMPTY = '0';
33	const DEFAULT_SHOW_USER  = '1';
34	const DEFAULT_SORT_STYLE = 'date_desc';
35	const DEFAULT_INFO_STYLE = 'table';
36	const MAX_DAYS           = 90;
37
38	/** {@inheritdoc} */
39	public function getTitle() {
40		return /* I18N: Name of a module */ I18N::translate('Recent changes');
41	}
42
43	/** {@inheritdoc} */
44	public function getDescription() {
45		return /* I18N: Description of the “Recent changes” module */ I18N::translate('A list of records that have been updated recently.');
46	}
47
48	/** {@inheritdoc} */
49	public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string {
50		global $ctype;
51
52		$days      = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
53		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE);
54		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE);
55		$show_user = (bool) $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER);
56
57		extract($cfg, EXTR_OVERWRITE);
58
59		$records = $this->getRecentChanges($tree, $days);
60
61		switch ($sortStyle) {
62			case 'name':
63				uasort($records, ['self', 'sortByNameAndChangeDate']);
64				break;
65			case 'date_asc':
66				uasort($records, ['self', 'sortByChangeDateAndName']);
67				$records = array_reverse($records);
68				break;
69			case 'date_desc':
70				uasort($records, ['self', 'sortByChangeDateAndName']);
71		}
72
73		if (empty($records)) {
74			$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));
75		} elseif ($infoStyle === 'list') {
76			$content = $this->changesList($records, $show_user);
77		} else {
78			$content = view('blocks/changes-' . $infoStyle, [
79				'records'   => $records,
80				'show_user' => $show_user,
81			]);
82		}
83
84		if ($template) {
85			if ($ctype === 'gedcom' && Auth::isManager($tree)) {
86				$config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]);
87			} elseif ($ctype === 'user' && Auth::check()) {
88				$config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]);
89			} else {
90				$config_url = '';
91			}
92
93			return view('blocks/template', [
94				'block'      => str_replace('_', '-', $this->getName()),
95				'id'         => $block_id,
96				'config_url' => $config_url,
97				'title'      => I18N::plural('Changes in the last %s day', 'Changes in the last %s days', $days, I18N::number($days)),
98				'content'    => $content,
99			]);
100		} else {
101			return $content;
102		}
103	}
104
105	/** {@inheritdoc} */
106	public function loadAjax(): bool {
107		return true;
108	}
109
110	/** {@inheritdoc} */
111	public function isUserBlock(): bool {
112		return true;
113	}
114
115	/** {@inheritdoc} */
116	public function isGedcomBlock(): bool {
117		return true;
118	}
119
120	/** {@inheritdoc} */
121	public function configureBlock(Tree $tree, int $block_id) {
122		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
123			$this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, self::MAX_DAYS));
124			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table'));
125			$this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'name|date_asc|date_desc'));
126			$this->setBlockSetting($block_id, 'show_user', Filter::postBool('show_user'));
127
128			return;
129		}
130
131		$days      = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
132		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE);
133		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE);
134		$show_user = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER);
135
136		$info_styles = [
137			'list'  => /* I18N: An option in a list-box */ I18N::translate('list'),
138			'table' => /* I18N: An option in a list-box */ I18N::translate('table'),
139		];
140
141		$sort_styles = [
142			'name'      => /* I18N: An option in a list-box */ I18N::translate('sort by name'),
143			'date_asc'  => /* I18N: An option in a list-box */ I18N::translate('sort by date, oldest first'),
144			'date_desc' => /* I18N: An option in a list-box */ I18N::translate('sort by date, newest first'),
145		];
146
147		echo view('blocks/recent-changes-config', [
148			'days'        => $days,
149			'infoStyle'   => $infoStyle,
150			'info_styles' => $info_styles,
151			'max_days'    => self::MAX_DAYS,
152			'sortStyle'   => $sortStyle,
153			'sort_styles' => $sort_styles,
154			'show_user'   => $show_user,
155		]);
156	}
157
158	/**
159	 * Find records that have changed since a given julian day
160	 *
161	 * @param Tree $tree Changes for which tree
162	 * @param int  $days Number of days
163	 *
164	 * @return GedcomRecord[] List of records with changes
165	 */
166	private function getRecentChanges(Tree $tree, $days) {
167		$sql =
168			"SELECT xref FROM `##change`" .
169			" WHERE new_gedcom != '' AND change_time > DATE_SUB(NOW(), INTERVAL :days DAY) AND gedcom_id = :tree_id" .
170			" GROUP BY xref" .
171			" ORDER BY MAX(change_id) DESC";
172
173		$vars = [
174			'days'    => $days,
175			'tree_id' => $tree->getTreeId(),
176		];
177
178		$xrefs = Database::prepare($sql)->execute($vars)->fetchOneColumn();
179
180		$records = [];
181		foreach ($xrefs as $xref) {
182			$record = GedcomRecord::getInstance($xref, $tree);
183			if ($record && $record->canShow()) {
184				$records[] = $record;
185			}
186		}
187
188		return $records;
189	}
190
191	/**
192	 * Format a table of events
193	 *
194	 * @param GedcomRecord[] $records
195	 * @param bool           $show_user
196	 *
197	 * @return string
198	 */
199	private function changesList(array $records, $show_user) {
200		$html = '';
201		foreach ($records as $record) {
202			$html .= '<a href="' . e($record->url()) . '" class="list_item name2">' . $record->getFullName() . '</a>';
203			$html .= '<div class="indent" style="margin-bottom: 5px;">';
204			if ($record instanceof Individual) {
205				if ($record->getAddName()) {
206					$html .= '<a href="' . e($record->url()) . '" class="list_item">' . $record->getAddName() . '</a>';
207				}
208			}
209
210			// The timestamp may be missing or private.
211			$timestamp = $record->lastChangeTimestamp();
212			if ($timestamp !== '') {
213				if ($show_user) {
214					$html .= /* I18N: [a record was] Changed on <date/time> by <user> */
215						I18N::translate('Changed on %1$s by %2$s', $timestamp, e($record->lastChangeUser()));
216				} else {
217					$html .= /* I18N: [a record was] Changed on <date/time> */
218						I18N::translate('Changed on %1$s', $timestamp);
219				}
220			}
221			$html .= '</div>';
222		}
223
224		return $html;
225	}
226
227	/**
228	 * Sort the records by (1) last change date and (2) name
229	 *
230	 * @param GedcomRecord $a
231	 * @param GedcomRecord $b
232	 *
233	 * @return int
234	 */
235	private static function sortByChangeDateAndName(GedcomRecord $a, GedcomRecord $b) {
236		return $b->lastChangeTimestamp(true) - $a->lastChangeTimestamp(true) ?: GedcomRecord::compare($a, $b);
237	}
238
239	/**
240	 * Sort the records by (1) name and (2) last change date
241	 *
242	 * @param GedcomRecord $a
243	 * @param GedcomRecord $b
244	 *
245	 * @return int
246	 */
247	private static function sortByNameAndChangeDate(GedcomRecord $a, GedcomRecord $b) {
248		return GedcomRecord::compare($a, $b) ?: $b->lastChangeTimestamp(true) - $a->lastChangeTimestamp(true);
249	}
250}
251