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 $content = view('modules/todays_events/empty'); 153 } elseif ($infoStyle === 'list') { 154 $content = view('lists/anniversaries-list', [ 155 'id' => $block_id, 156 'facts' => $facts, 157 'limit_low' => self::LIMIT_LOW, 158 'limit_high' => self::LIMIT_HIGH, 159 ]); 160 } else { 161 $content = view('lists/anniversaries-table', [ 162 'facts' => $facts, 163 'limit_low' => self::LIMIT_LOW, 164 'limit_high' => self::LIMIT_HIGH, 165 'order' => self::DATATABLES_ORDER[$sortStyle] ?? self::DATATABLES_ORDER[self::DEFAULT_SORT], 166 ]); 167 } 168 169 if ($context !== self::CONTEXT_EMBED) { 170 return view('modules/block-template', [ 171 'block' => Str::kebab($this->name()), 172 'id' => $block_id, 173 'config_url' => $this->configUrl($tree, $context, $block_id), 174 'title' => $this->title(), 175 'content' => $content, 176 ]); 177 } 178 179 return $content; 180 } 181 182 /** 183 * Should this block load asynchronously using AJAX? 184 * 185 * Simple blocks are faster in-line, more complex ones can be loaded later. 186 * 187 * @return bool 188 */ 189 public function loadAjax(): bool 190 { 191 return true; 192 } 193 194 /** 195 * Can this block be shown on the user’s home page? 196 * 197 * @return bool 198 */ 199 public function isUserBlock(): bool 200 { 201 return true; 202 } 203 204 /** 205 * Can this block be shown on the tree’s home page? 206 * 207 * @return bool 208 */ 209 public function isTreeBlock(): bool 210 { 211 return true; 212 } 213 214 /** 215 * Update the configuration for a block. 216 * 217 * @param ServerRequestInterface $request 218 * @param int $block_id 219 * 220 * @return void 221 */ 222 public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 223 { 224 $params = (array) $request->getParsedBody(); 225 226 $this->setBlockSetting($block_id, 'filter', $params['filter']); 227 $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']); 228 $this->setBlockSetting($block_id, 'sortStyle', $params['sortStyle']); 229 $this->setBlockSetting($block_id, 'events', implode(',', $params['events'] ?? [])); 230 } 231 232 /** 233 * An HTML form to edit block settings 234 * 235 * @param Tree $tree 236 * @param int $block_id 237 * 238 * @return string 239 */ 240 public function editBlockConfiguration(Tree $tree, int $block_id): string 241 { 242 $default_events = implode(',', self::DEFAULT_EVENTS); 243 244 $filter = $this->getBlockSetting($block_id, 'filter', '1'); 245 $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 246 $sort_style = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT); 247 $events = $this->getBlockSetting($block_id, 'events', $default_events); 248 249 $event_array = explode(',', $events); 250 251 $all_events = []; 252 foreach (self::ALL_EVENTS as $event => $tag) { 253 $all_events[$event] = Registry::elementFactory()->make($tag)->label(); 254 } 255 256 $info_styles = [ 257 /* I18N: An option in a list-box */ 258 'list' => I18N::translate('list'), 259 /* I18N: An option in a list-box */ 260 'table' => I18N::translate('table'), 261 ]; 262 263 $sort_styles = [ 264 /* I18N: An option in a list-box */ 265 'alpha' => I18N::translate('sort by name'), 266 /* I18N: An option in a list-box */ 267 'anniv_asc' => I18N::translate('sort by date, oldest first'), 268 /* I18N: An option in a list-box */ 269 'anniv_desc' => I18N::translate('sort by date, newest first'), 270 ]; 271 272 return view('modules/todays_events/config', [ 273 'all_events' => $all_events, 274 'event_array' => $event_array, 275 'filter' => $filter, 276 'info_style' => $info_style, 277 'info_styles' => $info_styles, 278 'sort_style' => $sort_style, 279 'sort_styles' => $sort_styles, 280 ]); 281 } 282} 283