xref: /webtrees/app/Module/RecentChangesModule.php (revision 369c0ce6d43eee62858778711fa4744ed347814a)
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\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	/**
44	 * Generate the HTML content of this block.
45	 *
46	 * @param int      $block_id
47	 * @param bool     $template
48	 * @param string[] $cfg
49	 *
50	 * @return string
51	 */
52	public function getBlock($block_id, $template = true, $cfg = array()) {
53		global $ctype, $WT_TREE;
54
55		$days       = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
56		$infoStyle  = $this->getBlockSetting($block_id, 'infoStyle', 'table');
57		$sortStyle  = $this->getBlockSetting($block_id, 'sortStyle', 'date_desc');
58		$block      = $this->getBlockSetting($block_id, 'block', '1');
59		$hide_empty = $this->getBlockSetting($block_id, 'hide_empty', '0');
60
61		foreach (array('days', 'infoStyle', 'sortStyle', 'hide_empty', 'block') as $name) {
62			if (array_key_exists($name, $cfg)) {
63				$$name = $cfg[$name];
64			}
65		}
66
67		$found_facts = FunctionsDb::getRecentChanges(WT_CLIENT_JD - $days);
68
69		if (!$found_facts && $hide_empty) {
70			return '';
71		}
72		// Print block header
73		$id    = $this->getName() . $block_id;
74		$class = $this->getName() . '_block';
75		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
76			$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>';
77		} else {
78			$title = '';
79		}
80		$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));
81
82		$content = '';
83		// Print block content
84		if (count($found_facts) == 0) {
85			$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));
86		} else {
87			ob_start();
88			switch ($infoStyle) {
89			case 'list':
90				$content .= FunctionsPrintLists::changesList($found_facts, $sortStyle);
91				break;
92			case 'table':
93				// sortable table
94				$content .= FunctionsPrintLists::changesTable($found_facts, $sortStyle);
95				break;
96			}
97			$content .= ob_get_clean();
98		}
99
100		if ($template) {
101			if ($block) {
102				$class .= ' small_inner_block';
103			}
104
105			return Theme::theme()->formatBlock($id, $title, $class, $content);
106		} else {
107			return $content;
108		}
109	}
110
111	/** {@inheritdoc} */
112	public function loadAjax() {
113		return true;
114	}
115
116	/** {@inheritdoc} */
117	public function isUserBlock() {
118		return true;
119	}
120
121	/** {@inheritdoc} */
122	public function isGedcomBlock() {
123		return true;
124	}
125
126	/**
127	 * An HTML form to edit block settings
128	 *
129	 * @param int $block_id
130	 */
131	public function configureBlock($block_id) {
132		if (Filter::postBool('save') && Filter::checkCsrf()) {
133			$this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, self::MAX_DAYS, self::DEFAULT_DAYS));
134			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
135			$this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'name|date_asc|date_desc', 'date_desc'));
136			$this->setBlockSetting($block_id, 'hide_empty', Filter::postBool('hide_empty'));
137			$this->setBlockSetting($block_id, 'block', Filter::postBool('block'));
138		}
139
140		$days       = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
141		$infoStyle  = $this->getBlockSetting($block_id, 'infoStyle', 'table');
142		$sortStyle  = $this->getBlockSetting($block_id, 'sortStyle', 'date_desc');
143		$block      = $this->getBlockSetting($block_id, 'block', '1');
144		$hide_empty = $this->getBlockSetting($block_id, 'hide_empty', '0');
145
146		echo '<tr><td class="descriptionbox wrap width33">';
147		echo I18N::translate('Number of days to show');
148		echo '</td><td class="optionbox">';
149		echo '<input type="text" name="days" size="2" value="', $days, '">';
150		echo ' <em>', I18N::plural('maximum %s day', 'maximum %s days', I18N::number(self::MAX_DAYS), I18N::number(self::MAX_DAYS)), '</em>';
151		echo '</td></tr>';
152
153		echo '<tr><td class="descriptionbox wrap width33">';
154		echo I18N::translate('Presentation style');
155		echo '</td><td class="optionbox">';
156		echo FunctionsEdit::selectEditControl('infoStyle', array('list' => I18N::translate('list'), 'table' => I18N::translate('table')), null, $infoStyle, '');
157		echo '</td></tr>';
158
159		echo '<tr><td class="descriptionbox wrap width33">';
160		echo I18N::translate('Sort order');
161		echo '</td><td class="optionbox">';
162		echo FunctionsEdit::selectEditControl('sortStyle', array(
163			'name'      => /* I18N: An option in a list-box */ I18N::translate('sort by name'),
164			'date_asc'  => /* I18N: An option in a list-box */ I18N::translate('sort by date, oldest first'),
165			'date_desc' => /* I18N: An option in a list-box */ I18N::translate('sort by date, newest first'),
166		), null, $sortStyle, '');
167		echo '</td></tr>';
168
169		echo '<tr><td class="descriptionbox wrap width33">';
170		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
171		echo '</td><td class="optionbox">';
172		echo FunctionsEdit::editFieldYesNo('block', $block);
173		echo '</td></tr>';
174
175		echo '<tr><td class="descriptionbox wrap width33">';
176		echo I18N::translate('Should this block be hidden when it is empty?');
177		echo '</td><td class="optionbox">';
178		echo FunctionsEdit::editFieldYesNo('hide_empty', $hide_empty);
179		echo '</td></tr>';
180		echo '<tr><td colspan="2" class="optionbox wrap">';
181		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>';
182		echo '</td></tr>';
183	}
184
185}
186