xref: /webtrees/app/Module/RecentChangesModule.php (revision 309092efc56540a95d8117fad7180fe470aadff6)
1<?php
2namespace Fisharebest\Webtrees;
3
4/**
5 * webtrees: online genealogy
6 * Copyright (C) 2015 webtrees development team
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/**
20 * Class RecentChangesModule
21 */
22class RecentChangesModule extends AbstractModule implements ModuleBlockInterface {
23	const DEFAULT_DAYS = 7;
24	const MAX_DAYS = 90;
25
26	/** {@inheritdoc} */
27	public function getTitle() {
28		return /* I18N: Name of a module */ I18N::translate('Recent changes');
29	}
30
31	/** {@inheritdoc} */
32	public function getDescription() {
33		return /* I18N: Description of the “Recent changes” module */ I18N::translate('A list of records that have been updated recently.');
34	}
35
36	/** {@inheritdoc} */
37	public function getBlock($block_id, $template = true, $cfg = null) {
38		global $ctype, $WT_TREE;
39
40		$days       = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
41		$infoStyle  = $this->getBlockSetting($block_id, 'infoStyle', 'table');
42		$sortStyle  = $this->getBlockSetting($block_id, 'sortStyle', 'date_desc');
43		$block      = $this->getBlockSetting($block_id, 'block', '1');
44		$hide_empty = $this->getBlockSetting($block_id, 'hide_empty', '0');
45
46
47		if ($cfg) {
48			foreach (array('days', 'infoStyle', 'sortStyle', 'hide_empty', 'block') as $name) {
49				if (array_key_exists($name, $cfg)) {
50					$$name = $cfg[$name];
51				}
52			}
53		}
54
55		$found_facts = get_recent_changes(WT_CLIENT_JD - $days);
56
57		if (!$found_facts && $hide_empty) {
58			return '';
59		}
60		// Print block header
61		$id    = $this->getName() . $block_id;
62		$class = $this->getName() . '_block';
63		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
64			$title = '<i class="icon-admin" title="' . I18N::translate('Configure') . '" onclick="modalDialog(\'block_edit.php?block_id=' . $block_id . '\', \'' . $this->getTitle() . '\');"></i>';
65		} else {
66			$title = '';
67		}
68		$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));
69
70		$content = '';
71		// Print block content
72		if (count($found_facts) == 0) {
73			$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));
74		} else {
75			ob_start();
76			switch ($infoStyle) {
77			case 'list':
78				$content .= print_changes_list($found_facts, $sortStyle);
79				break;
80			case 'table':
81				// sortable table
82				$content .= print_changes_table($found_facts, $sortStyle);
83				break;
84			}
85			$content .= ob_get_clean();
86		}
87
88		if ($template) {
89			if ($block) {
90				$class .= ' small_inner_block';
91			}
92			return Theme::theme()->formatBlock($id, $title, $class, $content);
93		} else {
94			return $content;
95		}
96	}
97
98	/** {@inheritdoc} */
99	public function loadAjax() {
100		return true;
101	}
102
103	/** {@inheritdoc} */
104	public function isUserBlock() {
105		return true;
106	}
107
108	/** {@inheritdoc} */
109	public function isGedcomBlock() {
110		return true;
111	}
112
113	/** {@inheritdoc} */
114	public function configureBlock($block_id) {
115		if (Filter::postBool('save') && Filter::checkCsrf()) {
116			$this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, self::MAX_DAYS, self::DEFAULT_DAYS));
117			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
118			$this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'name|date_asc|date_desc', 'date_desc'));
119			$this->setBlockSetting($block_id, 'hide_empty', Filter::postBool('hide_empty'));
120			$this->setBlockSetting($block_id, 'block', Filter::postBool('block'));
121		}
122
123		$days       = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
124		$infoStyle  = $this->getBlockSetting($block_id, 'infoStyle', 'table');
125		$sortStyle  = $this->getBlockSetting($block_id, 'sortStyle', 'date_desc');
126		$block      = $this->getBlockSetting($block_id, 'block', '1');
127		$hide_empty = $this->getBlockSetting($block_id, 'hide_empty', '0');
128
129		echo '<tr><td class="descriptionbox wrap width33">';
130		echo I18N::translate('Number of days to show');
131		echo '</td><td class="optionbox">';
132		echo '<input type="text" name="days" size="2" value="', $days, '">';
133		echo ' <em>', I18N::plural('maximum %s day', 'maximum %s days', I18N::number(self::MAX_DAYS), I18N::number(self::MAX_DAYS)), '</em>';
134		echo '</td></tr>';
135
136		echo '<tr><td class="descriptionbox wrap width33">';
137		echo I18N::translate('Presentation style');
138		echo '</td><td class="optionbox">';
139		echo select_edit_control('infoStyle', array('list' => I18N::translate('list'), 'table' => I18N::translate('table')), null, $infoStyle, '');
140		echo '</td></tr>';
141
142		echo '<tr><td class="descriptionbox wrap width33">';
143		echo I18N::translate('Sort order');
144		echo '</td><td class="optionbox">';
145		echo select_edit_control('sortStyle', array(
146			'name'      => /* I18N: An option in a list-box */ I18N::translate('sort by name'),
147			'date_asc'  => /* I18N: An option in a list-box */ I18N::translate('sort by date, oldest first'),
148			'date_desc' => /* I18N: An option in a list-box */ I18N::translate('sort by date, newest first')
149		), null, $sortStyle, '');
150		echo '</td></tr>';
151
152		echo '<tr><td class="descriptionbox wrap width33">';
153		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
154		echo '</td><td class="optionbox">';
155		echo edit_field_yes_no('block', $block);
156		echo '</td></tr>';
157
158		echo '<tr><td class="descriptionbox wrap width33">';
159		echo I18N::translate('Should this block be hidden when it is empty?');
160		echo '</td><td class="optionbox">';
161		echo edit_field_yes_no('hide_empty', $hide_empty);
162		echo '</td></tr>';
163		echo '<tr><td colspan="2" class="optionbox wrap">';
164		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>';
165		echo '</td></tr>';
166	}
167
168}
169