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\I18N; 24use Fisharebest\Webtrees\Individual; 25use Fisharebest\Webtrees\Services\ClipboardService; 26use Illuminate\Support\Collection; 27 28/** 29 * Class SourcesTabModule 30 */ 31class SourcesTabModule extends AbstractModule implements ModuleTabInterface 32{ 33 use ModuleTabTrait; 34 35 /** @var Collection<array-key,Fact>|null */ 36 private Collection|null $facts = null; 37 38 private ClipboardService $clipboard_service; 39 40 /** 41 * @param ClipboardService $clipboard_service 42 */ 43 public function __construct(ClipboardService $clipboard_service) 44 { 45 $this->clipboard_service = $clipboard_service; 46 } 47 48 /** 49 * How should this module be identified in the control panel, etc.? 50 * 51 * @return string 52 */ 53 public function title(): string 54 { 55 /* I18N: Name of a module */ 56 return I18N::translate('Sources'); 57 } 58 59 /** 60 * A sentence describing what this module does. 61 * 62 * @return string 63 */ 64 public function description(): string 65 { 66 /* I18N: Description of the “Sources” module */ 67 return I18N::translate('A tab showing the sources linked to an individual.'); 68 } 69 70 /** 71 * The default position for this tab. It can be changed in the control panel. 72 * 73 * @return int 74 */ 75 public function defaultTabOrder(): int 76 { 77 return 3; 78 } 79 80 /** 81 * Is this tab empty? If so, we don't always need to display it. 82 * 83 * @param Individual $individual 84 * 85 * @return bool 86 */ 87 public function hasTabContent(Individual $individual): bool 88 { 89 return $individual->canEdit() || $this->getFactsWithSources($individual)->isNotEmpty(); 90 } 91 92 /** 93 * A greyed out tab has no actual content, but may perhaps have 94 * options to create content. 95 * 96 * @param Individual $individual 97 * 98 * @return bool 99 */ 100 public function isGrayedOut(Individual $individual): bool 101 { 102 return $this->getFactsWithSources($individual)->isEmpty(); 103 } 104 105 /** 106 * Generate the HTML content of this tab. 107 * 108 * @param Individual $individual 109 * 110 * @return string 111 */ 112 public function getTabContent(Individual $individual): string 113 { 114 return view('modules/sources_tab/tab', [ 115 'can_edit' => $individual->canEdit(), 116 'clipboard_facts' => $this->clipboard_service->pastableFactsOfType($individual, $this->supportedFacts()), 117 'individual' => $individual, 118 'facts' => $this->getFactsWithSources($individual), 119 ]); 120 } 121 122 /** 123 * Get all the facts for an individual which contain sources. 124 * 125 * @param Individual $individual 126 * 127 * @return Collection<int,Fact> 128 */ 129 private function getFactsWithSources(Individual $individual): Collection 130 { 131 if ($this->facts === null) { 132 $facts = $individual->facts(); 133 134 foreach ($individual->spouseFamilies() as $family) { 135 if ($family->canShow()) { 136 foreach ($family->facts() as $fact) { 137 $facts->push($fact); 138 } 139 } 140 } 141 142 $this->facts = new Collection(); 143 144 foreach ($facts as $fact) { 145 if (preg_match('/(?:^1|\n\d) SOUR/', $fact->gedcom())) { 146 $this->facts->push($fact); 147 } 148 } 149 150 $this->facts = Fact::sortFacts($this->facts); 151 } 152 153 return $this->facts; 154 } 155 156 /** 157 * Can this tab load asynchronously? 158 * 159 * @return bool 160 */ 161 public function canLoadAjax(): bool 162 { 163 return false; 164 } 165 166 /** 167 * This module handles the following facts - so don't show them on the "Facts and events" tab. 168 * 169 * @return Collection<int,string> 170 */ 171 public function supportedFacts(): Collection 172 { 173 return new Collection(['INDI:SOUR', 'FAM:SOUR']); 174 } 175} 176