xref: /webtrees/app/Fact.php (revision a8d7c2188c0c2face35654c4e719fcea3cc41882)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
2176692c8bSGreg Roach
22580a4d11SGreg Roachuse Closure;
2355105892SGreg Roachuse Fisharebest\Webtrees\Elements\RestrictionNotice;
248af6bbf8SGreg Roachuse Fisharebest\Webtrees\Services\GedcomService;
251c7a6874SGreg Roachuse Illuminate\Support\Collection;
268f5f5da8SGreg Roachuse InvalidArgumentException;
273976b470SGreg Roach
28ae71f733SGreg Roachuse function array_flip;
29ae71f733SGreg Roachuse function array_key_exists;
30ae71f733SGreg Roachuse function count;
31ae71f733SGreg Roachuse function e;
32ae71f733SGreg Roachuse function implode;
33ae71f733SGreg Roachuse function in_array;
34ae71f733SGreg Roachuse function preg_match;
35ae71f733SGreg Roachuse function preg_replace;
36dec352c1SGreg Roachuse function str_contains;
3755105892SGreg Roachuse function str_ends_with;
38*a8d7c218SGreg Roachuse function str_starts_with;
39ae71f733SGreg Roachuse function usort;
40ae71f733SGreg Roach
41a25f0a04SGreg Roach/**
4276692c8bSGreg Roach * A GEDCOM fact or event object.
43a25f0a04SGreg Roach */
44c1010edaSGreg Roachclass Fact
45c1010edaSGreg Roach{
4616d6367aSGreg Roach    private const FACT_ORDER = [
47bbe4546fSGreg Roach        'BIRT',
48bbe4546fSGreg Roach        '_HNM',
49bbe4546fSGreg Roach        'ALIA',
50bbe4546fSGreg Roach        '_AKA',
51bbe4546fSGreg Roach        '_AKAN',
52bbe4546fSGreg Roach        'ADOP',
53bbe4546fSGreg Roach        '_ADPF',
54bbe4546fSGreg Roach        '_ADPF',
55bbe4546fSGreg Roach        '_BRTM',
56bbe4546fSGreg Roach        'CHR',
57bbe4546fSGreg Roach        'BAPM',
58bbe4546fSGreg Roach        'FCOM',
59bbe4546fSGreg Roach        'CONF',
60bbe4546fSGreg Roach        'BARM',
61bbe4546fSGreg Roach        'BASM',
62bbe4546fSGreg Roach        'EDUC',
63bbe4546fSGreg Roach        'GRAD',
64bbe4546fSGreg Roach        '_DEG',
65bbe4546fSGreg Roach        'EMIG',
66bbe4546fSGreg Roach        'IMMI',
67bbe4546fSGreg Roach        'NATU',
68bbe4546fSGreg Roach        '_MILI',
69bbe4546fSGreg Roach        '_MILT',
70bbe4546fSGreg Roach        'ENGA',
71bbe4546fSGreg Roach        'MARB',
72bbe4546fSGreg Roach        'MARC',
73bbe4546fSGreg Roach        'MARL',
74bbe4546fSGreg Roach        '_MARI',
75bbe4546fSGreg Roach        '_MBON',
76bbe4546fSGreg Roach        'MARR',
77bbe4546fSGreg Roach        '_COML',
78bbe4546fSGreg Roach        '_STAT',
79bbe4546fSGreg Roach        '_SEPR',
80bbe4546fSGreg Roach        'DIVF',
81bbe4546fSGreg Roach        'MARS',
82bbe4546fSGreg Roach        'DIV',
83bbe4546fSGreg Roach        'ANUL',
84bbe4546fSGreg Roach        'CENS',
85bbe4546fSGreg Roach        'OCCU',
86bbe4546fSGreg Roach        'RESI',
87bbe4546fSGreg Roach        'PROP',
88bbe4546fSGreg Roach        'CHRA',
89bbe4546fSGreg Roach        'RETI',
90bbe4546fSGreg Roach        'FACT',
91bbe4546fSGreg Roach        'EVEN',
92bbe4546fSGreg Roach        '_NMR',
93bbe4546fSGreg Roach        '_NMAR',
94bbe4546fSGreg Roach        'NMR',
95bbe4546fSGreg Roach        'NCHI',
96bbe4546fSGreg Roach        'WILL',
97bbe4546fSGreg Roach        '_HOL',
98bbe4546fSGreg Roach        '_????_',
99bbe4546fSGreg Roach        'DEAT',
100bbe4546fSGreg Roach        '_FNRL',
101bbe4546fSGreg Roach        'CREM',
102bbe4546fSGreg Roach        'BURI',
103bbe4546fSGreg Roach        '_INTE',
104bbe4546fSGreg Roach        '_YART',
105bbe4546fSGreg Roach        '_NLIV',
106bbe4546fSGreg Roach        'PROB',
107bbe4546fSGreg Roach        'TITL',
108bbe4546fSGreg Roach        'COMM',
109bbe4546fSGreg Roach        'NATI',
110bbe4546fSGreg Roach        'CITN',
111bbe4546fSGreg Roach        'CAST',
112bbe4546fSGreg Roach        'RELI',
113bbe4546fSGreg Roach        'SSN',
114bbe4546fSGreg Roach        'IDNO',
115bbe4546fSGreg Roach        'TEMP',
116bbe4546fSGreg Roach        'SLGC',
117bbe4546fSGreg Roach        'BAPL',
118bbe4546fSGreg Roach        'CONL',
119bbe4546fSGreg Roach        'ENDL',
120bbe4546fSGreg Roach        'SLGS',
121701f5d18SGreg Roach        'NO',
122bbe4546fSGreg Roach        'ADDR',
123bbe4546fSGreg Roach        'PHON',
124bbe4546fSGreg Roach        'EMAIL',
125bbe4546fSGreg Roach        '_EMAIL',
126bbe4546fSGreg Roach        'EMAL',
127bbe4546fSGreg Roach        'FAX',
128bbe4546fSGreg Roach        'WWW',
129bbe4546fSGreg Roach        'URL',
130bbe4546fSGreg Roach        '_URL',
131544f68e3SGreg Roach        '_FSFTID',
132bbe4546fSGreg Roach        'AFN',
133bbe4546fSGreg Roach        'REFN',
134bbe4546fSGreg Roach        '_PRMN',
135bbe4546fSGreg Roach        'REF',
136bbe4546fSGreg Roach        'RIN',
137bbe4546fSGreg Roach        '_UID',
138bbe4546fSGreg Roach        'OBJE',
139bbe4546fSGreg Roach        'NOTE',
140bbe4546fSGreg Roach        'SOUR',
141701f5d18SGreg Roach        'CREA',
142bbe4546fSGreg Roach        'CHAN',
143bbe4546fSGreg Roach        '_TODO',
144bbe4546fSGreg Roach    ];
145bbe4546fSGreg Roach
14633c746f1SGreg Roach    // Unique identifier for this fact (currently implemented as a hash of the raw data).
14733c746f1SGreg Roach    private string $id;
148a25f0a04SGreg Roach
14933c746f1SGreg Roach    // The GEDCOM record from which this fact is taken
15033c746f1SGreg Roach    private GedcomRecord $record;
151a25f0a04SGreg Roach
15233c746f1SGreg Roach    // The raw GEDCOM data for this fact
15333c746f1SGreg Roach    private string $gedcom;
154a25f0a04SGreg Roach
15533c746f1SGreg Roach    // The GEDCOM tag for this record
15633c746f1SGreg Roach    private string $tag;
157a25f0a04SGreg Roach
15833c746f1SGreg Roach    private bool $pending_deletion = false;
159a25f0a04SGreg Roach
16033c746f1SGreg Roach    private bool $pending_addition = false;
161a25f0a04SGreg Roach
16233c746f1SGreg Roach    private Date $date;
163a25f0a04SGreg Roach
16433c746f1SGreg Roach    private Place $place;
165a25f0a04SGreg Roach
166bfed30e4SGreg Roach    // Used to sort facts
16733c746f1SGreg Roach    public int $sortOrder;
168a25f0a04SGreg Roach
169bfed30e4SGreg Roach    // Used by anniversary calculations
170bfed30e4SGreg Roach    public int $jd;
171bfed30e4SGreg Roach    public int $anniv;
172bfed30e4SGreg Roach
173a25f0a04SGreg Roach    /**
174a25f0a04SGreg Roach     * Create an event object from a gedcom fragment.
175a25f0a04SGreg Roach     * We need the parent object (to check privacy) and a (pseudo) fact ID to
176a25f0a04SGreg Roach     * identify the fact within the record.
177a25f0a04SGreg Roach     *
178a25f0a04SGreg Roach     * @param string       $gedcom
179a25f0a04SGreg Roach     * @param GedcomRecord $parent
180905ab80aSGreg Roach     * @param string       $id
181a25f0a04SGreg Roach     *
1828f5f5da8SGreg Roach     * @throws InvalidArgumentException
183a25f0a04SGreg Roach     */
18424f2a3afSGreg Roach    public function __construct(string $gedcom, GedcomRecord $parent, string $id)
185c1010edaSGreg Roach    {
1868d0ebef0SGreg Roach        if (preg_match('/^1 (' . Gedcom::REGEX_TAG . ')/', $gedcom, $match)) {
187a25f0a04SGreg Roach            $this->gedcom = $gedcom;
188e7766c08SGreg Roach            $this->record = $parent;
189905ab80aSGreg Roach            $this->id     = $id;
190a25f0a04SGreg Roach            $this->tag    = $match[1];
191a25f0a04SGreg Roach        } else {
19252ac7b12SGreg Roach            throw new InvalidArgumentException('Invalid GEDCOM data passed to Fact::_construct(' . $gedcom . ',' . $parent->xref() . ')');
193a25f0a04SGreg Roach        }
194a25f0a04SGreg Roach    }
195a25f0a04SGreg Roach
196a25f0a04SGreg Roach    /**
197a25f0a04SGreg Roach     * Get the value of level 1 data in the fact
198a25f0a04SGreg Roach     * Allow for multi-line values
199a25f0a04SGreg Roach     *
200baacc364SGreg Roach     * @return string
201a25f0a04SGreg Roach     */
202dfa848b8SGreg Roach    public function value(): string
203c1010edaSGreg Roach    {
204d247596dSGreg Roach        if (preg_match('/^1 ' . $this->tag . ' ?(.*(?:\n2 CONT ?.*)*)/', $this->gedcom, $match)) {
205a25f0a04SGreg Roach            return preg_replace("/\n2 CONT ?/", "\n", $match[1]);
206a25f0a04SGreg Roach        }
207b2ce94c6SRico Sonntag
208b2ce94c6SRico Sonntag        return '';
209a25f0a04SGreg Roach    }
210a25f0a04SGreg Roach
211a25f0a04SGreg Roach    /**
212a25f0a04SGreg Roach     * Get the record to which this fact links
213a25f0a04SGreg Roach     *
2140d15532eSGreg Roach     * @return Family|GedcomRecord|Individual|Location|Media|Note|Repository|Source|Submission|Submitter|null
215a25f0a04SGreg Roach     */
216dc124885SGreg Roach    public function target()
217c1010edaSGreg Roach    {
21862b52849SGreg Roach        if (!preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $this->value(), $match)) {
21962b52849SGreg Roach            return null;
22062b52849SGreg Roach        }
22162b52849SGreg Roach
22262b52849SGreg Roach        $xref = $match[1];
22362b52849SGreg Roach
224a25f0a04SGreg Roach        switch ($this->tag) {
225a25f0a04SGreg Roach            case 'FAMC':
226a25f0a04SGreg Roach            case 'FAMS':
227cc3a55ccSGreg Roach                return Registry::familyFactory()->make($xref, $this->record->tree());
228a25f0a04SGreg Roach            case 'HUSB':
229a25f0a04SGreg Roach            case 'WIFE':
23062b52849SGreg Roach            case 'ALIA':
231a25f0a04SGreg Roach            case 'CHIL':
23262b52849SGreg Roach            case '_ASSO':
233cc3a55ccSGreg Roach                return Registry::individualFactory()->make($xref, $this->record->tree());
23462b52849SGreg Roach            case 'ASSO':
23562b52849SGreg Roach                return
236cc3a55ccSGreg Roach                    Registry::individualFactory()->make($xref, $this->record->tree()) ??
237cc3a55ccSGreg Roach                    Registry::submitterFactory()->make($xref, $this->record->tree());
238a25f0a04SGreg Roach            case 'SOUR':
239cc3a55ccSGreg Roach                return Registry::sourceFactory()->make($xref, $this->record->tree());
240a25f0a04SGreg Roach            case 'OBJE':
241cc3a55ccSGreg Roach                return Registry::mediaFactory()->make($xref, $this->record->tree());
242a25f0a04SGreg Roach            case 'REPO':
243cc3a55ccSGreg Roach                return Registry::repositoryFactory()->make($xref, $this->record->tree());
244a25f0a04SGreg Roach            case 'NOTE':
245cc3a55ccSGreg Roach                return Registry::noteFactory()->make($xref, $this->record->tree());
24662b52849SGreg Roach            case 'ANCI':
24762b52849SGreg Roach            case 'DESI':
248ffc0a61fSGreg Roach            case 'SUBM':
249cc3a55ccSGreg Roach                return Registry::submitterFactory()->make($xref, $this->record->tree());
2501635452cSGreg Roach            case 'SUBN':
251cc3a55ccSGreg Roach                return Registry::submissionFactory()->make($xref, $this->record->tree());
2520d15532eSGreg Roach            case '_LOC':
253cc3a55ccSGreg Roach                return Registry::locationFactory()->make($xref, $this->record->tree());
254a25f0a04SGreg Roach            default:
255cc3a55ccSGreg Roach                return Registry::gedcomRecordFactory()->make($xref, $this->record->tree());
256a25f0a04SGreg Roach        }
257a25f0a04SGreg Roach    }
258a25f0a04SGreg Roach
259a25f0a04SGreg Roach    /**
260a25f0a04SGreg Roach     * Get the value of level 2 data in the fact
261a25f0a04SGreg Roach     *
262a25f0a04SGreg Roach     * @param string $tag
263a25f0a04SGreg Roach     *
264baacc364SGreg Roach     * @return string
265a25f0a04SGreg Roach     */
26624f2a3afSGreg Roach    public function attribute(string $tag): string
267c1010edaSGreg Roach    {
26810d6d973SGreg Roach        if (preg_match('/\n2 ' . $tag . '\b ?(.*(?:(?:\n3 CONT ?.*)*)*)/', $this->gedcom, $match)) {
26949985628SGreg Roach            $value = preg_replace("/\n3 CONT ?/", "\n", $match[1]);
27049985628SGreg Roach
2718939e2c2SGreg Roach            return Registry::elementFactory()->make($this->tag() . ':' . $tag)->canonical($value);
272a25f0a04SGreg Roach        }
273b2ce94c6SRico Sonntag
274b2ce94c6SRico Sonntag        return '';
275a25f0a04SGreg Roach    }
276a25f0a04SGreg Roach
277a25f0a04SGreg Roach    /**
2788af6bbf8SGreg Roach     * Get the PLAC:MAP:LATI for the fact.
2798af6bbf8SGreg Roach     *
28090949315SGreg Roach     * @return float|null
2818af6bbf8SGreg Roach     */
28290949315SGreg Roach    public function latitude(): ?float
2838af6bbf8SGreg Roach    {
2848af6bbf8SGreg Roach        if (preg_match('/\n4 LATI (.+)/', $this->gedcom, $match)) {
2858af6bbf8SGreg Roach            $gedcom_service = new GedcomService();
2868af6bbf8SGreg Roach
2878af6bbf8SGreg Roach            return $gedcom_service->readLatitude($match[1]);
2888af6bbf8SGreg Roach        }
2898af6bbf8SGreg Roach
2900ec592a7SDavid Drury        return null;
2918af6bbf8SGreg Roach    }
2928af6bbf8SGreg Roach
2938af6bbf8SGreg Roach    /**
2948af6bbf8SGreg Roach     * Get the PLAC:MAP:LONG for the fact.
2958af6bbf8SGreg Roach     *
29690949315SGreg Roach     * @return float|null
2978af6bbf8SGreg Roach     */
29890949315SGreg Roach    public function longitude(): ?float
2998af6bbf8SGreg Roach    {
300819ac503SRico Sonntag        if (preg_match('/\n4 LONG (.+)/', $this->gedcom, $match)) {
3018af6bbf8SGreg Roach            $gedcom_service = new GedcomService();
3028af6bbf8SGreg Roach
3038af6bbf8SGreg Roach            return $gedcom_service->readLongitude($match[1]);
3048af6bbf8SGreg Roach        }
3058af6bbf8SGreg Roach
3060ec592a7SDavid Drury        return null;
3078af6bbf8SGreg Roach    }
3088af6bbf8SGreg Roach
3098af6bbf8SGreg Roach    /**
310a25f0a04SGreg Roach     * Do the privacy rules allow us to display this fact to the current user
311a25f0a04SGreg Roach     *
312cbc1590aSGreg Roach     * @param int|null $access_level
313a25f0a04SGreg Roach     *
314cbc1590aSGreg Roach     * @return bool
315a25f0a04SGreg Roach     */
31635584196SGreg Roach    public function canShow(int $access_level = null): bool
317c1010edaSGreg Roach    {
3183529c469SGreg Roach        $access_level ??= Auth::accessLevel($this->record->tree());
3194b9ff166SGreg Roach
32055105892SGreg Roach        // Does this record have an explicit restriction notice?
3214c2188e4SGreg Roach        $element     = new RestrictionNotice('');
3224c2188e4SGreg Roach        $restriction = $element->canonical($this->attribute('RESN'));
32355105892SGreg Roach
324*a8d7c218SGreg Roach        if (str_starts_with($restriction, RestrictionNotice::VALUE_CONFIDENTIAL)) {
3254b9ff166SGreg Roach            return Auth::PRIV_NONE >= $access_level;
326a25f0a04SGreg Roach        }
32755105892SGreg Roach
328*a8d7c218SGreg Roach        if (str_starts_with($restriction, RestrictionNotice::VALUE_PRIVACY)) {
3294b9ff166SGreg Roach            return Auth::PRIV_USER >= $access_level;
330a25f0a04SGreg Roach        }
331*a8d7c218SGreg Roach        if (str_starts_with($restriction, RestrictionNotice::VALUE_NONE)) {
332a25f0a04SGreg Roach            return true;
333a25f0a04SGreg Roach        }
334a25f0a04SGreg Roach
335b0a785fcSGreg Roach        // A link to a record of the same type: NOTE=>NOTE, OBJE=>OBJE, SOUR=>SOUR, etc.
336b0a785fcSGreg Roach        // Use the privacy of the target record.
337b0a785fcSGreg Roach        $target = $this->target();
338b0a785fcSGreg Roach
339b0a785fcSGreg Roach        if ($target instanceof GedcomRecord && $target->tag() === $this->tag) {
340b0a785fcSGreg Roach            return $target->canShow($access_level);
341b0a785fcSGreg Roach        }
342b0a785fcSGreg Roach
343a25f0a04SGreg Roach        // Does this record have a default RESN?
344c0935879SGreg Roach        $xref                    = $this->record->xref();
345f4afa648SGreg Roach        $fact_privacy            = $this->record->tree()->getFactPrivacy();
346f4afa648SGreg Roach        $individual_fact_privacy = $this->record->tree()->getIndividualFactPrivacy();
347518bbdc1SGreg Roach        if (isset($individual_fact_privacy[$xref][$this->tag])) {
348518bbdc1SGreg Roach            return $individual_fact_privacy[$xref][$this->tag] >= $access_level;
349a25f0a04SGreg Roach        }
350518bbdc1SGreg Roach        if (isset($fact_privacy[$this->tag])) {
351518bbdc1SGreg Roach            return $fact_privacy[$this->tag] >= $access_level;
352a25f0a04SGreg Roach        }
353a25f0a04SGreg Roach
354a25f0a04SGreg Roach        // No restrictions - it must be public
355a25f0a04SGreg Roach        return true;
356a25f0a04SGreg Roach    }
357a25f0a04SGreg Roach
358a25f0a04SGreg Roach    /**
359a25f0a04SGreg Roach     * Check whether this fact is protected against edit
360a25f0a04SGreg Roach     *
361cbc1590aSGreg Roach     * @return bool
362a25f0a04SGreg Roach     */
3638f53f488SRico Sonntag    public function canEdit(): bool
364c1010edaSGreg Roach    {
3651450f098SGreg Roach        if ($this->isPendingDeletion()) {
3661450f098SGreg Roach            return false;
3671450f098SGreg Roach        }
3681450f098SGreg Roach
3691450f098SGreg Roach        if (Auth::isManager($this->record->tree())) {
3701450f098SGreg Roach            return true;
3711450f098SGreg Roach        }
3721450f098SGreg Roach
373a25f0a04SGreg Roach        // Members cannot edit RESN, CHAN and locked records
37455105892SGreg Roach        return Auth::isEditor($this->record->tree()) && !str_ends_with($this->attribute('RESN'), RestrictionNotice::VALUE_LOCKED) && $this->tag !== 'RESN' && $this->tag !== 'CHAN';
375a25f0a04SGreg Roach    }
376a25f0a04SGreg Roach
377a25f0a04SGreg Roach    /**
378e63974caSStefan Weil     * The place where the event occurred.
379a25f0a04SGreg Roach     *
380a25f0a04SGreg Roach     * @return Place
381a25f0a04SGreg Roach     */
3824fb14fcbSGreg Roach    public function place(): Place
383c1010edaSGreg Roach    {
384cc3a55ccSGreg Roach        $this->place ??= new Place($this->attribute('PLAC'), $this->record->tree());
385a25f0a04SGreg Roach
386a25f0a04SGreg Roach        return $this->place;
387a25f0a04SGreg Roach    }
388a25f0a04SGreg Roach
389a25f0a04SGreg Roach    /**
390a25f0a04SGreg Roach     * Get the date for this fact.
391a25f0a04SGreg Roach     * We can call this function many times, especially when sorting,
392a25f0a04SGreg Roach     * so keep a copy of the date.
393a25f0a04SGreg Roach     *
394a25f0a04SGreg Roach     * @return Date
395a25f0a04SGreg Roach     */
3962decada7SGreg Roach    public function date(): Date
397c1010edaSGreg Roach    {
398448d466cSGreg Roach        $this->date ??= new Date($this->attribute('DATE'));
399a25f0a04SGreg Roach
400a25f0a04SGreg Roach        return $this->date;
401a25f0a04SGreg Roach    }
402a25f0a04SGreg Roach
403a25f0a04SGreg Roach    /**
404a25f0a04SGreg Roach     * The raw GEDCOM data for this fact
405a25f0a04SGreg Roach     *
406a25f0a04SGreg Roach     * @return string
407a25f0a04SGreg Roach     */
408138ca96cSGreg Roach    public function gedcom(): string
409c1010edaSGreg Roach    {
410a25f0a04SGreg Roach        return $this->gedcom;
411a25f0a04SGreg Roach    }
412a25f0a04SGreg Roach
413a25f0a04SGreg Roach    /**
414a25f0a04SGreg Roach     * Get a (pseudo) primary key for this fact.
415a25f0a04SGreg Roach     *
416a25f0a04SGreg Roach     * @return string
417a25f0a04SGreg Roach     */
418905ab80aSGreg Roach    public function id(): string
419c1010edaSGreg Roach    {
420905ab80aSGreg Roach        return $this->id;
421a25f0a04SGreg Roach    }
422a25f0a04SGreg Roach
423a25f0a04SGreg Roach    /**
424a25f0a04SGreg Roach     * What is the tag (type) of this fact, such as BIRT, MARR or DEAT.
425a25f0a04SGreg Roach     *
426a25f0a04SGreg Roach     * @return string
427a25f0a04SGreg Roach     */
4289ecabc6aSGreg Roach    public function tag(): string
429c1010edaSGreg Roach    {
43002467d32SGreg Roach        return $this->record->tag() . ':' . $this->tag;
431a25f0a04SGreg Roach    }
432a25f0a04SGreg Roach
433a25f0a04SGreg Roach    /**
434decb512fSGreg Roach     * The GEDCOM record where this Fact came from
435a25f0a04SGreg Roach     *
436decb512fSGreg Roach     * @return GedcomRecord
437a25f0a04SGreg Roach     */
438decb512fSGreg Roach    public function record(): GedcomRecord
439c1010edaSGreg Roach    {
440e7766c08SGreg Roach        return $this->record;
441a25f0a04SGreg Roach    }
442a25f0a04SGreg Roach
443a25f0a04SGreg Roach    /**
444a25f0a04SGreg Roach     * Get the name of this fact type, for use as a label.
445a25f0a04SGreg Roach     *
446a25f0a04SGreg Roach     * @return string
447a25f0a04SGreg Roach     */
4487b7d8067SGreg Roach    public function label(): string
449c1010edaSGreg Roach    {
450d0fe92f3SGreg Roach        if (str_ends_with($this->tag(), ':NOTE') && preg_match('/^@' . Gedcom::REGEX_XREF . '@$/', $this->value())) {
451ae1057f3SGreg Roach            return I18N::translate('Shared note');
452ae1057f3SGreg Roach        }
453ae1057f3SGreg Roach
45470ac1a22SGreg Roach        // Marriages
45570ac1a22SGreg Roach        if ($this->tag() === 'FAM:MARR') {
45670ac1a22SGreg Roach            $element = Registry::elementFactory()->make('FAM:MARR:TYPE');
45770ac1a22SGreg Roach            $type = $this->attribute('TYPE');
45870ac1a22SGreg Roach
45970ac1a22SGreg Roach            if ($type !== '') {
46070ac1a22SGreg Roach                return $element->value($type, $this->record->tree());
46170ac1a22SGreg Roach            }
46270ac1a22SGreg Roach        }
46370ac1a22SGreg Roach
464a25f0a04SGreg Roach        // Custom FACT/EVEN - with a TYPE
46518475794SGreg Roach        if ($this->tag === 'FACT' || $this->tag === 'EVEN') {
46618475794SGreg Roach            $type = $this->attribute('TYPE');
46718475794SGreg Roach
46818475794SGreg Roach            if ($type !== '') {
4693f9fe4ddSGreg Roach                if (!str_contains($type, '%')) {
47018475794SGreg Roach                    // Allow user-translations of custom types.
47118475794SGreg Roach                    $translated = I18N::translate($type);
47218475794SGreg Roach
47318475794SGreg Roach                    if ($translated !== $type) {
47418475794SGreg Roach                        return $translated;
475a25f0a04SGreg Roach                    }
4763f9fe4ddSGreg Roach                }
4774e5adf68SGreg Roach
47818475794SGreg Roach                return e($type);
47918475794SGreg Roach            }
48018475794SGreg Roach        }
48118475794SGreg Roach
482455a30feSGreg Roach        return Registry::elementFactory()->make($this->tag())->label();
483a25f0a04SGreg Roach    }
484a25f0a04SGreg Roach
485a25f0a04SGreg Roach    /**
486a25f0a04SGreg Roach     * This is a newly deleted fact, pending approval.
4877e96c925SGreg Roach     *
4887e96c925SGreg Roach     * @return void
489a25f0a04SGreg Roach     */
490e364afe4SGreg Roach    public function setPendingDeletion(): void
491c1010edaSGreg Roach    {
492a25f0a04SGreg Roach        $this->pending_deletion = true;
493a25f0a04SGreg Roach        $this->pending_addition = false;
494a25f0a04SGreg Roach    }
495a25f0a04SGreg Roach
496a25f0a04SGreg Roach    /**
497a25f0a04SGreg Roach     * Is this a newly deleted fact, pending approval.
498a25f0a04SGreg Roach     *
499cbc1590aSGreg Roach     * @return bool
500a25f0a04SGreg Roach     */
5018f53f488SRico Sonntag    public function isPendingDeletion(): bool
502c1010edaSGreg Roach    {
503a25f0a04SGreg Roach        return $this->pending_deletion;
504a25f0a04SGreg Roach    }
505a25f0a04SGreg Roach
506a25f0a04SGreg Roach    /**
507a25f0a04SGreg Roach     * This is a newly added fact, pending approval.
5087e96c925SGreg Roach     *
5097e96c925SGreg Roach     * @return void
510a25f0a04SGreg Roach     */
511e364afe4SGreg Roach    public function setPendingAddition(): void
512c1010edaSGreg Roach    {
513a25f0a04SGreg Roach        $this->pending_addition = true;
514a25f0a04SGreg Roach        $this->pending_deletion = false;
515a25f0a04SGreg Roach    }
516a25f0a04SGreg Roach
517a25f0a04SGreg Roach    /**
518a25f0a04SGreg Roach     * Is this a newly added fact, pending approval.
519a25f0a04SGreg Roach     *
520cbc1590aSGreg Roach     * @return bool
521a25f0a04SGreg Roach     */
5228f53f488SRico Sonntag    public function isPendingAddition(): bool
523c1010edaSGreg Roach    {
524a25f0a04SGreg Roach        return $this->pending_addition;
525a25f0a04SGreg Roach    }
526a25f0a04SGreg Roach
527a25f0a04SGreg Roach    /**
528a25f0a04SGreg Roach     * A one-line summary of the fact - for charts, etc.
529a25f0a04SGreg Roach     *
530a25f0a04SGreg Roach     * @return string
531a25f0a04SGreg Roach     */
5328f53f488SRico Sonntag    public function summary(): string
533c1010edaSGreg Roach    {
53413abd6f3SGreg Roach        $attributes = [];
535dc124885SGreg Roach        $target     = $this->target();
536db7bb364SGreg Roach        if ($target instanceof GedcomRecord) {
53739ca88baSGreg Roach            $attributes[] = $target->fullName();
538a25f0a04SGreg Roach        } else {
539726d7b07SGreg Roach            // Fact value
54084586c02SGreg Roach            $value = $this->value();
541726d7b07SGreg Roach            if ($value !== '' && $value !== 'Y') {
542315eb316SGreg Roach                $attributes[] = '<bdi>' . e($value) . '</bdi>';
543a25f0a04SGreg Roach            }
544726d7b07SGreg Roach            // Fact date
5452decada7SGreg Roach            $date = $this->date();
546726d7b07SGreg Roach            if ($date->isOK()) {
547cc3a55ccSGreg Roach                if ($this->record instanceof Individual && in_array($this->tag, Gedcom::BIRTH_EVENTS, true) && $this->record->tree()->getPreference('SHOW_PARENTS_AGE')) {
548cc3a55ccSGreg Roach                    $attributes[] = $date->display() . view('fact-parents-age', ['individual' => $this->record, 'birth_date' => $date]);
549a25f0a04SGreg Roach                } else {
550a25f0a04SGreg Roach                    $attributes[] = $date->display();
551a25f0a04SGreg Roach                }
552726d7b07SGreg Roach            }
553726d7b07SGreg Roach            // Fact place
554e364afe4SGreg Roach            if ($this->place()->gedcomName() !== '') {
555392561bbSGreg Roach                $attributes[] = $this->place()->shortName();
556a25f0a04SGreg Roach            }
557a25f0a04SGreg Roach        }
5582d8f08abSGreg Roach
5597fe676e5SGreg Roach        $class = 'fact_' . $this->tag;
560a25f0a04SGreg Roach        if ($this->isPendingAddition()) {
56171a69701SGreg Roach            $class .= ' wt-new';
562a25f0a04SGreg Roach        } elseif ($this->isPendingDeletion()) {
56371a69701SGreg Roach            $class .= ' wt-old';
564a25f0a04SGreg Roach        }
5652d8f08abSGreg Roach
56606c87791SGreg Roach        $label = '<span class="label">' . $this->label() . '</span>';
56706c87791SGreg Roach        $value = '<span class="field" dir="auto">' . implode(' — ', $attributes) . '</span>';
56806c87791SGreg Roach
5692d8f08abSGreg Roach        /* I18N: a label/value pair, such as “Occupation: Farmer”. Some languages may need to change the punctuation. */
57006c87791SGreg Roach        return '<div class="' . $class . '">' . I18N::translate('%1$s: %2$s', $label, $value) . '</div>';
571a25f0a04SGreg Roach    }
572a25f0a04SGreg Roach
573a25f0a04SGreg Roach    /**
5749927f70fSGreg Roach     * A one-line summary of the fact - for the clipboard, etc.
5759927f70fSGreg Roach     *
5769927f70fSGreg Roach     * @return string
5779927f70fSGreg Roach     */
5789927f70fSGreg Roach    public function name(): string
5799927f70fSGreg Roach    {
5809927f70fSGreg Roach        $items  = [$this->label()];
5819927f70fSGreg Roach        $target = $this->target();
5829927f70fSGreg Roach
5839927f70fSGreg Roach        if ($target instanceof GedcomRecord) {
584315eb316SGreg Roach            $items[] = '<bdi>' . $target->fullName() . '</bdi>';
5859927f70fSGreg Roach        } else {
5869927f70fSGreg Roach            // Fact value
5879927f70fSGreg Roach            $value = $this->value();
5889927f70fSGreg Roach            if ($value !== '' && $value !== 'Y') {
589315eb316SGreg Roach                $items[] = '<bdi>' . e($value) . '</bdi>';
5909927f70fSGreg Roach            }
5919927f70fSGreg Roach
5929927f70fSGreg Roach            // Fact date
5939927f70fSGreg Roach            if ($this->date()->isOK()) {
5949927f70fSGreg Roach                $items[] = $this->date()->minimumDate()->format('%Y');
5959927f70fSGreg Roach            }
5969927f70fSGreg Roach
5979927f70fSGreg Roach            // Fact place
5989927f70fSGreg Roach            if ($this->place()->gedcomName() !== '') {
5999927f70fSGreg Roach                $items[] = $this->place()->shortName();
6009927f70fSGreg Roach            }
6019927f70fSGreg Roach        }
6029927f70fSGreg Roach
6039927f70fSGreg Roach        return implode(' — ', $items);
6049927f70fSGreg Roach    }
6059927f70fSGreg Roach
6069927f70fSGreg Roach    /**
607580a4d11SGreg Roach     * Helper functions to sort facts
608a25f0a04SGreg Roach     *
609c6921a17SGreg Roach     * @return Closure(Fact,Fact):int
610a25f0a04SGreg Roach     */
611580a4d11SGreg Roach    private static function dateComparator(): Closure
612c1010edaSGreg Roach    {
6136c2179e2SGreg Roach        return static function (Fact $a, Fact $b): int {
6142decada7SGreg Roach            if ($a->date()->isOK() && $b->date()->isOK()) {
615a25f0a04SGreg Roach                // If both events have dates, compare by date
6162decada7SGreg Roach                $ret = Date::compare($a->date(), $b->date());
617a25f0a04SGreg Roach
618dfa848b8SGreg Roach                if ($ret === 0) {
619580a4d11SGreg Roach                    // If dates overlap, compare by fact type
620580a4d11SGreg Roach                    $ret = self::typeComparator()($a, $b);
621a25f0a04SGreg Roach
622a25f0a04SGreg Roach                    // If the fact type is also the same, retain the initial order
623dfa848b8SGreg Roach                    if ($ret === 0) {
624580a4d11SGreg Roach                        $ret = $a->sortOrder <=> $b->sortOrder;
625a25f0a04SGreg Roach                    }
626a25f0a04SGreg Roach                }
627a25f0a04SGreg Roach
628a25f0a04SGreg Roach                return $ret;
629b2ce94c6SRico Sonntag            }
630b2ce94c6SRico Sonntag
631a25f0a04SGreg Roach            // One or both events have no date - retain the initial order
632580a4d11SGreg Roach            return $a->sortOrder <=> $b->sortOrder;
633580a4d11SGreg Roach        };
634a25f0a04SGreg Roach    }
635a25f0a04SGreg Roach
636a25f0a04SGreg Roach    /**
637580a4d11SGreg Roach     * Helper functions to sort facts.
638a25f0a04SGreg Roach     *
639c6921a17SGreg Roach     * @return Closure(Fact,Fact):int
640a25f0a04SGreg Roach     */
641580a4d11SGreg Roach    public static function typeComparator(): Closure
642c1010edaSGreg Roach    {
643bbe4546fSGreg Roach        static $factsort = [];
644a25f0a04SGreg Roach
64554c1ab5eSGreg Roach        if ($factsort === []) {
646bbe4546fSGreg Roach            $factsort = array_flip(self::FACT_ORDER);
647a25f0a04SGreg Roach        }
648a25f0a04SGreg Roach
6496c2179e2SGreg Roach        return static function (Fact $a, Fact $b) use ($factsort): int {
650a25f0a04SGreg Roach            // Facts from same families stay grouped together
651a25f0a04SGreg Roach            // Keep MARR and DIV from the same families from mixing with events from other FAMs
652a25f0a04SGreg Roach            // Use the original order in which the facts were added
653e7766c08SGreg Roach            if ($a->record instanceof Family && $b->record instanceof Family && $a->record !== $b->record) {
6546a12476fSGreg Roach                return $a->sortOrder <=> $b->sortOrder;
655a25f0a04SGreg Roach            }
656a25f0a04SGreg Roach
657a2143fbeSGreg Roach            // NO events sort as the non-event itself.
658a2143fbeSGreg Roach            $atag = $a->tag === 'NO' ? $a->value() : $a->tag;
659a2143fbeSGreg Roach            $btag = $b->tag === 'NO' ? $b->value() : $b->tag;
660a25f0a04SGreg Roach
661a25f0a04SGreg Roach            // Events not in the above list get mapped onto one that is.
662a25f0a04SGreg Roach            if (!array_key_exists($atag, $factsort)) {
6637a6ee1acSGreg Roach                $atag = '_????_';
664a25f0a04SGreg Roach            }
665a25f0a04SGreg Roach
666a25f0a04SGreg Roach            if (!array_key_exists($btag, $factsort)) {
6677a6ee1acSGreg Roach                $btag = '_????_';
668a25f0a04SGreg Roach            }
669a25f0a04SGreg Roach
670a25f0a04SGreg Roach            // - Don't let dated after DEAT/BURI facts sort non-dated facts before DEAT/BURI
671a25f0a04SGreg Roach            // - Treat dated after BURI facts as BURI instead
6723425616eSGreg Roach            if ($a->attribute('DATE') !== '' && $factsort[$atag] > $factsort['BURI'] && $factsort[$atag] < $factsort['CHAN']) {
673a25f0a04SGreg Roach                $atag = 'BURI';
674a25f0a04SGreg Roach            }
675a25f0a04SGreg Roach
6763425616eSGreg Roach            if ($b->attribute('DATE') !== '' && $factsort[$btag] > $factsort['BURI'] && $factsort[$btag] < $factsort['CHAN']) {
677a25f0a04SGreg Roach                $btag = 'BURI';
678a25f0a04SGreg Roach            }
679a25f0a04SGreg Roach
680a25f0a04SGreg Roach            // If facts are the same then put dated facts before non-dated facts
6816a12476fSGreg Roach            if ($atag === $btag) {
6823425616eSGreg Roach                if ($a->attribute('DATE') !== '' && $b->attribute('DATE') === '') {
683a25f0a04SGreg Roach                    return -1;
684a25f0a04SGreg Roach                }
685a25f0a04SGreg Roach
6863425616eSGreg Roach                if ($b->attribute('DATE') !== '' && $a->attribute('DATE') === '') {
687a25f0a04SGreg Roach                    return 1;
688a25f0a04SGreg Roach                }
689a25f0a04SGreg Roach
690a25f0a04SGreg Roach                // If no sorting preference, then keep original ordering
6916a12476fSGreg Roach                return $a->sortOrder <=> $b->sortOrder;
692a25f0a04SGreg Roach            }
693a25f0a04SGreg Roach
6946a12476fSGreg Roach            return $factsort[$atag] <=> $factsort[$btag];
695580a4d11SGreg Roach        };
696580a4d11SGreg Roach    }
697580a4d11SGreg Roach
698580a4d11SGreg Roach    /**
699580a4d11SGreg Roach     * A multi-key sort
700580a4d11SGreg Roach     * 1. First divide the facts into two arrays one set with dates and one set without dates
701580a4d11SGreg Roach     * 2. Sort each of the two new arrays, the date using the compare date function, the non-dated
702580a4d11SGreg Roach     * using the compare type function
703580a4d11SGreg Roach     * 3. Then merge the arrays back into the original array using the compare type function
704580a4d11SGreg Roach     *
70536779af1SGreg Roach     * @param Collection<int,Fact> $unsorted
706580a4d11SGreg Roach     *
70736779af1SGreg Roach     * @return Collection<int,Fact>
708580a4d11SGreg Roach     */
7098af3e5c1SGreg Roach    public static function sortFacts(Collection $unsorted): Collection
710580a4d11SGreg Roach    {
711580a4d11SGreg Roach        $dated    = [];
712580a4d11SGreg Roach        $nondated = [];
713580a4d11SGreg Roach        $sorted   = [];
714580a4d11SGreg Roach
715580a4d11SGreg Roach        // Split the array into dated and non-dated arrays
716580a4d11SGreg Roach        $order = 0;
717580a4d11SGreg Roach
718580a4d11SGreg Roach        foreach ($unsorted as $fact) {
719580a4d11SGreg Roach            $fact->sortOrder = $order;
720580a4d11SGreg Roach            $order++;
721580a4d11SGreg Roach
722580a4d11SGreg Roach            if ($fact->date()->isOK()) {
723580a4d11SGreg Roach                $dated[] = $fact;
724580a4d11SGreg Roach            } else {
725580a4d11SGreg Roach                $nondated[] = $fact;
726580a4d11SGreg Roach            }
727580a4d11SGreg Roach        }
728580a4d11SGreg Roach
729580a4d11SGreg Roach        usort($dated, self::dateComparator());
730580a4d11SGreg Roach        usort($nondated, self::typeComparator());
731580a4d11SGreg Roach
732580a4d11SGreg Roach        // Merge the arrays
733580a4d11SGreg Roach        $dc = count($dated);
734580a4d11SGreg Roach        $nc = count($nondated);
735580a4d11SGreg Roach        $i  = 0;
736580a4d11SGreg Roach        $j  = 0;
737580a4d11SGreg Roach
738580a4d11SGreg Roach        // while there is anything in the dated array continue merging
739580a4d11SGreg Roach        while ($i < $dc) {
740580a4d11SGreg Roach            // compare each fact by type to merge them in order
741580a4d11SGreg Roach            if ($j < $nc && self::typeComparator()($dated[$i], $nondated[$j]) > 0) {
742580a4d11SGreg Roach                $sorted[] = $nondated[$j];
743580a4d11SGreg Roach                $j++;
744580a4d11SGreg Roach            } else {
745580a4d11SGreg Roach                $sorted[] = $dated[$i];
746580a4d11SGreg Roach                $i++;
747580a4d11SGreg Roach            }
748580a4d11SGreg Roach        }
749580a4d11SGreg Roach
750580a4d11SGreg Roach        // get anything that might be left in the nondated array
751580a4d11SGreg Roach        while ($j < $nc) {
752580a4d11SGreg Roach            $sorted[] = $nondated[$j];
753580a4d11SGreg Roach            $j++;
754580a4d11SGreg Roach        }
755580a4d11SGreg Roach
7561c7a6874SGreg Roach        return new Collection($sorted);
757a25f0a04SGreg Roach    }
758a25f0a04SGreg Roach
759a25f0a04SGreg Roach    /**
760ae71f733SGreg Roach     * Sort fact/event tags using the same order that we use for facts.
761ae71f733SGreg Roach     *
76236779af1SGreg Roach     * @param Collection<int,string> $unsorted
763ae71f733SGreg Roach     *
76436779af1SGreg Roach     * @return Collection<int,string>
765ae71f733SGreg Roach     */
766ae71f733SGreg Roach    public static function sortFactTags(Collection $unsorted): Collection
767ae71f733SGreg Roach    {
768ae71f733SGreg Roach        $tag_order = array_flip(self::FACT_ORDER);
769ae71f733SGreg Roach
770ae71f733SGreg Roach        return $unsorted->sort(static function (string $x, string $y) use ($tag_order): int {
771ae71f733SGreg Roach            $sort_x = $tag_order[$x] ?? $tag_order['_????_'];
772ae71f733SGreg Roach            $sort_y = $tag_order[$y] ?? $tag_order['_????_'];
773ae71f733SGreg Roach
774ae71f733SGreg Roach            return $sort_x - $sort_y;
775ae71f733SGreg Roach        });
776ae71f733SGreg Roach    }
777ae71f733SGreg Roach
778ae71f733SGreg Roach    /**
779a25f0a04SGreg Roach     * Allow native PHP functions such as array_unique() to work with objects
780a25f0a04SGreg Roach     *
781a25f0a04SGreg Roach     * @return string
782a25f0a04SGreg Roach     */
783dfa848b8SGreg Roach    public function __toString(): string
784c1010edaSGreg Roach    {
785c0935879SGreg Roach        return $this->id . '@' . $this->record->xref();
786a25f0a04SGreg Roach    }
787a25f0a04SGreg Roach}
788