xref: /webtrees/app/Module/YahrzeitModule.php (revision 76d39c55735cfa9ad0972b0dd530e96b051f9ebe)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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;
30e24053e5SGreg Roachuse Illuminate\Support\Collection;
311e7a7a28SGreg Roachuse Illuminate\Support\Str;
326ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
338c2e8227SGreg Roach
340195b0ddSGreg Roachuse function extract;
350195b0ddSGreg Roachuse function view;
360195b0ddSGreg Roach
370195b0ddSGreg Roachuse const EXTR_OVERWRITE;
380195b0ddSGreg Roach
398c2e8227SGreg Roach/**
408c2e8227SGreg Roach * Class YahrzeitModule
418c2e8227SGreg Roach */
4237eb8894SGreg Roachclass YahrzeitModule extends AbstractModule implements ModuleBlockInterface
43c1010edaSGreg Roach{
4449a243cbSGreg Roach    use ModuleBlockTrait;
4549a243cbSGreg Roach
46c385536dSGreg Roach    // Default values for new blocks.
4716d6367aSGreg Roach    private const DEFAULT_CALENDAR = 'jewish';
4816d6367aSGreg Roach    private const DEFAULT_DAYS     = '7';
4916d6367aSGreg Roach    private const DEFAULT_STYLE    = 'table';
50c385536dSGreg Roach
51c385536dSGreg Roach    // Can show this number of days into the future.
5216d6367aSGreg Roach    private const MAX_DAYS = 30;
53c385536dSGreg Roach
5401221f27SGreg Roach    // Pagination
5501221f27SGreg Roach    private const LIMIT_LOW  = 10;
5601221f27SGreg Roach    private const LIMIT_HIGH = 20;
5701221f27SGreg Roach
58961ec755SGreg Roach    /**
590cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
60961ec755SGreg Roach     *
61961ec755SGreg Roach     * @return string
62961ec755SGreg Roach     */
6349a243cbSGreg Roach    public function title(): string
64c1010edaSGreg Roach    {
65bbb76c12SGreg Roach        /* I18N: Name of a module. Yahrzeiten (the plural of Yahrzeit) are special anniversaries of deaths in the Hebrew faith/calendar. */
66bbb76c12SGreg Roach        return I18N::translate('Yahrzeiten');
678c2e8227SGreg Roach    }
688c2e8227SGreg Roach
69961ec755SGreg Roach    /**
70961ec755SGreg Roach     * A sentence describing what this module does.
71961ec755SGreg Roach     *
72961ec755SGreg Roach     * @return string
73961ec755SGreg Roach     */
7449a243cbSGreg Roach    public function description(): string
75c1010edaSGreg Roach    {
76bbb76c12SGreg Roach        /* I18N: Description of the “Yahrzeiten” module. A “Hebrew death” is a death where the date is recorded in the Hebrew calendar. */
77bbb76c12SGreg Roach        return I18N::translate('A list of the Hebrew death anniversaries that will occur in the near future.');
788c2e8227SGreg Roach    }
798c2e8227SGreg Roach
8076692c8bSGreg Roach    /**
8176692c8bSGreg Roach     * Generate the HTML content of this block.
8276692c8bSGreg Roach     *
83e490cd80SGreg Roach     * @param Tree                 $tree
8476692c8bSGreg Roach     * @param int                  $block_id
853caaa4d2SGreg Roach     * @param string               $context
86*76d39c55SGreg Roach     * @param array<string,string> $config
8776692c8bSGreg Roach     *
8876692c8bSGreg Roach     * @return string
8976692c8bSGreg Roach     */
903caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
91c1010edaSGreg Roach    {
92f0a11419SGreg Roach        $calendar_service = new CalendarService();
93f0a11419SGreg Roach
94e490cd80SGreg Roach        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
95c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
96c385536dSGreg Roach        $calendar  = $this->getBlockSetting($block_id, 'calendar', self::DEFAULT_CALENDAR);
978c2e8227SGreg Roach
983caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
998c2e8227SGreg Roach
10059f2f229SGreg Roach        $jewish_calendar = new JewishCalendar();
101d97083feSGreg Roach        $startjd         = Registry::timestampFactory()->now()->julianDay();
102d97083feSGreg Roach        $endjd           = Registry::timestampFactory()->now()->addDays($days - 1)->julianDay();
1038c2e8227SGreg Roach
1048c2e8227SGreg Roach        // The standard anniversary rules cover most of the Yahrzeit rules, we just
1058c2e8227SGreg Roach        // need to handle a few special cases.
10689f721acSGreg Roach        // Fetch normal anniversaries, with an extra day before/after
107e24053e5SGreg Roach        $yahrzeits = new Collection();
1088c2e8227SGreg Roach        for ($jd = $startjd - 1; $jd <= $endjd + $days; ++$jd) {
109f0a11419SGreg Roach            foreach ($calendar_service->getAnniversaryEvents($jd, 'DEAT _YART', $tree) as $fact) {
1108c2e8227SGreg Roach                // Exact hebrew dates only
1112decada7SGreg Roach                $date = $fact->date();
1128c2e8227SGreg Roach                if ($date->minimumDate() instanceof JewishDate && $date->minimumJulianDay() === $date->maximumJulianDay()) {
11363b8905bSGreg Roach                    $jd_yahrtzeit = $jd;
11489f721acSGreg Roach                    // ...then adjust DEAT dates (but not _YART)
11593386620SGreg Roach                    if ($fact->tag() === 'INDI:DEAT') {
11689f721acSGreg Roach                        $today     = new JewishDate($jd);
1172decada7SGreg Roach                        $hd        = $fact->date()->minimumDate();
1188c2e8227SGreg Roach                        $hd1       = new JewishDate($hd);
119e364afe4SGreg Roach                        ++$hd1->year;
1208c2e8227SGreg Roach                        $hd1->setJdFromYmd();
121ad3143ccSGreg Roach                        // Special rules. See https://www.hebcal.com/help/anniv.html
1228c2e8227SGreg Roach                        // Everything else is taken care of by our standard anniversary rules.
1230195b0ddSGreg Roach                        if ($hd->day === 30 && $hd->month === 2 && $hd->year !== 0 && $hd1->daysInMonth() < 30) {
1248c2e8227SGreg Roach                            // 30 CSH - Last day in CSH
12563b8905bSGreg Roach                            $jd_yahrtzeit = $jewish_calendar->ymdToJd($today->year, 3, 1) - 1;
1260195b0ddSGreg Roach                        } elseif ($hd->day === 30 && $hd->month === 3 && $hd->year !== 0 && $hd1->daysInMonth() < 30) {
1278c2e8227SGreg Roach                            // 30 KSL - Last day in KSL
12863b8905bSGreg Roach                            $jd_yahrtzeit = $jewish_calendar->ymdToJd($today->year, 4, 1) - 1;
1290195b0ddSGreg Roach                        } elseif ($hd->day === 30 && $hd->month === 6 && $hd->year !== 0 && $today->daysInMonth() < 30 && !$today->isLeapYear()) {
1308c2e8227SGreg Roach                            // 30 ADR - Last day in SHV
13163b8905bSGreg Roach                            $jd_yahrtzeit = $jewish_calendar->ymdToJd($today->year, 6, 1) - 1;
13289f721acSGreg Roach                        }
13389f721acSGreg Roach                    }
13489f721acSGreg Roach
13589f721acSGreg Roach                    // Filter adjusted dates to our date range
13663b8905bSGreg Roach                    if ($jd_yahrtzeit >= $startjd && $jd_yahrtzeit < $startjd + $days) {
137fceda430SGreg Roach                        // upcoming yahrzeit dates
13889f721acSGreg Roach                        switch ($calendar) {
13989f721acSGreg Roach                            case 'gregorian':
14063b8905bSGreg Roach                                $yahrzeit_calendar_date = new GregorianDate($jd_yahrtzeit);
14189f721acSGreg Roach                                break;
14289f721acSGreg Roach                            case 'jewish':
14389f721acSGreg Roach                            default:
14463b8905bSGreg Roach                                $yahrzeit_calendar_date = new JewishDate($jd_yahrtzeit);
14589f721acSGreg Roach                                break;
14689f721acSGreg Roach                        }
14763b8905bSGreg Roach                        $yahrzeit_date = new Date($yahrzeit_calendar_date->format('%@ %A %O %E'));
14889f721acSGreg Roach
149e24053e5SGreg Roach                        $yahrzeits->add((object) [
150e7766c08SGreg Roach                            'individual'    => $fact->record(),
1512decada7SGreg Roach                            'fact_date'     => $fact->date(),
15289f721acSGreg Roach                            'fact'          => $fact,
15389f721acSGreg Roach                            'yahrzeit_date' => $yahrzeit_date,
154e24053e5SGreg Roach                        ]);
15589f721acSGreg Roach                    }
1568c2e8227SGreg Roach                }
1578c2e8227SGreg Roach            }
1588c2e8227SGreg Roach        }
1598c2e8227SGreg Roach
1608c2e8227SGreg Roach        switch ($infoStyle) {
1618c2e8227SGreg Roach            case 'list':
162147e99aaSGreg Roach                $content = view('modules/yahrzeit/list', [
163e24053e5SGreg Roach                    'id'         => $block_id,
16401221f27SGreg Roach                    'limit_low'  => self::LIMIT_LOW,
16501221f27SGreg Roach                    'limit_high' => self::LIMIT_HIGH,
16689f721acSGreg Roach                    'yahrzeits'  => $yahrzeits,
16789f721acSGreg Roach                ]);
1688c2e8227SGreg Roach                break;
1698c2e8227SGreg Roach            case 'table':
1708c2e8227SGreg Roach            default:
171147e99aaSGreg Roach                $content = view('modules/yahrzeit/table', [
17201221f27SGreg Roach                    'limit_low'  => self::LIMIT_LOW,
17301221f27SGreg Roach                    'limit_high' => self::LIMIT_HIGH,
17489f721acSGreg Roach                    'yahrzeits'  => $yahrzeits,
17589f721acSGreg Roach                ]);
1768c2e8227SGreg Roach                break;
1778c2e8227SGreg Roach        }
1788c2e8227SGreg Roach
1793caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
180147e99aaSGreg Roach            return view('modules/block-template', [
1811e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1829c6524dcSGreg Roach                'id'         => $block_id,
1833caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
18449a243cbSGreg Roach                'title'      => $this->title(),
1859c6524dcSGreg Roach                'content'    => $content,
1869c6524dcSGreg Roach            ]);
1878c2e8227SGreg Roach        }
188b2ce94c6SRico Sonntag
189b2ce94c6SRico Sonntag        return $content;
1908c2e8227SGreg Roach    }
1918c2e8227SGreg Roach
1923caaa4d2SGreg Roach    /**
1933caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1943caaa4d2SGreg Roach     *
1953caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1963caaa4d2SGreg Roach     *
1973caaa4d2SGreg Roach     * @return bool
1983caaa4d2SGreg Roach     */
199c1010edaSGreg Roach    public function loadAjax(): bool
200c1010edaSGreg Roach    {
2018c2e8227SGreg Roach        return true;
2028c2e8227SGreg Roach    }
2038c2e8227SGreg Roach
2043caaa4d2SGreg Roach    /**
2053caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
2063caaa4d2SGreg Roach     *
2073caaa4d2SGreg Roach     * @return bool
2083caaa4d2SGreg Roach     */
209c1010edaSGreg Roach    public function isUserBlock(): bool
210c1010edaSGreg Roach    {
2118c2e8227SGreg Roach        return true;
2128c2e8227SGreg Roach    }
2138c2e8227SGreg Roach
2143caaa4d2SGreg Roach    /**
2153caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
2163caaa4d2SGreg Roach     *
2173caaa4d2SGreg Roach     * @return bool
2183caaa4d2SGreg Roach     */
21963276d8fSGreg Roach    public function isTreeBlock(): bool
220c1010edaSGreg Roach    {
2218c2e8227SGreg Roach        return true;
2228c2e8227SGreg Roach    }
2238c2e8227SGreg Roach
22476692c8bSGreg Roach    /**
225a45f9889SGreg Roach     * Update the configuration for a block.
226a45f9889SGreg Roach     *
2276ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
228a45f9889SGreg Roach     * @param int     $block_id
229a45f9889SGreg Roach     *
230a45f9889SGreg Roach     * @return void
231a45f9889SGreg Roach     */
2326ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
233a45f9889SGreg Roach    {
234b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
235c9e6b699SGreg Roach
236b46c87bdSGreg Roach        $this->setBlockSetting($block_id, 'days', $params['days'] ?? self::DEFAULT_DAYS);
237b46c87bdSGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle'] ?? self::DEFAULT_STYLE);
238b46c87bdSGreg Roach        $this->setBlockSetting($block_id, 'calendar', $params['calendar'] ?? self::DEFAULT_CALENDAR);
239a45f9889SGreg Roach    }
240a45f9889SGreg Roach
241a45f9889SGreg Roach    /**
24276692c8bSGreg Roach     * An HTML form to edit block settings
24376692c8bSGreg Roach     *
244e490cd80SGreg Roach     * @param Tree $tree
24576692c8bSGreg Roach     * @param int  $block_id
246a9430be8SGreg Roach     *
2473caaa4d2SGreg Roach     * @return string
24876692c8bSGreg Roach     */
2493caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
250c1010edaSGreg Roach    {
251e2a378d3SGreg Roach        $calendar   = $this->getBlockSetting($block_id, 'calendar', 'jewish');
252147e99aaSGreg Roach        $days       = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
253dc270d8cSGreg Roach        $info_style = $this->getBlockSetting($block_id, 'infoStyle', 'table');
2548c2e8227SGreg Roach
255c385536dSGreg Roach        $styles = [
256bbb76c12SGreg Roach            /* I18N: An option in a list-box */
257bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
258bbb76c12SGreg Roach            /* I18N: An option in a list-box */
259bbb76c12SGreg Roach            'table' => I18N::translate('table'),
260c385536dSGreg Roach        ];
2618c2e8227SGreg Roach
262c385536dSGreg Roach        $calendars = [
263c385536dSGreg Roach            'jewish'    => I18N::translate('Jewish'),
264c385536dSGreg Roach            'gregorian' => I18N::translate('Gregorian'),
265c385536dSGreg Roach        ];
2668c2e8227SGreg Roach
2673caaa4d2SGreg Roach        return view('modules/yahrzeit/config', [
268c385536dSGreg Roach            'calendar'   => $calendar,
269c385536dSGreg Roach            'calendars'  => $calendars,
270c385536dSGreg Roach            'days'       => $days,
271dc270d8cSGreg Roach            'info_style' => $info_style,
272c385536dSGreg Roach            'max_days'   => self::MAX_DAYS,
273c385536dSGreg Roach            'styles'     => $styles,
274c385536dSGreg Roach        ]);
2758c2e8227SGreg Roach    }
2768c2e8227SGreg Roach}
277