xref: /webtrees/app/Module/UpcomingAnniversariesModule.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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
2205cb9775SGreg Roachuse Fisharebest\Webtrees\Auth;
238d0ebef0SGreg Roachuse Fisharebest\Webtrees\Gedcom;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
2569cdf014SGreg Roachuse Fisharebest\Webtrees\Registry;
26f0a11419SGreg Roachuse Fisharebest\Webtrees\Services\CalendarService;
27e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
28b6f85d51SGreg Roachuse Fisharebest\Webtrees\Validator;
291e7a7a28SGreg Roachuse Illuminate\Support\Str;
306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
318c2e8227SGreg Roach
328c2e8227SGreg Roach/**
338c2e8227SGreg Roach * Class UpcomingAnniversariesModule
348c2e8227SGreg Roach */
3537eb8894SGreg Roachclass UpcomingAnniversariesModule extends AbstractModule implements ModuleBlockInterface
36c1010edaSGreg Roach{
3749a243cbSGreg Roach    use ModuleBlockTrait;
3849a243cbSGreg Roach
39b6f85d51SGreg Roach    private const SORT_STYLE_DATE = 'anniv';
40b6f85d51SGreg Roach    private const SORT_STYLE_NAME = 'alpha';
41b6f85d51SGreg Roach
42b6f85d51SGreg Roach    private const LAYOUT_STYLE_LIST  = 'list';
43b6f85d51SGreg Roach    private const LAYOUT_STYLE_TABLE = 'table';
44b6f85d51SGreg Roach
45c385536dSGreg Roach    // Default values for new blocks.
4616d6367aSGreg Roach    private const DEFAULT_DAYS   = '7';
4716d6367aSGreg Roach    private const DEFAULT_FILTER = '1';
48b6f85d51SGreg Roach    private const DEFAULT_SORT   = self::SORT_STYLE_NAME;
49b6f85d51SGreg Roach    private const DEFAULT_STYLE  = self::LAYOUT_STYLE_TABLE;
50c385536dSGreg Roach
510999990bSGreg Roach    // Initial sorting for datatables
520999990bSGreg Roach    private const DATATABLES_ORDER = [
53b6f85d51SGreg Roach        self::SORT_STYLE_NAME => [[0, 'asc']],
54b6f85d51SGreg Roach        self::SORT_STYLE_DATE => [[1, 'asc']],
550999990bSGreg Roach    ];
560999990bSGreg Roach
57c385536dSGreg Roach    // Can show this number of days into the future.
5816d6367aSGreg Roach    private const MAX_DAYS = 30;
59c385536dSGreg Roach
6001221f27SGreg Roach    // Pagination
6101221f27SGreg Roach    private const LIMIT_LOW  = 10;
6201221f27SGreg Roach    private const LIMIT_HIGH = 20;
6301221f27SGreg Roach
6412664b30SGreg Roach    // All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN
6516d6367aSGreg Roach    private const ALL_EVENTS = [
6669cdf014SGreg Roach        'ADOP' => 'INDI:ADOP',
6769cdf014SGreg Roach        'ANUL' => 'FAM:ANUL',
6869cdf014SGreg Roach        'BAPM' => 'INDI:BAPM',
6969cdf014SGreg Roach        'BARM' => 'INDI:BARM',
7069cdf014SGreg Roach        'BASM' => 'INDI:BASM',
7169cdf014SGreg Roach        'BIRT' => 'INDI:BIRT',
7269cdf014SGreg Roach        'BLES' => 'INDI:BLES',
7369cdf014SGreg Roach        'BURI' => 'INDI:BURI',
7469cdf014SGreg Roach        'CHR'  => 'INDI:CHR',
7569cdf014SGreg Roach        'CHRA' => 'INDI:CHRA',
7669cdf014SGreg Roach        'CONF' => 'INDI:CONF',
7769cdf014SGreg Roach        'CREM' => 'INDI:CREM',
7869cdf014SGreg Roach        'DEAT' => 'INDI:DEAT',
7969cdf014SGreg Roach        'DIV'  => 'FAM:DIV',
8069cdf014SGreg Roach        'DIVF' => 'FAM:DIVF',
8169cdf014SGreg Roach        'EMIG' => 'INDI:EMIG',
8269cdf014SGreg Roach        'ENGA' => 'FAM:ENGA',
8369cdf014SGreg Roach        'FCOM' => 'INDI:FCOM',
8469cdf014SGreg Roach        'GRAD' => 'INDI:GRAD',
8569cdf014SGreg Roach        'IMMI' => 'INDI:IMMI',
8669cdf014SGreg Roach        'MARB' => 'FAM:MARB',
8769cdf014SGreg Roach        'MARC' => 'FAM:MARC',
8869cdf014SGreg Roach        'MARL' => 'FAM:MARL',
8969cdf014SGreg Roach        'MARR' => 'FAM:MARR',
9069cdf014SGreg Roach        'MARS' => 'FAM:MARS',
9169cdf014SGreg Roach        'NATU' => 'INDI:NATU',
9269cdf014SGreg Roach        'ORDN' => 'INDI:ORDN',
9369cdf014SGreg Roach        'PROB' => 'INDI:PROB',
9469cdf014SGreg Roach        'RETI' => 'INDI:RETI',
9569cdf014SGreg Roach        'WILL' => 'INDI:WILL',
9612664b30SGreg Roach    ];
9712664b30SGreg Roach
9816d6367aSGreg Roach    private const DEFAULT_EVENTS = [
9912664b30SGreg Roach        'BIRT',
10012664b30SGreg Roach        'MARR',
10112664b30SGreg Roach        'DEAT',
10212664b30SGreg Roach    ];
10312664b30SGreg Roach
104b6f85d51SGreg Roach    private CalendarService $calendar_service;
105b6f85d51SGreg Roach
106b6f85d51SGreg Roach    /**
107b6f85d51SGreg Roach     * @param CalendarService $calendar_service
108b6f85d51SGreg Roach     */
109b6f85d51SGreg Roach    public function __construct(CalendarService $calendar_service)
110b6f85d51SGreg Roach    {
111b6f85d51SGreg Roach        $this->calendar_service = $calendar_service;
112b6f85d51SGreg Roach    }
113b6f85d51SGreg Roach
11476692c8bSGreg Roach    /**
1150cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
11676692c8bSGreg Roach     *
11776692c8bSGreg Roach     * @return string
11876692c8bSGreg Roach     */
11949a243cbSGreg Roach    public function title(): string
120c1010edaSGreg Roach    {
121bbb76c12SGreg Roach        /* I18N: Name of a module */
122bbb76c12SGreg Roach        return I18N::translate('Upcoming events');
1238c2e8227SGreg Roach    }
1248c2e8227SGreg Roach
12549a243cbSGreg Roach    public function description(): string
126c1010edaSGreg Roach    {
127bbb76c12SGreg Roach        /* I18N: Description of the “Upcoming events” module */
128bbb76c12SGreg Roach        return I18N::translate('A list of the anniversaries that will occur in the near future.');
1298c2e8227SGreg Roach    }
1308c2e8227SGreg Roach
13176692c8bSGreg Roach    /**
13276692c8bSGreg Roach     * Generate the HTML content of this block.
13376692c8bSGreg Roach     *
134e490cd80SGreg Roach     * @param Tree                 $tree
13576692c8bSGreg Roach     * @param int                  $block_id
1363caaa4d2SGreg Roach     * @param string               $context
13776d39c55SGreg Roach     * @param array<string,string> $config
13876692c8bSGreg Roach     *
13976692c8bSGreg Roach     * @return string
14076692c8bSGreg Roach     */
1413caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
142c1010edaSGreg Roach    {
14312664b30SGreg Roach        $default_events = implode(',', self::DEFAULT_EVENTS);
14412664b30SGreg Roach
14576f666f4SGreg Roach        $days      = (int)$this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
146c385536dSGreg Roach        $filter    = (bool)$this->getBlockSetting($block_id, 'filter', self::DEFAULT_FILTER);
147c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
148c385536dSGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT);
14912664b30SGreg Roach        $events    = $this->getBlockSetting($block_id, 'events', $default_events);
1508c2e8227SGreg Roach
1513caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
1528c2e8227SGreg Roach
15312664b30SGreg Roach        $event_array = explode(',', $events);
15412664b30SGreg Roach
15512664b30SGreg Roach        // If we are only showing living individuals, then we don't need to search for DEAT events.
15612664b30SGreg Roach        if ($filter) {
1578d0ebef0SGreg Roach            $event_array = array_diff($event_array, Gedcom::DEATH_EVENTS);
15812664b30SGreg Roach        }
15912664b30SGreg Roach
16012664b30SGreg Roach        $events_filter = implode('|', $event_array);
16112664b30SGreg Roach
162d97083feSGreg Roach        $startjd = Registry::timestampFactory()->now()->addDays(1)->julianDay();
163d97083feSGreg Roach        $endjd   = Registry::timestampFactory()->now()->addDays($days)->julianDay();
16412664b30SGreg Roach
165b6f85d51SGreg Roach        $facts = $this->calendar_service->getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $tree);
16612664b30SGreg Roach
167cd51dbdfSGreg Roach        if ($facts->isEmpty()) {
1687fa97a69SGreg Roach            if ($endjd === $startjd) {
16905cb9775SGreg Roach                if ($filter && Auth::check()) {
170b13a72dbSGreg Roach                    $message = I18N::translate('No events for living individuals exist for tomorrow.');
171b13a72dbSGreg Roach                } else {
172b13a72dbSGreg Roach                    $message = I18N::translate('No events exist for tomorrow.');
173b13a72dbSGreg Roach                }
174b13a72dbSGreg Roach            } else {
17505cb9775SGreg Roach                if ($filter && Auth::check()) {
176b13a72dbSGreg Roach                    $message = 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));
177d8189b6aSDavid Drury                } else {
178b13a72dbSGreg 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));
179b13a72dbSGreg Roach                }
1808c2e8227SGreg Roach            }
1811821c9e5SGreg Roach
1821821c9e5SGreg Roach            $content = view('modules/upcoming_events/empty', ['message' => $message]);
183147e99aaSGreg Roach        } elseif ($infoStyle === 'list') {
18448f08416SGreg Roach            $content = view('lists/anniversaries-list', [
185e24053e5SGreg Roach                'id'         => $block_id,
186d8189b6aSDavid Drury                'facts'      => $facts,
18701221f27SGreg Roach                'limit_low'  => self::LIMIT_LOW,
18801221f27SGreg Roach                'limit_high' => self::LIMIT_HIGH,
189147e99aaSGreg Roach            ]);
190147e99aaSGreg Roach        } else {
19148f08416SGreg Roach            $content = view('lists/anniversaries-table', [
192147e99aaSGreg Roach                'facts'      => $facts,
19301221f27SGreg Roach                'limit_low'  => self::LIMIT_LOW,
19401221f27SGreg Roach                'limit_high' => self::LIMIT_HIGH,
1950999990bSGreg Roach                'order'      => self::DATATABLES_ORDER[$sortStyle],
196147e99aaSGreg Roach            ]);
1970280f44aSGreg Roach        }
1988c2e8227SGreg Roach
1993caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
200147e99aaSGreg Roach            return view('modules/block-template', [
2011e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
2029c6524dcSGreg Roach                'id'         => $block_id,
2033caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
20449a243cbSGreg Roach                'title'      => $this->title(),
2059c6524dcSGreg Roach                'content'    => $content,
2069c6524dcSGreg Roach            ]);
2078c2e8227SGreg Roach        }
208b2ce94c6SRico Sonntag
209b2ce94c6SRico Sonntag        return $content;
2108c2e8227SGreg Roach    }
2118c2e8227SGreg Roach
2123caaa4d2SGreg Roach    /**
2133caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
2143caaa4d2SGreg Roach     *
2153caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
2163caaa4d2SGreg Roach     *
2173caaa4d2SGreg Roach     * @return bool
2183caaa4d2SGreg Roach     */
219c1010edaSGreg Roach    public function loadAjax(): bool
220c1010edaSGreg Roach    {
2218c2e8227SGreg Roach        return true;
2228c2e8227SGreg Roach    }
2238c2e8227SGreg Roach
2243caaa4d2SGreg Roach    /**
2253caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
2263caaa4d2SGreg Roach     *
2273caaa4d2SGreg Roach     * @return bool
2283caaa4d2SGreg Roach     */
229c1010edaSGreg Roach    public function isUserBlock(): bool
230c1010edaSGreg Roach    {
2318c2e8227SGreg Roach        return true;
2328c2e8227SGreg Roach    }
2338c2e8227SGreg Roach
2343caaa4d2SGreg Roach    /**
2353caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
2363caaa4d2SGreg Roach     *
2373caaa4d2SGreg Roach     * @return bool
2383caaa4d2SGreg Roach     */
23963276d8fSGreg Roach    public function isTreeBlock(): bool
240c1010edaSGreg Roach    {
2418c2e8227SGreg Roach        return true;
2428c2e8227SGreg Roach    }
2438c2e8227SGreg Roach
24476692c8bSGreg Roach    /**
245a45f9889SGreg Roach     * Update the configuration for a block.
246a45f9889SGreg Roach     *
2476ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
248a45f9889SGreg Roach     * @param int                    $block_id
249a45f9889SGreg Roach     *
250a45f9889SGreg Roach     * @return void
251a45f9889SGreg Roach     */
2526ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
253a45f9889SGreg Roach    {
254b6f85d51SGreg Roach        $days       = Validator::parsedBody($request)->isBetween(1, self::MAX_DAYS)->integer('days');
255b6f85d51SGreg Roach        $filter     = Validator::parsedBody($request)->boolean('filter');
256b6f85d51SGreg Roach        $info_style = Validator::parsedBody($request)->isInArrayKeys($this->infoStyles())->string('infoStyle');
257b6f85d51SGreg Roach        $sort_style = Validator::parsedBody($request)->isInArrayKeys($this->sortStyles())->string('sortStyle');
258b6f85d51SGreg Roach        $events     = Validator::parsedBody($request)->array('events');
259c50a90beSGreg Roach
260b6f85d51SGreg Roach        $this->setBlockSetting($block_id, 'days', (string)$days);
261b6f85d51SGreg Roach        $this->setBlockSetting($block_id, 'filter', (string)$filter);
262b6f85d51SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $info_style);
263b6f85d51SGreg Roach        $this->setBlockSetting($block_id, 'sortStyle', $sort_style);
264b6f85d51SGreg Roach        $this->setBlockSetting($block_id, 'events', implode(',', $events));
265a45f9889SGreg Roach    }
266a45f9889SGreg Roach
267a45f9889SGreg Roach    /**
26876692c8bSGreg Roach     * An HTML form to edit block settings
26976692c8bSGreg Roach     *
270e490cd80SGreg Roach     * @param Tree $tree
27176692c8bSGreg Roach     * @param int  $block_id
272a9430be8SGreg Roach     *
2733caaa4d2SGreg Roach     * @return string
27476692c8bSGreg Roach     */
2753caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
276c1010edaSGreg Roach    {
27712664b30SGreg Roach        $default_events = implode(',', self::DEFAULT_EVENTS);
27812664b30SGreg Roach
27976f666f4SGreg Roach        $days       = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
280c385536dSGreg Roach        $filter     = $this->getBlockSetting($block_id, 'filter', self::DEFAULT_FILTER);
2817c2c99faSGreg Roach        $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
2827c2c99faSGreg Roach        $sort_style = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT);
28312664b30SGreg Roach        $events     = $this->getBlockSetting($block_id, 'events', $default_events);
2848c2e8227SGreg Roach
28512664b30SGreg Roach        $event_array = explode(',', $events);
28612664b30SGreg Roach
28712664b30SGreg Roach        $all_events = [];
28869cdf014SGreg Roach        foreach (self::ALL_EVENTS as $event => $tag) {
28969cdf014SGreg Roach            $all_events[$event] = Registry::elementFactory()->make($tag)->label();
29012664b30SGreg Roach        }
29112664b30SGreg Roach
2923caaa4d2SGreg Roach        return view('modules/upcoming_events/config', [
293c385536dSGreg Roach            'all_events'  => $all_events,
294c385536dSGreg Roach            'days'        => $days,
295c385536dSGreg Roach            'event_array' => $event_array,
296c385536dSGreg Roach            'filter'      => $filter,
2977c2c99faSGreg Roach            'info_style'  => $info_style,
298b6f85d51SGreg Roach            'info_styles' => $this->infoStyles(),
299c385536dSGreg Roach            'max_days'    => self::MAX_DAYS,
3007c2c99faSGreg Roach            'sort_style'  => $sort_style,
301b6f85d51SGreg Roach            'sort_styles' => $this->sortStyles(),
302c385536dSGreg Roach        ]);
3038c2e8227SGreg Roach    }
304b6f85d51SGreg Roach
305b6f85d51SGreg Roach    /**
306b6f85d51SGreg Roach     * @return array<string,string>
307b6f85d51SGreg Roach     */
308b6f85d51SGreg Roach    private function infoStyles(): array
309b6f85d51SGreg Roach    {
310b6f85d51SGreg Roach        return [
311b6f85d51SGreg Roach            self::LAYOUT_STYLE_LIST  => /* I18N: An option in a list-box */ I18N::translate('list'),
312b6f85d51SGreg Roach            self::LAYOUT_STYLE_TABLE => /* I18N: An option in a list-box */ I18N::translate('table'),
313b6f85d51SGreg Roach        ];
314b6f85d51SGreg Roach    }
315b6f85d51SGreg Roach
316b6f85d51SGreg Roach    /**
317b6f85d51SGreg Roach     * @return array<string,string>
318b6f85d51SGreg Roach     */
319b6f85d51SGreg Roach    private function sortStyles(): array
320b6f85d51SGreg Roach    {
321b6f85d51SGreg Roach        return [
322b6f85d51SGreg Roach            self::SORT_STYLE_NAME => /* I18N: An option in a list-box */ I18N::translate('sort by name'),
323b6f85d51SGreg Roach            self::SORT_STYLE_DATE => /* I18N: An option in a list-box */ I18N::translate('sort by date'),
324b6f85d51SGreg Roach        ];
325b6f85d51SGreg Roach    }
3268c2e8227SGreg Roach}
327