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\Tree; 248c2e8227SGreg Roach 258c2e8227SGreg Roach/** 268c2e8227SGreg Roach * Class RecentChangesModule 278c2e8227SGreg Roach */ 28*c1010edaSGreg Roachclass RecentChangesModule extends AbstractModule implements ModuleBlockInterface 29*c1010edaSGreg Roach{ 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} */ 39*c1010edaSGreg Roach public function getTitle() 40*c1010edaSGreg Roach { 41*c1010edaSGreg Roach return /* I18N: Name of a module */ 42*c1010edaSGreg Roach I18N::translate('Recent changes'); 438c2e8227SGreg Roach } 448c2e8227SGreg Roach 458c2e8227SGreg Roach /** {@inheritdoc} */ 46*c1010edaSGreg Roach public function getDescription() 47*c1010edaSGreg Roach { 48*c1010edaSGreg Roach return /* I18N: Description of the “Recent changes” module */ 49*c1010edaSGreg Roach I18N::translate('A list of records that have been updated recently.'); 508c2e8227SGreg Roach } 518c2e8227SGreg Roach 526e45321fSGreg Roach /** {@inheritdoc} */ 53*c1010edaSGreg Roach public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string 54*c1010edaSGreg Roach { 55e490cd80SGreg Roach global $ctype; 568c2e8227SGreg Roach 57e2a378d3SGreg Roach $days = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 586e45321fSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE); 596e45321fSGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE); 60047cb287SGreg Roach $show_user = (bool)$this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 618c2e8227SGreg Roach 62c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 638c2e8227SGreg Roach 64e490cd80SGreg Roach $records = $this->getRecentChanges($tree, $days); 658c2e8227SGreg Roach 660280f44aSGreg Roach switch ($sortStyle) { 670280f44aSGreg Roach case 'name': 68*c1010edaSGreg Roach uasort($records, [ 69*c1010edaSGreg Roach 'self', 70*c1010edaSGreg Roach 'sortByNameAndChangeDate', 71*c1010edaSGreg Roach ]); 728c2e8227SGreg Roach break; 730280f44aSGreg Roach case 'date_asc': 74*c1010edaSGreg Roach uasort($records, [ 75*c1010edaSGreg Roach 'self', 76*c1010edaSGreg Roach 'sortByChangeDateAndName', 77*c1010edaSGreg Roach ]); 780280f44aSGreg Roach $records = array_reverse($records); 798c2e8227SGreg Roach break; 800280f44aSGreg Roach case 'date_desc': 81*c1010edaSGreg Roach uasort($records, [ 82*c1010edaSGreg Roach 'self', 83*c1010edaSGreg Roach 'sortByChangeDateAndName', 84*c1010edaSGreg Roach ]); 858c2e8227SGreg Roach } 860280f44aSGreg Roach 870280f44aSGreg Roach if (empty($records)) { 880280f44aSGreg 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)); 890280f44aSGreg Roach } elseif ($infoStyle === 'list') { 904d2d7d1aSGreg Roach $content = view('modules/recent_changes/changes-list', [ 914d2d7d1aSGreg Roach 'records' => $records, 924d2d7d1aSGreg Roach 'show_user' => $show_user, 934d2d7d1aSGreg Roach ]); 940280f44aSGreg Roach } else { 954d2d7d1aSGreg Roach $content = view('modules/recent_changes/changes-table', [ 960280f44aSGreg Roach 'records' => $records, 970280f44aSGreg Roach 'show_user' => $show_user, 980280f44aSGreg Roach ]); 998c2e8227SGreg Roach } 1008c2e8227SGreg Roach 1018c2e8227SGreg Roach if ($template) { 102e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 103*c1010edaSGreg Roach $config_url = route('tree-page-block-edit', [ 104*c1010edaSGreg Roach 'block_id' => $block_id, 105*c1010edaSGreg Roach 'ged' => $tree->getName(), 106*c1010edaSGreg Roach ]); 107397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 108*c1010edaSGreg Roach $config_url = route('user-page-block-edit', [ 109*c1010edaSGreg Roach 'block_id' => $block_id, 110*c1010edaSGreg Roach 'ged' => $tree->getName(), 111*c1010edaSGreg Roach ]); 1128cbbfdceSGreg Roach } else { 1138cbbfdceSGreg Roach $config_url = ''; 1148cbbfdceSGreg Roach } 1158cbbfdceSGreg Roach 116147e99aaSGreg Roach return view('modules/block-template', [ 1179c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1189c6524dcSGreg Roach 'id' => $block_id, 1198cbbfdceSGreg Roach 'config_url' => $config_url, 1208cbbfdceSGreg Roach 'title' => I18N::plural('Changes in the last %s day', 'Changes in the last %s days', $days, I18N::number($days)), 1219c6524dcSGreg Roach 'content' => $content, 1229c6524dcSGreg Roach ]); 1238c2e8227SGreg Roach } else { 1248c2e8227SGreg Roach return $content; 1258c2e8227SGreg Roach } 1268c2e8227SGreg Roach } 1278c2e8227SGreg Roach 1288c2e8227SGreg Roach /** {@inheritdoc} */ 129*c1010edaSGreg Roach public function loadAjax(): bool 130*c1010edaSGreg Roach { 1318c2e8227SGreg Roach return true; 1328c2e8227SGreg Roach } 1338c2e8227SGreg Roach 1348c2e8227SGreg Roach /** {@inheritdoc} */ 135*c1010edaSGreg Roach public function isUserBlock(): bool 136*c1010edaSGreg Roach { 1378c2e8227SGreg Roach return true; 1388c2e8227SGreg Roach } 1398c2e8227SGreg Roach 1408c2e8227SGreg Roach /** {@inheritdoc} */ 141*c1010edaSGreg Roach public function isGedcomBlock(): bool 142*c1010edaSGreg Roach { 1438c2e8227SGreg Roach return true; 1448c2e8227SGreg Roach } 1458c2e8227SGreg Roach 1466e45321fSGreg Roach /** {@inheritdoc} */ 147*c1010edaSGreg Roach public function configureBlock(Tree $tree, int $block_id) 148*c1010edaSGreg Roach { 149c385536dSGreg Roach if ($_SERVER['REQUEST_METHOD'] === 'POST') { 1506e45321fSGreg Roach $this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, self::MAX_DAYS)); 1516e45321fSGreg Roach $this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table')); 1526e45321fSGreg Roach $this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'name|date_asc|date_desc')); 153782f6af0SGreg Roach $this->setBlockSetting($block_id, 'show_user', Filter::postBool('show_user')); 154c385536dSGreg Roach 155c385536dSGreg Roach return; 1568c2e8227SGreg Roach } 1578c2e8227SGreg Roach 158e2a378d3SGreg Roach $days = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 1596e45321fSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE); 1606e45321fSGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE); 1616e45321fSGreg Roach $show_user = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 1628c2e8227SGreg Roach 163c385536dSGreg Roach $info_styles = [ 164*c1010edaSGreg Roach 'list' => /* I18N: An option in a list-box */ 165*c1010edaSGreg Roach I18N::translate('list'), 166*c1010edaSGreg Roach 'table' => /* I18N: An option in a list-box */ 167*c1010edaSGreg Roach I18N::translate('table'), 168c385536dSGreg Roach ]; 1698c2e8227SGreg Roach 170c385536dSGreg Roach $sort_styles = [ 171*c1010edaSGreg Roach 'name' => /* I18N: An option in a list-box */ 172*c1010edaSGreg Roach I18N::translate('sort by name'), 173*c1010edaSGreg Roach 'date_asc' => /* I18N: An option in a list-box */ 174*c1010edaSGreg Roach I18N::translate('sort by date, oldest first'), 175*c1010edaSGreg Roach 'date_desc' => /* I18N: An option in a list-box */ 176*c1010edaSGreg Roach I18N::translate('sort by date, newest first'), 177c385536dSGreg Roach ]; 1788c2e8227SGreg Roach 179147e99aaSGreg Roach echo view('modules/recent_changes/config', [ 180c385536dSGreg Roach 'days' => $days, 181c385536dSGreg Roach 'infoStyle' => $infoStyle, 182c385536dSGreg Roach 'info_styles' => $info_styles, 183c385536dSGreg Roach 'max_days' => self::MAX_DAYS, 184c385536dSGreg Roach 'sortStyle' => $sortStyle, 185c385536dSGreg Roach 'sort_styles' => $sort_styles, 186c385536dSGreg Roach 'show_user' => $show_user, 187c385536dSGreg Roach ]); 1888c2e8227SGreg Roach } 1898c2e8227SGreg Roach 1906e45321fSGreg Roach /** 1916e45321fSGreg Roach * Find records that have changed since a given julian day 1926e45321fSGreg Roach * 1936e45321fSGreg Roach * @param Tree $tree Changes for which tree 19424319d9dSGreg Roach * @param int $days Number of days 1956e45321fSGreg Roach * 1966e45321fSGreg Roach * @return GedcomRecord[] List of records with changes 1976e45321fSGreg Roach */ 198*c1010edaSGreg Roach private function getRecentChanges(Tree $tree, $days) 199*c1010edaSGreg Roach { 2006e45321fSGreg Roach $sql = 20124319d9dSGreg Roach "SELECT xref FROM `##change`" . 202b03b6f21SGreg Roach " WHERE new_gedcom != '' AND change_time > DATE_SUB(NOW(), INTERVAL :days DAY) AND gedcom_id = :tree_id" . 20324319d9dSGreg Roach " GROUP BY xref" . 20424319d9dSGreg Roach " ORDER BY MAX(change_id) DESC"; 2056e45321fSGreg Roach 20613abd6f3SGreg Roach $vars = [ 20724319d9dSGreg Roach 'days' => $days, 2086e45321fSGreg Roach 'tree_id' => $tree->getTreeId(), 20913abd6f3SGreg Roach ]; 2106e45321fSGreg Roach 2116e45321fSGreg Roach $xrefs = Database::prepare($sql)->execute($vars)->fetchOneColumn(); 2126e45321fSGreg Roach 21313abd6f3SGreg Roach $records = []; 2146e45321fSGreg Roach foreach ($xrefs as $xref) { 2156e45321fSGreg Roach $record = GedcomRecord::getInstance($xref, $tree); 2168e8029e5SRico Sonntag if ($record && $record->canShow()) { 2176e45321fSGreg Roach $records[] = $record; 2186e45321fSGreg Roach } 2196e45321fSGreg Roach } 2206e45321fSGreg Roach 2216e45321fSGreg Roach return $records; 2226e45321fSGreg Roach } 2236e45321fSGreg Roach 2246e45321fSGreg Roach /** 2256e45321fSGreg Roach * Sort the records by (1) last change date and (2) name 2266e45321fSGreg Roach * 2276e45321fSGreg Roach * @param GedcomRecord $a 2286e45321fSGreg Roach * @param GedcomRecord $b 2296e45321fSGreg Roach * 2306e45321fSGreg Roach * @return int 2316e45321fSGreg Roach */ 232*c1010edaSGreg Roach private static function sortByChangeDateAndName(GedcomRecord $a, GedcomRecord $b) 233*c1010edaSGreg Roach { 2346e45321fSGreg Roach return $b->lastChangeTimestamp(true) - $a->lastChangeTimestamp(true) ?: GedcomRecord::compare($a, $b); 2356e45321fSGreg Roach } 2366e45321fSGreg Roach 2376e45321fSGreg Roach /** 2386e45321fSGreg Roach * Sort the records by (1) name and (2) last change date 2396e45321fSGreg Roach * 2406e45321fSGreg Roach * @param GedcomRecord $a 2416e45321fSGreg Roach * @param GedcomRecord $b 2426e45321fSGreg Roach * 2436e45321fSGreg Roach * @return int 2446e45321fSGreg Roach */ 245*c1010edaSGreg Roach private static function sortByNameAndChangeDate(GedcomRecord $a, GedcomRecord $b) 246*c1010edaSGreg Roach { 2476e45321fSGreg Roach return GedcomRecord::compare($a, $b) ?: $b->lastChangeTimestamp(true) - $a->lastChangeTimestamp(true); 2486e45321fSGreg Roach } 2498c2e8227SGreg Roach} 250