xref: /webtrees/app/Module/YahrzeitModule.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;
218c2e8227SGreg Roach
228c2e8227SGreg Roachuse Fisharebest\ExtCalendar\JewishCalendar;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JewishDate;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
27d97083feSGreg Roachuse Fisharebest\Webtrees\Registry;
28f0a11419SGreg Roachuse Fisharebest\Webtrees\Services\CalendarService;
29e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
30748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator;
31e24053e5SGreg Roachuse Illuminate\Support\Collection;
321e7a7a28SGreg Roachuse Illuminate\Support\Str;
336ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
348c2e8227SGreg Roach
350195b0ddSGreg Roachuse function extract;
360195b0ddSGreg Roachuse function view;
370195b0ddSGreg Roach
380195b0ddSGreg Roachuse const EXTR_OVERWRITE;
390195b0ddSGreg Roach
408c2e8227SGreg Roach/**
418c2e8227SGreg Roach * Class YahrzeitModule
428c2e8227SGreg Roach */
4337eb8894SGreg Roachclass YahrzeitModule extends AbstractModule implements ModuleBlockInterface
44c1010edaSGreg Roach{
4549a243cbSGreg Roach    use ModuleBlockTrait;
4649a243cbSGreg Roach
47c385536dSGreg Roach    // Default values for new blocks.
4816d6367aSGreg Roach    private const DEFAULT_CALENDAR = 'jewish';
4916d6367aSGreg Roach    private const DEFAULT_DAYS     = '7';
5016d6367aSGreg Roach    private const DEFAULT_STYLE    = 'table';
51c385536dSGreg Roach
52c385536dSGreg Roach    // Can show this number of days into the future.
5316d6367aSGreg Roach    private const MAX_DAYS = 30;
54c385536dSGreg Roach
5501221f27SGreg Roach    // Pagination
5601221f27SGreg Roach    private const LIMIT_LOW  = 10;
5701221f27SGreg Roach    private const LIMIT_HIGH = 20;
5801221f27SGreg Roach
59961ec755SGreg Roach    /**
600cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
61961ec755SGreg Roach     *
62961ec755SGreg Roach     * @return string
63961ec755SGreg Roach     */
6449a243cbSGreg Roach    public function title(): string
65c1010edaSGreg Roach    {
66bbb76c12SGreg Roach        /* I18N: Name of a module. Yahrzeiten (the plural of Yahrzeit) are special anniversaries of deaths in the Hebrew faith/calendar. */
67bbb76c12SGreg Roach        return I18N::translate('Yahrzeiten');
688c2e8227SGreg Roach    }
698c2e8227SGreg Roach
7049a243cbSGreg Roach    public function description(): string
71c1010edaSGreg Roach    {
72bbb76c12SGreg Roach        /* I18N: Description of the “Yahrzeiten” module. A “Hebrew death” is a death where the date is recorded in the Hebrew calendar. */
73bbb76c12SGreg Roach        return I18N::translate('A list of the Hebrew death anniversaries that will occur in the near future.');
748c2e8227SGreg Roach    }
758c2e8227SGreg Roach
7676692c8bSGreg Roach    /**
7776692c8bSGreg Roach     * Generate the HTML content of this block.
7876692c8bSGreg Roach     *
79e490cd80SGreg Roach     * @param Tree                 $tree
8076692c8bSGreg Roach     * @param int                  $block_id
813caaa4d2SGreg Roach     * @param string               $context
8276d39c55SGreg Roach     * @param array<string,string> $config
8376692c8bSGreg Roach     *
8476692c8bSGreg Roach     * @return string
8576692c8bSGreg Roach     */
863caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
87c1010edaSGreg Roach    {
88f0a11419SGreg Roach        $calendar_service = new CalendarService();
89f0a11419SGreg Roach
90e490cd80SGreg Roach        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
91c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
92c385536dSGreg Roach        $calendar  = $this->getBlockSetting($block_id, 'calendar', self::DEFAULT_CALENDAR);
938c2e8227SGreg Roach
943caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
958c2e8227SGreg Roach
9659f2f229SGreg Roach        $jewish_calendar = new JewishCalendar();
97d97083feSGreg Roach        $startjd         = Registry::timestampFactory()->now()->julianDay();
98d97083feSGreg Roach        $endjd           = Registry::timestampFactory()->now()->addDays($days - 1)->julianDay();
998c2e8227SGreg Roach
1008c2e8227SGreg Roach        // The standard anniversary rules cover most of the Yahrzeit rules, we just
1018c2e8227SGreg Roach        // need to handle a few special cases.
10289f721acSGreg Roach        // Fetch normal anniversaries, with an extra day before/after
103e24053e5SGreg Roach        $yahrzeits = new Collection();
1048c2e8227SGreg Roach        for ($jd = $startjd - 1; $jd <= $endjd + $days; ++$jd) {
105f0a11419SGreg Roach            foreach ($calendar_service->getAnniversaryEvents($jd, 'DEAT _YART', $tree) as $fact) {
1068c2e8227SGreg Roach                // Exact hebrew dates only
1072decada7SGreg Roach                $date = $fact->date();
1088c2e8227SGreg Roach                if ($date->minimumDate() instanceof JewishDate && $date->minimumJulianDay() === $date->maximumJulianDay()) {
10963b8905bSGreg Roach                    $jd_yahrtzeit = $jd;
11089f721acSGreg Roach                    // ...then adjust DEAT dates (but not _YART)
11193386620SGreg Roach                    if ($fact->tag() === 'INDI:DEAT') {
11289f721acSGreg Roach                        $today     = new JewishDate($jd);
1132decada7SGreg Roach                        $hd        = $fact->date()->minimumDate();
1148c2e8227SGreg Roach                        $hd1       = new JewishDate($hd);
115e364afe4SGreg Roach                        ++$hd1->year;
1168c2e8227SGreg Roach                        $hd1->setJdFromYmd();
117ad3143ccSGreg Roach                        // Special rules. See https://www.hebcal.com/help/anniv.html
1188c2e8227SGreg Roach                        // Everything else is taken care of by our standard anniversary rules.
1190195b0ddSGreg Roach                        if ($hd->day === 30 && $hd->month === 2 && $hd->year !== 0 && $hd1->daysInMonth() < 30) {
1208c2e8227SGreg Roach                            // 30 CSH - Last day in CSH
12163b8905bSGreg Roach                            $jd_yahrtzeit = $jewish_calendar->ymdToJd($today->year, 3, 1) - 1;
1220195b0ddSGreg Roach                        } elseif ($hd->day === 30 && $hd->month === 3 && $hd->year !== 0 && $hd1->daysInMonth() < 30) {
1238c2e8227SGreg Roach                            // 30 KSL - Last day in KSL
12463b8905bSGreg Roach                            $jd_yahrtzeit = $jewish_calendar->ymdToJd($today->year, 4, 1) - 1;
1250195b0ddSGreg Roach                        } elseif ($hd->day === 30 && $hd->month === 6 && $hd->year !== 0 && $today->daysInMonth() < 30 && !$today->isLeapYear()) {
1268c2e8227SGreg Roach                            // 30 ADR - Last day in SHV
12763b8905bSGreg Roach                            $jd_yahrtzeit = $jewish_calendar->ymdToJd($today->year, 6, 1) - 1;
12889f721acSGreg Roach                        }
12989f721acSGreg Roach                    }
13089f721acSGreg Roach
13189f721acSGreg Roach                    // Filter adjusted dates to our date range
13263b8905bSGreg Roach                    if ($jd_yahrtzeit >= $startjd && $jd_yahrtzeit < $startjd + $days) {
133fceda430SGreg Roach                        // upcoming yahrzeit dates
13489f721acSGreg Roach                        switch ($calendar) {
13589f721acSGreg Roach                            case 'gregorian':
13663b8905bSGreg Roach                                $yahrzeit_calendar_date = new GregorianDate($jd_yahrtzeit);
13789f721acSGreg Roach                                break;
13889f721acSGreg Roach                            case 'jewish':
13989f721acSGreg Roach                            default:
14063b8905bSGreg Roach                                $yahrzeit_calendar_date = new JewishDate($jd_yahrtzeit);
14189f721acSGreg Roach                                break;
14289f721acSGreg Roach                        }
14363b8905bSGreg Roach                        $yahrzeit_date = new Date($yahrzeit_calendar_date->format('%@ %A %O %E'));
14489f721acSGreg Roach
145e24053e5SGreg Roach                        $yahrzeits->add((object) [
146e7766c08SGreg Roach                            'individual'    => $fact->record(),
1472decada7SGreg Roach                            'fact_date'     => $fact->date(),
14889f721acSGreg Roach                            'fact'          => $fact,
14989f721acSGreg Roach                            'yahrzeit_date' => $yahrzeit_date,
150e24053e5SGreg Roach                        ]);
15189f721acSGreg Roach                    }
1528c2e8227SGreg Roach                }
1538c2e8227SGreg Roach            }
1548c2e8227SGreg Roach        }
1558c2e8227SGreg Roach
1568c2e8227SGreg Roach        switch ($infoStyle) {
1578c2e8227SGreg Roach            case 'list':
158147e99aaSGreg Roach                $content = view('modules/yahrzeit/list', [
159e24053e5SGreg Roach                    'id'         => $block_id,
16001221f27SGreg Roach                    'limit_low'  => self::LIMIT_LOW,
16101221f27SGreg Roach                    'limit_high' => self::LIMIT_HIGH,
16289f721acSGreg Roach                    'yahrzeits'  => $yahrzeits,
16389f721acSGreg Roach                ]);
1648c2e8227SGreg Roach                break;
1658c2e8227SGreg Roach            case 'table':
1668c2e8227SGreg Roach            default:
167147e99aaSGreg Roach                $content = view('modules/yahrzeit/table', [
16801221f27SGreg Roach                    'limit_low'  => self::LIMIT_LOW,
16901221f27SGreg Roach                    'limit_high' => self::LIMIT_HIGH,
17089f721acSGreg Roach                    'yahrzeits'  => $yahrzeits,
17189f721acSGreg Roach                ]);
1728c2e8227SGreg Roach                break;
1738c2e8227SGreg Roach        }
1748c2e8227SGreg Roach
1753caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
176147e99aaSGreg Roach            return view('modules/block-template', [
1771e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1789c6524dcSGreg Roach                'id'         => $block_id,
1793caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
18049a243cbSGreg Roach                'title'      => $this->title(),
1819c6524dcSGreg Roach                'content'    => $content,
1829c6524dcSGreg Roach            ]);
1838c2e8227SGreg Roach        }
184b2ce94c6SRico Sonntag
185b2ce94c6SRico Sonntag        return $content;
1868c2e8227SGreg Roach    }
1878c2e8227SGreg Roach
1883caaa4d2SGreg Roach    /**
1893caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1903caaa4d2SGreg Roach     *
1913caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1923caaa4d2SGreg Roach     *
1933caaa4d2SGreg Roach     * @return bool
1943caaa4d2SGreg Roach     */
195c1010edaSGreg Roach    public function loadAjax(): bool
196c1010edaSGreg Roach    {
1978c2e8227SGreg Roach        return true;
1988c2e8227SGreg Roach    }
1998c2e8227SGreg Roach
2003caaa4d2SGreg Roach    /**
2013caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
2023caaa4d2SGreg Roach     *
2033caaa4d2SGreg Roach     * @return bool
2043caaa4d2SGreg Roach     */
205c1010edaSGreg Roach    public function isUserBlock(): bool
206c1010edaSGreg Roach    {
2078c2e8227SGreg Roach        return true;
2088c2e8227SGreg Roach    }
2098c2e8227SGreg Roach
2103caaa4d2SGreg Roach    /**
2113caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
2123caaa4d2SGreg Roach     *
2133caaa4d2SGreg Roach     * @return bool
2143caaa4d2SGreg Roach     */
21563276d8fSGreg Roach    public function isTreeBlock(): bool
216c1010edaSGreg Roach    {
2178c2e8227SGreg Roach        return true;
2188c2e8227SGreg Roach    }
2198c2e8227SGreg Roach
22076692c8bSGreg Roach    /**
221a45f9889SGreg Roach     * Update the configuration for a block.
222a45f9889SGreg Roach     *
2236ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
224a45f9889SGreg Roach     * @param int     $block_id
225a45f9889SGreg Roach     *
226a45f9889SGreg Roach     * @return void
227a45f9889SGreg Roach     */
2286ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
229a45f9889SGreg Roach    {
230748dbe15SGreg Roach        $days       = Validator::parsedBody($request)->string('days', self::DEFAULT_DAYS);
231748dbe15SGreg Roach        $info_style = Validator::parsedBody($request)->string('infoStyle', self::DEFAULT_STYLE);
232748dbe15SGreg Roach        $calendar   = Validator::parsedBody($request)->string('calendar', self::DEFAULT_CALENDAR);
233c9e6b699SGreg Roach
234748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'days', $days);
235748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $info_style);
236748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'calendar', $calendar);
237a45f9889SGreg Roach    }
238a45f9889SGreg Roach
239a45f9889SGreg Roach    /**
24076692c8bSGreg Roach     * An HTML form to edit block settings
24176692c8bSGreg Roach     *
242e490cd80SGreg Roach     * @param Tree $tree
24376692c8bSGreg Roach     * @param int  $block_id
244a9430be8SGreg Roach     *
2453caaa4d2SGreg Roach     * @return string
24676692c8bSGreg Roach     */
2473caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
248c1010edaSGreg Roach    {
249e2a378d3SGreg Roach        $calendar   = $this->getBlockSetting($block_id, 'calendar', 'jewish');
250147e99aaSGreg Roach        $days       = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
251dc270d8cSGreg Roach        $info_style = $this->getBlockSetting($block_id, 'infoStyle', 'table');
2528c2e8227SGreg Roach
253c385536dSGreg Roach        $styles = [
254bbb76c12SGreg Roach            /* I18N: An option in a list-box */
255bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
256bbb76c12SGreg Roach            /* I18N: An option in a list-box */
257bbb76c12SGreg Roach            'table' => I18N::translate('table'),
258c385536dSGreg Roach        ];
2598c2e8227SGreg Roach
260c385536dSGreg Roach        $calendars = [
261c385536dSGreg Roach            'jewish'    => I18N::translate('Jewish'),
262c385536dSGreg Roach            'gregorian' => I18N::translate('Gregorian'),
263c385536dSGreg Roach        ];
2648c2e8227SGreg Roach
2653caaa4d2SGreg Roach        return view('modules/yahrzeit/config', [
266c385536dSGreg Roach            'calendar'   => $calendar,
267c385536dSGreg Roach            'calendars'  => $calendars,
268c385536dSGreg Roach            'days'       => $days,
269dc270d8cSGreg Roach            'info_style' => $info_style,
270c385536dSGreg Roach            'max_days'   => self::MAX_DAYS,
271c385536dSGreg Roach            'styles'     => $styles,
272c385536dSGreg Roach        ]);
2738c2e8227SGreg Roach    }
2748c2e8227SGreg Roach}
275