xref: /webtrees/app/Module/OnThisDayModule.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 OnThisDayModule
338c2e8227SGreg Roach */
3437eb8894SGreg Roachclass OnThisDayModule extends AbstractModule implements ModuleBlockInterface
35c1010edaSGreg Roach{
3649a243cbSGreg Roach    use ModuleBlockTrait;
3749a243cbSGreg Roach
3812664b30SGreg Roach    // All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN
3916d6367aSGreg Roach    private 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
7216d6367aSGreg Roach    private const DEFAULT_EVENTS = [
7312664b30SGreg Roach        'BIRT',
7412664b30SGreg Roach        'MARR',
7512664b30SGreg Roach        'DEAT',
7612664b30SGreg Roach    ];
7712664b30SGreg Roach
7812664b30SGreg Roach    /**
790cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
8012664b30SGreg Roach     *
8112664b30SGreg Roach     * @return string
8212664b30SGreg Roach     */
8349a243cbSGreg Roach    public function title(): string
84c1010edaSGreg Roach    {
85bbb76c12SGreg Roach        /* I18N: Name of a module */
86bbb76c12SGreg Roach        return I18N::translate('On this day');
878c2e8227SGreg Roach    }
888c2e8227SGreg Roach
8912664b30SGreg Roach    /**
9012664b30SGreg Roach     * A sentence describing what this module does.
9112664b30SGreg Roach     *
9212664b30SGreg Roach     * @return string
9312664b30SGreg Roach     */
9449a243cbSGreg Roach    public function description(): string
95c1010edaSGreg Roach    {
96bbb76c12SGreg Roach        /* I18N: Description of the “On this day” module */
97bbb76c12SGreg Roach        return I18N::translate('A list of the anniversaries that occur today.');
988c2e8227SGreg Roach    }
998c2e8227SGreg Roach
10076692c8bSGreg Roach    /**
10176692c8bSGreg Roach     * Generate the HTML content of this block.
10276692c8bSGreg Roach     *
103e490cd80SGreg Roach     * @param Tree     $tree
10476692c8bSGreg Roach     * @param int      $block_id
1053caaa4d2SGreg Roach     * @param string   $context
1063caaa4d2SGreg Roach     * @param string[] $config
10776692c8bSGreg Roach     *
10876692c8bSGreg Roach     * @return string
10976692c8bSGreg Roach     */
1103caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
111c1010edaSGreg Roach    {
112f0a11419SGreg Roach        $calendar_service = new CalendarService();
113f0a11419SGreg Roach
11412664b30SGreg Roach        $default_events = implode(',', self::DEFAULT_EVENTS);
11512664b30SGreg Roach
11612664b30SGreg Roach        $filter    = (bool) $this->getBlockSetting($block_id, 'filter', '1');
117e2a378d3SGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
118e2a378d3SGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
11912664b30SGreg Roach        $events    = $this->getBlockSetting($block_id, 'events', $default_events);
1208c2e8227SGreg Roach
1213caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
1228c2e8227SGreg Roach
12312664b30SGreg Roach        $event_array = explode(',', $events);
12412664b30SGreg Roach
12512664b30SGreg Roach        // If we are only showing living individuals, then we don't need to search for DEAT events.
12612664b30SGreg Roach        if ($filter) {
1278d0ebef0SGreg Roach            $event_array  = array_diff($event_array, Gedcom::DEATH_EVENTS);
12812664b30SGreg Roach        }
12912664b30SGreg Roach
13012664b30SGreg Roach        $events_filter = implode('|', $event_array);
13112664b30SGreg Roach
1324459dc9aSGreg Roach        $startjd = Carbon::now()->julianDay();
133269fd10dSGreg Roach        $endjd   = $startjd;
13412664b30SGreg Roach
135f0a11419SGreg Roach        $facts = $calendar_service->getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $tree);
13612664b30SGreg Roach
137*b2448a1bSGreg Roach        if ($facts === []) {
138147e99aaSGreg Roach            $content = view('modules/todays_events/empty');
139147e99aaSGreg Roach        } elseif ($infoStyle === 'list') {
14048f08416SGreg Roach            $content = view('lists/anniversaries-list', [
141147e99aaSGreg Roach                'facts' => $facts,
1420280f44aSGreg Roach            ]);
143d8189b6aSDavid Drury        } else {
14448f08416SGreg Roach            $content = view('lists/anniversaries-table', [
145d8189b6aSDavid Drury                'facts' => $facts,
1460280f44aSGreg Roach            ]);
1470280f44aSGreg Roach        }
1488c2e8227SGreg Roach
1493caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
150147e99aaSGreg Roach            return view('modules/block-template', [
1511e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1529c6524dcSGreg Roach                'id'         => $block_id,
1533caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
15449a243cbSGreg Roach                'title'      => $this->title(),
1559c6524dcSGreg Roach                'content'    => $content,
15612664b30SGreg Roach            ]);
1578c2e8227SGreg Roach        }
158b2ce94c6SRico Sonntag
159b2ce94c6SRico Sonntag        return $content;
1608c2e8227SGreg Roach    }
1618c2e8227SGreg Roach
1623caaa4d2SGreg Roach    /**
1633caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1643caaa4d2SGreg Roach     *
1653caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1663caaa4d2SGreg Roach     *
1673caaa4d2SGreg Roach     * @return bool
1683caaa4d2SGreg Roach     */
169c1010edaSGreg Roach    public function loadAjax(): bool
170c1010edaSGreg Roach    {
1718c2e8227SGreg Roach        return true;
1728c2e8227SGreg Roach    }
1738c2e8227SGreg Roach
1743caaa4d2SGreg Roach    /**
1753caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
1763caaa4d2SGreg Roach     *
1773caaa4d2SGreg Roach     * @return bool
1783caaa4d2SGreg Roach     */
179c1010edaSGreg Roach    public function isUserBlock(): bool
180c1010edaSGreg Roach    {
1818c2e8227SGreg Roach        return true;
1828c2e8227SGreg Roach    }
1838c2e8227SGreg Roach
1843caaa4d2SGreg Roach    /**
1853caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
1863caaa4d2SGreg Roach     *
1873caaa4d2SGreg Roach     * @return bool
1883caaa4d2SGreg Roach     */
18963276d8fSGreg Roach    public function isTreeBlock(): bool
190c1010edaSGreg Roach    {
1918c2e8227SGreg Roach        return true;
1928c2e8227SGreg Roach    }
1938c2e8227SGreg Roach
19476692c8bSGreg Roach    /**
195a45f9889SGreg Roach     * Update the configuration for a block.
196a45f9889SGreg Roach     *
1976ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
198a45f9889SGreg Roach     * @param int     $block_id
199a45f9889SGreg Roach     *
200a45f9889SGreg Roach     * @return void
201a45f9889SGreg Roach     */
2026ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
203a45f9889SGreg Roach    {
204e106ff43SGreg Roach        $params = $request->getParsedBody();
205e106ff43SGreg Roach
206e106ff43SGreg Roach        $this->setBlockSetting($block_id, 'filter', $params['filter']);
207e106ff43SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']);
208e106ff43SGreg Roach        $this->setBlockSetting($block_id, 'sortStyle', $params['sortStyle']);
209e106ff43SGreg Roach        $this->setBlockSetting($block_id, 'events', implode(',', $params['events'] ?? []));
210a45f9889SGreg Roach    }
211a45f9889SGreg Roach
212a45f9889SGreg Roach    /**
21376692c8bSGreg Roach     * An HTML form to edit block settings
21476692c8bSGreg Roach     *
215e490cd80SGreg Roach     * @param Tree $tree
21676692c8bSGreg Roach     * @param int  $block_id
217a9430be8SGreg Roach     *
2183caaa4d2SGreg Roach     * @return string
21976692c8bSGreg Roach     */
2203caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
221c1010edaSGreg Roach    {
22212664b30SGreg Roach        $default_events = implode(',', self::DEFAULT_EVENTS);
22312664b30SGreg Roach
224e2a378d3SGreg Roach        $filter    = $this->getBlockSetting($block_id, 'filter', '1');
225e2a378d3SGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
226e2a378d3SGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
22712664b30SGreg Roach        $events    = $this->getBlockSetting($block_id, 'events', $default_events);
2288c2e8227SGreg Roach
22912664b30SGreg Roach        $event_array = explode(',', $events);
23012664b30SGreg Roach
23112664b30SGreg Roach        $all_events = [];
23212664b30SGreg Roach        foreach (self::ALL_EVENTS as $event) {
23312664b30SGreg Roach            $all_events[$event] = GedcomTag::getLabel($event);
23412664b30SGreg Roach        }
23512664b30SGreg Roach
236c385536dSGreg Roach        $info_styles = [
237bbb76c12SGreg Roach            /* I18N: An option in a list-box */
238bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
239bbb76c12SGreg Roach            /* I18N: An option in a list-box */
240bbb76c12SGreg Roach            'table' => I18N::translate('table'),
241c385536dSGreg Roach        ];
2428c2e8227SGreg Roach
243c385536dSGreg Roach        $sort_styles = [
244bbb76c12SGreg Roach            /* I18N: An option in a list-box */
245bbb76c12SGreg Roach            'alpha' => I18N::translate('sort by name'),
246bbb76c12SGreg Roach            /* I18N: An option in a list-box */
247bbb76c12SGreg Roach            'anniv' => I18N::translate('sort by date'),
248c385536dSGreg Roach        ];
249d8189b6aSDavid Drury
2503caaa4d2SGreg Roach        return view('modules/todays_events/config', [
251c385536dSGreg Roach            'all_events'  => $all_events,
252c385536dSGreg Roach            'event_array' => $event_array,
253c385536dSGreg Roach            'filter'      => $filter,
254c385536dSGreg Roach            'infoStyle'   => $infoStyle,
255c385536dSGreg Roach            'info_styles' => $info_styles,
256c385536dSGreg Roach            'sortStyle'   => $sortStyle,
257c385536dSGreg Roach            'sort_styles' => $sort_styles,
258c385536dSGreg Roach        ]);
2598c2e8227SGreg Roach    }
2608c2e8227SGreg Roach}
261