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