xref: /webtrees/app/Fact.php (revision 819ac503bae4ccce521d61c95f5c5f006b2ebffa)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8a25f0a04SGreg Roach * (at your option) any later version.
9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a25f0a04SGreg Roach * GNU General Public License for more details.
13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a25f0a04SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees;
1976692c8bSGreg Roach
203d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrint;
218af6bbf8SGreg Roachuse Fisharebest\Webtrees\Services\GedcomService;
228f5f5da8SGreg Roachuse InvalidArgumentException;
23a25f0a04SGreg Roach
24a25f0a04SGreg Roach/**
2576692c8bSGreg Roach * A GEDCOM fact or event object.
26a25f0a04SGreg Roach */
27c1010edaSGreg Roachclass Fact
28c1010edaSGreg Roach{
2916d6367aSGreg Roach    private const FACT_ORDER = [
30bbe4546fSGreg Roach        'BIRT',
31bbe4546fSGreg Roach        '_HNM',
32bbe4546fSGreg Roach        'ALIA',
33bbe4546fSGreg Roach        '_AKA',
34bbe4546fSGreg Roach        '_AKAN',
35bbe4546fSGreg Roach        'ADOP',
36bbe4546fSGreg Roach        '_ADPF',
37bbe4546fSGreg Roach        '_ADPF',
38bbe4546fSGreg Roach        '_BRTM',
39bbe4546fSGreg Roach        'CHR',
40bbe4546fSGreg Roach        'BAPM',
41bbe4546fSGreg Roach        'FCOM',
42bbe4546fSGreg Roach        'CONF',
43bbe4546fSGreg Roach        'BARM',
44bbe4546fSGreg Roach        'BASM',
45bbe4546fSGreg Roach        'EDUC',
46bbe4546fSGreg Roach        'GRAD',
47bbe4546fSGreg Roach        '_DEG',
48bbe4546fSGreg Roach        'EMIG',
49bbe4546fSGreg Roach        'IMMI',
50bbe4546fSGreg Roach        'NATU',
51bbe4546fSGreg Roach        '_MILI',
52bbe4546fSGreg Roach        '_MILT',
53bbe4546fSGreg Roach        'ENGA',
54bbe4546fSGreg Roach        'MARB',
55bbe4546fSGreg Roach        'MARC',
56bbe4546fSGreg Roach        'MARL',
57bbe4546fSGreg Roach        '_MARI',
58bbe4546fSGreg Roach        '_MBON',
59bbe4546fSGreg Roach        'MARR',
60bbe4546fSGreg Roach        'MARR_CIVIL',
61bbe4546fSGreg Roach        'MARR_RELIGIOUS',
62bbe4546fSGreg Roach        'MARR_PARTNERS',
63bbe4546fSGreg Roach        'MARR_UNKNOWN',
64bbe4546fSGreg Roach        '_COML',
65bbe4546fSGreg Roach        '_STAT',
66bbe4546fSGreg Roach        '_SEPR',
67bbe4546fSGreg Roach        'DIVF',
68bbe4546fSGreg Roach        'MARS',
69bbe4546fSGreg Roach        '_BIRT_CHIL',
70bbe4546fSGreg Roach        'DIV',
71bbe4546fSGreg Roach        'ANUL',
72bbe4546fSGreg Roach        '_BIRT_',
73bbe4546fSGreg Roach        '_MARR_',
74bbe4546fSGreg Roach        '_DEAT_',
75bbe4546fSGreg Roach        '_BURI_',
76bbe4546fSGreg Roach        'CENS',
77bbe4546fSGreg Roach        'OCCU',
78bbe4546fSGreg Roach        'RESI',
79bbe4546fSGreg Roach        'PROP',
80bbe4546fSGreg Roach        'CHRA',
81bbe4546fSGreg Roach        'RETI',
82bbe4546fSGreg Roach        'FACT',
83bbe4546fSGreg Roach        'EVEN',
84bbe4546fSGreg Roach        '_NMR',
85bbe4546fSGreg Roach        '_NMAR',
86bbe4546fSGreg Roach        'NMR',
87bbe4546fSGreg Roach        'NCHI',
88bbe4546fSGreg Roach        'WILL',
89bbe4546fSGreg Roach        '_HOL',
90bbe4546fSGreg Roach        '_????_',
91bbe4546fSGreg Roach        'DEAT',
92bbe4546fSGreg Roach        '_FNRL',
93bbe4546fSGreg Roach        'CREM',
94bbe4546fSGreg Roach        'BURI',
95bbe4546fSGreg Roach        '_INTE',
96bbe4546fSGreg Roach        '_YART',
97bbe4546fSGreg Roach        '_NLIV',
98bbe4546fSGreg Roach        'PROB',
99bbe4546fSGreg Roach        'TITL',
100bbe4546fSGreg Roach        'COMM',
101bbe4546fSGreg Roach        'NATI',
102bbe4546fSGreg Roach        'CITN',
103bbe4546fSGreg Roach        'CAST',
104bbe4546fSGreg Roach        'RELI',
105bbe4546fSGreg Roach        'SSN',
106bbe4546fSGreg Roach        'IDNO',
107bbe4546fSGreg Roach        'TEMP',
108bbe4546fSGreg Roach        'SLGC',
109bbe4546fSGreg Roach        'BAPL',
110bbe4546fSGreg Roach        'CONL',
111bbe4546fSGreg Roach        'ENDL',
112bbe4546fSGreg Roach        'SLGS',
113bbe4546fSGreg Roach        'ADDR',
114bbe4546fSGreg Roach        'PHON',
115bbe4546fSGreg Roach        'EMAIL',
116bbe4546fSGreg Roach        '_EMAIL',
117bbe4546fSGreg Roach        'EMAL',
118bbe4546fSGreg Roach        'FAX',
119bbe4546fSGreg Roach        'WWW',
120bbe4546fSGreg Roach        'URL',
121bbe4546fSGreg Roach        '_URL',
122bbe4546fSGreg Roach        'AFN',
123bbe4546fSGreg Roach        'REFN',
124bbe4546fSGreg Roach        '_PRMN',
125bbe4546fSGreg Roach        'REF',
126bbe4546fSGreg Roach        'RIN',
127bbe4546fSGreg Roach        '_UID',
128bbe4546fSGreg Roach        'OBJE',
129bbe4546fSGreg Roach        'NOTE',
130bbe4546fSGreg Roach        'SOUR',
131bbe4546fSGreg Roach        'CHAN',
132bbe4546fSGreg Roach        '_TODO',
133bbe4546fSGreg Roach    ];
134bbe4546fSGreg Roach
135a25f0a04SGreg Roach    /** @var string Unique identifier for this fact (currently implemented as a hash of the raw data). */
136905ab80aSGreg Roach    private $id;
137a25f0a04SGreg Roach
138a25f0a04SGreg Roach    /** @var GedcomRecord The GEDCOM record from which this fact is taken */
139e7766c08SGreg Roach    private $record;
140a25f0a04SGreg Roach
141a25f0a04SGreg Roach    /** @var string The raw GEDCOM data for this fact */
142a25f0a04SGreg Roach    private $gedcom;
143a25f0a04SGreg Roach
144a25f0a04SGreg Roach    /** @var string The GEDCOM tag for this record */
145a25f0a04SGreg Roach    private $tag;
146a25f0a04SGreg Roach
147cbc1590aSGreg Roach    /** @var bool Is this a recently deleted fact, pending approval? */
148a25f0a04SGreg Roach    private $pending_deletion = false;
149a25f0a04SGreg Roach
150cbc1590aSGreg Roach    /** @var bool Is this a recently added fact, pending approval? */
151a25f0a04SGreg Roach    private $pending_addition = false;
152a25f0a04SGreg Roach
153a25f0a04SGreg Roach    /** @var Date The date of this fact, from the “2 DATE …” attribute */
154a25f0a04SGreg Roach    private $date;
155a25f0a04SGreg Roach
156a25f0a04SGreg Roach    /** @var Place The place of this fact, from the “2 PLAC …” attribute */
157a25f0a04SGreg Roach    private $place;
158a25f0a04SGreg Roach
1593d7a8a4cSGreg Roach    /** @var int Temporary(!) variable Used by Functions::sortFacts() */
160a25f0a04SGreg Roach    public $sortOrder;
161a25f0a04SGreg Roach
162a25f0a04SGreg Roach    /**
163a25f0a04SGreg Roach     * Create an event object from a gedcom fragment.
164a25f0a04SGreg Roach     * We need the parent object (to check privacy) and a (pseudo) fact ID to
165a25f0a04SGreg Roach     * identify the fact within the record.
166a25f0a04SGreg Roach     *
167a25f0a04SGreg Roach     * @param string       $gedcom
168a25f0a04SGreg Roach     * @param GedcomRecord $parent
169905ab80aSGreg Roach     * @param string       $id
170a25f0a04SGreg Roach     *
1718f5f5da8SGreg Roach     * @throws InvalidArgumentException
172a25f0a04SGreg Roach     */
173905ab80aSGreg Roach    public function __construct($gedcom, GedcomRecord $parent, $id)
174c1010edaSGreg Roach    {
1758d0ebef0SGreg Roach        if (preg_match('/^1 (' . Gedcom::REGEX_TAG . ')/', $gedcom, $match)) {
176a25f0a04SGreg Roach            $this->gedcom = $gedcom;
177e7766c08SGreg Roach            $this->record = $parent;
178905ab80aSGreg Roach            $this->id     = $id;
179a25f0a04SGreg Roach            $this->tag    = $match[1];
180a25f0a04SGreg Roach        } else {
1818f5f5da8SGreg Roach            throw new InvalidArgumentException('Invalid GEDCOM data passed to Fact::_construct(' . $gedcom . ')');
182a25f0a04SGreg Roach        }
183a25f0a04SGreg Roach    }
184a25f0a04SGreg Roach
185a25f0a04SGreg Roach    /**
186a25f0a04SGreg Roach     * Get the value of level 1 data in the fact
187a25f0a04SGreg Roach     * Allow for multi-line values
188a25f0a04SGreg Roach     *
189baacc364SGreg Roach     * @return string
190a25f0a04SGreg Roach     */
191dfa848b8SGreg Roach    public function value(): string
192c1010edaSGreg Roach    {
193a25f0a04SGreg Roach        if (preg_match('/^1 (?:' . $this->tag . ') ?(.*(?:(?:\n2 CONT ?.*)*))/', $this->gedcom, $match)) {
194a25f0a04SGreg Roach            return preg_replace("/\n2 CONT ?/", "\n", $match[1]);
195a25f0a04SGreg Roach        }
196b2ce94c6SRico Sonntag
197b2ce94c6SRico Sonntag        return '';
198a25f0a04SGreg Roach    }
199a25f0a04SGreg Roach
200a25f0a04SGreg Roach    /**
201a25f0a04SGreg Roach     * Get the record to which this fact links
202a25f0a04SGreg Roach     *
203188adf12SGreg Roach     * @return Individual|Family|Source|Repository|Media|Note|GedcomRecord|null
204a25f0a04SGreg Roach     */
205dc124885SGreg Roach    public function target()
206c1010edaSGreg Roach    {
20784586c02SGreg Roach        $xref = trim($this->value(), '@');
208a25f0a04SGreg Roach        switch ($this->tag) {
209a25f0a04SGreg Roach            case 'FAMC':
210a25f0a04SGreg Roach            case 'FAMS':
211f4afa648SGreg Roach                return Family::getInstance($xref, $this->record()->tree());
212a25f0a04SGreg Roach            case 'HUSB':
213a25f0a04SGreg Roach            case 'WIFE':
214a25f0a04SGreg Roach            case 'CHIL':
215f4afa648SGreg Roach                return Individual::getInstance($xref, $this->record()->tree());
216a25f0a04SGreg Roach            case 'SOUR':
217f4afa648SGreg Roach                return Source::getInstance($xref, $this->record()->tree());
218a25f0a04SGreg Roach            case 'OBJE':
219f4afa648SGreg Roach                return Media::getInstance($xref, $this->record()->tree());
220a25f0a04SGreg Roach            case 'REPO':
221f4afa648SGreg Roach                return Repository::getInstance($xref, $this->record()->tree());
222a25f0a04SGreg Roach            case 'NOTE':
223f4afa648SGreg Roach                return Note::getInstance($xref, $this->record()->tree());
224a25f0a04SGreg Roach            default:
225f4afa648SGreg Roach                return GedcomRecord::getInstance($xref, $this->record()->tree());
226a25f0a04SGreg Roach        }
227a25f0a04SGreg Roach    }
228a25f0a04SGreg Roach
229a25f0a04SGreg Roach    /**
230a25f0a04SGreg Roach     * Get the value of level 2 data in the fact
231a25f0a04SGreg Roach     *
232a25f0a04SGreg Roach     * @param string $tag
233a25f0a04SGreg Roach     *
234baacc364SGreg Roach     * @return string
235a25f0a04SGreg Roach     */
236dfa848b8SGreg Roach    public function attribute($tag): string
237c1010edaSGreg Roach    {
238a25f0a04SGreg Roach        if (preg_match('/\n2 (?:' . $tag . ') ?(.*(?:(?:\n3 CONT ?.*)*)*)/', $this->gedcom, $match)) {
239a25f0a04SGreg Roach            return preg_replace("/\n3 CONT ?/", "\n", $match[1]);
240a25f0a04SGreg Roach        }
241b2ce94c6SRico Sonntag
242b2ce94c6SRico Sonntag        return '';
243a25f0a04SGreg Roach    }
244a25f0a04SGreg Roach
245a25f0a04SGreg Roach    /**
2468af6bbf8SGreg Roach     * Get the PLAC:MAP:LATI for the fact.
2478af6bbf8SGreg Roach     *
2488af6bbf8SGreg Roach     * @return float
2498af6bbf8SGreg Roach     */
2508af6bbf8SGreg Roach    public function latitude(): float
2518af6bbf8SGreg Roach    {
2528af6bbf8SGreg Roach        if (preg_match('/\n4 LATI (.+)/', $this->gedcom, $match)) {
2538af6bbf8SGreg Roach            $gedcom_service = new GedcomService();
2548af6bbf8SGreg Roach
2558af6bbf8SGreg Roach            return $gedcom_service->readLatitude($match[1]);
2568af6bbf8SGreg Roach        }
2578af6bbf8SGreg Roach
2588af6bbf8SGreg Roach        return 0.0;
2598af6bbf8SGreg Roach    }
2608af6bbf8SGreg Roach
2618af6bbf8SGreg Roach    /**
2628af6bbf8SGreg Roach     * Get the PLAC:MAP:LONG for the fact.
2638af6bbf8SGreg Roach     *
2648af6bbf8SGreg Roach     * @return float
2658af6bbf8SGreg Roach     */
2668af6bbf8SGreg Roach    public function longitude(): float
2678af6bbf8SGreg Roach    {
268*819ac503SRico Sonntag        if (preg_match('/\n4 LONG (.+)/', $this->gedcom, $match)) {
2698af6bbf8SGreg Roach            $gedcom_service = new GedcomService();
2708af6bbf8SGreg Roach
2718af6bbf8SGreg Roach            return $gedcom_service->readLongitude($match[1]);
2728af6bbf8SGreg Roach        }
2738af6bbf8SGreg Roach
2748af6bbf8SGreg Roach        return 0.0;
2758af6bbf8SGreg Roach    }
2768af6bbf8SGreg Roach
2778af6bbf8SGreg Roach    /**
278a25f0a04SGreg Roach     * Do the privacy rules allow us to display this fact to the current user
279a25f0a04SGreg Roach     *
280cbc1590aSGreg Roach     * @param int|null $access_level
281a25f0a04SGreg Roach     *
282cbc1590aSGreg Roach     * @return bool
283a25f0a04SGreg Roach     */
28435584196SGreg Roach    public function canShow(int $access_level = null): bool
285c1010edaSGreg Roach    {
2864b9ff166SGreg Roach        if ($access_level === null) {
287f4afa648SGreg Roach            $access_level = Auth::accessLevel($this->record()->tree());
2884b9ff166SGreg Roach        }
2894b9ff166SGreg Roach
290a25f0a04SGreg Roach        // Does this record have an explicit RESN?
29120ff464cSGreg Roach        if (strpos($this->gedcom, "\n2 RESN confidential") !== false) {
2924b9ff166SGreg Roach            return Auth::PRIV_NONE >= $access_level;
293a25f0a04SGreg Roach        }
29420ff464cSGreg Roach        if (strpos($this->gedcom, "\n2 RESN privacy") !== false) {
2954b9ff166SGreg Roach            return Auth::PRIV_USER >= $access_level;
296a25f0a04SGreg Roach        }
29720ff464cSGreg Roach        if (strpos($this->gedcom, "\n2 RESN none") !== false) {
298a25f0a04SGreg Roach            return true;
299a25f0a04SGreg Roach        }
300a25f0a04SGreg Roach
301a25f0a04SGreg Roach        // Does this record have a default RESN?
302c0935879SGreg Roach        $xref                    = $this->record->xref();
303f4afa648SGreg Roach        $fact_privacy            = $this->record->tree()->getFactPrivacy();
304f4afa648SGreg Roach        $individual_fact_privacy = $this->record->tree()->getIndividualFactPrivacy();
305518bbdc1SGreg Roach        if (isset($individual_fact_privacy[$xref][$this->tag])) {
306518bbdc1SGreg Roach            return $individual_fact_privacy[$xref][$this->tag] >= $access_level;
307a25f0a04SGreg Roach        }
308518bbdc1SGreg Roach        if (isset($fact_privacy[$this->tag])) {
309518bbdc1SGreg Roach            return $fact_privacy[$this->tag] >= $access_level;
310a25f0a04SGreg Roach        }
311a25f0a04SGreg Roach
312a25f0a04SGreg Roach        // No restrictions - it must be public
313a25f0a04SGreg Roach        return true;
314a25f0a04SGreg Roach    }
315a25f0a04SGreg Roach
316a25f0a04SGreg Roach    /**
317a25f0a04SGreg Roach     * Check whether this fact is protected against edit
318a25f0a04SGreg Roach     *
319cbc1590aSGreg Roach     * @return bool
320a25f0a04SGreg Roach     */
3218f53f488SRico Sonntag    public function canEdit(): bool
322c1010edaSGreg Roach    {
323a25f0a04SGreg Roach        // Managers can edit anything
324a25f0a04SGreg Roach        // Members cannot edit RESN, CHAN and locked records
325a25f0a04SGreg Roach        return
326e7766c08SGreg Roach            $this->record->canEdit() && !$this->isPendingDeletion() && (
327f4afa648SGreg Roach                Auth::isManager($this->record->tree()) ||
328f4afa648SGreg Roach                Auth::isEditor($this->record->tree()) && strpos($this->gedcom, "\n2 RESN locked") === false && $this->getTag() != 'RESN' && $this->getTag() != 'CHAN'
329a25f0a04SGreg Roach            );
330a25f0a04SGreg Roach    }
331a25f0a04SGreg Roach
332a25f0a04SGreg Roach    /**
333a25f0a04SGreg Roach     * The place where the event occured.
334a25f0a04SGreg Roach     *
335a25f0a04SGreg Roach     * @return Place
336a25f0a04SGreg Roach     */
3374fb14fcbSGreg Roach    public function place(): Place
338c1010edaSGreg Roach    {
339a25f0a04SGreg Roach        if ($this->place === null) {
340f4afa648SGreg Roach            $this->place = new Place($this->attribute('PLAC'), $this->record()->tree());
341a25f0a04SGreg Roach        }
342a25f0a04SGreg Roach
343a25f0a04SGreg Roach        return $this->place;
344a25f0a04SGreg Roach    }
345a25f0a04SGreg Roach
346a25f0a04SGreg Roach    /**
347a25f0a04SGreg Roach     * Get the date for this fact.
348a25f0a04SGreg Roach     * We can call this function many times, especially when sorting,
349a25f0a04SGreg Roach     * so keep a copy of the date.
350a25f0a04SGreg Roach     *
351a25f0a04SGreg Roach     * @return Date
352a25f0a04SGreg Roach     */
3532decada7SGreg Roach    public function date(): Date
354c1010edaSGreg Roach    {
355a25f0a04SGreg Roach        if ($this->date === null) {
3563425616eSGreg Roach            $this->date = new Date($this->attribute('DATE'));
357a25f0a04SGreg Roach        }
358a25f0a04SGreg Roach
359a25f0a04SGreg Roach        return $this->date;
360a25f0a04SGreg Roach    }
361a25f0a04SGreg Roach
362a25f0a04SGreg Roach    /**
363a25f0a04SGreg Roach     * The raw GEDCOM data for this fact
364a25f0a04SGreg Roach     *
365a25f0a04SGreg Roach     * @return string
366a25f0a04SGreg Roach     */
367138ca96cSGreg Roach    public function gedcom(): string
368c1010edaSGreg Roach    {
369a25f0a04SGreg Roach        return $this->gedcom;
370a25f0a04SGreg Roach    }
371a25f0a04SGreg Roach
372a25f0a04SGreg Roach    /**
373a25f0a04SGreg Roach     * Get a (pseudo) primary key for this fact.
374a25f0a04SGreg Roach     *
375a25f0a04SGreg Roach     * @return string
376a25f0a04SGreg Roach     */
377905ab80aSGreg Roach    public function id(): string
378c1010edaSGreg Roach    {
379905ab80aSGreg Roach        return $this->id;
380a25f0a04SGreg Roach    }
381a25f0a04SGreg Roach
382a25f0a04SGreg Roach    /**
383a25f0a04SGreg Roach     * What is the tag (type) of this fact, such as BIRT, MARR or DEAT.
384a25f0a04SGreg Roach     *
385a25f0a04SGreg Roach     * @return string
386a25f0a04SGreg Roach     */
3878f53f488SRico Sonntag    public function getTag(): string
388c1010edaSGreg Roach    {
389a25f0a04SGreg Roach        return $this->tag;
390a25f0a04SGreg Roach    }
391a25f0a04SGreg Roach
392a25f0a04SGreg Roach    /**
393a25f0a04SGreg Roach     * Used to convert a real fact (e.g. BIRT) into a close-relative’s fact (e.g. _BIRT_CHIL)
394a25f0a04SGreg Roach     *
395a25f0a04SGreg Roach     * @param string $tag
3967e96c925SGreg Roach     *
3977e96c925SGreg Roach     * @return void
398a25f0a04SGreg Roach     */
399c1010edaSGreg Roach    public function setTag($tag)
400c1010edaSGreg Roach    {
401a25f0a04SGreg Roach        $this->tag = $tag;
402a25f0a04SGreg Roach    }
403a25f0a04SGreg Roach
404a25f0a04SGreg Roach    /**
405a25f0a04SGreg Roach     * The Person/Family record where this Fact came from
406a25f0a04SGreg Roach     *
4076e59b7c4SGreg Roach     * @return Individual|Family|Source|Repository|Media|Note|GedcomRecord
408a25f0a04SGreg Roach     */
409e7766c08SGreg Roach    public function record()
410c1010edaSGreg Roach    {
411e7766c08SGreg Roach        return $this->record;
412a25f0a04SGreg Roach    }
413a25f0a04SGreg Roach
414a25f0a04SGreg Roach    /**
415a25f0a04SGreg Roach     * Get the name of this fact type, for use as a label.
416a25f0a04SGreg Roach     *
417a25f0a04SGreg Roach     * @return string
418a25f0a04SGreg Roach     */
4197b7d8067SGreg Roach    public function label(): string
420c1010edaSGreg Roach    {
421a25f0a04SGreg Roach        // Custom FACT/EVEN - with a TYPE
4223425616eSGreg Roach        if (($this->tag === 'FACT' || $this->tag === 'EVEN') && $this->attribute('TYPE') !== '') {
4233425616eSGreg Roach            return I18N::translate(e($this->attribute('TYPE')));
424a25f0a04SGreg Roach        }
4254e5adf68SGreg Roach
426e7766c08SGreg Roach        return GedcomTag::getLabel($this->tag, $this->record);
427a25f0a04SGreg Roach    }
428a25f0a04SGreg Roach
429a25f0a04SGreg Roach    /**
430a25f0a04SGreg Roach     * This is a newly deleted fact, pending approval.
4317e96c925SGreg Roach     *
4327e96c925SGreg Roach     * @return void
433a25f0a04SGreg Roach     */
434c1010edaSGreg Roach    public function setPendingDeletion()
435c1010edaSGreg Roach    {
436a25f0a04SGreg Roach        $this->pending_deletion = true;
437a25f0a04SGreg Roach        $this->pending_addition = false;
438a25f0a04SGreg Roach    }
439a25f0a04SGreg Roach
440a25f0a04SGreg Roach    /**
441a25f0a04SGreg Roach     * Is this a newly deleted fact, pending approval.
442a25f0a04SGreg Roach     *
443cbc1590aSGreg Roach     * @return bool
444a25f0a04SGreg Roach     */
4458f53f488SRico Sonntag    public function isPendingDeletion(): bool
446c1010edaSGreg Roach    {
447a25f0a04SGreg Roach        return $this->pending_deletion;
448a25f0a04SGreg Roach    }
449a25f0a04SGreg Roach
450a25f0a04SGreg Roach    /**
451a25f0a04SGreg Roach     * This is a newly added fact, pending approval.
4527e96c925SGreg Roach     *
4537e96c925SGreg Roach     * @return void
454a25f0a04SGreg Roach     */
455c1010edaSGreg Roach    public function setPendingAddition()
456c1010edaSGreg Roach    {
457a25f0a04SGreg Roach        $this->pending_addition = true;
458a25f0a04SGreg Roach        $this->pending_deletion = false;
459a25f0a04SGreg Roach    }
460a25f0a04SGreg Roach
461a25f0a04SGreg Roach    /**
462a25f0a04SGreg Roach     * Is this a newly added fact, pending approval.
463a25f0a04SGreg Roach     *
464cbc1590aSGreg Roach     * @return bool
465a25f0a04SGreg Roach     */
4668f53f488SRico Sonntag    public function isPendingAddition(): bool
467c1010edaSGreg Roach    {
468a25f0a04SGreg Roach        return $this->pending_addition;
469a25f0a04SGreg Roach    }
470a25f0a04SGreg Roach
471a25f0a04SGreg Roach    /**
472a25f0a04SGreg Roach     * Source citations linked to this fact
473a25f0a04SGreg Roach     *
474a25f0a04SGreg Roach     * @return string[]
475a25f0a04SGreg Roach     */
4768f53f488SRico Sonntag    public function getCitations(): array
477c1010edaSGreg Roach    {
4788d0ebef0SGreg Roach        preg_match_all('/\n(2 SOUR @(' . Gedcom::REGEX_XREF . ')@(?:\n[3-9] .*)*)/', $this->gedcom(), $matches, PREG_SET_ORDER);
47913abd6f3SGreg Roach        $citations = [];
480a25f0a04SGreg Roach        foreach ($matches as $match) {
481f4afa648SGreg Roach            $source = Source::getInstance($match[2], $this->record()->tree());
4822859ecedSJonathan Jaubart            if ($source && $source->canShow()) {
483a25f0a04SGreg Roach                $citations[] = $match[1];
484a25f0a04SGreg Roach            }
485a25f0a04SGreg Roach        }
486a25f0a04SGreg Roach
487a25f0a04SGreg Roach        return $citations;
488a25f0a04SGreg Roach    }
489a25f0a04SGreg Roach
490a25f0a04SGreg Roach    /**
491a25f0a04SGreg Roach     * Notes (inline and objects) linked to this fact
492a25f0a04SGreg Roach     *
493a25f0a04SGreg Roach     * @return string[]|Note[]
494a25f0a04SGreg Roach     */
4958f53f488SRico Sonntag    public function getNotes(): array
496c1010edaSGreg Roach    {
49713abd6f3SGreg Roach        $notes = [];
498138ca96cSGreg Roach        preg_match_all('/\n2 NOTE ?(.*(?:\n3.*)*)/', $this->gedcom(), $matches);
499a25f0a04SGreg Roach        foreach ($matches[1] as $match) {
500a25f0a04SGreg Roach            $note = preg_replace("/\n3 CONT ?/", "\n", $match);
5018d0ebef0SGreg Roach            if (preg_match('/@(' . Gedcom::REGEX_XREF . ')@/', $note, $nmatch)) {
502f4afa648SGreg Roach                $note = Note::getInstance($nmatch[1], $this->record()->tree());
503a25f0a04SGreg Roach                if ($note && $note->canShow()) {
504a25f0a04SGreg Roach                    // A note object
505a25f0a04SGreg Roach                    $notes[] = $note;
506a25f0a04SGreg Roach                }
507a25f0a04SGreg Roach            } else {
508a25f0a04SGreg Roach                // An inline note
509a25f0a04SGreg Roach                $notes[] = $note;
510a25f0a04SGreg Roach            }
511a25f0a04SGreg Roach        }
512a25f0a04SGreg Roach
513a25f0a04SGreg Roach        return $notes;
514a25f0a04SGreg Roach    }
515a25f0a04SGreg Roach
516a25f0a04SGreg Roach    /**
517a25f0a04SGreg Roach     * Media objects linked to this fact
518a25f0a04SGreg Roach     *
519a25f0a04SGreg Roach     * @return Media[]
520a25f0a04SGreg Roach     */
5218f53f488SRico Sonntag    public function getMedia(): array
522c1010edaSGreg Roach    {
52313abd6f3SGreg Roach        $media = [];
5248d0ebef0SGreg Roach        preg_match_all('/\n2 OBJE @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom(), $matches);
525a25f0a04SGreg Roach        foreach ($matches[1] as $match) {
526f4afa648SGreg Roach            $obje = Media::getInstance($match, $this->record()->tree());
5272859ecedSJonathan Jaubart            if ($obje && $obje->canShow()) {
528a25f0a04SGreg Roach                $media[] = $obje;
529a25f0a04SGreg Roach            }
530a25f0a04SGreg Roach        }
531a25f0a04SGreg Roach
532a25f0a04SGreg Roach        return $media;
533a25f0a04SGreg Roach    }
534a25f0a04SGreg Roach
535a25f0a04SGreg Roach    /**
536a25f0a04SGreg Roach     * A one-line summary of the fact - for charts, etc.
537a25f0a04SGreg Roach     *
538a25f0a04SGreg Roach     * @return string
539a25f0a04SGreg Roach     */
5408f53f488SRico Sonntag    public function summary(): string
541c1010edaSGreg Roach    {
54213abd6f3SGreg Roach        $attributes = [];
543dc124885SGreg Roach        $target     = $this->target();
544db7bb364SGreg Roach        if ($target instanceof GedcomRecord) {
54539ca88baSGreg Roach            $attributes[] = $target->fullName();
546a25f0a04SGreg Roach        } else {
547726d7b07SGreg Roach            // Fact value
54884586c02SGreg Roach            $value = $this->value();
549726d7b07SGreg Roach            if ($value !== '' && $value !== 'Y') {
550d53324c9SGreg Roach                $attributes[] = '<span dir="auto">' . e($value) . '</span>';
551a25f0a04SGreg Roach            }
552726d7b07SGreg Roach            // Fact date
5532decada7SGreg Roach            $date = $this->date();
554726d7b07SGreg Roach            if ($date->isOK()) {
5558d0ebef0SGreg Roach                if (in_array($this->getTag(), Gedcom::BIRTH_EVENTS) && $this->record() instanceof Individual && $this->record()->tree()->getPreference('SHOW_PARENTS_AGE')) {
556e7766c08SGreg Roach                    $attributes[] = $date->display() . FunctionsPrint::formatParentsAges($this->record(), $date);
557a25f0a04SGreg Roach                } else {
558a25f0a04SGreg Roach                    $attributes[] = $date->display();
559a25f0a04SGreg Roach                }
560726d7b07SGreg Roach            }
561726d7b07SGreg Roach            // Fact place
562392561bbSGreg Roach            if ($this->place()->gedcomName() <> '') {
563392561bbSGreg Roach                $attributes[] = $this->place()->shortName();
564a25f0a04SGreg Roach            }
565a25f0a04SGreg Roach        }
5662d8f08abSGreg Roach
5672d8f08abSGreg Roach        $class = 'fact_' . $this->getTag();
568a25f0a04SGreg Roach        if ($this->isPendingAddition()) {
5692d8f08abSGreg Roach            $class .= ' new';
570a25f0a04SGreg Roach        } elseif ($this->isPendingDeletion()) {
5712d8f08abSGreg Roach            $class .= ' old';
572a25f0a04SGreg Roach        }
5732d8f08abSGreg Roach
5742d8f08abSGreg Roach        return
5752d8f08abSGreg Roach            '<div class="' . $class . '">' .
5762d8f08abSGreg Roach            /* I18N: a label/value pair, such as “Occupation: Farmer”. Some languages may need to change the punctuation. */
5777b7d8067SGreg Roach            I18N::translate('<span class="label">%1$s:</span> <span class="field" dir="auto">%2$s</span>', $this->label(), implode(' — ', $attributes)) .
5782d8f08abSGreg Roach            '</div>';
579a25f0a04SGreg Roach    }
580a25f0a04SGreg Roach
581a25f0a04SGreg Roach    /**
582a25f0a04SGreg Roach     * Static Helper functions to sort events
583a25f0a04SGreg Roach     *
584a25f0a04SGreg Roach     * @param Fact $a Fact one
585a25f0a04SGreg Roach     * @param Fact $b Fact two
586a25f0a04SGreg Roach     *
587cbc1590aSGreg Roach     * @return int
588a25f0a04SGreg Roach     */
589dfa848b8SGreg Roach    public static function compareDate(Fact $a, Fact $b): int
590c1010edaSGreg Roach    {
5912decada7SGreg Roach        if ($a->date()->isOK() && $b->date()->isOK()) {
592a25f0a04SGreg Roach            // If both events have dates, compare by date
5932decada7SGreg Roach            $ret = Date::compare($a->date(), $b->date());
594a25f0a04SGreg Roach
595dfa848b8SGreg Roach            if ($ret === 0) {
596a25f0a04SGreg Roach                // If dates are the same, compare by fact type
597a25f0a04SGreg Roach                $ret = self::compareType($a, $b);
598a25f0a04SGreg Roach
599a25f0a04SGreg Roach                // If the fact type is also the same, retain the initial order
600dfa848b8SGreg Roach                if ($ret === 0) {
601a25f0a04SGreg Roach                    $ret = $a->sortOrder - $b->sortOrder;
602a25f0a04SGreg Roach                }
603a25f0a04SGreg Roach            }
604a25f0a04SGreg Roach
605a25f0a04SGreg Roach            return $ret;
606b2ce94c6SRico Sonntag        }
607b2ce94c6SRico Sonntag
608a25f0a04SGreg Roach        // One or both events have no date - retain the initial order
609a25f0a04SGreg Roach        return $a->sortOrder - $b->sortOrder;
610a25f0a04SGreg Roach    }
611a25f0a04SGreg Roach
612a25f0a04SGreg Roach    /**
613a25f0a04SGreg Roach     * Static method to compare two events by their type.
614a25f0a04SGreg Roach     *
615a25f0a04SGreg Roach     * @param Fact $a Fact one
616a25f0a04SGreg Roach     * @param Fact $b Fact two
617a25f0a04SGreg Roach     *
618cbc1590aSGreg Roach     * @return int
619a25f0a04SGreg Roach     */
6208f53f488SRico Sonntag    public static function compareType(Fact $a, Fact $b): int
621c1010edaSGreg Roach    {
622bbe4546fSGreg Roach        static $factsort = [];
623a25f0a04SGreg Roach
624a25f0a04SGreg Roach        if (empty($factsort)) {
625bbe4546fSGreg Roach            $factsort = array_flip(self::FACT_ORDER);
626a25f0a04SGreg Roach        }
627a25f0a04SGreg Roach
628a25f0a04SGreg Roach        // Facts from same families stay grouped together
629a25f0a04SGreg Roach        // Keep MARR and DIV from the same families from mixing with events from other FAMs
630a25f0a04SGreg Roach        // Use the original order in which the facts were added
631e7766c08SGreg Roach        if ($a->record instanceof Family && $b->record instanceof Family && $a->record !== $b->record) {
632a25f0a04SGreg Roach            return $a->sortOrder - $b->sortOrder;
633a25f0a04SGreg Roach        }
634a25f0a04SGreg Roach
635a25f0a04SGreg Roach        $atag = $a->getTag();
636a25f0a04SGreg Roach        $btag = $b->getTag();
637a25f0a04SGreg Roach
638a25f0a04SGreg Roach        // Events not in the above list get mapped onto one that is.
639a25f0a04SGreg Roach        if (!array_key_exists($atag, $factsort)) {
640a25f0a04SGreg Roach            if (preg_match('/^(_(BIRT|MARR|DEAT|BURI)_)/', $atag, $match)) {
641a25f0a04SGreg Roach                $atag = $match[1];
642a25f0a04SGreg Roach            } else {
6437a6ee1acSGreg Roach                $atag = '_????_';
644a25f0a04SGreg Roach            }
645a25f0a04SGreg Roach        }
646a25f0a04SGreg Roach
647a25f0a04SGreg Roach        if (!array_key_exists($btag, $factsort)) {
648a25f0a04SGreg Roach            if (preg_match('/^(_(BIRT|MARR|DEAT|BURI)_)/', $btag, $match)) {
649a25f0a04SGreg Roach                $btag = $match[1];
650a25f0a04SGreg Roach            } else {
6517a6ee1acSGreg Roach                $btag = '_????_';
652a25f0a04SGreg Roach            }
653a25f0a04SGreg Roach        }
654a25f0a04SGreg Roach
655a25f0a04SGreg Roach        // - Don't let dated after DEAT/BURI facts sort non-dated facts before DEAT/BURI
656a25f0a04SGreg Roach        // - Treat dated after BURI facts as BURI instead
6573425616eSGreg Roach        if ($a->attribute('DATE') !== '' && $factsort[$atag] > $factsort['BURI'] && $factsort[$atag] < $factsort['CHAN']) {
658a25f0a04SGreg Roach            $atag = 'BURI';
659a25f0a04SGreg Roach        }
660a25f0a04SGreg Roach
6613425616eSGreg Roach        if ($b->attribute('DATE') !== '' && $factsort[$btag] > $factsort['BURI'] && $factsort[$btag] < $factsort['CHAN']) {
662a25f0a04SGreg Roach            $btag = 'BURI';
663a25f0a04SGreg Roach        }
664a25f0a04SGreg Roach
665a25f0a04SGreg Roach        $ret = $factsort[$atag] - $factsort[$btag];
666a25f0a04SGreg Roach
667a25f0a04SGreg Roach        // If facts are the same then put dated facts before non-dated facts
668a25f0a04SGreg Roach        if ($ret == 0) {
6693425616eSGreg Roach            if ($a->attribute('DATE') !== '' && $b->attribute('DATE') === '') {
670a25f0a04SGreg Roach                return -1;
671a25f0a04SGreg Roach            }
672a25f0a04SGreg Roach
6733425616eSGreg Roach            if ($b->attribute('DATE') !== '' && $a->attribute('DATE') === '') {
674a25f0a04SGreg Roach                return 1;
675a25f0a04SGreg Roach            }
676a25f0a04SGreg Roach
677a25f0a04SGreg Roach            // If no sorting preference, then keep original ordering
678a25f0a04SGreg Roach            $ret = $a->sortOrder - $b->sortOrder;
679a25f0a04SGreg Roach        }
680a25f0a04SGreg Roach
681a25f0a04SGreg Roach        return $ret;
682a25f0a04SGreg Roach    }
683a25f0a04SGreg Roach
684a25f0a04SGreg Roach    /**
685a25f0a04SGreg Roach     * Allow native PHP functions such as array_unique() to work with objects
686a25f0a04SGreg Roach     *
687a25f0a04SGreg Roach     * @return string
688a25f0a04SGreg Roach     */
689dfa848b8SGreg Roach    public function __toString(): string
690c1010edaSGreg Roach    {
691c0935879SGreg Roach        return $this->id . '@' . $this->record->xref();
692a25f0a04SGreg Roach    }
693a25f0a04SGreg Roach}
694