18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 55bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team 68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 98c2e8227SGreg Roach * (at your option) any later version. 108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 138c2e8227SGreg Roach * GNU General Public License for more details. 148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 168c2e8227SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 2176692c8bSGreg Roach 2205cb9775SGreg Roachuse Fisharebest\Webtrees\Auth; 238d0ebef0SGreg Roachuse Fisharebest\Webtrees\Gedcom; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 2569cdf014SGreg Roachuse Fisharebest\Webtrees\Registry; 26f0a11419SGreg Roachuse Fisharebest\Webtrees\Services\CalendarService; 27e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 28*748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator; 291e7a7a28SGreg Roachuse Illuminate\Support\Str; 306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 318c2e8227SGreg Roach 328c2e8227SGreg Roach/** 338c2e8227SGreg Roach * Class OnThisDayModule 348c2e8227SGreg Roach */ 3537eb8894SGreg Roachclass OnThisDayModule extends AbstractModule implements ModuleBlockInterface 36c1010edaSGreg Roach{ 3749a243cbSGreg Roach use ModuleBlockTrait; 3849a243cbSGreg Roach 3901221f27SGreg Roach // Pagination 4001221f27SGreg Roach private const LIMIT_LOW = 10; 4101221f27SGreg Roach private const LIMIT_HIGH = 20; 4201221f27SGreg Roach 436f97ab23SGreg Roach // Default values for new blocks. 446f97ab23SGreg Roach private const DEFAULT_SORT = 'date_desc'; 456f97ab23SGreg Roach private const DEFAULT_STYLE = 'date_desc'; 466f97ab23SGreg Roach 470999990bSGreg Roach // Initial sorting for datatables 480999990bSGreg Roach private const DATATABLES_ORDER = [ 490999990bSGreg Roach 'alpha' => [[0, 'asc']], 506f97ab23SGreg Roach 'date_asc' => [[2, 'asc']], 516f97ab23SGreg Roach 'date_desc' => [[2, 'desc']], 520999990bSGreg Roach ]; 530999990bSGreg Roach 5412664b30SGreg Roach // All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN 5516d6367aSGreg Roach private const ALL_EVENTS = [ 5669cdf014SGreg Roach 'ADOP' => 'INDI:ADOP', 5769cdf014SGreg Roach 'ANUL' => 'FAM:ANUL', 5869cdf014SGreg Roach 'BAPM' => 'INDI:BAPM', 5969cdf014SGreg Roach 'BARM' => 'INDI:BARM', 6069cdf014SGreg Roach 'BASM' => 'INDI:BASM', 6169cdf014SGreg Roach 'BIRT' => 'INDI:BIRT', 6269cdf014SGreg Roach 'BLES' => 'INDI:BLES', 6369cdf014SGreg Roach 'BURI' => 'INDI:BURI', 6469cdf014SGreg Roach 'CHR' => 'INDI:CHR', 6569cdf014SGreg Roach 'CHRA' => 'INDI:CHRA', 6669cdf014SGreg Roach 'CONF' => 'INDI:CONF', 6769cdf014SGreg Roach 'CREM' => 'INDI:CREM', 6869cdf014SGreg Roach 'DEAT' => 'INDI:DEAT', 6969cdf014SGreg Roach 'DIV' => 'FAM:DIV', 7069cdf014SGreg Roach 'DIVF' => 'FAM:DIVF', 7169cdf014SGreg Roach 'EMIG' => 'INDI:EMIG', 7269cdf014SGreg Roach 'ENGA' => 'FAM:ENGA', 7369cdf014SGreg Roach 'FCOM' => 'INDI:FCOM', 7469cdf014SGreg Roach 'GRAD' => 'INDI:GRAD', 7569cdf014SGreg Roach 'IMMI' => 'INDI:IMMI', 7669cdf014SGreg Roach 'MARB' => 'FAM:MARB', 7769cdf014SGreg Roach 'MARC' => 'FAM:MARC', 7869cdf014SGreg Roach 'MARL' => 'FAM:MARL', 7969cdf014SGreg Roach 'MARR' => 'FAM:MARR', 8069cdf014SGreg Roach 'MARS' => 'FAM:MARS', 8169cdf014SGreg Roach 'NATU' => 'INDI:NATU', 8269cdf014SGreg Roach 'ORDN' => 'INDI:ORDN', 8369cdf014SGreg Roach 'PROB' => 'INDI:PROB', 8469cdf014SGreg Roach 'RETI' => 'INDI:RETI', 8569cdf014SGreg Roach 'WILL' => 'INDI:WILL', 8612664b30SGreg Roach ]; 8712664b30SGreg Roach 8816d6367aSGreg Roach private const DEFAULT_EVENTS = [ 8912664b30SGreg Roach 'BIRT', 9012664b30SGreg Roach 'MARR', 9112664b30SGreg Roach 'DEAT', 9212664b30SGreg Roach ]; 9312664b30SGreg Roach 9412664b30SGreg Roach /** 950cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 9612664b30SGreg Roach * 9712664b30SGreg Roach * @return string 9812664b30SGreg Roach */ 9949a243cbSGreg Roach public function title(): string 100c1010edaSGreg Roach { 101bbb76c12SGreg Roach /* I18N: Name of a module */ 102bbb76c12SGreg Roach return I18N::translate('On this day'); 1038c2e8227SGreg Roach } 1048c2e8227SGreg Roach 10512664b30SGreg Roach /** 10612664b30SGreg Roach * A sentence describing what this module does. 10712664b30SGreg Roach * 10812664b30SGreg Roach * @return string 10912664b30SGreg Roach */ 11049a243cbSGreg Roach public function description(): string 111c1010edaSGreg Roach { 112bbb76c12SGreg Roach /* I18N: Description of the “On this day” module */ 113bbb76c12SGreg Roach return I18N::translate('A list of the anniversaries that occur today.'); 1148c2e8227SGreg Roach } 1158c2e8227SGreg Roach 11676692c8bSGreg Roach /** 11776692c8bSGreg Roach * Generate the HTML content of this block. 11876692c8bSGreg Roach * 119e490cd80SGreg Roach * @param Tree $tree 12076692c8bSGreg Roach * @param int $block_id 1213caaa4d2SGreg Roach * @param string $context 12276d39c55SGreg Roach * @param array<string,string> $config 12376692c8bSGreg Roach * 12476692c8bSGreg Roach * @return string 12576692c8bSGreg Roach */ 1263caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 127c1010edaSGreg Roach { 128f0a11419SGreg Roach $calendar_service = new CalendarService(); 129f0a11419SGreg Roach 13012664b30SGreg Roach $default_events = implode(',', self::DEFAULT_EVENTS); 13112664b30SGreg Roach 13212664b30SGreg Roach $filter = (bool) $this->getBlockSetting($block_id, 'filter', '1'); 1336f97ab23SGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 1346f97ab23SGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT); 13512664b30SGreg Roach $events = $this->getBlockSetting($block_id, 'events', $default_events); 1368c2e8227SGreg Roach 1373caaa4d2SGreg Roach extract($config, EXTR_OVERWRITE); 1388c2e8227SGreg Roach 13912664b30SGreg Roach $event_array = explode(',', $events); 14012664b30SGreg Roach 14112664b30SGreg Roach // If we are only showing living individuals, then we don't need to search for DEAT events. 14212664b30SGreg Roach if ($filter) { 1438d0ebef0SGreg Roach $event_array = array_diff($event_array, Gedcom::DEATH_EVENTS); 14412664b30SGreg Roach } 14512664b30SGreg Roach 14612664b30SGreg Roach $events_filter = implode('|', $event_array); 14712664b30SGreg Roach 148d97083feSGreg Roach $startjd = Registry::timestampFactory()->now()->julianDay(); 149269fd10dSGreg Roach $endjd = $startjd; 15012664b30SGreg Roach 151f0a11419SGreg Roach $facts = $calendar_service->getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $tree); 15212664b30SGreg Roach 153cd51dbdfSGreg Roach if ($facts->isEmpty()) { 15405cb9775SGreg Roach if ($filter && Auth::check()) { 155b13a72dbSGreg Roach $message = I18N::translate('No events for living individuals exist for today.'); 156b13a72dbSGreg Roach } else { 157b13a72dbSGreg Roach $message = I18N::translate('No events exist for today.'); 158b13a72dbSGreg Roach } 159b13a72dbSGreg Roach $content = view('modules/todays_events/empty', ['message' => $message]); 160147e99aaSGreg Roach } elseif ($infoStyle === 'list') { 16148f08416SGreg Roach $content = view('lists/anniversaries-list', [ 162e24053e5SGreg Roach 'id' => $block_id, 163147e99aaSGreg Roach 'facts' => $facts, 16401221f27SGreg Roach 'limit_low' => self::LIMIT_LOW, 16501221f27SGreg Roach 'limit_high' => self::LIMIT_HIGH, 1660280f44aSGreg Roach ]); 167d8189b6aSDavid Drury } else { 16848f08416SGreg Roach $content = view('lists/anniversaries-table', [ 169d8189b6aSDavid Drury 'facts' => $facts, 17001221f27SGreg Roach 'limit_low' => self::LIMIT_LOW, 17101221f27SGreg Roach 'limit_high' => self::LIMIT_HIGH, 1726f97ab23SGreg Roach 'order' => self::DATATABLES_ORDER[$sortStyle] ?? self::DATATABLES_ORDER[self::DEFAULT_SORT], 1730280f44aSGreg Roach ]); 1740280f44aSGreg Roach } 1758c2e8227SGreg Roach 1763caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 177147e99aaSGreg Roach return view('modules/block-template', [ 1781e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1799c6524dcSGreg Roach 'id' => $block_id, 1803caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 18149a243cbSGreg Roach 'title' => $this->title(), 1829c6524dcSGreg Roach 'content' => $content, 18312664b30SGreg Roach ]); 1848c2e8227SGreg Roach } 185b2ce94c6SRico Sonntag 186b2ce94c6SRico Sonntag return $content; 1878c2e8227SGreg Roach } 1888c2e8227SGreg Roach 1893caaa4d2SGreg Roach /** 1903caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 1913caaa4d2SGreg Roach * 1923caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 1933caaa4d2SGreg Roach * 1943caaa4d2SGreg Roach * @return bool 1953caaa4d2SGreg Roach */ 196c1010edaSGreg Roach public function loadAjax(): bool 197c1010edaSGreg Roach { 1988c2e8227SGreg Roach return true; 1998c2e8227SGreg Roach } 2008c2e8227SGreg Roach 2013caaa4d2SGreg Roach /** 2023caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 2033caaa4d2SGreg Roach * 2043caaa4d2SGreg Roach * @return bool 2053caaa4d2SGreg Roach */ 206c1010edaSGreg Roach public function isUserBlock(): bool 207c1010edaSGreg Roach { 2088c2e8227SGreg Roach return true; 2098c2e8227SGreg Roach } 2108c2e8227SGreg Roach 2113caaa4d2SGreg Roach /** 2123caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 2133caaa4d2SGreg Roach * 2143caaa4d2SGreg Roach * @return bool 2153caaa4d2SGreg Roach */ 21663276d8fSGreg Roach public function isTreeBlock(): bool 217c1010edaSGreg Roach { 2188c2e8227SGreg Roach return true; 2198c2e8227SGreg Roach } 2208c2e8227SGreg Roach 22176692c8bSGreg Roach /** 222a45f9889SGreg Roach * Update the configuration for a block. 223a45f9889SGreg Roach * 2246ccdf4f0SGreg Roach * @param ServerRequestInterface $request 225a45f9889SGreg Roach * @param int $block_id 226a45f9889SGreg Roach * 227a45f9889SGreg Roach * @return void 228a45f9889SGreg Roach */ 2296ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 230a45f9889SGreg Roach { 231*748dbe15SGreg Roach $filter = Validator::parsedBody($request)->string('filter'); 232*748dbe15SGreg Roach $info_style = Validator::parsedBody($request)->string('infoStyle'); 233*748dbe15SGreg Roach $sort_style = Validator::parsedBody($request)->string('sortStyle'); 234*748dbe15SGreg Roach $events = Validator::parsedBody($request)->array('events'); 235e106ff43SGreg Roach 236*748dbe15SGreg Roach $this->setBlockSetting($block_id, 'filter', $filter); 237*748dbe15SGreg Roach $this->setBlockSetting($block_id, 'infoStyle', $info_style); 238*748dbe15SGreg Roach $this->setBlockSetting($block_id, 'sortStyle', $sort_style); 239*748dbe15SGreg Roach $this->setBlockSetting($block_id, 'events', implode(',', $events)); 240a45f9889SGreg Roach } 241a45f9889SGreg Roach 242a45f9889SGreg Roach /** 24376692c8bSGreg Roach * An HTML form to edit block settings 24476692c8bSGreg Roach * 245e490cd80SGreg Roach * @param Tree $tree 24676692c8bSGreg Roach * @param int $block_id 247a9430be8SGreg Roach * 2483caaa4d2SGreg Roach * @return string 24976692c8bSGreg Roach */ 2503caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 251c1010edaSGreg Roach { 25212664b30SGreg Roach $default_events = implode(',', self::DEFAULT_EVENTS); 25312664b30SGreg Roach 254e2a378d3SGreg Roach $filter = $this->getBlockSetting($block_id, 'filter', '1'); 2557c2c99faSGreg Roach $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 2567c2c99faSGreg Roach $sort_style = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT); 25712664b30SGreg Roach $events = $this->getBlockSetting($block_id, 'events', $default_events); 2588c2e8227SGreg Roach 25912664b30SGreg Roach $event_array = explode(',', $events); 26012664b30SGreg Roach 26112664b30SGreg Roach $all_events = []; 26269cdf014SGreg Roach foreach (self::ALL_EVENTS as $event => $tag) { 26369cdf014SGreg Roach $all_events[$event] = Registry::elementFactory()->make($tag)->label(); 26412664b30SGreg Roach } 26512664b30SGreg Roach 266c385536dSGreg Roach $info_styles = [ 267bbb76c12SGreg Roach /* I18N: An option in a list-box */ 268bbb76c12SGreg Roach 'list' => I18N::translate('list'), 269bbb76c12SGreg Roach /* I18N: An option in a list-box */ 270bbb76c12SGreg Roach 'table' => I18N::translate('table'), 271c385536dSGreg Roach ]; 2728c2e8227SGreg Roach 273c385536dSGreg Roach $sort_styles = [ 274bbb76c12SGreg Roach /* I18N: An option in a list-box */ 275bbb76c12SGreg Roach 'alpha' => I18N::translate('sort by name'), 276bbb76c12SGreg Roach /* I18N: An option in a list-box */ 2776f97ab23SGreg Roach 'anniv_asc' => I18N::translate('sort by date, oldest first'), 2786f97ab23SGreg Roach /* I18N: An option in a list-box */ 2796f97ab23SGreg Roach 'anniv_desc' => I18N::translate('sort by date, newest first'), 280c385536dSGreg Roach ]; 281d8189b6aSDavid Drury 2823caaa4d2SGreg Roach return view('modules/todays_events/config', [ 283c385536dSGreg Roach 'all_events' => $all_events, 284c385536dSGreg Roach 'event_array' => $event_array, 285c385536dSGreg Roach 'filter' => $filter, 2867c2c99faSGreg Roach 'info_style' => $info_style, 287c385536dSGreg Roach 'info_styles' => $info_styles, 2887c2c99faSGreg Roach 'sort_style' => $sort_style, 289c385536dSGreg Roach 'sort_styles' => $sort_styles, 290c385536dSGreg Roach ]); 2918c2e8227SGreg Roach } 2928c2e8227SGreg Roach} 293