xref: /webtrees/app/Module/YahrzeitModule.php (revision 6a8879fe9c08e13848197f33eb77fde0b2371399)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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
208c2e8227SGreg Roachuse Fisharebest\ExtCalendar\JewishCalendar;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JewishDate;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
26f0a11419SGreg Roachuse Fisharebest\Webtrees\Services\CalendarService;
27e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
28a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
298c2e8227SGreg Roach
308c2e8227SGreg Roach/**
318c2e8227SGreg Roach * Class YahrzeitModule
328c2e8227SGreg Roach */
33c1010edaSGreg Roachclass YahrzeitModule extends AbstractModule implements ModuleBlockInterface
34c1010edaSGreg Roach{
35c385536dSGreg Roach    // Default values for new blocks.
36c385536dSGreg Roach    const DEFAULT_CALENDAR = 'jewish';
3755664801SGreg Roach    const DEFAULT_DAYS     = '7';
38c385536dSGreg Roach    const DEFAULT_STYLE    = 'table';
39c385536dSGreg Roach
40c385536dSGreg Roach    // Can show this number of days into the future.
41c385536dSGreg Roach    const MAX_DAYS = 30;
42c385536dSGreg Roach
438c2e8227SGreg Roach    /** {@inheritdoc} */
448f53f488SRico Sonntag    public function getTitle(): string
45c1010edaSGreg Roach    {
46bbb76c12SGreg Roach        /* I18N: Name of a module. Yahrzeiten (the plural of Yahrzeit) are special anniversaries of deaths in the Hebrew faith/calendar. */
47bbb76c12SGreg Roach        return I18N::translate('Yahrzeiten');
488c2e8227SGreg Roach    }
498c2e8227SGreg Roach
508c2e8227SGreg Roach    /** {@inheritdoc} */
518f53f488SRico Sonntag    public function getDescription(): string
52c1010edaSGreg Roach    {
53bbb76c12SGreg Roach        /* I18N: Description of the “Yahrzeiten” module. A “Hebrew death” is a death where the date is recorded in the Hebrew calendar. */
54bbb76c12SGreg Roach        return I18N::translate('A list of the Hebrew death anniversaries that will occur in the near future.');
558c2e8227SGreg Roach    }
568c2e8227SGreg Roach
5776692c8bSGreg Roach    /**
5876692c8bSGreg Roach     * Generate the HTML content of this block.
5976692c8bSGreg Roach     *
60e490cd80SGreg Roach     * @param Tree     $tree
6176692c8bSGreg Roach     * @param int      $block_id
625f2ae573SGreg Roach     * @param string   $ctype
63727f238cSGreg Roach     * @param string[] $cfg
6476692c8bSGreg Roach     *
6576692c8bSGreg Roach     * @return string
6676692c8bSGreg Roach     */
675f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
68c1010edaSGreg Roach    {
69f0a11419SGreg Roach        $calendar_service = new CalendarService();
70f0a11419SGreg Roach
71e490cd80SGreg Roach        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
72c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
73c385536dSGreg Roach        $calendar  = $this->getBlockSetting($block_id, 'calendar', self::DEFAULT_CALENDAR);
748c2e8227SGreg Roach
75c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
768c2e8227SGreg Roach
7759f2f229SGreg Roach        $jewish_calendar = new JewishCalendar();
788c2e8227SGreg Roach        $startjd         = WT_CLIENT_JD;
798c2e8227SGreg Roach        $endjd           = WT_CLIENT_JD + $days - 1;
808c2e8227SGreg Roach
818c2e8227SGreg Roach        // The standard anniversary rules cover most of the Yahrzeit rules, we just
828c2e8227SGreg Roach        // need to handle a few special cases.
8389f721acSGreg Roach        // Fetch normal anniversaries, with an extra day before/after
8413abd6f3SGreg Roach        $yahrzeits = [];
858c2e8227SGreg Roach        for ($jd = $startjd - 1; $jd <= $endjd + $days; ++$jd) {
86f0a11419SGreg Roach            foreach ($calendar_service->getAnniversaryEvents($jd, 'DEAT _YART', $tree) as $fact) {
878c2e8227SGreg Roach                // Exact hebrew dates only
882decada7SGreg Roach                $date = $fact->date();
898c2e8227SGreg Roach                if ($date->minimumDate() instanceof JewishDate && $date->minimumJulianDay() === $date->maximumJulianDay()) {
9089f721acSGreg Roach                    // ...then adjust DEAT dates (but not _YART)
9189f721acSGreg Roach                    if ($fact->getTag() === 'DEAT') {
9289f721acSGreg Roach                        $today     = new JewishDate($jd);
932decada7SGreg Roach                        $hd        = $fact->date()->minimumDate();
948c2e8227SGreg Roach                        $hd1       = new JewishDate($hd);
954a83f5d7SGreg Roach                        $hd1->year += 1;
968c2e8227SGreg Roach                        $hd1->setJdFromYmd();
978c2e8227SGreg Roach                        // Special rules. See http://www.hebcal.com/help/anniv.html
988c2e8227SGreg Roach                        // Everything else is taken care of by our standard anniversary rules.
994a83f5d7SGreg Roach                        if ($hd->day == 30 && $hd->month == 2 && $hd->year != 0 && $hd1->daysInMonth() < 30) {
1008c2e8227SGreg Roach                            // 30 CSH - Last day in CSH
1014a83f5d7SGreg Roach                            $jd = $jewish_calendar->ymdToJd($today->year, 3, 1) - 1;
1024a83f5d7SGreg Roach                        } elseif ($hd->day == 30 && $hd->month == 3 && $hd->year != 0 && $hd1->daysInMonth() < 30) {
1038c2e8227SGreg Roach                            // 30 KSL - Last day in KSL
1044a83f5d7SGreg Roach                            $jd = $jewish_calendar->ymdToJd($today->year, 4, 1) - 1;
1054a83f5d7SGreg Roach                        } elseif ($hd->day == 30 && $hd->month == 6 && $hd->year != 0 && $today->daysInMonth() < 30 && !$today->isLeapYear()) {
1068c2e8227SGreg Roach                            // 30 ADR - Last day in SHV
1074a83f5d7SGreg Roach                            $jd = $jewish_calendar->ymdToJd($today->year, 6, 1) - 1;
10889f721acSGreg Roach                        }
10989f721acSGreg Roach                    }
11089f721acSGreg Roach
11189f721acSGreg Roach                    // Filter adjusted dates to our date range
11289f721acSGreg Roach                    if ($jd >= $startjd && $jd < $startjd + $days) {
11389f721acSGreg Roach                        // upcomming yahrzeit dates
11489f721acSGreg Roach                        switch ($calendar) {
11589f721acSGreg Roach                            case 'gregorian':
11689f721acSGreg Roach                                $yahrzeit_date = new GregorianDate($jd);
11789f721acSGreg Roach                                break;
11889f721acSGreg Roach                            case 'jewish':
11989f721acSGreg Roach                            default:
12089f721acSGreg Roach                                $yahrzeit_date = new JewishDate($jd);
12189f721acSGreg Roach                                break;
12289f721acSGreg Roach                        }
12389f721acSGreg Roach                        $yahrzeit_date = new Date($yahrzeit_date->format('%@ %A %O %E'));
12489f721acSGreg Roach
12589f721acSGreg Roach                        $yahrzeits[] = (object) [
126e7766c08SGreg Roach                            'individual'    => $fact->record(),
1272decada7SGreg Roach                            'fact_date'     => $fact->date(),
12889f721acSGreg Roach                            'fact'          => $fact,
12989f721acSGreg Roach                            'jd'            => $jd,
13089f721acSGreg Roach                            'yahrzeit_date' => $yahrzeit_date,
13189f721acSGreg Roach                        ];
13289f721acSGreg Roach                    }
1338c2e8227SGreg Roach                }
1348c2e8227SGreg Roach            }
1358c2e8227SGreg Roach        }
1368c2e8227SGreg Roach
1378c2e8227SGreg Roach        switch ($infoStyle) {
1388c2e8227SGreg Roach            case 'list':
139147e99aaSGreg Roach                $content = view('modules/yahrzeit/list', [
14089f721acSGreg Roach                    'yahrzeits' => $yahrzeits,
14189f721acSGreg Roach                ]);
1428c2e8227SGreg Roach                break;
1438c2e8227SGreg Roach            case 'table':
1448c2e8227SGreg Roach            default:
145147e99aaSGreg Roach                $content = view('modules/yahrzeit/table', [
14689f721acSGreg Roach                    'yahrzeits' => $yahrzeits,
14789f721acSGreg Roach                ]);
1488c2e8227SGreg Roach                break;
1498c2e8227SGreg Roach        }
1508c2e8227SGreg Roach
151*6a8879feSGreg Roach        if ($ctype !== '') {
152e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
153c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
154c1010edaSGreg Roach                    'block_id' => $block_id,
155aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
156c1010edaSGreg Roach                ]);
157397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
158c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
159c1010edaSGreg Roach                    'block_id' => $block_id,
160aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
161c1010edaSGreg Roach                ]);
1628cbbfdceSGreg Roach            } else {
1638cbbfdceSGreg Roach                $config_url = '';
1648cbbfdceSGreg Roach            }
1658cbbfdceSGreg Roach
166147e99aaSGreg Roach            return view('modules/block-template', [
1679c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1689c6524dcSGreg Roach                'id'         => $block_id,
169f7f4b984SGreg Roach                'config_url' => $config_url,
1709c6524dcSGreg Roach                'title'      => $this->getTitle(),
1719c6524dcSGreg Roach                'content'    => $content,
1729c6524dcSGreg Roach            ]);
1738c2e8227SGreg Roach        }
174b2ce94c6SRico Sonntag
175b2ce94c6SRico Sonntag        return $content;
1768c2e8227SGreg Roach    }
1778c2e8227SGreg Roach
1788c2e8227SGreg Roach    /** {@inheritdoc} */
179c1010edaSGreg Roach    public function loadAjax(): bool
180c1010edaSGreg Roach    {
1818c2e8227SGreg Roach        return true;
1828c2e8227SGreg Roach    }
1838c2e8227SGreg Roach
1848c2e8227SGreg Roach    /** {@inheritdoc} */
185c1010edaSGreg Roach    public function isUserBlock(): bool
186c1010edaSGreg Roach    {
1878c2e8227SGreg Roach        return true;
1888c2e8227SGreg Roach    }
1898c2e8227SGreg Roach
1908c2e8227SGreg Roach    /** {@inheritdoc} */
191c1010edaSGreg Roach    public function isGedcomBlock(): bool
192c1010edaSGreg Roach    {
1938c2e8227SGreg Roach        return true;
1948c2e8227SGreg Roach    }
1958c2e8227SGreg Roach
19676692c8bSGreg Roach    /**
197a45f9889SGreg Roach     * Update the configuration for a block.
198a45f9889SGreg Roach     *
199a45f9889SGreg Roach     * @param Request $request
200a45f9889SGreg Roach     * @param int     $block_id
201a45f9889SGreg Roach     *
202a45f9889SGreg Roach     * @return void
203a45f9889SGreg Roach     */
204a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
205a45f9889SGreg Roach    {
206a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'days', $request->get('days', self::DEFAULT_DAYS));
207a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE));
208a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'calendar', $request->get('calendar', self::DEFAULT_CALENDAR));
209a45f9889SGreg Roach    }
210a45f9889SGreg Roach
211a45f9889SGreg Roach    /**
21276692c8bSGreg Roach     * An HTML form to edit block settings
21376692c8bSGreg Roach     *
214e490cd80SGreg Roach     * @param Tree $tree
21576692c8bSGreg Roach     * @param int  $block_id
216a9430be8SGreg Roach     *
217a9430be8SGreg Roach     * @return void
21876692c8bSGreg Roach     */
219a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
220c1010edaSGreg Roach    {
221e2a378d3SGreg Roach        $calendar  = $this->getBlockSetting($block_id, 'calendar', 'jewish');
222147e99aaSGreg Roach        $days      = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
223c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
2248c2e8227SGreg Roach
225c385536dSGreg Roach        $styles = [
226bbb76c12SGreg Roach            /* I18N: An option in a list-box */
227bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
228bbb76c12SGreg Roach            /* I18N: An option in a list-box */
229bbb76c12SGreg Roach            'table' => I18N::translate('table'),
230c385536dSGreg Roach        ];
2318c2e8227SGreg Roach
232c385536dSGreg Roach        $calendars = [
233c385536dSGreg Roach            'jewish'    => I18N::translate('Jewish'),
234c385536dSGreg Roach            'gregorian' => I18N::translate('Gregorian'),
235c385536dSGreg Roach        ];
2368c2e8227SGreg Roach
237147e99aaSGreg Roach        echo view('modules/yahrzeit/config', [
238c385536dSGreg Roach            'calendar'  => $calendar,
239c385536dSGreg Roach            'calendars' => $calendars,
240c385536dSGreg Roach            'days'      => $days,
241c385536dSGreg Roach            'infoStyle' => $infoStyle,
242c385536dSGreg Roach            'max_days'  => self::MAX_DAYS,
243c385536dSGreg Roach            'styles'    => $styles,
244c385536dSGreg Roach        ]);
2458c2e8227SGreg Roach    }
2468c2e8227SGreg Roach}
247