xref: /webtrees/app/Module/FixPrimaryTag.php (revision 455a30fe529581f1d53045f9c38a0674472d498f)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Fact;
23use Fisharebest\Webtrees\GedcomRecord;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Individual;
26use Fisharebest\Webtrees\Tree;
27use Illuminate\Support\Collection;
28
29use function e;
30use function str_contains;
31use function strtoupper;
32
33/**
34 * Class FixPrimaryTag
35 */
36class FixPrimaryTag extends AbstractModule implements ModuleDataFixInterface
37{
38    use ModuleDataFixTrait;
39
40    /**
41     * How should this module be identified in the control panel, etc.?
42     *
43     * @return string
44     */
45    public function title(): string
46    {
47        /* I18N: Name of a module */
48        return I18N::translate('Convert _PRIM tags to GEDCOM 5.5.1');
49    }
50
51    /**
52     * A sentence describing what this module does.
53     *
54     * @return string
55     */
56    public function description(): string
57    {
58        /* I18N: Description of a “Data fix” module */
59        return I18N::translate('“Highlighted image” (_PRIM) tags are used by some genealogy applications to indicate the preferred image for an individual. An alternative is to re-order the images so that the preferred one is listed first.');
60    }
61
62    /**
63     * XREFs of media records that might need fixing.
64     *
65     * @param Tree                 $tree
66     * @param array<string,string> $params
67     *
68     * @return Collection<string>
69     */
70    public function mediaToFix(Tree $tree, array $params): Collection
71    {
72        return $this->mediaToFixQuery($tree, $params)
73            ->where('m_file', '=', $tree->id())
74            ->where('m_gedcom', 'LIKE', "%\n1 _PRIM %")
75            ->pluck('m_id');
76    }
77
78    /**
79     * Does a record need updating?
80     *
81     * @param GedcomRecord         $record
82     * @param array<string,string> $params
83     *
84     * @return bool
85     */
86    public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool
87    {
88        return str_contains($record->gedcom(), "\n1 _PRIM ");
89    }
90
91    /**
92     * Show the changes we would make
93     *
94     * @param GedcomRecord         $record
95     * @param array<string,string> $params
96     *
97     * @return string
98     */
99    public function previewUpdate(GedcomRecord $record, array $params): string
100    {
101        $html = '';
102        foreach ($record->facts(['_PRIM']) as $prim) {
103            $html = '<p>' . I18N::translate('Delete') . ' – <code>' . e($prim->gedcom()) . '</code></p>';
104        }
105
106        $html .= '<ul>';
107        foreach ($record->linkedIndividuals('OBJE') as $individual) {
108            $html .= '<li>' . I18N::translate('Re-order media') . ' – <a href="' . e($individual->url()) . '">' . $individual->fullName() . '</a></li>';
109        }
110        $html .= '</ul>';
111
112        return $html;
113    }
114
115    /**
116     * Fix a record
117     *
118     * @param GedcomRecord         $record
119     * @param array<string,string> $params
120     *
121     * @return void
122     */
123    public function updateRecord(GedcomRecord $record, array $params): void
124    {
125        $facts = $record->facts(['_PRIM'])->filter(static function (Fact $fact): bool {
126            return !$fact->isPendingDeletion();
127        });
128
129        foreach ($facts as $fact) {
130            $primary = strtoupper($fact->value()) !== 'N';
131
132            foreach ($record->linkedIndividuals('OBJE') as $individual) {
133                $this->updateMediaLinks($individual, $record->xref(), $primary);
134            }
135
136            $record->deleteFact($fact->id(), false);
137        }
138    }
139
140    /**
141     * @param Individual $individual
142     * @param string     $xref
143     * @param bool       $primary
144     */
145    private function updateMediaLinks(Individual $individual, string $xref, bool $primary): void
146    {
147        $facts = $individual->facts([], false, null, true);
148
149        $facts1 = new Collection();
150        $facts2 = new Collection();
151        $facts3 = new Collection();
152        $facts4 = new Collection();
153
154        foreach ($facts as $fact) {
155            if ($fact->getTag() !== 'OBJE') {
156                $facts1->push($fact);
157            } elseif ($fact->value() !== '@' . $xref . '@') {
158                $facts3->push($fact);
159            } elseif ($primary) {
160                $facts2->push($fact);
161            } else {
162                $facts4->push($fact);
163            }
164        }
165
166        $sorted_facts = $facts1->concat($facts2)->concat($facts3)->concat($facts4);
167
168        $gedcom = $sorted_facts->map(static function (Fact $fact): string {
169            return "\n" . $fact->gedcom();
170        })->implode('');
171
172        $gedcom = '0 @' . $individual->xref() . '@ INDI' . $gedcom;
173
174        $individual->updateRecord($gedcom, false);
175    }
176}
177