18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team 68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 98c2e8227SGreg Roach * (at your option) any later version. 108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 138c2e8227SGreg Roach * GNU General Public License for more details. 148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 168c2e8227SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 2176692c8bSGreg Roach 224459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon; 236b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 24501bc70dSGreg Roachuse Fisharebest\Webtrees\Family; 256e45321fSGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 260e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 27501bc70dSGreg Roachuse Fisharebest\Webtrees\Individual; 28bd77bf38SGreg Roachuse Fisharebest\Webtrees\Services\UserService; 296e45321fSGreg Roachuse Fisharebest\Webtrees\Tree; 30e10e1dc9SGreg Roachuse Fisharebest\Webtrees\User; 3177654037SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 32bd77bf38SGreg Roachuse Illuminate\Database\Query\Expression; 33501bc70dSGreg Roachuse Illuminate\Database\Query\JoinClause; 34bd77bf38SGreg Roachuse Illuminate\Support\Collection; 351e7a7a28SGreg Roachuse Illuminate\Support\Str; 366ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 37bd77bf38SGreg Roachuse stdClass; 38bd77bf38SGreg Roach 39bd77bf38SGreg Roachuse function extract; 40bd77bf38SGreg Roachuse function view; 41bd77bf38SGreg Roach 42bd77bf38SGreg Roachuse const EXTR_OVERWRITE; 438c2e8227SGreg Roach 448c2e8227SGreg Roach/** 458c2e8227SGreg Roach * Class RecentChangesModule 468c2e8227SGreg Roach */ 4737eb8894SGreg Roachclass RecentChangesModule extends AbstractModule implements ModuleBlockInterface 48c1010edaSGreg Roach{ 4949a243cbSGreg Roach use ModuleBlockTrait; 5049a243cbSGreg Roach 51501bc70dSGreg Roach // Where do we look for change information 52501bc70dSGreg Roach private const SOURCE_DATABASE = 'database'; 53501bc70dSGreg Roach private const SOURCE_GEDCOM = 'gedcom'; 54501bc70dSGreg Roach 5516d6367aSGreg Roach private const DEFAULT_DAYS = '7'; 5616d6367aSGreg Roach private const DEFAULT_SHOW_USER = '1'; 57501bc70dSGreg Roach private const DEFAULT_SHOW_DATE = '1'; 5816d6367aSGreg Roach private const DEFAULT_SORT_STYLE = 'date_desc'; 5916d6367aSGreg Roach private const DEFAULT_INFO_STYLE = 'table'; 60501bc70dSGreg Roach private const DEFAULT_SOURCE = self::SOURCE_DATABASE; 6116d6367aSGreg Roach private const MAX_DAYS = 90; 628c2e8227SGreg Roach 6301221f27SGreg Roach // Pagination 6401221f27SGreg Roach private const LIMIT_LOW = 10; 6501221f27SGreg Roach private const LIMIT_HIGH = 20; 6601221f27SGreg Roach 67bd77bf38SGreg Roach /** @var UserService */ 68bd77bf38SGreg Roach private $user_service; 69bd77bf38SGreg Roach 70bd77bf38SGreg Roach /** 71bd77bf38SGreg Roach * RecentChangesModule constructor. 72bd77bf38SGreg Roach * 73bd77bf38SGreg Roach * @param UserService $user_service 74bd77bf38SGreg Roach */ 75bd77bf38SGreg Roach public function __construct(UserService $user_service) 76bd77bf38SGreg Roach { 77bd77bf38SGreg Roach $this->user_service = $user_service; 78bd77bf38SGreg Roach } 79bd77bf38SGreg Roach 80961ec755SGreg Roach /** 810cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 82961ec755SGreg Roach * 83961ec755SGreg Roach * @return string 84961ec755SGreg Roach */ 8549a243cbSGreg Roach public function title(): string 86c1010edaSGreg Roach { 87bbb76c12SGreg Roach /* I18N: Name of a module */ 88bbb76c12SGreg Roach return I18N::translate('Recent changes'); 898c2e8227SGreg Roach } 908c2e8227SGreg Roach 91961ec755SGreg Roach /** 92961ec755SGreg Roach * A sentence describing what this module does. 93961ec755SGreg Roach * 94961ec755SGreg Roach * @return string 95961ec755SGreg Roach */ 9649a243cbSGreg Roach public function description(): string 97c1010edaSGreg Roach { 98bbb76c12SGreg Roach /* I18N: Description of the “Recent changes” module */ 99bbb76c12SGreg Roach return I18N::translate('A list of records that have been updated recently.'); 1008c2e8227SGreg Roach } 1018c2e8227SGreg Roach 1020dcd9387SGreg Roach /** 1030dcd9387SGreg Roach * @param Tree $tree 1040dcd9387SGreg Roach * @param int $block_id 1050dcd9387SGreg Roach * @param string $context 1060dcd9387SGreg Roach * @param array $config 1070dcd9387SGreg Roach * 1080dcd9387SGreg Roach * @return string 1090dcd9387SGreg Roach */ 1103caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 111c1010edaSGreg Roach { 11255664801SGreg Roach $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 1136e45321fSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE); 1146e45321fSGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE); 115047cb287SGreg Roach $show_user = (bool) $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 116501bc70dSGreg Roach $show_date = (bool) $this->getBlockSetting($block_id, 'show_date', self::DEFAULT_SHOW_DATE); 117501bc70dSGreg Roach $source = $this->getBlockSetting($block_id, 'source', self::DEFAULT_SOURCE); 1188c2e8227SGreg Roach 1193caaa4d2SGreg Roach extract($config, EXTR_OVERWRITE); 1208c2e8227SGreg Roach 121501bc70dSGreg Roach if ($source === self::SOURCE_DATABASE) { 122501bc70dSGreg Roach $rows = $this->getRecentChangesFromDatabase($tree, $days); 123501bc70dSGreg Roach } else { 124501bc70dSGreg Roach $rows = $this->getRecentChangesFromGenealogy($tree, $days); 125501bc70dSGreg Roach } 1268c2e8227SGreg Roach 1270280f44aSGreg Roach switch ($sortStyle) { 1280280f44aSGreg Roach case 'name': 129bd77bf38SGreg Roach $rows = $rows->sort(static function (stdClass $x, stdClass $y): int { 130bd77bf38SGreg Roach return GedcomRecord::nameComparator()($x->record, $y->record); 131bd77bf38SGreg Roach }); 132fbd9d3f8SGreg Roach $order = [[1, 'asc']]; 1338c2e8227SGreg Roach break; 13480e23601SGreg Roach 1350280f44aSGreg Roach case 'date_asc': 136bd77bf38SGreg Roach $rows = $rows->sort(static function (stdClass $x, stdClass $y): int { 137bd77bf38SGreg Roach return $x->time <=> $y->time; 138bd77bf38SGreg Roach }); 139fbd9d3f8SGreg Roach $order = [[2, 'asc']]; 1408c2e8227SGreg Roach break; 14180e23601SGreg Roach 142fbd9d3f8SGreg Roach default: 1430280f44aSGreg Roach case 'date_desc': 144bd77bf38SGreg Roach $rows = $rows->sort(static function (stdClass $x, stdClass $y): int { 145bd77bf38SGreg Roach return $y->time <=> $x->time; 146bd77bf38SGreg Roach }); 1471be6d38bSGreg Roach $order = [[2, 'desc']]; 148fbd9d3f8SGreg Roach break; 1498c2e8227SGreg Roach } 1500280f44aSGreg Roach 151bd77bf38SGreg Roach if ($rows->isEmpty()) { 1520280f44aSGreg 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)); 1530280f44aSGreg Roach } elseif ($infoStyle === 'list') { 1544d2d7d1aSGreg Roach $content = view('modules/recent_changes/changes-list', [ 155e24053e5SGreg Roach 'id' => $block_id, 15601221f27SGreg Roach 'limit_low' => self::LIMIT_LOW, 15701221f27SGreg Roach 'limit_high' => self::LIMIT_HIGH, 158e24053e5SGreg Roach 'rows' => $rows->values(), 159501bc70dSGreg Roach 'show_date' => $show_date, 1604d2d7d1aSGreg Roach 'show_user' => $show_user, 1614d2d7d1aSGreg Roach ]); 1620280f44aSGreg Roach } else { 1634d2d7d1aSGreg Roach $content = view('modules/recent_changes/changes-table', [ 16401221f27SGreg Roach 'limit_low' => self::LIMIT_LOW, 16501221f27SGreg Roach 'limit_high' => self::LIMIT_HIGH, 166bd77bf38SGreg Roach 'rows' => $rows, 167501bc70dSGreg Roach 'show_date' => $show_date, 1680280f44aSGreg Roach 'show_user' => $show_user, 169fbd9d3f8SGreg Roach 'order' => $order, 1700280f44aSGreg Roach ]); 1718c2e8227SGreg Roach } 1728c2e8227SGreg Roach 1733caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 174147e99aaSGreg Roach return view('modules/block-template', [ 1751e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1769c6524dcSGreg Roach 'id' => $block_id, 1773caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 1788cbbfdceSGreg Roach 'title' => I18N::plural('Changes in the last %s day', 'Changes in the last %s days', $days, I18N::number($days)), 1799c6524dcSGreg Roach 'content' => $content, 1809c6524dcSGreg Roach ]); 1818c2e8227SGreg Roach } 182b2ce94c6SRico Sonntag 183b2ce94c6SRico Sonntag return $content; 1848c2e8227SGreg Roach } 1858c2e8227SGreg Roach 1863caaa4d2SGreg Roach /** 1873caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 1883caaa4d2SGreg Roach * 1893caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 1903caaa4d2SGreg Roach * 1913caaa4d2SGreg Roach * @return bool 1923caaa4d2SGreg Roach */ 193c1010edaSGreg Roach public function loadAjax(): bool 194c1010edaSGreg Roach { 1958c2e8227SGreg Roach return true; 1968c2e8227SGreg Roach } 1978c2e8227SGreg Roach 1983caaa4d2SGreg Roach /** 1993caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 2003caaa4d2SGreg Roach * 2013caaa4d2SGreg Roach * @return bool 2023caaa4d2SGreg Roach */ 203c1010edaSGreg Roach public function isUserBlock(): bool 204c1010edaSGreg Roach { 2058c2e8227SGreg Roach return true; 2068c2e8227SGreg Roach } 2078c2e8227SGreg Roach 2083caaa4d2SGreg Roach /** 2093caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 2103caaa4d2SGreg Roach * 2113caaa4d2SGreg Roach * @return bool 2123caaa4d2SGreg Roach */ 21363276d8fSGreg Roach public function isTreeBlock(): bool 214c1010edaSGreg Roach { 2158c2e8227SGreg Roach return true; 2168c2e8227SGreg Roach } 2178c2e8227SGreg Roach 21820ac4041SGreg Roach /** 219a45f9889SGreg Roach * Update the configuration for a block. 220a45f9889SGreg Roach * 2216ccdf4f0SGreg Roach * @param ServerRequestInterface $request 222a45f9889SGreg Roach * @param int $block_id 223a45f9889SGreg Roach * 224a45f9889SGreg Roach * @return void 225a45f9889SGreg Roach */ 2266ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 227a45f9889SGreg Roach { 228b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 229af2b6902SGreg Roach 230eb235819SGreg Roach $this->setBlockSetting($block_id, 'days', $params['days']); 231eb235819SGreg Roach $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']); 232eb235819SGreg Roach $this->setBlockSetting($block_id, 'sortStyle', $params['sortStyle']); 233501bc70dSGreg Roach $this->setBlockSetting($block_id, 'show_date', $params['show_date']); 234eb235819SGreg Roach $this->setBlockSetting($block_id, 'show_user', $params['show_user']); 235501bc70dSGreg Roach $this->setBlockSetting($block_id, 'source', $params['source']); 236a45f9889SGreg Roach } 237a45f9889SGreg Roach 238a45f9889SGreg Roach /** 23920ac4041SGreg Roach * An HTML form to edit block settings 24020ac4041SGreg Roach * 24120ac4041SGreg Roach * @param Tree $tree 24220ac4041SGreg Roach * @param int $block_id 24320ac4041SGreg Roach * 2443caaa4d2SGreg Roach * @return string 24520ac4041SGreg Roach */ 2463caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 247c1010edaSGreg Roach { 24855664801SGreg Roach $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 2496e45321fSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE); 2506e45321fSGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE); 251501bc70dSGreg Roach $show_date = $this->getBlockSetting($block_id, 'show_date', self::DEFAULT_SHOW_DATE); 2526e45321fSGreg Roach $show_user = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 253501bc70dSGreg Roach $source = $this->getBlockSetting($block_id, 'source', self::DEFAULT_SOURCE); 2548c2e8227SGreg Roach 255c385536dSGreg Roach $info_styles = [ 256bbb76c12SGreg Roach /* I18N: An option in a list-box */ 257bbb76c12SGreg Roach 'list' => I18N::translate('list'), 258bbb76c12SGreg Roach /* I18N: An option in a list-box */ 259bbb76c12SGreg Roach 'table' => I18N::translate('table'), 260c385536dSGreg Roach ]; 2618c2e8227SGreg Roach 262c385536dSGreg Roach $sort_styles = [ 263bbb76c12SGreg Roach /* I18N: An option in a list-box */ 264bbb76c12SGreg Roach 'name' => I18N::translate('sort by name'), 265bbb76c12SGreg Roach /* I18N: An option in a list-box */ 266bbb76c12SGreg Roach 'date_asc' => I18N::translate('sort by date, oldest first'), 267bbb76c12SGreg Roach /* I18N: An option in a list-box */ 268bbb76c12SGreg Roach 'date_desc' => I18N::translate('sort by date, newest first'), 269c385536dSGreg Roach ]; 2708c2e8227SGreg Roach 271501bc70dSGreg Roach $sources = [ 272501bc70dSGreg Roach /* I18N: An option in a list-box */ 273501bc70dSGreg Roach self::SOURCE_DATABASE => I18N::translate('show changes made in webtrees'), 274501bc70dSGreg Roach /* I18N: An option in a list-box */ 275501bc70dSGreg Roach self::SOURCE_GEDCOM => I18N::translate('show changes recorded in the genealogy data'), 276501bc70dSGreg Roach ]; 277501bc70dSGreg Roach 2783caaa4d2SGreg Roach return view('modules/recent_changes/config', [ 279c385536dSGreg Roach 'days' => $days, 280c385536dSGreg Roach 'infoStyle' => $infoStyle, 281c385536dSGreg Roach 'info_styles' => $info_styles, 282c385536dSGreg Roach 'max_days' => self::MAX_DAYS, 283c385536dSGreg Roach 'sortStyle' => $sortStyle, 284c385536dSGreg Roach 'sort_styles' => $sort_styles, 285501bc70dSGreg Roach 'source' => $source, 286501bc70dSGreg Roach 'sources' => $sources, 287501bc70dSGreg Roach 'show_date' => $show_date, 288c385536dSGreg Roach 'show_user' => $show_user, 289c385536dSGreg Roach ]); 2908c2e8227SGreg Roach } 2918c2e8227SGreg Roach 2926e45321fSGreg Roach /** 2936e45321fSGreg Roach * Find records that have changed since a given julian day 2946e45321fSGreg Roach * 2956e45321fSGreg Roach * @param Tree $tree Changes for which tree 29624319d9dSGreg Roach * @param int $days Number of days 2976e45321fSGreg Roach * 298bd77bf38SGreg Roach * @return Collection<stdClass> List of records with changes 2996e45321fSGreg Roach */ 300501bc70dSGreg Roach private function getRecentChangesFromDatabase(Tree $tree, int $days): Collection 301c1010edaSGreg Roach { 302bd77bf38SGreg Roach $subquery = DB::table('change') 30377654037SGreg Roach ->where('gedcom_id', '=', $tree->id()) 30477654037SGreg Roach ->where('status', '=', 'accepted') 30577654037SGreg Roach ->where('new_gedcom', '<>', '') 30677654037SGreg Roach ->where('change_time', '>', Carbon::now()->subDays($days)) 3077f5c2944SGreg Roach ->groupBy(['xref']) 308bd77bf38SGreg Roach ->select(new Expression('MAX(change_id) AS recent_change_id')); 309bd77bf38SGreg Roach 310bd77bf38SGreg Roach $query = DB::table('change') 311bd77bf38SGreg Roach ->joinSub($subquery, 'recent', 'recent_change_id', '=', 'change_id') 312bd77bf38SGreg Roach ->select(['change.*']); 313bd77bf38SGreg Roach 314bd77bf38SGreg Roach return $query 315bd77bf38SGreg Roach ->get() 316bd77bf38SGreg Roach ->map(function (stdClass $row) use ($tree): stdClass { 317bd77bf38SGreg Roach return (object) [ 3186b9cb339SGreg Roach 'record' => Registry::gedcomRecordFactory()->make($row->xref, $tree, $row->new_gedcom), 319bd77bf38SGreg Roach 'time' => Carbon::create($row->change_time)->local(), 320*d608cecbSGreg Roach 'user' => $this->user_service->find((int) $row->user_id), 321bd77bf38SGreg Roach ]; 32277654037SGreg Roach }) 323bd77bf38SGreg Roach ->filter(static function (stdClass $row): bool { 324bd77bf38SGreg Roach return $row->record instanceof GedcomRecord && $row->record->canShow(); 325bd77bf38SGreg Roach }); 3266e45321fSGreg Roach } 327501bc70dSGreg Roach 328501bc70dSGreg Roach /** 329501bc70dSGreg Roach * Find records that have changed since a given julian day 330501bc70dSGreg Roach * 331501bc70dSGreg Roach * @param Tree $tree Changes for which tree 332501bc70dSGreg Roach * @param int $days Number of days 333501bc70dSGreg Roach * 334501bc70dSGreg Roach * @return Collection<stdClass> List of records with changes 335501bc70dSGreg Roach */ 336501bc70dSGreg Roach private function getRecentChangesFromGenealogy(Tree $tree, int $days): Collection 337501bc70dSGreg Roach { 338501bc70dSGreg Roach $julian_day = Carbon::now()->julianDay() - $days; 339501bc70dSGreg Roach 340501bc70dSGreg Roach $individuals = DB::table('dates') 341501bc70dSGreg Roach ->where('d_file', '=', $tree->id()) 342501bc70dSGreg Roach ->where('d_julianday1', '>=', $julian_day) 343501bc70dSGreg Roach ->where('d_fact', '=', 'CHAN') 344501bc70dSGreg Roach ->join('individuals', static function (JoinClause $join): void { 345501bc70dSGreg Roach $join 346501bc70dSGreg Roach ->on('d_file', '=', 'i_file') 347501bc70dSGreg Roach ->on('d_gid', '=', 'i_id'); 348501bc70dSGreg Roach }) 349501bc70dSGreg Roach ->select(['individuals.*']) 350501bc70dSGreg Roach ->get() 3516b9cb339SGreg Roach ->map(Registry::individualFactory()->mapper($tree)) 352501bc70dSGreg Roach ->filter(Individual::accessFilter()); 353501bc70dSGreg Roach 354501bc70dSGreg Roach $families = DB::table('dates') 355501bc70dSGreg Roach ->where('d_file', '=', $tree->id()) 356501bc70dSGreg Roach ->where('d_julianday1', '>=', $julian_day) 357501bc70dSGreg Roach ->where('d_fact', '=', 'CHAN') 358501bc70dSGreg Roach ->join('families', static function (JoinClause $join): void { 359501bc70dSGreg Roach $join 360501bc70dSGreg Roach ->on('d_file', '=', 'f_file') 361501bc70dSGreg Roach ->on('d_gid', '=', 'f_id'); 362501bc70dSGreg Roach }) 363501bc70dSGreg Roach ->select(['families.*']) 364501bc70dSGreg Roach ->get() 3656b9cb339SGreg Roach ->map(Registry::familyFactory()->mapper($tree)) 366501bc70dSGreg Roach ->filter(Family::accessFilter()); 367501bc70dSGreg Roach 368501bc70dSGreg Roach return $individuals->merge($families) 369501bc70dSGreg Roach ->map(function (GedcomRecord $record): stdClass { 370e10e1dc9SGreg Roach $user = $this->user_service->findByUserName($record->lastChangeUser()); 371e10e1dc9SGreg Roach 372501bc70dSGreg Roach return (object) [ 373501bc70dSGreg Roach 'record' => $record, 374501bc70dSGreg Roach 'time' => $record->lastChangeTimestamp(), 375e10e1dc9SGreg Roach 'user' => $user ?? new User(0, '…', '…', ''), 376501bc70dSGreg Roach ]; 377501bc70dSGreg Roach }); 378501bc70dSGreg Roach } 3798c2e8227SGreg Roach} 380