1c0dc1dc8SGreg Roach<?php 23976b470SGreg Roach 3c0dc1dc8SGreg Roach/** 4c0dc1dc8SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 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 15c0dc1dc8SGreg Roach * along with this program. If not, see <http://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 27c0dc1dc8SGreg Roach/** 28c0dc1dc8SGreg Roach * Copy and past facts between records. 29c0dc1dc8SGreg Roach */ 30c0dc1dc8SGreg Roachclass ClipboardService 31c0dc1dc8SGreg Roach{ 32c0dc1dc8SGreg Roach // Maximum number of entries in the clipboard. 33c0dc1dc8SGreg Roach private const CLIPBOARD_SIZE = 10; 34c0dc1dc8SGreg Roach 35c0dc1dc8SGreg Roach /** 36c0dc1dc8SGreg Roach * Copy a fact to the clipboard. 37c0dc1dc8SGreg Roach * 38c0dc1dc8SGreg Roach * @param Fact $fact 39c0dc1dc8SGreg Roach */ 4052a04bc1SGreg Roach public function copyFact(Fact $fact): void 4152a04bc1SGreg Roach { 42c0dc1dc8SGreg Roach $clipboard = Session::get('clipboard', []); 43b8361dd5SGreg Roach $record_type = $fact->record()::RECORD_TYPE; 44c0dc1dc8SGreg Roach $fact_id = $fact->id(); 45c0dc1dc8SGreg Roach 46b8361dd5SGreg Roach // If we are copying the same fact twice, make sure the new one is at the end. 472adcbd9aSGreg Roach unset($clipboard[$record_type][$fact_id]); 48c0dc1dc8SGreg Roach 492adcbd9aSGreg Roach $clipboard[$record_type][$fact_id] = [ 50c0dc1dc8SGreg Roach 'factrec' => $fact->gedcom(), 51c0dc1dc8SGreg Roach 'fact' => $fact->getTag(), 52c0dc1dc8SGreg Roach ]; 53c0dc1dc8SGreg Roach 54c0dc1dc8SGreg Roach // The clipboard only holds a limited number of facts. 552adcbd9aSGreg Roach $clipboard[$record_type] = array_slice($clipboard[$record_type], -self::CLIPBOARD_SIZE); 56c0dc1dc8SGreg Roach 57c0dc1dc8SGreg Roach Session::put('clipboard', $clipboard); 58c0dc1dc8SGreg Roach } 59c0dc1dc8SGreg Roach 60c0dc1dc8SGreg Roach /** 61c0dc1dc8SGreg Roach * Copy a fact from the clipboard to a record. 62c0dc1dc8SGreg Roach * 63c0dc1dc8SGreg Roach * @param string $fact_id 64c0dc1dc8SGreg Roach * @param GedcomRecord $record 65c0dc1dc8SGreg Roach * 66c0dc1dc8SGreg Roach * @return bool 67c0dc1dc8SGreg Roach */ 6852a04bc1SGreg Roach public function pasteFact(string $fact_id, GedcomRecord $record): bool 6952a04bc1SGreg Roach { 70c0dc1dc8SGreg Roach $clipboard = Session::get('clipboard'); 71c0dc1dc8SGreg Roach 72b8361dd5SGreg Roach $record_type = $record::RECORD_TYPE; 73b8361dd5SGreg Roach 74b8361dd5SGreg Roach if (isset($clipboard[$record_type][$fact_id])) { 75b8361dd5SGreg Roach $record->createFact($clipboard[$record_type][$fact_id]['factrec'], true); 76b8361dd5SGreg Roach 77c0dc1dc8SGreg Roach return true; 78c0dc1dc8SGreg Roach } 79c0dc1dc8SGreg Roach 80c0dc1dc8SGreg Roach return false; 81c0dc1dc8SGreg Roach } 82c0dc1dc8SGreg Roach 83c0dc1dc8SGreg Roach /** 842adcbd9aSGreg Roach * Create a list of facts that can be pasted into a given record 85c0dc1dc8SGreg Roach * 86b8361dd5SGreg Roach * @param GedcomRecord $record 872adcbd9aSGreg Roach * @param Collection $exclude_types 88c0dc1dc8SGreg Roach * 89*b5c8fd7eSGreg Roach * @return Collection<Fact> 90c0dc1dc8SGreg Roach */ 912adcbd9aSGreg Roach public function pastableFacts(GedcomRecord $record, Collection $exclude_types): Collection 9252a04bc1SGreg Roach { 93c0dc1dc8SGreg Roach // The facts are stored in the session. 942adcbd9aSGreg Roach return (new Collection(Session::get('clipboard', [])[$record::RECORD_TYPE] ?? [])) 95c0dc1dc8SGreg Roach // Put the most recently copied fact at the top of the list. 962adcbd9aSGreg Roach ->reverse() 97c0dc1dc8SGreg Roach // Create facts for the record. 980b5fd0a6SGreg Roach ->map(static function (array $clipping) use ($record): Fact { 99b8361dd5SGreg Roach return new Fact($clipping['factrec'], $record, md5($clipping['factrec'])); 1000b5fd0a6SGreg Roach })->filter(static function (Fact $fact) use ($exclude_types): bool { 1012adcbd9aSGreg Roach return $exclude_types->isEmpty() || !$exclude_types->contains($fact->getTag()); 1022adcbd9aSGreg Roach }); 1032adcbd9aSGreg Roach } 104c0dc1dc8SGreg Roach 1052adcbd9aSGreg Roach /** 1062adcbd9aSGreg Roach * Find facts of a given type, from all records. 1072adcbd9aSGreg Roach * 1082adcbd9aSGreg Roach * @param GedcomRecord $record 1092adcbd9aSGreg Roach * @param Collection $types 1102adcbd9aSGreg Roach * 111*b5c8fd7eSGreg Roach * @return Collection<Fact> 1122adcbd9aSGreg Roach */ 1132adcbd9aSGreg Roach public function pastableFactsOfType(GedcomRecord $record, Collection $types): Collection 1142adcbd9aSGreg Roach { 1152adcbd9aSGreg Roach // The facts are stored in the session. 1162adcbd9aSGreg Roach return (new Collection(Session::get('clipboard', []))) 1172adcbd9aSGreg Roach ->flatten(1) 1182adcbd9aSGreg Roach ->reverse() 1190b5fd0a6SGreg Roach ->map(static function (array $clipping) use ($record): Fact { 1202adcbd9aSGreg Roach return new Fact($clipping['factrec'], $record, md5($clipping['factrec'])); 1212adcbd9aSGreg Roach }) 1220b5fd0a6SGreg Roach ->filter(static function (Fact $fact) use ($types): bool { 1232adcbd9aSGreg Roach return $types->contains($fact->getTag()); 1242adcbd9aSGreg Roach }); 125c0dc1dc8SGreg Roach } 126c0dc1dc8SGreg Roach} 127