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