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