18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 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 224459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon; 238d0ebef0SGreg Roachuse Fisharebest\Webtrees\Gedcom; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 25*69cdf014SGreg Roachuse Fisharebest\Webtrees\Registry; 26f0a11419SGreg Roachuse Fisharebest\Webtrees\Services\CalendarService; 27e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 281e7a7a28SGreg Roachuse Illuminate\Support\Str; 296ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 308c2e8227SGreg Roach 318c2e8227SGreg Roach/** 328c2e8227SGreg Roach * Class OnThisDayModule 338c2e8227SGreg Roach */ 3437eb8894SGreg Roachclass OnThisDayModule extends AbstractModule implements ModuleBlockInterface 35c1010edaSGreg Roach{ 3649a243cbSGreg Roach use ModuleBlockTrait; 3749a243cbSGreg Roach 3801221f27SGreg Roach // Pagination 3901221f27SGreg Roach private const LIMIT_LOW = 10; 4001221f27SGreg Roach private const LIMIT_HIGH = 20; 4101221f27SGreg Roach 426f97ab23SGreg Roach // Default values for new blocks. 436f97ab23SGreg Roach private const DEFAULT_SORT = 'date_desc'; 446f97ab23SGreg Roach private const DEFAULT_STYLE = 'date_desc'; 456f97ab23SGreg Roach 460999990bSGreg Roach // Initial sorting for datatables 470999990bSGreg Roach private const DATATABLES_ORDER = [ 480999990bSGreg Roach 'alpha' => [[0, 'asc']], 496f97ab23SGreg Roach 'date_asc' => [[2, 'asc']], 506f97ab23SGreg Roach 'date_desc' => [[2, 'desc']], 510999990bSGreg Roach ]; 520999990bSGreg Roach 5312664b30SGreg Roach // All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN 5416d6367aSGreg Roach private const ALL_EVENTS = [ 55*69cdf014SGreg Roach 'ADOP' => 'INDI:ADOP', 56*69cdf014SGreg Roach 'ANUL' => 'FAM:ANUL', 57*69cdf014SGreg Roach 'BAPM' => 'INDI:BAPM', 58*69cdf014SGreg Roach 'BARM' => 'INDI:BARM', 59*69cdf014SGreg Roach 'BASM' => 'INDI:BASM', 60*69cdf014SGreg Roach 'BIRT' => 'INDI:BIRT', 61*69cdf014SGreg Roach 'BLES' => 'INDI:BLES', 62*69cdf014SGreg Roach 'BURI' => 'INDI:BURI', 63*69cdf014SGreg Roach 'CHR' => 'INDI:CHR', 64*69cdf014SGreg Roach 'CHRA' => 'INDI:CHRA', 65*69cdf014SGreg Roach 'CONF' => 'INDI:CONF', 66*69cdf014SGreg Roach 'CREM' => 'INDI:CREM', 67*69cdf014SGreg Roach 'DEAT' => 'INDI:DEAT', 68*69cdf014SGreg Roach 'DIV' => 'FAM:DIV', 69*69cdf014SGreg Roach 'DIVF' => 'FAM:DIVF', 70*69cdf014SGreg Roach 'EMIG' => 'INDI:EMIG', 71*69cdf014SGreg Roach 'ENGA' => 'FAM:ENGA', 72*69cdf014SGreg Roach 'FCOM' => 'INDI:FCOM', 73*69cdf014SGreg Roach 'GRAD' => 'INDI:GRAD', 74*69cdf014SGreg Roach 'IMMI' => 'INDI:IMMI', 75*69cdf014SGreg Roach 'MARB' => 'FAM:MARB', 76*69cdf014SGreg Roach 'MARC' => 'FAM:MARC', 77*69cdf014SGreg Roach 'MARL' => 'FAM:MARL', 78*69cdf014SGreg Roach 'MARR' => 'FAM:MARR', 79*69cdf014SGreg Roach 'MARS' => 'FAM:MARS', 80*69cdf014SGreg Roach 'NATU' => 'INDI:NATU', 81*69cdf014SGreg Roach 'ORDN' => 'INDI:ORDN', 82*69cdf014SGreg Roach 'PROB' => 'INDI:PROB', 83*69cdf014SGreg Roach 'RETI' => 'INDI:RETI', 84*69cdf014SGreg Roach 'WILL' => 'INDI:WILL', 8512664b30SGreg Roach ]; 8612664b30SGreg Roach 8716d6367aSGreg Roach private const DEFAULT_EVENTS = [ 8812664b30SGreg Roach 'BIRT', 8912664b30SGreg Roach 'MARR', 9012664b30SGreg Roach 'DEAT', 9112664b30SGreg Roach ]; 9212664b30SGreg Roach 9312664b30SGreg Roach /** 940cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 9512664b30SGreg Roach * 9612664b30SGreg Roach * @return string 9712664b30SGreg Roach */ 9849a243cbSGreg Roach public function title(): string 99c1010edaSGreg Roach { 100bbb76c12SGreg Roach /* I18N: Name of a module */ 101bbb76c12SGreg Roach return I18N::translate('On this day'); 1028c2e8227SGreg Roach } 1038c2e8227SGreg Roach 10412664b30SGreg Roach /** 10512664b30SGreg Roach * A sentence describing what this module does. 10612664b30SGreg Roach * 10712664b30SGreg Roach * @return string 10812664b30SGreg Roach */ 10949a243cbSGreg Roach public function description(): string 110c1010edaSGreg Roach { 111bbb76c12SGreg Roach /* I18N: Description of the “On this day” module */ 112bbb76c12SGreg Roach return I18N::translate('A list of the anniversaries that occur today.'); 1138c2e8227SGreg Roach } 1148c2e8227SGreg Roach 11576692c8bSGreg Roach /** 11676692c8bSGreg Roach * Generate the HTML content of this block. 11776692c8bSGreg Roach * 118e490cd80SGreg Roach * @param Tree $tree 11976692c8bSGreg Roach * @param int $block_id 1203caaa4d2SGreg Roach * @param string $context 1213caaa4d2SGreg Roach * @param string[] $config 12276692c8bSGreg Roach * 12376692c8bSGreg Roach * @return string 12476692c8bSGreg Roach */ 1253caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 126c1010edaSGreg Roach { 127f0a11419SGreg Roach $calendar_service = new CalendarService(); 128f0a11419SGreg Roach 12912664b30SGreg Roach $default_events = implode(',', self::DEFAULT_EVENTS); 13012664b30SGreg Roach 13112664b30SGreg Roach $filter = (bool) $this->getBlockSetting($block_id, 'filter', '1'); 1326f97ab23SGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 1336f97ab23SGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT); 13412664b30SGreg Roach $events = $this->getBlockSetting($block_id, 'events', $default_events); 1358c2e8227SGreg Roach 1363caaa4d2SGreg Roach extract($config, EXTR_OVERWRITE); 1378c2e8227SGreg Roach 13812664b30SGreg Roach $event_array = explode(',', $events); 13912664b30SGreg Roach 14012664b30SGreg Roach // If we are only showing living individuals, then we don't need to search for DEAT events. 14112664b30SGreg Roach if ($filter) { 1428d0ebef0SGreg Roach $event_array = array_diff($event_array, Gedcom::DEATH_EVENTS); 14312664b30SGreg Roach } 14412664b30SGreg Roach 14512664b30SGreg Roach $events_filter = implode('|', $event_array); 14612664b30SGreg Roach 1474459dc9aSGreg Roach $startjd = Carbon::now()->julianDay(); 148269fd10dSGreg Roach $endjd = $startjd; 14912664b30SGreg Roach 150f0a11419SGreg Roach $facts = $calendar_service->getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $tree); 15112664b30SGreg Roach 152cd51dbdfSGreg Roach if ($facts->isEmpty()) { 153147e99aaSGreg Roach $content = view('modules/todays_events/empty'); 154147e99aaSGreg Roach } elseif ($infoStyle === 'list') { 15548f08416SGreg Roach $content = view('lists/anniversaries-list', [ 156e24053e5SGreg Roach 'id' => $block_id, 157147e99aaSGreg Roach 'facts' => $facts, 15801221f27SGreg Roach 'limit_low' => self::LIMIT_LOW, 15901221f27SGreg Roach 'limit_high' => self::LIMIT_HIGH, 1600280f44aSGreg Roach ]); 161d8189b6aSDavid Drury } else { 16248f08416SGreg Roach $content = view('lists/anniversaries-table', [ 163d8189b6aSDavid Drury 'facts' => $facts, 16401221f27SGreg Roach 'limit_low' => self::LIMIT_LOW, 16501221f27SGreg Roach 'limit_high' => self::LIMIT_HIGH, 1666f97ab23SGreg Roach 'order' => self::DATATABLES_ORDER[$sortStyle] ?? self::DATATABLES_ORDER[self::DEFAULT_SORT], 1670280f44aSGreg Roach ]); 1680280f44aSGreg Roach } 1698c2e8227SGreg Roach 1703caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 171147e99aaSGreg Roach return view('modules/block-template', [ 1721e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1739c6524dcSGreg Roach 'id' => $block_id, 1743caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 17549a243cbSGreg Roach 'title' => $this->title(), 1769c6524dcSGreg Roach 'content' => $content, 17712664b30SGreg Roach ]); 1788c2e8227SGreg Roach } 179b2ce94c6SRico Sonntag 180b2ce94c6SRico Sonntag return $content; 1818c2e8227SGreg Roach } 1828c2e8227SGreg Roach 1833caaa4d2SGreg Roach /** 1843caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 1853caaa4d2SGreg Roach * 1863caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 1873caaa4d2SGreg Roach * 1883caaa4d2SGreg Roach * @return bool 1893caaa4d2SGreg Roach */ 190c1010edaSGreg Roach public function loadAjax(): bool 191c1010edaSGreg Roach { 1928c2e8227SGreg Roach return true; 1938c2e8227SGreg Roach } 1948c2e8227SGreg Roach 1953caaa4d2SGreg Roach /** 1963caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 1973caaa4d2SGreg Roach * 1983caaa4d2SGreg Roach * @return bool 1993caaa4d2SGreg Roach */ 200c1010edaSGreg Roach public function isUserBlock(): bool 201c1010edaSGreg Roach { 2028c2e8227SGreg Roach return true; 2038c2e8227SGreg Roach } 2048c2e8227SGreg Roach 2053caaa4d2SGreg Roach /** 2063caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 2073caaa4d2SGreg Roach * 2083caaa4d2SGreg Roach * @return bool 2093caaa4d2SGreg Roach */ 21063276d8fSGreg Roach public function isTreeBlock(): bool 211c1010edaSGreg Roach { 2128c2e8227SGreg Roach return true; 2138c2e8227SGreg Roach } 2148c2e8227SGreg Roach 21576692c8bSGreg Roach /** 216a45f9889SGreg Roach * Update the configuration for a block. 217a45f9889SGreg Roach * 2186ccdf4f0SGreg Roach * @param ServerRequestInterface $request 219a45f9889SGreg Roach * @param int $block_id 220a45f9889SGreg Roach * 221a45f9889SGreg Roach * @return void 222a45f9889SGreg Roach */ 2236ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 224a45f9889SGreg Roach { 225b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 226e106ff43SGreg Roach 227e106ff43SGreg Roach $this->setBlockSetting($block_id, 'filter', $params['filter']); 228e106ff43SGreg Roach $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']); 229e106ff43SGreg Roach $this->setBlockSetting($block_id, 'sortStyle', $params['sortStyle']); 230e106ff43SGreg Roach $this->setBlockSetting($block_id, 'events', implode(',', $params['events'] ?? [])); 231a45f9889SGreg Roach } 232a45f9889SGreg Roach 233a45f9889SGreg Roach /** 23476692c8bSGreg Roach * An HTML form to edit block settings 23576692c8bSGreg Roach * 236e490cd80SGreg Roach * @param Tree $tree 23776692c8bSGreg Roach * @param int $block_id 238a9430be8SGreg Roach * 2393caaa4d2SGreg Roach * @return string 24076692c8bSGreg Roach */ 2413caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 242c1010edaSGreg Roach { 24312664b30SGreg Roach $default_events = implode(',', self::DEFAULT_EVENTS); 24412664b30SGreg Roach 245e2a378d3SGreg Roach $filter = $this->getBlockSetting($block_id, 'filter', '1'); 2467c2c99faSGreg Roach $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 2477c2c99faSGreg Roach $sort_style = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT); 24812664b30SGreg Roach $events = $this->getBlockSetting($block_id, 'events', $default_events); 2498c2e8227SGreg Roach 25012664b30SGreg Roach $event_array = explode(',', $events); 25112664b30SGreg Roach 25212664b30SGreg Roach $all_events = []; 253*69cdf014SGreg Roach foreach (self::ALL_EVENTS as $event => $tag) { 254*69cdf014SGreg Roach $all_events[$event] = Registry::elementFactory()->make($tag)->label(); 25512664b30SGreg Roach } 25612664b30SGreg Roach 257c385536dSGreg Roach $info_styles = [ 258bbb76c12SGreg Roach /* I18N: An option in a list-box */ 259bbb76c12SGreg Roach 'list' => I18N::translate('list'), 260bbb76c12SGreg Roach /* I18N: An option in a list-box */ 261bbb76c12SGreg Roach 'table' => I18N::translate('table'), 262c385536dSGreg Roach ]; 2638c2e8227SGreg Roach 264c385536dSGreg Roach $sort_styles = [ 265bbb76c12SGreg Roach /* I18N: An option in a list-box */ 266bbb76c12SGreg Roach 'alpha' => I18N::translate('sort by name'), 267bbb76c12SGreg Roach /* I18N: An option in a list-box */ 2686f97ab23SGreg Roach 'anniv_asc' => I18N::translate('sort by date, oldest first'), 2696f97ab23SGreg Roach /* I18N: An option in a list-box */ 2706f97ab23SGreg Roach 'anniv_desc' => I18N::translate('sort by date, newest first'), 271c385536dSGreg Roach ]; 272d8189b6aSDavid Drury 2733caaa4d2SGreg Roach return view('modules/todays_events/config', [ 274c385536dSGreg Roach 'all_events' => $all_events, 275c385536dSGreg Roach 'event_array' => $event_array, 276c385536dSGreg Roach 'filter' => $filter, 2777c2c99faSGreg Roach 'info_style' => $info_style, 278c385536dSGreg Roach 'info_styles' => $info_styles, 2797c2c99faSGreg Roach 'sort_style' => $sort_style, 280c385536dSGreg Roach 'sort_styles' => $sort_styles, 281c385536dSGreg Roach ]); 2828c2e8227SGreg Roach } 2838c2e8227SGreg Roach} 284