xref: /webtrees/app/Module/YahrzeitModule.php (revision 12c79f746ea1903d66aea1fe0412eda6149a5532)
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 */
16namespace Fisharebest\Webtrees\Module;
17
18use Fisharebest\ExtCalendar\JewishCalendar;
19use Fisharebest\Webtrees\Auth;
20use Fisharebest\Webtrees\Date;
21use Fisharebest\Webtrees\Date\GregorianDate;
22use Fisharebest\Webtrees\Date\JewishDate;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Services\CalendarService;
25use Fisharebest\Webtrees\Tree;
26use Symfony\Component\HttpFoundation\Request;
27
28/**
29 * Class YahrzeitModule
30 */
31class YahrzeitModule extends AbstractModule implements ModuleBlockInterface
32{
33    // Default values for new blocks.
34    const DEFAULT_CALENDAR = 'jewish';
35    const DEFAULT_DAYS     = 7;
36    const DEFAULT_STYLE    = 'table';
37
38    // Can show this number of days into the future.
39    const MAX_DAYS = 30;
40
41    /** {@inheritdoc} */
42    public function getTitle()
43    {
44        /* I18N: Name of a module. Yahrzeiten (the plural of Yahrzeit) are special anniversaries of deaths in the Hebrew faith/calendar. */
45        return I18N::translate('Yahrzeiten');
46    }
47
48    /** {@inheritdoc} */
49    public function getDescription()
50    {
51        /* I18N: Description of the “Yahrzeiten” module. A “Hebrew death” is a death where the date is recorded in the Hebrew calendar. */
52        return I18N::translate('A list of the Hebrew death anniversaries that will occur in the near future.');
53    }
54
55    /**
56     * Generate the HTML content of this block.
57     *
58     * @param Tree     $tree
59     * @param int      $block_id
60     * @param bool     $template
61     * @param string[] $cfg
62     *
63     * @return string
64     */
65    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
66    {
67        global $ctype;
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->getDate();
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->getDate()->minimumDate();
94                        $hd1    = new JewishDate($hd);
95                        $hd1->y += 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->d == 30 && $hd->m == 2 && $hd->y != 0 && $hd1->daysInMonth() < 30) {
100                            // 30 CSH - Last day in CSH
101                            $jd = $jewish_calendar->ymdToJd($today->y, 3, 1) - 1;
102                        } elseif ($hd->d == 30 && $hd->m == 3 && $hd->y != 0 && $hd1->daysInMonth() < 30) {
103                            // 30 KSL - Last day in KSL
104                            $jd = $jewish_calendar->ymdToJd($today->y, 4, 1) - 1;
105                        } elseif ($hd->d == 30 && $hd->m == 6 && $hd->y != 0 && $today->daysInMonth() < 30 && !$today->isLeapYear()) {
106                            // 30 ADR - Last day in SHV
107                            $jd = $jewish_calendar->ymdToJd($today->y, 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->getParent(),
127                            'fact_date'     => $fact->getDate(),
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 ($template) {
152            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
153                $config_url = route('tree-page-block-edit', [
154                    'block_id' => $block_id,
155                    'ged'      => $tree->getName(),
156                ]);
157            } elseif ($ctype === 'user' && Auth::check()) {
158                $config_url = route('user-page-block-edit', [
159                    'block_id' => $block_id,
160                    'ged'      => $tree->getName(),
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        } else {
174            return $content;
175        }
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