xref: /webtrees/app/Module/UpcomingAnniversariesModule.php (revision b2448a1b0445a9bac7b5890fc1e38849872d3753)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 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
158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
224459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
238d0ebef0SGreg Roachuse Fisharebest\Webtrees\Gedcom;
2412664b30SGreg Roachuse Fisharebest\Webtrees\GedcomTag;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
26f0a11419SGreg Roachuse Fisharebest\Webtrees\Services\CalendarService;
27e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
281e7a7a28SGreg Roachuse Illuminate\Support\Str;
296ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
308c2e8227SGreg Roach
318c2e8227SGreg Roach/**
328c2e8227SGreg Roach * Class UpcomingAnniversariesModule
338c2e8227SGreg Roach */
3437eb8894SGreg Roachclass UpcomingAnniversariesModule extends AbstractModule implements ModuleBlockInterface
35c1010edaSGreg Roach{
3649a243cbSGreg Roach    use ModuleBlockTrait;
3749a243cbSGreg Roach
38c385536dSGreg Roach    // Default values for new blocks.
3916d6367aSGreg Roach    private const DEFAULT_DAYS   = '7';
4016d6367aSGreg Roach    private const DEFAULT_FILTER = '1';
4116d6367aSGreg Roach    private const DEFAULT_SORT   = 'alpha';
4216d6367aSGreg Roach    private const DEFAULT_STYLE  = 'table';
43c385536dSGreg Roach
44c385536dSGreg Roach    // Can show this number of days into the future.
4516d6367aSGreg Roach    private const MIN_DAYS = 1;
4616d6367aSGreg Roach    private const MAX_DAYS = 30;
47c385536dSGreg Roach
4812664b30SGreg Roach    // All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN
4916d6367aSGreg Roach    private const ALL_EVENTS = [
5012664b30SGreg Roach        'ADOP',
5112664b30SGreg Roach        'ANUL',
5212664b30SGreg Roach        'BAPM',
5312664b30SGreg Roach        'BARM',
5412664b30SGreg Roach        'BASM',
5512664b30SGreg Roach        'BIRT',
5612664b30SGreg Roach        'BLES',
5712664b30SGreg Roach        'BURI',
5812664b30SGreg Roach        'CHR',
5912664b30SGreg Roach        'CHRA',
6012664b30SGreg Roach        'CONF',
6112664b30SGreg Roach        'CREM',
6212664b30SGreg Roach        'DEAT',
6312664b30SGreg Roach        'DIV',
6412664b30SGreg Roach        'DIVF',
6512664b30SGreg Roach        'EMIG',
6612664b30SGreg Roach        'ENGA',
6712664b30SGreg Roach        'FCOM',
6812664b30SGreg Roach        'GRAD',
6912664b30SGreg Roach        'IMMI',
7012664b30SGreg Roach        'MARB',
7112664b30SGreg Roach        'MARC',
7212664b30SGreg Roach        'MARL',
7312664b30SGreg Roach        'MARR',
7412664b30SGreg Roach        'MARS',
7512664b30SGreg Roach        'NATU',
7612664b30SGreg Roach        'ORDN',
7712664b30SGreg Roach        'PROB',
7812664b30SGreg Roach        'RETI',
7912664b30SGreg Roach        'WILL',
8012664b30SGreg Roach    ];
8112664b30SGreg Roach
8216d6367aSGreg Roach    private const DEFAULT_EVENTS = [
8312664b30SGreg Roach        'BIRT',
8412664b30SGreg Roach        'MARR',
8512664b30SGreg Roach        'DEAT',
8612664b30SGreg Roach    ];
8712664b30SGreg Roach
8876692c8bSGreg Roach    /**
890cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
9076692c8bSGreg Roach     *
9176692c8bSGreg Roach     * @return string
9276692c8bSGreg Roach     */
9349a243cbSGreg Roach    public function title(): string
94c1010edaSGreg Roach    {
95bbb76c12SGreg Roach        /* I18N: Name of a module */
96bbb76c12SGreg Roach        return I18N::translate('Upcoming events');
978c2e8227SGreg Roach    }
988c2e8227SGreg Roach
9976692c8bSGreg Roach    /**
10076692c8bSGreg Roach     * A sentence describing what this module does.
10176692c8bSGreg Roach     *
10276692c8bSGreg Roach     * @return string
10376692c8bSGreg Roach     */
10449a243cbSGreg Roach    public function description(): string
105c1010edaSGreg Roach    {
106bbb76c12SGreg Roach        /* I18N: Description of the “Upcoming events” module */
107bbb76c12SGreg Roach        return I18N::translate('A list of the anniversaries that will occur in the near future.');
1088c2e8227SGreg Roach    }
1098c2e8227SGreg Roach
11076692c8bSGreg Roach    /**
11176692c8bSGreg Roach     * Generate the HTML content of this block.
11276692c8bSGreg Roach     *
113e490cd80SGreg Roach     * @param Tree     $tree
11476692c8bSGreg Roach     * @param int      $block_id
1153caaa4d2SGreg Roach     * @param string   $context
1163caaa4d2SGreg Roach     * @param string[] $config
11776692c8bSGreg Roach     *
11876692c8bSGreg Roach     * @return string
11976692c8bSGreg Roach     */
1203caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
121c1010edaSGreg Roach    {
122f0a11419SGreg Roach        $calendar_service = new CalendarService();
123f0a11419SGreg Roach
12412664b30SGreg Roach        $default_events = implode(',', self::DEFAULT_EVENTS);
12512664b30SGreg Roach
12676f666f4SGreg Roach        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
127c385536dSGreg Roach        $filter    = (bool) $this->getBlockSetting($block_id, 'filter', self::DEFAULT_FILTER);
128c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
129c385536dSGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT);
13012664b30SGreg Roach        $events    = $this->getBlockSetting($block_id, 'events', $default_events);
1318c2e8227SGreg Roach
1323caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
1338c2e8227SGreg Roach
13412664b30SGreg Roach        $event_array = explode(',', $events);
13512664b30SGreg Roach
13612664b30SGreg Roach        // If we are only showing living individuals, then we don't need to search for DEAT events.
13712664b30SGreg Roach        if ($filter) {
1388d0ebef0SGreg Roach            $event_array  = array_diff($event_array, Gedcom::DEATH_EVENTS);
13912664b30SGreg Roach        }
14012664b30SGreg Roach
14112664b30SGreg Roach        $events_filter = implode('|', $event_array);
14212664b30SGreg Roach
1434459dc9aSGreg Roach        $startjd = Carbon::now()->julianDay() + 1;
1444459dc9aSGreg Roach        $endjd   = Carbon::now()->julianDay() + $days;
14512664b30SGreg Roach
146f0a11419SGreg Roach        $facts = $calendar_service->getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $tree);
14712664b30SGreg Roach
148*b2448a1bSGreg Roach        if ($facts === []) {
149d8189b6aSDavid Drury            if ($endjd == $startjd) {
150147e99aaSGreg Roach                $content = view('modules/upcoming_events/empty', [
151c1010edaSGreg Roach                    'message' => I18N::translate('No events exist for tomorrow.'),
1520280f44aSGreg Roach                ]);
153d8189b6aSDavid Drury            } else {
154bbb76c12SGreg Roach                /* I18N: translation for %s==1 is unused; it is translated separately as “tomorrow” */                $content = view('modules/upcoming_events/empty', [
155bbb76c12SGreg 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)),
1560280f44aSGreg Roach                ]);
1578c2e8227SGreg Roach            }
158147e99aaSGreg Roach        } elseif ($infoStyle === 'list') {
15948f08416SGreg Roach            $content = view('lists/anniversaries-list', [
160d8189b6aSDavid Drury                'facts' => $facts,
161147e99aaSGreg Roach            ]);
162147e99aaSGreg Roach        } else {
16348f08416SGreg Roach            $content = view('lists/anniversaries-table', [
164147e99aaSGreg Roach                'facts' => $facts,
165147e99aaSGreg Roach            ]);
1660280f44aSGreg Roach        }
1678c2e8227SGreg Roach
1683caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
169147e99aaSGreg Roach            return view('modules/block-template', [
1701e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1719c6524dcSGreg Roach                'id'         => $block_id,
1723caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
17349a243cbSGreg Roach                'title'      => $this->title(),
1749c6524dcSGreg Roach                'content'    => $content,
1759c6524dcSGreg Roach            ]);
1768c2e8227SGreg Roach        }
177b2ce94c6SRico Sonntag
178b2ce94c6SRico Sonntag        return $content;
1798c2e8227SGreg Roach    }
1808c2e8227SGreg Roach
1813caaa4d2SGreg Roach    /**
1823caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1833caaa4d2SGreg Roach     *
1843caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1853caaa4d2SGreg Roach     *
1863caaa4d2SGreg Roach     * @return bool
1873caaa4d2SGreg Roach     */
188c1010edaSGreg Roach    public function loadAjax(): bool
189c1010edaSGreg Roach    {
1908c2e8227SGreg Roach        return true;
1918c2e8227SGreg Roach    }
1928c2e8227SGreg Roach
1933caaa4d2SGreg Roach    /**
1943caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
1953caaa4d2SGreg Roach     *
1963caaa4d2SGreg Roach     * @return bool
1973caaa4d2SGreg Roach     */
198c1010edaSGreg Roach    public function isUserBlock(): bool
199c1010edaSGreg Roach    {
2008c2e8227SGreg Roach        return true;
2018c2e8227SGreg Roach    }
2028c2e8227SGreg Roach
2033caaa4d2SGreg Roach    /**
2043caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
2053caaa4d2SGreg Roach     *
2063caaa4d2SGreg Roach     * @return bool
2073caaa4d2SGreg Roach     */
20863276d8fSGreg Roach    public function isTreeBlock(): bool
209c1010edaSGreg Roach    {
2108c2e8227SGreg Roach        return true;
2118c2e8227SGreg Roach    }
2128c2e8227SGreg Roach
21376692c8bSGreg Roach    /**
214a45f9889SGreg Roach     * Update the configuration for a block.
215a45f9889SGreg Roach     *
2166ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
217a45f9889SGreg Roach     * @param int     $block_id
218a45f9889SGreg Roach     *
219a45f9889SGreg Roach     * @return void
220a45f9889SGreg Roach     */
2216ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
222a45f9889SGreg Roach    {
223c50a90beSGreg Roach        $params = $request->getParsedBody();
224c50a90beSGreg Roach
225c50a90beSGreg Roach        $this->setBlockSetting($block_id, 'days', $params['days']);
226c50a90beSGreg Roach        $this->setBlockSetting($block_id, 'filter', $params['filter']);
227c50a90beSGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']);
228c50a90beSGreg Roach        $this->setBlockSetting($block_id, 'sortStyle', $params['sortStyle']);
229c50a90beSGreg Roach        $this->setBlockSetting($block_id, 'events', implode(',', $params['events'] ?? []));
230a45f9889SGreg Roach    }
231a45f9889SGreg Roach
232a45f9889SGreg Roach    /**
23376692c8bSGreg Roach     * An HTML form to edit block settings
23476692c8bSGreg Roach     *
235e490cd80SGreg Roach     * @param Tree $tree
23676692c8bSGreg Roach     * @param int  $block_id
237a9430be8SGreg Roach     *
2383caaa4d2SGreg Roach     * @return string
23976692c8bSGreg Roach     */
2403caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
241c1010edaSGreg Roach    {
24212664b30SGreg Roach        $default_events = implode(',', self::DEFAULT_EVENTS);
24312664b30SGreg Roach
24476f666f4SGreg Roach        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
245c385536dSGreg Roach        $filter    = $this->getBlockSetting($block_id, 'filter', self::DEFAULT_FILTER);
246c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
247c385536dSGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', self::DEFAULT_SORT);
24812664b30SGreg Roach        $events    = $this->getBlockSetting($block_id, 'events', $default_events);
2498c2e8227SGreg Roach
25012664b30SGreg Roach        $event_array = explode(',', $events);
25112664b30SGreg Roach
25212664b30SGreg Roach        $all_events = [];
25312664b30SGreg Roach        foreach (self::ALL_EVENTS as $event) {
25412664b30SGreg Roach            $all_events[$event] = GedcomTag::getLabel($event);
25512664b30SGreg Roach        }
25612664b30SGreg Roach
257c385536dSGreg Roach        $info_styles = [
258bbb76c12SGreg Roach            /* I18N: An option in a list-box */
259bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
260bbb76c12SGreg Roach            /* I18N: An option in a list-box */
261bbb76c12SGreg Roach            'table' => I18N::translate('table'),
262c385536dSGreg Roach        ];
2638c2e8227SGreg Roach
264c385536dSGreg Roach        $sort_styles = [
265bbb76c12SGreg Roach            /* I18N: An option in a list-box */
266bbb76c12SGreg Roach            'alpha' => I18N::translate('sort by name'),
267bbb76c12SGreg Roach            /* I18N: An option in a list-box */
268bbb76c12SGreg Roach            'anniv' => I18N::translate('sort by date'),
269c385536dSGreg Roach        ];
2708c2e8227SGreg Roach
2713caaa4d2SGreg Roach        return view('modules/upcoming_events/config', [
272c385536dSGreg Roach            'all_events'  => $all_events,
273c385536dSGreg Roach            'days'        => $days,
274c385536dSGreg Roach            'event_array' => $event_array,
275c385536dSGreg Roach            'filter'      => $filter,
276c385536dSGreg Roach            'infoStyle'   => $infoStyle,
277c385536dSGreg Roach            'info_styles' => $info_styles,
278c385536dSGreg Roach            'max_days'    => self::MAX_DAYS,
279c385536dSGreg Roach            'sortStyle'   => $sortStyle,
280c385536dSGreg Roach            'sort_styles' => $sort_styles,
281c385536dSGreg Roach        ]);
2828c2e8227SGreg Roach    }
2838c2e8227SGreg Roach}
284