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 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\Webtrees\Carbon; 23use Fisharebest\Webtrees\GedcomRecord; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Services\UserService; 26use Fisharebest\Webtrees\Tree; 27use Illuminate\Database\Capsule\Manager as DB; 28use Illuminate\Database\Query\Expression; 29use Illuminate\Support\Collection; 30use Illuminate\Support\Str; 31use Psr\Http\Message\ServerRequestInterface; 32use stdClass; 33 34use function extract; 35use function view; 36 37use const EXTR_OVERWRITE; 38 39/** 40 * Class RecentChangesModule 41 */ 42class RecentChangesModule extends AbstractModule implements ModuleBlockInterface 43{ 44 use ModuleBlockTrait; 45 46 private const DEFAULT_DAYS = '7'; 47 private const DEFAULT_SHOW_USER = '1'; 48 private const DEFAULT_SORT_STYLE = 'date_desc'; 49 private const DEFAULT_INFO_STYLE = 'table'; 50 private const MAX_DAYS = 90; 51 52 // Pagination 53 private const LIMIT_LOW = 10; 54 private const LIMIT_HIGH = 20; 55 56 /** @var UserService */ 57 private $user_service; 58 59 /** 60 * RecentChangesModule constructor. 61 * 62 * @param UserService $user_service 63 */ 64 public function __construct(UserService $user_service) 65 { 66 $this->user_service = $user_service; 67 } 68 69 /** 70 * How should this module be identified in the control panel, etc.? 71 * 72 * @return string 73 */ 74 public function title(): string 75 { 76 /* I18N: Name of a module */ 77 return I18N::translate('Recent changes'); 78 } 79 80 /** 81 * A sentence describing what this module does. 82 * 83 * @return string 84 */ 85 public function description(): string 86 { 87 /* I18N: Description of the “Recent changes” module */ 88 return I18N::translate('A list of records that have been updated recently.'); 89 } 90 91 /** {@inheritdoc} */ 92 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 93 { 94 $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 95 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE); 96 $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE); 97 $show_user = (bool) $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 98 99 extract($config, EXTR_OVERWRITE); 100 101 $rows = $this->getRecentChanges($tree, $days); 102 103 switch ($sortStyle) { 104 case 'name': 105 $rows = $rows->sort(static function (stdClass $x, stdClass $y): int { 106 return GedcomRecord::nameComparator()($x->record, $y->record); 107 }); 108 $order = [[1, 'asc']]; 109 break; 110 111 case 'date_asc': 112 $rows = $rows->sort(static function (stdClass $x, stdClass $y): int { 113 return $x->time <=> $y->time; 114 }); 115 $order = [[2, 'asc']]; 116 break; 117 118 default: 119 case 'date_desc': 120 $rows = $rows->sort(static function (stdClass $x, stdClass $y): int { 121 return $y->time <=> $x->time; 122 }); 123 $order = [[2, 'asc']]; 124 break; 125 } 126 127 if ($rows->isEmpty()) { 128 $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)); 129 } elseif ($infoStyle === 'list') { 130 $content = view('modules/recent_changes/changes-list', [ 131 'id' => $block_id, 132 'limit_low' => self::LIMIT_LOW, 133 'limit_high' => self::LIMIT_HIGH, 134 'rows' => $rows->values(), 135 'show_user' => $show_user, 136 ]); 137 } else { 138 $content = view('modules/recent_changes/changes-table', [ 139 'limit_low' => self::LIMIT_LOW, 140 'limit_high' => self::LIMIT_HIGH, 141 'rows' => $rows, 142 'show_user' => $show_user, 143 'order' => $order, 144 ]); 145 } 146 147 if ($context !== self::CONTEXT_EMBED) { 148 return view('modules/block-template', [ 149 'block' => Str::kebab($this->name()), 150 'id' => $block_id, 151 'config_url' => $this->configUrl($tree, $context, $block_id), 152 'title' => I18N::plural('Changes in the last %s day', 'Changes in the last %s days', $days, I18N::number($days)), 153 'content' => $content, 154 ]); 155 } 156 157 return $content; 158 } 159 160 /** 161 * Should this block load asynchronously using AJAX? 162 * 163 * Simple blocks are faster in-line, more complex ones can be loaded later. 164 * 165 * @return bool 166 */ 167 public function loadAjax(): bool 168 { 169 return true; 170 } 171 172 /** 173 * Can this block be shown on the user’s home page? 174 * 175 * @return bool 176 */ 177 public function isUserBlock(): bool 178 { 179 return true; 180 } 181 182 /** 183 * Can this block be shown on the tree’s home page? 184 * 185 * @return bool 186 */ 187 public function isTreeBlock(): bool 188 { 189 return true; 190 } 191 192 /** 193 * Update the configuration for a block. 194 * 195 * @param ServerRequestInterface $request 196 * @param int $block_id 197 * 198 * @return void 199 */ 200 public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 201 { 202 $params = (array) $request->getParsedBody(); 203 204 $this->setBlockSetting($block_id, 'days', $params['days']); 205 $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']); 206 $this->setBlockSetting($block_id, 'sortStyle', $params['sortStyle']); 207 $this->setBlockSetting($block_id, 'show_user', $params['show_user']); 208 } 209 210 /** 211 * An HTML form to edit block settings 212 * 213 * @param Tree $tree 214 * @param int $block_id 215 * 216 * @return string 217 */ 218 public function editBlockConfiguration(Tree $tree, int $block_id): string 219 { 220 $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 221 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE); 222 $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE); 223 $show_user = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 224 225 $info_styles = [ 226 /* I18N: An option in a list-box */ 227 'list' => I18N::translate('list'), 228 /* I18N: An option in a list-box */ 229 'table' => I18N::translate('table'), 230 ]; 231 232 $sort_styles = [ 233 /* I18N: An option in a list-box */ 234 'name' => I18N::translate('sort by name'), 235 /* I18N: An option in a list-box */ 236 'date_asc' => I18N::translate('sort by date, oldest first'), 237 /* I18N: An option in a list-box */ 238 'date_desc' => I18N::translate('sort by date, newest first'), 239 ]; 240 241 return view('modules/recent_changes/config', [ 242 'days' => $days, 243 'infoStyle' => $infoStyle, 244 'info_styles' => $info_styles, 245 'max_days' => self::MAX_DAYS, 246 'sortStyle' => $sortStyle, 247 'sort_styles' => $sort_styles, 248 'show_user' => $show_user, 249 ]); 250 } 251 252 /** 253 * Find records that have changed since a given julian day 254 * 255 * @param Tree $tree Changes for which tree 256 * @param int $days Number of days 257 * 258 * @return Collection<stdClass> List of records with changes 259 */ 260 private function getRecentChanges(Tree $tree, int $days): Collection 261 { 262 $subquery = DB::table('change') 263 ->where('gedcom_id', '=', $tree->id()) 264 ->where('status', '=', 'accepted') 265 ->where('new_gedcom', '<>', '') 266 ->where('change_time', '>', Carbon::now()->subDays($days)) 267 ->groupBy(['xref']) 268 ->select(new Expression('MAX(change_id) AS recent_change_id')); 269 270 $query = DB::table('change') 271 ->joinSub($subquery, 'recent', 'recent_change_id', '=', 'change_id') 272 ->select(['change.*']); 273 274 return $query 275 ->get() 276 ->map(function (stdClass $row) use ($tree): stdClass { 277 return (object) [ 278 'record' => GedcomRecord::getInstance($row->xref, $tree, $row->new_gedcom), 279 'time' => Carbon::create($row->change_time)->local(), 280 'user' => $this->user_service->find($row->user_id), 281 ]; 282 }) 283 ->filter(static function (stdClass $row): bool { 284 return $row->record instanceof GedcomRecord && $row->record->canShow(); 285 }); 286 } 287} 288