xref: /webtrees/app/Module/YahrzeitModule.php (revision 26684e686fb5ab50ecb57e7e6c6a0a55852d2203)
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
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 */
3349a243cbSGreg Roachclass YahrzeitModule extends AbstractModule implements ModuleInterface, ModuleBlockInterface
34c1010edaSGreg Roach{
3549a243cbSGreg Roach    use ModuleBlockTrait;
3649a243cbSGreg Roach
37c385536dSGreg Roach    // Default values for new blocks.
3816d6367aSGreg Roach    private const DEFAULT_CALENDAR = 'jewish';
3916d6367aSGreg Roach    private const DEFAULT_DAYS     = '7';
4016d6367aSGreg Roach    private const DEFAULT_STYLE    = 'table';
41c385536dSGreg Roach
42c385536dSGreg Roach    // Can show this number of days into the future.
4316d6367aSGreg Roach    private const MAX_DAYS = 30;
44c385536dSGreg Roach
45961ec755SGreg Roach    /**
46961ec755SGreg Roach     * How should this module be labelled on tabs, menus, etc.?
47961ec755SGreg Roach     *
48961ec755SGreg Roach     * @return string
49961ec755SGreg Roach     */
5049a243cbSGreg Roach    public function title(): string
51c1010edaSGreg Roach    {
52bbb76c12SGreg Roach        /* I18N: Name of a module. Yahrzeiten (the plural of Yahrzeit) are special anniversaries of deaths in the Hebrew faith/calendar. */
53bbb76c12SGreg Roach        return I18N::translate('Yahrzeiten');
548c2e8227SGreg Roach    }
558c2e8227SGreg Roach
56961ec755SGreg Roach    /**
57961ec755SGreg Roach     * A sentence describing what this module does.
58961ec755SGreg Roach     *
59961ec755SGreg Roach     * @return string
60961ec755SGreg Roach     */
6149a243cbSGreg Roach    public function description(): string
62c1010edaSGreg Roach    {
63bbb76c12SGreg Roach        /* I18N: Description of the “Yahrzeiten” module. A “Hebrew death” is a death where the date is recorded in the Hebrew calendar. */
64bbb76c12SGreg Roach        return I18N::translate('A list of the Hebrew death anniversaries that will occur in the near future.');
658c2e8227SGreg Roach    }
668c2e8227SGreg Roach
6776692c8bSGreg Roach    /**
6876692c8bSGreg Roach     * Generate the HTML content of this block.
6976692c8bSGreg Roach     *
70e490cd80SGreg Roach     * @param Tree     $tree
7176692c8bSGreg Roach     * @param int      $block_id
725f2ae573SGreg Roach     * @param string   $ctype
73727f238cSGreg Roach     * @param string[] $cfg
7476692c8bSGreg Roach     *
7576692c8bSGreg Roach     * @return string
7676692c8bSGreg Roach     */
775f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
78c1010edaSGreg Roach    {
79f0a11419SGreg Roach        $calendar_service = new CalendarService();
80f0a11419SGreg Roach
81e490cd80SGreg Roach        $days      = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
82c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
83c385536dSGreg Roach        $calendar  = $this->getBlockSetting($block_id, 'calendar', self::DEFAULT_CALENDAR);
848c2e8227SGreg Roach
85c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
868c2e8227SGreg Roach
8759f2f229SGreg Roach        $jewish_calendar = new JewishCalendar();
888c2e8227SGreg Roach        $startjd         = WT_CLIENT_JD;
898c2e8227SGreg Roach        $endjd           = WT_CLIENT_JD + $days - 1;
908c2e8227SGreg Roach
918c2e8227SGreg Roach        // The standard anniversary rules cover most of the Yahrzeit rules, we just
928c2e8227SGreg Roach        // need to handle a few special cases.
9389f721acSGreg Roach        // Fetch normal anniversaries, with an extra day before/after
9413abd6f3SGreg Roach        $yahrzeits = [];
958c2e8227SGreg Roach        for ($jd = $startjd - 1; $jd <= $endjd + $days; ++$jd) {
96f0a11419SGreg Roach            foreach ($calendar_service->getAnniversaryEvents($jd, 'DEAT _YART', $tree) as $fact) {
978c2e8227SGreg Roach                // Exact hebrew dates only
982decada7SGreg Roach                $date = $fact->date();
998c2e8227SGreg Roach                if ($date->minimumDate() instanceof JewishDate && $date->minimumJulianDay() === $date->maximumJulianDay()) {
10089f721acSGreg Roach                    // ...then adjust DEAT dates (but not _YART)
10189f721acSGreg Roach                    if ($fact->getTag() === 'DEAT') {
10289f721acSGreg Roach                        $today     = new JewishDate($jd);
1032decada7SGreg Roach                        $hd        = $fact->date()->minimumDate();
1048c2e8227SGreg Roach                        $hd1       = new JewishDate($hd);
1054a83f5d7SGreg Roach                        $hd1->year += 1;
1068c2e8227SGreg Roach                        $hd1->setJdFromYmd();
1078c2e8227SGreg Roach                        // Special rules. See http://www.hebcal.com/help/anniv.html
1088c2e8227SGreg Roach                        // Everything else is taken care of by our standard anniversary rules.
1094a83f5d7SGreg Roach                        if ($hd->day == 30 && $hd->month == 2 && $hd->year != 0 && $hd1->daysInMonth() < 30) {
1108c2e8227SGreg Roach                            // 30 CSH - Last day in CSH
1114a83f5d7SGreg Roach                            $jd = $jewish_calendar->ymdToJd($today->year, 3, 1) - 1;
1124a83f5d7SGreg Roach                        } elseif ($hd->day == 30 && $hd->month == 3 && $hd->year != 0 && $hd1->daysInMonth() < 30) {
1138c2e8227SGreg Roach                            // 30 KSL - Last day in KSL
1144a83f5d7SGreg Roach                            $jd = $jewish_calendar->ymdToJd($today->year, 4, 1) - 1;
1154a83f5d7SGreg Roach                        } elseif ($hd->day == 30 && $hd->month == 6 && $hd->year != 0 && $today->daysInMonth() < 30 && !$today->isLeapYear()) {
1168c2e8227SGreg Roach                            // 30 ADR - Last day in SHV
1174a83f5d7SGreg Roach                            $jd = $jewish_calendar->ymdToJd($today->year, 6, 1) - 1;
11889f721acSGreg Roach                        }
11989f721acSGreg Roach                    }
12089f721acSGreg Roach
12189f721acSGreg Roach                    // Filter adjusted dates to our date range
12289f721acSGreg Roach                    if ($jd >= $startjd && $jd < $startjd + $days) {
12389f721acSGreg Roach                        // upcomming yahrzeit dates
12489f721acSGreg Roach                        switch ($calendar) {
12589f721acSGreg Roach                            case 'gregorian':
12689f721acSGreg Roach                                $yahrzeit_date = new GregorianDate($jd);
12789f721acSGreg Roach                                break;
12889f721acSGreg Roach                            case 'jewish':
12989f721acSGreg Roach                            default:
13089f721acSGreg Roach                                $yahrzeit_date = new JewishDate($jd);
13189f721acSGreg Roach                                break;
13289f721acSGreg Roach                        }
13389f721acSGreg Roach                        $yahrzeit_date = new Date($yahrzeit_date->format('%@ %A %O %E'));
13489f721acSGreg Roach
13589f721acSGreg Roach                        $yahrzeits[] = (object) [
136e7766c08SGreg Roach                            'individual'    => $fact->record(),
1372decada7SGreg Roach                            'fact_date'     => $fact->date(),
13889f721acSGreg Roach                            'fact'          => $fact,
13989f721acSGreg Roach                            'jd'            => $jd,
14089f721acSGreg Roach                            'yahrzeit_date' => $yahrzeit_date,
14189f721acSGreg Roach                        ];
14289f721acSGreg Roach                    }
1438c2e8227SGreg Roach                }
1448c2e8227SGreg Roach            }
1458c2e8227SGreg Roach        }
1468c2e8227SGreg Roach
1478c2e8227SGreg Roach        switch ($infoStyle) {
1488c2e8227SGreg Roach            case 'list':
149147e99aaSGreg Roach                $content = view('modules/yahrzeit/list', [
15089f721acSGreg Roach                    'yahrzeits' => $yahrzeits,
15189f721acSGreg Roach                ]);
1528c2e8227SGreg Roach                break;
1538c2e8227SGreg Roach            case 'table':
1548c2e8227SGreg Roach            default:
155147e99aaSGreg Roach                $content = view('modules/yahrzeit/table', [
15689f721acSGreg Roach                    'yahrzeits' => $yahrzeits,
15789f721acSGreg Roach                ]);
1588c2e8227SGreg Roach                break;
1598c2e8227SGreg Roach        }
1608c2e8227SGreg Roach
1616a8879feSGreg Roach        if ($ctype !== '') {
162e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
163c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
164c1010edaSGreg Roach                    'block_id' => $block_id,
165aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
166c1010edaSGreg Roach                ]);
167397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
168c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
169c1010edaSGreg Roach                    'block_id' => $block_id,
170aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
171c1010edaSGreg Roach                ]);
1728cbbfdceSGreg Roach            } else {
1738cbbfdceSGreg Roach                $config_url = '';
1748cbbfdceSGreg Roach            }
1758cbbfdceSGreg Roach
176147e99aaSGreg Roach            return view('modules/block-template', [
177*26684e68SGreg Roach                'block'      => str_replace('_', '-', $this->name()),
1789c6524dcSGreg Roach                'id'         => $block_id,
179f7f4b984SGreg Roach                'config_url' => $config_url,
18049a243cbSGreg Roach                'title'      => $this->title(),
1819c6524dcSGreg Roach                'content'    => $content,
1829c6524dcSGreg Roach            ]);
1838c2e8227SGreg Roach        }
184b2ce94c6SRico Sonntag
185b2ce94c6SRico Sonntag        return $content;
1868c2e8227SGreg Roach    }
1878c2e8227SGreg Roach
1888c2e8227SGreg Roach    /** {@inheritdoc} */
189c1010edaSGreg Roach    public function loadAjax(): bool
190c1010edaSGreg Roach    {
1918c2e8227SGreg Roach        return true;
1928c2e8227SGreg Roach    }
1938c2e8227SGreg Roach
1948c2e8227SGreg Roach    /** {@inheritdoc} */
195c1010edaSGreg Roach    public function isUserBlock(): bool
196c1010edaSGreg Roach    {
1978c2e8227SGreg Roach        return true;
1988c2e8227SGreg Roach    }
1998c2e8227SGreg Roach
2008c2e8227SGreg Roach    /** {@inheritdoc} */
201c1010edaSGreg Roach    public function isGedcomBlock(): bool
202c1010edaSGreg Roach    {
2038c2e8227SGreg Roach        return true;
2048c2e8227SGreg Roach    }
2058c2e8227SGreg Roach
20676692c8bSGreg Roach    /**
207a45f9889SGreg Roach     * Update the configuration for a block.
208a45f9889SGreg Roach     *
209a45f9889SGreg Roach     * @param Request $request
210a45f9889SGreg Roach     * @param int     $block_id
211a45f9889SGreg Roach     *
212a45f9889SGreg Roach     * @return void
213a45f9889SGreg Roach     */
214a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
215a45f9889SGreg Roach    {
216a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'days', $request->get('days', self::DEFAULT_DAYS));
217a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE));
218a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'calendar', $request->get('calendar', self::DEFAULT_CALENDAR));
219a45f9889SGreg Roach    }
220a45f9889SGreg Roach
221a45f9889SGreg Roach    /**
22276692c8bSGreg Roach     * An HTML form to edit block settings
22376692c8bSGreg Roach     *
224e490cd80SGreg Roach     * @param Tree $tree
22576692c8bSGreg Roach     * @param int  $block_id
226a9430be8SGreg Roach     *
227a9430be8SGreg Roach     * @return void
22876692c8bSGreg Roach     */
229a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
230c1010edaSGreg Roach    {
231e2a378d3SGreg Roach        $calendar  = $this->getBlockSetting($block_id, 'calendar', 'jewish');
232147e99aaSGreg Roach        $days      = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
233c385536dSGreg Roach        $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
2348c2e8227SGreg Roach
235c385536dSGreg Roach        $styles = [
236bbb76c12SGreg Roach            /* I18N: An option in a list-box */
237bbb76c12SGreg Roach            'list'  => I18N::translate('list'),
238bbb76c12SGreg Roach            /* I18N: An option in a list-box */
239bbb76c12SGreg Roach            'table' => I18N::translate('table'),
240c385536dSGreg Roach        ];
2418c2e8227SGreg Roach
242c385536dSGreg Roach        $calendars = [
243c385536dSGreg Roach            'jewish'    => I18N::translate('Jewish'),
244c385536dSGreg Roach            'gregorian' => I18N::translate('Gregorian'),
245c385536dSGreg Roach        ];
2468c2e8227SGreg Roach
247147e99aaSGreg Roach        echo view('modules/yahrzeit/config', [
248c385536dSGreg Roach            'calendar'  => $calendar,
249c385536dSGreg Roach            'calendars' => $calendars,
250c385536dSGreg Roach            'days'      => $days,
251c385536dSGreg Roach            'infoStyle' => $infoStyle,
252c385536dSGreg Roach            'max_days'  => self::MAX_DAYS,
253c385536dSGreg Roach            'styles'    => $styles,
254c385536dSGreg Roach        ]);
2558c2e8227SGreg Roach    }
2568c2e8227SGreg Roach}
257