18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 41062a142SGreg Roach * Copyright (C) 2018 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; 196e45321fSGreg Roachuse Fisharebest\Webtrees\Database; 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter; 216e45321fSGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 236e45321fSGreg Roachuse Fisharebest\Webtrees\Individual; 246e45321fSGreg Roachuse Fisharebest\Webtrees\Tree; 258c2e8227SGreg Roach 268c2e8227SGreg Roach/** 278c2e8227SGreg Roach * Class RecentChangesModule 288c2e8227SGreg Roach */ 29e2a378d3SGreg Roachclass RecentChangesModule extends AbstractModule implements ModuleBlockInterface { 306e45321fSGreg Roach const DEFAULT_BLOCK = '1'; 318c2e8227SGreg Roach const DEFAULT_DAYS = 7; 326e45321fSGreg Roach const DEFAULT_HIDE_EMPTY = '0'; 336e45321fSGreg Roach const DEFAULT_SHOW_USER = '1'; 346e45321fSGreg Roach const DEFAULT_SORT_STYLE = 'date_desc'; 356e45321fSGreg Roach const DEFAULT_INFO_STYLE = 'table'; 368c2e8227SGreg Roach const MAX_DAYS = 90; 378c2e8227SGreg Roach 388c2e8227SGreg Roach /** {@inheritdoc} */ 398c2e8227SGreg Roach public function getTitle() { 408c2e8227SGreg Roach return /* I18N: Name of a module */ I18N::translate('Recent changes'); 418c2e8227SGreg Roach } 428c2e8227SGreg Roach 438c2e8227SGreg Roach /** {@inheritdoc} */ 448c2e8227SGreg Roach public function getDescription() { 458c2e8227SGreg Roach return /* I18N: Description of the “Recent changes” module */ I18N::translate('A list of records that have been updated recently.'); 468c2e8227SGreg Roach } 478c2e8227SGreg Roach 486e45321fSGreg Roach /** {@inheritdoc} */ 49*e490cd80SGreg Roach public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string { 50*e490cd80SGreg Roach global $ctype; 518c2e8227SGreg Roach 52e2a378d3SGreg Roach $days = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 536e45321fSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE); 546e45321fSGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE); 55047cb287SGreg Roach $show_user = (bool) $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 568c2e8227SGreg Roach 57c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 588c2e8227SGreg Roach 59*e490cd80SGreg Roach $records = $this->getRecentChanges($tree, $days); 608c2e8227SGreg Roach 610280f44aSGreg Roach switch ($sortStyle) { 620280f44aSGreg Roach case 'name': 630280f44aSGreg Roach uasort($records, ['self', 'sortByNameAndChangeDate']); 648c2e8227SGreg Roach break; 650280f44aSGreg Roach case 'date_asc': 660280f44aSGreg Roach uasort($records, ['self', 'sortByChangeDateAndName']); 670280f44aSGreg Roach $records = array_reverse($records); 688c2e8227SGreg Roach break; 690280f44aSGreg Roach case 'date_desc': 700280f44aSGreg Roach uasort($records, ['self', 'sortByChangeDateAndName']); 718c2e8227SGreg Roach } 720280f44aSGreg Roach 730280f44aSGreg Roach if (empty($records)) { 740280f44aSGreg 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)); 750280f44aSGreg Roach } elseif ($infoStyle === 'list') { 760280f44aSGreg Roach $content = $this->changesList($records, $show_user); 770280f44aSGreg Roach } else { 780280f44aSGreg Roach $content = view('blocks/changes-' . $infoStyle, [ 790280f44aSGreg Roach 'records' => $records, 800280f44aSGreg Roach 'show_user' => $show_user, 810280f44aSGreg Roach ]); 828c2e8227SGreg Roach } 838c2e8227SGreg Roach 848c2e8227SGreg Roach if ($template) { 85*e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 86*e490cd80SGreg Roach $config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]); 87397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 88*e490cd80SGreg Roach $config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]); 898cbbfdceSGreg Roach } else { 908cbbfdceSGreg Roach $config_url = ''; 918cbbfdceSGreg Roach } 928cbbfdceSGreg Roach 93225e381fSGreg Roach return view('blocks/template', [ 949c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 959c6524dcSGreg Roach 'id' => $block_id, 968cbbfdceSGreg Roach 'config_url' => $config_url, 978cbbfdceSGreg Roach 'title' => I18N::plural('Changes in the last %s day', 'Changes in the last %s days', $days, I18N::number($days)), 989c6524dcSGreg Roach 'content' => $content, 999c6524dcSGreg Roach ]); 1008c2e8227SGreg Roach } else { 1018c2e8227SGreg Roach return $content; 1028c2e8227SGreg Roach } 1038c2e8227SGreg Roach } 1048c2e8227SGreg Roach 1058c2e8227SGreg Roach /** {@inheritdoc} */ 106a9430be8SGreg Roach public function loadAjax(): bool { 1078c2e8227SGreg Roach return true; 1088c2e8227SGreg Roach } 1098c2e8227SGreg Roach 1108c2e8227SGreg Roach /** {@inheritdoc} */ 111a9430be8SGreg Roach public function isUserBlock(): bool { 1128c2e8227SGreg Roach return true; 1138c2e8227SGreg Roach } 1148c2e8227SGreg Roach 1158c2e8227SGreg Roach /** {@inheritdoc} */ 116a9430be8SGreg Roach public function isGedcomBlock(): bool { 1178c2e8227SGreg Roach return true; 1188c2e8227SGreg Roach } 1198c2e8227SGreg Roach 1206e45321fSGreg Roach /** {@inheritdoc} */ 121*e490cd80SGreg Roach public function configureBlock(Tree $tree, int $block_id) { 122c385536dSGreg Roach if ($_SERVER['REQUEST_METHOD'] === 'POST') { 1236e45321fSGreg Roach $this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, self::MAX_DAYS)); 1246e45321fSGreg Roach $this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table')); 1256e45321fSGreg Roach $this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'name|date_asc|date_desc')); 126782f6af0SGreg Roach $this->setBlockSetting($block_id, 'show_user', Filter::postBool('show_user')); 127c385536dSGreg Roach 128c385536dSGreg Roach return; 1298c2e8227SGreg Roach } 1308c2e8227SGreg Roach 131e2a378d3SGreg Roach $days = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 1326e45321fSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE); 1336e45321fSGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE); 1346e45321fSGreg Roach $show_user = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 1358c2e8227SGreg Roach 136c385536dSGreg Roach $info_styles = [ 137c385536dSGreg Roach 'list' => /* I18N: An option in a list-box */ I18N::translate('list'), 138c385536dSGreg Roach 'table' => /* I18N: An option in a list-box */ I18N::translate('table'), 139c385536dSGreg Roach ]; 1408c2e8227SGreg Roach 141c385536dSGreg Roach $sort_styles = [ 1428c2e8227SGreg Roach 'name' => /* I18N: An option in a list-box */ I18N::translate('sort by name'), 1438c2e8227SGreg Roach 'date_asc' => /* I18N: An option in a list-box */ I18N::translate('sort by date, oldest first'), 144cbc1590aSGreg Roach 'date_desc' => /* I18N: An option in a list-box */ I18N::translate('sort by date, newest first'), 145c385536dSGreg Roach ]; 1468c2e8227SGreg Roach 147c385536dSGreg Roach echo view('blocks/recent-changes-config', [ 148c385536dSGreg Roach 'days' => $days, 149c385536dSGreg Roach 'infoStyle' => $infoStyle, 150c385536dSGreg Roach 'info_styles' => $info_styles, 151c385536dSGreg Roach 'max_days' => self::MAX_DAYS, 152c385536dSGreg Roach 'sortStyle' => $sortStyle, 153c385536dSGreg Roach 'sort_styles' => $sort_styles, 154c385536dSGreg Roach 'show_user' => $show_user, 155c385536dSGreg Roach ]); 1568c2e8227SGreg Roach } 1578c2e8227SGreg Roach 1586e45321fSGreg Roach /** 1596e45321fSGreg Roach * Find records that have changed since a given julian day 1606e45321fSGreg Roach * 1616e45321fSGreg Roach * @param Tree $tree Changes for which tree 16224319d9dSGreg Roach * @param int $days Number of days 1636e45321fSGreg Roach * 1646e45321fSGreg Roach * @return GedcomRecord[] List of records with changes 1656e45321fSGreg Roach */ 16624319d9dSGreg Roach private function getRecentChanges(Tree $tree, $days) { 1676e45321fSGreg Roach $sql = 16824319d9dSGreg Roach "SELECT xref FROM `##change`" . 169b03b6f21SGreg Roach " WHERE new_gedcom != '' AND change_time > DATE_SUB(NOW(), INTERVAL :days DAY) AND gedcom_id = :tree_id" . 17024319d9dSGreg Roach " GROUP BY xref" . 17124319d9dSGreg Roach " ORDER BY MAX(change_id) DESC"; 1726e45321fSGreg Roach 17313abd6f3SGreg Roach $vars = [ 17424319d9dSGreg Roach 'days' => $days, 1756e45321fSGreg Roach 'tree_id' => $tree->getTreeId(), 17613abd6f3SGreg Roach ]; 1776e45321fSGreg Roach 1786e45321fSGreg Roach $xrefs = Database::prepare($sql)->execute($vars)->fetchOneColumn(); 1796e45321fSGreg Roach 18013abd6f3SGreg Roach $records = []; 1816e45321fSGreg Roach foreach ($xrefs as $xref) { 1826e45321fSGreg Roach $record = GedcomRecord::getInstance($xref, $tree); 1838e8029e5SRico Sonntag if ($record && $record->canShow()) { 1846e45321fSGreg Roach $records[] = $record; 1856e45321fSGreg Roach } 1866e45321fSGreg Roach } 1876e45321fSGreg Roach 1886e45321fSGreg Roach return $records; 1896e45321fSGreg Roach } 1906e45321fSGreg Roach 1916e45321fSGreg Roach /** 1926e45321fSGreg Roach * Format a table of events 1936e45321fSGreg Roach * 1946e45321fSGreg Roach * @param GedcomRecord[] $records 1956e45321fSGreg Roach * @param bool $show_user 1966e45321fSGreg Roach * 1976e45321fSGreg Roach * @return string 1986e45321fSGreg Roach */ 1990280f44aSGreg Roach private function changesList(array $records, $show_user) { 2006e45321fSGreg Roach $html = ''; 2016e45321fSGreg Roach foreach ($records as $record) { 202b1f1e4efSGreg Roach $html .= '<a href="' . e($record->url()) . '" class="list_item name2">' . $record->getFullName() . '</a>'; 2036e45321fSGreg Roach $html .= '<div class="indent" style="margin-bottom: 5px;">'; 2046e45321fSGreg Roach if ($record instanceof Individual) { 2056e45321fSGreg Roach if ($record->getAddName()) { 206b1f1e4efSGreg Roach $html .= '<a href="' . e($record->url()) . '" class="list_item">' . $record->getAddName() . '</a>'; 2076e45321fSGreg Roach } 2086e45321fSGreg Roach } 20956834ce1SGreg Roach 21056834ce1SGreg Roach // The timestamp may be missing or private. 21156834ce1SGreg Roach $timestamp = $record->lastChangeTimestamp(); 21256834ce1SGreg Roach if ($timestamp !== '') { 2136e45321fSGreg Roach if ($show_user) { 2146e45321fSGreg Roach $html .= /* I18N: [a record was] Changed on <date/time> by <user> */ 215d53324c9SGreg Roach I18N::translate('Changed on %1$s by %2$s', $timestamp, e($record->lastChangeUser())); 2166e45321fSGreg Roach } else { 2176e45321fSGreg Roach $html .= /* I18N: [a record was] Changed on <date/time> */ 21856834ce1SGreg Roach I18N::translate('Changed on %1$s', $timestamp); 21956834ce1SGreg Roach } 2206e45321fSGreg Roach } 2216e45321fSGreg Roach $html .= '</div>'; 2226e45321fSGreg Roach } 2236e45321fSGreg Roach 2246e45321fSGreg Roach return $html; 2256e45321fSGreg Roach } 2266e45321fSGreg Roach 2276e45321fSGreg Roach /** 2286e45321fSGreg Roach * Sort the records by (1) last change date and (2) name 2296e45321fSGreg Roach * 2306e45321fSGreg Roach * @param GedcomRecord $a 2316e45321fSGreg Roach * @param GedcomRecord $b 2326e45321fSGreg Roach * 2336e45321fSGreg Roach * @return int 2346e45321fSGreg Roach */ 2356e45321fSGreg Roach private static function sortByChangeDateAndName(GedcomRecord $a, GedcomRecord $b) { 2366e45321fSGreg Roach return $b->lastChangeTimestamp(true) - $a->lastChangeTimestamp(true) ?: GedcomRecord::compare($a, $b); 2376e45321fSGreg Roach } 2386e45321fSGreg Roach 2396e45321fSGreg Roach /** 2406e45321fSGreg Roach * Sort the records by (1) name and (2) last change date 2416e45321fSGreg Roach * 2426e45321fSGreg Roach * @param GedcomRecord $a 2436e45321fSGreg Roach * @param GedcomRecord $b 2446e45321fSGreg Roach * 2456e45321fSGreg Roach * @return int 2466e45321fSGreg Roach */ 2476e45321fSGreg Roach private static function sortByNameAndChangeDate(GedcomRecord $a, GedcomRecord $b) { 2486e45321fSGreg Roach return GedcomRecord::compare($a, $b) ?: $b->lastChangeTimestamp(true) - $a->lastChangeTimestamp(true); 2496e45321fSGreg Roach } 2508c2e8227SGreg Roach} 251