18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 41062a142SGreg Roach * Copyright (C) 2018 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; 2112664b30SGreg Roachuse Fisharebest\Webtrees\GedcomTag; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 23f0a11419SGreg Roachuse Fisharebest\Webtrees\Services\CalendarService; 24e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 25a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request; 268c2e8227SGreg Roach 278c2e8227SGreg Roach/** 288c2e8227SGreg Roach * Class UpcomingAnniversariesModule 298c2e8227SGreg Roach */ 30c1010edaSGreg Roachclass UpcomingAnniversariesModule extends AbstractModule implements ModuleBlockInterface 31c1010edaSGreg Roach{ 32c385536dSGreg Roach // Default values for new blocks. 3376f666f4SGreg Roach const DEFAULT_DAYS = '7'; 34c385536dSGreg Roach const DEFAULT_FILTER = '1'; 35c385536dSGreg Roach const DEFAULT_SORT = 'alpha'; 36c385536dSGreg Roach const DEFAULT_STYLE = 'table'; 37c385536dSGreg Roach 38c385536dSGreg Roach // Can show this number of days into the future. 39c385536dSGreg Roach const MIN_DAYS = 1; 40c385536dSGreg Roach const MAX_DAYS = 30; 41c385536dSGreg Roach 4212664b30SGreg Roach // All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN 4312664b30SGreg Roach const ALL_EVENTS = [ 4412664b30SGreg Roach 'ADOP', 4512664b30SGreg Roach 'ANUL', 4612664b30SGreg Roach 'BAPM', 4712664b30SGreg Roach 'BARM', 4812664b30SGreg Roach 'BASM', 4912664b30SGreg Roach 'BIRT', 5012664b30SGreg Roach 'BLES', 5112664b30SGreg Roach 'BURI', 5212664b30SGreg Roach 'CHR', 5312664b30SGreg Roach 'CHRA', 5412664b30SGreg Roach 'CONF', 5512664b30SGreg Roach 'CREM', 5612664b30SGreg Roach 'DEAT', 5712664b30SGreg Roach 'DIV', 5812664b30SGreg Roach 'DIVF', 5912664b30SGreg Roach 'EMIG', 6012664b30SGreg Roach 'ENGA', 6112664b30SGreg Roach 'FCOM', 6212664b30SGreg Roach 'GRAD', 6312664b30SGreg Roach 'IMMI', 6412664b30SGreg Roach 'MARB', 6512664b30SGreg Roach 'MARC', 6612664b30SGreg Roach 'MARL', 6712664b30SGreg Roach 'MARR', 6812664b30SGreg Roach 'MARS', 6912664b30SGreg Roach 'NATU', 7012664b30SGreg Roach 'ORDN', 7112664b30SGreg Roach 'PROB', 7212664b30SGreg Roach 'RETI', 7312664b30SGreg Roach 'WILL', 7412664b30SGreg Roach ]; 7512664b30SGreg Roach 7612664b30SGreg Roach const DEFAULT_EVENTS = [ 7712664b30SGreg Roach 'BIRT', 7812664b30SGreg Roach 'MARR', 7912664b30SGreg Roach 'DEAT', 8012664b30SGreg Roach ]; 8112664b30SGreg Roach 8276692c8bSGreg Roach /** 8376692c8bSGreg Roach * How should this module be labelled on tabs, menus, etc.? 8476692c8bSGreg Roach * 8576692c8bSGreg Roach * @return string 8676692c8bSGreg Roach */ 878f53f488SRico Sonntag public function getTitle(): string 88c1010edaSGreg Roach { 89bbb76c12SGreg Roach /* I18N: Name of a module */ 90bbb76c12SGreg Roach return I18N::translate('Upcoming events'); 918c2e8227SGreg Roach } 928c2e8227SGreg Roach 9376692c8bSGreg Roach /** 9476692c8bSGreg Roach * A sentence describing what this module does. 9576692c8bSGreg Roach * 9676692c8bSGreg Roach * @return string 9776692c8bSGreg Roach */ 988f53f488SRico Sonntag public function getDescription(): string 99c1010edaSGreg Roach { 100bbb76c12SGreg Roach /* I18N: Description of the “Upcoming events” module */ 101bbb76c12SGreg Roach return I18N::translate('A list of the anniversaries that will occur in the near future.'); 1028c2e8227SGreg Roach } 1038c2e8227SGreg Roach 10476692c8bSGreg Roach /** 10576692c8bSGreg Roach * Generate the HTML content of this block. 10676692c8bSGreg Roach * 107e490cd80SGreg Roach * @param Tree $tree 10876692c8bSGreg Roach * @param int $block_id 10976692c8bSGreg Roach * @param bool $template 110727f238cSGreg Roach * @param string[] $cfg 11176692c8bSGreg Roach * 11276692c8bSGreg Roach * @return string 11376692c8bSGreg Roach */ 114c1010edaSGreg Roach public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string 115c1010edaSGreg Roach { 116e490cd80SGreg Roach global $ctype; 1178c2e8227SGreg Roach 118f0a11419SGreg Roach $calendar_service = new CalendarService(); 119f0a11419SGreg Roach 12012664b30SGreg Roach $default_events = implode(',', self::DEFAULT_EVENTS); 12112664b30SGreg Roach 12276f666f4SGreg Roach $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 123c385536dSGreg Roach $filter = (bool) $this->getBlockSetting($block_id, 'filter', self::DEFAULT_FILTER); 124c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 125c385536dSGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT); 12612664b30SGreg Roach $events = $this->getBlockSetting($block_id, 'events', $default_events); 1278c2e8227SGreg Roach 128c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 1298c2e8227SGreg Roach 13012664b30SGreg Roach $event_array = explode(',', $events); 13112664b30SGreg Roach 13212664b30SGreg Roach // If we are only showing living individuals, then we don't need to search for DEAT events. 13312664b30SGreg Roach if ($filter) { 13412664b30SGreg Roach $death_events = explode('|', WT_EVENTS_DEAT); 13512664b30SGreg Roach $event_array = array_diff($event_array, $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 1658c2e8227SGreg Roach if ($template) { 166e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 167c1010edaSGreg Roach $config_url = route('tree-page-block-edit', [ 168c1010edaSGreg Roach 'block_id' => $block_id, 169*aa6f03bbSGreg 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, 174*aa6f03bbSGreg Roach 'ged' => $tree->name(), 175c1010edaSGreg Roach ]); 1768cbbfdceSGreg Roach } else { 1778cbbfdceSGreg Roach $config_url = ''; 1788cbbfdceSGreg Roach } 1798cbbfdceSGreg Roach 180147e99aaSGreg Roach return view('modules/block-template', [ 1819c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1829c6524dcSGreg Roach 'id' => $block_id, 1838cbbfdceSGreg Roach 'config_url' => $config_url, 1849c6524dcSGreg Roach 'title' => $this->getTitle(), 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