18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg 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 */ 3149a243cbSGreg Roachclass UpcomingAnniversariesModule extends AbstractModule implements ModuleInterface, ModuleBlockInterface 32c1010edaSGreg Roach{ 3349a243cbSGreg Roach use ModuleBlockTrait; 3449a243cbSGreg Roach 35c385536dSGreg Roach // Default values for new blocks. 3616d6367aSGreg Roach private const DEFAULT_DAYS = '7'; 3716d6367aSGreg Roach private const DEFAULT_FILTER = '1'; 3816d6367aSGreg Roach private const DEFAULT_SORT = 'alpha'; 3916d6367aSGreg Roach private const DEFAULT_STYLE = 'table'; 40c385536dSGreg Roach 41c385536dSGreg Roach // Can show this number of days into the future. 4216d6367aSGreg Roach private const MIN_DAYS = 1; 4316d6367aSGreg Roach private const MAX_DAYS = 30; 44c385536dSGreg Roach 4512664b30SGreg Roach // All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN 4616d6367aSGreg Roach private const ALL_EVENTS = [ 4712664b30SGreg Roach 'ADOP', 4812664b30SGreg Roach 'ANUL', 4912664b30SGreg Roach 'BAPM', 5012664b30SGreg Roach 'BARM', 5112664b30SGreg Roach 'BASM', 5212664b30SGreg Roach 'BIRT', 5312664b30SGreg Roach 'BLES', 5412664b30SGreg Roach 'BURI', 5512664b30SGreg Roach 'CHR', 5612664b30SGreg Roach 'CHRA', 5712664b30SGreg Roach 'CONF', 5812664b30SGreg Roach 'CREM', 5912664b30SGreg Roach 'DEAT', 6012664b30SGreg Roach 'DIV', 6112664b30SGreg Roach 'DIVF', 6212664b30SGreg Roach 'EMIG', 6312664b30SGreg Roach 'ENGA', 6412664b30SGreg Roach 'FCOM', 6512664b30SGreg Roach 'GRAD', 6612664b30SGreg Roach 'IMMI', 6712664b30SGreg Roach 'MARB', 6812664b30SGreg Roach 'MARC', 6912664b30SGreg Roach 'MARL', 7012664b30SGreg Roach 'MARR', 7112664b30SGreg Roach 'MARS', 7212664b30SGreg Roach 'NATU', 7312664b30SGreg Roach 'ORDN', 7412664b30SGreg Roach 'PROB', 7512664b30SGreg Roach 'RETI', 7612664b30SGreg Roach 'WILL', 7712664b30SGreg Roach ]; 7812664b30SGreg Roach 7916d6367aSGreg Roach private const DEFAULT_EVENTS = [ 8012664b30SGreg Roach 'BIRT', 8112664b30SGreg Roach 'MARR', 8212664b30SGreg Roach 'DEAT', 8312664b30SGreg Roach ]; 8412664b30SGreg Roach 8576692c8bSGreg Roach /** 8676692c8bSGreg Roach * How should this module be labelled on tabs, menus, etc.? 8776692c8bSGreg Roach * 8876692c8bSGreg Roach * @return string 8976692c8bSGreg Roach */ 9049a243cbSGreg Roach public function title(): string 91c1010edaSGreg Roach { 92bbb76c12SGreg Roach /* I18N: Name of a module */ 93bbb76c12SGreg Roach return I18N::translate('Upcoming events'); 948c2e8227SGreg Roach } 958c2e8227SGreg Roach 9676692c8bSGreg Roach /** 9776692c8bSGreg Roach * A sentence describing what this module does. 9876692c8bSGreg Roach * 9976692c8bSGreg Roach * @return string 10076692c8bSGreg Roach */ 10149a243cbSGreg Roach public function description(): string 102c1010edaSGreg Roach { 103bbb76c12SGreg Roach /* I18N: Description of the “Upcoming events” module */ 104bbb76c12SGreg Roach return I18N::translate('A list of the anniversaries that will occur in the near future.'); 1058c2e8227SGreg Roach } 1068c2e8227SGreg Roach 10776692c8bSGreg Roach /** 10876692c8bSGreg Roach * Generate the HTML content of this block. 10976692c8bSGreg Roach * 110e490cd80SGreg Roach * @param Tree $tree 11176692c8bSGreg Roach * @param int $block_id 1125f2ae573SGreg Roach * @param string $ctype 113727f238cSGreg Roach * @param string[] $cfg 11476692c8bSGreg Roach * 11576692c8bSGreg Roach * @return string 11676692c8bSGreg Roach */ 1175f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 118c1010edaSGreg Roach { 119f0a11419SGreg Roach $calendar_service = new CalendarService(); 120f0a11419SGreg Roach 12112664b30SGreg Roach $default_events = implode(',', self::DEFAULT_EVENTS); 12212664b30SGreg Roach 12376f666f4SGreg Roach $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 124c385536dSGreg Roach $filter = (bool) $this->getBlockSetting($block_id, 'filter', self::DEFAULT_FILTER); 125c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 126c385536dSGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT); 12712664b30SGreg Roach $events = $this->getBlockSetting($block_id, 'events', $default_events); 1288c2e8227SGreg Roach 129c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 1308c2e8227SGreg Roach 13112664b30SGreg Roach $event_array = explode(',', $events); 13212664b30SGreg Roach 13312664b30SGreg Roach // If we are only showing living individuals, then we don't need to search for DEAT events. 13412664b30SGreg Roach if ($filter) { 1358d0ebef0SGreg Roach $event_array = array_diff($event_array, Gedcom::DEATH_EVENTS); 13612664b30SGreg Roach } 13712664b30SGreg Roach 13812664b30SGreg Roach $events_filter = implode('|', $event_array); 13912664b30SGreg Roach 1408c2e8227SGreg Roach $startjd = WT_CLIENT_JD + 1; 14176f666f4SGreg Roach $endjd = WT_CLIENT_JD + $days; 14212664b30SGreg Roach 143f0a11419SGreg Roach $facts = $calendar_service->getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $tree); 14412664b30SGreg Roach 14512664b30SGreg Roach if (empty($facts)) { 146d8189b6aSDavid Drury if ($endjd == $startjd) { 147147e99aaSGreg Roach $content = view('modules/upcoming_events/empty', [ 148c1010edaSGreg Roach 'message' => I18N::translate('No events exist for tomorrow.'), 1490280f44aSGreg Roach ]); 150d8189b6aSDavid Drury } else { 151bbb76c12SGreg Roach /* I18N: translation for %s==1 is unused; it is translated separately as “tomorrow” */ $content = view('modules/upcoming_events/empty', [ 152bbb76c12SGreg 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)), 1530280f44aSGreg Roach ]); 1548c2e8227SGreg Roach } 155147e99aaSGreg Roach } elseif ($infoStyle === 'list') { 156147e99aaSGreg Roach $content = view('modules/upcoming_events/list', [ 157d8189b6aSDavid Drury 'facts' => $facts, 158147e99aaSGreg Roach ]); 159147e99aaSGreg Roach } else { 160147e99aaSGreg Roach $content = view('modules/upcoming_events/table', [ 161147e99aaSGreg Roach 'facts' => $facts, 162147e99aaSGreg Roach ]); 1630280f44aSGreg Roach } 1648c2e8227SGreg Roach 1656a8879feSGreg Roach if ($ctype !== '') { 166e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 167c1010edaSGreg Roach $config_url = route('tree-page-block-edit', [ 168c1010edaSGreg Roach 'block_id' => $block_id, 169aa6f03bbSGreg Roach 'ged' => $tree->name(), 170c1010edaSGreg Roach ]); 171397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 172c1010edaSGreg Roach $config_url = route('user-page-block-edit', [ 173c1010edaSGreg Roach 'block_id' => $block_id, 174aa6f03bbSGreg Roach 'ged' => $tree->name(), 175c1010edaSGreg Roach ]); 1768cbbfdceSGreg Roach } else { 1778cbbfdceSGreg Roach $config_url = ''; 1788cbbfdceSGreg Roach } 1798cbbfdceSGreg Roach 180147e99aaSGreg Roach return view('modules/block-template', [ 181*26684e68SGreg Roach 'block' => str_replace('_', '-', $this->name()), 1829c6524dcSGreg Roach 'id' => $block_id, 1838cbbfdceSGreg Roach 'config_url' => $config_url, 18449a243cbSGreg Roach 'title' => $this->title(), 1859c6524dcSGreg Roach 'content' => $content, 1869c6524dcSGreg Roach ]); 1878c2e8227SGreg Roach } 188b2ce94c6SRico Sonntag 189b2ce94c6SRico Sonntag return $content; 1908c2e8227SGreg Roach } 1918c2e8227SGreg Roach 1928c2e8227SGreg Roach /** {@inheritdoc} */ 193c1010edaSGreg Roach public function loadAjax(): bool 194c1010edaSGreg Roach { 1958c2e8227SGreg Roach return true; 1968c2e8227SGreg Roach } 1978c2e8227SGreg Roach 1988c2e8227SGreg Roach /** {@inheritdoc} */ 199c1010edaSGreg Roach public function isUserBlock(): bool 200c1010edaSGreg Roach { 2018c2e8227SGreg Roach return true; 2028c2e8227SGreg Roach } 2038c2e8227SGreg Roach 2048c2e8227SGreg Roach /** {@inheritdoc} */ 205c1010edaSGreg Roach public function isGedcomBlock(): bool 206c1010edaSGreg Roach { 2078c2e8227SGreg Roach return true; 2088c2e8227SGreg Roach } 2098c2e8227SGreg Roach 21076692c8bSGreg Roach /** 211a45f9889SGreg Roach * Update the configuration for a block. 212a45f9889SGreg Roach * 213a45f9889SGreg Roach * @param Request $request 214a45f9889SGreg Roach * @param int $block_id 215a45f9889SGreg Roach * 216a45f9889SGreg Roach * @return void 217a45f9889SGreg Roach */ 218a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 219a45f9889SGreg Roach { 220a45f9889SGreg Roach $this->setBlockSetting($block_id, 'days', $request->get('days', self::DEFAULT_DAYS)); 221a45f9889SGreg Roach $this->setBlockSetting($block_id, 'filter', $request->get('filter', '')); 222a45f9889SGreg Roach $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE)); 223a45f9889SGreg Roach $this->setBlockSetting($block_id, 'sortStyle', $request->get('sortStyle', self::DEFAULT_SORT)); 224a45f9889SGreg Roach $this->setBlockSetting($block_id, 'events', implode(',', (array) $request->get('events'))); 225a45f9889SGreg Roach } 226a45f9889SGreg Roach 227a45f9889SGreg Roach /** 22876692c8bSGreg Roach * An HTML form to edit block settings 22976692c8bSGreg Roach * 230e490cd80SGreg Roach * @param Tree $tree 23176692c8bSGreg Roach * @param int $block_id 232a9430be8SGreg Roach * 233a9430be8SGreg Roach * @return void 23476692c8bSGreg Roach */ 235a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 236c1010edaSGreg Roach { 23712664b30SGreg Roach $default_events = implode(',', self::DEFAULT_EVENTS); 23812664b30SGreg Roach 23976f666f4SGreg Roach $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 240c385536dSGreg Roach $filter = $this->getBlockSetting($block_id, 'filter', self::DEFAULT_FILTER); 241c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 242c385536dSGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT); 24312664b30SGreg Roach $events = $this->getBlockSetting($block_id, 'events', $default_events); 2448c2e8227SGreg Roach 24512664b30SGreg Roach $event_array = explode(',', $events); 24612664b30SGreg Roach 24712664b30SGreg Roach $all_events = []; 24812664b30SGreg Roach foreach (self::ALL_EVENTS as $event) { 24912664b30SGreg Roach $all_events[$event] = GedcomTag::getLabel($event); 25012664b30SGreg Roach } 25112664b30SGreg Roach 252c385536dSGreg Roach $info_styles = [ 253bbb76c12SGreg Roach /* I18N: An option in a list-box */ 254bbb76c12SGreg Roach 'list' => I18N::translate('list'), 255bbb76c12SGreg Roach /* I18N: An option in a list-box */ 256bbb76c12SGreg Roach 'table' => I18N::translate('table'), 257c385536dSGreg Roach ]; 2588c2e8227SGreg Roach 259c385536dSGreg Roach $sort_styles = [ 260bbb76c12SGreg Roach /* I18N: An option in a list-box */ 261bbb76c12SGreg Roach 'alpha' => I18N::translate('sort by name'), 262bbb76c12SGreg Roach /* I18N: An option in a list-box */ 263bbb76c12SGreg Roach 'anniv' => I18N::translate('sort by date'), 264c385536dSGreg Roach ]; 2658c2e8227SGreg Roach 266147e99aaSGreg Roach echo view('modules/upcoming_events/config', [ 267c385536dSGreg Roach 'all_events' => $all_events, 268c385536dSGreg Roach 'days' => $days, 269c385536dSGreg Roach 'event_array' => $event_array, 270c385536dSGreg Roach 'filter' => $filter, 271c385536dSGreg Roach 'infoStyle' => $infoStyle, 272c385536dSGreg Roach 'info_styles' => $info_styles, 273c385536dSGreg Roach 'max_days' => self::MAX_DAYS, 274c385536dSGreg Roach 'sortStyle' => $sortStyle, 275c385536dSGreg Roach 'sort_styles' => $sort_styles, 276c385536dSGreg Roach ]); 2778c2e8227SGreg Roach } 2788c2e8227SGreg Roach} 279