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\Services; 21 22use Fisharebest\Webtrees\Fact; 23use Fisharebest\Webtrees\GedcomRecord; 24use Fisharebest\Webtrees\Session; 25use Illuminate\Support\Collection; 26 27use function explode; 28use function is_array; 29 30/** 31 * Copy and past facts between records. 32 */ 33class ClipboardService 34{ 35 // Maximum number of entries in the clipboard. 36 private const CLIPBOARD_SIZE = 10; 37 38 /** 39 * Copy a fact to the clipboard. 40 * 41 * @param Fact $fact 42 */ 43 public function copyFact(Fact $fact): void 44 { 45 $clipboard = Session::get('clipboard'); 46 $clipboard = is_array($clipboard) ? $clipboard : []; 47 $record_type = $fact->record()->tag(); 48 $fact_id = $fact->id(); 49 50 // If we are copying the same fact twice, make sure the new one is at the end. 51 unset($clipboard[$record_type][$fact_id]); 52 53 $clipboard[$record_type][$fact_id] = $fact->gedcom(); 54 55 // The clipboard only holds a limited number of facts. 56 $clipboard[$record_type] = array_slice($clipboard[$record_type], -self::CLIPBOARD_SIZE); 57 58 Session::put('clipboard', $clipboard); 59 } 60 61 /** 62 * Copy a fact from the clipboard to a record. 63 * 64 * @param string $fact_id 65 * @param GedcomRecord $record 66 * 67 * @return bool 68 */ 69 public function pasteFact(string $fact_id, GedcomRecord $record): bool 70 { 71 $clipboard = Session::get('clipboard'); 72 73 $record_type = $record->tag(); 74 75 if (isset($clipboard[$record_type][$fact_id])) { 76 $record->createFact($clipboard[$record_type][$fact_id], true); 77 78 return true; 79 } 80 81 return false; 82 } 83 84 /** 85 * Empty the clipboard 86 * 87 * @return void 88 */ 89 public function emptyClipboard(): void 90 { 91 Session::put('clipboard', []); 92 } 93 94 /** 95 * Create a list of facts that can be pasted into a given record 96 * 97 * @param GedcomRecord $record 98 * 99 * @return Collection<int,Fact> 100 */ 101 public function pastableFacts(GedcomRecord $record): Collection 102 { 103 $clipboard = Session::get('clipboard'); 104 $clipboard = is_array($clipboard) ? $clipboard : []; 105 $facts = $clipboard[$record->tag()] ?? []; 106 107 return (new Collection($facts)) 108 ->reverse() 109 ->map(static fn (string $clipping): Fact => new Fact($clipping, $record, md5($clipping))); 110 } 111 112 /** 113 * Find facts of a given type, from all records. 114 * 115 * @param GedcomRecord $record 116 * @param Collection<int,string> $types 117 * 118 * @return Collection<int,Fact> 119 */ 120 public function pastableFactsOfType(GedcomRecord $record, Collection $types): Collection 121 { 122 $clipboard = Session::get('clipboard'); 123 $clipboard = is_array($clipboard) ? $clipboard : []; 124 125 // The facts are stored in the session. 126 return (new Collection($clipboard)) 127 ->flatten(1) 128 ->reverse() 129 ->map(static fn (string $clipping): Fact => new Fact($clipping, $record, md5($clipping))) 130 ->filter(static fn (Fact $fact): bool => $types->contains(explode(':', $fact->tag())[1])); 131 } 132} 133