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