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