xref: /webtrees/app/Module/UpcomingAnniversariesModule.php (revision d97083fe315dad9b7d0a150d4fb5f563e57d1869)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98c2e8227SGreg Roach * (at your option) any later version.
108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138c2e8227SGreg Roach * GNU General Public License for more details.
148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
228d0ebef0SGreg Roachuse Fisharebest\Webtrees\Gedcom;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
2469cdf014SGreg Roachuse Fisharebest\Webtrees\Registry;
25f0a11419SGreg Roachuse Fisharebest\Webtrees\Services\CalendarService;
26e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
271e7a7a28SGreg Roachuse Illuminate\Support\Str;
286ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
298c2e8227SGreg Roach
308c2e8227SGreg Roach/**
318c2e8227SGreg Roach * Class UpcomingAnniversariesModule
328c2e8227SGreg Roach */
3337eb8894SGreg Roachclass UpcomingAnniversariesModule extends AbstractModule implements ModuleBlockInterface
34c1010edaSGreg Roach{
3549a243cbSGreg Roach    use ModuleBlockTrait;
3649a243cbSGreg Roach
37c385536dSGreg Roach    // Default values for new blocks.
3816d6367aSGreg Roach    private const DEFAULT_DAYS   = '7';
3916d6367aSGreg Roach    private const DEFAULT_FILTER = '1';
4016d6367aSGreg Roach    private const DEFAULT_SORT   = 'alpha';
4116d6367aSGreg Roach    private const DEFAULT_STYLE  = 'table';
42c385536dSGreg Roach
430999990bSGreg Roach    // Initial sorting for datatables
440999990bSGreg Roach    private const DATATABLES_ORDER = [
450999990bSGreg Roach        'alpha' => [[0, 'asc']],
460999990bSGreg Roach        'anniv' => [[1, 'asc']],
470999990bSGreg Roach    ];
480999990bSGreg Roach
49c385536dSGreg Roach    // Can show this number of days into the future.
5016d6367aSGreg Roach    private const MAX_DAYS = 30;
51c385536dSGreg Roach
5201221f27SGreg Roach    // Pagination
5301221f27SGreg Roach    private const LIMIT_LOW  = 10;
5401221f27SGreg Roach    private const LIMIT_HIGH = 20;
5501221f27SGreg Roach
5612664b30SGreg Roach    // All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN
5716d6367aSGreg Roach    private const ALL_EVENTS = [
5869cdf014SGreg Roach        'ADOP' => 'INDI:ADOP',
5969cdf014SGreg Roach        'ANUL' => 'FAM:ANUL',
6069cdf014SGreg Roach        'BAPM' => 'INDI:BAPM',
6169cdf014SGreg Roach        'BARM' => 'INDI:BARM',
6269cdf014SGreg Roach        'BASM' => 'INDI:BASM',
6369cdf014SGreg Roach        'BIRT' => 'INDI:BIRT',
6469cdf014SGreg Roach        'BLES' => 'INDI:BLES',
6569cdf014SGreg Roach        'BURI' => 'INDI:BURI',
6669cdf014SGreg Roach        'CHR'  => 'INDI:CHR',
6769cdf014SGreg Roach        'CHRA' => 'INDI:CHRA',
6869cdf014SGreg Roach        'CONF' => 'INDI:CONF',
6969cdf014SGreg Roach        'CREM' => 'INDI:CREM',
7069cdf014SGreg Roach        'DEAT' => 'INDI:DEAT',
7169cdf014SGreg Roach        'DIV'  => 'FAM:DIV',
7269cdf014SGreg Roach        'DIVF' => 'FAM:DIVF',
7369cdf014SGreg Roach        'EMIG' => 'INDI:EMIG',
7469cdf014SGreg Roach        'ENGA' => 'FAM:ENGA',
7569cdf014SGreg Roach        'FCOM' => 'INDI:FCOM',
7669cdf014SGreg Roach        'GRAD' => 'INDI:GRAD',
7769cdf014SGreg Roach        'IMMI' => 'INDI:IMMI',
7869cdf014SGreg Roach        'MARB' => 'FAM:MARB',
7969cdf014SGreg Roach        'MARC' => 'FAM:MARC',
8069cdf014SGreg Roach        'MARL' => 'FAM:MARL',
8169cdf014SGreg Roach        'MARR' => 'FAM:MARR',
8269cdf014SGreg Roach        'MARS' => 'FAM:MARS',
8369cdf014SGreg Roach        'NATU' => 'INDI:NATU',
8469cdf014SGreg Roach        'ORDN' => 'INDI:ORDN',
8569cdf014SGreg Roach        'PROB' => 'INDI:PROB',
8669cdf014SGreg Roach        'RETI' => 'INDI:RETI',
8769cdf014SGreg Roach        'WILL' => 'INDI:WILL',
8812664b30SGreg Roach    ];
8912664b30SGreg Roach
9016d6367aSGreg Roach    private const DEFAULT_EVENTS = [
9112664b30SGreg Roach        'BIRT',
9212664b30SGreg Roach        'MARR',
9312664b30SGreg Roach        'DEAT',
9412664b30SGreg Roach    ];
9512664b30SGreg Roach
9676692c8bSGreg Roach    /**
970cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
9876692c8bSGreg Roach     *
9976692c8bSGreg Roach     * @return string
10076692c8bSGreg Roach     */
10149a243cbSGreg Roach    public function title(): string
102c1010edaSGreg Roach    {
103bbb76c12SGreg Roach        /* I18N: Name of a module */
104bbb76c12SGreg Roach        return I18N::translate('Upcoming events');
1058c2e8227SGreg Roach    }
1068c2e8227SGreg Roach
10776692c8bSGreg Roach    /**
10876692c8bSGreg Roach     * A sentence describing what this module does.
10976692c8bSGreg Roach     *
11076692c8bSGreg Roach     * @return string
11176692c8bSGreg Roach     */
11249a243cbSGreg Roach    public function description(): string
113c1010edaSGreg Roach    {
114bbb76c12SGreg Roach        /* I18N: Description of the “Upcoming events” module */
115bbb76c12SGreg Roach        return I18N::translate('A list of the anniversaries that will occur in the near future.');
1168c2e8227SGreg Roach    }
1178c2e8227SGreg Roach
11876692c8bSGreg Roach    /**
11976692c8bSGreg Roach     * Generate the HTML content of this block.
12076692c8bSGreg Roach     *
121e490cd80SGreg Roach     * @param Tree          $tree
12276692c8bSGreg Roach     * @param int           $block_id
1233caaa4d2SGreg Roach     * @param string        $context
12409482a55SGreg Roach     * @param array<string> $config
12576692c8bSGreg Roach     *
12676692c8bSGreg Roach     * @return string
12776692c8bSGreg Roach     */
1283caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
129c1010edaSGreg Roach    {
130f0a11419SGreg Roach        $calendar_service = new CalendarService();
131f0a11419SGreg Roach
13212664b30SGreg Roach        $default_events = implode(',', self::DEFAULT_EVENTS);
13312664b30SGreg Roach
13476f666f4SGreg Roach        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
135c385536dSGreg Roach        $filter    = (bool) $this->getBlockSetting($block_id, 'filter', self::DEFAULT_FILTER);
136c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
137c385536dSGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT);
13812664b30SGreg Roach        $events    = $this->getBlockSetting($block_id, 'events', $default_events);
1398c2e8227SGreg Roach
1403caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
1418c2e8227SGreg Roach
14212664b30SGreg Roach        $event_array = explode(',', $events);
14312664b30SGreg Roach
14412664b30SGreg Roach        // If we are only showing living individuals, then we don't need to search for DEAT events.
14512664b30SGreg Roach        if ($filter) {
1468d0ebef0SGreg Roach            $event_array = array_diff($event_array, Gedcom::DEATH_EVENTS);
14712664b30SGreg Roach        }
14812664b30SGreg Roach
14912664b30SGreg Roach        $events_filter = implode('|', $event_array);
15012664b30SGreg Roach
151*d97083feSGreg Roach        $startjd = Registry::timestampFactory()->now()->addDays(1)->julianDay();
152*d97083feSGreg Roach        $endjd   = Registry::timestampFactory()->now()->addDays($days)->julianDay();
15312664b30SGreg Roach
154f0a11419SGreg Roach        $facts = $calendar_service->getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $tree);
15512664b30SGreg Roach
156cd51dbdfSGreg Roach        if ($facts->isEmpty()) {
1577fa97a69SGreg Roach            if ($endjd === $startjd) {
158147e99aaSGreg Roach                $content = view('modules/upcoming_events/empty', [
159c1010edaSGreg Roach                    'message' => I18N::translate('No events exist for tomorrow.'),
1600280f44aSGreg Roach                ]);
161d8189b6aSDavid Drury            } else {
162bbb76c12SGreg Roach                /* I18N: translation for %s==1 is unused; it is translated separately as “tomorrow” */                $content = view('modules/upcoming_events/empty', [
163bbb76c12SGreg 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)),
1640280f44aSGreg Roach                ]);
1658c2e8227SGreg Roach            }
166147e99aaSGreg Roach        } elseif ($infoStyle === 'list') {
16748f08416SGreg Roach            $content = view('lists/anniversaries-list', [
168e24053e5SGreg Roach                'id'         => $block_id,
169d8189b6aSDavid Drury                'facts'      => $facts,
17001221f27SGreg Roach                'limit_low'  => self::LIMIT_LOW,
17101221f27SGreg Roach                'limit_high' => self::LIMIT_HIGH,
172147e99aaSGreg Roach            ]);
173147e99aaSGreg Roach        } else {
17448f08416SGreg Roach            $content = view('lists/anniversaries-table', [
175147e99aaSGreg Roach                'facts'      => $facts,
17601221f27SGreg Roach                'limit_low'  => self::LIMIT_LOW,
17701221f27SGreg Roach                'limit_high' => self::LIMIT_HIGH,
1780999990bSGreg Roach                'order'      => self::DATATABLES_ORDER[$sortStyle],
179147e99aaSGreg Roach            ]);
1800280f44aSGreg Roach        }
1818c2e8227SGreg Roach
1823caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
183147e99aaSGreg Roach            return view('modules/block-template', [
1841e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1859c6524dcSGreg Roach                'id'         => $block_id,
1863caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
18749a243cbSGreg Roach                'title'      => $this->title(),
1889c6524dcSGreg Roach                'content'    => $content,
1899c6524dcSGreg Roach            ]);
1908c2e8227SGreg Roach        }
191b2ce94c6SRico Sonntag
192b2ce94c6SRico Sonntag        return $content;
1938c2e8227SGreg Roach    }
1948c2e8227SGreg Roach
1953caaa4d2SGreg Roach    /**
1963caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1973caaa4d2SGreg Roach     *
1983caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1993caaa4d2SGreg Roach     *
2003caaa4d2SGreg Roach     * @return bool
2013caaa4d2SGreg Roach     */
202c1010edaSGreg Roach    public function loadAjax(): bool
203c1010edaSGreg Roach    {
2048c2e8227SGreg Roach        return true;
2058c2e8227SGreg Roach    }
2068c2e8227SGreg Roach
2073caaa4d2SGreg Roach    /**
2083caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
2093caaa4d2SGreg Roach     *
2103caaa4d2SGreg Roach     * @return bool
2113caaa4d2SGreg Roach     */
212c1010edaSGreg Roach    public function isUserBlock(): bool
213c1010edaSGreg Roach    {
2148c2e8227SGreg Roach        return true;
2158c2e8227SGreg Roach    }
2168c2e8227SGreg Roach
2173caaa4d2SGreg Roach    /**
2183caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
2193caaa4d2SGreg Roach     *
2203caaa4d2SGreg Roach     * @return bool
2213caaa4d2SGreg Roach     */
22263276d8fSGreg Roach    public function isTreeBlock(): bool
223c1010edaSGreg Roach    {
2248c2e8227SGreg Roach        return true;
2258c2e8227SGreg Roach    }
2268c2e8227SGreg Roach
22776692c8bSGreg Roach    /**
228a45f9889SGreg Roach     * Update the configuration for a block.
229a45f9889SGreg Roach     *
2306ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
231a45f9889SGreg Roach     * @param int     $block_id
232a45f9889SGreg Roach     *
233a45f9889SGreg Roach     * @return void
234a45f9889SGreg Roach     */
2356ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
236a45f9889SGreg Roach    {
237b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
238c50a90beSGreg Roach
239c50a90beSGreg Roach        $this->setBlockSetting($block_id, 'days', $params['days']);
240c50a90beSGreg Roach        $this->setBlockSetting($block_id, 'filter', $params['filter']);
241c50a90beSGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']);
242c50a90beSGreg Roach        $this->setBlockSetting($block_id, 'sortStyle', $params['sortStyle']);
243c50a90beSGreg Roach        $this->setBlockSetting($block_id, 'events', implode(',', $params['events'] ?? []));
244a45f9889SGreg Roach    }
245a45f9889SGreg Roach
246a45f9889SGreg Roach    /**
24776692c8bSGreg Roach     * An HTML form to edit block settings
24876692c8bSGreg Roach     *
249e490cd80SGreg Roach     * @param Tree $tree
25076692c8bSGreg Roach     * @param int  $block_id
251a9430be8SGreg Roach     *
2523caaa4d2SGreg Roach     * @return string
25376692c8bSGreg Roach     */
2543caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
255c1010edaSGreg Roach    {
25612664b30SGreg Roach        $default_events = implode(',', self::DEFAULT_EVENTS);
25712664b30SGreg Roach
25876f666f4SGreg Roach        $days       = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
259c385536dSGreg Roach        $filter     = $this->getBlockSetting($block_id, 'filter', self::DEFAULT_FILTER);
2607c2c99faSGreg Roach        $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
2617c2c99faSGreg Roach        $sort_style = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT);
26212664b30SGreg Roach        $events     = $this->getBlockSetting($block_id, 'events', $default_events);
2638c2e8227SGreg Roach
26412664b30SGreg Roach        $event_array = explode(',', $events);
26512664b30SGreg Roach
26612664b30SGreg Roach        $all_events = [];
26769cdf014SGreg Roach        foreach (self::ALL_EVENTS as $event => $tag) {
26869cdf014SGreg Roach            $all_events[$event] = Registry::elementFactory()->make($tag)->label();
26912664b30SGreg Roach        }
27012664b30SGreg Roach
271c385536dSGreg Roach        $info_styles = [
272bbb76c12SGreg Roach            /* I18N: An option in a list-box */
273bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
274bbb76c12SGreg Roach            /* I18N: An option in a list-box */
275bbb76c12SGreg Roach            'table' => I18N::translate('table'),
276c385536dSGreg Roach        ];
2778c2e8227SGreg Roach
278c385536dSGreg Roach        $sort_styles = [
279bbb76c12SGreg Roach            /* I18N: An option in a list-box */
280bbb76c12SGreg Roach            'alpha' => I18N::translate('sort by name'),
281bbb76c12SGreg Roach            /* I18N: An option in a list-box */
282bbb76c12SGreg Roach            'anniv' => I18N::translate('sort by date'),
283c385536dSGreg Roach        ];
2848c2e8227SGreg Roach
2853caaa4d2SGreg Roach        return view('modules/upcoming_events/config', [
286c385536dSGreg Roach            'all_events'  => $all_events,
287c385536dSGreg Roach            'days'        => $days,
288c385536dSGreg Roach            'event_array' => $event_array,
289c385536dSGreg Roach            'filter'      => $filter,
2907c2c99faSGreg Roach            'info_style'  => $info_style,
291c385536dSGreg Roach            'info_styles' => $info_styles,
292c385536dSGreg Roach            'max_days'    => self::MAX_DAYS,
2937c2c99faSGreg Roach            'sort_style'  => $sort_style,
294c385536dSGreg Roach            'sort_styles' => $sort_styles,
295c385536dSGreg Roach        ]);
2968c2e8227SGreg Roach    }
2978c2e8227SGreg Roach}
298