xref: /webtrees/app/Services/ClipboardService.php (revision 54c7f8df0fc7bddb0bcc1441937c30320ca68563)
1c0dc1dc8SGreg Roach<?php
2c0dc1dc8SGreg Roach/**
3c0dc1dc8SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
5c0dc1dc8SGreg Roach * This program is free software: you can redistribute it and/or modify
6c0dc1dc8SGreg Roach * it under the terms of the GNU General Public License as published by
7c0dc1dc8SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8c0dc1dc8SGreg Roach * (at your option) any later version.
9c0dc1dc8SGreg Roach * This program is distributed in the hope that it will be useful,
10c0dc1dc8SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11c0dc1dc8SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12c0dc1dc8SGreg Roach * GNU General Public License for more details.
13c0dc1dc8SGreg Roach * You should have received a copy of the GNU General Public License
14c0dc1dc8SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15c0dc1dc8SGreg Roach */
16c0dc1dc8SGreg Roachdeclare(strict_types=1);
17c0dc1dc8SGreg Roach
18c0dc1dc8SGreg Roachnamespace Fisharebest\Webtrees\Services;
19c0dc1dc8SGreg Roach
20c0dc1dc8SGreg Roachuse Fisharebest\Webtrees\Fact;
21c0dc1dc8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
22c0dc1dc8SGreg Roachuse Fisharebest\Webtrees\Session;
232adcbd9aSGreg Roachuse Illuminate\Support\Collection;
24c0dc1dc8SGreg Roach
25c0dc1dc8SGreg Roach/**
26c0dc1dc8SGreg Roach * Copy and past facts between records.
27c0dc1dc8SGreg Roach */
28c0dc1dc8SGreg Roachclass ClipboardService
29c0dc1dc8SGreg Roach{
30c0dc1dc8SGreg Roach    // Maximum number of entries in the clipboard.
31c0dc1dc8SGreg Roach    private const CLIPBOARD_SIZE = 10;
32c0dc1dc8SGreg Roach
33c0dc1dc8SGreg Roach    /**
34c0dc1dc8SGreg Roach     * Copy a fact to the clipboard.
35c0dc1dc8SGreg Roach     *
36c0dc1dc8SGreg Roach     * @param Fact $fact
37c0dc1dc8SGreg Roach     */
3852a04bc1SGreg Roach    public function copyFact(Fact $fact): void
3952a04bc1SGreg Roach    {
40c0dc1dc8SGreg Roach        $clipboard   = Session::get('clipboard', []);
41b8361dd5SGreg Roach        $record_type = $fact->record()::RECORD_TYPE;
42c0dc1dc8SGreg Roach        $fact_id     = $fact->id();
43c0dc1dc8SGreg Roach
44b8361dd5SGreg Roach        // If we are copying the same fact twice, make sure the new one is at the end.
452adcbd9aSGreg Roach        unset($clipboard[$record_type][$fact_id]);
46c0dc1dc8SGreg Roach
472adcbd9aSGreg Roach        $clipboard[$record_type][$fact_id] = [
48c0dc1dc8SGreg Roach            'factrec' => $fact->gedcom(),
49c0dc1dc8SGreg Roach            'fact'    => $fact->getTag(),
50c0dc1dc8SGreg Roach        ];
51c0dc1dc8SGreg Roach
52c0dc1dc8SGreg Roach        // The clipboard only holds a limited number of facts.
532adcbd9aSGreg Roach        $clipboard[$record_type] = array_slice($clipboard[$record_type], -self::CLIPBOARD_SIZE);
54c0dc1dc8SGreg Roach
55c0dc1dc8SGreg Roach        Session::put('clipboard', $clipboard);
56c0dc1dc8SGreg Roach    }
57c0dc1dc8SGreg Roach
58c0dc1dc8SGreg Roach    /**
59c0dc1dc8SGreg Roach     * Copy a fact from the clipboard to a record.
60c0dc1dc8SGreg Roach     *
61c0dc1dc8SGreg Roach     * @param string       $fact_id
62c0dc1dc8SGreg Roach     * @param GedcomRecord $record
63c0dc1dc8SGreg Roach     *
64c0dc1dc8SGreg Roach     * @return bool
65c0dc1dc8SGreg Roach     */
6652a04bc1SGreg Roach    public function pasteFact(string $fact_id, GedcomRecord $record): bool
6752a04bc1SGreg Roach    {
68c0dc1dc8SGreg Roach        $clipboard = Session::get('clipboard');
69c0dc1dc8SGreg Roach
70b8361dd5SGreg Roach        $record_type = $record::RECORD_TYPE;
71b8361dd5SGreg Roach
72b8361dd5SGreg Roach        if (isset($clipboard[$record_type][$fact_id])) {
73b8361dd5SGreg Roach            $record->createFact($clipboard[$record_type][$fact_id]['factrec'], true);
74b8361dd5SGreg Roach
75c0dc1dc8SGreg Roach            return true;
76c0dc1dc8SGreg Roach        }
77c0dc1dc8SGreg Roach
78c0dc1dc8SGreg Roach        return false;
79c0dc1dc8SGreg Roach    }
80c0dc1dc8SGreg Roach
81c0dc1dc8SGreg Roach    /**
822adcbd9aSGreg Roach     * Create a list of facts that can be pasted into a given record
83c0dc1dc8SGreg Roach     *
84b8361dd5SGreg Roach     * @param GedcomRecord $record
852adcbd9aSGreg Roach     * @param Collection   $exclude_types
86c0dc1dc8SGreg Roach     *
87*54c7f8dfSGreg Roach     * @return Collection
88*54c7f8dfSGreg Roach     * @return Fact[]
89c0dc1dc8SGreg Roach     */
902adcbd9aSGreg Roach    public function pastableFacts(GedcomRecord $record, Collection $exclude_types): Collection
9152a04bc1SGreg Roach    {
92c0dc1dc8SGreg Roach        // The facts are stored in the session.
932adcbd9aSGreg Roach        return (new Collection(Session::get('clipboard', [])[$record::RECORD_TYPE] ?? []))
94c0dc1dc8SGreg Roach            // Put the most recently copied fact at the top of the list.
952adcbd9aSGreg Roach            ->reverse()
96c0dc1dc8SGreg Roach            // Create facts for the record.
972adcbd9aSGreg Roach            ->map(function (array $clipping) use ($record): Fact {
98b8361dd5SGreg Roach                return new Fact($clipping['factrec'], $record, md5($clipping['factrec']));
992adcbd9aSGreg Roach            })->filter(function (Fact $fact) use ($exclude_types): bool {
1002adcbd9aSGreg Roach                return $exclude_types->isEmpty() || !$exclude_types->contains($fact->getTag());
1012adcbd9aSGreg Roach            });
1022adcbd9aSGreg Roach    }
103c0dc1dc8SGreg Roach
1042adcbd9aSGreg Roach    /**
1052adcbd9aSGreg Roach     * Find facts of a given type, from all records.
1062adcbd9aSGreg Roach     *
1072adcbd9aSGreg Roach     * @param GedcomRecord $record
1082adcbd9aSGreg Roach     * @param Collection   $types
1092adcbd9aSGreg Roach     *
110*54c7f8dfSGreg Roach     * @return Collection
111*54c7f8dfSGreg Roach     * @return 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()
1192adcbd9aSGreg Roach            ->map(function (array $clipping) use ($record): Fact {
1202adcbd9aSGreg Roach                return new Fact($clipping['factrec'], $record, md5($clipping['factrec']));
1212adcbd9aSGreg Roach            })
1222adcbd9aSGreg Roach            ->filter(function (Fact $fact) use ($types): bool {
1232adcbd9aSGreg Roach                return $types->contains($fact->getTag());
1242adcbd9aSGreg Roach            });
125c0dc1dc8SGreg Roach    }
126c0dc1dc8SGreg Roach}
127