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