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; 2869cdf014SGreg Roach 29c0dc1dc8SGreg Roach/** 30c0dc1dc8SGreg Roach * Copy and past facts between records. 31c0dc1dc8SGreg Roach */ 32c0dc1dc8SGreg Roachclass ClipboardService 33c0dc1dc8SGreg Roach{ 34c0dc1dc8SGreg Roach // Maximum number of entries in the clipboard. 35c0dc1dc8SGreg Roach private const CLIPBOARD_SIZE = 10; 36c0dc1dc8SGreg Roach 37c0dc1dc8SGreg Roach /** 38c0dc1dc8SGreg Roach * Copy a fact to the clipboard. 39c0dc1dc8SGreg Roach * 40c0dc1dc8SGreg Roach * @param Fact $fact 41c0dc1dc8SGreg Roach */ 4252a04bc1SGreg Roach public function copyFact(Fact $fact): void 4352a04bc1SGreg Roach { 44c0dc1dc8SGreg Roach $clipboard = Session::get('clipboard', []); 4502467d32SGreg Roach $record_type = $fact->record()->tag(); 46c0dc1dc8SGreg Roach $fact_id = $fact->id(); 47c0dc1dc8SGreg Roach 48b8361dd5SGreg Roach // If we are copying the same fact twice, make sure the new one is at the end. 492adcbd9aSGreg Roach unset($clipboard[$record_type][$fact_id]); 50c0dc1dc8SGreg Roach 5169cdf014SGreg Roach $clipboard[$record_type][$fact_id] = $fact->gedcom(); 52c0dc1dc8SGreg Roach 53c0dc1dc8SGreg Roach // The clipboard only holds a limited number of facts. 542adcbd9aSGreg Roach $clipboard[$record_type] = array_slice($clipboard[$record_type], -self::CLIPBOARD_SIZE); 55c0dc1dc8SGreg Roach 56c0dc1dc8SGreg Roach Session::put('clipboard', $clipboard); 57c0dc1dc8SGreg Roach } 58c0dc1dc8SGreg Roach 59c0dc1dc8SGreg Roach /** 60c0dc1dc8SGreg Roach * Copy a fact from the clipboard to a record. 61c0dc1dc8SGreg Roach * 62c0dc1dc8SGreg Roach * @param string $fact_id 63c0dc1dc8SGreg Roach * @param GedcomRecord $record 64c0dc1dc8SGreg Roach * 65c0dc1dc8SGreg Roach * @return bool 66c0dc1dc8SGreg Roach */ 6752a04bc1SGreg Roach public function pasteFact(string $fact_id, GedcomRecord $record): bool 6852a04bc1SGreg Roach { 69c0dc1dc8SGreg Roach $clipboard = Session::get('clipboard'); 70c0dc1dc8SGreg Roach 7102467d32SGreg Roach $record_type = $record->tag(); 72b8361dd5SGreg Roach 73b8361dd5SGreg Roach if (isset($clipboard[$record_type][$fact_id])) { 7469cdf014SGreg Roach $record->createFact($clipboard[$record_type][$fact_id], true); 75b8361dd5SGreg Roach 76c0dc1dc8SGreg Roach return true; 77c0dc1dc8SGreg Roach } 78c0dc1dc8SGreg Roach 79c0dc1dc8SGreg Roach return false; 80c0dc1dc8SGreg Roach } 81c0dc1dc8SGreg Roach 82c0dc1dc8SGreg Roach /** 83*9927f70fSGreg Roach * Empty the clipboard 84*9927f70fSGreg Roach * 85*9927f70fSGreg Roach * @return void 86*9927f70fSGreg Roach */ 87*9927f70fSGreg Roach public function emptyClipboard(): void 88*9927f70fSGreg Roach { 89*9927f70fSGreg Roach Session::put('clipboard', []); 90*9927f70fSGreg Roach } 91*9927f70fSGreg Roach 92*9927f70fSGreg Roach /** 932adcbd9aSGreg Roach * Create a list of facts that can be pasted into a given record 94c0dc1dc8SGreg Roach * 95b8361dd5SGreg Roach * @param GedcomRecord $record 96c0dc1dc8SGreg Roach * 97b5c8fd7eSGreg Roach * @return Collection<Fact> 98c0dc1dc8SGreg Roach */ 9969cdf014SGreg Roach public function pastableFacts(GedcomRecord $record): Collection 10052a04bc1SGreg Roach { 101c0dc1dc8SGreg Roach // The facts are stored in the session. 10202467d32SGreg Roach return (new Collection(Session::get('clipboard', [])[$record->tag()] ?? [])) 103c0dc1dc8SGreg Roach // Put the most recently copied fact at the top of the list. 1042adcbd9aSGreg Roach ->reverse() 105c0dc1dc8SGreg Roach // Create facts for the record. 10669cdf014SGreg Roach ->map(static function (string $clipping) use ($record): Fact { 10769cdf014SGreg Roach return new Fact($clipping, $record, md5($clipping)); 1082adcbd9aSGreg Roach }); 1092adcbd9aSGreg Roach } 110c0dc1dc8SGreg Roach 1112adcbd9aSGreg Roach /** 1122adcbd9aSGreg Roach * Find facts of a given type, from all records. 1132adcbd9aSGreg Roach * 1142adcbd9aSGreg Roach * @param GedcomRecord $record 1152adcbd9aSGreg Roach * @param Collection $types 1162adcbd9aSGreg Roach * 117b5c8fd7eSGreg Roach * @return Collection<Fact> 1182adcbd9aSGreg Roach */ 1192adcbd9aSGreg Roach public function pastableFactsOfType(GedcomRecord $record, Collection $types): Collection 1202adcbd9aSGreg Roach { 1212adcbd9aSGreg Roach // The facts are stored in the session. 1222adcbd9aSGreg Roach return (new Collection(Session::get('clipboard', []))) 1232adcbd9aSGreg Roach ->flatten(1) 1242adcbd9aSGreg Roach ->reverse() 12569cdf014SGreg Roach ->map(static function (string $clipping) use ($record): Fact { 12669cdf014SGreg Roach return new Fact($clipping, $record, md5($clipping)); 1272adcbd9aSGreg Roach }) 1280b5fd0a6SGreg Roach ->filter(static function (Fact $fact) use ($types): bool { 12969cdf014SGreg Roach return $types->contains(explode(':', $fact->tag())[1]); 1302adcbd9aSGreg Roach }); 131c0dc1dc8SGreg Roach } 132c0dc1dc8SGreg Roach} 133