1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>. 16 */ 17declare(strict_types=1); 18 19namespace Fisharebest\Webtrees\Module; 20 21use Fisharebest\Webtrees\Carbon; 22use Fisharebest\Webtrees\GedcomRecord; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Tree; 25use Illuminate\Database\Capsule\Manager as DB; 26use Illuminate\Support\Str; 27use Psr\Http\Message\ServerRequestInterface; 28 29/** 30 * Class RecentChangesModule 31 */ 32class RecentChangesModule extends AbstractModule implements ModuleBlockInterface 33{ 34 use ModuleBlockTrait; 35 36 private const DEFAULT_DAYS = '7'; 37 private const DEFAULT_SHOW_USER = '1'; 38 private const DEFAULT_SORT_STYLE = 'date_desc'; 39 private const DEFAULT_INFO_STYLE = 'table'; 40 private const MAX_DAYS = 90; 41 42 /** 43 * How should this module be identified in the control panel, etc.? 44 * 45 * @return string 46 */ 47 public function title(): string 48 { 49 /* I18N: Name of a module */ 50 return I18N::translate('Recent changes'); 51 } 52 53 /** 54 * A sentence describing what this module does. 55 * 56 * @return string 57 */ 58 public function description(): string 59 { 60 /* I18N: Description of the “Recent changes” module */ 61 return I18N::translate('A list of records that have been updated recently.'); 62 } 63 64 /** {@inheritdoc} */ 65 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 66 { 67 $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 68 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE); 69 $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE); 70 $show_user = (bool) $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 71 72 extract($config, EXTR_OVERWRITE); 73 74 $records = $this->getRecentChanges($tree, $days); 75 76 switch ($sortStyle) { 77 case 'name': 78 uasort($records, GedcomRecord::nameComparator()); 79 break; 80 81 case 'date_asc': 82 uasort($records, GedcomRecord::lastChangeComparator()); 83 break; 84 85 case 'date_desc': 86 uasort($records, GedcomRecord::lastChangeComparator(-1)); 87 } 88 89 if (empty($records)) { 90 $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)); 91 } elseif ($infoStyle === 'list') { 92 $content = view('modules/recent_changes/changes-list', [ 93 'records' => $records, 94 'show_user' => $show_user, 95 ]); 96 } else { 97 $content = view('modules/recent_changes/changes-table', [ 98 'records' => $records, 99 'show_user' => $show_user, 100 ]); 101 } 102 103 if ($context !== self::CONTEXT_EMBED) { 104 return view('modules/block-template', [ 105 'block' => Str::kebab($this->name()), 106 'id' => $block_id, 107 'config_url' => $this->configUrl($tree, $context, $block_id), 108 'title' => I18N::plural('Changes in the last %s day', 'Changes in the last %s days', $days, I18N::number($days)), 109 'content' => $content, 110 ]); 111 } 112 113 return $content; 114 } 115 116 /** 117 * Should this block load asynchronously using AJAX? 118 * 119 * Simple blocks are faster in-line, more complex ones can be loaded later. 120 * 121 * @return bool 122 */ 123 public function loadAjax(): bool 124 { 125 return true; 126 } 127 128 /** 129 * Can this block be shown on the user’s home page? 130 * 131 * @return bool 132 */ 133 public function isUserBlock(): bool 134 { 135 return true; 136 } 137 138 /** 139 * Can this block be shown on the tree’s home page? 140 * 141 * @return bool 142 */ 143 public function isTreeBlock(): bool 144 { 145 return true; 146 } 147 148 /** 149 * Update the configuration for a block. 150 * 151 * @param ServerRequestInterface $request 152 * @param int $block_id 153 * 154 * @return void 155 */ 156 public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 157 { 158 $params = $request->getParsedBody(); 159 160 $this->setBlockSetting($block_id, 'days', $params['days']); 161 $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']); 162 $this->setBlockSetting($block_id, 'sortStyle', $params['sortStyle']); 163 $this->setBlockSetting($block_id, 'show_user', $params['show_user']); 164 } 165 166 /** 167 * An HTML form to edit block settings 168 * 169 * @param Tree $tree 170 * @param int $block_id 171 * 172 * @return string 173 */ 174 public function editBlockConfiguration(Tree $tree, int $block_id): string 175 { 176 $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 177 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE); 178 $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE); 179 $show_user = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 180 181 $info_styles = [ 182 /* I18N: An option in a list-box */ 183 'list' => I18N::translate('list'), 184 /* I18N: An option in a list-box */ 185 'table' => I18N::translate('table'), 186 ]; 187 188 $sort_styles = [ 189 /* I18N: An option in a list-box */ 190 'name' => I18N::translate('sort by name'), 191 /* I18N: An option in a list-box */ 192 'date_asc' => I18N::translate('sort by date, oldest first'), 193 /* I18N: An option in a list-box */ 194 'date_desc' => I18N::translate('sort by date, newest first'), 195 ]; 196 197 return view('modules/recent_changes/config', [ 198 'days' => $days, 199 'infoStyle' => $infoStyle, 200 'info_styles' => $info_styles, 201 'max_days' => self::MAX_DAYS, 202 'sortStyle' => $sortStyle, 203 'sort_styles' => $sort_styles, 204 'show_user' => $show_user, 205 ]); 206 } 207 208 /** 209 * Find records that have changed since a given julian day 210 * 211 * @param Tree $tree Changes for which tree 212 * @param int $days Number of days 213 * 214 * @return GedcomRecord[] List of records with changes 215 */ 216 private function getRecentChanges(Tree $tree, int $days): array 217 { 218 return DB::table('change') 219 ->where('gedcom_id', '=', $tree->id()) 220 ->where('status', '=', 'accepted') 221 ->where('new_gedcom', '<>', '') 222 ->where('change_time', '>', Carbon::now()->subDays($days)) 223 ->groupBy(['xref']) 224 ->pluck('xref') 225 ->map(static function (string $xref) use ($tree): ?GedcomRecord { 226 return GedcomRecord::getInstance($xref, $tree); 227 }) 228 ->filter(static function (?GedcomRecord $record): bool { 229 return $record instanceof GedcomRecord && $record->canShow(); 230 }) 231 ->all(); 232 } 233} 234