xref: /webtrees/app/Module/RecentChangesModule.php (revision 3d7a8a4ca809135634f38216b734b15acff479f7)
1<?php
2namespace Fisharebest\Webtrees\Module;
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 */
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Filter;
20use Fisharebest\Webtrees\Functions\FunctionsDb;
21use Fisharebest\Webtrees\Functions\FunctionsEdit;
22use Fisharebest\Webtrees\Functions\FunctionsPrintLists;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Theme;
25
26/**
27 * Class RecentChangesModule
28 */
29class RecentChangesModule extends AbstractModule implements ModuleBlockInterface {
30	const DEFAULT_DAYS = 7;
31	const MAX_DAYS     = 90;
32
33	/** {@inheritdoc} */
34	public function getTitle() {
35		return /* I18N: Name of a module */ I18N::translate('Recent changes');
36	}
37
38	/** {@inheritdoc} */
39	public function getDescription() {
40		return /* I18N: Description of the “Recent changes” module */ I18N::translate('A list of records that have been updated recently.');
41	}
42
43	/** {@inheritdoc} */
44	public function getBlock($block_id, $template = true, $cfg = null) {
45		global $ctype, $WT_TREE;
46
47		$days       = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
48		$infoStyle  = $this->getBlockSetting($block_id, 'infoStyle', 'table');
49		$sortStyle  = $this->getBlockSetting($block_id, 'sortStyle', 'date_desc');
50		$block      = $this->getBlockSetting($block_id, 'block', '1');
51		$hide_empty = $this->getBlockSetting($block_id, 'hide_empty', '0');
52
53		if ($cfg) {
54			foreach (array('days', 'infoStyle', 'sortStyle', 'hide_empty', 'block') as $name) {
55				if (array_key_exists($name, $cfg)) {
56					$$name = $cfg[$name];
57				}
58			}
59		}
60
61		$found_facts = FunctionsDb::getRecentChanges(WT_CLIENT_JD - $days);
62
63		if (!$found_facts && $hide_empty) {
64			return '';
65		}
66		// Print block header
67		$id    = $this->getName() . $block_id;
68		$class = $this->getName() . '_block';
69		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
70			$title = '<a class="icon-admin" title="' . I18N::translate('Configure') . '" href="block_edit.php?block_id=' . $block_id . '&amp;ged=' . $WT_TREE->getNameHtml() . '&amp;ctype=' . $ctype . '"></a>';
71		} else {
72			$title = '';
73		}
74		$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));
75
76		$content = '';
77		// Print block content
78		if (count($found_facts) == 0) {
79			$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));
80		} else {
81			ob_start();
82			switch ($infoStyle) {
83			case 'list':
84				$content .= FunctionsPrintLists::changesList($found_facts, $sortStyle);
85				break;
86			case 'table':
87				// sortable table
88				$content .= FunctionsPrintLists::changesTable($found_facts, $sortStyle);
89				break;
90			}
91			$content .= ob_get_clean();
92		}
93
94		if ($template) {
95			if ($block) {
96				$class .= ' small_inner_block';
97			}
98
99			return Theme::theme()->formatBlock($id, $title, $class, $content);
100		} else {
101			return $content;
102		}
103	}
104
105	/** {@inheritdoc} */
106	public function loadAjax() {
107		return true;
108	}
109
110	/** {@inheritdoc} */
111	public function isUserBlock() {
112		return true;
113	}
114
115	/** {@inheritdoc} */
116	public function isGedcomBlock() {
117		return true;
118	}
119
120	/** {@inheritdoc} */
121	public function configureBlock($block_id) {
122		if (Filter::postBool('save') && Filter::checkCsrf()) {
123			$this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, self::MAX_DAYS, self::DEFAULT_DAYS));
124			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
125			$this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'name|date_asc|date_desc', 'date_desc'));
126			$this->setBlockSetting($block_id, 'hide_empty', Filter::postBool('hide_empty'));
127			$this->setBlockSetting($block_id, 'block', Filter::postBool('block'));
128		}
129
130		$days       = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
131		$infoStyle  = $this->getBlockSetting($block_id, 'infoStyle', 'table');
132		$sortStyle  = $this->getBlockSetting($block_id, 'sortStyle', 'date_desc');
133		$block      = $this->getBlockSetting($block_id, 'block', '1');
134		$hide_empty = $this->getBlockSetting($block_id, 'hide_empty', '0');
135
136		echo '<tr><td class="descriptionbox wrap width33">';
137		echo I18N::translate('Number of days to show');
138		echo '</td><td class="optionbox">';
139		echo '<input type="text" name="days" size="2" value="', $days, '">';
140		echo ' <em>', I18N::plural('maximum %s day', 'maximum %s days', I18N::number(self::MAX_DAYS), I18N::number(self::MAX_DAYS)), '</em>';
141		echo '</td></tr>';
142
143		echo '<tr><td class="descriptionbox wrap width33">';
144		echo I18N::translate('Presentation style');
145		echo '</td><td class="optionbox">';
146		echo FunctionsEdit::selectEditControl('infoStyle', array('list' => I18N::translate('list'), 'table' => I18N::translate('table')), null, $infoStyle, '');
147		echo '</td></tr>';
148
149		echo '<tr><td class="descriptionbox wrap width33">';
150		echo I18N::translate('Sort order');
151		echo '</td><td class="optionbox">';
152		echo FunctionsEdit::selectEditControl('sortStyle', array(
153			'name'      => /* I18N: An option in a list-box */ I18N::translate('sort by name'),
154			'date_asc'  => /* I18N: An option in a list-box */ I18N::translate('sort by date, oldest first'),
155			'date_desc' => /* I18N: An option in a list-box */ I18N::translate('sort by date, newest first'),
156		), null, $sortStyle, '');
157		echo '</td></tr>';
158
159		echo '<tr><td class="descriptionbox wrap width33">';
160		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
161		echo '</td><td class="optionbox">';
162		echo FunctionsEdit::editFieldYesNo('block', $block);
163		echo '</td></tr>';
164
165		echo '<tr><td class="descriptionbox wrap width33">';
166		echo I18N::translate('Should this block be hidden when it is empty?');
167		echo '</td><td class="optionbox">';
168		echo FunctionsEdit::editFieldYesNo('hide_empty', $hide_empty);
169		echo '</td></tr>';
170		echo '<tr><td colspan="2" class="optionbox wrap">';
171		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>';
172		echo '</td></tr>';
173	}
174
175}
176