xref: /webtrees/app/Module/FixPrimaryTag.php (revision 78606cf427772fbdc5222ff607ee23f43078f94b)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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\Services\LinkedRecordService;
27use Fisharebest\Webtrees\Tree;
28use Illuminate\Support\Collection;
29
30use function e;
31use function str_contains;
32use function strtoupper;
33
34/**
35 * Class FixPrimaryTag
36 */
37class FixPrimaryTag extends AbstractModule implements ModuleDataFixInterface
38{
39    use ModuleDataFixTrait;
40
41    private LinkedRecordService $linked_record_service;
42
43    /**
44     * @param LinkedRecordService $linked_record_service
45     */
46    public function __construct(LinkedRecordService $linked_record_service)
47    {
48        $this->linked_record_service = $linked_record_service;
49    }
50
51    /**
52     * How should this module be identified in the control panel, etc.?
53     *
54     * @return string
55     */
56    public function title(): string
57    {
58        /* I18N: Name of a module */
59        return I18N::translate('Convert %s tags to GEDCOM 5.5.1', 'OBJE:_PRIM');
60    }
61
62    /**
63     * A sentence describing what this module does.
64     *
65     * @return string
66     */
67    public function description(): string
68    {
69        /* I18N: Description of a “Data fix” module */
70        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.');
71    }
72
73    /**
74     * XREFs of media records that might need fixing.
75     *
76     * @param Tree                 $tree
77     * @param array<string,string> $params
78     *
79     * @return Collection<int,string>
80     */
81    public function mediaToFix(Tree $tree, array $params): Collection
82    {
83        return $this->mediaToFixQuery($tree, $params)
84            ->where('m_file', '=', $tree->id())
85            ->where('m_gedcom', 'LIKE', "%\n1 _PRIM %")
86            ->pluck('m_id');
87    }
88
89    /**
90     * Does a record need updating?
91     *
92     * @param GedcomRecord         $record
93     * @param array<string,string> $params
94     *
95     * @return bool
96     */
97    public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool
98    {
99        return str_contains($record->gedcom(), "\n1 _PRIM ");
100    }
101
102    /**
103     * Show the changes we would make
104     *
105     * @param GedcomRecord         $record
106     * @param array<string,string> $params
107     *
108     * @return string
109     */
110    public function previewUpdate(GedcomRecord $record, array $params): string
111    {
112        $html = '';
113        foreach ($record->facts(['_PRIM']) as $prim) {
114            $html = '<p>' . I18N::translate('Delete') . ' – <code>' . e($prim->gedcom()) . '</code></p>';
115        }
116
117        $html .= '<ul>';
118        foreach ($this->linked_record_service->linkedIndividuals($record) as $individual) {
119            $html .= '<li>' . I18N::translate('Re-order media') . ' – <a href="' . e($individual->url()) . '">' . $individual->fullName() . '</a></li>';
120        }
121        $html .= '</ul>';
122
123        return $html;
124    }
125
126    /**
127     * Fix a record
128     *
129     * @param GedcomRecord         $record
130     * @param array<string,string> $params
131     *
132     * @return void
133     */
134    public function updateRecord(GedcomRecord $record, array $params): void
135    {
136        $facts = $record->facts(['_PRIM'])->filter(static function (Fact $fact): bool {
137            return !$fact->isPendingDeletion();
138        });
139
140        foreach ($facts as $fact) {
141            $primary = strtoupper($fact->value()) !== 'N';
142
143            foreach ($this->linked_record_service->linkedIndividuals($record) as $individual) {
144                $this->updateMediaLinks($individual, $record->xref(), $primary);
145            }
146
147            $record->deleteFact($fact->id(), false);
148        }
149    }
150
151    /**
152     * @param Individual $individual
153     * @param string     $xref
154     * @param bool       $primary
155     */
156    private function updateMediaLinks(Individual $individual, string $xref, bool $primary): void
157    {
158        $facts = $individual->facts([], false, null, true);
159
160        $facts1 = new Collection();
161        $facts2 = new Collection();
162        $facts3 = new Collection();
163        $facts4 = new Collection();
164
165        foreach ($facts as $fact) {
166            if ($fact->tag() !== 'INDI:OBJE') {
167                $facts1->push($fact);
168            } elseif ($fact->value() !== '@' . $xref . '@') {
169                $facts3->push($fact);
170            } elseif ($primary) {
171                $facts2->push($fact);
172            } else {
173                $facts4->push($fact);
174            }
175        }
176
177        $sorted_facts = $facts1->concat($facts2)->concat($facts3)->concat($facts4);
178
179        $gedcom = $sorted_facts->map(static function (Fact $fact): string {
180            return "\n" . $fact->gedcom();
181        })->implode('');
182
183        $gedcom = '0 @' . $individual->xref() . '@ INDI' . $gedcom;
184
185        $individual->updateRecord($gedcom, false);
186    }
187}
188