1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\Webtrees\DB; 23use Fisharebest\Webtrees\Family; 24use Fisharebest\Webtrees\GedcomRecord; 25use Fisharebest\Webtrees\I18N; 26use Fisharebest\Webtrees\Individual; 27use Fisharebest\Webtrees\Registry; 28use Fisharebest\Webtrees\Services\UserService; 29use Fisharebest\Webtrees\Tree; 30use Fisharebest\Webtrees\User; 31use Fisharebest\Webtrees\Validator; 32use Illuminate\Database\Query\Expression; 33use Illuminate\Database\Query\JoinClause; 34use Illuminate\Support\Collection; 35use Illuminate\Support\Str; 36use Psr\Http\Message\ServerRequestInterface; 37use stdClass; 38 39use function extract; 40use function view; 41 42use const EXTR_OVERWRITE; 43 44/** 45 * Class RecentChangesModule 46 */ 47class RecentChangesModule extends AbstractModule implements ModuleBlockInterface 48{ 49 use ModuleBlockTrait; 50 51 // Where do we look for change information 52 private const SOURCE_DATABASE = 'database'; 53 private const SOURCE_GEDCOM = 'gedcom'; 54 55 private const DEFAULT_DAYS = '7'; 56 private const DEFAULT_SHOW_USER = '1'; 57 private const DEFAULT_SHOW_DATE = '1'; 58 private const DEFAULT_SORT_STYLE = 'date_desc'; 59 private const DEFAULT_INFO_STYLE = 'table'; 60 private const DEFAULT_SOURCE = self::SOURCE_DATABASE; 61 private const MAX_DAYS = 90; 62 63 // Pagination 64 private const LIMIT_LOW = 10; 65 private const LIMIT_HIGH = 20; 66 67 private UserService $user_service; 68 69 /** 70 * @param UserService $user_service 71 */ 72 public function __construct(UserService $user_service) 73 { 74 $this->user_service = $user_service; 75 } 76 77 /** 78 * How should this module be identified in the control panel, etc.? 79 * 80 * @return string 81 */ 82 public function title(): string 83 { 84 /* I18N: Name of a module */ 85 return I18N::translate('Recent changes'); 86 } 87 88 /** 89 * A sentence describing what this module does. 90 * 91 * @return string 92 */ 93 public function description(): string 94 { 95 /* I18N: Description of the “Recent changes” module */ 96 return I18N::translate('A list of records that have been updated recently.'); 97 } 98 99 /** 100 * @param Tree $tree 101 * @param int $block_id 102 * @param string $context 103 * @param array<string,string> $config 104 * 105 * @return string 106 */ 107 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 108 { 109 $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 110 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE); 111 $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE); 112 $show_user = (bool) $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 113 $show_date = (bool) $this->getBlockSetting($block_id, 'show_date', self::DEFAULT_SHOW_DATE); 114 $source = $this->getBlockSetting($block_id, 'source', self::DEFAULT_SOURCE); 115 116 extract($config, EXTR_OVERWRITE); 117 118 if ($source === self::SOURCE_DATABASE) { 119 $rows = $this->getRecentChangesFromDatabase($tree, $days); 120 } else { 121 $rows = $this->getRecentChangesFromGenealogy($tree, $days); 122 } 123 124 switch ($sortStyle) { 125 case 'name': 126 $rows = $rows->sort(static function (stdClass $x, stdClass $y): int { 127 return GedcomRecord::nameComparator()($x->record, $y->record); 128 }); 129 $order = [[1, 'asc']]; 130 break; 131 132 case 'date_asc': 133 $rows = $rows->sort(static function (stdClass $x, stdClass $y): int { 134 return $x->time <=> $y->time; 135 }); 136 $order = [[2, 'asc']]; 137 break; 138 139 default: 140 case 'date_desc': 141 $rows = $rows->sort(static function (stdClass $x, stdClass $y): int { 142 return $y->time <=> $x->time; 143 }); 144 $order = [[2, 'desc']]; 145 break; 146 } 147 148 if ($rows->isEmpty()) { 149 $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)); 150 } elseif ($infoStyle === 'list') { 151 $content = view('modules/recent_changes/changes-list', [ 152 'id' => $block_id, 153 'limit_low' => self::LIMIT_LOW, 154 'limit_high' => self::LIMIT_HIGH, 155 'rows' => $rows->values(), 156 'show_date' => $show_date, 157 'show_user' => $show_user, 158 ]); 159 } else { 160 $content = view('modules/recent_changes/changes-table', [ 161 'limit_low' => self::LIMIT_LOW, 162 'limit_high' => self::LIMIT_HIGH, 163 'rows' => $rows, 164 'show_date' => $show_date, 165 'show_user' => $show_user, 166 'order' => $order, 167 ]); 168 } 169 170 if ($context !== self::CONTEXT_EMBED) { 171 return view('modules/block-template', [ 172 'block' => Str::kebab($this->name()), 173 'id' => $block_id, 174 'config_url' => $this->configUrl($tree, $context, $block_id), 175 'title' => I18N::plural('Changes in the last %s day', 'Changes in the last %s days', $days, I18N::number($days)), 176 'content' => $content, 177 ]); 178 } 179 180 return $content; 181 } 182 183 /** 184 * Should this block load asynchronously using AJAX? 185 * 186 * Simple blocks are faster in-line, more complex ones can be loaded later. 187 * 188 * @return bool 189 */ 190 public function loadAjax(): bool 191 { 192 return true; 193 } 194 195 /** 196 * Can this block be shown on the user’s home page? 197 * 198 * @return bool 199 */ 200 public function isUserBlock(): bool 201 { 202 return true; 203 } 204 205 /** 206 * Can this block be shown on the tree’s home page? 207 * 208 * @return bool 209 */ 210 public function isTreeBlock(): bool 211 { 212 return true; 213 } 214 215 /** 216 * Update the configuration for a block. 217 * 218 * @param ServerRequestInterface $request 219 * @param int $block_id 220 * 221 * @return void 222 */ 223 public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 224 { 225 $days = Validator::parsedBody($request)->integer('days'); 226 $info_style = Validator::parsedBody($request)->string('infoStyle'); 227 $sort_style = Validator::parsedBody($request)->string('sortStyle'); 228 $show_date = Validator::parsedBody($request)->boolean('show_date'); 229 $show_user = Validator::parsedBody($request)->boolean('show_user'); 230 $source = Validator::parsedBody($request)->string('source'); 231 232 $this->setBlockSetting($block_id, 'days', (string) $days); 233 $this->setBlockSetting($block_id, 'infoStyle', $info_style); 234 $this->setBlockSetting($block_id, 'sortStyle', $sort_style); 235 $this->setBlockSetting($block_id, 'show_date', (string) $show_date); 236 $this->setBlockSetting($block_id, 'show_user', (string) $show_user); 237 $this->setBlockSetting($block_id, 'source', $source); 238 } 239 240 /** 241 * An HTML form to edit block settings 242 * 243 * @param Tree $tree 244 * @param int $block_id 245 * 246 * @return string 247 */ 248 public function editBlockConfiguration(Tree $tree, int $block_id): string 249 { 250 $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 251 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE); 252 $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE); 253 $show_date = $this->getBlockSetting($block_id, 'show_date', self::DEFAULT_SHOW_DATE); 254 $show_user = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 255 $source = $this->getBlockSetting($block_id, 'source', self::DEFAULT_SOURCE); 256 257 $info_styles = [ 258 /* I18N: An option in a list-box */ 259 'list' => I18N::translate('list'), 260 /* I18N: An option in a list-box */ 261 'table' => I18N::translate('table'), 262 ]; 263 264 $sort_styles = [ 265 /* I18N: An option in a list-box */ 266 'name' => I18N::translate('sort by name'), 267 /* I18N: An option in a list-box */ 268 'date_asc' => I18N::translate('sort by date, oldest first'), 269 /* I18N: An option in a list-box */ 270 'date_desc' => I18N::translate('sort by date, newest first'), 271 ]; 272 273 $sources = [ 274 /* I18N: An option in a list-box */ 275 self::SOURCE_DATABASE => I18N::translate('show changes made in webtrees'), 276 /* I18N: An option in a list-box */ 277 self::SOURCE_GEDCOM => I18N::translate('show changes recorded in the genealogy data'), 278 ]; 279 280 return view('modules/recent_changes/config', [ 281 'days' => $days, 282 'infoStyle' => $infoStyle, 283 'info_styles' => $info_styles, 284 'max_days' => self::MAX_DAYS, 285 'sortStyle' => $sortStyle, 286 'sort_styles' => $sort_styles, 287 'source' => $source, 288 'sources' => $sources, 289 'show_date' => $show_date, 290 'show_user' => $show_user, 291 ]); 292 } 293 294 /** 295 * Find records that have changed since a given julian day 296 * 297 * @param Tree $tree Changes for which tree 298 * @param int $days Number of days 299 * 300 * @return Collection<array-key,stdClass> List of records with changes 301 */ 302 private function getRecentChangesFromDatabase(Tree $tree, int $days): Collection 303 { 304 $subquery = DB::table('change') 305 ->where('gedcom_id', '=', $tree->id()) 306 ->where('status', '=', 'accepted') 307 ->where('new_gedcom', '<>', '') 308 ->where('change_time', '>', Registry::timestampFactory()->now()->subtractDays($days)->toDateTimeString()) 309 ->groupBy(['xref']) 310 ->select([new Expression('MAX(change_id) AS recent_change_id')]); 311 312 $query = DB::table('change') 313 ->joinSub($subquery, 'recent', 'recent_change_id', '=', 'change_id') 314 ->select(['change.*']); 315 316 return $query 317 ->get() 318 ->map(function (object $row) use ($tree): object { 319 return (object) [ 320 'record' => Registry::gedcomRecordFactory()->make($row->xref, $tree, $row->new_gedcom), 321 'time' => Registry::timestampFactory()->fromString($row->change_time), 322 'user' => $this->user_service->find((int) $row->user_id), 323 ]; 324 }) 325 ->filter(static function (object $row): bool { 326 return $row->record instanceof GedcomRecord && $row->record->canShow(); 327 }); 328 } 329 330 /** 331 * Find records that have changed since a given julian day 332 * 333 * @param Tree $tree Changes for which tree 334 * @param int $days Number of days 335 * 336 * @return Collection<array-key,stdClass> List of records with changes 337 */ 338 private function getRecentChangesFromGenealogy(Tree $tree, int $days): Collection 339 { 340 $julian_day = Registry::timestampFactory()->now()->subtractDays($days)->julianDay(); 341 342 $individuals = DB::table('dates') 343 ->where('d_file', '=', $tree->id()) 344 ->where('d_julianday1', '>=', $julian_day) 345 ->where('d_fact', '=', 'CHAN') 346 ->join('individuals', static function (JoinClause $join): void { 347 $join 348 ->on('d_file', '=', 'i_file') 349 ->on('d_gid', '=', 'i_id'); 350 }) 351 ->select(['individuals.*']) 352 ->get() 353 ->map(Registry::individualFactory()->mapper($tree)) 354 ->filter(Individual::accessFilter()); 355 356 $families = DB::table('dates') 357 ->where('d_file', '=', $tree->id()) 358 ->where('d_julianday1', '>=', $julian_day) 359 ->where('d_fact', '=', 'CHAN') 360 ->join('families', static function (JoinClause $join): void { 361 $join 362 ->on('d_file', '=', 'f_file') 363 ->on('d_gid', '=', 'f_id'); 364 }) 365 ->select(['families.*']) 366 ->get() 367 ->map(Registry::familyFactory()->mapper($tree)) 368 ->filter(Family::accessFilter()); 369 370 return $individuals->merge($families) 371 ->map(function (GedcomRecord $record): object { 372 $user = $this->user_service->findByUserName($record->lastChangeUser()); 373 374 return (object) [ 375 'record' => $record, 376 'time' => $record->lastChangeTimestamp(), 377 'user' => $user ?? new User(0, '…', '…', ''), 378 ]; 379 }); 380 } 381} 382