1c0dc1dc8SGreg Roach<?php 23976b470SGreg Roach 3c0dc1dc8SGreg Roach/** 4c0dc1dc8SGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 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 2769cdf014SGreg Roachuse function explode; 28d8809d62SGreg Roachuse function is_array; 2969cdf014SGreg Roach 30c0dc1dc8SGreg Roach/** 31c0dc1dc8SGreg Roach * Copy and past facts between records. 32c0dc1dc8SGreg Roach */ 33c0dc1dc8SGreg Roachclass ClipboardService 34c0dc1dc8SGreg Roach{ 35c0dc1dc8SGreg Roach // Maximum number of entries in the clipboard. 36c0dc1dc8SGreg Roach private const CLIPBOARD_SIZE = 10; 37c0dc1dc8SGreg Roach 38c0dc1dc8SGreg Roach /** 39c0dc1dc8SGreg Roach * Copy a fact to the clipboard. 40c0dc1dc8SGreg Roach * 41c0dc1dc8SGreg Roach * @param Fact $fact 42c0dc1dc8SGreg Roach */ 4352a04bc1SGreg Roach public function copyFact(Fact $fact): void 4452a04bc1SGreg Roach { 45d8809d62SGreg Roach $clipboard = Session::get('clipboard'); 46d8809d62SGreg Roach $clipboard = is_array($clipboard) ? $clipboard : []; 4702467d32SGreg Roach $record_type = $fact->record()->tag(); 48c0dc1dc8SGreg Roach $fact_id = $fact->id(); 49c0dc1dc8SGreg Roach 50b8361dd5SGreg Roach // If we are copying the same fact twice, make sure the new one is at the end. 512adcbd9aSGreg Roach unset($clipboard[$record_type][$fact_id]); 52c0dc1dc8SGreg Roach 5369cdf014SGreg Roach $clipboard[$record_type][$fact_id] = $fact->gedcom(); 54c0dc1dc8SGreg Roach 55c0dc1dc8SGreg Roach // The clipboard only holds a limited number of facts. 562adcbd9aSGreg Roach $clipboard[$record_type] = array_slice($clipboard[$record_type], -self::CLIPBOARD_SIZE); 57c0dc1dc8SGreg Roach 58c0dc1dc8SGreg Roach Session::put('clipboard', $clipboard); 59c0dc1dc8SGreg Roach } 60c0dc1dc8SGreg Roach 61c0dc1dc8SGreg Roach /** 62c0dc1dc8SGreg Roach * Copy a fact from the clipboard to a record. 63c0dc1dc8SGreg Roach * 64c0dc1dc8SGreg Roach * @param string $fact_id 65c0dc1dc8SGreg Roach * @param GedcomRecord $record 66c0dc1dc8SGreg Roach * 67c0dc1dc8SGreg Roach * @return bool 68c0dc1dc8SGreg Roach */ 6952a04bc1SGreg Roach public function pasteFact(string $fact_id, GedcomRecord $record): bool 7052a04bc1SGreg Roach { 71c0dc1dc8SGreg Roach $clipboard = Session::get('clipboard'); 72c0dc1dc8SGreg Roach 7302467d32SGreg Roach $record_type = $record->tag(); 74b8361dd5SGreg Roach 75b8361dd5SGreg Roach if (isset($clipboard[$record_type][$fact_id])) { 7669cdf014SGreg Roach $record->createFact($clipboard[$record_type][$fact_id], true); 77b8361dd5SGreg Roach 78c0dc1dc8SGreg Roach return true; 79c0dc1dc8SGreg Roach } 80c0dc1dc8SGreg Roach 81c0dc1dc8SGreg Roach return false; 82c0dc1dc8SGreg Roach } 83c0dc1dc8SGreg Roach 84c0dc1dc8SGreg Roach /** 859927f70fSGreg Roach * Empty the clipboard 869927f70fSGreg Roach * 879927f70fSGreg Roach * @return void 889927f70fSGreg Roach */ 899927f70fSGreg Roach public function emptyClipboard(): void 909927f70fSGreg Roach { 919927f70fSGreg Roach Session::put('clipboard', []); 929927f70fSGreg Roach } 939927f70fSGreg Roach 949927f70fSGreg Roach /** 952adcbd9aSGreg Roach * Create a list of facts that can be pasted into a given record 96c0dc1dc8SGreg Roach * 97b8361dd5SGreg Roach * @param GedcomRecord $record 98c0dc1dc8SGreg Roach * 99*36779af1SGreg Roach * @return Collection<int,Fact> 100c0dc1dc8SGreg Roach */ 10169cdf014SGreg Roach public function pastableFacts(GedcomRecord $record): Collection 10252a04bc1SGreg Roach { 103d8809d62SGreg Roach $clipboard = Session::get('clipboard'); 104d8809d62SGreg Roach $clipboard = is_array($clipboard) ? $clipboard : []; 105d8809d62SGreg Roach $facts = $clipboard[$record->tag()] ?? []; 106d8809d62SGreg Roach 107d8809d62SGreg Roach return (new Collection($facts)) 1082adcbd9aSGreg Roach ->reverse() 109d8809d62SGreg Roach ->map(static fn (string $clipping): Fact => new Fact($clipping, $record, md5($clipping))); 1102adcbd9aSGreg Roach } 111c0dc1dc8SGreg Roach 1122adcbd9aSGreg Roach /** 1132adcbd9aSGreg Roach * Find facts of a given type, from all records. 1142adcbd9aSGreg Roach * 1152adcbd9aSGreg Roach * @param GedcomRecord $record 1162adcbd9aSGreg Roach * @param Collection $types 1172adcbd9aSGreg Roach * 118*36779af1SGreg Roach * @return Collection<int,Fact> 1192adcbd9aSGreg Roach */ 1202adcbd9aSGreg Roach public function pastableFactsOfType(GedcomRecord $record, Collection $types): Collection 1212adcbd9aSGreg Roach { 122d8809d62SGreg Roach $clipboard = Session::get('clipboard'); 123d8809d62SGreg Roach $clipboard = is_array($clipboard) ? $clipboard : []; 124d8809d62SGreg Roach 1252adcbd9aSGreg Roach // The facts are stored in the session. 126d8809d62SGreg Roach return (new Collection($clipboard)) 1272adcbd9aSGreg Roach ->flatten(1) 1282adcbd9aSGreg Roach ->reverse() 129d8809d62SGreg Roach ->map(static fn (string $clipping): Fact => new Fact($clipping, $record, md5($clipping))) 130d8809d62SGreg Roach ->filter(static fn (Fact $fact): bool => $types->contains(explode(':', $fact->tag())[1])); 131c0dc1dc8SGreg Roach } 132c0dc1dc8SGreg Roach} 133