1f0a11419SGreg Roach<?php 23976b470SGreg Roach 3f0a11419SGreg Roach/** 4f0a11419SGreg Roach * webtrees: online genealogy 5a091ac74SGreg Roach * Copyright (C) 2020 webtrees development team 6f0a11419SGreg Roach * This program is free software: you can redistribute it and/or modify 7f0a11419SGreg Roach * it under the terms of the GNU General Public License as published by 8f0a11419SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9f0a11419SGreg Roach * (at your option) any later version. 10f0a11419SGreg Roach * This program is distributed in the hope that it will be useful, 11f0a11419SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12f0a11419SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13f0a11419SGreg Roach * GNU General Public License for more details. 14f0a11419SGreg Roach * You should have received a copy of the GNU General Public License 15f0a11419SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 16f0a11419SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 20f0a11419SGreg Roachnamespace Fisharebest\Webtrees\Services; 21f0a11419SGreg Roach 224a9ac934SGreg Roachuse Fisharebest\ExtCalendar\PersianCalendar; 23b00cb080SGreg Roachuse Fisharebest\Webtrees\Carbon; 24f0a11419SGreg Roachuse Fisharebest\Webtrees\Date; 2525d4b688SGreg Roachuse Fisharebest\Webtrees\Date\AbstractCalendarDate; 26f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\FrenchDate; 27f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate; 28f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\HijriDate; 29f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\JalaliDate; 30f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\JewishDate; 31f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\JulianDate; 32f0a11419SGreg Roachuse Fisharebest\Webtrees\Fact; 33a091ac74SGreg Roachuse Fisharebest\Webtrees\Factory; 34f0a11419SGreg Roachuse Fisharebest\Webtrees\Family; 35f0a11419SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 36f0a11419SGreg Roachuse Fisharebest\Webtrees\Individual; 37f0a11419SGreg Roachuse Fisharebest\Webtrees\Tree; 389e87c1a2SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 399e87c1a2SGreg Roachuse Illuminate\Database\Query\Builder; 40a69f5655SGreg Roachuse Illuminate\Database\Query\Expression; 419e87c1a2SGreg Roachuse Illuminate\Database\Query\JoinClause; 428af3e5c1SGreg Roachuse Illuminate\Support\Collection; 433976b470SGreg Roach 440195b0ddSGreg Roachuse function array_merge; 450195b0ddSGreg Roachuse function in_array; 460a614d28SGreg Roachuse function preg_match_all; 470a614d28SGreg Roachuse function range; 48f0a11419SGreg Roach 49f0a11419SGreg Roach/** 50f0a11419SGreg Roach * Calculate anniversaries, etc. 51f0a11419SGreg Roach */ 52f0a11419SGreg Roachclass CalendarService 53f0a11419SGreg Roach{ 540a614d28SGreg Roach // If no facts specified, get all except these 550a614d28SGreg Roach protected const SKIP_FACTS = ['CHAN', 'BAPL', 'SLGC', 'SLGS', 'ENDL', 'CENS', 'RESI', 'NOTE', 'ADDR', 'OBJE', 'SOUR', '_TODO']; 560a614d28SGreg Roach 57f0a11419SGreg Roach /** 5870d8159dSGreg Roach * List all the months in a given year. 5970d8159dSGreg Roach * 6070d8159dSGreg Roach * @param string $calendar 6170d8159dSGreg Roach * @param int $year 6270d8159dSGreg Roach * 6370d8159dSGreg Roach * @return string[] 6470d8159dSGreg Roach */ 6570d8159dSGreg Roach public function calendarMonthsInYear(string $calendar, int $year): array 6670d8159dSGreg Roach { 6770d8159dSGreg Roach $date = new Date($calendar . ' ' . $year); 6870d8159dSGreg Roach $calendar_date = $date->minimumDate(); 6970d8159dSGreg Roach $month_numbers = range(1, $calendar_date->monthsInYear()); 7070d8159dSGreg Roach $month_names = []; 7170d8159dSGreg Roach 7270d8159dSGreg Roach foreach ($month_numbers as $month_number) { 734a83f5d7SGreg Roach $calendar_date->day = 1; 744a83f5d7SGreg Roach $calendar_date->month = $month_number; 7570d8159dSGreg Roach $calendar_date->setJdFromYmd(); 7670d8159dSGreg Roach 7770d8159dSGreg Roach if ($month_number === 6 && $calendar_date instanceof JewishDate && !$calendar_date->isLeapYear()) { 7870d8159dSGreg Roach // No month 6 in Jewish non-leap years. 7970d8159dSGreg Roach continue; 8070d8159dSGreg Roach } 8170d8159dSGreg Roach 8270d8159dSGreg Roach if ($month_number === 7 && $calendar_date instanceof JewishDate && !$calendar_date->isLeapYear()) { 8370d8159dSGreg Roach // Month 7 is ADR in Jewish non-leap years (and ADS in others). 8470d8159dSGreg Roach $mon = 'ADR'; 8570d8159dSGreg Roach } else { 8670d8159dSGreg Roach $mon = $calendar_date->format('%O'); 8770d8159dSGreg Roach } 8870d8159dSGreg Roach 8970d8159dSGreg Roach $month_names[$mon] = $calendar_date->format('%F'); 9070d8159dSGreg Roach } 9170d8159dSGreg Roach 9270d8159dSGreg Roach return $month_names; 9370d8159dSGreg Roach } 9470d8159dSGreg Roach 9570d8159dSGreg Roach /** 96f0a11419SGreg Roach * Get a list of events which occured during a given date range. 97f0a11419SGreg Roach * 98f0a11419SGreg Roach * @param int $jd1 the start range of julian day 99f0a11419SGreg Roach * @param int $jd2 the end range of julian day 1000a614d28SGreg Roach * @param string $facts restrict the search to just these facts or leave blank for all 101f0a11419SGreg Roach * @param Tree $tree the tree to search 102b00cb080SGreg Roach * @param string $filterof filter by living/recent 103b00cb080SGreg Roach * @param string $filtersx filter by sex 104f0a11419SGreg Roach * 105f0a11419SGreg Roach * @return Fact[] 106f0a11419SGreg Roach */ 107b00cb080SGreg Roach public function getCalendarEvents(int $jd1, int $jd2, string $facts, Tree $tree, string $filterof = '', string $filtersx = ''): array 108f0a11419SGreg Roach { 109f0a11419SGreg Roach // Events that start or end during the period 11025d4b688SGreg Roach $query = DB::table('dates') 1119e87c1a2SGreg Roach ->where('d_file', '=', $tree->id()) 1120b5fd0a6SGreg Roach ->where(static function (Builder $query) use ($jd1, $jd2): void { 1130b5fd0a6SGreg Roach $query->where(static function (Builder $query) use ($jd1, $jd2): void { 1149e87c1a2SGreg Roach $query 1159e87c1a2SGreg Roach ->where('d_julianday1', '>=', $jd1) 1169e87c1a2SGreg Roach ->where('d_julianday1', '<=', $jd2); 1170b5fd0a6SGreg Roach })->orWhere(static function (Builder $query) use ($jd1, $jd2): void { 1189e87c1a2SGreg Roach $query 1199e87c1a2SGreg Roach ->where('d_julianday2', '>=', $jd1) 1209e87c1a2SGreg Roach ->where('d_julianday2', '<=', $jd2); 1219e87c1a2SGreg Roach }); 1229e87c1a2SGreg Roach }); 123f0a11419SGreg Roach 124f0a11419SGreg Roach // Restrict to certain types of fact 1250a614d28SGreg Roach if ($facts === '') { 1260a614d28SGreg Roach $query->whereNotIn('d_fact', self::SKIP_FACTS); 127f0a11419SGreg Roach } else { 1280a614d28SGreg Roach preg_match_all('/([_A-Z]+)/', $facts, $matches); 1290a614d28SGreg Roach 1300a614d28SGreg Roach $query->whereIn('d_fact', $matches[1]); 131f0a11419SGreg Roach } 132f0a11419SGreg Roach 133b00cb080SGreg Roach if ($filterof === 'recent') { 134b00cb080SGreg Roach $query->where('d_julianday1', '>=', Carbon::now()->subYears(100)->julianDay()); 135b00cb080SGreg Roach } 136b00cb080SGreg Roach 13725d4b688SGreg Roach $ind_query = (clone $query) 1380b5fd0a6SGreg Roach ->join('individuals', static function (JoinClause $join): void { 13925d4b688SGreg Roach $join->on('d_gid', '=', 'i_id')->on('d_file', '=', 'i_file'); 14025d4b688SGreg Roach }) 14125d4b688SGreg Roach ->select(['i_id AS xref', 'i_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact', 'd_type']); 142f0a11419SGreg Roach 143b00cb080SGreg Roach $queries = ['INDI' => $ind_query]; 144b00cb080SGreg Roach 145b00cb080SGreg Roach if ($filtersx === '') { 14625d4b688SGreg Roach $fam_query = (clone $query) 1470b5fd0a6SGreg Roach ->join('families', static function (JoinClause $join): void { 14825d4b688SGreg Roach $join->on('d_gid', '=', 'f_id')->on('d_file', '=', 'f_file'); 14925d4b688SGreg Roach }) 15025d4b688SGreg Roach ->select(['f_id AS xref', 'f_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact', 'd_type']); 15125d4b688SGreg Roach 152b00cb080SGreg Roach $queries['FAM'] = $fam_query; 153b00cb080SGreg Roach } else { 154b00cb080SGreg Roach $queries['INDI']->where('i_sex', '=', $filtersx); 155b00cb080SGreg Roach } 156b00cb080SGreg Roach 15725d4b688SGreg Roach // Now fetch these events 15825d4b688SGreg Roach $found_facts = []; 15925d4b688SGreg Roach 160b00cb080SGreg Roach foreach ($queries as $type => $record_query) { 16161b2e6eeSGreg Roach foreach ($record_query->get() as $row) { 162f0a11419SGreg Roach if ($type === 'INDI') { 163a091ac74SGreg Roach $record = Factory::individual()->make($row->xref, $tree, $row->gedcom); 164b00cb080SGreg Roach assert($record instanceof Individual); 165b00cb080SGreg Roach 166b00cb080SGreg Roach if ($filterof === 'living' && $record->isDead()) { 167b00cb080SGreg Roach continue; 168b00cb080SGreg Roach } 169f0a11419SGreg Roach } else { 170a091ac74SGreg Roach $record = Factory::family()->make($row->xref, $tree, $row->gedcom); 171b00cb080SGreg Roach assert($record instanceof Family); 172b00cb080SGreg Roach $husb = $record->husband(); 173b00cb080SGreg Roach $wife = $record->wife(); 174b00cb080SGreg Roach 175b00cb080SGreg Roach if ($filterof === 'living' && ($husb && $husb->isDead() || $wife && $wife->isDead())) { 176b00cb080SGreg Roach continue; 177b00cb080SGreg Roach } 178f0a11419SGreg Roach } 17925d4b688SGreg Roach 180f0a11419SGreg Roach $anniv_date = new Date($row->d_type . ' ' . $row->d_day . ' ' . $row->d_month . ' ' . $row->d_year); 18125d4b688SGreg Roach 182b00cb080SGreg Roach foreach ($record->facts([$row->d_fact]) as $fact) { 183f0a11419SGreg Roach // For date ranges, we need a match on either the start/end. 184b00cb080SGreg Roach if (($fact->date()->minimumJulianDay() === $anniv_date->minimumJulianDay() || $fact->date()->maximumJulianDay() === $anniv_date->maximumJulianDay())) { 185f0a11419SGreg Roach $fact->anniv = 0; 186f0a11419SGreg Roach $found_facts[] = $fact; 187f0a11419SGreg Roach } 188f0a11419SGreg Roach } 189f0a11419SGreg Roach } 190f0a11419SGreg Roach } 191f0a11419SGreg Roach 192f0a11419SGreg Roach return $found_facts; 193f0a11419SGreg Roach } 194f0a11419SGreg Roach 195f0a11419SGreg Roach /** 196f0a11419SGreg Roach * Get the list of current and upcoming events, sorted by anniversary date 197f0a11419SGreg Roach * 198f0a11419SGreg Roach * @param int $jd1 199f0a11419SGreg Roach * @param int $jd2 200f0a11419SGreg Roach * @param string $events 201c7ff4153SGreg Roach * @param bool $only_living 202f0a11419SGreg Roach * @param string $sort_by 203f0a11419SGreg Roach * @param Tree $tree 204f0a11419SGreg Roach * 205cd51dbdfSGreg Roach * @return Collection<Fact> 206f0a11419SGreg Roach */ 207cd51dbdfSGreg Roach public function getEventsList(int $jd1, int $jd2, string $events, bool $only_living, string $sort_by, Tree $tree): Collection 208f0a11419SGreg Roach { 209f0a11419SGreg Roach $found_facts = []; 210cd51dbdfSGreg Roach $facts = new Collection(); 211f0a11419SGreg Roach 212f0a11419SGreg Roach foreach (range($jd1, $jd2) as $jd) { 213f0a11419SGreg Roach $found_facts = array_merge($found_facts, $this->getAnniversaryEvents($jd, $events, $tree)); 214f0a11419SGreg Roach } 215f0a11419SGreg Roach 216f0a11419SGreg Roach foreach ($found_facts as $fact) { 217e7766c08SGreg Roach $record = $fact->record(); 218f0a11419SGreg Roach // only living people ? 219f0a11419SGreg Roach if ($only_living) { 220f0a11419SGreg Roach if ($record instanceof Individual && $record->isDead()) { 221f0a11419SGreg Roach continue; 222f0a11419SGreg Roach } 223f0a11419SGreg Roach if ($record instanceof Family) { 22439ca88baSGreg Roach $husb = $record->husband(); 2258f038c36SRico Sonntag if ($husb === null || $husb->isDead()) { 226f0a11419SGreg Roach continue; 227f0a11419SGreg Roach } 22839ca88baSGreg Roach $wife = $record->wife(); 2298f038c36SRico Sonntag if ($wife === null || $wife->isDead()) { 230f0a11419SGreg Roach continue; 231f0a11419SGreg Roach } 232f0a11419SGreg Roach } 233f0a11419SGreg Roach } 234cd51dbdfSGreg Roach $facts->push($fact); 235f0a11419SGreg Roach } 236f0a11419SGreg Roach 237f0a11419SGreg Roach switch ($sort_by) { 238f0a11419SGreg Roach case 'anniv': 2396f97ab23SGreg Roach case 'anniv_asc': 240cd51dbdfSGreg Roach $facts = $facts->sort(static function (Fact $x, Fact $y): int { 2416f97ab23SGreg Roach return $x->jd <=> $y->jd ?: $x->date()->minimumJulianDay() <=> $y->date()->minimumJulianDay(); 2426f97ab23SGreg Roach }); 2436f97ab23SGreg Roach break; 2446f97ab23SGreg Roach 2456f97ab23SGreg Roach case 'anniv_desc': 2466f97ab23SGreg Roach $facts = $facts->sort(static function (Fact $x, Fact $y): int { 2476f97ab23SGreg Roach return $x->jd <=> $y->jd ?: $y->date()->minimumJulianDay() <=> $x->date()->minimumJulianDay(); 248cd51dbdfSGreg Roach }); 249f0a11419SGreg Roach break; 250580a4d11SGreg Roach 251f0a11419SGreg Roach case 'alpha': 252cd51dbdfSGreg Roach $facts = $facts->sort(static function (Fact $x, Fact $y): int { 253c156e8f5SGreg Roach return GedcomRecord::nameComparator()($x->record(), $y->record()); 254f0a11419SGreg Roach }); 255f0a11419SGreg Roach break; 256f0a11419SGreg Roach } 257f0a11419SGreg Roach 258e24053e5SGreg Roach return $facts->values(); 259f0a11419SGreg Roach } 260f0a11419SGreg Roach 261f0a11419SGreg Roach /** 262f0a11419SGreg Roach * Get a list of events whose anniversary occured on a given julian day. 263f0a11419SGreg Roach * Used on the on-this-day/upcoming blocks and the day/month calendar views. 264f0a11419SGreg Roach * 265f0a11419SGreg Roach * @param int $jd the julian day 266f0a11419SGreg Roach * @param string $facts restrict the search to just these facts or leave blank for all 267f0a11419SGreg Roach * @param Tree $tree the tree to search 268b00cb080SGreg Roach * @param string $filterof filter by living/recent 269b00cb080SGreg Roach * @param string $filtersx filter by sex 270f0a11419SGreg Roach * 271f0a11419SGreg Roach * @return Fact[] 272f0a11419SGreg Roach */ 273b00cb080SGreg Roach public function getAnniversaryEvents($jd, string $facts, Tree $tree, string $filterof = '', string $filtersx = ''): array 274f0a11419SGreg Roach { 275f0a11419SGreg Roach $found_facts = []; 276f0a11419SGreg Roach 277f0a11419SGreg Roach $anniversaries = [ 278f0a11419SGreg Roach new GregorianDate($jd), 279f0a11419SGreg Roach new JulianDate($jd), 280f0a11419SGreg Roach new FrenchDate($jd), 281f0a11419SGreg Roach new JewishDate($jd), 282f0a11419SGreg Roach new HijriDate($jd), 283f0a11419SGreg Roach ]; 284f0a11419SGreg Roach 2851bd3bc77SGreg Roach // There is a bug in the Persian Calendar that gives zero months for invalid dates 2864a9ac934SGreg Roach if ($jd > (new PersianCalendar())->jdStart()) { 2874a9ac934SGreg Roach $anniversaries[] = new JalaliDate($jd); 2884a9ac934SGreg Roach } 2894a9ac934SGreg Roach 290f0a11419SGreg Roach foreach ($anniversaries as $anniv) { 29125d4b688SGreg Roach // Build a query to match anniversaries in the appropriate calendar. 29225d4b688SGreg Roach $query = DB::table('dates') 29325d4b688SGreg Roach ->distinct() 29425d4b688SGreg Roach ->where('d_file', '=', $tree->id()) 29525d4b688SGreg Roach ->where('d_type', '=', $anniv->format('%@')); 296f0a11419SGreg Roach 297f0a11419SGreg Roach // SIMPLE CASES: 298f0a11419SGreg Roach // a) Non-hebrew anniversaries 299f0a11419SGreg Roach // b) Hebrew months TVT, SHV, IYR, SVN, TMZ, AAV, ELL 300e364afe4SGreg Roach if (!$anniv instanceof JewishDate || in_array($anniv->month, [1, 5, 6, 9, 10, 11, 12, 13], true)) { 30125d4b688SGreg Roach $this->defaultAnniversaries($query, $anniv); 302f0a11419SGreg Roach } else { 303f0a11419SGreg Roach // SPECIAL CASES: 3044a83f5d7SGreg Roach switch ($anniv->month) { 305f0a11419SGreg Roach case 2: 30625d4b688SGreg Roach $this->cheshvanAnniversaries($query, $anniv); 307f0a11419SGreg Roach break; 308f0a11419SGreg Roach case 3: 30925d4b688SGreg Roach $this->kislevAnniversaries($query, $anniv); 310f0a11419SGreg Roach break; 311f0a11419SGreg Roach case 4: 31225d4b688SGreg Roach $this->tevetAnniversaries($query, $anniv); 313f0a11419SGreg Roach break; 31425d4b688SGreg Roach case 7: 31525d4b688SGreg Roach $this->adarIIAnniversaries($query, $anniv); 316f0a11419SGreg Roach break; 31725d4b688SGreg Roach case 8: 31825d4b688SGreg Roach $this->nisanAnniversaries($query, $anniv); 319f0a11419SGreg Roach break; 320f0a11419SGreg Roach } 321f0a11419SGreg Roach } 322f0a11419SGreg Roach // Only events in the past (includes dates without a year) 32325d4b688SGreg Roach $query->where('d_year', '<=', $anniv->year()); 324f0a11419SGreg Roach 3250a614d28SGreg Roach if ($facts === '') { 3260a614d28SGreg Roach // If no facts specified, get all except these 3270a614d28SGreg Roach $query->whereNotIn('d_fact', self::SKIP_FACTS); 3280a614d28SGreg Roach } else { 3290a614d28SGreg Roach // Restrict to certain types of fact 330f0a11419SGreg Roach preg_match_all('/([_A-Z]+)/', $facts, $matches); 33125d4b688SGreg Roach 33225d4b688SGreg Roach $query->whereIn('d_fact', $matches[1]); 333f0a11419SGreg Roach } 334f0a11419SGreg Roach 335b00cb080SGreg Roach if ($filterof === 'recent') { 336b00cb080SGreg Roach $query->where('d_julianday1', '>=', Carbon::now()->subYears(100)->julianDay()); 337b00cb080SGreg Roach } 338b00cb080SGreg Roach 33925d4b688SGreg Roach $query 34094969abbSRico Sonntag ->orderBy('d_day') 3410195b0ddSGreg Roach ->orderBy('d_year', 'DESC'); 34225d4b688SGreg Roach 34325d4b688SGreg Roach $ind_query = (clone $query) 3440b5fd0a6SGreg Roach ->join('individuals', static function (JoinClause $join): void { 34525d4b688SGreg Roach $join->on('d_gid', '=', 'i_id')->on('d_file', '=', 'i_file'); 34625d4b688SGreg Roach }) 34725d4b688SGreg Roach ->select(['i_id AS xref', 'i_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact']); 34825d4b688SGreg Roach 349b00cb080SGreg Roach $queries = ['INDI' => $ind_query]; 350b00cb080SGreg Roach 351b00cb080SGreg Roach if ($filtersx === '') { 35225d4b688SGreg Roach $fam_query = (clone $query) 3530b5fd0a6SGreg Roach ->join('families', static function (JoinClause $join): void { 35425d4b688SGreg Roach $join->on('d_gid', '=', 'f_id')->on('d_file', '=', 'f_file'); 35525d4b688SGreg Roach }) 35625d4b688SGreg Roach ->select(['f_id AS xref', 'f_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact']); 357f0a11419SGreg Roach 358b00cb080SGreg Roach $queries['FAM'] = $fam_query; 359b00cb080SGreg Roach } else { 360b00cb080SGreg Roach $queries['INDI']->where('i_sex', '=', $filtersx); 361b00cb080SGreg Roach } 362b00cb080SGreg Roach 363f0a11419SGreg Roach // Now fetch these anniversaries 364b00cb080SGreg Roach foreach ($queries as $type => $record_query) { 36561b2e6eeSGreg Roach foreach ($record_query->get() as $row) { 366f0a11419SGreg Roach if ($type === 'INDI') { 367a091ac74SGreg Roach $record = Factory::individual()->make($row->xref, $tree, $row->gedcom); 368b00cb080SGreg Roach assert($record instanceof Individual); 369b00cb080SGreg Roach 370b00cb080SGreg Roach if ($filterof === 'living' && $record->isDead()) { 371b00cb080SGreg Roach continue; 372b00cb080SGreg Roach } 373f0a11419SGreg Roach } else { 374a091ac74SGreg Roach $record = Factory::family()->make($row->xref, $tree, $row->gedcom); 375b00cb080SGreg Roach assert($record instanceof Family); 376b00cb080SGreg Roach $husb = $record->husband(); 377b00cb080SGreg Roach $wife = $record->wife(); 378b00cb080SGreg Roach 379b00cb080SGreg Roach if ($filterof === 'living' && ($husb && $husb->isDead() || $wife && $wife->isDead())) { 380b00cb080SGreg Roach continue; 381b00cb080SGreg Roach } 382f0a11419SGreg Roach } 38325d4b688SGreg Roach 384f0a11419SGreg Roach $anniv_date = new Date($row->d_type . ' ' . $row->d_day . ' ' . $row->d_month . ' ' . $row->d_year); 38525d4b688SGreg Roach 386543ab5f6SGreg Roach // The record may have multiple facts of this type. 387543ab5f6SGreg Roach // Find the ones that match the date. 388543ab5f6SGreg Roach foreach ($record->facts([$row->d_fact]) as $fact) { 389543ab5f6SGreg Roach $min_date = $fact->date()->minimumDate(); 390543ab5f6SGreg Roach $max_date = $fact->date()->maximumDate(); 391543ab5f6SGreg Roach 392543ab5f6SGreg Roach if ($min_date->minimumJulianDay() === $anniv_date->minimumJulianDay() && $min_date::ESCAPE === $row->d_type || $max_date->maximumJulianDay() === $anniv_date->maximumJulianDay() && $max_date::ESCAPE === $row->d_type) { 3934a83f5d7SGreg Roach $fact->anniv = $row->d_year === '0' ? 0 : $anniv->year - $row->d_year; 394f0a11419SGreg Roach $fact->jd = $jd; 395f0a11419SGreg Roach $found_facts[] = $fact; 396f0a11419SGreg Roach } 397f0a11419SGreg Roach } 398f0a11419SGreg Roach } 399f0a11419SGreg Roach } 400f0a11419SGreg Roach } 401f0a11419SGreg Roach 402f0a11419SGreg Roach return $found_facts; 403f0a11419SGreg Roach } 40425d4b688SGreg Roach 40525d4b688SGreg Roach /** 40625d4b688SGreg Roach * By default, missing days have anniversaries on the first of the month, 40725d4b688SGreg Roach * and invalid days have anniversaries on the last day of the month. 40825d4b688SGreg Roach * 40925d4b688SGreg Roach * @param Builder $query 41025d4b688SGreg Roach * @param AbstractCalendarDate $anniv 41125d4b688SGreg Roach */ 41225d4b688SGreg Roach private function defaultAnniversaries(Builder $query, AbstractCalendarDate $anniv): void 41325d4b688SGreg Roach { 41425d4b688SGreg Roach if ($anniv->day() === 1) { 41525d4b688SGreg Roach $query->where('d_day', '<=', 1); 41625d4b688SGreg Roach } elseif ($anniv->day() === $anniv->daysInMonth()) { 41725d4b688SGreg Roach $query->where('d_day', '>=', $anniv->daysInMonth()); 41825d4b688SGreg Roach } else { 41925d4b688SGreg Roach $query->where('d_day', '=', $anniv->day()); 42025d4b688SGreg Roach } 42125d4b688SGreg Roach 42225d4b688SGreg Roach $query->where('d_mon', '=', $anniv->month()); 42325d4b688SGreg Roach } 42425d4b688SGreg Roach 42525d4b688SGreg Roach /** 42625d4b688SGreg Roach * 29 CSH does not include 30 CSH (but would include an invalid 31 CSH if there were no 30 CSH). 42725d4b688SGreg Roach * 42825d4b688SGreg Roach * @param Builder $query 42925d4b688SGreg Roach * @param JewishDate $anniv 43025d4b688SGreg Roach */ 43125d4b688SGreg Roach private function cheshvanAnniversaries(Builder $query, JewishDate $anniv): void 43225d4b688SGreg Roach { 43325d4b688SGreg Roach if ($anniv->day === 29 && $anniv->daysInMonth() === 29) { 43425d4b688SGreg Roach $query 43525d4b688SGreg Roach ->where('d_mon', '=', 2) 43625d4b688SGreg Roach ->where('d_day', '>=', 29) 43725d4b688SGreg Roach ->where('d_day', '<>', 30); 43825d4b688SGreg Roach } else { 43925d4b688SGreg Roach $this->defaultAnniversaries($query, $anniv); 44025d4b688SGreg Roach } 44125d4b688SGreg Roach } 44225d4b688SGreg Roach 44325d4b688SGreg Roach /** 44425d4b688SGreg Roach * 1 KSL includes 30 CSH (if this year didn’t have 30 CSH). 44525d4b688SGreg Roach * 29 KSL does not include 30 KSL (but would include an invalid 31 KSL if there were no 30 KSL). 44625d4b688SGreg Roach * 44725d4b688SGreg Roach * @param Builder $query 44825d4b688SGreg Roach * @param JewishDate $anniv 44925d4b688SGreg Roach */ 45025d4b688SGreg Roach private function kislevAnniversaries(Builder $query, JewishDate $anniv): void 45125d4b688SGreg Roach { 45225d4b688SGreg Roach $tmp = new JewishDate([(string) $anniv->year, 'CSH', '1']); 45325d4b688SGreg Roach 45425d4b688SGreg Roach if ($anniv->day() === 1 && $tmp->daysInMonth() === 29) { 4550b5fd0a6SGreg Roach $query->where(static function (Builder $query): void { 4560b5fd0a6SGreg Roach $query->where(static function (Builder $query): void { 45725d4b688SGreg Roach $query->where('d_day', '<=', 1)->where('d_mon', '=', 3); 4580b5fd0a6SGreg Roach })->orWhere(static function (Builder $query): void { 45925d4b688SGreg Roach $query->where('d_day', '=', 30)->where('d_mon', '=', 2); 46025d4b688SGreg Roach }); 46125d4b688SGreg Roach }); 46225d4b688SGreg Roach } elseif ($anniv->day === 29 && $anniv->daysInMonth() === 29) { 46325d4b688SGreg Roach $query 46425d4b688SGreg Roach ->where('d_mon', '=', 3) 46525d4b688SGreg Roach ->where('d_day', '>=', 29) 46625d4b688SGreg Roach ->where('d_day', '<>', 30); 46725d4b688SGreg Roach } else { 46825d4b688SGreg Roach $this->defaultAnniversaries($query, $anniv); 46925d4b688SGreg Roach } 47025d4b688SGreg Roach } 47125d4b688SGreg Roach 47225d4b688SGreg Roach /** 47325d4b688SGreg Roach * 1 TVT includes 30 KSL (if this year didn’t have 30 KSL). 47425d4b688SGreg Roach * 47525d4b688SGreg Roach * @param Builder $query 47625d4b688SGreg Roach * @param JewishDate $anniv 47725d4b688SGreg Roach */ 47825d4b688SGreg Roach private function tevetAnniversaries(Builder $query, JewishDate $anniv): void 47925d4b688SGreg Roach { 48025d4b688SGreg Roach $tmp = new JewishDate([(string) $anniv->year, 'KSL', '1']); 48125d4b688SGreg Roach 48225d4b688SGreg Roach if ($anniv->day === 1 && $tmp->daysInMonth() === 29) { 4830b5fd0a6SGreg Roach $query->where(static function (Builder $query): void { 4840b5fd0a6SGreg Roach $query->where(static function (Builder $query): void { 48525d4b688SGreg Roach $query->where('d_day', '<=', 1)->where('d_mon', '=', 4); 4860b5fd0a6SGreg Roach })->orWhere(static function (Builder $query): void { 48725d4b688SGreg Roach $query->where('d_day', '=', 30)->where('d_mon', '=', 3); 48825d4b688SGreg Roach }); 48925d4b688SGreg Roach }); 49025d4b688SGreg Roach } else { 49125d4b688SGreg Roach $this->defaultAnniversaries($query, $anniv); 49225d4b688SGreg Roach } 49325d4b688SGreg Roach } 49425d4b688SGreg Roach 49525d4b688SGreg Roach /** 49625d4b688SGreg Roach * ADS includes non-leap ADR. 49725d4b688SGreg Roach * 49825d4b688SGreg Roach * @param Builder $query 49925d4b688SGreg Roach * @param JewishDate $anniv 50025d4b688SGreg Roach */ 50125d4b688SGreg Roach private function adarIIAnniversaries(Builder $query, JewishDate $anniv): void 50225d4b688SGreg Roach { 50325d4b688SGreg Roach if ($anniv->day() === 1) { 50425d4b688SGreg Roach $query->where('d_day', '<=', 1); 50525d4b688SGreg Roach } elseif ($anniv->day() === $anniv->daysInMonth()) { 50625d4b688SGreg Roach $query->where('d_day', '>=', $anniv->daysInMonth()); 50725d4b688SGreg Roach } else { 5080bab1c4aSGreg Roach $query->where('d_day', '=', $anniv->day()); 50925d4b688SGreg Roach } 51025d4b688SGreg Roach 511*082dc12eSGreg Roach if ($anniv->isLeapYear()) { 512*082dc12eSGreg Roach $query->where('d_mon', '=', 7); 513*082dc12eSGreg Roach } else { 514*082dc12eSGreg Roach $query->whereIn('d_mon', [6, 7]); 515*082dc12eSGreg Roach } 51625d4b688SGreg Roach } 51725d4b688SGreg Roach 51825d4b688SGreg Roach /** 51925d4b688SGreg Roach * 1 NSN includes 30 ADR, if this year is non-leap. 52025d4b688SGreg Roach * 52125d4b688SGreg Roach * @param Builder $query 52225d4b688SGreg Roach * @param JewishDate $anniv 52325d4b688SGreg Roach */ 52425d4b688SGreg Roach private function nisanAnniversaries(Builder $query, JewishDate $anniv): void 52525d4b688SGreg Roach { 52625d4b688SGreg Roach if ($anniv->day === 1 && !$anniv->isLeapYear()) { 5270b5fd0a6SGreg Roach $query->where(static function (Builder $query): void { 5280b5fd0a6SGreg Roach $query->where(static function (Builder $query): void { 52925d4b688SGreg Roach $query->where('d_day', '<=', 1)->where('d_mon', '=', 8); 5300b5fd0a6SGreg Roach })->orWhere(static function (Builder $query): void { 53125d4b688SGreg Roach $query->where('d_day', '=', 30)->where('d_mon', '=', 6); 53225d4b688SGreg Roach }); 53325d4b688SGreg Roach }); 53425d4b688SGreg Roach } else { 53525d4b688SGreg Roach $this->defaultAnniversaries($query, $anniv); 53625d4b688SGreg Roach } 53725d4b688SGreg Roach } 538f0a11419SGreg Roach} 539