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 */ 1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 1776692c8bSGreg Roach 180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter; 200eb072a2Smakitsouse Fisharebest\Webtrees\Functions\FunctionsDb; 2112664b30SGreg Roachuse Fisharebest\Webtrees\GedcomTag; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 23e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 248c2e8227SGreg Roach 258c2e8227SGreg Roach/** 268c2e8227SGreg Roach * Class UpcomingAnniversariesModule 278c2e8227SGreg Roach */ 28e2a378d3SGreg Roachclass UpcomingAnniversariesModule extends AbstractModule implements ModuleBlockInterface { 29c385536dSGreg Roach // Default values for new blocks. 30c385536dSGreg Roach const DEFAULT_DAYS = 7; 31c385536dSGreg Roach const DEFAULT_FILTER = '1'; 32c385536dSGreg Roach const DEFAULT_SORT = 'alpha'; 33c385536dSGreg Roach const DEFAULT_STYLE = 'table'; 34c385536dSGreg Roach 35c385536dSGreg Roach // Can show this number of days into the future. 36c385536dSGreg Roach const MIN_DAYS = 1; 37c385536dSGreg Roach const MAX_DAYS = 30; 38c385536dSGreg Roach 3912664b30SGreg Roach // All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN 4012664b30SGreg Roach const ALL_EVENTS = [ 4112664b30SGreg Roach 'ADOP', 4212664b30SGreg Roach 'ANUL', 4312664b30SGreg Roach 'BAPM', 4412664b30SGreg Roach 'BARM', 4512664b30SGreg Roach 'BASM', 4612664b30SGreg Roach 'BIRT', 4712664b30SGreg Roach 'BLES', 4812664b30SGreg Roach 'BURI', 4912664b30SGreg Roach 'CHR', 5012664b30SGreg Roach 'CHRA', 5112664b30SGreg Roach 'CONF', 5212664b30SGreg Roach 'CREM', 5312664b30SGreg Roach 'DEAT', 5412664b30SGreg Roach 'DIV', 5512664b30SGreg Roach 'DIVF', 5612664b30SGreg Roach 'EMIG', 5712664b30SGreg Roach 'ENGA', 5812664b30SGreg Roach 'FCOM', 5912664b30SGreg Roach 'GRAD', 6012664b30SGreg Roach 'IMMI', 6112664b30SGreg Roach 'MARB', 6212664b30SGreg Roach 'MARC', 6312664b30SGreg Roach 'MARL', 6412664b30SGreg Roach 'MARR', 6512664b30SGreg Roach 'MARS', 6612664b30SGreg Roach 'NATU', 6712664b30SGreg Roach 'ORDN', 6812664b30SGreg Roach 'PROB', 6912664b30SGreg Roach 'RETI', 7012664b30SGreg Roach 'WILL', 7112664b30SGreg Roach ]; 7212664b30SGreg Roach 7312664b30SGreg Roach const DEFAULT_EVENTS = [ 7412664b30SGreg Roach 'BIRT', 7512664b30SGreg Roach 'MARR', 7612664b30SGreg Roach 'DEAT', 7712664b30SGreg Roach ]; 7812664b30SGreg Roach 7976692c8bSGreg Roach /** 8076692c8bSGreg Roach * How should this module be labelled on tabs, menus, etc.? 8176692c8bSGreg Roach * 8276692c8bSGreg Roach * @return string 8376692c8bSGreg Roach */ 848c2e8227SGreg Roach public function getTitle() { 858c2e8227SGreg Roach return /* I18N: Name of a module */ I18N::translate('Upcoming events'); 868c2e8227SGreg Roach } 878c2e8227SGreg Roach 8876692c8bSGreg Roach /** 8976692c8bSGreg Roach * A sentence describing what this module does. 9076692c8bSGreg Roach * 9176692c8bSGreg Roach * @return string 9276692c8bSGreg Roach */ 938c2e8227SGreg Roach public function getDescription() { 948c2e8227SGreg Roach return /* I18N: Description of the “Upcoming events” module */ I18N::translate('A list of the anniversaries that will occur in the near future.'); 958c2e8227SGreg Roach } 968c2e8227SGreg Roach 9776692c8bSGreg Roach /** 9876692c8bSGreg Roach * Generate the HTML content of this block. 9976692c8bSGreg Roach * 100e490cd80SGreg Roach * @param Tree $tree 10176692c8bSGreg Roach * @param int $block_id 10276692c8bSGreg Roach * @param bool $template 103727f238cSGreg Roach * @param string[] $cfg 10476692c8bSGreg Roach * 10576692c8bSGreg Roach * @return string 10676692c8bSGreg Roach */ 107e490cd80SGreg Roach public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string { 108e490cd80SGreg Roach global $ctype; 1098c2e8227SGreg Roach 11012664b30SGreg Roach $default_events = implode(',', self::DEFAULT_EVENTS); 11112664b30SGreg Roach 112c385536dSGreg Roach $days = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 113c385536dSGreg Roach $filter = (bool) $this->getBlockSetting($block_id, 'filter', self::DEFAULT_FILTER); 114c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 115c385536dSGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT); 11612664b30SGreg Roach $events = $this->getBlockSetting($block_id, 'events', $default_events); 1178c2e8227SGreg Roach 118c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 1198c2e8227SGreg Roach 12012664b30SGreg Roach $event_array = explode(',', $events); 12112664b30SGreg Roach 12212664b30SGreg Roach // If we are only showing living individuals, then we don't need to search for DEAT events. 12312664b30SGreg Roach if ($filter) { 12412664b30SGreg Roach $death_events = explode('|', WT_EVENTS_DEAT); 12512664b30SGreg Roach $event_array = array_diff($event_array, $death_events); 12612664b30SGreg Roach } 12712664b30SGreg Roach 12812664b30SGreg Roach $events_filter = implode('|', $event_array); 12912664b30SGreg Roach 1308c2e8227SGreg Roach $startjd = WT_CLIENT_JD + 1; 131e490cd80SGreg Roach $endjd = WT_CLIENT_JD + (int) $days; 13212664b30SGreg Roach 133e490cd80SGreg Roach $facts = FunctionsDb::getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $tree); 13412664b30SGreg Roach 13512664b30SGreg Roach if (empty($facts)) { 136d8189b6aSDavid Drury if ($endjd == $startjd) { 137*147e99aaSGreg Roach $content = view('modules/upcoming_events/empty', [ 1380280f44aSGreg Roach 'message' => I18N::translate('No events exist for tomorrow.') 1390280f44aSGreg Roach ]); 140d8189b6aSDavid Drury } else { 141*147e99aaSGreg Roach $content = view('modules/upcoming_events/empty', [ 1420280f44aSGreg Roach 'message' => /* I18N: translation for %s==1 is unused; it is translated separately as “tomorrow” */ 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)) 1430280f44aSGreg Roach ]); 1448c2e8227SGreg Roach } 145*147e99aaSGreg Roach } elseif ($infoStyle === 'list') { 146*147e99aaSGreg Roach $content = view('modules/upcoming_events/list', [ 147d8189b6aSDavid Drury 'facts' => $facts, 148*147e99aaSGreg Roach ]); 149*147e99aaSGreg Roach } else { 150*147e99aaSGreg Roach $content = view('modules/upcoming_events/table', [ 151*147e99aaSGreg Roach 'facts' => $facts, 152*147e99aaSGreg Roach ]); 1530280f44aSGreg Roach } 1548c2e8227SGreg Roach 1558c2e8227SGreg Roach if ($template) { 156e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 157e490cd80SGreg Roach $config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]); 158397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 159e490cd80SGreg Roach $config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]); 1608cbbfdceSGreg Roach } else { 1618cbbfdceSGreg Roach $config_url = ''; 1628cbbfdceSGreg Roach } 1638cbbfdceSGreg Roach 164*147e99aaSGreg Roach return view('modules/block-template', [ 1659c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1669c6524dcSGreg Roach 'id' => $block_id, 1678cbbfdceSGreg Roach 'config_url' => $config_url, 1689c6524dcSGreg Roach 'title' => $this->getTitle(), 1699c6524dcSGreg Roach 'content' => $content, 1709c6524dcSGreg Roach ]); 1718c2e8227SGreg Roach } else { 1728c2e8227SGreg Roach return $content; 1738c2e8227SGreg Roach } 1748c2e8227SGreg Roach } 1758c2e8227SGreg Roach 1768c2e8227SGreg Roach /** {@inheritdoc} */ 177a9430be8SGreg Roach public function loadAjax(): bool { 1788c2e8227SGreg Roach return true; 1798c2e8227SGreg Roach } 1808c2e8227SGreg Roach 1818c2e8227SGreg Roach /** {@inheritdoc} */ 182a9430be8SGreg Roach public function isUserBlock(): bool { 1838c2e8227SGreg Roach return true; 1848c2e8227SGreg Roach } 1858c2e8227SGreg Roach 1868c2e8227SGreg Roach /** {@inheritdoc} */ 187a9430be8SGreg Roach public function isGedcomBlock(): bool { 1888c2e8227SGreg Roach return true; 1898c2e8227SGreg Roach } 1908c2e8227SGreg Roach 19176692c8bSGreg Roach /** 19276692c8bSGreg Roach * An HTML form to edit block settings 19376692c8bSGreg Roach * 194e490cd80SGreg Roach * @param Tree $tree 19576692c8bSGreg Roach * @param int $block_id 196a9430be8SGreg Roach * 197a9430be8SGreg Roach * @return void 19876692c8bSGreg Roach */ 199e490cd80SGreg Roach public function configureBlock(Tree $tree, int $block_id) { 200c385536dSGreg Roach if ($_SERVER['REQUEST_METHOD'] === 'POST') { 201c385536dSGreg Roach $this->setBlockSetting($block_id, 'days', Filter::postInteger('days', self::MIN_DAYS, self::MAX_DAYS, self::DEFAULT_DAYS)); 202e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter', Filter::postBool('filter')); 203c385536dSGreg Roach $this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', self::DEFAULT_STYLE)); 204c385536dSGreg Roach $this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'alpha|anniv', self::DEFAULT_SORT)); 20512664b30SGreg Roach $this->setBlockSetting($block_id, 'events', implode(',', Filter::postArray('events'))); 206c385536dSGreg Roach 207c385536dSGreg Roach return; 2088c2e8227SGreg Roach } 2098c2e8227SGreg Roach 21012664b30SGreg Roach $default_events = implode(',', self::DEFAULT_EVENTS); 21112664b30SGreg Roach 212c385536dSGreg Roach $days = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 213c385536dSGreg Roach $filter = $this->getBlockSetting($block_id, 'filter', self::DEFAULT_FILTER); 214c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 215c385536dSGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT); 21612664b30SGreg Roach $events = $this->getBlockSetting($block_id, 'events', $default_events); 2178c2e8227SGreg Roach 21812664b30SGreg Roach $event_array = explode(',', $events); 21912664b30SGreg Roach 22012664b30SGreg Roach $all_events = []; 22112664b30SGreg Roach foreach (self::ALL_EVENTS as $event) { 22212664b30SGreg Roach $all_events[$event] = GedcomTag::getLabel($event); 22312664b30SGreg Roach } 22412664b30SGreg Roach 225c385536dSGreg Roach $info_styles = [ 226c385536dSGreg Roach 'list' => /* I18N: An option in a list-box */ I18N::translate('list'), 227c385536dSGreg Roach 'table' => /* I18N: An option in a list-box */ I18N::translate('table'), 228c385536dSGreg Roach ]; 2298c2e8227SGreg Roach 230c385536dSGreg Roach $sort_styles = [ 231c385536dSGreg Roach 'alpha' => /* I18N: An option in a list-box */ I18N::translate('sort by name'), 232c385536dSGreg Roach 'anniv' => /* I18N: An option in a list-box */ I18N::translate('sort by date'), 233c385536dSGreg Roach ]; 2348c2e8227SGreg Roach 235*147e99aaSGreg Roach echo view('modules/upcoming_events/config', [ 236c385536dSGreg Roach 'all_events' => $all_events, 237c385536dSGreg Roach 'days' => $days, 238c385536dSGreg Roach 'event_array' => $event_array, 239c385536dSGreg Roach 'filter' => $filter, 240c385536dSGreg Roach 'infoStyle' => $infoStyle, 241c385536dSGreg Roach 'info_styles' => $info_styles, 242c385536dSGreg Roach 'max_days' => self::MAX_DAYS, 243c385536dSGreg Roach 'sortStyle' => $sortStyle, 244c385536dSGreg Roach 'sort_styles' => $sort_styles, 245c385536dSGreg Roach ]); 2468c2e8227SGreg Roach } 2478c2e8227SGreg Roach} 248