xref: /webtrees/app/Http/RequestHandlers/CopyFact.php (revision e93a8df2f8d797005750082cc3766c0e80799688)
13b3db8adSGreg Roach<?php
23b3db8adSGreg Roach
33b3db8adSGreg Roach/**
43b3db8adSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
63b3db8adSGreg Roach * This program is free software: you can redistribute it and/or modify
73b3db8adSGreg Roach * it under the terms of the GNU General Public License as published by
83b3db8adSGreg Roach * the Free Software Foundation, either version 3 of the License, or
93b3db8adSGreg Roach * (at your option) any later version.
103b3db8adSGreg Roach * This program is distributed in the hope that it will be useful,
113b3db8adSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
123b3db8adSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
133b3db8adSGreg Roach * GNU General Public License for more details.
143b3db8adSGreg 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/>.
163b3db8adSGreg Roach */
173b3db8adSGreg Roach
183b3db8adSGreg Roachdeclare(strict_types=1);
193b3db8adSGreg Roach
203b3db8adSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
213b3db8adSGreg Roach
223b3db8adSGreg Roachuse Fisharebest\Webtrees\Auth;
233b3db8adSGreg Roachuse Fisharebest\Webtrees\FlashMessages;
243b3db8adSGreg Roachuse Fisharebest\Webtrees\I18N;
256b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
263b3db8adSGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService;
27b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
283b3db8adSGreg Roachuse Psr\Http\Message\ResponseInterface;
293b3db8adSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
303b3db8adSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
313b3db8adSGreg Roach
323b3db8adSGreg Roachuse function response;
333b3db8adSGreg Roach
343b3db8adSGreg Roach/**
353b3db8adSGreg Roach * Copy a fact to the clipboard.
363b3db8adSGreg Roach */
373b3db8adSGreg Roachclass CopyFact implements RequestHandlerInterface
383b3db8adSGreg Roach{
39c4943cffSGreg Roach    private ClipboardService $clipboard_service;
403b3db8adSGreg Roach
413b3db8adSGreg Roach    /**
423b3db8adSGreg Roach     * @param ClipboardService $clipboard_service
433b3db8adSGreg Roach     */
443b3db8adSGreg Roach    public function __construct(ClipboardService $clipboard_service)
453b3db8adSGreg Roach    {
463b3db8adSGreg Roach        $this->clipboard_service = $clipboard_service;
473b3db8adSGreg Roach    }
483b3db8adSGreg Roach
493b3db8adSGreg Roach    /**
503b3db8adSGreg Roach     * Copy a fact to the clipboard.
513b3db8adSGreg Roach     *
523b3db8adSGreg Roach     * @param ServerRequestInterface $request
533b3db8adSGreg Roach     *
543b3db8adSGreg Roach     * @return ResponseInterface
553b3db8adSGreg Roach     */
563b3db8adSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
573b3db8adSGreg Roach    {
58b55cbc6bSGreg Roach        $tree    = Validator::attributes($request)->tree();
59b55cbc6bSGreg Roach        $xref    = Validator::attributes($request)->isXref()->string('xref');
60b55cbc6bSGreg Roach        $fact_id = Validator::attributes($request)->string('fact_id');
616b9cb339SGreg Roach        $record  = Registry::gedcomRecordFactory()->make($xref, $tree);
62ddeb3354SGreg Roach        $record  = Auth::checkRecordAccess($record, true);
633b3db8adSGreg Roach
643b3db8adSGreg Roach        foreach ($record->facts() as $fact) {
653b3db8adSGreg Roach            if ($fact->id() === $fact_id) {
663b3db8adSGreg Roach                $this->clipboard_service->copyFact($fact);
673b3db8adSGreg Roach
689927f70fSGreg Roach                $message =
699927f70fSGreg Roach                    I18N::translate('“%s“ has been copied to the clipboard.', $fact->name()) .
709927f70fSGreg Roach                    '<br>' .
719927f70fSGreg Roach                    I18N::translate('Use the “edit“ menu to paste this into another record.');
729927f70fSGreg Roach
739927f70fSGreg Roach                FlashMessages::addMessage($message);
743b3db8adSGreg Roach                break;
753b3db8adSGreg Roach            }
763b3db8adSGreg Roach        }
773b3db8adSGreg Roach
783b3db8adSGreg Roach        return response();
793b3db8adSGreg Roach    }
803b3db8adSGreg Roach}
81