18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 4*8fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 88c2e8227SGreg Roach * (at your option) any later version. 98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 128c2e8227SGreg Roach * GNU General Public License for more details. 138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 158c2e8227SGreg Roach */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 1976692c8bSGreg Roach 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 218d0ebef0SGreg Roachuse Fisharebest\Webtrees\Gedcom; 2212664b30SGreg Roachuse Fisharebest\Webtrees\GedcomTag; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 24f0a11419SGreg Roachuse Fisharebest\Webtrees\Services\CalendarService; 25e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 26a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request; 278c2e8227SGreg Roach 288c2e8227SGreg Roach/** 298c2e8227SGreg Roach * Class UpcomingAnniversariesModule 308c2e8227SGreg Roach */ 31c1010edaSGreg Roachclass UpcomingAnniversariesModule extends AbstractModule implements ModuleBlockInterface 32c1010edaSGreg Roach{ 33c385536dSGreg Roach // Default values for new blocks. 3476f666f4SGreg Roach const DEFAULT_DAYS = '7'; 35c385536dSGreg Roach const DEFAULT_FILTER = '1'; 36c385536dSGreg Roach const DEFAULT_SORT = 'alpha'; 37c385536dSGreg Roach const DEFAULT_STYLE = 'table'; 38c385536dSGreg Roach 39c385536dSGreg Roach // Can show this number of days into the future. 40c385536dSGreg Roach const MIN_DAYS = 1; 41c385536dSGreg Roach const MAX_DAYS = 30; 42c385536dSGreg Roach 4312664b30SGreg Roach // All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN 4412664b30SGreg Roach const ALL_EVENTS = [ 4512664b30SGreg Roach 'ADOP', 4612664b30SGreg Roach 'ANUL', 4712664b30SGreg Roach 'BAPM', 4812664b30SGreg Roach 'BARM', 4912664b30SGreg Roach 'BASM', 5012664b30SGreg Roach 'BIRT', 5112664b30SGreg Roach 'BLES', 5212664b30SGreg Roach 'BURI', 5312664b30SGreg Roach 'CHR', 5412664b30SGreg Roach 'CHRA', 5512664b30SGreg Roach 'CONF', 5612664b30SGreg Roach 'CREM', 5712664b30SGreg Roach 'DEAT', 5812664b30SGreg Roach 'DIV', 5912664b30SGreg Roach 'DIVF', 6012664b30SGreg Roach 'EMIG', 6112664b30SGreg Roach 'ENGA', 6212664b30SGreg Roach 'FCOM', 6312664b30SGreg Roach 'GRAD', 6412664b30SGreg Roach 'IMMI', 6512664b30SGreg Roach 'MARB', 6612664b30SGreg Roach 'MARC', 6712664b30SGreg Roach 'MARL', 6812664b30SGreg Roach 'MARR', 6912664b30SGreg Roach 'MARS', 7012664b30SGreg Roach 'NATU', 7112664b30SGreg Roach 'ORDN', 7212664b30SGreg Roach 'PROB', 7312664b30SGreg Roach 'RETI', 7412664b30SGreg Roach 'WILL', 7512664b30SGreg Roach ]; 7612664b30SGreg Roach 7712664b30SGreg Roach const DEFAULT_EVENTS = [ 7812664b30SGreg Roach 'BIRT', 7912664b30SGreg Roach 'MARR', 8012664b30SGreg Roach 'DEAT', 8112664b30SGreg Roach ]; 8212664b30SGreg Roach 8376692c8bSGreg Roach /** 8476692c8bSGreg Roach * How should this module be labelled on tabs, menus, etc.? 8576692c8bSGreg Roach * 8676692c8bSGreg Roach * @return string 8776692c8bSGreg Roach */ 888f53f488SRico Sonntag public function getTitle(): string 89c1010edaSGreg Roach { 90bbb76c12SGreg Roach /* I18N: Name of a module */ 91bbb76c12SGreg Roach return I18N::translate('Upcoming events'); 928c2e8227SGreg Roach } 938c2e8227SGreg Roach 9476692c8bSGreg Roach /** 9576692c8bSGreg Roach * A sentence describing what this module does. 9676692c8bSGreg Roach * 9776692c8bSGreg Roach * @return string 9876692c8bSGreg Roach */ 998f53f488SRico Sonntag public function getDescription(): string 100c1010edaSGreg Roach { 101bbb76c12SGreg Roach /* I18N: Description of the “Upcoming events” module */ 102bbb76c12SGreg Roach return I18N::translate('A list of the anniversaries that will occur in the near future.'); 1038c2e8227SGreg Roach } 1048c2e8227SGreg Roach 10576692c8bSGreg Roach /** 10676692c8bSGreg Roach * Generate the HTML content of this block. 10776692c8bSGreg Roach * 108e490cd80SGreg Roach * @param Tree $tree 10976692c8bSGreg Roach * @param int $block_id 1105f2ae573SGreg Roach * @param string $ctype 111727f238cSGreg Roach * @param string[] $cfg 11276692c8bSGreg Roach * 11376692c8bSGreg Roach * @return string 11476692c8bSGreg Roach */ 1155f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 116c1010edaSGreg Roach { 117f0a11419SGreg Roach $calendar_service = new CalendarService(); 118f0a11419SGreg Roach 11912664b30SGreg Roach $default_events = implode(',', self::DEFAULT_EVENTS); 12012664b30SGreg Roach 12176f666f4SGreg Roach $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 122c385536dSGreg Roach $filter = (bool) $this->getBlockSetting($block_id, 'filter', self::DEFAULT_FILTER); 123c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 124c385536dSGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT); 12512664b30SGreg Roach $events = $this->getBlockSetting($block_id, 'events', $default_events); 1268c2e8227SGreg Roach 127c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 1288c2e8227SGreg Roach 12912664b30SGreg Roach $event_array = explode(',', $events); 13012664b30SGreg Roach 13112664b30SGreg Roach // If we are only showing living individuals, then we don't need to search for DEAT events. 13212664b30SGreg Roach if ($filter) { 1338d0ebef0SGreg Roach $event_array = array_diff($event_array, Gedcom::DEATH_EVENTS); 13412664b30SGreg Roach } 13512664b30SGreg Roach 13612664b30SGreg Roach $events_filter = implode('|', $event_array); 13712664b30SGreg Roach 1388c2e8227SGreg Roach $startjd = WT_CLIENT_JD + 1; 13976f666f4SGreg Roach $endjd = WT_CLIENT_JD + $days; 14012664b30SGreg Roach 141f0a11419SGreg Roach $facts = $calendar_service->getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $tree); 14212664b30SGreg Roach 14312664b30SGreg Roach if (empty($facts)) { 144d8189b6aSDavid Drury if ($endjd == $startjd) { 145147e99aaSGreg Roach $content = view('modules/upcoming_events/empty', [ 146c1010edaSGreg Roach 'message' => I18N::translate('No events exist for tomorrow.'), 1470280f44aSGreg Roach ]); 148d8189b6aSDavid Drury } else { 149bbb76c12SGreg Roach /* I18N: translation for %s==1 is unused; it is translated separately as “tomorrow” */ $content = view('modules/upcoming_events/empty', [ 150bbb76c12SGreg Roach 'message' => I18N::plural('No events exist for the next %s day.', 'No events exist for the next %s days.', $endjd - $startjd + 1, I18N::number($endjd - $startjd + 1)), 1510280f44aSGreg Roach ]); 1528c2e8227SGreg Roach } 153147e99aaSGreg Roach } elseif ($infoStyle === 'list') { 154147e99aaSGreg Roach $content = view('modules/upcoming_events/list', [ 155d8189b6aSDavid Drury 'facts' => $facts, 156147e99aaSGreg Roach ]); 157147e99aaSGreg Roach } else { 158147e99aaSGreg Roach $content = view('modules/upcoming_events/table', [ 159147e99aaSGreg Roach 'facts' => $facts, 160147e99aaSGreg Roach ]); 1610280f44aSGreg Roach } 1628c2e8227SGreg Roach 1636a8879feSGreg Roach if ($ctype !== '') { 164e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 165c1010edaSGreg Roach $config_url = route('tree-page-block-edit', [ 166c1010edaSGreg Roach 'block_id' => $block_id, 167aa6f03bbSGreg Roach 'ged' => $tree->name(), 168c1010edaSGreg Roach ]); 169397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 170c1010edaSGreg Roach $config_url = route('user-page-block-edit', [ 171c1010edaSGreg Roach 'block_id' => $block_id, 172aa6f03bbSGreg Roach 'ged' => $tree->name(), 173c1010edaSGreg Roach ]); 1748cbbfdceSGreg Roach } else { 1758cbbfdceSGreg Roach $config_url = ''; 1768cbbfdceSGreg Roach } 1778cbbfdceSGreg Roach 178147e99aaSGreg Roach return view('modules/block-template', [ 1799c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1809c6524dcSGreg Roach 'id' => $block_id, 1818cbbfdceSGreg Roach 'config_url' => $config_url, 1829c6524dcSGreg Roach 'title' => $this->getTitle(), 1839c6524dcSGreg Roach 'content' => $content, 1849c6524dcSGreg Roach ]); 1858c2e8227SGreg Roach } 186b2ce94c6SRico Sonntag 187b2ce94c6SRico Sonntag return $content; 1888c2e8227SGreg Roach } 1898c2e8227SGreg Roach 1908c2e8227SGreg Roach /** {@inheritdoc} */ 191c1010edaSGreg Roach public function loadAjax(): bool 192c1010edaSGreg Roach { 1938c2e8227SGreg Roach return true; 1948c2e8227SGreg Roach } 1958c2e8227SGreg Roach 1968c2e8227SGreg Roach /** {@inheritdoc} */ 197c1010edaSGreg Roach public function isUserBlock(): bool 198c1010edaSGreg Roach { 1998c2e8227SGreg Roach return true; 2008c2e8227SGreg Roach } 2018c2e8227SGreg Roach 2028c2e8227SGreg Roach /** {@inheritdoc} */ 203c1010edaSGreg Roach public function isGedcomBlock(): bool 204c1010edaSGreg Roach { 2058c2e8227SGreg Roach return true; 2068c2e8227SGreg Roach } 2078c2e8227SGreg Roach 20876692c8bSGreg Roach /** 209a45f9889SGreg Roach * Update the configuration for a block. 210a45f9889SGreg Roach * 211a45f9889SGreg Roach * @param Request $request 212a45f9889SGreg Roach * @param int $block_id 213a45f9889SGreg Roach * 214a45f9889SGreg Roach * @return void 215a45f9889SGreg Roach */ 216a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 217a45f9889SGreg Roach { 218a45f9889SGreg Roach $this->setBlockSetting($block_id, 'days', $request->get('days', self::DEFAULT_DAYS)); 219a45f9889SGreg Roach $this->setBlockSetting($block_id, 'filter', $request->get('filter', '')); 220a45f9889SGreg Roach $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE)); 221a45f9889SGreg Roach $this->setBlockSetting($block_id, 'sortStyle', $request->get('sortStyle', self::DEFAULT_SORT)); 222a45f9889SGreg Roach $this->setBlockSetting($block_id, 'events', implode(',', (array) $request->get('events'))); 223a45f9889SGreg Roach } 224a45f9889SGreg Roach 225a45f9889SGreg Roach /** 22676692c8bSGreg Roach * An HTML form to edit block settings 22776692c8bSGreg Roach * 228e490cd80SGreg Roach * @param Tree $tree 22976692c8bSGreg Roach * @param int $block_id 230a9430be8SGreg Roach * 231a9430be8SGreg Roach * @return void 23276692c8bSGreg Roach */ 233a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 234c1010edaSGreg Roach { 23512664b30SGreg Roach $default_events = implode(',', self::DEFAULT_EVENTS); 23612664b30SGreg Roach 23776f666f4SGreg Roach $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 238c385536dSGreg Roach $filter = $this->getBlockSetting($block_id, 'filter', self::DEFAULT_FILTER); 239c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 240c385536dSGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT); 24112664b30SGreg Roach $events = $this->getBlockSetting($block_id, 'events', $default_events); 2428c2e8227SGreg Roach 24312664b30SGreg Roach $event_array = explode(',', $events); 24412664b30SGreg Roach 24512664b30SGreg Roach $all_events = []; 24612664b30SGreg Roach foreach (self::ALL_EVENTS as $event) { 24712664b30SGreg Roach $all_events[$event] = GedcomTag::getLabel($event); 24812664b30SGreg Roach } 24912664b30SGreg Roach 250c385536dSGreg Roach $info_styles = [ 251bbb76c12SGreg Roach /* I18N: An option in a list-box */ 252bbb76c12SGreg Roach 'list' => I18N::translate('list'), 253bbb76c12SGreg Roach /* I18N: An option in a list-box */ 254bbb76c12SGreg Roach 'table' => I18N::translate('table'), 255c385536dSGreg Roach ]; 2568c2e8227SGreg Roach 257c385536dSGreg Roach $sort_styles = [ 258bbb76c12SGreg Roach /* I18N: An option in a list-box */ 259bbb76c12SGreg Roach 'alpha' => I18N::translate('sort by name'), 260bbb76c12SGreg Roach /* I18N: An option in a list-box */ 261bbb76c12SGreg Roach 'anniv' => I18N::translate('sort by date'), 262c385536dSGreg Roach ]; 2638c2e8227SGreg Roach 264147e99aaSGreg Roach echo view('modules/upcoming_events/config', [ 265c385536dSGreg Roach 'all_events' => $all_events, 266c385536dSGreg Roach 'days' => $days, 267c385536dSGreg Roach 'event_array' => $event_array, 268c385536dSGreg Roach 'filter' => $filter, 269c385536dSGreg Roach 'infoStyle' => $infoStyle, 270c385536dSGreg Roach 'info_styles' => $info_styles, 271c385536dSGreg Roach 'max_days' => self::MAX_DAYS, 272c385536dSGreg Roach 'sortStyle' => $sortStyle, 273c385536dSGreg Roach 'sort_styles' => $sort_styles, 274c385536dSGreg Roach ]); 2758c2e8227SGreg Roach } 2768c2e8227SGreg Roach} 277