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