18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 5*5bfc6897SGreg 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 228d0ebef0SGreg Roachuse Fisharebest\Webtrees\Gedcom; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 2469cdf014SGreg Roachuse Fisharebest\Webtrees\Registry; 25f0a11419SGreg Roachuse Fisharebest\Webtrees\Services\CalendarService; 26e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 271e7a7a28SGreg Roachuse Illuminate\Support\Str; 286ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 298c2e8227SGreg Roach 308c2e8227SGreg Roach/** 318c2e8227SGreg Roach * Class OnThisDayModule 328c2e8227SGreg Roach */ 3337eb8894SGreg Roachclass OnThisDayModule extends AbstractModule implements ModuleBlockInterface 34c1010edaSGreg Roach{ 3549a243cbSGreg Roach use ModuleBlockTrait; 3649a243cbSGreg Roach 3701221f27SGreg Roach // Pagination 3801221f27SGreg Roach private const LIMIT_LOW = 10; 3901221f27SGreg Roach private const LIMIT_HIGH = 20; 4001221f27SGreg Roach 416f97ab23SGreg Roach // Default values for new blocks. 426f97ab23SGreg Roach private const DEFAULT_SORT = 'date_desc'; 436f97ab23SGreg Roach private const DEFAULT_STYLE = 'date_desc'; 446f97ab23SGreg Roach 450999990bSGreg Roach // Initial sorting for datatables 460999990bSGreg Roach private const DATATABLES_ORDER = [ 470999990bSGreg Roach 'alpha' => [[0, 'asc']], 486f97ab23SGreg Roach 'date_asc' => [[2, 'asc']], 496f97ab23SGreg Roach 'date_desc' => [[2, 'desc']], 500999990bSGreg Roach ]; 510999990bSGreg Roach 5212664b30SGreg Roach // All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN 5316d6367aSGreg Roach private const ALL_EVENTS = [ 5469cdf014SGreg Roach 'ADOP' => 'INDI:ADOP', 5569cdf014SGreg Roach 'ANUL' => 'FAM:ANUL', 5669cdf014SGreg Roach 'BAPM' => 'INDI:BAPM', 5769cdf014SGreg Roach 'BARM' => 'INDI:BARM', 5869cdf014SGreg Roach 'BASM' => 'INDI:BASM', 5969cdf014SGreg Roach 'BIRT' => 'INDI:BIRT', 6069cdf014SGreg Roach 'BLES' => 'INDI:BLES', 6169cdf014SGreg Roach 'BURI' => 'INDI:BURI', 6269cdf014SGreg Roach 'CHR' => 'INDI:CHR', 6369cdf014SGreg Roach 'CHRA' => 'INDI:CHRA', 6469cdf014SGreg Roach 'CONF' => 'INDI:CONF', 6569cdf014SGreg Roach 'CREM' => 'INDI:CREM', 6669cdf014SGreg Roach 'DEAT' => 'INDI:DEAT', 6769cdf014SGreg Roach 'DIV' => 'FAM:DIV', 6869cdf014SGreg Roach 'DIVF' => 'FAM:DIVF', 6969cdf014SGreg Roach 'EMIG' => 'INDI:EMIG', 7069cdf014SGreg Roach 'ENGA' => 'FAM:ENGA', 7169cdf014SGreg Roach 'FCOM' => 'INDI:FCOM', 7269cdf014SGreg Roach 'GRAD' => 'INDI:GRAD', 7369cdf014SGreg Roach 'IMMI' => 'INDI:IMMI', 7469cdf014SGreg Roach 'MARB' => 'FAM:MARB', 7569cdf014SGreg Roach 'MARC' => 'FAM:MARC', 7669cdf014SGreg Roach 'MARL' => 'FAM:MARL', 7769cdf014SGreg Roach 'MARR' => 'FAM:MARR', 7869cdf014SGreg Roach 'MARS' => 'FAM:MARS', 7969cdf014SGreg Roach 'NATU' => 'INDI:NATU', 8069cdf014SGreg Roach 'ORDN' => 'INDI:ORDN', 8169cdf014SGreg Roach 'PROB' => 'INDI:PROB', 8269cdf014SGreg Roach 'RETI' => 'INDI:RETI', 8369cdf014SGreg Roach 'WILL' => 'INDI:WILL', 8412664b30SGreg Roach ]; 8512664b30SGreg Roach 8616d6367aSGreg Roach private const DEFAULT_EVENTS = [ 8712664b30SGreg Roach 'BIRT', 8812664b30SGreg Roach 'MARR', 8912664b30SGreg Roach 'DEAT', 9012664b30SGreg Roach ]; 9112664b30SGreg Roach 9212664b30SGreg Roach /** 930cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 9412664b30SGreg Roach * 9512664b30SGreg Roach * @return string 9612664b30SGreg Roach */ 9749a243cbSGreg Roach public function title(): string 98c1010edaSGreg Roach { 99bbb76c12SGreg Roach /* I18N: Name of a module */ 100bbb76c12SGreg Roach return I18N::translate('On this day'); 1018c2e8227SGreg Roach } 1028c2e8227SGreg Roach 10312664b30SGreg Roach /** 10412664b30SGreg Roach * A sentence describing what this module does. 10512664b30SGreg Roach * 10612664b30SGreg Roach * @return string 10712664b30SGreg Roach */ 10849a243cbSGreg Roach public function description(): string 109c1010edaSGreg Roach { 110bbb76c12SGreg Roach /* I18N: Description of the “On this day” module */ 111bbb76c12SGreg Roach return I18N::translate('A list of the anniversaries that occur today.'); 1128c2e8227SGreg Roach } 1138c2e8227SGreg Roach 11476692c8bSGreg Roach /** 11576692c8bSGreg Roach * Generate the HTML content of this block. 11676692c8bSGreg Roach * 117e490cd80SGreg Roach * @param Tree $tree 11876692c8bSGreg Roach * @param int $block_id 1193caaa4d2SGreg Roach * @param string $context 12076d39c55SGreg Roach * @param array<string,string> $config 12176692c8bSGreg Roach * 12276692c8bSGreg Roach * @return string 12376692c8bSGreg Roach */ 1243caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 125c1010edaSGreg Roach { 126f0a11419SGreg Roach $calendar_service = new CalendarService(); 127f0a11419SGreg Roach 12812664b30SGreg Roach $default_events = implode(',', self::DEFAULT_EVENTS); 12912664b30SGreg Roach 13012664b30SGreg Roach $filter = (bool) $this->getBlockSetting($block_id, 'filter', '1'); 1316f97ab23SGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 1326f97ab23SGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT); 13312664b30SGreg Roach $events = $this->getBlockSetting($block_id, 'events', $default_events); 1348c2e8227SGreg Roach 1353caaa4d2SGreg Roach extract($config, EXTR_OVERWRITE); 1368c2e8227SGreg Roach 13712664b30SGreg Roach $event_array = explode(',', $events); 13812664b30SGreg Roach 13912664b30SGreg Roach // If we are only showing living individuals, then we don't need to search for DEAT events. 14012664b30SGreg Roach if ($filter) { 1418d0ebef0SGreg Roach $event_array = array_diff($event_array, Gedcom::DEATH_EVENTS); 14212664b30SGreg Roach } 14312664b30SGreg Roach 14412664b30SGreg Roach $events_filter = implode('|', $event_array); 14512664b30SGreg Roach 146d97083feSGreg Roach $startjd = Registry::timestampFactory()->now()->julianDay(); 147269fd10dSGreg Roach $endjd = $startjd; 14812664b30SGreg Roach 149f0a11419SGreg Roach $facts = $calendar_service->getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $tree); 15012664b30SGreg Roach 151cd51dbdfSGreg Roach if ($facts->isEmpty()) { 152147e99aaSGreg Roach $content = view('modules/todays_events/empty'); 153147e99aaSGreg Roach } elseif ($infoStyle === 'list') { 15448f08416SGreg Roach $content = view('lists/anniversaries-list', [ 155e24053e5SGreg Roach 'id' => $block_id, 156147e99aaSGreg Roach 'facts' => $facts, 15701221f27SGreg Roach 'limit_low' => self::LIMIT_LOW, 15801221f27SGreg Roach 'limit_high' => self::LIMIT_HIGH, 1590280f44aSGreg Roach ]); 160d8189b6aSDavid Drury } else { 16148f08416SGreg Roach $content = view('lists/anniversaries-table', [ 162d8189b6aSDavid Drury 'facts' => $facts, 16301221f27SGreg Roach 'limit_low' => self::LIMIT_LOW, 16401221f27SGreg Roach 'limit_high' => self::LIMIT_HIGH, 1656f97ab23SGreg Roach 'order' => self::DATATABLES_ORDER[$sortStyle] ?? self::DATATABLES_ORDER[self::DEFAULT_SORT], 1660280f44aSGreg Roach ]); 1670280f44aSGreg Roach } 1688c2e8227SGreg Roach 1693caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 170147e99aaSGreg Roach return view('modules/block-template', [ 1711e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1729c6524dcSGreg Roach 'id' => $block_id, 1733caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 17449a243cbSGreg Roach 'title' => $this->title(), 1759c6524dcSGreg Roach 'content' => $content, 17612664b30SGreg Roach ]); 1778c2e8227SGreg Roach } 178b2ce94c6SRico Sonntag 179b2ce94c6SRico Sonntag return $content; 1808c2e8227SGreg Roach } 1818c2e8227SGreg Roach 1823caaa4d2SGreg Roach /** 1833caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 1843caaa4d2SGreg Roach * 1853caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 1863caaa4d2SGreg Roach * 1873caaa4d2SGreg Roach * @return bool 1883caaa4d2SGreg Roach */ 189c1010edaSGreg Roach public function loadAjax(): bool 190c1010edaSGreg Roach { 1918c2e8227SGreg Roach return true; 1928c2e8227SGreg Roach } 1938c2e8227SGreg Roach 1943caaa4d2SGreg Roach /** 1953caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 1963caaa4d2SGreg Roach * 1973caaa4d2SGreg Roach * @return bool 1983caaa4d2SGreg Roach */ 199c1010edaSGreg Roach public function isUserBlock(): bool 200c1010edaSGreg Roach { 2018c2e8227SGreg Roach return true; 2028c2e8227SGreg Roach } 2038c2e8227SGreg Roach 2043caaa4d2SGreg Roach /** 2053caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 2063caaa4d2SGreg Roach * 2073caaa4d2SGreg Roach * @return bool 2083caaa4d2SGreg Roach */ 20963276d8fSGreg Roach public function isTreeBlock(): bool 210c1010edaSGreg Roach { 2118c2e8227SGreg Roach return true; 2128c2e8227SGreg Roach } 2138c2e8227SGreg Roach 21476692c8bSGreg Roach /** 215a45f9889SGreg Roach * Update the configuration for a block. 216a45f9889SGreg Roach * 2176ccdf4f0SGreg Roach * @param ServerRequestInterface $request 218a45f9889SGreg Roach * @param int $block_id 219a45f9889SGreg Roach * 220a45f9889SGreg Roach * @return void 221a45f9889SGreg Roach */ 2226ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 223a45f9889SGreg Roach { 224b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 225e106ff43SGreg Roach 226e106ff43SGreg Roach $this->setBlockSetting($block_id, 'filter', $params['filter']); 227e106ff43SGreg Roach $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']); 228e106ff43SGreg Roach $this->setBlockSetting($block_id, 'sortStyle', $params['sortStyle']); 229e106ff43SGreg Roach $this->setBlockSetting($block_id, 'events', implode(',', $params['events'] ?? [])); 230a45f9889SGreg Roach } 231a45f9889SGreg Roach 232a45f9889SGreg Roach /** 23376692c8bSGreg Roach * An HTML form to edit block settings 23476692c8bSGreg Roach * 235e490cd80SGreg Roach * @param Tree $tree 23676692c8bSGreg Roach * @param int $block_id 237a9430be8SGreg Roach * 2383caaa4d2SGreg Roach * @return string 23976692c8bSGreg Roach */ 2403caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 241c1010edaSGreg Roach { 24212664b30SGreg Roach $default_events = implode(',', self::DEFAULT_EVENTS); 24312664b30SGreg Roach 244e2a378d3SGreg Roach $filter = $this->getBlockSetting($block_id, 'filter', '1'); 2457c2c99faSGreg Roach $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 2467c2c99faSGreg Roach $sort_style = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT); 24712664b30SGreg Roach $events = $this->getBlockSetting($block_id, 'events', $default_events); 2488c2e8227SGreg Roach 24912664b30SGreg Roach $event_array = explode(',', $events); 25012664b30SGreg Roach 25112664b30SGreg Roach $all_events = []; 25269cdf014SGreg Roach foreach (self::ALL_EVENTS as $event => $tag) { 25369cdf014SGreg Roach $all_events[$event] = Registry::elementFactory()->make($tag)->label(); 25412664b30SGreg Roach } 25512664b30SGreg Roach 256c385536dSGreg Roach $info_styles = [ 257bbb76c12SGreg Roach /* I18N: An option in a list-box */ 258bbb76c12SGreg Roach 'list' => I18N::translate('list'), 259bbb76c12SGreg Roach /* I18N: An option in a list-box */ 260bbb76c12SGreg Roach 'table' => I18N::translate('table'), 261c385536dSGreg Roach ]; 2628c2e8227SGreg Roach 263c385536dSGreg Roach $sort_styles = [ 264bbb76c12SGreg Roach /* I18N: An option in a list-box */ 265bbb76c12SGreg Roach 'alpha' => I18N::translate('sort by name'), 266bbb76c12SGreg Roach /* I18N: An option in a list-box */ 2676f97ab23SGreg Roach 'anniv_asc' => I18N::translate('sort by date, oldest first'), 2686f97ab23SGreg Roach /* I18N: An option in a list-box */ 2696f97ab23SGreg Roach 'anniv_desc' => I18N::translate('sort by date, newest first'), 270c385536dSGreg Roach ]; 271d8189b6aSDavid Drury 2723caaa4d2SGreg Roach return view('modules/todays_events/config', [ 273c385536dSGreg Roach 'all_events' => $all_events, 274c385536dSGreg Roach 'event_array' => $event_array, 275c385536dSGreg Roach 'filter' => $filter, 2767c2c99faSGreg Roach 'info_style' => $info_style, 277c385536dSGreg Roach 'info_styles' => $info_styles, 2787c2c99faSGreg Roach 'sort_style' => $sort_style, 279c385536dSGreg Roach 'sort_styles' => $sort_styles, 280c385536dSGreg Roach ]); 2818c2e8227SGreg Roach } 2828c2e8227SGreg Roach} 283