1c0dc1dc8SGreg Roach<?php 23976b470SGreg Roach 3c0dc1dc8SGreg Roach/** 4c0dc1dc8SGreg Roach * webtrees: online genealogy 5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 6c0dc1dc8SGreg Roach * This program is free software: you can redistribute it and/or modify 7c0dc1dc8SGreg Roach * it under the terms of the GNU General Public License as published by 8c0dc1dc8SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9c0dc1dc8SGreg Roach * (at your option) any later version. 10c0dc1dc8SGreg Roach * This program is distributed in the hope that it will be useful, 11c0dc1dc8SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12c0dc1dc8SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13c0dc1dc8SGreg Roach * GNU General Public License for more details. 14c0dc1dc8SGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16c0dc1dc8SGreg Roach */ 17fcfa147eSGreg Roach 18c0dc1dc8SGreg Roachdeclare(strict_types=1); 19c0dc1dc8SGreg Roach 20c0dc1dc8SGreg Roachnamespace Fisharebest\Webtrees\Services; 21c0dc1dc8SGreg Roach 22c0dc1dc8SGreg Roachuse Fisharebest\Webtrees\Fact; 23c0dc1dc8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 24c0dc1dc8SGreg Roachuse Fisharebest\Webtrees\Session; 252adcbd9aSGreg Roachuse Illuminate\Support\Collection; 26c0dc1dc8SGreg Roach 2710e06497SGreg Roachuse function array_slice; 2869cdf014SGreg Roachuse function explode; 29d8809d62SGreg Roachuse function is_array; 3069cdf014SGreg Roach 31c0dc1dc8SGreg Roach/** 32c0dc1dc8SGreg Roach * Copy and past facts between records. 33c0dc1dc8SGreg Roach */ 34c0dc1dc8SGreg Roachclass ClipboardService 35c0dc1dc8SGreg Roach{ 36c0dc1dc8SGreg Roach // Maximum number of entries in the clipboard. 37c0dc1dc8SGreg Roach private const CLIPBOARD_SIZE = 10; 38c0dc1dc8SGreg Roach 39c0dc1dc8SGreg Roach /** 40c0dc1dc8SGreg Roach * Copy a fact to the clipboard. 41c0dc1dc8SGreg Roach * 42c0dc1dc8SGreg Roach * @param Fact $fact 43c0dc1dc8SGreg Roach */ 4452a04bc1SGreg Roach public function copyFact(Fact $fact): void 4552a04bc1SGreg Roach { 46d8809d62SGreg Roach $clipboard = Session::get('clipboard'); 47d8809d62SGreg Roach $clipboard = is_array($clipboard) ? $clipboard : []; 4802467d32SGreg Roach $record_type = $fact->record()->tag(); 49c0dc1dc8SGreg Roach $fact_id = $fact->id(); 50c0dc1dc8SGreg Roach 51b8361dd5SGreg Roach // If we are copying the same fact twice, make sure the new one is at the end. 522adcbd9aSGreg Roach unset($clipboard[$record_type][$fact_id]); 53c0dc1dc8SGreg Roach 5469cdf014SGreg Roach $clipboard[$record_type][$fact_id] = $fact->gedcom(); 55c0dc1dc8SGreg Roach 56c0dc1dc8SGreg Roach // The clipboard only holds a limited number of facts. 572adcbd9aSGreg Roach $clipboard[$record_type] = array_slice($clipboard[$record_type], -self::CLIPBOARD_SIZE); 58c0dc1dc8SGreg Roach 59c0dc1dc8SGreg Roach Session::put('clipboard', $clipboard); 60c0dc1dc8SGreg Roach } 61c0dc1dc8SGreg Roach 62c0dc1dc8SGreg Roach /** 63c0dc1dc8SGreg Roach * Copy a fact from the clipboard to a record. 64c0dc1dc8SGreg Roach * 65c0dc1dc8SGreg Roach * @param string $fact_id 66c0dc1dc8SGreg Roach * @param GedcomRecord $record 67c0dc1dc8SGreg Roach * 68c0dc1dc8SGreg Roach * @return bool 69c0dc1dc8SGreg Roach */ 7052a04bc1SGreg Roach public function pasteFact(string $fact_id, GedcomRecord $record): bool 7152a04bc1SGreg Roach { 72c0dc1dc8SGreg Roach $clipboard = Session::get('clipboard'); 73c0dc1dc8SGreg Roach 7402467d32SGreg Roach $record_type = $record->tag(); 75b8361dd5SGreg Roach 76b8361dd5SGreg Roach if (isset($clipboard[$record_type][$fact_id])) { 7769cdf014SGreg Roach $record->createFact($clipboard[$record_type][$fact_id], true); 78b8361dd5SGreg Roach 79c0dc1dc8SGreg Roach return true; 80c0dc1dc8SGreg Roach } 81c0dc1dc8SGreg Roach 82c0dc1dc8SGreg Roach return false; 83c0dc1dc8SGreg Roach } 84c0dc1dc8SGreg Roach 85c0dc1dc8SGreg Roach /** 869927f70fSGreg Roach * Empty the clipboard 879927f70fSGreg Roach * 889927f70fSGreg Roach * @return void 899927f70fSGreg Roach */ 909927f70fSGreg Roach public function emptyClipboard(): void 919927f70fSGreg Roach { 929927f70fSGreg Roach Session::put('clipboard', []); 939927f70fSGreg Roach } 949927f70fSGreg Roach 959927f70fSGreg Roach /** 962adcbd9aSGreg Roach * Create a list of facts that can be pasted into a given record 97c0dc1dc8SGreg Roach * 98b8361dd5SGreg Roach * @param GedcomRecord $record 99c0dc1dc8SGreg Roach * 10036779af1SGreg Roach * @return Collection<int,Fact> 101c0dc1dc8SGreg Roach */ 10269cdf014SGreg Roach public function pastableFacts(GedcomRecord $record): Collection 10352a04bc1SGreg Roach { 104d8809d62SGreg Roach $clipboard = Session::get('clipboard'); 105d8809d62SGreg Roach $clipboard = is_array($clipboard) ? $clipboard : []; 106d8809d62SGreg Roach $facts = $clipboard[$record->tag()] ?? []; 107d8809d62SGreg Roach 108d8809d62SGreg Roach return (new Collection($facts)) 1092adcbd9aSGreg Roach ->reverse() 110d8809d62SGreg Roach ->map(static fn (string $clipping): Fact => new Fact($clipping, $record, md5($clipping))); 1112adcbd9aSGreg Roach } 112c0dc1dc8SGreg Roach 1132adcbd9aSGreg Roach /** 1142adcbd9aSGreg Roach * Find facts of a given type, from all records. 1152adcbd9aSGreg Roach * 1162adcbd9aSGreg Roach * @param GedcomRecord $record 11776d39c55SGreg Roach * @param Collection<int,string> $types 1182adcbd9aSGreg Roach * 11936779af1SGreg Roach * @return Collection<int,Fact> 1202adcbd9aSGreg Roach */ 1212adcbd9aSGreg Roach public function pastableFactsOfType(GedcomRecord $record, Collection $types): Collection 1222adcbd9aSGreg Roach { 123d8809d62SGreg Roach $clipboard = Session::get('clipboard'); 124d8809d62SGreg Roach $clipboard = is_array($clipboard) ? $clipboard : []; 125d8809d62SGreg Roach 1262adcbd9aSGreg Roach // The facts are stored in the session. 127d8809d62SGreg Roach return (new Collection($clipboard)) 1282adcbd9aSGreg Roach ->flatten(1) 1292adcbd9aSGreg Roach ->reverse() 130d8809d62SGreg Roach ->map(static fn (string $clipping): Fact => new Fact($clipping, $record, md5($clipping))) 131d8809d62SGreg Roach ->filter(static fn (Fact $fact): bool => $types->contains(explode(':', $fact->tag())[1])); 132c0dc1dc8SGreg Roach } 133c0dc1dc8SGreg Roach} 134