xref: /webtrees/app/Module/YahrzeitModule.php (revision 269fd10d8b81cb5e33a7a34320b7f88f5aec76eb)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
198c2e8227SGreg Roach
20*269fd10dSGreg Roachuse Carbon\Carbon;
218c2e8227SGreg Roachuse Fisharebest\ExtCalendar\JewishCalendar;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JewishDate;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
27f0a11419SGreg Roachuse Fisharebest\Webtrees\Services\CalendarService;
28e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
29a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
308c2e8227SGreg Roach
318c2e8227SGreg Roach/**
328c2e8227SGreg Roach * Class YahrzeitModule
338c2e8227SGreg Roach */
3437eb8894SGreg Roachclass YahrzeitModule extends AbstractModule implements ModuleBlockInterface
35c1010edaSGreg Roach{
3649a243cbSGreg Roach    use ModuleBlockTrait;
3749a243cbSGreg Roach
38c385536dSGreg Roach    // Default values for new blocks.
3916d6367aSGreg Roach    private const DEFAULT_CALENDAR = 'jewish';
4016d6367aSGreg Roach    private const DEFAULT_DAYS     = '7';
4116d6367aSGreg Roach    private const DEFAULT_STYLE    = 'table';
42c385536dSGreg Roach
43c385536dSGreg Roach    // Can show this number of days into the future.
4416d6367aSGreg Roach    private const MAX_DAYS = 30;
45c385536dSGreg Roach
46961ec755SGreg Roach    /**
47961ec755SGreg Roach     * How should this module be labelled on tabs, menus, etc.?
48961ec755SGreg Roach     *
49961ec755SGreg Roach     * @return string
50961ec755SGreg Roach     */
5149a243cbSGreg Roach    public function title(): string
52c1010edaSGreg Roach    {
53bbb76c12SGreg Roach        /* I18N: Name of a module. Yahrzeiten (the plural of Yahrzeit) are special anniversaries of deaths in the Hebrew faith/calendar. */
54bbb76c12SGreg Roach        return I18N::translate('Yahrzeiten');
558c2e8227SGreg Roach    }
568c2e8227SGreg Roach
57961ec755SGreg Roach    /**
58961ec755SGreg Roach     * A sentence describing what this module does.
59961ec755SGreg Roach     *
60961ec755SGreg Roach     * @return string
61961ec755SGreg Roach     */
6249a243cbSGreg Roach    public function description(): string
63c1010edaSGreg Roach    {
64bbb76c12SGreg Roach        /* I18N: Description of the “Yahrzeiten” module. A “Hebrew death” is a death where the date is recorded in the Hebrew calendar. */
65bbb76c12SGreg Roach        return I18N::translate('A list of the Hebrew death anniversaries that will occur in the near future.');
668c2e8227SGreg Roach    }
678c2e8227SGreg Roach
6876692c8bSGreg Roach    /**
6976692c8bSGreg Roach     * Generate the HTML content of this block.
7076692c8bSGreg Roach     *
71e490cd80SGreg Roach     * @param Tree     $tree
7276692c8bSGreg Roach     * @param int      $block_id
735f2ae573SGreg Roach     * @param string   $ctype
74727f238cSGreg Roach     * @param string[] $cfg
7576692c8bSGreg Roach     *
7676692c8bSGreg Roach     * @return string
7776692c8bSGreg Roach     */
785f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
79c1010edaSGreg Roach    {
80f0a11419SGreg Roach        $calendar_service = new CalendarService();
81f0a11419SGreg Roach
82e490cd80SGreg Roach        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
83c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
84c385536dSGreg Roach        $calendar  = $this->getBlockSetting($block_id, 'calendar', self::DEFAULT_CALENDAR);
858c2e8227SGreg Roach
86c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
878c2e8227SGreg Roach
8859f2f229SGreg Roach        $jewish_calendar = new JewishCalendar();
89*269fd10dSGreg Roach        $startjd         = unixtojd(Carbon::now()->timestamp);
90*269fd10dSGreg Roach        $endjd           = $startjd + $days - 1;
918c2e8227SGreg Roach
928c2e8227SGreg Roach        // The standard anniversary rules cover most of the Yahrzeit rules, we just
938c2e8227SGreg Roach        // need to handle a few special cases.
9489f721acSGreg Roach        // Fetch normal anniversaries, with an extra day before/after
9513abd6f3SGreg Roach        $yahrzeits = [];
968c2e8227SGreg Roach        for ($jd = $startjd - 1; $jd <= $endjd + $days; ++$jd) {
97f0a11419SGreg Roach            foreach ($calendar_service->getAnniversaryEvents($jd, 'DEAT _YART', $tree) as $fact) {
988c2e8227SGreg Roach                // Exact hebrew dates only
992decada7SGreg Roach                $date = $fact->date();
1008c2e8227SGreg Roach                if ($date->minimumDate() instanceof JewishDate && $date->minimumJulianDay() === $date->maximumJulianDay()) {
10189f721acSGreg Roach                    // ...then adjust DEAT dates (but not _YART)
10289f721acSGreg Roach                    if ($fact->getTag() === 'DEAT') {
10389f721acSGreg Roach                        $today     = new JewishDate($jd);
1042decada7SGreg Roach                        $hd        = $fact->date()->minimumDate();
1058c2e8227SGreg Roach                        $hd1       = new JewishDate($hd);
1064a83f5d7SGreg Roach                        $hd1->year += 1;
1078c2e8227SGreg Roach                        $hd1->setJdFromYmd();
1088c2e8227SGreg Roach                        // Special rules. See http://www.hebcal.com/help/anniv.html
1098c2e8227SGreg Roach                        // Everything else is taken care of by our standard anniversary rules.
1104a83f5d7SGreg Roach                        if ($hd->day == 30 && $hd->month == 2 && $hd->year != 0 && $hd1->daysInMonth() < 30) {
1118c2e8227SGreg Roach                            // 30 CSH - Last day in CSH
1124a83f5d7SGreg Roach                            $jd = $jewish_calendar->ymdToJd($today->year, 3, 1) - 1;
1134a83f5d7SGreg Roach                        } elseif ($hd->day == 30 && $hd->month == 3 && $hd->year != 0 && $hd1->daysInMonth() < 30) {
1148c2e8227SGreg Roach                            // 30 KSL - Last day in KSL
1154a83f5d7SGreg Roach                            $jd = $jewish_calendar->ymdToJd($today->year, 4, 1) - 1;
1164a83f5d7SGreg Roach                        } elseif ($hd->day == 30 && $hd->month == 6 && $hd->year != 0 && $today->daysInMonth() < 30 && !$today->isLeapYear()) {
1178c2e8227SGreg Roach                            // 30 ADR - Last day in SHV
1184a83f5d7SGreg Roach                            $jd = $jewish_calendar->ymdToJd($today->year, 6, 1) - 1;
11989f721acSGreg Roach                        }
12089f721acSGreg Roach                    }
12189f721acSGreg Roach
12289f721acSGreg Roach                    // Filter adjusted dates to our date range
12389f721acSGreg Roach                    if ($jd >= $startjd && $jd < $startjd + $days) {
12489f721acSGreg Roach                        // upcomming yahrzeit dates
12589f721acSGreg Roach                        switch ($calendar) {
12689f721acSGreg Roach                            case 'gregorian':
12789f721acSGreg Roach                                $yahrzeit_date = new GregorianDate($jd);
12889f721acSGreg Roach                                break;
12989f721acSGreg Roach                            case 'jewish':
13089f721acSGreg Roach                            default:
13189f721acSGreg Roach                                $yahrzeit_date = new JewishDate($jd);
13289f721acSGreg Roach                                break;
13389f721acSGreg Roach                        }
13489f721acSGreg Roach                        $yahrzeit_date = new Date($yahrzeit_date->format('%@ %A %O %E'));
13589f721acSGreg Roach
13689f721acSGreg Roach                        $yahrzeits[] = (object) [
137e7766c08SGreg Roach                            'individual'    => $fact->record(),
1382decada7SGreg Roach                            'fact_date'     => $fact->date(),
13989f721acSGreg Roach                            'fact'          => $fact,
14089f721acSGreg Roach                            'jd'            => $jd,
14189f721acSGreg Roach                            'yahrzeit_date' => $yahrzeit_date,
14289f721acSGreg Roach                        ];
14389f721acSGreg Roach                    }
1448c2e8227SGreg Roach                }
1458c2e8227SGreg Roach            }
1468c2e8227SGreg Roach        }
1478c2e8227SGreg Roach
1488c2e8227SGreg Roach        switch ($infoStyle) {
1498c2e8227SGreg Roach            case 'list':
150147e99aaSGreg Roach                $content = view('modules/yahrzeit/list', [
15189f721acSGreg Roach                    'yahrzeits' => $yahrzeits,
15289f721acSGreg Roach                ]);
1538c2e8227SGreg Roach                break;
1548c2e8227SGreg Roach            case 'table':
1558c2e8227SGreg Roach            default:
156147e99aaSGreg Roach                $content = view('modules/yahrzeit/table', [
15789f721acSGreg Roach                    'yahrzeits' => $yahrzeits,
15889f721acSGreg Roach                ]);
1598c2e8227SGreg Roach                break;
1608c2e8227SGreg Roach        }
1618c2e8227SGreg Roach
1626a8879feSGreg Roach        if ($ctype !== '') {
163e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
164c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
165c1010edaSGreg Roach                    'block_id' => $block_id,
166aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
167c1010edaSGreg Roach                ]);
168397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
169c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
170c1010edaSGreg Roach                    'block_id' => $block_id,
171aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
172c1010edaSGreg Roach                ]);
1738cbbfdceSGreg Roach            } else {
1748cbbfdceSGreg Roach                $config_url = '';
1758cbbfdceSGreg Roach            }
1768cbbfdceSGreg Roach
177147e99aaSGreg Roach            return view('modules/block-template', [
17826684e68SGreg Roach                'block'      => str_replace('_', '-', $this->name()),
1799c6524dcSGreg Roach                'id'         => $block_id,
180f7f4b984SGreg Roach                'config_url' => $config_url,
18149a243cbSGreg Roach                'title'      => $this->title(),
1829c6524dcSGreg Roach                'content'    => $content,
1839c6524dcSGreg Roach            ]);
1848c2e8227SGreg Roach        }
185b2ce94c6SRico Sonntag
186b2ce94c6SRico Sonntag        return $content;
1878c2e8227SGreg Roach    }
1888c2e8227SGreg Roach
1898c2e8227SGreg Roach    /** {@inheritdoc} */
190c1010edaSGreg Roach    public function loadAjax(): bool
191c1010edaSGreg Roach    {
1928c2e8227SGreg Roach        return true;
1938c2e8227SGreg Roach    }
1948c2e8227SGreg Roach
1958c2e8227SGreg Roach    /** {@inheritdoc} */
196c1010edaSGreg Roach    public function isUserBlock(): bool
197c1010edaSGreg Roach    {
1988c2e8227SGreg Roach        return true;
1998c2e8227SGreg Roach    }
2008c2e8227SGreg Roach
2018c2e8227SGreg Roach    /** {@inheritdoc} */
20263276d8fSGreg Roach    public function isTreeBlock(): bool
203c1010edaSGreg Roach    {
2048c2e8227SGreg Roach        return true;
2058c2e8227SGreg Roach    }
2068c2e8227SGreg Roach
20776692c8bSGreg Roach    /**
208a45f9889SGreg Roach     * Update the configuration for a block.
209a45f9889SGreg Roach     *
210a45f9889SGreg Roach     * @param Request $request
211a45f9889SGreg Roach     * @param int     $block_id
212a45f9889SGreg Roach     *
213a45f9889SGreg Roach     * @return void
214a45f9889SGreg Roach     */
215a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
216a45f9889SGreg Roach    {
217a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'days', $request->get('days', self::DEFAULT_DAYS));
218a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE));
219a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'calendar', $request->get('calendar', self::DEFAULT_CALENDAR));
220a45f9889SGreg Roach    }
221a45f9889SGreg Roach
222a45f9889SGreg Roach    /**
22376692c8bSGreg Roach     * An HTML form to edit block settings
22476692c8bSGreg Roach     *
225e490cd80SGreg Roach     * @param Tree $tree
22676692c8bSGreg Roach     * @param int  $block_id
227a9430be8SGreg Roach     *
228a9430be8SGreg Roach     * @return void
22976692c8bSGreg Roach     */
230a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
231c1010edaSGreg Roach    {
232e2a378d3SGreg Roach        $calendar  = $this->getBlockSetting($block_id, 'calendar', 'jewish');
233147e99aaSGreg Roach        $days      = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
234c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
2358c2e8227SGreg Roach
236c385536dSGreg Roach        $styles = [
237bbb76c12SGreg Roach            /* I18N: An option in a list-box */
238bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
239bbb76c12SGreg Roach            /* I18N: An option in a list-box */
240bbb76c12SGreg Roach            'table' => I18N::translate('table'),
241c385536dSGreg Roach        ];
2428c2e8227SGreg Roach
243c385536dSGreg Roach        $calendars = [
244c385536dSGreg Roach            'jewish'    => I18N::translate('Jewish'),
245c385536dSGreg Roach            'gregorian' => I18N::translate('Gregorian'),
246c385536dSGreg Roach        ];
2478c2e8227SGreg Roach
248147e99aaSGreg Roach        echo view('modules/yahrzeit/config', [
249c385536dSGreg Roach            'calendar'  => $calendar,
250c385536dSGreg Roach            'calendars' => $calendars,
251c385536dSGreg Roach            'days'      => $days,
252c385536dSGreg Roach            'infoStyle' => $infoStyle,
253c385536dSGreg Roach            'max_days'  => self::MAX_DAYS,
254c385536dSGreg Roach            'styles'    => $styles,
255c385536dSGreg Roach        ]);
2568c2e8227SGreg Roach    }
2578c2e8227SGreg Roach}
258