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