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; 20*0eb072a2Smakitsouse Fisharebest\Webtrees\Functions\FunctionsDb; 2112664b30SGreg Roachuse Fisharebest\Webtrees\GedcomTag; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 238c2e8227SGreg Roach 248c2e8227SGreg Roach/** 258c2e8227SGreg Roach * Class UpcomingAnniversariesModule 268c2e8227SGreg Roach */ 27e2a378d3SGreg Roachclass UpcomingAnniversariesModule extends AbstractModule implements ModuleBlockInterface { 28c385536dSGreg Roach // Default values for new blocks. 29c385536dSGreg Roach const DEFAULT_DAYS = 7; 30c385536dSGreg Roach const DEFAULT_FILTER = '1'; 31c385536dSGreg Roach const DEFAULT_SORT = 'alpha'; 32c385536dSGreg Roach const DEFAULT_STYLE = 'table'; 33c385536dSGreg Roach 34c385536dSGreg Roach // Can show this number of days into the future. 35c385536dSGreg Roach const MIN_DAYS = 1; 36c385536dSGreg Roach const MAX_DAYS = 30; 37c385536dSGreg Roach 3812664b30SGreg Roach // All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN 3912664b30SGreg Roach const ALL_EVENTS = [ 4012664b30SGreg Roach 'ADOP', 4112664b30SGreg Roach 'ANUL', 4212664b30SGreg Roach 'BAPM', 4312664b30SGreg Roach 'BARM', 4412664b30SGreg Roach 'BASM', 4512664b30SGreg Roach 'BIRT', 4612664b30SGreg Roach 'BLES', 4712664b30SGreg Roach 'BURI', 4812664b30SGreg Roach 'CHR', 4912664b30SGreg Roach 'CHRA', 5012664b30SGreg Roach 'CONF', 5112664b30SGreg Roach 'CREM', 5212664b30SGreg Roach 'DEAT', 5312664b30SGreg Roach 'DIV', 5412664b30SGreg Roach 'DIVF', 5512664b30SGreg Roach 'EMIG', 5612664b30SGreg Roach 'ENGA', 5712664b30SGreg Roach 'FCOM', 5812664b30SGreg Roach 'GRAD', 5912664b30SGreg Roach 'IMMI', 6012664b30SGreg Roach 'MARB', 6112664b30SGreg Roach 'MARC', 6212664b30SGreg Roach 'MARL', 6312664b30SGreg Roach 'MARR', 6412664b30SGreg Roach 'MARS', 6512664b30SGreg Roach 'NATU', 6612664b30SGreg Roach 'ORDN', 6712664b30SGreg Roach 'PROB', 6812664b30SGreg Roach 'RETI', 6912664b30SGreg Roach 'WILL', 7012664b30SGreg Roach ]; 7112664b30SGreg Roach 7212664b30SGreg Roach const DEFAULT_EVENTS = [ 7312664b30SGreg Roach 'BIRT', 7412664b30SGreg Roach 'MARR', 7512664b30SGreg Roach 'DEAT', 7612664b30SGreg Roach ]; 7712664b30SGreg Roach 7876692c8bSGreg Roach /** 7976692c8bSGreg Roach * How should this module be labelled on tabs, menus, etc.? 8076692c8bSGreg Roach * 8176692c8bSGreg Roach * @return string 8276692c8bSGreg Roach */ 838c2e8227SGreg Roach public function getTitle() { 848c2e8227SGreg Roach return /* I18N: Name of a module */ I18N::translate('Upcoming events'); 858c2e8227SGreg Roach } 868c2e8227SGreg Roach 8776692c8bSGreg Roach /** 8876692c8bSGreg Roach * A sentence describing what this module does. 8976692c8bSGreg Roach * 9076692c8bSGreg Roach * @return string 9176692c8bSGreg Roach */ 928c2e8227SGreg Roach public function getDescription() { 938c2e8227SGreg Roach return /* I18N: Description of the “Upcoming events” module */ I18N::translate('A list of the anniversaries that will occur in the near future.'); 948c2e8227SGreg Roach } 958c2e8227SGreg Roach 9676692c8bSGreg Roach /** 9776692c8bSGreg Roach * Generate the HTML content of this block. 9876692c8bSGreg Roach * 9976692c8bSGreg Roach * @param int $block_id 10076692c8bSGreg Roach * @param bool $template 101727f238cSGreg Roach * @param string[] $cfg 10276692c8bSGreg Roach * 10376692c8bSGreg Roach * @return string 10476692c8bSGreg Roach */ 105a9430be8SGreg Roach public function getBlock($block_id, $template = true, $cfg = []): string { 1064b9ff166SGreg Roach global $ctype, $WT_TREE; 1078c2e8227SGreg Roach 10812664b30SGreg Roach $default_events = implode(',', self::DEFAULT_EVENTS); 10912664b30SGreg Roach 110c385536dSGreg Roach $days = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 111c385536dSGreg Roach $filter = (bool) $this->getBlockSetting($block_id, 'filter', self::DEFAULT_FILTER); 112c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 113c385536dSGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT); 11412664b30SGreg Roach $events = $this->getBlockSetting($block_id, 'events', $default_events); 1158c2e8227SGreg Roach 116c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 1178c2e8227SGreg Roach 11812664b30SGreg Roach $event_array = explode(',', $events); 11912664b30SGreg Roach 12012664b30SGreg Roach // If we are only showing living individuals, then we don't need to search for DEAT events. 12112664b30SGreg Roach if ($filter) { 12212664b30SGreg Roach $death_events = explode('|', WT_EVENTS_DEAT); 12312664b30SGreg Roach $event_array = array_diff($event_array, $death_events); 12412664b30SGreg Roach } 12512664b30SGreg Roach 12612664b30SGreg Roach $events_filter = implode('|', $event_array); 12712664b30SGreg Roach 1288c2e8227SGreg Roach $startjd = WT_CLIENT_JD + 1; 1298c2e8227SGreg Roach $endjd = WT_CLIENT_JD + $days; 13012664b30SGreg Roach 131*0eb072a2Smakitso $facts = FunctionsDb::getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $WT_TREE); 13212664b30SGreg Roach 133d8189b6aSDavid Drury $summary = ''; 1348c2e8227SGreg Roach 13512664b30SGreg Roach if (empty($facts)) { 136d8189b6aSDavid Drury if ($filter) { 137d8189b6aSDavid Drury if ($endjd == $startjd) { 138d8189b6aSDavid Drury $summary = I18N::translate('No events for living individuals exist for tomorrow.'); 139d8189b6aSDavid Drury } else { 140d8189b6aSDavid Drury // I18N: translation for %s==1 is unused; it is translated separately as “tomorrow” 141d8189b6aSDavid Drury $summary = I18N::plural('No events for living people exist for the next %s day.', 'No events for living people exist for the next %s days.', $endjd - $startjd + 1, I18N::number($endjd - $startjd + 1)); 1428c2e8227SGreg Roach } 143d8189b6aSDavid Drury } else { 144d8189b6aSDavid Drury if ($endjd == $startjd) { 145d8189b6aSDavid Drury $summary = I18N::translate('No events exist for tomorrow.'); 146d8189b6aSDavid Drury } else { 147d8189b6aSDavid Drury // I18N: translation for %s==1 is unused; it is translated separately as “tomorrow” 148d8189b6aSDavid Drury $summary = 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)); 149d8189b6aSDavid Drury } 150d8189b6aSDavid Drury } 151d8189b6aSDavid Drury } 152d8189b6aSDavid Drury 153d8189b6aSDavid Drury $content = view('blocks/events-' . $infoStyle, [ 154d8189b6aSDavid Drury 'facts' => $facts, 155d8189b6aSDavid Drury 'summary' => $summary, 156d8189b6aSDavid Drury ] 157d8189b6aSDavid Drury ); 1588c2e8227SGreg Roach 1598c2e8227SGreg Roach if ($template) { 160397e599aSGreg Roach if ($ctype === 'gedcom' && Auth::isManager($WT_TREE)) { 161397e599aSGreg Roach $config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]); 162397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 163397e599aSGreg Roach $config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]); 1648cbbfdceSGreg Roach } else { 1658cbbfdceSGreg Roach $config_url = ''; 1668cbbfdceSGreg Roach } 1678cbbfdceSGreg Roach 168225e381fSGreg Roach return view('blocks/template', [ 1699c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1709c6524dcSGreg Roach 'id' => $block_id, 1718cbbfdceSGreg Roach 'config_url' => $config_url, 1729c6524dcSGreg Roach 'title' => $this->getTitle(), 1739c6524dcSGreg Roach 'content' => $content, 1749c6524dcSGreg Roach ]); 1758c2e8227SGreg Roach } else { 1768c2e8227SGreg Roach return $content; 1778c2e8227SGreg Roach } 1788c2e8227SGreg Roach } 1798c2e8227SGreg Roach 1808c2e8227SGreg Roach /** {@inheritdoc} */ 181a9430be8SGreg Roach public function loadAjax(): bool { 1828c2e8227SGreg Roach return true; 1838c2e8227SGreg Roach } 1848c2e8227SGreg Roach 1858c2e8227SGreg Roach /** {@inheritdoc} */ 186a9430be8SGreg Roach public function isUserBlock(): bool { 1878c2e8227SGreg Roach return true; 1888c2e8227SGreg Roach } 1898c2e8227SGreg Roach 1908c2e8227SGreg Roach /** {@inheritdoc} */ 191a9430be8SGreg Roach public function isGedcomBlock(): bool { 1928c2e8227SGreg Roach return true; 1938c2e8227SGreg Roach } 1948c2e8227SGreg Roach 19576692c8bSGreg Roach /** 19676692c8bSGreg Roach * An HTML form to edit block settings 19776692c8bSGreg Roach * 19876692c8bSGreg Roach * @param int $block_id 199a9430be8SGreg Roach * 200a9430be8SGreg Roach * @return void 20176692c8bSGreg Roach */ 202be9a728cSGreg Roach public function configureBlock($block_id) { 203c385536dSGreg Roach if ($_SERVER['REQUEST_METHOD'] === 'POST') { 204c385536dSGreg Roach $this->setBlockSetting($block_id, 'days', Filter::postInteger('days', self::MIN_DAYS, self::MAX_DAYS, self::DEFAULT_DAYS)); 205e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter', Filter::postBool('filter')); 206c385536dSGreg Roach $this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', self::DEFAULT_STYLE)); 207c385536dSGreg Roach $this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'alpha|anniv', self::DEFAULT_SORT)); 20812664b30SGreg Roach $this->setBlockSetting($block_id, 'events', implode(',', Filter::postArray('events'))); 209c385536dSGreg Roach 210c385536dSGreg Roach return; 2118c2e8227SGreg Roach } 2128c2e8227SGreg Roach 21312664b30SGreg Roach $default_events = implode(',', self::DEFAULT_EVENTS); 21412664b30SGreg Roach 215c385536dSGreg Roach $days = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 216c385536dSGreg Roach $filter = $this->getBlockSetting($block_id, 'filter', self::DEFAULT_FILTER); 217c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 218c385536dSGreg Roach $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT); 21912664b30SGreg Roach $events = $this->getBlockSetting($block_id, 'events', $default_events); 2208c2e8227SGreg Roach 22112664b30SGreg Roach $event_array = explode(',', $events); 22212664b30SGreg Roach 22312664b30SGreg Roach $all_events = []; 22412664b30SGreg Roach foreach (self::ALL_EVENTS as $event) { 22512664b30SGreg Roach $all_events[$event] = GedcomTag::getLabel($event); 22612664b30SGreg Roach } 22712664b30SGreg Roach 228c385536dSGreg Roach $info_styles = [ 229c385536dSGreg Roach 'list' => /* I18N: An option in a list-box */ I18N::translate('list'), 230c385536dSGreg Roach 'table' => /* I18N: An option in a list-box */ I18N::translate('table'), 231c385536dSGreg Roach ]; 2328c2e8227SGreg Roach 233c385536dSGreg Roach $sort_styles = [ 234c385536dSGreg Roach 'alpha' => /* I18N: An option in a list-box */ I18N::translate('sort by name'), 235c385536dSGreg Roach 'anniv' => /* I18N: An option in a list-box */ I18N::translate('sort by date'), 236c385536dSGreg Roach ]; 2378c2e8227SGreg Roach 238c385536dSGreg Roach echo view('blocks/upcoming-anniversaries-config', [ 239c385536dSGreg Roach 'all_events' => $all_events, 240c385536dSGreg Roach 'days' => $days, 241c385536dSGreg Roach 'event_array' => $event_array, 242c385536dSGreg Roach 'filter' => $filter, 243c385536dSGreg Roach 'infoStyle' => $infoStyle, 244c385536dSGreg Roach 'info_styles' => $info_styles, 245c385536dSGreg Roach 'max_days' => self::MAX_DAYS, 246c385536dSGreg Roach 'sortStyle' => $sortStyle, 247c385536dSGreg Roach 'sort_styles' => $sort_styles, 248c385536dSGreg Roach ]); 2498c2e8227SGreg Roach } 2508c2e8227SGreg Roach} 251