1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 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 */ 16namespace Fisharebest\Webtrees\Module; 17 18use Fisharebest\Webtrees\Auth; 19use Fisharebest\Webtrees\Bootstrap4; 20use Fisharebest\Webtrees\Database; 21use Fisharebest\Webtrees\Filter; 22use Fisharebest\Webtrees\FontAwesome; 23use Fisharebest\Webtrees\Functions\FunctionsEdit; 24use Fisharebest\Webtrees\GedcomRecord; 25use Fisharebest\Webtrees\GedcomTag; 26use Fisharebest\Webtrees\Html; 27use Fisharebest\Webtrees\I18N; 28use Fisharebest\Webtrees\Individual; 29use Fisharebest\Webtrees\Tree; 30use Ramsey\Uuid\Uuid; 31 32/** 33 * Class RecentChangesModule 34 */ 35class RecentChangesModule extends AbstractModule implements ModuleBlockInterface { 36 const DEFAULT_BLOCK = '1'; 37 const DEFAULT_DAYS = 7; 38 const DEFAULT_HIDE_EMPTY = '0'; 39 const DEFAULT_SHOW_USER = '1'; 40 const DEFAULT_SORT_STYLE = 'date_desc'; 41 const DEFAULT_INFO_STYLE = 'table'; 42 const MAX_DAYS = 90; 43 44 /** {@inheritdoc} */ 45 public function getTitle() { 46 return /* I18N: Name of a module */ I18N::translate('Recent changes'); 47 } 48 49 /** {@inheritdoc} */ 50 public function getDescription() { 51 return /* I18N: Description of the “Recent changes” module */ I18N::translate('A list of records that have been updated recently.'); 52 } 53 54 /** {@inheritdoc} */ 55 public function getBlock($block_id, $template = true, $cfg = []): string { 56 global $ctype, $WT_TREE; 57 58 $days = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 59 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE); 60 $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE); 61 $show_user = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 62 63 foreach (['days', 'infoStyle', 'sortStyle', 'show_user'] as $name) { 64 if (array_key_exists($name, $cfg)) { 65 $$name = $cfg[$name]; 66 } 67 } 68 69 $records = $this->getRecentChanges($WT_TREE, $days); 70 71 $content = ''; 72 // Print block content 73 if (empty($records)) { 74 $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)); 75 } else { 76 switch ($infoStyle) { 77 case 'list': 78 $content .= $this->changesList($records, $sortStyle, $show_user); 79 break; 80 case 'table': 81 $content .= $this->changesTable($records, $sortStyle, $show_user); 82 break; 83 } 84 } 85 86 if ($template) { 87 if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) { 88 $config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]); 89 } else { 90 $config_url = ''; 91 } 92 93 return view('blocks/template', [ 94 'block' => str_replace('_', '-', $this->getName()), 95 'id' => $block_id, 96 'config_url' => $config_url, 97 'title' => I18N::plural('Changes in the last %s day', 'Changes in the last %s days', $days, I18N::number($days)), 98 'content' => $content, 99 ]); 100 } else { 101 return $content; 102 } 103 } 104 105 /** {@inheritdoc} */ 106 public function loadAjax(): bool { 107 return true; 108 } 109 110 /** {@inheritdoc} */ 111 public function isUserBlock(): bool { 112 return true; 113 } 114 115 /** {@inheritdoc} */ 116 public function isGedcomBlock(): bool { 117 return true; 118 } 119 120 /** {@inheritdoc} */ 121 public function configureBlock($block_id) { 122 if (Filter::postBool('save') && Filter::checkCsrf()) { 123 $this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, self::MAX_DAYS)); 124 $this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table')); 125 $this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'name|date_asc|date_desc')); 126 $this->setBlockSetting($block_id, 'show_user', Filter::postBool('show_user')); 127 } 128 129 $days = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 130 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE); 131 $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE); 132 $show_user = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 133 134 echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="days">'; 135 echo I18N::translate('Number of days to show'); 136 echo '</div><div class="col-sm-9">'; 137 echo '<input type="text" name="days" size="2" value="', $days, '">'; 138 echo ' ' . I18N::plural('maximum %s day', 'maximum %s days', I18N::number(self::MAX_DAYS), I18N::number(self::MAX_DAYS)); 139 echo '</div></div>'; 140 141 echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="infoStyle">'; 142 echo I18N::translate('Presentation style'); 143 echo '</div><div class="col-sm-9">'; 144 echo Bootstrap4::select(['list' => I18N::translate('list'), 'table' => I18N::translate('table')], $infoStyle, ['id' => 'infoStyle', 'name' => 'infoStyle']); 145 echo '</div></div>'; 146 147 echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="sortStyle">'; 148 echo I18N::translate('Sort order'); 149 echo '</div><div class="col-sm-9">'; 150 echo Bootstrap4::select([ 151 'name' => /* I18N: An option in a list-box */ I18N::translate('sort by name'), 152 'date_asc' => /* I18N: An option in a list-box */ I18N::translate('sort by date, oldest first'), 153 'date_desc' => /* I18N: An option in a list-box */ I18N::translate('sort by date, newest first'), 154 ], $sortStyle, ['id' => 'sortStyle', 'name' => 'sortStyle']); 155 echo '</div></div>'; 156 157 echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="show_usere">'; 158 echo /* I18N: label for a yes/no option */ I18N::translate('Show the user who made the change'); 159 echo '</div><div class="col-sm-9">'; 160 echo Bootstrap4::radioButtons('show_user', FunctionsEdit::optionsNoYes(), $show_user, true); 161 echo '</div></div>'; 162 } 163 164 /** 165 * Find records that have changed since a given julian day 166 * 167 * @param Tree $tree Changes for which tree 168 * @param int $days Number of days 169 * 170 * @return GedcomRecord[] List of records with changes 171 */ 172 private function getRecentChanges(Tree $tree, $days) { 173 $sql = 174 "SELECT xref FROM `##change`" . 175 " WHERE new_gedcom != '' AND change_time > DATE_SUB(NOW(), INTERVAL :days DAY) AND gedcom_id = :tree_id" . 176 " GROUP BY xref" . 177 " ORDER BY MAX(change_id) DESC"; 178 179 $vars = [ 180 'days' => $days, 181 'tree_id' => $tree->getTreeId(), 182 ]; 183 184 $xrefs = Database::prepare($sql)->execute($vars)->fetchOneColumn(); 185 186 $records = []; 187 foreach ($xrefs as $xref) { 188 $record = GedcomRecord::getInstance($xref, $tree); 189 if ($record && $record->canShow()) { 190 $records[] = $record; 191 } 192 } 193 194 return $records; 195 } 196 197 /** 198 * Format a table of events 199 * 200 * @param GedcomRecord[] $records 201 * @param string $sort 202 * @param bool $show_user 203 * 204 * @return string 205 */ 206 private function changesList(array $records, $sort, $show_user) { 207 switch ($sort) { 208 case 'name': 209 uasort($records, ['self', 'sortByNameAndChangeDate']); 210 break; 211 case 'date_asc': 212 uasort($records, ['self', 'sortByChangeDateAndName']); 213 $records = array_reverse($records); 214 break; 215 case 'date_desc': 216 uasort($records, ['self', 'sortByChangeDateAndName']); 217 } 218 219 $html = ''; 220 foreach ($records as $record) { 221 $html .= '<a href="' . e($record->url()) . '" class="list_item name2">' . $record->getFullName() . '</a>'; 222 $html .= '<div class="indent" style="margin-bottom: 5px;">'; 223 if ($record instanceof Individual) { 224 if ($record->getAddName()) { 225 $html .= '<a href="' . e($record->url()) . '" class="list_item">' . $record->getAddName() . '</a>'; 226 } 227 } 228 229 // The timestamp may be missing or private. 230 $timestamp = $record->lastChangeTimestamp(); 231 if ($timestamp !== '') { 232 if ($show_user) { 233 $html .= /* I18N: [a record was] Changed on <date/time> by <user> */ 234 I18N::translate('Changed on %1$s by %2$s', $timestamp, e($record->lastChangeUser())); 235 } else { 236 $html .= /* I18N: [a record was] Changed on <date/time> */ 237 I18N::translate('Changed on %1$s', $timestamp); 238 } 239 } 240 $html .= '</div>'; 241 } 242 243 return $html; 244 } 245 246 /** 247 * Format a table of events 248 * 249 * @param GedcomRecord[] $records 250 * @param string $sort 251 * @param bool $show_user 252 * 253 * @return string 254 */ 255 private function changesTable($records, $sort, $show_user) { 256 global $controller; 257 258 $table_id = 'table-chan-' . Uuid::uuid4(); // lists requires a unique ID in case there are multiple lists per page 259 260 switch ($sort) { 261 case 'name': 262 default: 263 $aaSorting = "[1,'asc'], [2,'desc']"; 264 break; 265 case 'date_asc': 266 $aaSorting = "[2,'asc'], [1,'asc']"; 267 break; 268 case 'date_desc': 269 $aaSorting = "[2,'desc'], [1,'asc']"; 270 break; 271 } 272 273 $html = ''; 274 $controller 275 ->addInlineJavascript(' 276 $("#' . $table_id . '").dataTable({ 277 dom: \'t\', 278 paging: false, 279 autoWidth:false, 280 lengthChange: false, 281 filter: false, 282 ' . I18N::datatablesI18N() . ', 283 sorting: [' . $aaSorting . '], 284 columns: [ 285 { sortable: false, class: "center" }, 286 null, 287 null, 288 { visible: ' . ($show_user ? 'true' : 'false') . ' } 289 ] 290 }); 291 '); 292 293 $html .= '<table id="' . $table_id . '" class="width100">'; 294 $html .= '<thead><tr>'; 295 $html .= '<th></th>'; 296 $html .= '<th>' . I18N::translate('Record') . '</th>'; 297 $html .= '<th>' . I18N::translate('Last change') . '</th>'; 298 $html .= '<th>' . GedcomTag::getLabel('_WT_USER') . '</th>'; 299 $html .= '</tr></thead><tbody>'; 300 301 foreach ($records as $record) { 302 $html .= '<tr><td>'; 303 switch ($record::RECORD_TYPE) { 304 case 'INDI': 305 $html .= FontAwesome::semanticIcon('individual', I18N::translate('Individual')); 306 break; 307 case 'FAM': 308 $html .= FontAwesome::semanticicon('family', I18N::translate('Family')); 309 break; 310 case 'OBJE': 311 $html .= FontAwesome::semanticIcon('media', I18N::translate('Media')); 312 break; 313 case 'NOTE': 314 $html .= FontAwesome::semanticIcon('note', I18N::translate('Note')); 315 break; 316 case 'SOUR': 317 $html .= FontAwesome::semanticIcon('source', I18N::translate('Source')); 318 break; 319 case 'SUBM': 320 $html .= FontAwesome::semanticIcon('submitter', I18N::translate('Submitter')); 321 break; 322 case 'REPO': 323 $html .= FontAwesome::semanticIcon('repository', I18N::translate('Repository')); 324 break; 325 } 326 $html .= '</td>'; 327 $html .= '<td data-sort="' . e($record->getSortName()) . '">'; 328 $html .= '<a href="' . e($record->url()) . '">' . $record->getFullName() . '</a>'; 329 $addname = $record->getAddName(); 330 if ($addname) { 331 $html .= '<div class="indent"><a href="' . e($record->url()) . '">' . $addname . '</a></div>'; 332 } 333 $html .= '</td>'; 334 $html .= '<td data-sort="' . $record->lastChangeTimestamp(true) . '">' . $record->lastChangeTimestamp() . '</td>'; 335 $html .= '<td>' . e($record->lastChangeUser()) . '</td>'; 336 $html .= '</tr>'; 337 } 338 339 $html .= '</tbody></table>'; 340 341 return $html; 342 } 343 344 /** 345 * Sort the records by (1) last change date and (2) name 346 * 347 * @param GedcomRecord $a 348 * @param GedcomRecord $b 349 * 350 * @return int 351 */ 352 private static function sortByChangeDateAndName(GedcomRecord $a, GedcomRecord $b) { 353 return $b->lastChangeTimestamp(true) - $a->lastChangeTimestamp(true) ?: GedcomRecord::compare($a, $b); 354 } 355 356 /** 357 * Sort the records by (1) name and (2) last change date 358 * 359 * @param GedcomRecord $a 360 * @param GedcomRecord $b 361 * 362 * @return int 363 */ 364 private static function sortByNameAndChangeDate(GedcomRecord $a, GedcomRecord $b) { 365 return GedcomRecord::compare($a, $b) ?: $b->lastChangeTimestamp(true) - $a->lastChangeTimestamp(true); 366 } 367} 368