xref: /webtrees/app/Module/YahrzeitModule.php (revision 8634febf6487fcb3ee7b59e88f828071a22cba22)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\ExtCalendar\JewishCalendar;
23use Fisharebest\Webtrees\Date;
24use Fisharebest\Webtrees\Date\GregorianDate;
25use Fisharebest\Webtrees\Date\JewishDate;
26use Fisharebest\Webtrees\I18N;
27use Fisharebest\Webtrees\Registry;
28use Fisharebest\Webtrees\Services\CalendarService;
29use Fisharebest\Webtrees\Tree;
30use Fisharebest\Webtrees\Validator;
31use Illuminate\Support\Collection;
32use Illuminate\Support\Str;
33use Psr\Http\Message\ServerRequestInterface;
34
35use function extract;
36use function view;
37
38use const EXTR_OVERWRITE;
39
40/**
41 * Class YahrzeitModule
42 */
43class YahrzeitModule extends AbstractModule implements ModuleBlockInterface
44{
45    use ModuleBlockTrait;
46
47    // Default values for new blocks.
48    private const DEFAULT_CALENDAR = 'jewish';
49    private const DEFAULT_DAYS     = '7';
50    private const DEFAULT_STYLE    = 'table';
51
52    // Can show this number of days into the future.
53    private const MAX_DAYS = 30;
54
55    // Pagination
56    private const LIMIT_LOW  = 10;
57    private const LIMIT_HIGH = 20;
58
59    /**
60     * How should this module be identified in the control panel, etc.?
61     *
62     * @return string
63     */
64    public function title(): string
65    {
66        /* I18N: Name of a module. Yahrzeiten (the plural of Yahrzeit) are special anniversaries of deaths in the Hebrew faith/calendar. */
67        return I18N::translate('Yahrzeiten');
68    }
69
70    /**
71     * A sentence describing what this module does.
72     *
73     * @return string
74     */
75    public function description(): string
76    {
77        /* I18N: Description of the “Yahrzeiten” module. A “Hebrew death” is a death where the date is recorded in the Hebrew calendar. */
78        return I18N::translate('A list of the Hebrew death anniversaries that will occur in the near future.');
79    }
80
81    /**
82     * Generate the HTML content of this block.
83     *
84     * @param Tree                 $tree
85     * @param int                  $block_id
86     * @param string               $context
87     * @param array<string,string> $config
88     *
89     * @return string
90     */
91    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
92    {
93        $calendar_service = new CalendarService();
94
95        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
96        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
97        $calendar  = $this->getBlockSetting($block_id, 'calendar', self::DEFAULT_CALENDAR);
98
99        extract($config, EXTR_OVERWRITE);
100
101        $jewish_calendar = new JewishCalendar();
102        $startjd         = Registry::timestampFactory()->now()->julianDay();
103        $endjd           = Registry::timestampFactory()->now()->addDays($days - 1)->julianDay();
104
105        // The standard anniversary rules cover most of the Yahrzeit rules, we just
106        // need to handle a few special cases.
107        // Fetch normal anniversaries, with an extra day before/after
108        $yahrzeits = new Collection();
109        for ($jd = $startjd - 1; $jd <= $endjd + $days; ++$jd) {
110            foreach ($calendar_service->getAnniversaryEvents($jd, 'DEAT _YART', $tree) as $fact) {
111                // Exact hebrew dates only
112                $date = $fact->date();
113                if ($date->minimumDate() instanceof JewishDate && $date->minimumJulianDay() === $date->maximumJulianDay()) {
114                    $jd_yahrtzeit = $jd;
115                    // ...then adjust DEAT dates (but not _YART)
116                    if ($fact->tag() === 'INDI:DEAT') {
117                        $today     = new JewishDate($jd);
118                        $hd        = $fact->date()->minimumDate();
119                        $hd1       = new JewishDate($hd);
120                        ++$hd1->year;
121                        $hd1->setJdFromYmd();
122                        // Special rules. See https://www.hebcal.com/help/anniv.html
123                        // Everything else is taken care of by our standard anniversary rules.
124                        if ($hd->day === 30 && $hd->month === 2 && $hd->year !== 0 && $hd1->daysInMonth() < 30) {
125                            // 30 CSH - Last day in CSH
126                            $jd_yahrtzeit = $jewish_calendar->ymdToJd($today->year, 3, 1) - 1;
127                        } elseif ($hd->day === 30 && $hd->month === 3 && $hd->year !== 0 && $hd1->daysInMonth() < 30) {
128                            // 30 KSL - Last day in KSL
129                            $jd_yahrtzeit = $jewish_calendar->ymdToJd($today->year, 4, 1) - 1;
130                        } elseif ($hd->day === 30 && $hd->month === 6 && $hd->year !== 0 && $today->daysInMonth() < 30 && !$today->isLeapYear()) {
131                            // 30 ADR - Last day in SHV
132                            $jd_yahrtzeit = $jewish_calendar->ymdToJd($today->year, 6, 1) - 1;
133                        }
134                    }
135
136                    // Filter adjusted dates to our date range
137                    if ($jd_yahrtzeit >= $startjd && $jd_yahrtzeit < $startjd + $days) {
138                        // upcoming yahrzeit dates
139                        switch ($calendar) {
140                            case 'gregorian':
141                                $yahrzeit_calendar_date = new GregorianDate($jd_yahrtzeit);
142                                break;
143                            case 'jewish':
144                            default:
145                                $yahrzeit_calendar_date = new JewishDate($jd_yahrtzeit);
146                                break;
147                        }
148                        $yahrzeit_date = new Date($yahrzeit_calendar_date->format('%@ %A %O %E'));
149
150                        $yahrzeits->add((object) [
151                            'individual'    => $fact->record(),
152                            'fact_date'     => $fact->date(),
153                            'fact'          => $fact,
154                            'yahrzeit_date' => $yahrzeit_date,
155                        ]);
156                    }
157                }
158            }
159        }
160
161        switch ($infoStyle) {
162            case 'list':
163                $content = view('modules/yahrzeit/list', [
164                    'id'         => $block_id,
165                    'limit_low'  => self::LIMIT_LOW,
166                    'limit_high' => self::LIMIT_HIGH,
167                    'yahrzeits'  => $yahrzeits,
168                ]);
169                break;
170            case 'table':
171            default:
172                $content = view('modules/yahrzeit/table', [
173                    'limit_low'  => self::LIMIT_LOW,
174                    'limit_high' => self::LIMIT_HIGH,
175                    'yahrzeits'  => $yahrzeits,
176                ]);
177                break;
178        }
179
180        if ($context !== self::CONTEXT_EMBED) {
181            return view('modules/block-template', [
182                'block'      => Str::kebab($this->name()),
183                'id'         => $block_id,
184                'config_url' => $this->configUrl($tree, $context, $block_id),
185                'title'      => $this->title(),
186                'content'    => $content,
187            ]);
188        }
189
190        return $content;
191    }
192
193    /**
194     * Should this block load asynchronously using AJAX?
195     *
196     * Simple blocks are faster in-line, more complex ones can be loaded later.
197     *
198     * @return bool
199     */
200    public function loadAjax(): bool
201    {
202        return true;
203    }
204
205    /**
206     * Can this block be shown on the user’s home page?
207     *
208     * @return bool
209     */
210    public function isUserBlock(): bool
211    {
212        return true;
213    }
214
215    /**
216     * Can this block be shown on the tree’s home page?
217     *
218     * @return bool
219     */
220    public function isTreeBlock(): bool
221    {
222        return true;
223    }
224
225    /**
226     * Update the configuration for a block.
227     *
228     * @param ServerRequestInterface $request
229     * @param int     $block_id
230     *
231     * @return void
232     */
233    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
234    {
235        $days       = Validator::parsedBody($request)->string('days', self::DEFAULT_DAYS);
236        $info_style = Validator::parsedBody($request)->string('infoStyle', self::DEFAULT_STYLE);
237        $calendar   = Validator::parsedBody($request)->string('calendar', self::DEFAULT_CALENDAR);
238
239        $this->setBlockSetting($block_id, 'days', $days);
240        $this->setBlockSetting($block_id, 'infoStyle', $info_style);
241        $this->setBlockSetting($block_id, 'calendar', $calendar);
242    }
243
244    /**
245     * An HTML form to edit block settings
246     *
247     * @param Tree $tree
248     * @param int  $block_id
249     *
250     * @return string
251     */
252    public function editBlockConfiguration(Tree $tree, int $block_id): string
253    {
254        $calendar   = $this->getBlockSetting($block_id, 'calendar', 'jewish');
255        $days       = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
256        $info_style = $this->getBlockSetting($block_id, 'infoStyle', 'table');
257
258        $styles = [
259            /* I18N: An option in a list-box */
260            'list'  => I18N::translate('list'),
261            /* I18N: An option in a list-box */
262            'table' => I18N::translate('table'),
263        ];
264
265        $calendars = [
266            'jewish'    => I18N::translate('Jewish'),
267            'gregorian' => I18N::translate('Gregorian'),
268        ];
269
270        return view('modules/yahrzeit/config', [
271            'calendar'   => $calendar,
272            'calendars'  => $calendars,
273            'days'       => $days,
274            'info_style' => $info_style,
275            'max_days'   => self::MAX_DAYS,
276            'styles'     => $styles,
277        ]);
278    }
279}
280