18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 5*d11be702SGreg Roach * Copyright (C) 2023 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; 28748dbe15SGreg 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 10549a243cbSGreg Roach public function description(): string 106c1010edaSGreg Roach { 107bbb76c12SGreg Roach /* I18N: Description of the “On this day” module */ 108bbb76c12SGreg Roach return I18N::translate('A list of the anniversaries that occur today.'); 1098c2e8227SGreg Roach } 1108c2e8227SGreg Roach 11176692c8bSGreg Roach /** 11276692c8bSGreg Roach * Generate the HTML content of this block. 11376692c8bSGreg Roach * 114e490cd80SGreg Roach * @param Tree $tree 11576692c8bSGreg Roach * @param int $block_id 1163caaa4d2SGreg Roach * @param string $context 11776d39c55SGreg Roach * @param array<string,string> $config 11876692c8bSGreg Roach * 11976692c8bSGreg Roach * @return string 12076692c8bSGreg Roach */ 1213caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 122c1010edaSGreg Roach { 123f0a11419SGreg Roach $calendar_service = new CalendarService(); 124f0a11419SGreg Roach 12512664b30SGreg Roach $default_events = implode(',', self::DEFAULT_EVENTS); 12612664b30SGreg Roach 12712664b30SGreg Roach $filter = (bool) $this->getBlockSetting($block_id, 'filter', '1'); 1286f97ab23SGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 1296f97ab23SGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT); 13012664b30SGreg Roach $events = $this->getBlockSetting($block_id, 'events', $default_events); 1318c2e8227SGreg Roach 1323caaa4d2SGreg Roach extract($config, EXTR_OVERWRITE); 1338c2e8227SGreg Roach 13412664b30SGreg Roach $event_array = explode(',', $events); 13512664b30SGreg Roach 13612664b30SGreg Roach // If we are only showing living individuals, then we don't need to search for DEAT events. 13712664b30SGreg Roach if ($filter) { 1388d0ebef0SGreg Roach $event_array = array_diff($event_array, Gedcom::DEATH_EVENTS); 13912664b30SGreg Roach } 14012664b30SGreg Roach 14112664b30SGreg Roach $events_filter = implode('|', $event_array); 14212664b30SGreg Roach 143d97083feSGreg Roach $startjd = Registry::timestampFactory()->now()->julianDay(); 144269fd10dSGreg Roach $endjd = $startjd; 14512664b30SGreg Roach 146f0a11419SGreg Roach $facts = $calendar_service->getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $tree); 14712664b30SGreg Roach 148cd51dbdfSGreg Roach if ($facts->isEmpty()) { 14905cb9775SGreg Roach if ($filter && Auth::check()) { 150b13a72dbSGreg Roach $message = I18N::translate('No events for living individuals exist for today.'); 151b13a72dbSGreg Roach } else { 152b13a72dbSGreg Roach $message = I18N::translate('No events exist for today.'); 153b13a72dbSGreg Roach } 154b13a72dbSGreg Roach $content = view('modules/todays_events/empty', ['message' => $message]); 155147e99aaSGreg Roach } elseif ($infoStyle === 'list') { 15648f08416SGreg Roach $content = view('lists/anniversaries-list', [ 157e24053e5SGreg Roach 'id' => $block_id, 158147e99aaSGreg Roach 'facts' => $facts, 15901221f27SGreg Roach 'limit_low' => self::LIMIT_LOW, 16001221f27SGreg Roach 'limit_high' => self::LIMIT_HIGH, 1610280f44aSGreg Roach ]); 162d8189b6aSDavid Drury } else { 16348f08416SGreg Roach $content = view('lists/anniversaries-table', [ 164d8189b6aSDavid Drury 'facts' => $facts, 16501221f27SGreg Roach 'limit_low' => self::LIMIT_LOW, 16601221f27SGreg Roach 'limit_high' => self::LIMIT_HIGH, 1676f97ab23SGreg Roach 'order' => self::DATATABLES_ORDER[$sortStyle] ?? self::DATATABLES_ORDER[self::DEFAULT_SORT], 1680280f44aSGreg Roach ]); 1690280f44aSGreg Roach } 1708c2e8227SGreg Roach 1713caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 172147e99aaSGreg Roach return view('modules/block-template', [ 1731e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1749c6524dcSGreg Roach 'id' => $block_id, 1753caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 17649a243cbSGreg Roach 'title' => $this->title(), 1779c6524dcSGreg Roach 'content' => $content, 17812664b30SGreg Roach ]); 1798c2e8227SGreg Roach } 180b2ce94c6SRico Sonntag 181b2ce94c6SRico Sonntag return $content; 1828c2e8227SGreg Roach } 1838c2e8227SGreg Roach 1843caaa4d2SGreg Roach /** 1853caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 1863caaa4d2SGreg Roach * 1873caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 1883caaa4d2SGreg Roach * 1893caaa4d2SGreg Roach * @return bool 1903caaa4d2SGreg Roach */ 191c1010edaSGreg Roach public function loadAjax(): bool 192c1010edaSGreg Roach { 1938c2e8227SGreg Roach return true; 1948c2e8227SGreg Roach } 1958c2e8227SGreg Roach 1963caaa4d2SGreg Roach /** 1973caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 1983caaa4d2SGreg Roach * 1993caaa4d2SGreg Roach * @return bool 2003caaa4d2SGreg Roach */ 201c1010edaSGreg Roach public function isUserBlock(): bool 202c1010edaSGreg Roach { 2038c2e8227SGreg Roach return true; 2048c2e8227SGreg Roach } 2058c2e8227SGreg Roach 2063caaa4d2SGreg Roach /** 2073caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 2083caaa4d2SGreg Roach * 2093caaa4d2SGreg Roach * @return bool 2103caaa4d2SGreg Roach */ 21163276d8fSGreg Roach public function isTreeBlock(): bool 212c1010edaSGreg Roach { 2138c2e8227SGreg Roach return true; 2148c2e8227SGreg Roach } 2158c2e8227SGreg Roach 21676692c8bSGreg Roach /** 217a45f9889SGreg Roach * Update the configuration for a block. 218a45f9889SGreg Roach * 2196ccdf4f0SGreg Roach * @param ServerRequestInterface $request 220a45f9889SGreg Roach * @param int $block_id 221a45f9889SGreg Roach * 222a45f9889SGreg Roach * @return void 223a45f9889SGreg Roach */ 2246ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 225a45f9889SGreg Roach { 226748dbe15SGreg Roach $filter = Validator::parsedBody($request)->string('filter'); 227748dbe15SGreg Roach $info_style = Validator::parsedBody($request)->string('infoStyle'); 228748dbe15SGreg Roach $sort_style = Validator::parsedBody($request)->string('sortStyle'); 229748dbe15SGreg Roach $events = Validator::parsedBody($request)->array('events'); 230e106ff43SGreg Roach 231748dbe15SGreg Roach $this->setBlockSetting($block_id, 'filter', $filter); 232748dbe15SGreg Roach $this->setBlockSetting($block_id, 'infoStyle', $info_style); 233748dbe15SGreg Roach $this->setBlockSetting($block_id, 'sortStyle', $sort_style); 234748dbe15SGreg Roach $this->setBlockSetting($block_id, 'events', implode(',', $events)); 235a45f9889SGreg Roach } 236a45f9889SGreg Roach 237a45f9889SGreg Roach /** 23876692c8bSGreg Roach * An HTML form to edit block settings 23976692c8bSGreg Roach * 240e490cd80SGreg Roach * @param Tree $tree 24176692c8bSGreg Roach * @param int $block_id 242a9430be8SGreg Roach * 2433caaa4d2SGreg Roach * @return string 24476692c8bSGreg Roach */ 2453caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 246c1010edaSGreg Roach { 24712664b30SGreg Roach $default_events = implode(',', self::DEFAULT_EVENTS); 24812664b30SGreg Roach 249e2a378d3SGreg Roach $filter = $this->getBlockSetting($block_id, 'filter', '1'); 2507c2c99faSGreg Roach $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 2517c2c99faSGreg Roach $sort_style = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT); 25212664b30SGreg Roach $events = $this->getBlockSetting($block_id, 'events', $default_events); 2538c2e8227SGreg Roach 25412664b30SGreg Roach $event_array = explode(',', $events); 25512664b30SGreg Roach 25612664b30SGreg Roach $all_events = []; 25769cdf014SGreg Roach foreach (self::ALL_EVENTS as $event => $tag) { 25869cdf014SGreg Roach $all_events[$event] = Registry::elementFactory()->make($tag)->label(); 25912664b30SGreg Roach } 26012664b30SGreg Roach 261c385536dSGreg Roach $info_styles = [ 262bbb76c12SGreg Roach /* I18N: An option in a list-box */ 263bbb76c12SGreg Roach 'list' => I18N::translate('list'), 264bbb76c12SGreg Roach /* I18N: An option in a list-box */ 265bbb76c12SGreg Roach 'table' => I18N::translate('table'), 266c385536dSGreg Roach ]; 2678c2e8227SGreg Roach 268c385536dSGreg Roach $sort_styles = [ 269bbb76c12SGreg Roach /* I18N: An option in a list-box */ 270bbb76c12SGreg Roach 'alpha' => I18N::translate('sort by name'), 271bbb76c12SGreg Roach /* I18N: An option in a list-box */ 2726f97ab23SGreg Roach 'anniv_asc' => I18N::translate('sort by date, oldest first'), 2736f97ab23SGreg Roach /* I18N: An option in a list-box */ 2746f97ab23SGreg Roach 'anniv_desc' => I18N::translate('sort by date, newest first'), 275c385536dSGreg Roach ]; 276d8189b6aSDavid Drury 2773caaa4d2SGreg Roach return view('modules/todays_events/config', [ 278c385536dSGreg Roach 'all_events' => $all_events, 279c385536dSGreg Roach 'event_array' => $event_array, 280c385536dSGreg Roach 'filter' => $filter, 2817c2c99faSGreg Roach 'info_style' => $info_style, 282c385536dSGreg Roach 'info_styles' => $info_styles, 2837c2c99faSGreg Roach 'sort_style' => $sort_style, 284c385536dSGreg Roach 'sort_styles' => $sort_styles, 285c385536dSGreg Roach ]); 2868c2e8227SGreg Roach } 2878c2e8227SGreg Roach} 288