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