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