Passed
Push — master ( 94abca...7bb122 )
by Greg
05:28
created

FixPrimaryTag   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 44
dl 0
loc 141
rs 10
c 1
b 1
f 0
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A previewUpdate() 0 14 3
A mediaToFix() 0 6 1
A updateRecord() 0 14 3
A title() 0 4 1
A updateMediaLinks() 0 32 5
A description() 0 4 1
A doesRecordNeedUpdate() 0 3 1
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2019 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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Module;
21
22
use Fisharebest\Webtrees\Fact;
23
use Fisharebest\Webtrees\GedcomRecord;
24
use Fisharebest\Webtrees\I18N;
25
use Fisharebest\Webtrees\Individual;
26
use Fisharebest\Webtrees\Tree;
27
use Illuminate\Database\Capsule\Manager as DB;
28
use Illuminate\Support\Collection;
29
30
use function e;
31
use function strpos;
32
use function strtoupper;
33
34
/**
35
 * Class FixPrimaryTag
36
 */
37
class FixPrimaryTag extends AbstractModule implements ModuleDataFixInterface
38
{
39
    use ModuleDataFixTrait;
1 ignored issue
show
Bug introduced by
The trait Fisharebest\Webtrees\Module\ModuleDataFixTrait requires the property $xref which is not provided by Fisharebest\Webtrees\Module\FixPrimaryTag.
Loading history...
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
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

71
    public function mediaToFix(Tree $tree, /** @scrutinizer ignore-unused */ array $params): Collection

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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 strpos($record->gedcom(), "\n1 _PRIM ") !== false;
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()->filter(static function (Fact $fact): bool {
149
            return !$fact->isPendingDeletion();
150
        });
151
152
        $facts1 = new Collection();
153
        $facts2 = new Collection();
154
        $facts3 = new Collection();
155
        $facts4 = new Collection();
156
157
        foreach ($facts as $fact) {
158
            if ($fact->getTag() !== 'OBJE') {
159
                $facts1->push($fact);
160
            } elseif ($fact->value() !== '@' . $xref . '@') {
161
                $facts3->push($fact);
162
            } elseif ($primary) {
163
                $facts2->push($fact);
164
            } else {
165
                $facts4->push($fact);
166
            }
167
        }
168
169
        $sorted_facts = $facts1->concat($facts2)->concat($facts3)->concat($facts4);
170
171
        $gedcom = $sorted_facts->map(static function (Fact $fact): string {
172
            return "\n" . $fact->gedcom();
173
        })->implode('');
174
175
        $gedcom = '0 @' . $individual->xref() . '@ INDI' . $gedcom;
176
177
        $individual->updateRecord($gedcom, false);
178
    }
179
}
180