18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 41062a142SGreg Roach * Copyright (C) 2018 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 */ 1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 1776692c8bSGreg Roach 180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 196e45321fSGreg Roachuse Fisharebest\Webtrees\Database; 206e45321fSGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 226e45321fSGreg Roachuse Fisharebest\Webtrees\Tree; 23a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request; 248c2e8227SGreg Roach 258c2e8227SGreg Roach/** 268c2e8227SGreg Roach * Class RecentChangesModule 278c2e8227SGreg Roach */ 28c1010edaSGreg Roachclass RecentChangesModule extends AbstractModule implements ModuleBlockInterface 29c1010edaSGreg Roach{ 306e45321fSGreg Roach const DEFAULT_BLOCK = '1'; 318c2e8227SGreg Roach const DEFAULT_DAYS = 7; 326e45321fSGreg Roach const DEFAULT_HIDE_EMPTY = '0'; 336e45321fSGreg Roach const DEFAULT_SHOW_USER = '1'; 346e45321fSGreg Roach const DEFAULT_SORT_STYLE = 'date_desc'; 356e45321fSGreg Roach const DEFAULT_INFO_STYLE = 'table'; 368c2e8227SGreg Roach const MAX_DAYS = 90; 378c2e8227SGreg Roach 388c2e8227SGreg Roach /** {@inheritdoc} */ 39*8f53f488SRico Sonntag public function getTitle(): string 40c1010edaSGreg Roach { 41bbb76c12SGreg Roach /* I18N: Name of a module */ 42bbb76c12SGreg Roach return I18N::translate('Recent changes'); 438c2e8227SGreg Roach } 448c2e8227SGreg Roach 458c2e8227SGreg Roach /** {@inheritdoc} */ 46*8f53f488SRico Sonntag public function getDescription(): string 47c1010edaSGreg Roach { 48bbb76c12SGreg Roach /* I18N: Description of the “Recent changes” module */ 49bbb76c12SGreg Roach return I18N::translate('A list of records that have been updated recently.'); 508c2e8227SGreg Roach } 518c2e8227SGreg Roach 526e45321fSGreg Roach /** {@inheritdoc} */ 53c1010edaSGreg Roach public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string 54c1010edaSGreg Roach { 55e490cd80SGreg Roach global $ctype; 568c2e8227SGreg Roach 57e2a378d3SGreg Roach $days = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 586e45321fSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE); 596e45321fSGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE); 60047cb287SGreg Roach $show_user = (bool)$this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 618c2e8227SGreg Roach 62c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 638c2e8227SGreg Roach 64e490cd80SGreg Roach $records = $this->getRecentChanges($tree, $days); 658c2e8227SGreg Roach 660280f44aSGreg Roach switch ($sortStyle) { 670280f44aSGreg Roach case 'name': 68c1010edaSGreg Roach uasort($records, [ 69c1010edaSGreg Roach 'self', 70c1010edaSGreg Roach 'sortByNameAndChangeDate', 71c1010edaSGreg Roach ]); 728c2e8227SGreg Roach break; 730280f44aSGreg Roach case 'date_asc': 74c1010edaSGreg Roach uasort($records, [ 75c1010edaSGreg Roach 'self', 76c1010edaSGreg Roach 'sortByChangeDateAndName', 77c1010edaSGreg Roach ]); 780280f44aSGreg Roach $records = array_reverse($records); 798c2e8227SGreg Roach break; 800280f44aSGreg Roach case 'date_desc': 81c1010edaSGreg Roach uasort($records, [ 82c1010edaSGreg Roach 'self', 83c1010edaSGreg Roach 'sortByChangeDateAndName', 84c1010edaSGreg Roach ]); 858c2e8227SGreg Roach } 860280f44aSGreg Roach 870280f44aSGreg Roach if (empty($records)) { 880280f44aSGreg 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)); 890280f44aSGreg Roach } elseif ($infoStyle === 'list') { 904d2d7d1aSGreg Roach $content = view('modules/recent_changes/changes-list', [ 914d2d7d1aSGreg Roach 'records' => $records, 924d2d7d1aSGreg Roach 'show_user' => $show_user, 934d2d7d1aSGreg Roach ]); 940280f44aSGreg Roach } else { 954d2d7d1aSGreg Roach $content = view('modules/recent_changes/changes-table', [ 960280f44aSGreg Roach 'records' => $records, 970280f44aSGreg Roach 'show_user' => $show_user, 980280f44aSGreg Roach ]); 998c2e8227SGreg Roach } 1008c2e8227SGreg Roach 1018c2e8227SGreg Roach if ($template) { 102e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 103c1010edaSGreg Roach $config_url = route('tree-page-block-edit', [ 104c1010edaSGreg Roach 'block_id' => $block_id, 105c1010edaSGreg Roach 'ged' => $tree->getName(), 106c1010edaSGreg Roach ]); 107397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 108c1010edaSGreg Roach $config_url = route('user-page-block-edit', [ 109c1010edaSGreg Roach 'block_id' => $block_id, 110c1010edaSGreg Roach 'ged' => $tree->getName(), 111c1010edaSGreg Roach ]); 1128cbbfdceSGreg Roach } else { 1138cbbfdceSGreg Roach $config_url = ''; 1148cbbfdceSGreg Roach } 1158cbbfdceSGreg Roach 116147e99aaSGreg Roach return view('modules/block-template', [ 1179c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1189c6524dcSGreg Roach 'id' => $block_id, 1198cbbfdceSGreg Roach 'config_url' => $config_url, 1208cbbfdceSGreg Roach 'title' => I18N::plural('Changes in the last %s day', 'Changes in the last %s days', $days, I18N::number($days)), 1219c6524dcSGreg Roach 'content' => $content, 1229c6524dcSGreg Roach ]); 1238c2e8227SGreg Roach } else { 1248c2e8227SGreg Roach return $content; 1258c2e8227SGreg Roach } 1268c2e8227SGreg Roach } 1278c2e8227SGreg Roach 1288c2e8227SGreg Roach /** {@inheritdoc} */ 129c1010edaSGreg Roach public function loadAjax(): bool 130c1010edaSGreg Roach { 1318c2e8227SGreg Roach return true; 1328c2e8227SGreg Roach } 1338c2e8227SGreg Roach 1348c2e8227SGreg Roach /** {@inheritdoc} */ 135c1010edaSGreg Roach public function isUserBlock(): bool 136c1010edaSGreg Roach { 1378c2e8227SGreg Roach return true; 1388c2e8227SGreg Roach } 1398c2e8227SGreg Roach 1408c2e8227SGreg Roach /** {@inheritdoc} */ 141c1010edaSGreg Roach public function isGedcomBlock(): bool 142c1010edaSGreg Roach { 1438c2e8227SGreg Roach return true; 1448c2e8227SGreg Roach } 1458c2e8227SGreg Roach 14620ac4041SGreg Roach /** 147a45f9889SGreg Roach * Update the configuration for a block. 148a45f9889SGreg Roach * 149a45f9889SGreg Roach * @param Request $request 150a45f9889SGreg Roach * @param int $block_id 151a45f9889SGreg Roach * 152a45f9889SGreg Roach * @return void 153a45f9889SGreg Roach */ 154a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 155a45f9889SGreg Roach { 156a45f9889SGreg Roach $this->setBlockSetting($block_id, 'days', $request->get('days', self::DEFAULT_DAYS)); 157a45f9889SGreg Roach $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_INFO_STYLE)); 158a45f9889SGreg Roach $this->setBlockSetting($block_id, 'sortStyle', $request->get('sortStyle', self::DEFAULT_SORT_STYLE)); 159a45f9889SGreg Roach $this->setBlockSetting($block_id, 'show_user', $request->get('show_user', self::DEFAULT_SHOW_USER)); 160a45f9889SGreg Roach } 161a45f9889SGreg Roach 162a45f9889SGreg Roach /** 16320ac4041SGreg Roach * An HTML form to edit block settings 16420ac4041SGreg Roach * 16520ac4041SGreg Roach * @param Tree $tree 16620ac4041SGreg Roach * @param int $block_id 16720ac4041SGreg Roach * 16820ac4041SGreg Roach * @return void 16920ac4041SGreg Roach */ 170a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 171c1010edaSGreg Roach { 172e2a378d3SGreg Roach $days = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 1736e45321fSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE); 1746e45321fSGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE); 1756e45321fSGreg Roach $show_user = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 1768c2e8227SGreg Roach 177c385536dSGreg Roach $info_styles = [ 178bbb76c12SGreg Roach /* I18N: An option in a list-box */ 179bbb76c12SGreg Roach 'list' => I18N::translate('list'), 180bbb76c12SGreg Roach /* I18N: An option in a list-box */ 181bbb76c12SGreg Roach 'table' => I18N::translate('table'), 182c385536dSGreg Roach ]; 1838c2e8227SGreg Roach 184c385536dSGreg Roach $sort_styles = [ 185bbb76c12SGreg Roach /* I18N: An option in a list-box */ 186bbb76c12SGreg Roach 'name' => I18N::translate('sort by name'), 187bbb76c12SGreg Roach /* I18N: An option in a list-box */ 188bbb76c12SGreg Roach 'date_asc' => I18N::translate('sort by date, oldest first'), 189bbb76c12SGreg Roach /* I18N: An option in a list-box */ 190bbb76c12SGreg Roach 'date_desc' => I18N::translate('sort by date, newest first'), 191c385536dSGreg Roach ]; 1928c2e8227SGreg Roach 193147e99aaSGreg Roach echo view('modules/recent_changes/config', [ 194c385536dSGreg Roach 'days' => $days, 195c385536dSGreg Roach 'infoStyle' => $infoStyle, 196c385536dSGreg Roach 'info_styles' => $info_styles, 197c385536dSGreg Roach 'max_days' => self::MAX_DAYS, 198c385536dSGreg Roach 'sortStyle' => $sortStyle, 199c385536dSGreg Roach 'sort_styles' => $sort_styles, 200c385536dSGreg Roach 'show_user' => $show_user, 201c385536dSGreg Roach ]); 2028c2e8227SGreg Roach } 2038c2e8227SGreg Roach 2046e45321fSGreg Roach /** 2056e45321fSGreg Roach * Find records that have changed since a given julian day 2066e45321fSGreg Roach * 2076e45321fSGreg Roach * @param Tree $tree Changes for which tree 20824319d9dSGreg Roach * @param int $days Number of days 2096e45321fSGreg Roach * 2106e45321fSGreg Roach * @return GedcomRecord[] List of records with changes 2116e45321fSGreg Roach */ 212*8f53f488SRico Sonntag private function getRecentChanges(Tree $tree, $days): array 213c1010edaSGreg Roach { 2146e45321fSGreg Roach $sql = 21524319d9dSGreg Roach "SELECT xref FROM `##change`" . 216b03b6f21SGreg Roach " WHERE new_gedcom != '' AND change_time > DATE_SUB(NOW(), INTERVAL :days DAY) AND gedcom_id = :tree_id" . 21724319d9dSGreg Roach " GROUP BY xref" . 21824319d9dSGreg Roach " ORDER BY MAX(change_id) DESC"; 2196e45321fSGreg Roach 22013abd6f3SGreg Roach $vars = [ 22124319d9dSGreg Roach 'days' => $days, 2226e45321fSGreg Roach 'tree_id' => $tree->getTreeId(), 22313abd6f3SGreg Roach ]; 2246e45321fSGreg Roach 2256e45321fSGreg Roach $xrefs = Database::prepare($sql)->execute($vars)->fetchOneColumn(); 2266e45321fSGreg Roach 22713abd6f3SGreg Roach $records = []; 2286e45321fSGreg Roach foreach ($xrefs as $xref) { 2296e45321fSGreg Roach $record = GedcomRecord::getInstance($xref, $tree); 2308e8029e5SRico Sonntag if ($record && $record->canShow()) { 2316e45321fSGreg Roach $records[] = $record; 2326e45321fSGreg Roach } 2336e45321fSGreg Roach } 2346e45321fSGreg Roach 2356e45321fSGreg Roach return $records; 2366e45321fSGreg Roach } 2376e45321fSGreg Roach 2386e45321fSGreg Roach /** 2396e45321fSGreg Roach * Sort the records by (1) last change date and (2) name 2406e45321fSGreg Roach * 2416e45321fSGreg Roach * @param GedcomRecord $a 2426e45321fSGreg Roach * @param GedcomRecord $b 2436e45321fSGreg Roach * 2446e45321fSGreg Roach * @return int 2456e45321fSGreg Roach */ 246*8f53f488SRico Sonntag private static function sortByChangeDateAndName(GedcomRecord $a, GedcomRecord $b): int 247c1010edaSGreg Roach { 2486e45321fSGreg Roach return $b->lastChangeTimestamp(true) - $a->lastChangeTimestamp(true) ?: GedcomRecord::compare($a, $b); 2496e45321fSGreg Roach } 2506e45321fSGreg Roach 2516e45321fSGreg Roach /** 2526e45321fSGreg Roach * Sort the records by (1) name and (2) last change date 2536e45321fSGreg Roach * 2546e45321fSGreg Roach * @param GedcomRecord $a 2556e45321fSGreg Roach * @param GedcomRecord $b 2566e45321fSGreg Roach * 2576e45321fSGreg Roach * @return int 2586e45321fSGreg Roach */ 259*8f53f488SRico Sonntag private static function sortByNameAndChangeDate(GedcomRecord $a, GedcomRecord $b): int 260c1010edaSGreg Roach { 2616e45321fSGreg Roach return GedcomRecord::compare($a, $b) ?: $b->lastChangeTimestamp(true) - $a->lastChangeTimestamp(true); 2626e45321fSGreg Roach } 2638c2e8227SGreg Roach} 264