1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Module; 19 20use Fisharebest\Webtrees\Auth; 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 Symfony\Component\HttpFoundation\Request; 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 $ctype = '', array $cfg = []): 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($cfg, 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 ($ctype !== '') { 104 if ($ctype === 'gedcom' && Auth::isManager($tree)) { 105 $config_url = route('tree-page-block-edit', [ 106 'block_id' => $block_id, 107 'ged' => $tree->name(), 108 ]); 109 } elseif ($ctype === 'user' && Auth::check()) { 110 $config_url = route('user-page-block-edit', [ 111 'block_id' => $block_id, 112 'ged' => $tree->name(), 113 ]); 114 } else { 115 $config_url = ''; 116 } 117 118 return view('modules/block-template', [ 119 'block' => Str::kebab($this->name()), 120 'id' => $block_id, 121 'config_url' => $config_url, 122 'title' => I18N::plural('Changes in the last %s day', 'Changes in the last %s days', $days, I18N::number($days)), 123 'content' => $content, 124 ]); 125 } 126 127 return $content; 128 } 129 130 /** {@inheritdoc} */ 131 public function loadAjax(): bool 132 { 133 return true; 134 } 135 136 /** {@inheritdoc} */ 137 public function isUserBlock(): bool 138 { 139 return true; 140 } 141 142 /** {@inheritdoc} */ 143 public function isTreeBlock(): bool 144 { 145 return true; 146 } 147 148 /** 149 * Update the configuration for a block. 150 * 151 * @param Request $request 152 * @param int $block_id 153 * 154 * @return void 155 */ 156 public function saveBlockConfiguration(Request $request, int $block_id): void 157 { 158 $days = $request->get('days', self::DEFAULT_DAYS); 159 160 if ((int) $days > self::MAX_DAYS || (int) $days < 1) { 161 $days = self::DEFAULT_DAYS; 162 } 163 164 $this->setBlockSetting($block_id, 'days', $days); 165 $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_INFO_STYLE)); 166 $this->setBlockSetting($block_id, 'sortStyle', $request->get('sortStyle', self::DEFAULT_SORT_STYLE)); 167 $this->setBlockSetting($block_id, 'show_user', $request->get('show_user', self::DEFAULT_SHOW_USER)); 168 } 169 170 /** 171 * An HTML form to edit block settings 172 * 173 * @param Tree $tree 174 * @param int $block_id 175 * 176 * @return void 177 */ 178 public function editBlockConfiguration(Tree $tree, int $block_id): void 179 { 180 $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 181 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE); 182 $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE); 183 $show_user = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 184 185 $info_styles = [ 186 /* I18N: An option in a list-box */ 187 'list' => I18N::translate('list'), 188 /* I18N: An option in a list-box */ 189 'table' => I18N::translate('table'), 190 ]; 191 192 $sort_styles = [ 193 /* I18N: An option in a list-box */ 194 'name' => I18N::translate('sort by name'), 195 /* I18N: An option in a list-box */ 196 'date_asc' => I18N::translate('sort by date, oldest first'), 197 /* I18N: An option in a list-box */ 198 'date_desc' => I18N::translate('sort by date, newest first'), 199 ]; 200 201 echo view('modules/recent_changes/config', [ 202 'days' => $days, 203 'infoStyle' => $infoStyle, 204 'info_styles' => $info_styles, 205 'max_days' => self::MAX_DAYS, 206 'sortStyle' => $sortStyle, 207 'sort_styles' => $sort_styles, 208 'show_user' => $show_user, 209 ]); 210 } 211 212 /** 213 * Find records that have changed since a given julian day 214 * 215 * @param Tree $tree Changes for which tree 216 * @param int $days Number of days 217 * 218 * @return GedcomRecord[] List of records with changes 219 */ 220 private function getRecentChanges(Tree $tree, int $days): array 221 { 222 return DB::table('change') 223 ->where('gedcom_id', '=', $tree->id()) 224 ->where('status', '=', 'accepted') 225 ->where('new_gedcom', '<>', '') 226 ->where('change_time', '>', Carbon::now()->subDays($days)) 227 ->groupBy('xref') 228 ->pluck('xref') 229 ->map(function (string $xref) use ($tree): ?GedcomRecord { 230 return GedcomRecord::getInstance($xref, $tree); 231 }) 232 ->filter(function (?GedcomRecord $record): bool { 233 return $record instanceof GedcomRecord && $record->canShow(); 234 }) 235 ->all(); 236 } 237} 238