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 */ 16f0a11419SGreg Roachnamespace Fisharebest\Webtrees\Services; 17f0a11419SGreg Roach 18f0a11419SGreg Roachuse Fisharebest\Webtrees\Database; 19f0a11419SGreg Roachuse Fisharebest\Webtrees\Date; 20f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\FrenchDate; 21f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate; 22f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\HijriDate; 23f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\JalaliDate; 24f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\JewishDate; 25f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\JulianDate; 26f0a11419SGreg Roachuse Fisharebest\Webtrees\Fact; 27f0a11419SGreg Roachuse Fisharebest\Webtrees\Family; 28f0a11419SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 29f0a11419SGreg Roachuse Fisharebest\Webtrees\Individual; 30f0a11419SGreg Roachuse Fisharebest\Webtrees\Tree; 31f0a11419SGreg Roach 32f0a11419SGreg Roach/** 33f0a11419SGreg Roach * Calculate anniversaries, etc. 34f0a11419SGreg Roach */ 35f0a11419SGreg Roachclass CalendarService 36f0a11419SGreg Roach{ 37f0a11419SGreg Roach /** 38f0a11419SGreg Roach * Get a list of events which occured during a given date range. 39f0a11419SGreg Roach * 40f0a11419SGreg Roach * @param int $jd1 the start range of julian day 41f0a11419SGreg Roach * @param int $jd2 the end range of julian day 42f0a11419SGreg Roach * @param string $facts restrict the search to just these facts or leave blank for all 43f0a11419SGreg Roach * @param Tree $tree the tree to search 44f0a11419SGreg Roach * 45f0a11419SGreg Roach * @return Fact[] 46f0a11419SGreg Roach */ 47f0a11419SGreg Roach public function getCalendarEvents(int $jd1, int $jd2, string $facts, Tree $tree): array 48f0a11419SGreg Roach { 49f0a11419SGreg Roach // If no facts specified, get all except these 50f0a11419SGreg Roach $skipfacts = 'CHAN,BAPL,SLGC,SLGS,ENDL,CENS,RESI,NOTE,ADDR,OBJE,SOUR'; 51f0a11419SGreg Roach 52f0a11419SGreg Roach $found_facts = []; 53f0a11419SGreg Roach 54f0a11419SGreg Roach // Events that start or end during the period 55f0a11419SGreg Roach $where = "WHERE (d_julianday1>={$jd1} AND d_julianday1<={$jd2} OR d_julianday2>={$jd1} AND d_julianday2<={$jd2})"; 56f0a11419SGreg Roach 57f0a11419SGreg Roach // Restrict to certain types of fact 58f0a11419SGreg Roach if (empty($facts)) { 59f0a11419SGreg Roach $excl_facts = "'" . preg_replace('/\W+/', "','", $skipfacts) . "'"; 60f0a11419SGreg Roach $where .= " AND d_fact NOT IN ({$excl_facts})"; 61f0a11419SGreg Roach } else { 62f0a11419SGreg Roach $incl_facts = "'" . preg_replace('/\W+/', "','", $facts) . "'"; 63f0a11419SGreg Roach $where .= " AND d_fact IN ({$incl_facts})"; 64f0a11419SGreg Roach } 65f0a11419SGreg Roach // Only get events from the current gedcom 66f0a11419SGreg Roach $where .= " AND d_file=" . $tree->getTreeId(); 67f0a11419SGreg Roach 68f0a11419SGreg Roach // Now fetch these events 69f0a11419SGreg 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"; 70f0a11419SGreg 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"; 71f0a11419SGreg Roach 72f0a11419SGreg Roach foreach (['INDI' => $ind_sql, 'FAM' => $fam_sql] as $type => $sql) { 73f0a11419SGreg Roach $rows = Database::prepare($sql)->fetchAll(); 74f0a11419SGreg Roach 75f0a11419SGreg Roach foreach ($rows as $row) { 76f0a11419SGreg Roach if ($type === 'INDI') { 77f0a11419SGreg Roach $record = Individual::getInstance($row->xref, $tree, $row->gedcom); 78f0a11419SGreg Roach } else { 79f0a11419SGreg Roach $record = Family::getInstance($row->xref, $tree, $row->gedcom); 80f0a11419SGreg Roach } 81f0a11419SGreg Roach $anniv_date = new Date($row->d_type . ' ' . $row->d_day . ' ' . $row->d_month . ' ' . $row->d_year); 82f0a11419SGreg Roach foreach ($record->getFacts() as $fact) { 83f0a11419SGreg Roach // For date ranges, we need a match on either the start/end. 84f0a11419SGreg Roach if (($fact->getDate()->minimumJulianDay() === $anniv_date->minimumJulianDay() || $fact->getDate()->maximumJulianDay() == $anniv_date->maximumJulianDay()) && $fact->getTag() === $row->d_fact) { 85f0a11419SGreg Roach $fact->anniv = 0; 86f0a11419SGreg Roach $found_facts[] = $fact; 87f0a11419SGreg Roach } 88f0a11419SGreg Roach } 89f0a11419SGreg Roach } 90f0a11419SGreg Roach } 91f0a11419SGreg Roach 92f0a11419SGreg Roach return $found_facts; 93f0a11419SGreg Roach } 94f0a11419SGreg Roach 95f0a11419SGreg Roach /** 96f0a11419SGreg Roach * Get the list of current and upcoming events, sorted by anniversary date 97f0a11419SGreg Roach * 98f0a11419SGreg Roach * @param int $jd1 99f0a11419SGreg Roach * @param int $jd2 100f0a11419SGreg Roach * @param string $events 101f0a11419SGreg Roach * @param Boolean $only_living 102f0a11419SGreg Roach * @param string $sort_by 103f0a11419SGreg Roach * @param Tree $tree 104f0a11419SGreg Roach * 105f0a11419SGreg Roach * @return Fact[] 106f0a11419SGreg Roach */ 107f0a11419SGreg Roach public function getEventsList(int $jd1, int $jd2, string $events, bool $only_living, string $sort_by, Tree $tree): array 108f0a11419SGreg Roach { 109f0a11419SGreg Roach $found_facts = []; 110f0a11419SGreg Roach $facts = []; 111f0a11419SGreg Roach 112f0a11419SGreg Roach foreach (range($jd1, $jd2) as $jd) { 113f0a11419SGreg Roach $found_facts = array_merge($found_facts, $this->getAnniversaryEvents($jd, $events, $tree)); 114f0a11419SGreg Roach } 115f0a11419SGreg Roach 116f0a11419SGreg Roach foreach ($found_facts as $fact) { 117f0a11419SGreg Roach $record = $fact->getParent(); 118f0a11419SGreg Roach // only living people ? 119f0a11419SGreg Roach if ($only_living) { 120f0a11419SGreg Roach if ($record instanceof Individual && $record->isDead()) { 121f0a11419SGreg Roach continue; 122f0a11419SGreg Roach } 123f0a11419SGreg Roach if ($record instanceof Family) { 124f0a11419SGreg Roach $husb = $record->getHusband(); 125*8f038c36SRico Sonntag if ($husb === null || $husb->isDead()) { 126f0a11419SGreg Roach continue; 127f0a11419SGreg Roach } 128f0a11419SGreg Roach $wife = $record->getWife(); 129*8f038c36SRico Sonntag if ($wife === null || $wife->isDead()) { 130f0a11419SGreg Roach continue; 131f0a11419SGreg Roach } 132f0a11419SGreg Roach } 133f0a11419SGreg Roach } 134f0a11419SGreg Roach $facts[] = $fact; 135f0a11419SGreg Roach } 136f0a11419SGreg Roach 137f0a11419SGreg Roach switch ($sort_by) { 138f0a11419SGreg Roach case 'anniv': 139f0a11419SGreg Roach uasort($facts, function (Fact $x, Fact $y) { 140f0a11419SGreg Roach return Fact::compareDate($y, $x); 141f0a11419SGreg Roach }); 142f0a11419SGreg Roach break; 143f0a11419SGreg Roach case 'alpha': 144f0a11419SGreg Roach uasort($facts, function (Fact $x, Fact $y) { 145f0a11419SGreg Roach return GedcomRecord::compare($x->getParent(), $y->getParent()); 146f0a11419SGreg Roach }); 147f0a11419SGreg Roach break; 148f0a11419SGreg Roach } 149f0a11419SGreg Roach 150f0a11419SGreg Roach return $facts; 151f0a11419SGreg Roach } 152f0a11419SGreg Roach 153f0a11419SGreg Roach /** 154f0a11419SGreg Roach * Get a list of events whose anniversary occured on a given julian day. 155f0a11419SGreg Roach * Used on the on-this-day/upcoming blocks and the day/month calendar views. 156f0a11419SGreg Roach * 157f0a11419SGreg Roach * @param int $jd the julian day 158f0a11419SGreg Roach * @param string $facts restrict the search to just these facts or leave blank for all 159f0a11419SGreg Roach * @param Tree $tree the tree to search 160f0a11419SGreg Roach * 161f0a11419SGreg Roach * @return Fact[] 162f0a11419SGreg Roach */ 163f0a11419SGreg Roach public function getAnniversaryEvents($jd, $facts, Tree $tree): array 164f0a11419SGreg Roach { 165f0a11419SGreg Roach $found_facts = []; 166f0a11419SGreg Roach 167f0a11419SGreg Roach $anniversaries = [ 168f0a11419SGreg Roach new GregorianDate($jd), 169f0a11419SGreg Roach new JulianDate($jd), 170f0a11419SGreg Roach new FrenchDate($jd), 171f0a11419SGreg Roach new JewishDate($jd), 172f0a11419SGreg Roach new HijriDate($jd), 173f0a11419SGreg Roach new JalaliDate($jd), 174f0a11419SGreg Roach ]; 175f0a11419SGreg Roach 176f0a11419SGreg Roach foreach ($anniversaries as $anniv) { 177f0a11419SGreg Roach // Build a SQL where clause to match anniversaries in the appropriate calendar. 178f0a11419SGreg Roach $ind_sql = 179f0a11419SGreg Roach "SELECT DISTINCT i_id AS xref, i_gedcom AS gedcom, d_type, d_day, d_month, d_year, d_fact" . 180f0a11419SGreg Roach " FROM `##dates` JOIN `##individuals` ON d_gid = i_id AND d_file = i_file" . 181f0a11419SGreg Roach " WHERE d_type = :type AND d_file = :tree_id"; 182f0a11419SGreg Roach $fam_sql = 183f0a11419SGreg Roach "SELECT DISTINCT f_id AS xref, f_gedcom AS gedcom, d_type, d_day, d_month, d_year, d_fact" . 184f0a11419SGreg Roach " FROM `##dates` JOIN `##families` ON d_gid = f_id AND d_file = f_file" . 185f0a11419SGreg Roach " WHERE d_type = :type AND d_file = :tree_id"; 186f0a11419SGreg Roach $args = [ 187f0a11419SGreg Roach 'type' => $anniv->format('%@'), 188f0a11419SGreg Roach 'tree_id' => $tree->getTreeId(), 189f0a11419SGreg Roach ]; 190f0a11419SGreg Roach 191f0a11419SGreg Roach $where = ""; 192f0a11419SGreg Roach // SIMPLE CASES: 193f0a11419SGreg Roach // a) Non-hebrew anniversaries 194f0a11419SGreg Roach // b) Hebrew months TVT, SHV, IYR, SVN, TMZ, AAV, ELL 195f0a11419SGreg Roach if (!$anniv instanceof JewishDate || in_array($anniv->m, [ 196f0a11419SGreg Roach 1, 197f0a11419SGreg Roach 5, 198f0a11419SGreg Roach 6, 199f0a11419SGreg Roach 9, 200f0a11419SGreg Roach 10, 201f0a11419SGreg Roach 11, 202f0a11419SGreg Roach 12, 203f0a11419SGreg Roach 13, 204f0a11419SGreg Roach ])) { 205f0a11419SGreg Roach // Dates without days go on the first day of the month 206f0a11419SGreg Roach // Dates with invalid days go on the last day of the month 207f0a11419SGreg Roach if ($anniv->d === 1) { 208f0a11419SGreg Roach $where .= " AND d_day <= 1"; 209f0a11419SGreg Roach } elseif ($anniv->d === $anniv->daysInMonth()) { 210f0a11419SGreg Roach $where .= " AND d_day >= :day"; 211f0a11419SGreg Roach $args['day'] = $anniv->d; 212f0a11419SGreg Roach } else { 213f0a11419SGreg Roach $where .= " AND d_day = :day"; 214f0a11419SGreg Roach $args['day'] = $anniv->d; 215f0a11419SGreg Roach } 216f0a11419SGreg Roach $where .= " AND d_mon = :month"; 217f0a11419SGreg Roach $args['month'] = $anniv->m; 218f0a11419SGreg Roach } else { 219f0a11419SGreg Roach // SPECIAL CASES: 220f0a11419SGreg Roach switch ($anniv->m) { 221f0a11419SGreg Roach case 2: 222f0a11419SGreg Roach // 29 CSH does not include 30 CSH (but would include an invalid 31 CSH if there were no 30 CSH) 223f0a11419SGreg Roach if ($anniv->d === 1) { 224f0a11419SGreg Roach $where .= " AND d_day <= 1 AND d_mon = 2"; 225f0a11419SGreg Roach } elseif ($anniv->d === 30) { 226f0a11419SGreg Roach $where .= " AND d_day >= 30 AND d_mon = 2"; 227f0a11419SGreg Roach } elseif ($anniv->d === 29 && $anniv->daysInMonth() === 29) { 228f0a11419SGreg Roach $where .= " AND (d_day = 29 OR d_day > 30) AND d_mon = 2"; 229f0a11419SGreg Roach } else { 230f0a11419SGreg Roach $where .= " AND d_day = :day AND d_mon = 2"; 231f0a11419SGreg Roach $args['day'] = $anniv->d; 232f0a11419SGreg Roach } 233f0a11419SGreg Roach break; 234f0a11419SGreg Roach case 3: 235f0a11419SGreg Roach // 1 KSL includes 30 CSH (if this year didn’t have 30 CSH) 236f0a11419SGreg Roach // 29 KSL does not include 30 KSL (but would include an invalid 31 KSL if there were no 30 KSL) 237f0a11419SGreg Roach if ($anniv->d === 1) { 238f0a11419SGreg Roach $tmp = new JewishDate([ 239f0a11419SGreg Roach $anniv->y, 240f0a11419SGreg Roach 'CSH', 241f0a11419SGreg Roach 1, 242f0a11419SGreg Roach ]); 243f0a11419SGreg Roach if ($tmp->daysInMonth() === 29) { 244f0a11419SGreg Roach $where .= " AND (d_day <= 1 AND d_mon = 3 OR d_day = 30 AND d_mon = 2)"; 245f0a11419SGreg Roach } else { 246f0a11419SGreg Roach $where .= " AND d_day <= 1 AND d_mon = 3"; 247f0a11419SGreg Roach } 248f0a11419SGreg Roach } elseif ($anniv->d === 30) { 249f0a11419SGreg Roach $where .= " AND d_day >= 30 AND d_mon = 3"; 250f0a11419SGreg Roach } elseif ($anniv->d == 29 && $anniv->daysInMonth() === 29) { 251f0a11419SGreg Roach $where .= " AND (d_day = 29 OR d_day > 30) AND d_mon = 3"; 252f0a11419SGreg Roach } else { 253f0a11419SGreg Roach $where .= " AND d_day = :day AND d_mon = 3"; 254f0a11419SGreg Roach $args['day'] = $anniv->d; 255f0a11419SGreg Roach } 256f0a11419SGreg Roach break; 257f0a11419SGreg Roach case 4: 258f0a11419SGreg Roach // 1 TVT includes 30 KSL (if this year didn’t have 30 KSL) 259f0a11419SGreg Roach if ($anniv->d === 1) { 260f0a11419SGreg Roach $tmp = new JewishDate([ 261f0a11419SGreg Roach $anniv->y, 262f0a11419SGreg Roach 'KSL', 263f0a11419SGreg Roach 1, 264f0a11419SGreg Roach ]); 265f0a11419SGreg Roach if ($tmp->daysInMonth() === 29) { 266f0a11419SGreg Roach $where .= " AND (d_day <=1 AND d_mon = 4 OR d_day = 30 AND d_mon = 3)"; 267f0a11419SGreg Roach } else { 268f0a11419SGreg Roach $where .= " AND d_day <= 1 AND d_mon = 4"; 269f0a11419SGreg Roach } 270f0a11419SGreg Roach } elseif ($anniv->d === $anniv->daysInMonth()) { 271f0a11419SGreg Roach $where .= " AND d_day >= :day AND d_mon=4"; 272f0a11419SGreg Roach $args['day'] = $anniv->d; 273f0a11419SGreg Roach } else { 274f0a11419SGreg Roach $where .= " AND d_day = :day AND d_mon=4"; 275f0a11419SGreg Roach $args['day'] = $anniv->d; 276f0a11419SGreg Roach } 277f0a11419SGreg Roach break; 278f0a11419SGreg Roach case 7: // ADS includes ADR (non-leap) 279f0a11419SGreg Roach if ($anniv->d === 1) { 280f0a11419SGreg Roach $where .= " AND d_day <= 1"; 281f0a11419SGreg Roach } elseif ($anniv->d === $anniv->daysInMonth()) { 282f0a11419SGreg Roach $where .= " AND d_day >= :day"; 283f0a11419SGreg Roach $args['day'] = $anniv->d; 284f0a11419SGreg Roach } else { 285f0a11419SGreg Roach $where .= " AND d_day = :day"; 286f0a11419SGreg Roach $args['day'] = $anniv->d; 287f0a11419SGreg Roach } 288f0a11419SGreg Roach $where .= " AND (d_mon = 6 AND MOD(7 * d_year + 1, 19) >= 7 OR d_mon = 7)"; 289f0a11419SGreg Roach break; 290f0a11419SGreg Roach case 8: // 1 NSN includes 30 ADR, if this year is non-leap 291f0a11419SGreg Roach if ($anniv->d === 1) { 292f0a11419SGreg Roach if ($anniv->isLeapYear()) { 293f0a11419SGreg Roach $where .= " AND d_day <= 1 AND d_mon = 8"; 294f0a11419SGreg Roach } else { 295f0a11419SGreg Roach $where .= " AND (d_day <= 1 AND d_mon = 8 OR d_day = 30 AND d_mon = 6)"; 296f0a11419SGreg Roach } 297f0a11419SGreg Roach } elseif ($anniv->d === $anniv->daysInMonth()) { 298f0a11419SGreg Roach $where .= " AND d_day >= :day AND d_mon = 8"; 299f0a11419SGreg Roach $args['day'] = $anniv->d; 300f0a11419SGreg Roach } else { 301f0a11419SGreg Roach $where .= " AND d_day = :day AND d_mon = 8"; 302f0a11419SGreg Roach $args['day'] = $anniv->d; 303f0a11419SGreg Roach } 304f0a11419SGreg Roach break; 305f0a11419SGreg Roach } 306f0a11419SGreg Roach } 307f0a11419SGreg Roach // Only events in the past (includes dates without a year) 308f0a11419SGreg Roach $where .= " AND d_year <= :year"; 309f0a11419SGreg Roach $args['year'] = $anniv->y; 310f0a11419SGreg Roach 311f0a11419SGreg Roach if ($facts) { 312f0a11419SGreg Roach // Restrict to certain types of fact 313f0a11419SGreg Roach $where .= " AND d_fact IN ("; 314f0a11419SGreg Roach preg_match_all('/([_A-Z]+)/', $facts, $matches); 315f0a11419SGreg Roach foreach ($matches[1] as $n => $fact) { 316f0a11419SGreg Roach $where .= $n ? ", " : ""; 317f0a11419SGreg Roach $where .= ":fact_" . $n; 318f0a11419SGreg Roach $args['fact_' . $n] = $fact; 319f0a11419SGreg Roach } 320f0a11419SGreg Roach $where .= ")"; 321f0a11419SGreg Roach } else { 322f0a11419SGreg Roach // If no facts specified, get all except these 323f0a11419SGreg Roach $where .= " AND d_fact NOT IN ('CHAN', 'BAPL', 'SLGC', 'SLGS', 'ENDL', 'CENS', 'RESI', '_TODO')"; 324f0a11419SGreg Roach } 325f0a11419SGreg Roach 326f0a11419SGreg Roach $order_by = " ORDER BY d_day, d_year DESC"; 327f0a11419SGreg Roach 328f0a11419SGreg Roach // Now fetch these anniversaries 329f0a11419SGreg Roach foreach ([ 330f0a11419SGreg Roach 'INDI' => $ind_sql . $where . $order_by, 331f0a11419SGreg Roach 'FAM' => $fam_sql . $where . $order_by, 332f0a11419SGreg Roach ] as $type => $sql) { 333f0a11419SGreg Roach $rows = Database::prepare($sql)->execute($args)->fetchAll(); 334f0a11419SGreg Roach foreach ($rows as $row) { 335f0a11419SGreg Roach if ($type === 'INDI') { 336f0a11419SGreg Roach $record = Individual::getInstance($row->xref, $tree, $row->gedcom); 337f0a11419SGreg Roach } else { 338f0a11419SGreg Roach $record = Family::getInstance($row->xref, $tree, $row->gedcom); 339f0a11419SGreg Roach } 340f0a11419SGreg Roach $anniv_date = new Date($row->d_type . ' ' . $row->d_day . ' ' . $row->d_month . ' ' . $row->d_year); 341f0a11419SGreg Roach foreach ($record->getFacts() as $fact) { 342f0a11419SGreg Roach if (($fact->getDate()->minimumJulianDay() === $anniv_date->minimumJulianDay() || $fact->getDate()->maximumJulianDay() === $anniv_date->maximumJulianDay()) && $fact->getTag() === $row->d_fact) { 343f0a11419SGreg Roach $fact->anniv = $row->d_year === '0' ? 0 : $anniv->y - $row->d_year; 344f0a11419SGreg Roach $fact->jd = $jd; 345f0a11419SGreg Roach $found_facts[] = $fact; 346f0a11419SGreg Roach } 347f0a11419SGreg Roach } 348f0a11419SGreg Roach } 349f0a11419SGreg Roach } 350f0a11419SGreg Roach } 351f0a11419SGreg Roach 352f0a11419SGreg Roach return $found_facts; 353f0a11419SGreg Roach } 354f0a11419SGreg Roach} 355