xref: /webtrees/app/Fact.php (revision ffc0a61f636c2f449ee87acaba2c9b803ca6e758)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 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
15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
2176692c8bSGreg Roach
22580a4d11SGreg Roachuse Closure;
233d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrint;
248af6bbf8SGreg Roachuse Fisharebest\Webtrees\Services\GedcomService;
251c7a6874SGreg Roachuse Illuminate\Support\Collection;
268f5f5da8SGreg Roachuse InvalidArgumentException;
273976b470SGreg Roach
281450f098SGreg Roachuse function strpos;
29a25f0a04SGreg Roach
30a25f0a04SGreg Roach/**
3176692c8bSGreg Roach * A GEDCOM fact or event object.
32a25f0a04SGreg Roach */
33c1010edaSGreg Roachclass Fact
34c1010edaSGreg Roach{
3516d6367aSGreg Roach    private const FACT_ORDER = [
36bbe4546fSGreg Roach        'BIRT',
37bbe4546fSGreg Roach        '_HNM',
38bbe4546fSGreg Roach        'ALIA',
39bbe4546fSGreg Roach        '_AKA',
40bbe4546fSGreg Roach        '_AKAN',
41bbe4546fSGreg Roach        'ADOP',
42bbe4546fSGreg Roach        '_ADPF',
43bbe4546fSGreg Roach        '_ADPF',
44bbe4546fSGreg Roach        '_BRTM',
45bbe4546fSGreg Roach        'CHR',
46bbe4546fSGreg Roach        'BAPM',
47bbe4546fSGreg Roach        'FCOM',
48bbe4546fSGreg Roach        'CONF',
49bbe4546fSGreg Roach        'BARM',
50bbe4546fSGreg Roach        'BASM',
51bbe4546fSGreg Roach        'EDUC',
52bbe4546fSGreg Roach        'GRAD',
53bbe4546fSGreg Roach        '_DEG',
54bbe4546fSGreg Roach        'EMIG',
55bbe4546fSGreg Roach        'IMMI',
56bbe4546fSGreg Roach        'NATU',
57bbe4546fSGreg Roach        '_MILI',
58bbe4546fSGreg Roach        '_MILT',
59bbe4546fSGreg Roach        'ENGA',
60bbe4546fSGreg Roach        'MARB',
61bbe4546fSGreg Roach        'MARC',
62bbe4546fSGreg Roach        'MARL',
63bbe4546fSGreg Roach        '_MARI',
64bbe4546fSGreg Roach        '_MBON',
65bbe4546fSGreg Roach        'MARR',
66bbe4546fSGreg Roach        'MARR_CIVIL',
67bbe4546fSGreg Roach        'MARR_RELIGIOUS',
68bbe4546fSGreg Roach        'MARR_PARTNERS',
69bbe4546fSGreg Roach        'MARR_UNKNOWN',
70bbe4546fSGreg Roach        '_COML',
71bbe4546fSGreg Roach        '_STAT',
72bbe4546fSGreg Roach        '_SEPR',
73bbe4546fSGreg Roach        'DIVF',
74bbe4546fSGreg Roach        'MARS',
75bbe4546fSGreg Roach        '_BIRT_CHIL',
76bbe4546fSGreg Roach        'DIV',
77bbe4546fSGreg Roach        'ANUL',
78bbe4546fSGreg Roach        '_BIRT_',
79bbe4546fSGreg Roach        '_MARR_',
80bbe4546fSGreg Roach        '_DEAT_',
81bbe4546fSGreg Roach        '_BURI_',
82bbe4546fSGreg Roach        'CENS',
83bbe4546fSGreg Roach        'OCCU',
84bbe4546fSGreg Roach        'RESI',
85bbe4546fSGreg Roach        'PROP',
86bbe4546fSGreg Roach        'CHRA',
87bbe4546fSGreg Roach        'RETI',
88bbe4546fSGreg Roach        'FACT',
89bbe4546fSGreg Roach        'EVEN',
90bbe4546fSGreg Roach        '_NMR',
91bbe4546fSGreg Roach        '_NMAR',
92bbe4546fSGreg Roach        'NMR',
93bbe4546fSGreg Roach        'NCHI',
94bbe4546fSGreg Roach        'WILL',
95bbe4546fSGreg Roach        '_HOL',
96bbe4546fSGreg Roach        '_????_',
97bbe4546fSGreg Roach        'DEAT',
98bbe4546fSGreg Roach        '_FNRL',
99bbe4546fSGreg Roach        'CREM',
100bbe4546fSGreg Roach        'BURI',
101bbe4546fSGreg Roach        '_INTE',
102bbe4546fSGreg Roach        '_YART',
103bbe4546fSGreg Roach        '_NLIV',
104bbe4546fSGreg Roach        'PROB',
105bbe4546fSGreg Roach        'TITL',
106bbe4546fSGreg Roach        'COMM',
107bbe4546fSGreg Roach        'NATI',
108bbe4546fSGreg Roach        'CITN',
109bbe4546fSGreg Roach        'CAST',
110bbe4546fSGreg Roach        'RELI',
111bbe4546fSGreg Roach        'SSN',
112bbe4546fSGreg Roach        'IDNO',
113bbe4546fSGreg Roach        'TEMP',
114bbe4546fSGreg Roach        'SLGC',
115bbe4546fSGreg Roach        'BAPL',
116bbe4546fSGreg Roach        'CONL',
117bbe4546fSGreg Roach        'ENDL',
118bbe4546fSGreg Roach        'SLGS',
119bbe4546fSGreg Roach        'ADDR',
120bbe4546fSGreg Roach        'PHON',
121bbe4546fSGreg Roach        'EMAIL',
122bbe4546fSGreg Roach        '_EMAIL',
123bbe4546fSGreg Roach        'EMAL',
124bbe4546fSGreg Roach        'FAX',
125bbe4546fSGreg Roach        'WWW',
126bbe4546fSGreg Roach        'URL',
127bbe4546fSGreg Roach        '_URL',
128bbe4546fSGreg Roach        'AFN',
129bbe4546fSGreg Roach        'REFN',
130bbe4546fSGreg Roach        '_PRMN',
131bbe4546fSGreg Roach        'REF',
132bbe4546fSGreg Roach        'RIN',
133bbe4546fSGreg Roach        '_UID',
134bbe4546fSGreg Roach        'OBJE',
135bbe4546fSGreg Roach        'NOTE',
136bbe4546fSGreg Roach        'SOUR',
137bbe4546fSGreg Roach        'CHAN',
138bbe4546fSGreg Roach        '_TODO',
139bbe4546fSGreg Roach    ];
140bbe4546fSGreg Roach
141a25f0a04SGreg Roach    /** @var string Unique identifier for this fact (currently implemented as a hash of the raw data). */
142905ab80aSGreg Roach    private $id;
143a25f0a04SGreg Roach
144a25f0a04SGreg Roach    /** @var GedcomRecord The GEDCOM record from which this fact is taken */
145e7766c08SGreg Roach    private $record;
146a25f0a04SGreg Roach
147a25f0a04SGreg Roach    /** @var string The raw GEDCOM data for this fact */
148a25f0a04SGreg Roach    private $gedcom;
149a25f0a04SGreg Roach
150a25f0a04SGreg Roach    /** @var string The GEDCOM tag for this record */
151a25f0a04SGreg Roach    private $tag;
152a25f0a04SGreg Roach
153cbc1590aSGreg Roach    /** @var bool Is this a recently deleted fact, pending approval? */
154a25f0a04SGreg Roach    private $pending_deletion = false;
155a25f0a04SGreg Roach
156cbc1590aSGreg Roach    /** @var bool Is this a recently added fact, pending approval? */
157a25f0a04SGreg Roach    private $pending_addition = false;
158a25f0a04SGreg Roach
159a25f0a04SGreg Roach    /** @var Date The date of this fact, from the “2 DATE …” attribute */
160a25f0a04SGreg Roach    private $date;
161a25f0a04SGreg Roach
162a25f0a04SGreg Roach    /** @var Place The place of this fact, from the “2 PLAC …” attribute */
163a25f0a04SGreg Roach    private $place;
164a25f0a04SGreg Roach
165580a4d11SGreg Roach    /** @var int Used by Functions::sortFacts() */
166580a4d11SGreg Roach    private $sortOrder;
167a25f0a04SGreg Roach
168a25f0a04SGreg Roach    /**
169a25f0a04SGreg Roach     * Create an event object from a gedcom fragment.
170a25f0a04SGreg Roach     * We need the parent object (to check privacy) and a (pseudo) fact ID to
171a25f0a04SGreg Roach     * identify the fact within the record.
172a25f0a04SGreg Roach     *
173a25f0a04SGreg Roach     * @param string       $gedcom
174a25f0a04SGreg Roach     * @param GedcomRecord $parent
175905ab80aSGreg Roach     * @param string       $id
176a25f0a04SGreg Roach     *
1778f5f5da8SGreg Roach     * @throws InvalidArgumentException
178a25f0a04SGreg Roach     */
179905ab80aSGreg Roach    public function __construct($gedcom, GedcomRecord $parent, $id)
180c1010edaSGreg Roach    {
1818d0ebef0SGreg Roach        if (preg_match('/^1 (' . Gedcom::REGEX_TAG . ')/', $gedcom, $match)) {
182a25f0a04SGreg Roach            $this->gedcom = $gedcom;
183e7766c08SGreg Roach            $this->record = $parent;
184905ab80aSGreg Roach            $this->id     = $id;
185a25f0a04SGreg Roach            $this->tag    = $match[1];
186a25f0a04SGreg Roach        } else {
1878f5f5da8SGreg Roach            throw new InvalidArgumentException('Invalid GEDCOM data passed to Fact::_construct(' . $gedcom . ')');
188a25f0a04SGreg Roach        }
189a25f0a04SGreg Roach    }
190a25f0a04SGreg Roach
191a25f0a04SGreg Roach    /**
192a25f0a04SGreg Roach     * Get the value of level 1 data in the fact
193a25f0a04SGreg Roach     * Allow for multi-line values
194a25f0a04SGreg Roach     *
195baacc364SGreg Roach     * @return string
196a25f0a04SGreg Roach     */
197dfa848b8SGreg Roach    public function value(): string
198c1010edaSGreg Roach    {
199a25f0a04SGreg Roach        if (preg_match('/^1 (?:' . $this->tag . ') ?(.*(?:(?:\n2 CONT ?.*)*))/', $this->gedcom, $match)) {
200a25f0a04SGreg Roach            return preg_replace("/\n2 CONT ?/", "\n", $match[1]);
201a25f0a04SGreg Roach        }
202b2ce94c6SRico Sonntag
203b2ce94c6SRico Sonntag        return '';
204a25f0a04SGreg Roach    }
205a25f0a04SGreg Roach
206a25f0a04SGreg Roach    /**
207a25f0a04SGreg Roach     * Get the record to which this fact links
208a25f0a04SGreg Roach     *
209188adf12SGreg Roach     * @return Individual|Family|Source|Repository|Media|Note|GedcomRecord|null
210a25f0a04SGreg Roach     */
211dc124885SGreg Roach    public function target()
212c1010edaSGreg Roach    {
21384586c02SGreg Roach        $xref = trim($this->value(), '@');
214a25f0a04SGreg Roach        switch ($this->tag) {
215a25f0a04SGreg Roach            case 'FAMC':
216a25f0a04SGreg Roach            case 'FAMS':
217f4afa648SGreg Roach                return Family::getInstance($xref, $this->record()->tree());
218a25f0a04SGreg Roach            case 'HUSB':
219a25f0a04SGreg Roach            case 'WIFE':
220a25f0a04SGreg Roach            case 'CHIL':
221f4afa648SGreg Roach                return Individual::getInstance($xref, $this->record()->tree());
222a25f0a04SGreg Roach            case 'SOUR':
223f4afa648SGreg Roach                return Source::getInstance($xref, $this->record()->tree());
224a25f0a04SGreg Roach            case 'OBJE':
225f4afa648SGreg Roach                return Media::getInstance($xref, $this->record()->tree());
226a25f0a04SGreg Roach            case 'REPO':
227f4afa648SGreg Roach                return Repository::getInstance($xref, $this->record()->tree());
228a25f0a04SGreg Roach            case 'NOTE':
229f4afa648SGreg Roach                return Note::getInstance($xref, $this->record()->tree());
230*ffc0a61fSGreg Roach            case 'SUBM':
231*ffc0a61fSGreg Roach                return Submitter::getInstance($xref, $this->record()->tree());
232a25f0a04SGreg Roach            default:
233f4afa648SGreg Roach                return GedcomRecord::getInstance($xref, $this->record()->tree());
234a25f0a04SGreg Roach        }
235a25f0a04SGreg Roach    }
236a25f0a04SGreg Roach
237a25f0a04SGreg Roach    /**
238a25f0a04SGreg Roach     * Get the value of level 2 data in the fact
239a25f0a04SGreg Roach     *
240a25f0a04SGreg Roach     * @param string $tag
241a25f0a04SGreg Roach     *
242baacc364SGreg Roach     * @return string
243a25f0a04SGreg Roach     */
244dfa848b8SGreg Roach    public function attribute($tag): string
245c1010edaSGreg Roach    {
246a25f0a04SGreg Roach        if (preg_match('/\n2 (?:' . $tag . ') ?(.*(?:(?:\n3 CONT ?.*)*)*)/', $this->gedcom, $match)) {
247a25f0a04SGreg Roach            return preg_replace("/\n3 CONT ?/", "\n", $match[1]);
248a25f0a04SGreg Roach        }
249b2ce94c6SRico Sonntag
250b2ce94c6SRico Sonntag        return '';
251a25f0a04SGreg Roach    }
252a25f0a04SGreg Roach
253a25f0a04SGreg Roach    /**
2548af6bbf8SGreg Roach     * Get the PLAC:MAP:LATI for the fact.
2558af6bbf8SGreg Roach     *
2568af6bbf8SGreg Roach     * @return float
2578af6bbf8SGreg Roach     */
2588af6bbf8SGreg Roach    public function latitude(): float
2598af6bbf8SGreg Roach    {
2608af6bbf8SGreg Roach        if (preg_match('/\n4 LATI (.+)/', $this->gedcom, $match)) {
2618af6bbf8SGreg Roach            $gedcom_service = new GedcomService();
2628af6bbf8SGreg Roach
2638af6bbf8SGreg Roach            return $gedcom_service->readLatitude($match[1]);
2648af6bbf8SGreg Roach        }
2658af6bbf8SGreg Roach
2668af6bbf8SGreg Roach        return 0.0;
2678af6bbf8SGreg Roach    }
2688af6bbf8SGreg Roach
2698af6bbf8SGreg Roach    /**
2708af6bbf8SGreg Roach     * Get the PLAC:MAP:LONG for the fact.
2718af6bbf8SGreg Roach     *
2728af6bbf8SGreg Roach     * @return float
2738af6bbf8SGreg Roach     */
2748af6bbf8SGreg Roach    public function longitude(): float
2758af6bbf8SGreg Roach    {
276819ac503SRico Sonntag        if (preg_match('/\n4 LONG (.+)/', $this->gedcom, $match)) {
2778af6bbf8SGreg Roach            $gedcom_service = new GedcomService();
2788af6bbf8SGreg Roach
2798af6bbf8SGreg Roach            return $gedcom_service->readLongitude($match[1]);
2808af6bbf8SGreg Roach        }
2818af6bbf8SGreg Roach
2828af6bbf8SGreg Roach        return 0.0;
2838af6bbf8SGreg Roach    }
2848af6bbf8SGreg Roach
2858af6bbf8SGreg Roach    /**
286a25f0a04SGreg Roach     * Do the privacy rules allow us to display this fact to the current user
287a25f0a04SGreg Roach     *
288cbc1590aSGreg Roach     * @param int|null $access_level
289a25f0a04SGreg Roach     *
290cbc1590aSGreg Roach     * @return bool
291a25f0a04SGreg Roach     */
29235584196SGreg Roach    public function canShow(int $access_level = null): bool
293c1010edaSGreg Roach    {
294d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->record->tree());
2954b9ff166SGreg Roach
296a25f0a04SGreg Roach        // Does this record have an explicit RESN?
29720ff464cSGreg Roach        if (strpos($this->gedcom, "\n2 RESN confidential") !== false) {
2984b9ff166SGreg Roach            return Auth::PRIV_NONE >= $access_level;
299a25f0a04SGreg Roach        }
30020ff464cSGreg Roach        if (strpos($this->gedcom, "\n2 RESN privacy") !== false) {
3014b9ff166SGreg Roach            return Auth::PRIV_USER >= $access_level;
302a25f0a04SGreg Roach        }
30320ff464cSGreg Roach        if (strpos($this->gedcom, "\n2 RESN none") !== false) {
304a25f0a04SGreg Roach            return true;
305a25f0a04SGreg Roach        }
306a25f0a04SGreg Roach
307a25f0a04SGreg Roach        // Does this record have a default RESN?
308c0935879SGreg Roach        $xref                    = $this->record->xref();
309f4afa648SGreg Roach        $fact_privacy            = $this->record->tree()->getFactPrivacy();
310f4afa648SGreg Roach        $individual_fact_privacy = $this->record->tree()->getIndividualFactPrivacy();
311518bbdc1SGreg Roach        if (isset($individual_fact_privacy[$xref][$this->tag])) {
312518bbdc1SGreg Roach            return $individual_fact_privacy[$xref][$this->tag] >= $access_level;
313a25f0a04SGreg Roach        }
314518bbdc1SGreg Roach        if (isset($fact_privacy[$this->tag])) {
315518bbdc1SGreg Roach            return $fact_privacy[$this->tag] >= $access_level;
316a25f0a04SGreg Roach        }
317a25f0a04SGreg Roach
318a25f0a04SGreg Roach        // No restrictions - it must be public
319a25f0a04SGreg Roach        return true;
320a25f0a04SGreg Roach    }
321a25f0a04SGreg Roach
322a25f0a04SGreg Roach    /**
323a25f0a04SGreg Roach     * Check whether this fact is protected against edit
324a25f0a04SGreg Roach     *
325cbc1590aSGreg Roach     * @return bool
326a25f0a04SGreg Roach     */
3278f53f488SRico Sonntag    public function canEdit(): bool
328c1010edaSGreg Roach    {
3291450f098SGreg Roach        if ($this->isPendingDeletion()) {
3301450f098SGreg Roach            return false;
3311450f098SGreg Roach        }
3321450f098SGreg Roach
3331450f098SGreg Roach        if (Auth::isManager($this->record->tree())) {
3341450f098SGreg Roach            return true;
3351450f098SGreg Roach        }
3361450f098SGreg Roach
337a25f0a04SGreg Roach        // Members cannot edit RESN, CHAN and locked records
3381450f098SGreg Roach        return Auth::isEditor($this->record->tree()) && strpos($this->gedcom, "\n2 RESN locked") === false && $this->getTag() !== 'RESN' && $this->getTag() !== 'CHAN';
339a25f0a04SGreg Roach    }
340a25f0a04SGreg Roach
341a25f0a04SGreg Roach    /**
342a25f0a04SGreg Roach     * The place where the event occured.
343a25f0a04SGreg Roach     *
344a25f0a04SGreg Roach     * @return Place
345a25f0a04SGreg Roach     */
3464fb14fcbSGreg Roach    public function place(): Place
347c1010edaSGreg Roach    {
348a25f0a04SGreg Roach        if ($this->place === null) {
349f4afa648SGreg Roach            $this->place = new Place($this->attribute('PLAC'), $this->record()->tree());
350a25f0a04SGreg Roach        }
351a25f0a04SGreg Roach
352a25f0a04SGreg Roach        return $this->place;
353a25f0a04SGreg Roach    }
354a25f0a04SGreg Roach
355a25f0a04SGreg Roach    /**
356a25f0a04SGreg Roach     * Get the date for this fact.
357a25f0a04SGreg Roach     * We can call this function many times, especially when sorting,
358a25f0a04SGreg Roach     * so keep a copy of the date.
359a25f0a04SGreg Roach     *
360a25f0a04SGreg Roach     * @return Date
361a25f0a04SGreg Roach     */
3622decada7SGreg Roach    public function date(): Date
363c1010edaSGreg Roach    {
364a25f0a04SGreg Roach        if ($this->date === null) {
3653425616eSGreg Roach            $this->date = new Date($this->attribute('DATE'));
366a25f0a04SGreg Roach        }
367a25f0a04SGreg Roach
368a25f0a04SGreg Roach        return $this->date;
369a25f0a04SGreg Roach    }
370a25f0a04SGreg Roach
371a25f0a04SGreg Roach    /**
372a25f0a04SGreg Roach     * The raw GEDCOM data for this fact
373a25f0a04SGreg Roach     *
374a25f0a04SGreg Roach     * @return string
375a25f0a04SGreg Roach     */
376138ca96cSGreg Roach    public function gedcom(): string
377c1010edaSGreg Roach    {
378a25f0a04SGreg Roach        return $this->gedcom;
379a25f0a04SGreg Roach    }
380a25f0a04SGreg Roach
381a25f0a04SGreg Roach    /**
382a25f0a04SGreg Roach     * Get a (pseudo) primary key for this fact.
383a25f0a04SGreg Roach     *
384a25f0a04SGreg Roach     * @return string
385a25f0a04SGreg Roach     */
386905ab80aSGreg Roach    public function id(): string
387c1010edaSGreg Roach    {
388905ab80aSGreg Roach        return $this->id;
389a25f0a04SGreg Roach    }
390a25f0a04SGreg Roach
391a25f0a04SGreg Roach    /**
392a25f0a04SGreg Roach     * What is the tag (type) of this fact, such as BIRT, MARR or DEAT.
393a25f0a04SGreg Roach     *
394a25f0a04SGreg Roach     * @return string
395a25f0a04SGreg Roach     */
3968f53f488SRico Sonntag    public function getTag(): string
397c1010edaSGreg Roach    {
398a25f0a04SGreg Roach        return $this->tag;
399a25f0a04SGreg Roach    }
400a25f0a04SGreg Roach
401a25f0a04SGreg Roach    /**
402a25f0a04SGreg Roach     * Used to convert a real fact (e.g. BIRT) into a close-relative’s fact (e.g. _BIRT_CHIL)
403a25f0a04SGreg Roach     *
404a25f0a04SGreg Roach     * @param string $tag
4057e96c925SGreg Roach     *
4067e96c925SGreg Roach     * @return void
407a25f0a04SGreg Roach     */
408e364afe4SGreg Roach    public function setTag($tag): void
409c1010edaSGreg Roach    {
410a25f0a04SGreg Roach        $this->tag = $tag;
411a25f0a04SGreg Roach    }
412a25f0a04SGreg Roach
413a25f0a04SGreg Roach    /**
414a25f0a04SGreg Roach     * The Person/Family record where this Fact came from
415a25f0a04SGreg Roach     *
4166e59b7c4SGreg Roach     * @return Individual|Family|Source|Repository|Media|Note|GedcomRecord
417a25f0a04SGreg Roach     */
418e7766c08SGreg Roach    public function record()
419c1010edaSGreg Roach    {
420e7766c08SGreg Roach        return $this->record;
421a25f0a04SGreg Roach    }
422a25f0a04SGreg Roach
423a25f0a04SGreg Roach    /**
424a25f0a04SGreg Roach     * Get the name of this fact type, for use as a label.
425a25f0a04SGreg Roach     *
426a25f0a04SGreg Roach     * @return string
427a25f0a04SGreg Roach     */
4287b7d8067SGreg Roach    public function label(): string
429c1010edaSGreg Roach    {
430a25f0a04SGreg Roach        // Custom FACT/EVEN - with a TYPE
4313425616eSGreg Roach        if (($this->tag === 'FACT' || $this->tag === 'EVEN') && $this->attribute('TYPE') !== '') {
4323425616eSGreg Roach            return I18N::translate(e($this->attribute('TYPE')));
433a25f0a04SGreg Roach        }
4344e5adf68SGreg Roach
435e7766c08SGreg Roach        return GedcomTag::getLabel($this->tag, $this->record);
436a25f0a04SGreg Roach    }
437a25f0a04SGreg Roach
438a25f0a04SGreg Roach    /**
439a25f0a04SGreg Roach     * This is a newly deleted fact, pending approval.
4407e96c925SGreg Roach     *
4417e96c925SGreg Roach     * @return void
442a25f0a04SGreg Roach     */
443e364afe4SGreg Roach    public function setPendingDeletion(): void
444c1010edaSGreg Roach    {
445a25f0a04SGreg Roach        $this->pending_deletion = true;
446a25f0a04SGreg Roach        $this->pending_addition = false;
447a25f0a04SGreg Roach    }
448a25f0a04SGreg Roach
449a25f0a04SGreg Roach    /**
450a25f0a04SGreg Roach     * Is this a newly deleted fact, pending approval.
451a25f0a04SGreg Roach     *
452cbc1590aSGreg Roach     * @return bool
453a25f0a04SGreg Roach     */
4548f53f488SRico Sonntag    public function isPendingDeletion(): bool
455c1010edaSGreg Roach    {
456a25f0a04SGreg Roach        return $this->pending_deletion;
457a25f0a04SGreg Roach    }
458a25f0a04SGreg Roach
459a25f0a04SGreg Roach    /**
460a25f0a04SGreg Roach     * This is a newly added fact, pending approval.
4617e96c925SGreg Roach     *
4627e96c925SGreg Roach     * @return void
463a25f0a04SGreg Roach     */
464e364afe4SGreg Roach    public function setPendingAddition(): void
465c1010edaSGreg Roach    {
466a25f0a04SGreg Roach        $this->pending_addition = true;
467a25f0a04SGreg Roach        $this->pending_deletion = false;
468a25f0a04SGreg Roach    }
469a25f0a04SGreg Roach
470a25f0a04SGreg Roach    /**
471a25f0a04SGreg Roach     * Is this a newly added fact, pending approval.
472a25f0a04SGreg Roach     *
473cbc1590aSGreg Roach     * @return bool
474a25f0a04SGreg Roach     */
4758f53f488SRico Sonntag    public function isPendingAddition(): bool
476c1010edaSGreg Roach    {
477a25f0a04SGreg Roach        return $this->pending_addition;
478a25f0a04SGreg Roach    }
479a25f0a04SGreg Roach
480a25f0a04SGreg Roach    /**
481a25f0a04SGreg Roach     * Source citations linked to this fact
482a25f0a04SGreg Roach     *
483a25f0a04SGreg Roach     * @return string[]
484a25f0a04SGreg Roach     */
4858f53f488SRico Sonntag    public function getCitations(): array
486c1010edaSGreg Roach    {
4878d0ebef0SGreg Roach        preg_match_all('/\n(2 SOUR @(' . Gedcom::REGEX_XREF . ')@(?:\n[3-9] .*)*)/', $this->gedcom(), $matches, PREG_SET_ORDER);
48813abd6f3SGreg Roach        $citations = [];
489a25f0a04SGreg Roach        foreach ($matches as $match) {
490f4afa648SGreg Roach            $source = Source::getInstance($match[2], $this->record()->tree());
4912859ecedSJonathan Jaubart            if ($source && $source->canShow()) {
492a25f0a04SGreg Roach                $citations[] = $match[1];
493a25f0a04SGreg Roach            }
494a25f0a04SGreg Roach        }
495a25f0a04SGreg Roach
496a25f0a04SGreg Roach        return $citations;
497a25f0a04SGreg Roach    }
498a25f0a04SGreg Roach
499a25f0a04SGreg Roach    /**
500a25f0a04SGreg Roach     * Notes (inline and objects) linked to this fact
501a25f0a04SGreg Roach     *
502a25f0a04SGreg Roach     * @return string[]|Note[]
503a25f0a04SGreg Roach     */
5048f53f488SRico Sonntag    public function getNotes(): array
505c1010edaSGreg Roach    {
50613abd6f3SGreg Roach        $notes = [];
507138ca96cSGreg Roach        preg_match_all('/\n2 NOTE ?(.*(?:\n3.*)*)/', $this->gedcom(), $matches);
508a25f0a04SGreg Roach        foreach ($matches[1] as $match) {
509a25f0a04SGreg Roach            $note = preg_replace("/\n3 CONT ?/", "\n", $match);
5108d0ebef0SGreg Roach            if (preg_match('/@(' . Gedcom::REGEX_XREF . ')@/', $note, $nmatch)) {
511f4afa648SGreg Roach                $note = Note::getInstance($nmatch[1], $this->record()->tree());
512a25f0a04SGreg Roach                if ($note && $note->canShow()) {
513a25f0a04SGreg Roach                    // A note object
514a25f0a04SGreg Roach                    $notes[] = $note;
515a25f0a04SGreg Roach                }
516a25f0a04SGreg Roach            } else {
517a25f0a04SGreg Roach                // An inline note
518a25f0a04SGreg Roach                $notes[] = $note;
519a25f0a04SGreg Roach            }
520a25f0a04SGreg Roach        }
521a25f0a04SGreg Roach
522a25f0a04SGreg Roach        return $notes;
523a25f0a04SGreg Roach    }
524a25f0a04SGreg Roach
525a25f0a04SGreg Roach    /**
526a25f0a04SGreg Roach     * Media objects linked to this fact
527a25f0a04SGreg Roach     *
528a25f0a04SGreg Roach     * @return Media[]
529a25f0a04SGreg Roach     */
5308f53f488SRico Sonntag    public function getMedia(): array
531c1010edaSGreg Roach    {
53213abd6f3SGreg Roach        $media = [];
5338d0ebef0SGreg Roach        preg_match_all('/\n2 OBJE @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom(), $matches);
534a25f0a04SGreg Roach        foreach ($matches[1] as $match) {
535f4afa648SGreg Roach            $obje = Media::getInstance($match, $this->record()->tree());
5362859ecedSJonathan Jaubart            if ($obje && $obje->canShow()) {
537a25f0a04SGreg Roach                $media[] = $obje;
538a25f0a04SGreg Roach            }
539a25f0a04SGreg Roach        }
540a25f0a04SGreg Roach
541a25f0a04SGreg Roach        return $media;
542a25f0a04SGreg Roach    }
543a25f0a04SGreg Roach
544a25f0a04SGreg Roach    /**
545a25f0a04SGreg Roach     * A one-line summary of the fact - for charts, etc.
546a25f0a04SGreg Roach     *
547a25f0a04SGreg Roach     * @return string
548a25f0a04SGreg Roach     */
5498f53f488SRico Sonntag    public function summary(): string
550c1010edaSGreg Roach    {
55113abd6f3SGreg Roach        $attributes = [];
552dc124885SGreg Roach        $target     = $this->target();
553db7bb364SGreg Roach        if ($target instanceof GedcomRecord) {
55439ca88baSGreg Roach            $attributes[] = $target->fullName();
555a25f0a04SGreg Roach        } else {
556726d7b07SGreg Roach            // Fact value
55784586c02SGreg Roach            $value = $this->value();
558726d7b07SGreg Roach            if ($value !== '' && $value !== 'Y') {
559d53324c9SGreg Roach                $attributes[] = '<span dir="auto">' . e($value) . '</span>';
560a25f0a04SGreg Roach            }
561726d7b07SGreg Roach            // Fact date
5622decada7SGreg Roach            $date = $this->date();
563726d7b07SGreg Roach            if ($date->isOK()) {
564e364afe4SGreg Roach                if ($this->record() instanceof Individual && in_array($this->getTag(), Gedcom::BIRTH_EVENTS, true) && $this->record()->tree()->getPreference('SHOW_PARENTS_AGE')) {
565e7766c08SGreg Roach                    $attributes[] = $date->display() . FunctionsPrint::formatParentsAges($this->record(), $date);
566a25f0a04SGreg Roach                } else {
567a25f0a04SGreg Roach                    $attributes[] = $date->display();
568a25f0a04SGreg Roach                }
569726d7b07SGreg Roach            }
570726d7b07SGreg Roach            // Fact place
571e364afe4SGreg Roach            if ($this->place()->gedcomName() !== '') {
572392561bbSGreg Roach                $attributes[] = $this->place()->shortName();
573a25f0a04SGreg Roach            }
574a25f0a04SGreg Roach        }
5752d8f08abSGreg Roach
5762d8f08abSGreg Roach        $class = 'fact_' . $this->getTag();
577a25f0a04SGreg Roach        if ($this->isPendingAddition()) {
5782d8f08abSGreg Roach            $class .= ' new';
579a25f0a04SGreg Roach        } elseif ($this->isPendingDeletion()) {
5802d8f08abSGreg Roach            $class .= ' old';
581a25f0a04SGreg Roach        }
5822d8f08abSGreg Roach
5832d8f08abSGreg Roach        return
5842d8f08abSGreg Roach            '<div class="' . $class . '">' .
5852d8f08abSGreg Roach            /* I18N: a label/value pair, such as “Occupation: Farmer”. Some languages may need to change the punctuation. */
5867b7d8067SGreg Roach            I18N::translate('<span class="label">%1$s:</span> <span class="field" dir="auto">%2$s</span>', $this->label(), implode(' — ', $attributes)) .
5872d8f08abSGreg Roach            '</div>';
588a25f0a04SGreg Roach    }
589a25f0a04SGreg Roach
590a25f0a04SGreg Roach    /**
591580a4d11SGreg Roach     * Helper functions to sort facts
592a25f0a04SGreg Roach     *
593580a4d11SGreg Roach     * @return Closure
594a25f0a04SGreg Roach     */
595580a4d11SGreg Roach    private static function dateComparator(): Closure
596c1010edaSGreg Roach    {
5976c2179e2SGreg Roach        return static function (Fact $a, Fact $b): int {
5982decada7SGreg Roach            if ($a->date()->isOK() && $b->date()->isOK()) {
599a25f0a04SGreg Roach                // If both events have dates, compare by date
6002decada7SGreg Roach                $ret = Date::compare($a->date(), $b->date());
601a25f0a04SGreg Roach
602dfa848b8SGreg Roach                if ($ret === 0) {
603580a4d11SGreg Roach                    // If dates overlap, compare by fact type
604580a4d11SGreg Roach                    $ret = self::typeComparator()($a, $b);
605a25f0a04SGreg Roach
606a25f0a04SGreg Roach                    // If the fact type is also the same, retain the initial order
607dfa848b8SGreg Roach                    if ($ret === 0) {
608580a4d11SGreg Roach                        $ret = $a->sortOrder <=> $b->sortOrder;
609a25f0a04SGreg Roach                    }
610a25f0a04SGreg Roach                }
611a25f0a04SGreg Roach
612a25f0a04SGreg Roach                return $ret;
613b2ce94c6SRico Sonntag            }
614b2ce94c6SRico Sonntag
615a25f0a04SGreg Roach            // One or both events have no date - retain the initial order
616580a4d11SGreg Roach            return $a->sortOrder <=> $b->sortOrder;
617580a4d11SGreg Roach        };
618a25f0a04SGreg Roach    }
619a25f0a04SGreg Roach
620a25f0a04SGreg Roach    /**
621580a4d11SGreg Roach     * Helper functions to sort facts.
622a25f0a04SGreg Roach     *
623580a4d11SGreg Roach     * @return Closure
624a25f0a04SGreg Roach     */
625580a4d11SGreg Roach    public static function typeComparator(): Closure
626c1010edaSGreg Roach    {
627bbe4546fSGreg Roach        static $factsort = [];
628a25f0a04SGreg Roach
62954c1ab5eSGreg Roach        if ($factsort === []) {
630bbe4546fSGreg Roach            $factsort = array_flip(self::FACT_ORDER);
631a25f0a04SGreg Roach        }
632a25f0a04SGreg Roach
6336c2179e2SGreg Roach        return static function (Fact $a, Fact $b) use ($factsort): int {
634a25f0a04SGreg Roach            // Facts from same families stay grouped together
635a25f0a04SGreg Roach            // Keep MARR and DIV from the same families from mixing with events from other FAMs
636a25f0a04SGreg Roach            // Use the original order in which the facts were added
637e7766c08SGreg Roach            if ($a->record instanceof Family && $b->record instanceof Family && $a->record !== $b->record) {
638a25f0a04SGreg Roach                return $a->sortOrder - $b->sortOrder;
639a25f0a04SGreg Roach            }
640a25f0a04SGreg Roach
641a25f0a04SGreg Roach            $atag = $a->getTag();
642a25f0a04SGreg Roach            $btag = $b->getTag();
643a25f0a04SGreg Roach
644a25f0a04SGreg Roach            // Events not in the above list get mapped onto one that is.
645a25f0a04SGreg Roach            if (!array_key_exists($atag, $factsort)) {
646a25f0a04SGreg Roach                if (preg_match('/^(_(BIRT|MARR|DEAT|BURI)_)/', $atag, $match)) {
647a25f0a04SGreg Roach                    $atag = $match[1];
648a25f0a04SGreg Roach                } else {
6497a6ee1acSGreg Roach                    $atag = '_????_';
650a25f0a04SGreg Roach                }
651a25f0a04SGreg Roach            }
652a25f0a04SGreg Roach
653a25f0a04SGreg Roach            if (!array_key_exists($btag, $factsort)) {
654a25f0a04SGreg Roach                if (preg_match('/^(_(BIRT|MARR|DEAT|BURI)_)/', $btag, $match)) {
655a25f0a04SGreg Roach                    $btag = $match[1];
656a25f0a04SGreg Roach                } else {
6577a6ee1acSGreg Roach                    $btag = '_????_';
658a25f0a04SGreg Roach                }
659a25f0a04SGreg Roach            }
660a25f0a04SGreg Roach
661a25f0a04SGreg Roach            // - Don't let dated after DEAT/BURI facts sort non-dated facts before DEAT/BURI
662a25f0a04SGreg Roach            // - Treat dated after BURI facts as BURI instead
6633425616eSGreg Roach            if ($a->attribute('DATE') !== '' && $factsort[$atag] > $factsort['BURI'] && $factsort[$atag] < $factsort['CHAN']) {
664a25f0a04SGreg Roach                $atag = 'BURI';
665a25f0a04SGreg Roach            }
666a25f0a04SGreg Roach
6673425616eSGreg Roach            if ($b->attribute('DATE') !== '' && $factsort[$btag] > $factsort['BURI'] && $factsort[$btag] < $factsort['CHAN']) {
668a25f0a04SGreg Roach                $btag = 'BURI';
669a25f0a04SGreg Roach            }
670a25f0a04SGreg Roach
671a25f0a04SGreg Roach            $ret = $factsort[$atag] - $factsort[$btag];
672a25f0a04SGreg Roach
673a25f0a04SGreg Roach            // If facts are the same then put dated facts before non-dated facts
674a25f0a04SGreg Roach            if ($ret == 0) {
6753425616eSGreg Roach                if ($a->attribute('DATE') !== '' && $b->attribute('DATE') === '') {
676a25f0a04SGreg Roach                    return -1;
677a25f0a04SGreg Roach                }
678a25f0a04SGreg Roach
6793425616eSGreg Roach                if ($b->attribute('DATE') !== '' && $a->attribute('DATE') === '') {
680a25f0a04SGreg Roach                    return 1;
681a25f0a04SGreg Roach                }
682a25f0a04SGreg Roach
683a25f0a04SGreg Roach                // If no sorting preference, then keep original ordering
684a25f0a04SGreg Roach                $ret = $a->sortOrder - $b->sortOrder;
685a25f0a04SGreg Roach            }
686a25f0a04SGreg Roach
687a25f0a04SGreg Roach            return $ret;
688580a4d11SGreg Roach        };
689580a4d11SGreg Roach    }
690580a4d11SGreg Roach
691580a4d11SGreg Roach    /**
692580a4d11SGreg Roach     * A multi-key sort
693580a4d11SGreg Roach     * 1. First divide the facts into two arrays one set with dates and one set without dates
694580a4d11SGreg Roach     * 2. Sort each of the two new arrays, the date using the compare date function, the non-dated
695580a4d11SGreg Roach     * using the compare type function
696580a4d11SGreg Roach     * 3. Then merge the arrays back into the original array using the compare type function
697580a4d11SGreg Roach     *
6988af3e5c1SGreg Roach     * @param Collection $unsorted
699580a4d11SGreg Roach     *
70054c7f8dfSGreg Roach     * @return Collection
701580a4d11SGreg Roach     */
7028af3e5c1SGreg Roach    public static function sortFacts(Collection $unsorted): Collection
703580a4d11SGreg Roach    {
704580a4d11SGreg Roach        $dated    = [];
705580a4d11SGreg Roach        $nondated = [];
706580a4d11SGreg Roach        $sorted   = [];
707580a4d11SGreg Roach
708580a4d11SGreg Roach        // Split the array into dated and non-dated arrays
709580a4d11SGreg Roach        $order = 0;
710580a4d11SGreg Roach
711580a4d11SGreg Roach        foreach ($unsorted as $fact) {
712580a4d11SGreg Roach            $fact->sortOrder = $order;
713580a4d11SGreg Roach            $order++;
714580a4d11SGreg Roach
715580a4d11SGreg Roach            if ($fact->date()->isOK()) {
716580a4d11SGreg Roach                $dated[] = $fact;
717580a4d11SGreg Roach            } else {
718580a4d11SGreg Roach                $nondated[] = $fact;
719580a4d11SGreg Roach            }
720580a4d11SGreg Roach        }
721580a4d11SGreg Roach
722580a4d11SGreg Roach        usort($dated, self::dateComparator());
723580a4d11SGreg Roach        usort($nondated, self::typeComparator());
724580a4d11SGreg Roach
725580a4d11SGreg Roach        // Merge the arrays
726580a4d11SGreg Roach        $dc = count($dated);
727580a4d11SGreg Roach        $nc = count($nondated);
728580a4d11SGreg Roach        $i  = 0;
729580a4d11SGreg Roach        $j  = 0;
730580a4d11SGreg Roach
731580a4d11SGreg Roach        // while there is anything in the dated array continue merging
732580a4d11SGreg Roach        while ($i < $dc) {
733580a4d11SGreg Roach            // compare each fact by type to merge them in order
734580a4d11SGreg Roach            if ($j < $nc && self::typeComparator()($dated[$i], $nondated[$j]) > 0) {
735580a4d11SGreg Roach                $sorted[] = $nondated[$j];
736580a4d11SGreg Roach                $j++;
737580a4d11SGreg Roach            } else {
738580a4d11SGreg Roach                $sorted[] = $dated[$i];
739580a4d11SGreg Roach                $i++;
740580a4d11SGreg Roach            }
741580a4d11SGreg Roach        }
742580a4d11SGreg Roach
743580a4d11SGreg Roach        // get anything that might be left in the nondated array
744580a4d11SGreg Roach        while ($j < $nc) {
745580a4d11SGreg Roach            $sorted[] = $nondated[$j];
746580a4d11SGreg Roach            $j++;
747580a4d11SGreg Roach        }
748580a4d11SGreg Roach
7491c7a6874SGreg Roach        return new Collection($sorted);
750a25f0a04SGreg Roach    }
751a25f0a04SGreg Roach
752a25f0a04SGreg Roach    /**
753a25f0a04SGreg Roach     * Allow native PHP functions such as array_unique() to work with objects
754a25f0a04SGreg Roach     *
755a25f0a04SGreg Roach     * @return string
756a25f0a04SGreg Roach     */
757dfa848b8SGreg Roach    public function __toString(): string
758c1010edaSGreg Roach    {
759c0935879SGreg Roach        return $this->id . '@' . $this->record->xref();
760a25f0a04SGreg Roach    }
761a25f0a04SGreg Roach}
762