xref: /webtrees/app/Module/RecentChangesModule.php (revision 782f6af03e3244928e45d08eb52f2084a2d7072a)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
4369c0ce6SGreg Roach * Copyright (C) 2016 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
203d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDb;
213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsEdit;
223d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintLists;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Theme;
258c2e8227SGreg Roach
268c2e8227SGreg Roach/**
278c2e8227SGreg Roach * Class RecentChangesModule
288c2e8227SGreg Roach */
29e2a378d3SGreg Roachclass RecentChangesModule extends AbstractModule implements ModuleBlockInterface {
308c2e8227SGreg Roach	const DEFAULT_DAYS = 7;
318c2e8227SGreg Roach	const MAX_DAYS     = 90;
328c2e8227SGreg Roach
338c2e8227SGreg Roach	/** {@inheritdoc} */
348c2e8227SGreg Roach	public function getTitle() {
358c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('Recent changes');
368c2e8227SGreg Roach	}
378c2e8227SGreg Roach
388c2e8227SGreg Roach	/** {@inheritdoc} */
398c2e8227SGreg Roach	public function getDescription() {
408c2e8227SGreg Roach		return /* I18N: Description of the “Recent changes” module */ I18N::translate('A list of records that have been updated recently.');
418c2e8227SGreg Roach	}
428c2e8227SGreg Roach
4376692c8bSGreg Roach	/**
4476692c8bSGreg Roach	 * Generate the HTML content of this block.
4576692c8bSGreg Roach	 *
4676692c8bSGreg Roach	 * @param int      $block_id
4776692c8bSGreg Roach	 * @param bool     $template
48727f238cSGreg Roach	 * @param string[] $cfg
4976692c8bSGreg Roach	 *
5076692c8bSGreg Roach	 * @return string
5176692c8bSGreg Roach	 */
5276692c8bSGreg Roach	public function getBlock($block_id, $template = true, $cfg = array()) {
534b9ff166SGreg Roach		global $ctype, $WT_TREE;
548c2e8227SGreg Roach
55e2a378d3SGreg Roach		$days       = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
56e2a378d3SGreg Roach		$infoStyle  = $this->getBlockSetting($block_id, 'infoStyle', 'table');
57e2a378d3SGreg Roach		$sortStyle  = $this->getBlockSetting($block_id, 'sortStyle', 'date_desc');
58*782f6af0SGreg Roach		$show_user  = $this->getBlockSetting($block_id, 'show_user', '1');
59e2a378d3SGreg Roach		$block      = $this->getBlockSetting($block_id, 'block', '1');
60e2a378d3SGreg Roach		$hide_empty = $this->getBlockSetting($block_id, 'hide_empty', '0');
618c2e8227SGreg Roach
628c2e8227SGreg Roach		foreach (array('days', 'infoStyle', 'sortStyle', 'hide_empty', 'block') as $name) {
638c2e8227SGreg Roach			if (array_key_exists($name, $cfg)) {
648c2e8227SGreg Roach				$$name = $cfg[$name];
658c2e8227SGreg Roach			}
668c2e8227SGreg Roach		}
678c2e8227SGreg Roach
683d7a8a4cSGreg Roach		$found_facts = FunctionsDb::getRecentChanges(WT_CLIENT_JD - $days);
698c2e8227SGreg Roach
708c2e8227SGreg Roach		if (!$found_facts && $hide_empty) {
718c2e8227SGreg Roach			return '';
728c2e8227SGreg Roach		}
738c2e8227SGreg Roach		// Print block header
748c2e8227SGreg Roach		$id    = $this->getName() . $block_id;
758c2e8227SGreg Roach		$class = $this->getName() . '_block';
764b9ff166SGreg Roach		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
779353052eSGreg Roach			$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>';
788c2e8227SGreg Roach		} else {
798c2e8227SGreg Roach			$title = '';
808c2e8227SGreg Roach		}
818c2e8227SGreg Roach		$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));
828c2e8227SGreg Roach
838c2e8227SGreg Roach		$content = '';
848c2e8227SGreg Roach		// Print block content
858c2e8227SGreg Roach		if (count($found_facts) == 0) {
868c2e8227SGreg Roach			$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));
878c2e8227SGreg Roach		} else {
888c2e8227SGreg Roach			ob_start();
898c2e8227SGreg Roach			switch ($infoStyle) {
908c2e8227SGreg Roach			case 'list':
91*782f6af0SGreg Roach				$content .= FunctionsPrintLists::changesList($found_facts, $sortStyle, $show_user);
928c2e8227SGreg Roach				break;
938c2e8227SGreg Roach			case 'table':
948c2e8227SGreg Roach				// sortable table
95*782f6af0SGreg Roach				$content .= FunctionsPrintLists::changesTable($found_facts, $sortStyle, $show_user);
968c2e8227SGreg Roach				break;
978c2e8227SGreg Roach			}
988c2e8227SGreg Roach			$content .= ob_get_clean();
998c2e8227SGreg Roach		}
1008c2e8227SGreg Roach
1018c2e8227SGreg Roach		if ($template) {
1028c2e8227SGreg Roach			if ($block) {
1038c2e8227SGreg Roach				$class .= ' small_inner_block';
1048c2e8227SGreg Roach			}
105cbc1590aSGreg Roach
1068c2e8227SGreg Roach			return Theme::theme()->formatBlock($id, $title, $class, $content);
1078c2e8227SGreg Roach		} else {
1088c2e8227SGreg Roach			return $content;
1098c2e8227SGreg Roach		}
1108c2e8227SGreg Roach	}
1118c2e8227SGreg Roach
1128c2e8227SGreg Roach	/** {@inheritdoc} */
1138c2e8227SGreg Roach	public function loadAjax() {
1148c2e8227SGreg Roach		return true;
1158c2e8227SGreg Roach	}
1168c2e8227SGreg Roach
1178c2e8227SGreg Roach	/** {@inheritdoc} */
1188c2e8227SGreg Roach	public function isUserBlock() {
1198c2e8227SGreg Roach		return true;
1208c2e8227SGreg Roach	}
1218c2e8227SGreg Roach
1228c2e8227SGreg Roach	/** {@inheritdoc} */
1238c2e8227SGreg Roach	public function isGedcomBlock() {
1248c2e8227SGreg Roach		return true;
1258c2e8227SGreg Roach	}
1268c2e8227SGreg Roach
12776692c8bSGreg Roach	/**
12876692c8bSGreg Roach	 * An HTML form to edit block settings
12976692c8bSGreg Roach	 *
13076692c8bSGreg Roach	 * @param int $block_id
13176692c8bSGreg Roach	 */
1328c2e8227SGreg Roach	public function configureBlock($block_id) {
1338c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
134e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, self::MAX_DAYS, self::DEFAULT_DAYS));
135e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
136e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'name|date_asc|date_desc', 'date_desc'));
137*782f6af0SGreg Roach			$this->setBlockSetting($block_id, 'show_user', Filter::postBool('show_user'));
138e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'hide_empty', Filter::postBool('hide_empty'));
139e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'block', Filter::postBool('block'));
1408c2e8227SGreg Roach		}
1418c2e8227SGreg Roach
142e2a378d3SGreg Roach		$days       = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
143e2a378d3SGreg Roach		$infoStyle  = $this->getBlockSetting($block_id, 'infoStyle', 'table');
144e2a378d3SGreg Roach		$sortStyle  = $this->getBlockSetting($block_id, 'sortStyle', 'date_desc');
145*782f6af0SGreg Roach		$show_user  = $this->getBlockSetting($block_id, 'show_user', '1');
146e2a378d3SGreg Roach		$block      = $this->getBlockSetting($block_id, 'block', '1');
147e2a378d3SGreg Roach		$hide_empty = $this->getBlockSetting($block_id, 'hide_empty', '0');
1488c2e8227SGreg Roach
1498c2e8227SGreg Roach		echo '<tr><td class="descriptionbox wrap width33">';
1508c2e8227SGreg Roach		echo I18N::translate('Number of days to show');
1518c2e8227SGreg Roach		echo '</td><td class="optionbox">';
1528c2e8227SGreg Roach		echo '<input type="text" name="days" size="2" value="', $days, '">';
153def7396fSGreg Roach		echo ' <em>', I18N::plural('maximum %s day', 'maximum %s days', I18N::number(self::MAX_DAYS), I18N::number(self::MAX_DAYS)), '</em>';
1548c2e8227SGreg Roach		echo '</td></tr>';
1558c2e8227SGreg Roach
1568c2e8227SGreg Roach		echo '<tr><td class="descriptionbox wrap width33">';
1578c2e8227SGreg Roach		echo I18N::translate('Presentation style');
1588c2e8227SGreg Roach		echo '</td><td class="optionbox">';
1593d7a8a4cSGreg Roach		echo FunctionsEdit::selectEditControl('infoStyle', array('list' => I18N::translate('list'), 'table' => I18N::translate('table')), null, $infoStyle, '');
1608c2e8227SGreg Roach		echo '</td></tr>';
1618c2e8227SGreg Roach
1628c2e8227SGreg Roach		echo '<tr><td class="descriptionbox wrap width33">';
1638c2e8227SGreg Roach		echo I18N::translate('Sort order');
1648c2e8227SGreg Roach		echo '</td><td class="optionbox">';
1653d7a8a4cSGreg Roach		echo FunctionsEdit::selectEditControl('sortStyle', array(
1668c2e8227SGreg Roach			'name'      => /* I18N: An option in a list-box */ I18N::translate('sort by name'),
1678c2e8227SGreg Roach			'date_asc'  => /* I18N: An option in a list-box */ I18N::translate('sort by date, oldest first'),
168cbc1590aSGreg Roach			'date_desc' => /* I18N: An option in a list-box */ I18N::translate('sort by date, newest first'),
1698c2e8227SGreg Roach		), null, $sortStyle, '');
1708c2e8227SGreg Roach		echo '</td></tr>';
1718c2e8227SGreg Roach
1728c2e8227SGreg Roach		echo '<tr><td class="descriptionbox wrap width33">';
173*782f6af0SGreg Roach		echo /* I18N: label for a yes/no option */ I18N::translate('Show the user who made the change');
174*782f6af0SGreg Roach		echo '</td><td class="optionbox">';
175*782f6af0SGreg Roach		echo FunctionsEdit::editFieldYesNo('show_user', $block);
176*782f6af0SGreg Roach		echo '</td></tr>';
177*782f6af0SGreg Roach
178*782f6af0SGreg Roach		echo '<tr><td class="descriptionbox wrap width33">';
1798c2e8227SGreg Roach		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
1808c2e8227SGreg Roach		echo '</td><td class="optionbox">';
1813d7a8a4cSGreg Roach		echo FunctionsEdit::editFieldYesNo('block', $block);
1828c2e8227SGreg Roach		echo '</td></tr>';
1838c2e8227SGreg Roach
1848c2e8227SGreg Roach		echo '<tr><td class="descriptionbox wrap width33">';
1858c2e8227SGreg Roach		echo I18N::translate('Should this block be hidden when it is empty?');
1868c2e8227SGreg Roach		echo '</td><td class="optionbox">';
1873d7a8a4cSGreg Roach		echo FunctionsEdit::editFieldYesNo('hide_empty', $hide_empty);
1888c2e8227SGreg Roach		echo '</td></tr>';
1898c2e8227SGreg Roach		echo '<tr><td colspan="2" class="optionbox wrap">';
1908c2e8227SGreg Roach		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>';
1918c2e8227SGreg Roach		echo '</td></tr>';
1928c2e8227SGreg Roach	}
1938c2e8227SGreg Roach
1948c2e8227SGreg Roach}
195