1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 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 fn(Fact $fact): bool => !$fact->isPendingDeletion()); 137 138 foreach ($facts as $fact) { 139 $primary = strtoupper($fact->value()) !== 'N'; 140 141 foreach ($this->linked_record_service->linkedIndividuals($record) as $individual) { 142 $this->updateMediaLinks($individual, $record->xref(), $primary); 143 } 144 145 $record->deleteFact($fact->id(), false); 146 } 147 } 148 149 /** 150 * @param Individual $individual 151 * @param string $xref 152 * @param bool $primary 153 */ 154 private function updateMediaLinks(Individual $individual, string $xref, bool $primary): void 155 { 156 $facts = $individual->facts([], false, null, true); 157 158 $facts1 = new Collection(); 159 $facts2 = new Collection(); 160 $facts3 = new Collection(); 161 $facts4 = new Collection(); 162 163 foreach ($facts as $fact) { 164 if ($fact->tag() !== 'INDI:OBJE') { 165 $facts1->push($fact); 166 } elseif ($fact->value() !== '@' . $xref . '@') { 167 $facts3->push($fact); 168 } elseif ($primary) { 169 $facts2->push($fact); 170 } else { 171 $facts4->push($fact); 172 } 173 } 174 175 $sorted_facts = $facts1->concat($facts2)->concat($facts3)->concat($facts4); 176 177 $gedcom = $sorted_facts->map(static fn(Fact $fact): string => "\n" . $fact->gedcom())->implode(''); 178 179 $gedcom = '0 @' . $individual->xref() . '@ INDI' . $gedcom; 180 181 $individual->updateRecord($gedcom, false); 182 } 183} 184