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