1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2016 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\Database; 20use Fisharebest\Webtrees\Filter; 21use Fisharebest\Webtrees\Functions\FunctionsEdit; 22use Fisharebest\Webtrees\GedcomRecord; 23use Fisharebest\Webtrees\GedcomTag; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Individual; 26use Fisharebest\Webtrees\Theme; 27use Fisharebest\Webtrees\Tree; 28use Rhumsaa\Uuid\Uuid; 29 30/** 31 * Class RecentChangesModule 32 */ 33class RecentChangesModule extends AbstractModule implements ModuleBlockInterface { 34 const DEFAULT_BLOCK = '1'; 35 const DEFAULT_DAYS = 7; 36 const DEFAULT_HIDE_EMPTY = '0'; 37 const DEFAULT_SHOW_USER = '1'; 38 const DEFAULT_SORT_STYLE = 'date_desc'; 39 const DEFAULT_INFO_STYLE = 'table'; 40 const MAX_DAYS = 90; 41 42 /** {@inheritdoc} */ 43 public function getTitle() { 44 return /* I18N: Name of a module */ I18N::translate('Recent changes'); 45 } 46 47 /** {@inheritdoc} */ 48 public function getDescription() { 49 return /* I18N: Description of the “Recent changes” module */ I18N::translate('A list of records that have been updated recently.'); 50 } 51 52 /** {@inheritdoc} */ 53 public function getBlock($block_id, $template = true, $cfg = array()) { 54 global $ctype, $WT_TREE; 55 56 $days = $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 = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 60 $block = $this->getBlockSetting($block_id, 'block', self::DEFAULT_BLOCK); 61 $hide_empty = $this->getBlockSetting($block_id, 'hide_empty', self::DEFAULT_HIDE_EMPTY); 62 63 foreach (array('days', 'infoStyle', 'sortStyle', 'hide_empty', 'show_user', 'block') as $name) { 64 if (array_key_exists($name, $cfg)) { 65 $$name = $cfg[$name]; 66 } 67 } 68 69 $records = $this->getRecentChanges($WT_TREE, WT_CLIENT_JD - $days); 70 71 if (empty($records) && $hide_empty) { 72 return ''; 73 } 74 75 // Print block header 76 $id = $this->getName() . $block_id; 77 $class = $this->getName() . '_block'; 78 79 if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) { 80 $title = '<a class="icon-admin" title="' . I18N::translate('Preferences') . '" href="block_edit.php?block_id=' . $block_id . '&ged=' . $WT_TREE->getNameHtml() . '&ctype=' . $ctype . '"></a>'; 81 } else { 82 $title = ''; 83 } 84 $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)); 85 86 $content = ''; 87 // Print block content 88 if (count($records) == 0) { 89 $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)); 90 } else { 91 switch ($infoStyle) { 92 case 'list': 93 $content .= $this->changesList($records, $sortStyle, $show_user); 94 break; 95 case 'table': 96 $content .= $this->changesTable($records, $sortStyle, $show_user); 97 break; 98 } 99 } 100 101 if ($template) { 102 if ($block) { 103 $class .= ' small_inner_block'; 104 } 105 106 return Theme::theme()->formatBlock($id, $title, $class, $content); 107 } else { 108 return $content; 109 } 110 } 111 112 /** {@inheritdoc} */ 113 public function loadAjax() { 114 return true; 115 } 116 117 /** {@inheritdoc} */ 118 public function isUserBlock() { 119 return true; 120 } 121 122 /** {@inheritdoc} */ 123 public function isGedcomBlock() { 124 return true; 125 } 126 127 /** {@inheritdoc} */ 128 public function configureBlock($block_id) { 129 if (Filter::postBool('save') && Filter::checkCsrf()) { 130 $this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, self::MAX_DAYS)); 131 $this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table')); 132 $this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'name|date_asc|date_desc')); 133 $this->setBlockSetting($block_id, 'show_user', Filter::postBool('show_user')); 134 $this->setBlockSetting($block_id, 'hide_empty', Filter::postBool('hide_empty')); 135 $this->setBlockSetting($block_id, 'block', Filter::postBool('block')); 136 } 137 138 $days = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 139 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_INFO_STYLE); 140 $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT_STYLE); 141 $show_user = $this->getBlockSetting($block_id, 'show_user', self::DEFAULT_SHOW_USER); 142 $block = $this->getBlockSetting($block_id, 'block', self::DEFAULT_BLOCK); 143 $hide_empty = $this->getBlockSetting($block_id, 'hide_empty', self::DEFAULT_HIDE_EMPTY); 144 145 echo '<tr><td class="descriptionbox wrap width33">'; 146 echo I18N::translate('Number of days to show'); 147 echo '</td><td class="optionbox">'; 148 echo '<input type="text" name="days" size="2" value="', $days, '">'; 149 echo ' <em>', I18N::plural('maximum %s day', 'maximum %s days', I18N::number(self::MAX_DAYS), I18N::number(self::MAX_DAYS)), '</em>'; 150 echo '</td></tr>'; 151 152 echo '<tr><td class="descriptionbox wrap width33">'; 153 echo I18N::translate('Presentation style'); 154 echo '</td><td class="optionbox">'; 155 echo FunctionsEdit::selectEditControl('infoStyle', array('list' => I18N::translate('list'), 'table' => I18N::translate('table')), null, $infoStyle, ''); 156 echo '</td></tr>'; 157 158 echo '<tr><td class="descriptionbox wrap width33">'; 159 echo I18N::translate('Sort order'); 160 echo '</td><td class="optionbox">'; 161 echo FunctionsEdit::selectEditControl('sortStyle', array( 162 'name' => /* I18N: An option in a list-box */ I18N::translate('sort by name'), 163 'date_asc' => /* I18N: An option in a list-box */ I18N::translate('sort by date, oldest first'), 164 'date_desc' => /* I18N: An option in a list-box */ I18N::translate('sort by date, newest first'), 165 ), null, $sortStyle, ''); 166 echo '</td></tr>'; 167 168 echo '<tr><td class="descriptionbox wrap width33">'; 169 echo /* I18N: label for a yes/no option */ I18N::translate('Show the user who made the change'); 170 echo '</td><td class="optionbox">'; 171 echo FunctionsEdit::editFieldYesNo('show_user', $show_user); 172 echo '</td></tr>'; 173 174 echo '<tr><td class="descriptionbox wrap width33">'; 175 echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow'); 176 echo '</td><td class="optionbox">'; 177 echo FunctionsEdit::editFieldYesNo('block', $block); 178 echo '</td></tr>'; 179 180 echo '<tr><td class="descriptionbox wrap width33">'; 181 echo I18N::translate('Should this block be hidden when it is empty'); 182 echo '</td><td class="optionbox">'; 183 echo FunctionsEdit::editFieldYesNo('hide_empty', $hide_empty); 184 echo '</td></tr>'; 185 echo '<tr><td colspan="2" class="optionbox wrap">'; 186 echo '<span class="error">', I18N::translate('If you hide an empty block, you will not be able to change its configuration until it becomes visible by no longer being empty.'), '</span>'; 187 echo '</td></tr>'; 188 } 189 190 /** 191 * Find records that have changed since a given julian day 192 * 193 * @param Tree $tree Changes for which tree 194 * @param int $jd Julian day 195 * 196 * @return GedcomRecord[] List of records with changes 197 */ 198 private function getRecentChanges(Tree $tree, $jd) { 199 $sql = 200 "SELECT d_gid FROM `##dates`" . 201 " WHERE d_fact='CHAN' AND d_julianday1 >= :jd AND d_file = :tree_id"; 202 203 $vars = array( 204 'jd' => $jd, 205 'tree_id' => $tree->getTreeId(), 206 ); 207 208 $xrefs = Database::prepare($sql)->execute($vars)->fetchOneColumn(); 209 210 $records = array(); 211 foreach ($xrefs as $xref) { 212 $record = GedcomRecord::getInstance($xref, $tree); 213 if ($record->canShow()) { 214 $records[] = $record; 215 } 216 } 217 218 return $records; 219 } 220 221 /** 222 * Format a table of events 223 * 224 * @param GedcomRecord[] $records 225 * @param string $sort 226 * @param bool $show_user 227 * 228 * @return string 229 */ 230 private function changesList(array $records, $sort, $show_user) { 231 switch ($sort) { 232 case 'name': 233 uasort($records, array('self', 'sortByNameAndChangeDate')); 234 break; 235 case 'date_asc': 236 uasort($records, array('self', 'sortByChangeDateAndName')); 237 $records = array_reverse($records); 238 break; 239 case 'date_desc': 240 uasort($records, array('self', 'sortByChangeDateAndName')); 241 } 242 243 $html = ''; 244 foreach ($records as $record) { 245 $html .= '<a href="' . $record->getHtmlUrl() . '" class="list_item name2">' . $record->getFullName() . '</a>'; 246 $html .= '<div class="indent" style="margin-bottom: 5px;">'; 247 if ($record instanceof Individual) { 248 if ($record->getAddName()) { 249 $html .= '<a href="' . $record->getHtmlUrl() . '" class="list_item">' . $record->getAddName() . '</a>'; 250 } 251 } 252 253 // The timestamp may be missing or private. 254 $timestamp = $record->lastChangeTimestamp(); 255 if ($timestamp !== '') { 256 if ($show_user) { 257 $html .= /* I18N: [a record was] Changed on <date/time> by <user> */ 258 I18N::translate('Changed on %1$s by %2$s', $timestamp, Filter::escapeHtml($record->lastChangeUser())); 259 } else { 260 $html .= /* I18N: [a record was] Changed on <date/time> */ 261 I18N::translate('Changed on %1$s', $timestamp); 262 } 263 } 264 $html .= '</div>'; 265 } 266 267 return $html; 268 } 269 270 /** 271 * Format a table of events 272 * 273 * @param GedcomRecord[] $records 274 * @param string $sort 275 * @param bool $show_user 276 * 277 * @return string 278 */ 279 private function changesTable($records, $sort, $show_user) { 280 global $controller; 281 282 $table_id = 'table-chan-' . Uuid::uuid4(); // lists requires a unique ID in case there are multiple lists per page 283 284 switch ($sort) { 285 case 'name': 286 default: 287 $aaSorting = "[2,'asc'], [4,'desc']"; 288 break; 289 case 'date_asc': 290 $aaSorting = "[4,'asc'], [2,'asc']"; 291 break; 292 case 'date_desc': 293 $aaSorting = "[4,'desc'], [2,'asc']"; 294 break; 295 } 296 297 $html = ''; 298 $controller 299 ->addExternalJavascript(WT_JQUERY_DATATABLES_JS_URL) 300 ->addInlineJavascript(' 301 jQuery.fn.dataTableExt.oSort["unicode-asc" ]=function(a,b) {return a.replace(/<[^<]*>/, "").localeCompare(b.replace(/<[^<]*>/, ""))}; 302 jQuery.fn.dataTableExt.oSort["unicode-desc"]=function(a,b) {return b.replace(/<[^<]*>/, "").localeCompare(a.replace(/<[^<]*>/, ""))}; 303 jQuery("#' . $table_id . '").dataTable({ 304 dom: \'t\', 305 paging: false, 306 autoWidth:false, 307 lengthChange: false, 308 filter: false, 309 ' . I18N::datatablesI18N() . ', 310 jQueryUI: true, 311 sorting: [' . $aaSorting . '], 312 columns: [ 313 /* 0-Type */ { sortable: false, class: "center" }, 314 /* 1-Record */ { dataSort: 2 }, 315 /* 2-SORTNAME */ { type: "unicode" }, 316 /* 3-Change */ { dataSort: 4 }, 317 /* 4-DATE */ null 318 ' . ($show_user ? ',/* 5-By */ null' : '') . ' 319 ] 320 }); 321 '); 322 323 $html .= '<table id="' . $table_id . '" class="width100">'; 324 $html .= '<thead><tr>'; 325 $html .= '<th></th>'; 326 $html .= '<th>' . I18N::translate('Record') . '</th>'; 327 $html .= '<th hidden>SORTNAME</th>'; 328 $html .= '<th>' . GedcomTag::getLabel('CHAN') . '</th>'; 329 $html .= '<th hidden>DATE</th>'; 330 if ($show_user) { 331 $html .= '<th>' . GedcomTag::getLabel('_WT_USER') . '</th>'; 332 } 333 $html .= '</tr></thead><tbody>'; 334 335 foreach ($records as $record) { 336 $html .= '<tr><td>'; 337 switch ($record::RECORD_TYPE) { 338 case 'INDI': 339 $icon = $record->getSexImage('small'); 340 break; 341 case 'FAM': 342 $icon = '<i class="icon-button_family"></i>'; 343 break; 344 case 'OBJE': 345 $icon = '<i class="icon-button_media"></i>'; 346 break; 347 case 'NOTE': 348 $icon = '<i class="icon-button_note"></i>'; 349 break; 350 case 'SOUR': 351 $icon = '<i class="icon-button_source"></i>'; 352 break; 353 case 'REPO': 354 $icon = '<i class="icon-button_repository"></i>'; 355 break; 356 default: 357 $icon = ' '; 358 break; 359 } 360 $html .= '<a href="' . $record->getHtmlUrl() . '">' . $icon . '</a>'; 361 $html .= '</td>'; 362 $name = $record->getFullName(); 363 $html .= '<td class="wrap">'; 364 $html .= '<a href="' . $record->getHtmlUrl() . '">' . $name . '</a>'; 365 if ($record instanceof Individual) { 366 $addname = $record->getAddName(); 367 if ($addname) { 368 $html .= '<div class="indent"><a href="' . $record->getHtmlUrl() . '">' . $addname . '</a></div>'; 369 } 370 } 371 $html .= '</td>'; 372 $html .= '<td hidden>' . $record->getSortName() . '</td>'; 373 $html .= '<td class="wrap">' . $record->lastChangeTimestamp() . '</td>'; 374 $html .= '<td hidden>' . $record->lastChangeTimestamp(true) . '</td>'; 375 if ($show_user) { 376 $html .= '<td class="wrap">' . Filter::escapeHtml($record->lastChangeUser()) . '</td>'; 377 } 378 $html .= '</tr>'; 379 } 380 381 $html .= '</tbody></table>'; 382 383 return $html; 384 } 385 386 /** 387 * Sort the records by (1) last change date and (2) name 388 * 389 * @param GedcomRecord $a 390 * @param GedcomRecord $b 391 * 392 * @return int 393 */ 394 private static function sortByChangeDateAndName(GedcomRecord $a, GedcomRecord $b) { 395 return $b->lastChangeTimestamp(true) - $a->lastChangeTimestamp(true) ?: GedcomRecord::compare($a, $b); 396 } 397 398 /** 399 * Sort the records by (1) name and (2) last change date 400 * 401 * @param GedcomRecord $a 402 * @param GedcomRecord $b 403 * 404 * @return int 405 */ 406 private static function sortByNameAndChangeDate(GedcomRecord $a, GedcomRecord $b) { 407 return GedcomRecord::compare($a, $b) ?: $b->lastChangeTimestamp(true) - $a->lastChangeTimestamp(true); 408 } 409} 410