xref: /webtrees/app/Module/YahrzeitModule.php (revision 43e2aec9e526d1a242c3e80ff940736aec157724)
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\Filter;
24use Fisharebest\Webtrees\Functions\FunctionsDb;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Tree;
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        return /* I18N: Name of a module. Yahrzeiten (the plural of Yahrzeit) are special anniversaries of deaths in the Hebrew faith/calendar. */
45            I18N::translate('Yahrzeiten');
46    }
47
48    /** {@inheritdoc} */
49    public function getDescription()
50    {
51        return /* I18N: Description of the “Yahrzeiten” module. A “Hebrew death” is a death where the date is recorded in the Hebrew calendar. */
52            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        $days      = (int)$this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
70        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
71        $calendar  = $this->getBlockSetting($block_id, 'calendar', self::DEFAULT_CALENDAR);
72
73        extract($cfg, EXTR_OVERWRITE);
74
75        $jewish_calendar = new JewishCalendar;
76        $startjd         = WT_CLIENT_JD;
77        $endjd           = WT_CLIENT_JD + $days - 1;
78
79        // The standard anniversary rules cover most of the Yahrzeit rules, we just
80        // need to handle a few special cases.
81        // Fetch normal anniversaries, with an extra day before/after
82        $yahrzeits = [];
83        for ($jd = $startjd - 1; $jd <= $endjd + $days; ++$jd) {
84            foreach (FunctionsDb::getAnniversaryEvents($jd, 'DEAT _YART', $tree) as $fact) {
85                // Exact hebrew dates only
86                $date = $fact->getDate();
87                if ($date->minimumDate() instanceof JewishDate && $date->minimumJulianDay() === $date->maximumJulianDay()) {
88
89                    // ...then adjust DEAT dates (but not _YART)
90                    if ($fact->getTag() === 'DEAT') {
91                        $today  = new JewishDate($jd);
92                        $hd     = $fact->getDate()->minimumDate();
93                        $hd1    = new JewishDate($hd);
94                        $hd1->y += 1;
95                        $hd1->setJdFromYmd();
96                        // Special rules. See http://www.hebcal.com/help/anniv.html
97                        // Everything else is taken care of by our standard anniversary rules.
98                        if ($hd->d == 30 && $hd->m == 2 && $hd->y != 0 && $hd1->daysInMonth() < 30) {
99                            // 30 CSH - Last day in CSH
100                            $jd = $jewish_calendar->ymdToJd($today->y, 3, 1) - 1;
101                        } elseif ($hd->d == 30 && $hd->m == 3 && $hd->y != 0 && $hd1->daysInMonth() < 30) {
102                            // 30 KSL - Last day in KSL
103                            $jd = $jewish_calendar->ymdToJd($today->y, 4, 1) - 1;
104                        } elseif ($hd->d == 30 && $hd->m == 6 && $hd->y != 0 && $today->daysInMonth() < 30 && !$today->isLeapYear()) {
105                            // 30 ADR - Last day in SHV
106                            $jd = $jewish_calendar->ymdToJd($today->y, 6, 1) - 1;
107                        }
108                    }
109
110                    // Filter adjusted dates to our date range
111                    if ($jd >= $startjd && $jd < $startjd + $days) {
112                        // upcomming yahrzeit dates
113                        switch ($calendar) {
114                            case 'gregorian':
115                                $yahrzeit_date = new GregorianDate($jd);
116                                break;
117                            case 'jewish':
118                            default:
119                                $yahrzeit_date = new JewishDate($jd);
120                                break;
121                        }
122                        $yahrzeit_date = new Date($yahrzeit_date->format('%@ %A %O %E'));
123
124                        $yahrzeits[] = (object)[
125                            'individual'    => $fact->getParent(),
126                            'fact_date'     => $fact->getDate(),
127                            'fact'          => $fact,
128                            'jd'            => $jd,
129                            'yahrzeit_date' => $yahrzeit_date,
130                        ];
131                    }
132                }
133            }
134        }
135
136        switch ($infoStyle) {
137            case 'list':
138                $content = view('modules/yahrzeit/list', [
139                    'yahrzeits' => $yahrzeits,
140                ]);
141                break;
142            case 'table':
143            default:
144                $content = view('modules/yahrzeit/table', [
145                    'yahrzeits' => $yahrzeits,
146                ]);
147                break;
148        }
149
150        if ($template) {
151            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
152                $config_url = route('tree-page-block-edit', [
153                    'block_id' => $block_id,
154                    'ged'      => $tree->getName(),
155                ]);
156            } elseif ($ctype === 'user' && Auth::check()) {
157                $config_url = route('user-page-block-edit', [
158                    'block_id' => $block_id,
159                    'ged'      => $tree->getName(),
160                ]);
161            } else {
162                $config_url = '';
163            }
164
165            return view('modules/block-template', [
166                'block'      => str_replace('_', '-', $this->getName()),
167                'id'         => $block_id,
168                'config_url' => $config_url,
169                'title'      => $this->getTitle(),
170                'content'    => $content,
171            ]);
172        } else {
173            return $content;
174        }
175    }
176
177    /** {@inheritdoc} */
178    public function loadAjax(): bool
179    {
180        return true;
181    }
182
183    /** {@inheritdoc} */
184    public function isUserBlock(): bool
185    {
186        return true;
187    }
188
189    /** {@inheritdoc} */
190    public function isGedcomBlock(): bool
191    {
192        return true;
193    }
194
195    /**
196     * An HTML form to edit block settings
197     *
198     * @param Tree $tree
199     * @param int  $block_id
200     *
201     * @return void
202     */
203    public function configureBlock(Tree $tree, int $block_id)
204    {
205        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
206            $this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, self::MAX_DAYS, self::DEFAULT_DAYS));
207            $this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', self::DEFAULT_STYLE));
208            $this->setBlockSetting($block_id, 'calendar', Filter::post('calendar', 'jewish|gregorian', self::DEFAULT_CALENDAR));
209
210            return;
211        }
212
213        $calendar  = $this->getBlockSetting($block_id, 'calendar', 'jewish');
214        $days      = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
215        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
216
217        $styles = [
218            'list'  => /* I18N: An option in a list-box */
219                I18N::translate('list'),
220            'table' => /* I18N: An option in a list-box */
221                I18N::translate('table'),
222        ];
223
224        $calendars = [
225            'jewish'    => I18N::translate('Jewish'),
226            'gregorian' => I18N::translate('Gregorian'),
227        ];
228
229        echo view('modules/yahrzeit/config', [
230            'calendar'  => $calendar,
231            'calendars' => $calendars,
232            'days'      => $days,
233            'infoStyle' => $infoStyle,
234            'max_days'  => self::MAX_DAYS,
235            'styles'    => $styles,
236        ]);
237    }
238}
239