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 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 214459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon; 226e45321fSGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 246e45321fSGreg Roachuse Fisharebest\Webtrees\Tree; 2577654037SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 261e7a7a28SGreg Roachuse Illuminate\Support\Str; 27a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request; 288c2e8227SGreg Roach 298c2e8227SGreg Roach/** 308c2e8227SGreg Roach * Class RecentChangesModule 318c2e8227SGreg Roach */ 3237eb8894SGreg Roachclass RecentChangesModule extends AbstractModule implements ModuleBlockInterface 33c1010edaSGreg Roach{ 3449a243cbSGreg Roach use ModuleBlockTrait; 3549a243cbSGreg Roach 3616d6367aSGreg Roach private const DEFAULT_DAYS = '7'; 3716d6367aSGreg Roach private const DEFAULT_SHOW_USER = '1'; 3816d6367aSGreg Roach private const DEFAULT_SORT_STYLE = 'date_desc'; 3916d6367aSGreg Roach private const DEFAULT_INFO_STYLE = 'table'; 4016d6367aSGreg Roach private const MAX_DAYS = 90; 418c2e8227SGreg Roach 42961ec755SGreg Roach /** 430cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 44961ec755SGreg Roach * 45961ec755SGreg Roach * @return string 46961ec755SGreg Roach */ 4749a243cbSGreg Roach public function title(): string 48c1010edaSGreg Roach { 49bbb76c12SGreg Roach /* I18N: Name of a module */ 50bbb76c12SGreg Roach return I18N::translate('Recent changes'); 518c2e8227SGreg Roach } 528c2e8227SGreg Roach 53961ec755SGreg Roach /** 54961ec755SGreg Roach * A sentence describing what this module does. 55961ec755SGreg Roach * 56961ec755SGreg Roach * @return string 57961ec755SGreg Roach */ 5849a243cbSGreg Roach public function description(): string 59c1010edaSGreg Roach { 60bbb76c12SGreg Roach /* I18N: Description of the “Recent changes” module */ 61bbb76c12SGreg Roach return I18N::translate('A list of records that have been updated recently.'); 628c2e8227SGreg Roach } 638c2e8227SGreg Roach 646e45321fSGreg Roach /** {@inheritdoc} */ 655f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 66c1010edaSGreg Roach { 6755664801SGreg Roach $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 686e45321fSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE); 696e45321fSGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE); 70047cb287SGreg Roach $show_user = (bool) $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 718c2e8227SGreg Roach 72c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 738c2e8227SGreg Roach 74e490cd80SGreg Roach $records = $this->getRecentChanges($tree, $days); 758c2e8227SGreg Roach 760280f44aSGreg Roach switch ($sortStyle) { 770280f44aSGreg Roach case 'name': 78c156e8f5SGreg Roach uasort($records, GedcomRecord::nameComparator()); 798c2e8227SGreg Roach break; 8080e23601SGreg Roach 810280f44aSGreg Roach case 'date_asc': 82c156e8f5SGreg Roach uasort($records, GedcomRecord::lastChangeComparator()); 838c2e8227SGreg Roach break; 8480e23601SGreg Roach 850280f44aSGreg Roach case 'date_desc': 86c156e8f5SGreg Roach uasort($records, GedcomRecord::lastChangeComparator(-1)); 878c2e8227SGreg Roach } 880280f44aSGreg Roach 890280f44aSGreg Roach if (empty($records)) { 900280f44aSGreg 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)); 910280f44aSGreg Roach } elseif ($infoStyle === 'list') { 924d2d7d1aSGreg Roach $content = view('modules/recent_changes/changes-list', [ 934d2d7d1aSGreg Roach 'records' => $records, 944d2d7d1aSGreg Roach 'show_user' => $show_user, 954d2d7d1aSGreg Roach ]); 960280f44aSGreg Roach } else { 974d2d7d1aSGreg Roach $content = view('modules/recent_changes/changes-table', [ 980280f44aSGreg Roach 'records' => $records, 990280f44aSGreg Roach 'show_user' => $show_user, 1000280f44aSGreg Roach ]); 1018c2e8227SGreg Roach } 1028c2e8227SGreg Roach 1036a8879feSGreg Roach if ($ctype !== '') { 104e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 105c1010edaSGreg Roach $config_url = route('tree-page-block-edit', [ 106c1010edaSGreg Roach 'block_id' => $block_id, 107aa6f03bbSGreg Roach 'ged' => $tree->name(), 108c1010edaSGreg Roach ]); 109397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 110c1010edaSGreg Roach $config_url = route('user-page-block-edit', [ 111c1010edaSGreg Roach 'block_id' => $block_id, 112aa6f03bbSGreg Roach 'ged' => $tree->name(), 113c1010edaSGreg Roach ]); 1148cbbfdceSGreg Roach } else { 1158cbbfdceSGreg Roach $config_url = ''; 1168cbbfdceSGreg Roach } 1178cbbfdceSGreg Roach 118147e99aaSGreg Roach return view('modules/block-template', [ 1191e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1209c6524dcSGreg Roach 'id' => $block_id, 1218cbbfdceSGreg Roach 'config_url' => $config_url, 1228cbbfdceSGreg Roach 'title' => I18N::plural('Changes in the last %s day', 'Changes in the last %s days', $days, I18N::number($days)), 1239c6524dcSGreg Roach 'content' => $content, 1249c6524dcSGreg Roach ]); 1258c2e8227SGreg Roach } 126b2ce94c6SRico Sonntag 127b2ce94c6SRico Sonntag return $content; 1288c2e8227SGreg Roach } 1298c2e8227SGreg Roach 1308c2e8227SGreg Roach /** {@inheritdoc} */ 131c1010edaSGreg Roach public function loadAjax(): bool 132c1010edaSGreg Roach { 1338c2e8227SGreg Roach return true; 1348c2e8227SGreg Roach } 1358c2e8227SGreg Roach 1368c2e8227SGreg Roach /** {@inheritdoc} */ 137c1010edaSGreg Roach public function isUserBlock(): bool 138c1010edaSGreg Roach { 1398c2e8227SGreg Roach return true; 1408c2e8227SGreg Roach } 1418c2e8227SGreg Roach 1428c2e8227SGreg Roach /** {@inheritdoc} */ 14363276d8fSGreg Roach public function isTreeBlock(): bool 144c1010edaSGreg Roach { 1458c2e8227SGreg Roach return true; 1468c2e8227SGreg Roach } 1478c2e8227SGreg Roach 14820ac4041SGreg Roach /** 149a45f9889SGreg Roach * Update the configuration for a block. 150a45f9889SGreg Roach * 151a45f9889SGreg Roach * @param Request $request 152a45f9889SGreg Roach * @param int $block_id 153a45f9889SGreg Roach * 154a45f9889SGreg Roach * @return void 155a45f9889SGreg Roach */ 156e364afe4SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id): void 157a45f9889SGreg Roach { 158af2b6902SGreg Roach $days = $request->get('days', self::DEFAULT_DAYS); 159af2b6902SGreg Roach 160af2b6902SGreg Roach if ((int) $days > self::MAX_DAYS || (int) $days < 1) { 161af2b6902SGreg Roach $days = self::DEFAULT_DAYS; 162af2b6902SGreg Roach } 163af2b6902SGreg Roach 164af2b6902SGreg Roach $this->setBlockSetting($block_id, 'days', $days); 165a45f9889SGreg Roach $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_INFO_STYLE)); 166a45f9889SGreg Roach $this->setBlockSetting($block_id, 'sortStyle', $request->get('sortStyle', self::DEFAULT_SORT_STYLE)); 167a45f9889SGreg Roach $this->setBlockSetting($block_id, 'show_user', $request->get('show_user', self::DEFAULT_SHOW_USER)); 168a45f9889SGreg Roach } 169a45f9889SGreg Roach 170a45f9889SGreg Roach /** 17120ac4041SGreg Roach * An HTML form to edit block settings 17220ac4041SGreg Roach * 17320ac4041SGreg Roach * @param Tree $tree 17420ac4041SGreg Roach * @param int $block_id 17520ac4041SGreg Roach * 17620ac4041SGreg Roach * @return void 17720ac4041SGreg Roach */ 178e364afe4SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): void 179c1010edaSGreg Roach { 18055664801SGreg Roach $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 1816e45321fSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE); 1826e45321fSGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE); 1836e45321fSGreg Roach $show_user = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 1848c2e8227SGreg Roach 185c385536dSGreg Roach $info_styles = [ 186bbb76c12SGreg Roach /* I18N: An option in a list-box */ 187bbb76c12SGreg Roach 'list' => I18N::translate('list'), 188bbb76c12SGreg Roach /* I18N: An option in a list-box */ 189bbb76c12SGreg Roach 'table' => I18N::translate('table'), 190c385536dSGreg Roach ]; 1918c2e8227SGreg Roach 192c385536dSGreg Roach $sort_styles = [ 193bbb76c12SGreg Roach /* I18N: An option in a list-box */ 194bbb76c12SGreg Roach 'name' => I18N::translate('sort by name'), 195bbb76c12SGreg Roach /* I18N: An option in a list-box */ 196bbb76c12SGreg Roach 'date_asc' => I18N::translate('sort by date, oldest first'), 197bbb76c12SGreg Roach /* I18N: An option in a list-box */ 198bbb76c12SGreg Roach 'date_desc' => I18N::translate('sort by date, newest first'), 199c385536dSGreg Roach ]; 2008c2e8227SGreg Roach 201147e99aaSGreg Roach echo view('modules/recent_changes/config', [ 202c385536dSGreg Roach 'days' => $days, 203c385536dSGreg Roach 'infoStyle' => $infoStyle, 204c385536dSGreg Roach 'info_styles' => $info_styles, 205c385536dSGreg Roach 'max_days' => self::MAX_DAYS, 206c385536dSGreg Roach 'sortStyle' => $sortStyle, 207c385536dSGreg Roach 'sort_styles' => $sort_styles, 208c385536dSGreg Roach 'show_user' => $show_user, 209c385536dSGreg Roach ]); 2108c2e8227SGreg Roach } 2118c2e8227SGreg Roach 2126e45321fSGreg Roach /** 2136e45321fSGreg Roach * Find records that have changed since a given julian day 2146e45321fSGreg Roach * 2156e45321fSGreg Roach * @param Tree $tree Changes for which tree 21624319d9dSGreg Roach * @param int $days Number of days 2176e45321fSGreg Roach * 2186e45321fSGreg Roach * @return GedcomRecord[] List of records with changes 2196e45321fSGreg Roach */ 22055664801SGreg Roach private function getRecentChanges(Tree $tree, int $days): array 221c1010edaSGreg Roach { 22277654037SGreg Roach return DB::table('change') 22377654037SGreg Roach ->where('gedcom_id', '=', $tree->id()) 22477654037SGreg Roach ->where('status', '=', 'accepted') 22577654037SGreg Roach ->where('new_gedcom', '<>', '') 22677654037SGreg Roach ->where('change_time', '>', Carbon::now()->subDays($days)) 22777654037SGreg Roach ->groupBy('xref') 22877654037SGreg Roach ->pluck('xref') 229*0b5fd0a6SGreg Roach ->map(static function (string $xref) use ($tree): ?GedcomRecord { 23077654037SGreg Roach return GedcomRecord::getInstance($xref, $tree); 23177654037SGreg Roach }) 232*0b5fd0a6SGreg Roach ->filter(static function (?GedcomRecord $record): bool { 23377654037SGreg Roach return $record instanceof GedcomRecord && $record->canShow(); 23477654037SGreg Roach }) 23577654037SGreg Roach ->all(); 2366e45321fSGreg Roach } 2378c2e8227SGreg Roach} 238