xref: /webtrees/app/Module/OnThisDayModule.php (revision 3caaa4d28e5aa6cc52c831b60066ebdc346fcd50)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 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 */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
204459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
218d0ebef0SGreg Roachuse Fisharebest\Webtrees\Gedcom;
2212664b30SGreg Roachuse Fisharebest\Webtrees\GedcomTag;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
24f0a11419SGreg Roachuse Fisharebest\Webtrees\Services\CalendarService;
25e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
261e7a7a28SGreg Roachuse Illuminate\Support\Str;
276ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
288c2e8227SGreg Roach
298c2e8227SGreg Roach/**
308c2e8227SGreg Roach * Class OnThisDayModule
318c2e8227SGreg Roach */
3237eb8894SGreg Roachclass OnThisDayModule extends AbstractModule implements ModuleBlockInterface
33c1010edaSGreg Roach{
3449a243cbSGreg Roach    use ModuleBlockTrait;
3549a243cbSGreg Roach
3612664b30SGreg Roach    // All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN
3716d6367aSGreg Roach    private const ALL_EVENTS = [
3812664b30SGreg Roach        'ADOP',
3912664b30SGreg Roach        'ANUL',
4012664b30SGreg Roach        'BAPM',
4112664b30SGreg Roach        'BARM',
4212664b30SGreg Roach        'BASM',
4312664b30SGreg Roach        'BIRT',
4412664b30SGreg Roach        'BLES',
4512664b30SGreg Roach        'BURI',
4612664b30SGreg Roach        'CHR',
4712664b30SGreg Roach        'CHRA',
4812664b30SGreg Roach        'CONF',
4912664b30SGreg Roach        'CREM',
5012664b30SGreg Roach        'DEAT',
5112664b30SGreg Roach        'DIV',
5212664b30SGreg Roach        'DIVF',
5312664b30SGreg Roach        'EMIG',
5412664b30SGreg Roach        'ENGA',
5512664b30SGreg Roach        'FCOM',
5612664b30SGreg Roach        'GRAD',
5712664b30SGreg Roach        'IMMI',
5812664b30SGreg Roach        'MARB',
5912664b30SGreg Roach        'MARC',
6012664b30SGreg Roach        'MARL',
6112664b30SGreg Roach        'MARR',
6212664b30SGreg Roach        'MARS',
6312664b30SGreg Roach        'NATU',
6412664b30SGreg Roach        'ORDN',
6512664b30SGreg Roach        'PROB',
6612664b30SGreg Roach        'RETI',
6712664b30SGreg Roach        'WILL',
6812664b30SGreg Roach    ];
6912664b30SGreg Roach
7016d6367aSGreg Roach    private const DEFAULT_EVENTS = [
7112664b30SGreg Roach        'BIRT',
7212664b30SGreg Roach        'MARR',
7312664b30SGreg Roach        'DEAT',
7412664b30SGreg Roach    ];
7512664b30SGreg Roach
7612664b30SGreg Roach    /**
770cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
7812664b30SGreg Roach     *
7912664b30SGreg Roach     * @return string
8012664b30SGreg Roach     */
8149a243cbSGreg Roach    public function title(): string
82c1010edaSGreg Roach    {
83bbb76c12SGreg Roach        /* I18N: Name of a module */
84bbb76c12SGreg Roach        return I18N::translate('On this day');
858c2e8227SGreg Roach    }
868c2e8227SGreg Roach
8712664b30SGreg Roach    /**
8812664b30SGreg Roach     * A sentence describing what this module does.
8912664b30SGreg Roach     *
9012664b30SGreg Roach     * @return string
9112664b30SGreg Roach     */
9249a243cbSGreg Roach    public function description(): string
93c1010edaSGreg Roach    {
94bbb76c12SGreg Roach        /* I18N: Description of the “On this day” module */
95bbb76c12SGreg Roach        return I18N::translate('A list of the anniversaries that occur today.');
968c2e8227SGreg Roach    }
978c2e8227SGreg Roach
9876692c8bSGreg Roach    /**
9976692c8bSGreg Roach     * Generate the HTML content of this block.
10076692c8bSGreg Roach     *
101e490cd80SGreg Roach     * @param Tree     $tree
10276692c8bSGreg Roach     * @param int      $block_id
103*3caaa4d2SGreg Roach     * @param string   $context
104*3caaa4d2SGreg Roach     * @param string[] $config
10576692c8bSGreg Roach     *
10676692c8bSGreg Roach     * @return string
10776692c8bSGreg Roach     */
108*3caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
109c1010edaSGreg Roach    {
110f0a11419SGreg Roach        $calendar_service = new CalendarService();
111f0a11419SGreg Roach
11212664b30SGreg Roach        $default_events = implode(',', self::DEFAULT_EVENTS);
11312664b30SGreg Roach
11412664b30SGreg Roach        $filter    = (bool) $this->getBlockSetting($block_id, 'filter', '1');
115e2a378d3SGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
116e2a378d3SGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
11712664b30SGreg Roach        $events    = $this->getBlockSetting($block_id, 'events', $default_events);
1188c2e8227SGreg Roach
119*3caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
1208c2e8227SGreg Roach
12112664b30SGreg Roach        $event_array = explode(',', $events);
12212664b30SGreg Roach
12312664b30SGreg Roach        // If we are only showing living individuals, then we don't need to search for DEAT events.
12412664b30SGreg Roach        if ($filter) {
1258d0ebef0SGreg Roach            $event_array  = array_diff($event_array, Gedcom::DEATH_EVENTS);
12612664b30SGreg Roach        }
12712664b30SGreg Roach
12812664b30SGreg Roach        $events_filter = implode('|', $event_array);
12912664b30SGreg Roach
1304459dc9aSGreg Roach        $startjd = Carbon::now()->julianDay();
131269fd10dSGreg Roach        $endjd   = $startjd;
13212664b30SGreg Roach
133f0a11419SGreg Roach        $facts = $calendar_service->getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $tree);
13412664b30SGreg Roach
13512664b30SGreg Roach        if (empty($facts)) {
136147e99aaSGreg Roach            $content = view('modules/todays_events/empty');
137147e99aaSGreg Roach        } elseif ($infoStyle === 'list') {
13848f08416SGreg Roach            $content = view('lists/anniversaries-list', [
139147e99aaSGreg Roach                'facts' => $facts,
1400280f44aSGreg Roach            ]);
141d8189b6aSDavid Drury        } else {
14248f08416SGreg Roach            $content = view('lists/anniversaries-table', [
143d8189b6aSDavid Drury                'facts' => $facts,
1440280f44aSGreg Roach            ]);
1450280f44aSGreg Roach        }
1468c2e8227SGreg Roach
147*3caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
148147e99aaSGreg Roach            return view('modules/block-template', [
1491e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1509c6524dcSGreg Roach                'id'         => $block_id,
151*3caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
15249a243cbSGreg Roach                'title'      => $this->title(),
1539c6524dcSGreg Roach                'content'    => $content,
15412664b30SGreg Roach            ]);
1558c2e8227SGreg Roach        }
156b2ce94c6SRico Sonntag
157b2ce94c6SRico Sonntag        return $content;
1588c2e8227SGreg Roach    }
1598c2e8227SGreg Roach
160*3caaa4d2SGreg Roach    /**
161*3caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
162*3caaa4d2SGreg Roach     *
163*3caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
164*3caaa4d2SGreg Roach     *
165*3caaa4d2SGreg Roach     * @return bool
166*3caaa4d2SGreg Roach     */
167c1010edaSGreg Roach    public function loadAjax(): bool
168c1010edaSGreg Roach    {
1698c2e8227SGreg Roach        return true;
1708c2e8227SGreg Roach    }
1718c2e8227SGreg Roach
172*3caaa4d2SGreg Roach    /**
173*3caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
174*3caaa4d2SGreg Roach     *
175*3caaa4d2SGreg Roach     * @return bool
176*3caaa4d2SGreg Roach     */
177c1010edaSGreg Roach    public function isUserBlock(): bool
178c1010edaSGreg Roach    {
1798c2e8227SGreg Roach        return true;
1808c2e8227SGreg Roach    }
1818c2e8227SGreg Roach
182*3caaa4d2SGreg Roach    /**
183*3caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
184*3caaa4d2SGreg Roach     *
185*3caaa4d2SGreg Roach     * @return bool
186*3caaa4d2SGreg Roach     */
18763276d8fSGreg Roach    public function isTreeBlock(): bool
188c1010edaSGreg Roach    {
1898c2e8227SGreg Roach        return true;
1908c2e8227SGreg Roach    }
1918c2e8227SGreg Roach
19276692c8bSGreg Roach    /**
193a45f9889SGreg Roach     * Update the configuration for a block.
194a45f9889SGreg Roach     *
1956ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
196a45f9889SGreg Roach     * @param int     $block_id
197a45f9889SGreg Roach     *
198a45f9889SGreg Roach     * @return void
199a45f9889SGreg Roach     */
2006ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
201a45f9889SGreg Roach    {
202e106ff43SGreg Roach        $params = $request->getParsedBody();
203e106ff43SGreg Roach
204e106ff43SGreg Roach        $this->setBlockSetting($block_id, 'filter', $params['filter']);
205e106ff43SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle']);
206e106ff43SGreg Roach        $this->setBlockSetting($block_id, 'sortStyle', $params['sortStyle']);
207e106ff43SGreg Roach        $this->setBlockSetting($block_id, 'events', implode(',', $params['events'] ?? []));
208a45f9889SGreg Roach    }
209a45f9889SGreg Roach
210a45f9889SGreg Roach    /**
21176692c8bSGreg Roach     * An HTML form to edit block settings
21276692c8bSGreg Roach     *
213e490cd80SGreg Roach     * @param Tree $tree
21476692c8bSGreg Roach     * @param int  $block_id
215a9430be8SGreg Roach     *
216*3caaa4d2SGreg Roach     * @return string
21776692c8bSGreg Roach     */
218*3caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
219c1010edaSGreg Roach    {
22012664b30SGreg Roach        $default_events = implode(',', self::DEFAULT_EVENTS);
22112664b30SGreg Roach
222e2a378d3SGreg Roach        $filter    = $this->getBlockSetting($block_id, 'filter', '1');
223e2a378d3SGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
224e2a378d3SGreg Roach        $sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
22512664b30SGreg Roach        $events    = $this->getBlockSetting($block_id, 'events', $default_events);
2268c2e8227SGreg Roach
22712664b30SGreg Roach        $event_array = explode(',', $events);
22812664b30SGreg Roach
22912664b30SGreg Roach        $all_events = [];
23012664b30SGreg Roach        foreach (self::ALL_EVENTS as $event) {
23112664b30SGreg Roach            $all_events[$event] = GedcomTag::getLabel($event);
23212664b30SGreg Roach        }
23312664b30SGreg Roach
234c385536dSGreg Roach        $info_styles = [
235bbb76c12SGreg Roach            /* I18N: An option in a list-box */
236bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
237bbb76c12SGreg Roach            /* I18N: An option in a list-box */
238bbb76c12SGreg Roach            'table' => I18N::translate('table'),
239c385536dSGreg Roach        ];
2408c2e8227SGreg Roach
241c385536dSGreg Roach        $sort_styles = [
242bbb76c12SGreg Roach            /* I18N: An option in a list-box */
243bbb76c12SGreg Roach            'alpha' => I18N::translate('sort by name'),
244bbb76c12SGreg Roach            /* I18N: An option in a list-box */
245bbb76c12SGreg Roach            'anniv' => I18N::translate('sort by date'),
246c385536dSGreg Roach        ];
247d8189b6aSDavid Drury
248*3caaa4d2SGreg Roach        return view('modules/todays_events/config', [
249c385536dSGreg Roach            'all_events'  => $all_events,
250c385536dSGreg Roach            'event_array' => $event_array,
251c385536dSGreg Roach            'filter'      => $filter,
252c385536dSGreg Roach            'infoStyle'   => $infoStyle,
253c385536dSGreg Roach            'info_styles' => $info_styles,
254c385536dSGreg Roach            'sortStyle'   => $sortStyle,
255c385536dSGreg Roach            'sort_styles' => $sort_styles,
256c385536dSGreg Roach        ]);
2578c2e8227SGreg Roach    }
2588c2e8227SGreg Roach}
259