xref: /webtrees/app/Module/YahrzeitModule.php (revision 556648010e8842a7736e41d58a268c4da22e9d34)
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 */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
178c2e8227SGreg Roach
188c2e8227SGreg Roachuse Fisharebest\ExtCalendar\JewishCalendar;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JewishDate;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
24f0a11419SGreg Roachuse Fisharebest\Webtrees\Services\CalendarService;
25e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
26a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
278c2e8227SGreg Roach
288c2e8227SGreg Roach/**
298c2e8227SGreg Roach * Class YahrzeitModule
308c2e8227SGreg Roach */
31c1010edaSGreg Roachclass YahrzeitModule extends AbstractModule implements ModuleBlockInterface
32c1010edaSGreg Roach{
33c385536dSGreg Roach    // Default values for new blocks.
34c385536dSGreg Roach    const DEFAULT_CALENDAR = 'jewish';
35*55664801SGreg Roach    const DEFAULT_DAYS     = '7';
36c385536dSGreg Roach    const DEFAULT_STYLE    = 'table';
37c385536dSGreg Roach
38c385536dSGreg Roach    // Can show this number of days into the future.
39c385536dSGreg Roach    const MAX_DAYS = 30;
40c385536dSGreg Roach
418c2e8227SGreg Roach    /** {@inheritdoc} */
428f53f488SRico Sonntag    public function getTitle(): string
43c1010edaSGreg Roach    {
44bbb76c12SGreg Roach        /* I18N: Name of a module. Yahrzeiten (the plural of Yahrzeit) are special anniversaries of deaths in the Hebrew faith/calendar. */
45bbb76c12SGreg Roach        return I18N::translate('Yahrzeiten');
468c2e8227SGreg Roach    }
478c2e8227SGreg Roach
488c2e8227SGreg Roach    /** {@inheritdoc} */
498f53f488SRico Sonntag    public function getDescription(): string
50c1010edaSGreg Roach    {
51bbb76c12SGreg Roach        /* I18N: Description of the “Yahrzeiten” module. A “Hebrew death” is a death where the date is recorded in the Hebrew calendar. */
52bbb76c12SGreg Roach        return I18N::translate('A list of the Hebrew death anniversaries that will occur in the near future.');
538c2e8227SGreg Roach    }
548c2e8227SGreg Roach
5576692c8bSGreg Roach    /**
5676692c8bSGreg Roach     * Generate the HTML content of this block.
5776692c8bSGreg Roach     *
58e490cd80SGreg Roach     * @param Tree     $tree
5976692c8bSGreg Roach     * @param int      $block_id
6076692c8bSGreg Roach     * @param bool     $template
61727f238cSGreg Roach     * @param string[] $cfg
6276692c8bSGreg Roach     *
6376692c8bSGreg Roach     * @return string
6476692c8bSGreg Roach     */
65c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
66c1010edaSGreg Roach    {
67e490cd80SGreg Roach        global $ctype;
688c2e8227SGreg 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
888c2e8227SGreg Roach                $date = $fact->getDate();
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);
9389f721acSGreg Roach                        $hd     = $fact->getDate()->minimumDate();
948c2e8227SGreg Roach                        $hd1    = new JewishDate($hd);
958c2e8227SGreg Roach                        $hd1->y += 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.
998c2e8227SGreg Roach                        if ($hd->d == 30 && $hd->m == 2 && $hd->y != 0 && $hd1->daysInMonth() < 30) {
1008c2e8227SGreg Roach                            // 30 CSH - Last day in CSH
10189f721acSGreg Roach                            $jd = $jewish_calendar->ymdToJd($today->y, 3, 1) - 1;
1028c2e8227SGreg Roach                        } elseif ($hd->d == 30 && $hd->m == 3 && $hd->y != 0 && $hd1->daysInMonth() < 30) {
1038c2e8227SGreg Roach                            // 30 KSL - Last day in KSL
10489f721acSGreg Roach                            $jd = $jewish_calendar->ymdToJd($today->y, 4, 1) - 1;
1058c2e8227SGreg Roach                        } elseif ($hd->d == 30 && $hd->m == 6 && $hd->y != 0 && $today->daysInMonth() < 30 && !$today->isLeapYear()) {
1068c2e8227SGreg Roach                            // 30 ADR - Last day in SHV
10789f721acSGreg Roach                            $jd = $jewish_calendar->ymdToJd($today->y, 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)[
12689f721acSGreg Roach                            'individual'    => $fact->getParent(),
12789f721acSGreg Roach                            'fact_date'     => $fact->getDate(),
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
1518c2e8227SGreg Roach        if ($template) {
152e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
153c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
154c1010edaSGreg Roach                    'block_id' => $block_id,
155c1010edaSGreg Roach                    'ged'      => $tree->getName(),
156c1010edaSGreg Roach                ]);
157397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
158c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
159c1010edaSGreg Roach                    'block_id' => $block_id,
160c1010edaSGreg Roach                    'ged'      => $tree->getName(),
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        } else {
1748c2e8227SGreg Roach            return $content;
1758c2e8227SGreg Roach        }
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