xref: /webtrees/app/Module/YahrzeitModule.php (revision 342dcecd8628deacd49d86f3247fd77e64bf33c3)
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        /* 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        $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                    // ...then adjust DEAT dates (but not _YART)
89                    if ($fact->getTag() === 'DEAT') {
90                        $today  = new JewishDate($jd);
91                        $hd     = $fact->getDate()->minimumDate();
92                        $hd1    = new JewishDate($hd);
93                        $hd1->y += 1;
94                        $hd1->setJdFromYmd();
95                        // Special rules. See http://www.hebcal.com/help/anniv.html
96                        // Everything else is taken care of by our standard anniversary rules.
97                        if ($hd->d == 30 && $hd->m == 2 && $hd->y != 0 && $hd1->daysInMonth() < 30) {
98                            // 30 CSH - Last day in CSH
99                            $jd = $jewish_calendar->ymdToJd($today->y, 3, 1) - 1;
100                        } elseif ($hd->d == 30 && $hd->m == 3 && $hd->y != 0 && $hd1->daysInMonth() < 30) {
101                            // 30 KSL - Last day in KSL
102                            $jd = $jewish_calendar->ymdToJd($today->y, 4, 1) - 1;
103                        } elseif ($hd->d == 30 && $hd->m == 6 && $hd->y != 0 && $today->daysInMonth() < 30 && !$today->isLeapYear()) {
104                            // 30 ADR - Last day in SHV
105                            $jd = $jewish_calendar->ymdToJd($today->y, 6, 1) - 1;
106                        }
107                    }
108
109                    // Filter adjusted dates to our date range
110                    if ($jd >= $startjd && $jd < $startjd + $days) {
111                        // upcomming yahrzeit dates
112                        switch ($calendar) {
113                            case 'gregorian':
114                                $yahrzeit_date = new GregorianDate($jd);
115                                break;
116                            case 'jewish':
117                            default:
118                                $yahrzeit_date = new JewishDate($jd);
119                                break;
120                        }
121                        $yahrzeit_date = new Date($yahrzeit_date->format('%@ %A %O %E'));
122
123                        $yahrzeits[] = (object)[
124                            'individual'    => $fact->getParent(),
125                            'fact_date'     => $fact->getDate(),
126                            'fact'          => $fact,
127                            'jd'            => $jd,
128                            'yahrzeit_date' => $yahrzeit_date,
129                        ];
130                    }
131                }
132            }
133        }
134
135        switch ($infoStyle) {
136            case 'list':
137                $content = view('modules/yahrzeit/list', [
138                    'yahrzeits' => $yahrzeits,
139                ]);
140                break;
141            case 'table':
142            default:
143                $content = view('modules/yahrzeit/table', [
144                    'yahrzeits' => $yahrzeits,
145                ]);
146                break;
147        }
148
149        if ($template) {
150            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
151                $config_url = route('tree-page-block-edit', [
152                    'block_id' => $block_id,
153                    'ged'      => $tree->getName(),
154                ]);
155            } elseif ($ctype === 'user' && Auth::check()) {
156                $config_url = route('user-page-block-edit', [
157                    'block_id' => $block_id,
158                    'ged'      => $tree->getName(),
159                ]);
160            } else {
161                $config_url = '';
162            }
163
164            return view('modules/block-template', [
165                'block'      => str_replace('_', '-', $this->getName()),
166                'id'         => $block_id,
167                'config_url' => $config_url,
168                'title'      => $this->getTitle(),
169                'content'    => $content,
170            ]);
171        } else {
172            return $content;
173        }
174    }
175
176    /** {@inheritdoc} */
177    public function loadAjax(): bool
178    {
179        return true;
180    }
181
182    /** {@inheritdoc} */
183    public function isUserBlock(): bool
184    {
185        return true;
186    }
187
188    /** {@inheritdoc} */
189    public function isGedcomBlock(): bool
190    {
191        return true;
192    }
193
194    /**
195     * An HTML form to edit block settings
196     *
197     * @param Tree $tree
198     * @param int  $block_id
199     *
200     * @return void
201     */
202    public function configureBlock(Tree $tree, int $block_id)
203    {
204        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
205            $this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, self::MAX_DAYS, self::DEFAULT_DAYS));
206            $this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', self::DEFAULT_STYLE));
207            $this->setBlockSetting($block_id, 'calendar', Filter::post('calendar', 'jewish|gregorian', self::DEFAULT_CALENDAR));
208
209            return;
210        }
211
212        $calendar  = $this->getBlockSetting($block_id, 'calendar', 'jewish');
213        $days      = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
214        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
215
216        $styles = [
217            /* I18N: An option in a list-box */
218            'list'  => I18N::translate('list'),
219            /* I18N: An option in a list-box */
220            'table' => I18N::translate('table'),
221        ];
222
223        $calendars = [
224            'jewish'    => I18N::translate('Jewish'),
225            'gregorian' => I18N::translate('Gregorian'),
226        ];
227
228        echo view('modules/yahrzeit/config', [
229            'calendar'  => $calendar,
230            'calendars' => $calendars,
231            'days'      => $days,
232            'infoStyle' => $infoStyle,
233            'max_days'  => self::MAX_DAYS,
234            'styles'    => $styles,
235        ]);
236    }
237}
238