1f0a11419SGreg Roach<?php 2f0a11419SGreg Roach/** 3f0a11419SGreg Roach * webtrees: online genealogy 4f0a11419SGreg Roach * Copyright (C) 2018 webtrees development team 5f0a11419SGreg Roach * This program is free software: you can redistribute it and/or modify 6f0a11419SGreg Roach * it under the terms of the GNU General Public License as published by 7f0a11419SGreg Roach * the Free Software Foundation, either version 3 of the License, or 8f0a11419SGreg Roach * (at your option) any later version. 9f0a11419SGreg Roach * This program is distributed in the hope that it will be useful, 10f0a11419SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 11f0a11419SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12f0a11419SGreg Roach * GNU General Public License for more details. 13f0a11419SGreg Roach * You should have received a copy of the GNU General Public License 14f0a11419SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 15f0a11419SGreg Roach */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 18f0a11419SGreg Roachnamespace Fisharebest\Webtrees\Services; 19f0a11419SGreg Roach 20f0a11419SGreg Roachuse Fisharebest\Webtrees\Database; 21f0a11419SGreg Roachuse Fisharebest\Webtrees\Date; 22f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\FrenchDate; 23f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate; 24f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\HijriDate; 25f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\JalaliDate; 26f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\JewishDate; 27f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\JulianDate; 28f0a11419SGreg Roachuse Fisharebest\Webtrees\Fact; 29f0a11419SGreg Roachuse Fisharebest\Webtrees\Family; 30f0a11419SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 31f0a11419SGreg Roachuse Fisharebest\Webtrees\Individual; 32f0a11419SGreg Roachuse Fisharebest\Webtrees\Tree; 33f0a11419SGreg Roach 34f0a11419SGreg Roach/** 35f0a11419SGreg Roach * Calculate anniversaries, etc. 36f0a11419SGreg Roach */ 37f0a11419SGreg Roachclass CalendarService 38f0a11419SGreg Roach{ 39f0a11419SGreg Roach /** 4070d8159dSGreg Roach * List all the months in a given year. 4170d8159dSGreg Roach * 4270d8159dSGreg Roach * @param string $calendar 4370d8159dSGreg Roach * @param int $year 4470d8159dSGreg Roach * 4570d8159dSGreg Roach * @return string[] 4670d8159dSGreg Roach */ 4770d8159dSGreg Roach public function calendarMonthsInYear(string $calendar, int $year): array 4870d8159dSGreg Roach { 4970d8159dSGreg Roach $date = new Date($calendar . ' ' . $year); 5070d8159dSGreg Roach $calendar_date = $date->minimumDate(); 5170d8159dSGreg Roach $month_numbers = range(1, $calendar_date->monthsInYear()); 5270d8159dSGreg Roach $month_names = []; 5370d8159dSGreg Roach 5470d8159dSGreg Roach foreach ($month_numbers as $month_number) { 554a83f5d7SGreg Roach $calendar_date->day = 1; 564a83f5d7SGreg Roach $calendar_date->month = $month_number; 5770d8159dSGreg Roach $calendar_date->setJdFromYmd(); 5870d8159dSGreg Roach 5970d8159dSGreg Roach if ($month_number === 6 && $calendar_date instanceof JewishDate && !$calendar_date->isLeapYear()) { 6070d8159dSGreg Roach // No month 6 in Jewish non-leap years. 6170d8159dSGreg Roach continue; 6270d8159dSGreg Roach } 6370d8159dSGreg Roach 6470d8159dSGreg Roach if ($month_number === 7 && $calendar_date instanceof JewishDate && !$calendar_date->isLeapYear()) { 6570d8159dSGreg Roach // Month 7 is ADR in Jewish non-leap years (and ADS in others). 6670d8159dSGreg Roach $mon = 'ADR'; 6770d8159dSGreg Roach } else { 6870d8159dSGreg Roach $mon = $calendar_date->format('%O'); 6970d8159dSGreg Roach } 7070d8159dSGreg Roach 7170d8159dSGreg Roach 7270d8159dSGreg Roach $month_names[$mon] = $calendar_date->format('%F'); 7370d8159dSGreg Roach } 7470d8159dSGreg Roach 7570d8159dSGreg Roach return $month_names; 7670d8159dSGreg Roach } 7770d8159dSGreg Roach 7870d8159dSGreg Roach /** 79f0a11419SGreg Roach * Get a list of events which occured during a given date range. 80f0a11419SGreg Roach * 81f0a11419SGreg Roach * @param int $jd1 the start range of julian day 82f0a11419SGreg Roach * @param int $jd2 the end range of julian day 83f0a11419SGreg Roach * @param string $facts restrict the search to just these facts or leave blank for all 84f0a11419SGreg Roach * @param Tree $tree the tree to search 85f0a11419SGreg Roach * 86f0a11419SGreg Roach * @return Fact[] 87f0a11419SGreg Roach */ 88f0a11419SGreg Roach public function getCalendarEvents(int $jd1, int $jd2, string $facts, Tree $tree): array 89f0a11419SGreg Roach { 90f0a11419SGreg Roach // If no facts specified, get all except these 91f0a11419SGreg Roach $skipfacts = 'CHAN,BAPL,SLGC,SLGS,ENDL,CENS,RESI,NOTE,ADDR,OBJE,SOUR'; 92f0a11419SGreg Roach 93f0a11419SGreg Roach $found_facts = []; 94f0a11419SGreg Roach 95f0a11419SGreg Roach // Events that start or end during the period 96f0a11419SGreg Roach $where = "WHERE (d_julianday1>={$jd1} AND d_julianday1<={$jd2} OR d_julianday2>={$jd1} AND d_julianday2<={$jd2})"; 97f0a11419SGreg Roach 98f0a11419SGreg Roach // Restrict to certain types of fact 99f0a11419SGreg Roach if (empty($facts)) { 100f0a11419SGreg Roach $excl_facts = "'" . preg_replace('/\W+/', "','", $skipfacts) . "'"; 101f0a11419SGreg Roach $where .= " AND d_fact NOT IN ({$excl_facts})"; 102f0a11419SGreg Roach } else { 103f0a11419SGreg Roach $incl_facts = "'" . preg_replace('/\W+/', "','", $facts) . "'"; 104f0a11419SGreg Roach $where .= " AND d_fact IN ({$incl_facts})"; 105f0a11419SGreg Roach } 106f0a11419SGreg Roach // Only get events from the current gedcom 107f0a11419SGreg Roach $where .= " AND d_file=" . $tree->getTreeId(); 108f0a11419SGreg Roach 109f0a11419SGreg Roach // Now fetch these events 110f0a11419SGreg Roach $ind_sql = "SELECT d_gid AS xref, i_gedcom AS gedcom, d_type, d_day, d_month, d_year, d_fact, d_type FROM `##dates`, `##individuals` {$where} AND d_gid=i_id AND d_file=i_file ORDER BY d_julianday1"; 111f0a11419SGreg Roach $fam_sql = "SELECT d_gid AS xref, f_gedcom AS gedcom, d_type, d_day, d_month, d_year, d_fact, d_type FROM `##dates`, `##families` {$where} AND d_gid=f_id AND d_file=f_file ORDER BY d_julianday1"; 112f0a11419SGreg Roach 113f0a11419SGreg Roach foreach (['INDI' => $ind_sql, 'FAM' => $fam_sql] as $type => $sql) { 114f0a11419SGreg Roach $rows = Database::prepare($sql)->fetchAll(); 115f0a11419SGreg Roach 116f0a11419SGreg Roach foreach ($rows as $row) { 117f0a11419SGreg Roach if ($type === 'INDI') { 118f0a11419SGreg Roach $record = Individual::getInstance($row->xref, $tree, $row->gedcom); 119f0a11419SGreg Roach } else { 120f0a11419SGreg Roach $record = Family::getInstance($row->xref, $tree, $row->gedcom); 121f0a11419SGreg Roach } 122f0a11419SGreg Roach $anniv_date = new Date($row->d_type . ' ' . $row->d_day . ' ' . $row->d_month . ' ' . $row->d_year); 123f0a11419SGreg Roach foreach ($record->getFacts() as $fact) { 124f0a11419SGreg Roach // For date ranges, we need a match on either the start/end. 125f0a11419SGreg Roach if (($fact->getDate()->minimumJulianDay() === $anniv_date->minimumJulianDay() || $fact->getDate()->maximumJulianDay() == $anniv_date->maximumJulianDay()) && $fact->getTag() === $row->d_fact) { 126f0a11419SGreg Roach $fact->anniv = 0; 127f0a11419SGreg Roach $found_facts[] = $fact; 128f0a11419SGreg Roach } 129f0a11419SGreg Roach } 130f0a11419SGreg Roach } 131f0a11419SGreg Roach } 132f0a11419SGreg Roach 133f0a11419SGreg Roach return $found_facts; 134f0a11419SGreg Roach } 135f0a11419SGreg Roach 136f0a11419SGreg Roach /** 137f0a11419SGreg Roach * Get the list of current and upcoming events, sorted by anniversary date 138f0a11419SGreg Roach * 139f0a11419SGreg Roach * @param int $jd1 140f0a11419SGreg Roach * @param int $jd2 141f0a11419SGreg Roach * @param string $events 142c7ff4153SGreg Roach * @param bool $only_living 143f0a11419SGreg Roach * @param string $sort_by 144f0a11419SGreg Roach * @param Tree $tree 145f0a11419SGreg Roach * 146f0a11419SGreg Roach * @return Fact[] 147f0a11419SGreg Roach */ 148f0a11419SGreg Roach public function getEventsList(int $jd1, int $jd2, string $events, bool $only_living, string $sort_by, Tree $tree): array 149f0a11419SGreg Roach { 150f0a11419SGreg Roach $found_facts = []; 151f0a11419SGreg Roach $facts = []; 152f0a11419SGreg Roach 153f0a11419SGreg Roach foreach (range($jd1, $jd2) as $jd) { 154f0a11419SGreg Roach $found_facts = array_merge($found_facts, $this->getAnniversaryEvents($jd, $events, $tree)); 155f0a11419SGreg Roach } 156f0a11419SGreg Roach 157f0a11419SGreg Roach foreach ($found_facts as $fact) { 158*e7766c08SGreg Roach $record = $fact->record(); 159f0a11419SGreg Roach // only living people ? 160f0a11419SGreg Roach if ($only_living) { 161f0a11419SGreg Roach if ($record instanceof Individual && $record->isDead()) { 162f0a11419SGreg Roach continue; 163f0a11419SGreg Roach } 164f0a11419SGreg Roach if ($record instanceof Family) { 165f0a11419SGreg Roach $husb = $record->getHusband(); 1668f038c36SRico Sonntag if ($husb === null || $husb->isDead()) { 167f0a11419SGreg Roach continue; 168f0a11419SGreg Roach } 169f0a11419SGreg Roach $wife = $record->getWife(); 1708f038c36SRico Sonntag if ($wife === null || $wife->isDead()) { 171f0a11419SGreg Roach continue; 172f0a11419SGreg Roach } 173f0a11419SGreg Roach } 174f0a11419SGreg Roach } 175f0a11419SGreg Roach $facts[] = $fact; 176f0a11419SGreg Roach } 177f0a11419SGreg Roach 178f0a11419SGreg Roach switch ($sort_by) { 179f0a11419SGreg Roach case 'anniv': 180a6312006SGreg Roach uasort($facts, function (Fact $x, Fact $y): int { 181f0a11419SGreg Roach return Fact::compareDate($y, $x); 182f0a11419SGreg Roach }); 183f0a11419SGreg Roach break; 184f0a11419SGreg Roach case 'alpha': 185a6312006SGreg Roach uasort($facts, function (Fact $x, Fact $y): int { 186*e7766c08SGreg Roach return GedcomRecord::compare($x->record(), $y->record()); 187f0a11419SGreg Roach }); 188f0a11419SGreg Roach break; 189f0a11419SGreg Roach } 190f0a11419SGreg Roach 191f0a11419SGreg Roach return $facts; 192f0a11419SGreg Roach } 193f0a11419SGreg Roach 194f0a11419SGreg Roach /** 195f0a11419SGreg Roach * Get a list of events whose anniversary occured on a given julian day. 196f0a11419SGreg Roach * Used on the on-this-day/upcoming blocks and the day/month calendar views. 197f0a11419SGreg Roach * 198f0a11419SGreg Roach * @param int $jd the julian day 199f0a11419SGreg Roach * @param string $facts restrict the search to just these facts or leave blank for all 200f0a11419SGreg Roach * @param Tree $tree the tree to search 201f0a11419SGreg Roach * 202f0a11419SGreg Roach * @return Fact[] 203f0a11419SGreg Roach */ 204f0a11419SGreg Roach public function getAnniversaryEvents($jd, $facts, Tree $tree): array 205f0a11419SGreg Roach { 206f0a11419SGreg Roach $found_facts = []; 207f0a11419SGreg Roach 208f0a11419SGreg Roach $anniversaries = [ 209f0a11419SGreg Roach new GregorianDate($jd), 210f0a11419SGreg Roach new JulianDate($jd), 211f0a11419SGreg Roach new FrenchDate($jd), 212f0a11419SGreg Roach new JewishDate($jd), 213f0a11419SGreg Roach new HijriDate($jd), 214f0a11419SGreg Roach new JalaliDate($jd), 215f0a11419SGreg Roach ]; 216f0a11419SGreg Roach 217f0a11419SGreg Roach foreach ($anniversaries as $anniv) { 218f0a11419SGreg Roach // Build a SQL where clause to match anniversaries in the appropriate calendar. 219f0a11419SGreg Roach $ind_sql = 220f0a11419SGreg Roach "SELECT DISTINCT i_id AS xref, i_gedcom AS gedcom, d_type, d_day, d_month, d_year, d_fact" . 221f0a11419SGreg Roach " FROM `##dates` JOIN `##individuals` ON d_gid = i_id AND d_file = i_file" . 222f0a11419SGreg Roach " WHERE d_type = :type AND d_file = :tree_id"; 223f0a11419SGreg Roach $fam_sql = 224f0a11419SGreg Roach "SELECT DISTINCT f_id AS xref, f_gedcom AS gedcom, d_type, d_day, d_month, d_year, d_fact" . 225f0a11419SGreg Roach " FROM `##dates` JOIN `##families` ON d_gid = f_id AND d_file = f_file" . 226f0a11419SGreg Roach " WHERE d_type = :type AND d_file = :tree_id"; 227f0a11419SGreg Roach $args = [ 228f0a11419SGreg Roach 'type' => $anniv->format('%@'), 229f0a11419SGreg Roach 'tree_id' => $tree->getTreeId(), 230f0a11419SGreg Roach ]; 231f0a11419SGreg Roach 232f0a11419SGreg Roach $where = ""; 233f0a11419SGreg Roach // SIMPLE CASES: 234f0a11419SGreg Roach // a) Non-hebrew anniversaries 235f0a11419SGreg Roach // b) Hebrew months TVT, SHV, IYR, SVN, TMZ, AAV, ELL 2364a83f5d7SGreg Roach if (!$anniv instanceof JewishDate || in_array($anniv->month, [ 237f0a11419SGreg Roach 1, 238f0a11419SGreg Roach 5, 239f0a11419SGreg Roach 6, 240f0a11419SGreg Roach 9, 241f0a11419SGreg Roach 10, 242f0a11419SGreg Roach 11, 243f0a11419SGreg Roach 12, 244f0a11419SGreg Roach 13, 245f0a11419SGreg Roach ])) { 246f0a11419SGreg Roach // Dates without days go on the first day of the month 247f0a11419SGreg Roach // Dates with invalid days go on the last day of the month 2484a83f5d7SGreg Roach if ($anniv->day === 1) { 249f0a11419SGreg Roach $where .= " AND d_day <= 1"; 2504a83f5d7SGreg Roach } elseif ($anniv->day === $anniv->daysInMonth()) { 251f0a11419SGreg Roach $where .= " AND d_day >= :day"; 2524a83f5d7SGreg Roach $args['day'] = $anniv->day; 253f0a11419SGreg Roach } else { 254f0a11419SGreg Roach $where .= " AND d_day = :day"; 2554a83f5d7SGreg Roach $args['day'] = $anniv->day; 256f0a11419SGreg Roach } 257f0a11419SGreg Roach $where .= " AND d_mon = :month"; 2584a83f5d7SGreg Roach $args['month'] = $anniv->month; 259f0a11419SGreg Roach } else { 260f0a11419SGreg Roach // SPECIAL CASES: 2614a83f5d7SGreg Roach switch ($anniv->month) { 262f0a11419SGreg Roach case 2: 263f0a11419SGreg Roach // 29 CSH does not include 30 CSH (but would include an invalid 31 CSH if there were no 30 CSH) 2644a83f5d7SGreg Roach if ($anniv->day === 1) { 265f0a11419SGreg Roach $where .= " AND d_day <= 1 AND d_mon = 2"; 2664a83f5d7SGreg Roach } elseif ($anniv->day === 30) { 267f0a11419SGreg Roach $where .= " AND d_day >= 30 AND d_mon = 2"; 2684a83f5d7SGreg Roach } elseif ($anniv->day === 29 && $anniv->daysInMonth() === 29) { 269f0a11419SGreg Roach $where .= " AND (d_day = 29 OR d_day > 30) AND d_mon = 2"; 270f0a11419SGreg Roach } else { 271f0a11419SGreg Roach $where .= " AND d_day = :day AND d_mon = 2"; 2724a83f5d7SGreg Roach $args['day'] = $anniv->day; 273f0a11419SGreg Roach } 274f0a11419SGreg Roach break; 275f0a11419SGreg Roach case 3: 276f0a11419SGreg Roach // 1 KSL includes 30 CSH (if this year didn’t have 30 CSH) 277f0a11419SGreg Roach // 29 KSL does not include 30 KSL (but would include an invalid 31 KSL if there were no 30 KSL) 2784a83f5d7SGreg Roach if ($anniv->day === 1) { 279f0a11419SGreg Roach $tmp = new JewishDate([ 2803baaf269SGreg Roach (string) $anniv->year, 281f0a11419SGreg Roach 'CSH', 282f0a11419SGreg Roach 1, 283f0a11419SGreg Roach ]); 284f0a11419SGreg Roach if ($tmp->daysInMonth() === 29) { 285f0a11419SGreg Roach $where .= " AND (d_day <= 1 AND d_mon = 3 OR d_day = 30 AND d_mon = 2)"; 286f0a11419SGreg Roach } else { 287f0a11419SGreg Roach $where .= " AND d_day <= 1 AND d_mon = 3"; 288f0a11419SGreg Roach } 2894a83f5d7SGreg Roach } elseif ($anniv->day === 30) { 290f0a11419SGreg Roach $where .= " AND d_day >= 30 AND d_mon = 3"; 2914a83f5d7SGreg Roach } elseif ($anniv->day == 29 && $anniv->daysInMonth() === 29) { 292f0a11419SGreg Roach $where .= " AND (d_day = 29 OR d_day > 30) AND d_mon = 3"; 293f0a11419SGreg Roach } else { 294f0a11419SGreg Roach $where .= " AND d_day = :day AND d_mon = 3"; 2954a83f5d7SGreg Roach $args['day'] = $anniv->day; 296f0a11419SGreg Roach } 297f0a11419SGreg Roach break; 298f0a11419SGreg Roach case 4: 299f0a11419SGreg Roach // 1 TVT includes 30 KSL (if this year didn’t have 30 KSL) 3004a83f5d7SGreg Roach if ($anniv->day === 1) { 301f0a11419SGreg Roach $tmp = new JewishDate([ 3023baaf269SGreg Roach (string) $anniv->year, 303f0a11419SGreg Roach 'KSL', 304f0a11419SGreg Roach 1, 305f0a11419SGreg Roach ]); 306f0a11419SGreg Roach if ($tmp->daysInMonth() === 29) { 307f0a11419SGreg Roach $where .= " AND (d_day <=1 AND d_mon = 4 OR d_day = 30 AND d_mon = 3)"; 308f0a11419SGreg Roach } else { 309f0a11419SGreg Roach $where .= " AND d_day <= 1 AND d_mon = 4"; 310f0a11419SGreg Roach } 3114a83f5d7SGreg Roach } elseif ($anniv->day === $anniv->daysInMonth()) { 312f0a11419SGreg Roach $where .= " AND d_day >= :day AND d_mon=4"; 3134a83f5d7SGreg Roach $args['day'] = $anniv->day; 314f0a11419SGreg Roach } else { 315f0a11419SGreg Roach $where .= " AND d_day = :day AND d_mon=4"; 3164a83f5d7SGreg Roach $args['day'] = $anniv->day; 317f0a11419SGreg Roach } 318f0a11419SGreg Roach break; 319f0a11419SGreg Roach case 7: // ADS includes ADR (non-leap) 3204a83f5d7SGreg Roach if ($anniv->day === 1) { 321f0a11419SGreg Roach $where .= " AND d_day <= 1"; 3224a83f5d7SGreg Roach } elseif ($anniv->day === $anniv->daysInMonth()) { 323f0a11419SGreg Roach $where .= " AND d_day >= :day"; 3244a83f5d7SGreg Roach $args['day'] = $anniv->day; 325f0a11419SGreg Roach } else { 326f0a11419SGreg Roach $where .= " AND d_day = :day"; 3274a83f5d7SGreg Roach $args['day'] = $anniv->day; 328f0a11419SGreg Roach } 329f0a11419SGreg Roach $where .= " AND (d_mon = 6 AND MOD(7 * d_year + 1, 19) >= 7 OR d_mon = 7)"; 330f0a11419SGreg Roach break; 331f0a11419SGreg Roach case 8: // 1 NSN includes 30 ADR, if this year is non-leap 3324a83f5d7SGreg Roach if ($anniv->day === 1) { 333f0a11419SGreg Roach if ($anniv->isLeapYear()) { 334f0a11419SGreg Roach $where .= " AND d_day <= 1 AND d_mon = 8"; 335f0a11419SGreg Roach } else { 336f0a11419SGreg Roach $where .= " AND (d_day <= 1 AND d_mon = 8 OR d_day = 30 AND d_mon = 6)"; 337f0a11419SGreg Roach } 3384a83f5d7SGreg Roach } elseif ($anniv->day === $anniv->daysInMonth()) { 339f0a11419SGreg Roach $where .= " AND d_day >= :day AND d_mon = 8"; 3404a83f5d7SGreg Roach $args['day'] = $anniv->day; 341f0a11419SGreg Roach } else { 342f0a11419SGreg Roach $where .= " AND d_day = :day AND d_mon = 8"; 3434a83f5d7SGreg Roach $args['day'] = $anniv->day; 344f0a11419SGreg Roach } 345f0a11419SGreg Roach break; 346f0a11419SGreg Roach } 347f0a11419SGreg Roach } 348f0a11419SGreg Roach // Only events in the past (includes dates without a year) 349f0a11419SGreg Roach $where .= " AND d_year <= :year"; 3504a83f5d7SGreg Roach $args['year'] = $anniv->year; 351f0a11419SGreg Roach 352f0a11419SGreg Roach if ($facts) { 353f0a11419SGreg Roach // Restrict to certain types of fact 354f0a11419SGreg Roach $where .= " AND d_fact IN ("; 355f0a11419SGreg Roach preg_match_all('/([_A-Z]+)/', $facts, $matches); 356f0a11419SGreg Roach foreach ($matches[1] as $n => $fact) { 357f0a11419SGreg Roach $where .= $n ? ", " : ""; 358f0a11419SGreg Roach $where .= ":fact_" . $n; 359f0a11419SGreg Roach $args['fact_' . $n] = $fact; 360f0a11419SGreg Roach } 361f0a11419SGreg Roach $where .= ")"; 362f0a11419SGreg Roach } else { 363f0a11419SGreg Roach // If no facts specified, get all except these 364f0a11419SGreg Roach $where .= " AND d_fact NOT IN ('CHAN', 'BAPL', 'SLGC', 'SLGS', 'ENDL', 'CENS', 'RESI', '_TODO')"; 365f0a11419SGreg Roach } 366f0a11419SGreg Roach 367f0a11419SGreg Roach $order_by = " ORDER BY d_day, d_year DESC"; 368f0a11419SGreg Roach 369f0a11419SGreg Roach // Now fetch these anniversaries 370f0a11419SGreg Roach foreach ([ 371f0a11419SGreg Roach 'INDI' => $ind_sql . $where . $order_by, 372f0a11419SGreg Roach 'FAM' => $fam_sql . $where . $order_by, 373f0a11419SGreg Roach ] as $type => $sql) { 374f0a11419SGreg Roach $rows = Database::prepare($sql)->execute($args)->fetchAll(); 375f0a11419SGreg Roach foreach ($rows as $row) { 376f0a11419SGreg Roach if ($type === 'INDI') { 377f0a11419SGreg Roach $record = Individual::getInstance($row->xref, $tree, $row->gedcom); 378f0a11419SGreg Roach } else { 379f0a11419SGreg Roach $record = Family::getInstance($row->xref, $tree, $row->gedcom); 380f0a11419SGreg Roach } 381f0a11419SGreg Roach $anniv_date = new Date($row->d_type . ' ' . $row->d_day . ' ' . $row->d_month . ' ' . $row->d_year); 382f0a11419SGreg Roach foreach ($record->getFacts() as $fact) { 383f0a11419SGreg Roach if (($fact->getDate()->minimumJulianDay() === $anniv_date->minimumJulianDay() || $fact->getDate()->maximumJulianDay() === $anniv_date->maximumJulianDay()) && $fact->getTag() === $row->d_fact) { 3844a83f5d7SGreg Roach $fact->anniv = $row->d_year === '0' ? 0 : $anniv->year - $row->d_year; 385f0a11419SGreg Roach $fact->jd = $jd; 386f0a11419SGreg Roach $found_facts[] = $fact; 387f0a11419SGreg Roach } 388f0a11419SGreg Roach } 389f0a11419SGreg Roach } 390f0a11419SGreg Roach } 391f0a11419SGreg Roach } 392f0a11419SGreg Roach 393f0a11419SGreg Roach return $found_facts; 394f0a11419SGreg Roach } 395f0a11419SGreg Roach} 396