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 */ 3149a243cbSGreg Roachclass RecentChangesModule extends AbstractModule implements ModuleInterface, ModuleBlockInterface 32c1010edaSGreg Roach{ 3349a243cbSGreg Roach use ModuleBlockTrait; 3449a243cbSGreg Roach 3516d6367aSGreg Roach private const DEFAULT_DAYS = '7'; 3616d6367aSGreg Roach private const DEFAULT_SHOW_USER = '1'; 3716d6367aSGreg Roach private const DEFAULT_SORT_STYLE = 'date_desc'; 3816d6367aSGreg Roach private const DEFAULT_INFO_STYLE = 'table'; 3916d6367aSGreg Roach private const MAX_DAYS = 90; 408c2e8227SGreg Roach 41961ec755SGreg Roach /** 42961ec755SGreg Roach * How should this module be labelled on tabs, menus, etc.? 43961ec755SGreg Roach * 44961ec755SGreg Roach * @return string 45961ec755SGreg Roach */ 4649a243cbSGreg Roach public function title(): string 47c1010edaSGreg Roach { 48bbb76c12SGreg Roach /* I18N: Name of a module */ 49bbb76c12SGreg Roach return I18N::translate('Recent changes'); 508c2e8227SGreg Roach } 518c2e8227SGreg Roach 52961ec755SGreg Roach /** 53961ec755SGreg Roach * A sentence describing what this module does. 54961ec755SGreg Roach * 55961ec755SGreg Roach * @return string 56961ec755SGreg Roach */ 5749a243cbSGreg Roach public function description(): string 58c1010edaSGreg Roach { 59bbb76c12SGreg Roach /* I18N: Description of the “Recent changes” module */ 60bbb76c12SGreg Roach return I18N::translate('A list of records that have been updated recently.'); 618c2e8227SGreg Roach } 628c2e8227SGreg Roach 636e45321fSGreg Roach /** {@inheritdoc} */ 645f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 65c1010edaSGreg Roach { 6655664801SGreg Roach $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 676e45321fSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE); 686e45321fSGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE); 69047cb287SGreg Roach $show_user = (bool) $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 708c2e8227SGreg Roach 71c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 728c2e8227SGreg Roach 73e490cd80SGreg Roach $records = $this->getRecentChanges($tree, $days); 748c2e8227SGreg Roach 750280f44aSGreg Roach switch ($sortStyle) { 760280f44aSGreg Roach case 'name': 77c156e8f5SGreg Roach uasort($records, GedcomRecord::nameComparator()); 788c2e8227SGreg Roach break; 7980e23601SGreg Roach 800280f44aSGreg Roach case 'date_asc': 81c156e8f5SGreg Roach uasort($records, GedcomRecord::lastChangeComparator()); 828c2e8227SGreg Roach break; 8380e23601SGreg Roach 840280f44aSGreg Roach case 'date_desc': 85c156e8f5SGreg Roach uasort($records, GedcomRecord::lastChangeComparator(-1)); 868c2e8227SGreg Roach } 870280f44aSGreg Roach 880280f44aSGreg Roach if (empty($records)) { 890280f44aSGreg 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)); 900280f44aSGreg Roach } elseif ($infoStyle === 'list') { 914d2d7d1aSGreg Roach $content = view('modules/recent_changes/changes-list', [ 924d2d7d1aSGreg Roach 'records' => $records, 934d2d7d1aSGreg Roach 'show_user' => $show_user, 944d2d7d1aSGreg Roach ]); 950280f44aSGreg Roach } else { 964d2d7d1aSGreg Roach $content = view('modules/recent_changes/changes-table', [ 970280f44aSGreg Roach 'records' => $records, 980280f44aSGreg Roach 'show_user' => $show_user, 990280f44aSGreg Roach ]); 1008c2e8227SGreg Roach } 1018c2e8227SGreg Roach 1026a8879feSGreg Roach if ($ctype !== '') { 103e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 104c1010edaSGreg Roach $config_url = route('tree-page-block-edit', [ 105c1010edaSGreg Roach 'block_id' => $block_id, 106aa6f03bbSGreg Roach 'ged' => $tree->name(), 107c1010edaSGreg Roach ]); 108397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 109c1010edaSGreg Roach $config_url = route('user-page-block-edit', [ 110c1010edaSGreg Roach 'block_id' => $block_id, 111aa6f03bbSGreg Roach 'ged' => $tree->name(), 112c1010edaSGreg Roach ]); 1138cbbfdceSGreg Roach } else { 1148cbbfdceSGreg Roach $config_url = ''; 1158cbbfdceSGreg Roach } 1168cbbfdceSGreg Roach 117147e99aaSGreg Roach return view('modules/block-template', [ 11826684e68SGreg Roach 'block' => str_replace('_', '-', $this->name()), 1199c6524dcSGreg Roach 'id' => $block_id, 1208cbbfdceSGreg Roach 'config_url' => $config_url, 1218cbbfdceSGreg Roach 'title' => I18N::plural('Changes in the last %s day', 'Changes in the last %s days', $days, I18N::number($days)), 1229c6524dcSGreg Roach 'content' => $content, 1239c6524dcSGreg Roach ]); 1248c2e8227SGreg Roach } 125b2ce94c6SRico Sonntag 126b2ce94c6SRico Sonntag return $content; 1278c2e8227SGreg Roach } 1288c2e8227SGreg Roach 1298c2e8227SGreg Roach /** {@inheritdoc} */ 130c1010edaSGreg Roach public function loadAjax(): bool 131c1010edaSGreg Roach { 1328c2e8227SGreg Roach return true; 1338c2e8227SGreg Roach } 1348c2e8227SGreg Roach 1358c2e8227SGreg Roach /** {@inheritdoc} */ 136c1010edaSGreg Roach public function isUserBlock(): bool 137c1010edaSGreg Roach { 1388c2e8227SGreg Roach return true; 1398c2e8227SGreg Roach } 1408c2e8227SGreg Roach 1418c2e8227SGreg Roach /** {@inheritdoc} */ 142*63276d8fSGreg Roach public function isTreeBlock(): bool 143c1010edaSGreg Roach { 1448c2e8227SGreg Roach return true; 1458c2e8227SGreg Roach } 1468c2e8227SGreg Roach 14720ac4041SGreg Roach /** 148a45f9889SGreg Roach * Update the configuration for a block. 149a45f9889SGreg Roach * 150a45f9889SGreg Roach * @param Request $request 151a45f9889SGreg Roach * @param int $block_id 152a45f9889SGreg Roach * 153a45f9889SGreg Roach * @return void 154a45f9889SGreg Roach */ 155a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 156a45f9889SGreg Roach { 157af2b6902SGreg Roach $days = $request->get('days', self::DEFAULT_DAYS); 158af2b6902SGreg Roach 159af2b6902SGreg Roach if ((int) $days > self::MAX_DAYS || (int) $days < 1) { 160af2b6902SGreg Roach $days = self::DEFAULT_DAYS; 161af2b6902SGreg Roach } 162af2b6902SGreg Roach 163af2b6902SGreg Roach $this->setBlockSetting($block_id, 'days', $days); 164a45f9889SGreg Roach $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_INFO_STYLE)); 165a45f9889SGreg Roach $this->setBlockSetting($block_id, 'sortStyle', $request->get('sortStyle', self::DEFAULT_SORT_STYLE)); 166a45f9889SGreg Roach $this->setBlockSetting($block_id, 'show_user', $request->get('show_user', self::DEFAULT_SHOW_USER)); 167a45f9889SGreg Roach } 168a45f9889SGreg Roach 169a45f9889SGreg Roach /** 17020ac4041SGreg Roach * An HTML form to edit block settings 17120ac4041SGreg Roach * 17220ac4041SGreg Roach * @param Tree $tree 17320ac4041SGreg Roach * @param int $block_id 17420ac4041SGreg Roach * 17520ac4041SGreg Roach * @return void 17620ac4041SGreg Roach */ 177a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 178c1010edaSGreg Roach { 17955664801SGreg Roach $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 1806e45321fSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE); 1816e45321fSGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE); 1826e45321fSGreg Roach $show_user = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 1838c2e8227SGreg Roach 184c385536dSGreg Roach $info_styles = [ 185bbb76c12SGreg Roach /* I18N: An option in a list-box */ 186bbb76c12SGreg Roach 'list' => I18N::translate('list'), 187bbb76c12SGreg Roach /* I18N: An option in a list-box */ 188bbb76c12SGreg Roach 'table' => I18N::translate('table'), 189c385536dSGreg Roach ]; 1908c2e8227SGreg Roach 191c385536dSGreg Roach $sort_styles = [ 192bbb76c12SGreg Roach /* I18N: An option in a list-box */ 193bbb76c12SGreg Roach 'name' => I18N::translate('sort by name'), 194bbb76c12SGreg Roach /* I18N: An option in a list-box */ 195bbb76c12SGreg Roach 'date_asc' => I18N::translate('sort by date, oldest first'), 196bbb76c12SGreg Roach /* I18N: An option in a list-box */ 197bbb76c12SGreg Roach 'date_desc' => I18N::translate('sort by date, newest first'), 198c385536dSGreg Roach ]; 1998c2e8227SGreg Roach 200147e99aaSGreg Roach echo view('modules/recent_changes/config', [ 201c385536dSGreg Roach 'days' => $days, 202c385536dSGreg Roach 'infoStyle' => $infoStyle, 203c385536dSGreg Roach 'info_styles' => $info_styles, 204c385536dSGreg Roach 'max_days' => self::MAX_DAYS, 205c385536dSGreg Roach 'sortStyle' => $sortStyle, 206c385536dSGreg Roach 'sort_styles' => $sort_styles, 207c385536dSGreg Roach 'show_user' => $show_user, 208c385536dSGreg Roach ]); 2098c2e8227SGreg Roach } 2108c2e8227SGreg Roach 2116e45321fSGreg Roach /** 2126e45321fSGreg Roach * Find records that have changed since a given julian day 2136e45321fSGreg Roach * 2146e45321fSGreg Roach * @param Tree $tree Changes for which tree 21524319d9dSGreg Roach * @param int $days Number of days 2166e45321fSGreg Roach * 2176e45321fSGreg Roach * @return GedcomRecord[] List of records with changes 2186e45321fSGreg Roach */ 21955664801SGreg Roach private function getRecentChanges(Tree $tree, int $days): array 220c1010edaSGreg Roach { 22177654037SGreg Roach return DB::table('change') 22277654037SGreg Roach ->where('gedcom_id', '=', $tree->id()) 22377654037SGreg Roach ->where('status', '=', 'accepted') 22477654037SGreg Roach ->where('new_gedcom', '<>', '') 22577654037SGreg Roach ->where('change_time', '>', Carbon::now()->subDays($days)) 22677654037SGreg Roach ->groupBy('xref') 22777654037SGreg Roach ->pluck('xref') 22877654037SGreg Roach ->map(function (string $xref) use ($tree): ?GedcomRecord { 22977654037SGreg Roach return GedcomRecord::getInstance($xref, $tree); 23077654037SGreg Roach }) 23177654037SGreg Roach ->filter(function (?GedcomRecord $record): bool { 23277654037SGreg Roach return $record instanceof GedcomRecord && $record->canShow(); 23377654037SGreg Roach }) 23477654037SGreg Roach ->all(); 2356e45321fSGreg Roach } 2368c2e8227SGreg Roach} 237