18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 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 */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 1976692c8bSGreg Roach 2077654037SGreg Roachuse Carbon\Carbon; 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 226e45321fSGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 246e45321fSGreg Roachuse Fisharebest\Webtrees\Tree; 2577654037SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 26a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request; 278c2e8227SGreg Roach 288c2e8227SGreg Roach/** 298c2e8227SGreg Roach * Class RecentChangesModule 308c2e8227SGreg Roach */ 31c1010edaSGreg Roachclass RecentChangesModule extends AbstractModule implements ModuleBlockInterface 32c1010edaSGreg Roach{ 3316d6367aSGreg Roach private const DEFAULT_DAYS = '7'; 3416d6367aSGreg Roach private const DEFAULT_SHOW_USER = '1'; 3516d6367aSGreg Roach private const DEFAULT_SORT_STYLE = 'date_desc'; 3616d6367aSGreg Roach private const DEFAULT_INFO_STYLE = 'table'; 3716d6367aSGreg Roach private const MAX_DAYS = 90; 388c2e8227SGreg Roach 398c2e8227SGreg Roach /** {@inheritdoc} */ 408f53f488SRico Sonntag public function getTitle(): string 41c1010edaSGreg Roach { 42bbb76c12SGreg Roach /* I18N: Name of a module */ 43bbb76c12SGreg Roach return I18N::translate('Recent changes'); 448c2e8227SGreg Roach } 458c2e8227SGreg Roach 468c2e8227SGreg Roach /** {@inheritdoc} */ 478f53f488SRico Sonntag public function getDescription(): string 48c1010edaSGreg Roach { 49bbb76c12SGreg Roach /* I18N: Description of the “Recent changes” module */ 50bbb76c12SGreg Roach return I18N::translate('A list of records that have been updated recently.'); 518c2e8227SGreg Roach } 528c2e8227SGreg Roach 536e45321fSGreg Roach /** {@inheritdoc} */ 545f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 55c1010edaSGreg Roach { 5655664801SGreg Roach $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 576e45321fSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE); 586e45321fSGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE); 59047cb287SGreg Roach $show_user = (bool) $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 608c2e8227SGreg Roach 61c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 628c2e8227SGreg Roach 63e490cd80SGreg Roach $records = $this->getRecentChanges($tree, $days); 648c2e8227SGreg Roach 650280f44aSGreg Roach switch ($sortStyle) { 660280f44aSGreg Roach case 'name': 67*c156e8f5SGreg Roach uasort($records, GedcomRecord::nameComparator()); 688c2e8227SGreg Roach break; 6980e23601SGreg Roach 700280f44aSGreg Roach case 'date_asc': 71*c156e8f5SGreg Roach uasort($records, GedcomRecord::lastChangeComparator()); 728c2e8227SGreg Roach break; 7380e23601SGreg Roach 740280f44aSGreg Roach case 'date_desc': 75*c156e8f5SGreg Roach uasort($records, GedcomRecord::lastChangeComparator(-1)); 768c2e8227SGreg Roach } 770280f44aSGreg Roach 780280f44aSGreg Roach if (empty($records)) { 790280f44aSGreg 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)); 800280f44aSGreg Roach } elseif ($infoStyle === 'list') { 814d2d7d1aSGreg Roach $content = view('modules/recent_changes/changes-list', [ 824d2d7d1aSGreg Roach 'records' => $records, 834d2d7d1aSGreg Roach 'show_user' => $show_user, 844d2d7d1aSGreg Roach ]); 850280f44aSGreg Roach } else { 864d2d7d1aSGreg Roach $content = view('modules/recent_changes/changes-table', [ 870280f44aSGreg Roach 'records' => $records, 880280f44aSGreg Roach 'show_user' => $show_user, 890280f44aSGreg Roach ]); 908c2e8227SGreg Roach } 918c2e8227SGreg Roach 926a8879feSGreg Roach if ($ctype !== '') { 93e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 94c1010edaSGreg Roach $config_url = route('tree-page-block-edit', [ 95c1010edaSGreg Roach 'block_id' => $block_id, 96aa6f03bbSGreg Roach 'ged' => $tree->name(), 97c1010edaSGreg Roach ]); 98397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 99c1010edaSGreg Roach $config_url = route('user-page-block-edit', [ 100c1010edaSGreg Roach 'block_id' => $block_id, 101aa6f03bbSGreg Roach 'ged' => $tree->name(), 102c1010edaSGreg Roach ]); 1038cbbfdceSGreg Roach } else { 1048cbbfdceSGreg Roach $config_url = ''; 1058cbbfdceSGreg Roach } 1068cbbfdceSGreg Roach 107147e99aaSGreg Roach return view('modules/block-template', [ 1089c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1099c6524dcSGreg Roach 'id' => $block_id, 1108cbbfdceSGreg Roach 'config_url' => $config_url, 1118cbbfdceSGreg Roach 'title' => I18N::plural('Changes in the last %s day', 'Changes in the last %s days', $days, I18N::number($days)), 1129c6524dcSGreg Roach 'content' => $content, 1139c6524dcSGreg Roach ]); 1148c2e8227SGreg Roach } 115b2ce94c6SRico Sonntag 116b2ce94c6SRico Sonntag return $content; 1178c2e8227SGreg Roach } 1188c2e8227SGreg Roach 1198c2e8227SGreg Roach /** {@inheritdoc} */ 120c1010edaSGreg Roach public function loadAjax(): bool 121c1010edaSGreg Roach { 1228c2e8227SGreg Roach return true; 1238c2e8227SGreg Roach } 1248c2e8227SGreg Roach 1258c2e8227SGreg Roach /** {@inheritdoc} */ 126c1010edaSGreg Roach public function isUserBlock(): bool 127c1010edaSGreg Roach { 1288c2e8227SGreg Roach return true; 1298c2e8227SGreg Roach } 1308c2e8227SGreg Roach 1318c2e8227SGreg Roach /** {@inheritdoc} */ 132c1010edaSGreg Roach public function isGedcomBlock(): bool 133c1010edaSGreg Roach { 1348c2e8227SGreg Roach return true; 1358c2e8227SGreg Roach } 1368c2e8227SGreg Roach 13720ac4041SGreg Roach /** 138a45f9889SGreg Roach * Update the configuration for a block. 139a45f9889SGreg Roach * 140a45f9889SGreg Roach * @param Request $request 141a45f9889SGreg Roach * @param int $block_id 142a45f9889SGreg Roach * 143a45f9889SGreg Roach * @return void 144a45f9889SGreg Roach */ 145a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 146a45f9889SGreg Roach { 147af2b6902SGreg Roach $days = $request->get('days', self::DEFAULT_DAYS); 148af2b6902SGreg Roach 149af2b6902SGreg Roach if ((int) $days > self::MAX_DAYS || (int) $days < 1) { 150af2b6902SGreg Roach $days = self::DEFAULT_DAYS; 151af2b6902SGreg Roach } 152af2b6902SGreg Roach 153af2b6902SGreg Roach $this->setBlockSetting($block_id, 'days', $days); 154a45f9889SGreg Roach $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_INFO_STYLE)); 155a45f9889SGreg Roach $this->setBlockSetting($block_id, 'sortStyle', $request->get('sortStyle', self::DEFAULT_SORT_STYLE)); 156a45f9889SGreg Roach $this->setBlockSetting($block_id, 'show_user', $request->get('show_user', self::DEFAULT_SHOW_USER)); 157a45f9889SGreg Roach } 158a45f9889SGreg Roach 159a45f9889SGreg Roach /** 16020ac4041SGreg Roach * An HTML form to edit block settings 16120ac4041SGreg Roach * 16220ac4041SGreg Roach * @param Tree $tree 16320ac4041SGreg Roach * @param int $block_id 16420ac4041SGreg Roach * 16520ac4041SGreg Roach * @return void 16620ac4041SGreg Roach */ 167a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 168c1010edaSGreg Roach { 16955664801SGreg Roach $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 1706e45321fSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE); 1716e45321fSGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE); 1726e45321fSGreg Roach $show_user = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 1738c2e8227SGreg Roach 174c385536dSGreg Roach $info_styles = [ 175bbb76c12SGreg Roach /* I18N: An option in a list-box */ 176bbb76c12SGreg Roach 'list' => I18N::translate('list'), 177bbb76c12SGreg Roach /* I18N: An option in a list-box */ 178bbb76c12SGreg Roach 'table' => I18N::translate('table'), 179c385536dSGreg Roach ]; 1808c2e8227SGreg Roach 181c385536dSGreg Roach $sort_styles = [ 182bbb76c12SGreg Roach /* I18N: An option in a list-box */ 183bbb76c12SGreg Roach 'name' => I18N::translate('sort by name'), 184bbb76c12SGreg Roach /* I18N: An option in a list-box */ 185bbb76c12SGreg Roach 'date_asc' => I18N::translate('sort by date, oldest first'), 186bbb76c12SGreg Roach /* I18N: An option in a list-box */ 187bbb76c12SGreg Roach 'date_desc' => I18N::translate('sort by date, newest first'), 188c385536dSGreg Roach ]; 1898c2e8227SGreg Roach 190147e99aaSGreg Roach echo view('modules/recent_changes/config', [ 191c385536dSGreg Roach 'days' => $days, 192c385536dSGreg Roach 'infoStyle' => $infoStyle, 193c385536dSGreg Roach 'info_styles' => $info_styles, 194c385536dSGreg Roach 'max_days' => self::MAX_DAYS, 195c385536dSGreg Roach 'sortStyle' => $sortStyle, 196c385536dSGreg Roach 'sort_styles' => $sort_styles, 197c385536dSGreg Roach 'show_user' => $show_user, 198c385536dSGreg Roach ]); 1998c2e8227SGreg Roach } 2008c2e8227SGreg Roach 2016e45321fSGreg Roach /** 2026e45321fSGreg Roach * Find records that have changed since a given julian day 2036e45321fSGreg Roach * 2046e45321fSGreg Roach * @param Tree $tree Changes for which tree 20524319d9dSGreg Roach * @param int $days Number of days 2066e45321fSGreg Roach * 2076e45321fSGreg Roach * @return GedcomRecord[] List of records with changes 2086e45321fSGreg Roach */ 20955664801SGreg Roach private function getRecentChanges(Tree $tree, int $days): array 210c1010edaSGreg Roach { 21177654037SGreg Roach return DB::table('change') 21277654037SGreg Roach ->where('gedcom_id', '=', $tree->id()) 21377654037SGreg Roach ->where('status', '=', 'accepted') 21477654037SGreg Roach ->where('new_gedcom', '<>', '') 21577654037SGreg Roach ->where('change_time', '>', Carbon::now()->subDays($days)) 21677654037SGreg Roach ->groupBy('xref') 21777654037SGreg Roach ->pluck('xref') 21877654037SGreg Roach ->map(function (string $xref) use ($tree): ?GedcomRecord { 21977654037SGreg Roach return GedcomRecord::getInstance($xref, $tree); 22077654037SGreg Roach }) 22177654037SGreg Roach ->filter(function (?GedcomRecord $record): bool { 22277654037SGreg Roach return $record instanceof GedcomRecord && $record->canShow(); 22377654037SGreg Roach }) 22477654037SGreg Roach ->all(); 2256e45321fSGreg Roach } 2268c2e8227SGreg Roach} 227