1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2022 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\Gedcom; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Registry; 26use Fisharebest\Webtrees\Services\CalendarService; 27use Fisharebest\Webtrees\Tree; 28use Illuminate\Support\Str; 29use Psr\Http\Message\ServerRequestInterface; 30 31/** 32 * Class OnThisDayModule 33 */ 34class OnThisDayModule extends AbstractModule implements ModuleBlockInterface 35{ 36 use ModuleBlockTrait; 37 38 // Pagination 39 private const LIMIT_LOW = 10; 40 private const LIMIT_HIGH = 20; 41 42 // Default values for new blocks. 43 private const DEFAULT_SORT = 'date_desc'; 44 private const DEFAULT_STYLE = 'date_desc'; 45 46 // Initial sorting for datatables 47 private const DATATABLES_ORDER = [ 48 'alpha' => [[0, 'asc']], 49 'date_asc' => [[2, 'asc']], 50 'date_desc' => [[2, 'desc']], 51 ]; 52 53 // All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN 54 private const ALL_EVENTS = [ 55 'ADOP' => 'INDI:ADOP', 56 'ANUL' => 'FAM:ANUL', 57 'BAPM' => 'INDI:BAPM', 58 'BARM' => 'INDI:BARM', 59 'BASM' => 'INDI:BASM', 60 'BIRT' => 'INDI:BIRT', 61 'BLES' => 'INDI:BLES', 62 'BURI' => 'INDI:BURI', 63 'CHR' => 'INDI:CHR', 64 'CHRA' => 'INDI:CHRA', 65 'CONF' => 'INDI:CONF', 66 'CREM' => 'INDI:CREM', 67 'DEAT' => 'INDI:DEAT', 68 'DIV' => 'FAM:DIV', 69 'DIVF' => 'FAM:DIVF', 70 'EMIG' => 'INDI:EMIG', 71 'ENGA' => 'FAM:ENGA', 72 'FCOM' => 'INDI:FCOM', 73 'GRAD' => 'INDI:GRAD', 74 'IMMI' => 'INDI:IMMI', 75 'MARB' => 'FAM:MARB', 76 'MARC' => 'FAM:MARC', 77 'MARL' => 'FAM:MARL', 78 'MARR' => 'FAM:MARR', 79 'MARS' => 'FAM:MARS', 80 'NATU' => 'INDI:NATU', 81 'ORDN' => 'INDI:ORDN', 82 'PROB' => 'INDI:PROB', 83 'RETI' => 'INDI:RETI', 84 'WILL' => 'INDI:WILL', 85 ]; 86 87 private const DEFAULT_EVENTS = [ 88 'BIRT', 89 'MARR', 90 'DEAT', 91 ]; 92 93 /** 94 * How should this module be identified in the control panel, etc.? 95 * 96 * @return string 97 */ 98 public function title(): string 99 { 100 /* I18N: Name of a module */ 101 return I18N::translate('On this day'); 102 } 103 104 /** 105 * A sentence describing what this module does. 106 * 107 * @return string 108 */ 109 public function description(): string 110 { 111 /* I18N: Description of the “On this day” module */ 112 return I18N::translate('A list of the anniversaries that occur today.'); 113 } 114 115 /** 116 * Generate the HTML content of this block. 117 * 118 * @param Tree $tree 119 * @param int $block_id 120 * @param string $context 121 * @param array<string,string> $config 122 * 123 * @return string 124 */ 125 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 126 { 127 $calendar_service = new CalendarService(); 128 129 $default_events = implode(',', self::DEFAULT_EVENTS); 130 131 $filter = (bool) $this->getBlockSetting($block_id, 'filter', '1'); 132 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 133 $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT); 134 $events = $this->getBlockSetting($block_id, 'events', $default_events); 135 136 extract($config, EXTR_OVERWRITE); 137 138 $event_array = explode(',', $events); 139 140 // If we are only showing living individuals, then we don't need to search for DEAT events. 141 if ($filter) { 142 $event_array = array_diff($event_array, Gedcom::DEATH_EVENTS); 143 } 144 145 $events_filter = implode('|', $event_array); 146 147 $startjd = Registry::timestampFactory()->now()->julianDay(); 148 $endjd = $startjd; 149 150 $facts = $calendar_service->getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $tree); 151 152 if ($facts->isEmpty()) { 153 if ($filter && Auth::check()) { 154 $message = I18N::translate('No events for living individuals exist for today.'); 155 } else { 156 $message = I18N::translate('No events exist for today.'); 157 } 158 $content = view('modules/todays_events/empty', ['message' => $message]); 159 } elseif ($infoStyle === 'list') { 160 $content = view('lists/anniversaries-list', [ 161 'id' => $block_id, 162 'facts' => $facts, 163 'limit_low' => self::LIMIT_LOW, 164 'limit_high' => self::LIMIT_HIGH, 165 ]); 166 } else { 167 $content = view('lists/anniversaries-table', [ 168 'facts' => $facts, 169 'limit_low' => self::LIMIT_LOW, 170 'limit_high' => self::LIMIT_HIGH, 171 'order' => self::DATATABLES_ORDER[$sortStyle] ?? self::DATATABLES_ORDER[self::DEFAULT_SORT], 172 ]); 173 } 174 175 if ($context !== self::CONTEXT_EMBED) { 176 return view('modules/block-template', [ 177 'block' => Str::kebab($this->name()), 178 'id' => $block_id, 179 'config_url' => $this->configUrl($tree, $context, $block_id), 180 'title' => $this->title(), 181 'content' => $content, 182 ]); 183 } 184 185 return $content; 186 } 187 188 /** 189 * Should this block load asynchronously using AJAX? 190 * 191 * Simple blocks are faster in-line, more complex ones can be loaded later. 192 * 193 * @return bool 194 */ 195 public function loadAjax(): bool 196 { 197 return true; 198 } 199 200 /** 201 * Can this block be shown on the user’s home page? 202 * 203 * @return bool 204 */ 205 public function isUserBlock(): bool 206 { 207 return true; 208 } 209 210 /** 211 * Can this block be shown on the tree’s home page? 212 * 213 * @return bool 214 */ 215 public function isTreeBlock(): bool 216 { 217 return true; 218 } 219 220 /** 221 * Update the configuration for a block. 222 * 223 * @param ServerRequestInterface $request 224 * @param int $block_id 225 * 226 * @return void 227 */ 228 public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 229 { 230 $params = (array) $request->getParsedBody(); 231 232 $this->setBlockSetting($block_id, 'filter', $params['filter']); 233 $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']); 234 $this->setBlockSetting($block_id, 'sortStyle', $params['sortStyle']); 235 $this->setBlockSetting($block_id, 'events', implode(',', $params['events'] ?? [])); 236 } 237 238 /** 239 * An HTML form to edit block settings 240 * 241 * @param Tree $tree 242 * @param int $block_id 243 * 244 * @return string 245 */ 246 public function editBlockConfiguration(Tree $tree, int $block_id): string 247 { 248 $default_events = implode(',', self::DEFAULT_EVENTS); 249 250 $filter = $this->getBlockSetting($block_id, 'filter', '1'); 251 $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 252 $sort_style = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT); 253 $events = $this->getBlockSetting($block_id, 'events', $default_events); 254 255 $event_array = explode(',', $events); 256 257 $all_events = []; 258 foreach (self::ALL_EVENTS as $event => $tag) { 259 $all_events[$event] = Registry::elementFactory()->make($tag)->label(); 260 } 261 262 $info_styles = [ 263 /* I18N: An option in a list-box */ 264 'list' => I18N::translate('list'), 265 /* I18N: An option in a list-box */ 266 'table' => I18N::translate('table'), 267 ]; 268 269 $sort_styles = [ 270 /* I18N: An option in a list-box */ 271 'alpha' => I18N::translate('sort by name'), 272 /* I18N: An option in a list-box */ 273 'anniv_asc' => I18N::translate('sort by date, oldest first'), 274 /* I18N: An option in a list-box */ 275 'anniv_desc' => I18N::translate('sort by date, newest first'), 276 ]; 277 278 return view('modules/todays_events/config', [ 279 'all_events' => $all_events, 280 'event_array' => $event_array, 281 'filter' => $filter, 282 'info_style' => $info_style, 283 'info_styles' => $info_styles, 284 'sort_style' => $sort_style, 285 'sort_styles' => $sort_styles, 286 ]); 287 } 288} 289