xref: /webtrees/app/Module/ClippingsCartModule.php (revision e2ed7c791d7adc6080be1f2b3897a4e621cd859f)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98c2e8227SGreg Roach * (at your option) any later version.
108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138c2e8227SGreg Roach * GNU General Public License for more details.
148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
22de2aa325SGreg Roachuse Aura\Router\Route;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
240bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\FamilyNotFoundException;
250bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualNotFoundException;
260bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\MediaNotFoundException;
270bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\NoteNotFoundException;
280bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\RepositoryNotFoundException;
290bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\SourceNotFoundException;
300e62c4b8SGreg Roachuse Fisharebest\Webtrees\Family;
315a78cd34SGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsExport;
325a78cd34SGreg Roachuse Fisharebest\Webtrees\Gedcom;
330e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
34f95e0480SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\FamilyPage;
35f95e0480SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\IndividualPage;
36f95e0480SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\MediaPage;
37f95e0480SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\NotePage;
38f95e0480SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\RepositoryPage;
39f95e0480SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\SourcePage;
400e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
410e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
425a78cd34SGreg Roachuse Fisharebest\Webtrees\Media;
430e62c4b8SGreg Roachuse Fisharebest\Webtrees\Menu;
445a78cd34SGreg Roachuse Fisharebest\Webtrees\Note;
455a78cd34SGreg Roachuse Fisharebest\Webtrees\Repository;
46e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
470e62c4b8SGreg Roachuse Fisharebest\Webtrees\Session;
485a78cd34SGreg Roachuse Fisharebest\Webtrees\Source;
49aee13b6dSGreg Roachuse Fisharebest\Webtrees\Tree;
505a78cd34SGreg Roachuse League\Flysystem\Filesystem;
51a04bb9a2SGreg Roachuse League\Flysystem\FilesystemInterface;
5261bf91b2SGreg Roachuse League\Flysystem\MountManager;
535a78cd34SGreg Roachuse League\Flysystem\ZipArchive\ZipArchiveAdapter;
54bed27cedSGreg Roachuse Psr\Http\Message\ResponseFactoryInterface;
556ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
566ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
576ccdf4f0SGreg Roachuse Psr\Http\Message\StreamFactoryInterface;
583976b470SGreg Roach
59eb235819SGreg Roachuse function app;
60bf80ec58SGreg Roachuse function array_filter;
61bf80ec58SGreg Roachuse function array_keys;
62bf80ec58SGreg Roachuse function array_map;
635229eadeSGreg Roachuse function assert;
64bf80ec58SGreg Roachuse function in_array;
65ddeb3354SGreg Roachuse function is_string;
66bf80ec58SGreg Roachuse function key;
67bf80ec58SGreg Roachuse function preg_match_all;
68bf80ec58SGreg Roachuse function redirect;
69bf80ec58SGreg Roachuse function route;
70e5a6b4d4SGreg Roachuse function str_replace;
71bf80ec58SGreg Roachuse function strip_tags;
72bf80ec58SGreg Roachuse function sys_get_temp_dir;
73bf80ec58SGreg Roachuse function tempnam;
74bf80ec58SGreg Roachuse function utf8_decode;
758c2e8227SGreg Roach
768c2e8227SGreg Roach/**
778c2e8227SGreg Roach * Class ClippingsCartModule
788c2e8227SGreg Roach */
7937eb8894SGreg Roachclass ClippingsCartModule extends AbstractModule implements ModuleMenuInterface
80c1010edaSGreg Roach{
8149a243cbSGreg Roach    use ModuleMenuTrait;
8249a243cbSGreg Roach
835a78cd34SGreg Roach    // Routes that have a record which can be added to the clipboard
8416d6367aSGreg Roach    private const ROUTES_WITH_RECORDS = [
85f95e0480SGreg Roach        'Family' => FamilyPage::class,
86f95e0480SGreg Roach        'Individual' => IndividualPage::class,
87f95e0480SGreg Roach        'Media' => MediaPage::class,
88f95e0480SGreg Roach        'Note' => NotePage::class,
89f95e0480SGreg Roach        'Repository' => RepositoryPage::class,
90f95e0480SGreg Roach        'Source' => SourcePage::class,
91c1010edaSGreg Roach    ];
925a78cd34SGreg Roach
9349a243cbSGreg Roach    /** @var int The default access level for this module.  It can be changed in the control panel. */
9449a243cbSGreg Roach    protected $access_level = Auth::PRIV_USER;
9549a243cbSGreg Roach
96961ec755SGreg Roach    /**
97e5a6b4d4SGreg Roach     * @var UserService
98e5a6b4d4SGreg Roach     */
99e5a6b4d4SGreg Roach    private $user_service;
100e5a6b4d4SGreg Roach
101e5a6b4d4SGreg Roach    /**
102e5a6b4d4SGreg Roach     * ClippingsCartModule constructor.
103e5a6b4d4SGreg Roach     *
104e5a6b4d4SGreg Roach     * @param UserService $user_service
105e5a6b4d4SGreg Roach     */
106e5a6b4d4SGreg Roach    public function __construct(UserService $user_service)
107e5a6b4d4SGreg Roach    {
108e5a6b4d4SGreg Roach        $this->user_service = $user_service;
109e5a6b4d4SGreg Roach    }
110e5a6b4d4SGreg Roach
111e5a6b4d4SGreg Roach    /**
1120cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
113961ec755SGreg Roach     *
114961ec755SGreg Roach     * @return string
115961ec755SGreg Roach     */
11649a243cbSGreg Roach    public function title(): string
117c1010edaSGreg Roach    {
118bbb76c12SGreg Roach        /* I18N: Name of a module */
119bbb76c12SGreg Roach        return I18N::translate('Clippings cart');
1208c2e8227SGreg Roach    }
1218c2e8227SGreg Roach
122961ec755SGreg Roach    /**
123961ec755SGreg Roach     * A sentence describing what this module does.
124961ec755SGreg Roach     *
125961ec755SGreg Roach     * @return string
126961ec755SGreg Roach     */
12749a243cbSGreg Roach    public function description(): string
128c1010edaSGreg Roach    {
129bbb76c12SGreg Roach        /* I18N: Description of the “Clippings cart” module */
130bbb76c12SGreg Roach        return I18N::translate('Select records from your family tree and save them as a GEDCOM file.');
1318c2e8227SGreg Roach    }
1328c2e8227SGreg Roach
1330ee13198SGreg Roach    /**
13449a243cbSGreg Roach     * The default position for this menu.  It can be changed in the control panel.
1350ee13198SGreg Roach     *
1360ee13198SGreg Roach     * @return int
1370ee13198SGreg Roach     */
1388f53f488SRico Sonntag    public function defaultMenuOrder(): int
139c1010edaSGreg Roach    {
140353b36abSGreg Roach        return 6;
1418c2e8227SGreg Roach    }
1428c2e8227SGreg Roach
1430ee13198SGreg Roach    /**
1440ee13198SGreg Roach     * A menu, to be added to the main application menu.
1450ee13198SGreg Roach     *
146aee13b6dSGreg Roach     * @param Tree $tree
147aee13b6dSGreg Roach     *
1480ee13198SGreg Roach     * @return Menu|null
1490ee13198SGreg Roach     */
15046295629SGreg Roach    public function getMenu(Tree $tree): ?Menu
151c1010edaSGreg Roach    {
152eb235819SGreg Roach        /** @var ServerRequestInterface $request */
1536ccdf4f0SGreg Roach        $request = app(ServerRequestInterface::class);
1548c2e8227SGreg Roach
155f7ab47b1SGreg Roach        $route = $request->getAttribute('route');
156de2aa325SGreg Roach        assert($route instanceof Route);
1575a78cd34SGreg Roach
1585a78cd34SGreg Roach        $submenus = [
15949a243cbSGreg Roach            new Menu($this->title(), route('module', [
16026684e68SGreg Roach                'module' => $this->name(),
161c1010edaSGreg Roach                'action' => 'Show',
162d72b284aSGreg Roach                'tree'    => $tree->name(),
163c1010edaSGreg Roach            ]), 'menu-clippings-cart', ['rel' => 'nofollow']),
1645a78cd34SGreg Roach        ];
1655a78cd34SGreg Roach
1662b0d92b4SGreg Roach        $action = array_search($route->name, self::ROUTES_WITH_RECORDS, true);
167f95e0480SGreg Roach        if ($action !== false) {
1682b0d92b4SGreg Roach            $xref = $route->attributes['xref'];
169ddeb3354SGreg Roach            assert(is_string($xref));
170ddeb3354SGreg Roach
171c1010edaSGreg Roach            $add_route = route('module', [
17226684e68SGreg Roach                'module' => $this->name(),
173f95e0480SGreg Roach                'action' => 'Add' . $action,
174c1010edaSGreg Roach                'xref'   => $xref,
175d72b284aSGreg Roach                'tree'    => $tree->name(),
176c1010edaSGreg Roach            ]);
1775a78cd34SGreg Roach
17825b2dde3SGreg Roach            $submenus[] = new Menu(I18N::translate('Add to the clippings cart'), $add_route, 'menu-clippings-add', ['rel' => 'nofollow']);
1798c2e8227SGreg Roach        }
180cbc1590aSGreg Roach
1815a78cd34SGreg Roach        if (!$this->isCartEmpty($tree)) {
182c1010edaSGreg Roach            $submenus[] = new Menu(I18N::translate('Empty the clippings cart'), route('module', [
18326684e68SGreg Roach                'module' => $this->name(),
184c1010edaSGreg Roach                'action' => 'Empty',
185d72b284aSGreg Roach                'tree'    => $tree->name(),
186c1010edaSGreg Roach            ]), 'menu-clippings-empty', ['rel' => 'nofollow']);
187f95e0480SGreg Roach
188c1010edaSGreg Roach            $submenus[] = new Menu(I18N::translate('Download'), route('module', [
18926684e68SGreg Roach                'module' => $this->name(),
190c1010edaSGreg Roach                'action' => 'DownloadForm',
191d72b284aSGreg Roach                'tree'    => $tree->name(),
192c1010edaSGreg Roach            ]), 'menu-clippings-download', ['rel' => 'nofollow']);
1935a78cd34SGreg Roach        }
1945a78cd34SGreg Roach
19549a243cbSGreg Roach        return new Menu($this->title(), '#', 'menu-clippings', ['rel' => 'nofollow'], $submenus);
1968c2e8227SGreg Roach    }
1978c2e8227SGreg Roach
19876692c8bSGreg Roach    /**
1996ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
20076692c8bSGreg Roach     *
2016ccdf4f0SGreg Roach     * @return ResponseInterface
20276692c8bSGreg Roach     */
203f95e0480SGreg Roach    public function postDownloadAction(ServerRequestInterface $request): ResponseInterface
204c1010edaSGreg Roach    {
20557ab2231SGreg Roach        $tree = $request->getAttribute('tree');
2064ea62551SGreg Roach        assert($tree instanceof Tree);
2074ea62551SGreg Roach
208a04bb9a2SGreg Roach        $data_filesystem = $request->getAttribute('filesystem.data');
209a04bb9a2SGreg Roach        assert($data_filesystem instanceof FilesystemInterface);
210a04bb9a2SGreg Roach
211b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
212b46c87bdSGreg Roach
213b46c87bdSGreg Roach        $privatize_export = $params['privatize_export'];
214*e2ed7c79SGreg Roach
215*e2ed7c79SGreg Roach        if ($privatize_export === 'none' && !Auth::isManager($tree)) {
216*e2ed7c79SGreg Roach            $privatize_export = 'member';
217*e2ed7c79SGreg Roach        }
218*e2ed7c79SGreg Roach
219*e2ed7c79SGreg Roach        if ($privatize_export === 'gedadmin' && !Auth::isManager($tree)) {
220*e2ed7c79SGreg Roach            $privatize_export = 'member';
221*e2ed7c79SGreg Roach        }
222*e2ed7c79SGreg Roach
223*e2ed7c79SGreg Roach        if ($privatize_export === 'user' && !Auth::isMember($tree)) {
224*e2ed7c79SGreg Roach            $privatize_export = 'visitor';
225*e2ed7c79SGreg Roach        }
226*e2ed7c79SGreg Roach
227b46c87bdSGreg Roach        $convert = (bool) ($params['convert'] ?? false);
2288c2e8227SGreg Roach
22913abd6f3SGreg Roach        $cart = Session::get('cart', []);
2308c2e8227SGreg Roach
231aa6f03bbSGreg Roach        $xrefs = array_keys($cart[$tree->name()] ?? []);
2325a78cd34SGreg Roach
2335a78cd34SGreg Roach        // Create a new/empty .ZIP file
234a00baf47SGreg Roach        $temp_zip_file  = stream_get_meta_data(tmpfile())['uri'];
2357f996f6eSGreg Roach        $zip_adapter    = new ZipArchiveAdapter($temp_zip_file);
2367f996f6eSGreg Roach        $zip_filesystem = new Filesystem($zip_adapter);
2375a78cd34SGreg Roach
23861bf91b2SGreg Roach        $manager = new MountManager([
239a04bb9a2SGreg Roach            'media' => $tree->mediaFilesystem($data_filesystem),
24061bf91b2SGreg Roach            'zip'   => $zip_filesystem,
24161bf91b2SGreg Roach        ]);
24261bf91b2SGreg Roach
2435a78cd34SGreg Roach        // Media file prefix
2445a78cd34SGreg Roach        $path = $tree->getPreference('MEDIA_DIRECTORY');
2455a78cd34SGreg Roach
2465a78cd34SGreg Roach        // GEDCOM file header
247a3d8780cSGreg Roach        $filetext = FunctionsExport::gedcomHeader($tree, $convert ? 'ANSI' : 'UTF-8');
2485a78cd34SGreg Roach
2495a78cd34SGreg Roach        switch ($privatize_export) {
2505a78cd34SGreg Roach            case 'gedadmin':
2515a78cd34SGreg Roach                $access_level = Auth::PRIV_NONE;
2525a78cd34SGreg Roach                break;
2535a78cd34SGreg Roach            case 'user':
2545a78cd34SGreg Roach                $access_level = Auth::PRIV_USER;
2555a78cd34SGreg Roach                break;
2565a78cd34SGreg Roach            case 'visitor':
2575a78cd34SGreg Roach                $access_level = Auth::PRIV_PRIVATE;
2585a78cd34SGreg Roach                break;
2595a78cd34SGreg Roach            case 'none':
2605a78cd34SGreg Roach            default:
2615a78cd34SGreg Roach                $access_level = Auth::PRIV_HIDE;
2625a78cd34SGreg Roach                break;
2635a78cd34SGreg Roach        }
2645a78cd34SGreg Roach
2655a78cd34SGreg Roach        foreach ($xrefs as $xref) {
2665a78cd34SGreg Roach            $object = GedcomRecord::getInstance($xref, $tree);
2675a78cd34SGreg Roach            // The object may have been deleted since we added it to the cart....
268bed27cedSGreg Roach            if ($object instanceof  GedcomRecord) {
2695a78cd34SGreg Roach                $record = $object->privatizeGedcom($access_level);
2705a78cd34SGreg Roach                // Remove links to objects that aren't in the cart
2718d0ebef0SGreg Roach                preg_match_all('/\n1 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[2-9].*)*/', $record, $matches, PREG_SET_ORDER);
2725a78cd34SGreg Roach                foreach ($matches as $match) {
273bf80ec58SGreg Roach                    if (!in_array($match[1], $xrefs, true)) {
2745a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2755a78cd34SGreg Roach                    }
2765a78cd34SGreg Roach                }
2778d0ebef0SGreg Roach                preg_match_all('/\n2 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[3-9].*)*/', $record, $matches, PREG_SET_ORDER);
2785a78cd34SGreg Roach                foreach ($matches as $match) {
279bf80ec58SGreg Roach                    if (!in_array($match[1], $xrefs, true)) {
2805a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2815a78cd34SGreg Roach                    }
2825a78cd34SGreg Roach                }
2838d0ebef0SGreg Roach                preg_match_all('/\n3 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[4-9].*)*/', $record, $matches, PREG_SET_ORDER);
2845a78cd34SGreg Roach                foreach ($matches as $match) {
285bf80ec58SGreg Roach                    if (!in_array($match[1], $xrefs, true)) {
2865a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2875a78cd34SGreg Roach                    }
2885a78cd34SGreg Roach                }
2895a78cd34SGreg Roach
29055167344SGreg Roach                if ($object instanceof Individual || $object instanceof Family) {
2915a78cd34SGreg Roach                    $filetext .= $record . "\n";
2925a78cd34SGreg Roach                    $filetext .= "1 SOUR @WEBTREES@\n";
2931f273236SGreg Roach                    $filetext .= '2 PAGE ' . $object->url() . "\n";
29455167344SGreg Roach                } elseif ($object instanceof Source) {
2955a78cd34SGreg Roach                    $filetext .= $record . "\n";
2961f273236SGreg Roach                    $filetext .= '1 NOTE ' . $object->url() . "\n";
29755167344SGreg Roach                } elseif ($object instanceof Media) {
29855167344SGreg Roach                    // Add the media files to the archive
2995a78cd34SGreg Roach                    foreach ($object->mediaFiles() as $media_file) {
30061bf91b2SGreg Roach                        $from = 'media://' . $media_file->filename();
30161bf91b2SGreg Roach                        $to   = 'zip://' . $path . $media_file->filename();
3025de4ab51SGreg Roach                        if (!$media_file->isExternal() && $manager->has($from) && !$manager->has($to)) {
30361bf91b2SGreg Roach                            $manager->copy($from, $to);
3045a78cd34SGreg Roach                        }
3055a78cd34SGreg Roach                    }
3065a78cd34SGreg Roach                    $filetext .= $record . "\n";
30755167344SGreg Roach                } else {
3085a78cd34SGreg Roach                    $filetext .= $record . "\n";
3098c2e8227SGreg Roach                }
3108c2e8227SGreg Roach            }
3118c2e8227SGreg Roach        }
3128c2e8227SGreg Roach
3139b93b7c3SGreg Roach        $base_url = $request->getAttribute('base_url');
3149b93b7c3SGreg Roach
3155a78cd34SGreg Roach        // Create a source, to indicate the source of the data.
3169b93b7c3SGreg Roach        $filetext .= "0 @WEBTREES@ SOUR\n1 TITL " . $base_url . "\n";
317e5a6b4d4SGreg Roach        $author   = $this->user_service->find((int) $tree->getPreference('CONTACT_USER_ID'));
3185a78cd34SGreg Roach        if ($author !== null) {
319e5a6b4d4SGreg Roach            $filetext .= '1 AUTH ' . $author->realName() . "\n";
3205a78cd34SGreg Roach        }
3215a78cd34SGreg Roach        $filetext .= "0 TRLR\n";
3225a78cd34SGreg Roach
3235a78cd34SGreg Roach        // Make sure the preferred line endings are used
324679203a4SGreg Roach        $filetext = strtr($filetext, ["\n" => Gedcom::EOL]);
3255a78cd34SGreg Roach
32655167344SGreg Roach        if ($convert) {
3275a78cd34SGreg Roach            $filetext = utf8_decode($filetext);
3288c2e8227SGreg Roach        }
329cbc1590aSGreg Roach
3305a78cd34SGreg Roach        // Finally add the GEDCOM file to the .ZIP file.
3315a78cd34SGreg Roach        $zip_filesystem->write('clippings.ged', $filetext);
3325a78cd34SGreg Roach
33361bf91b2SGreg Roach        // Need to force-close ZipArchive filesystems.
3347f996f6eSGreg Roach        $zip_adapter->getArchive()->close();
3355a78cd34SGreg Roach
3366ccdf4f0SGreg Roach        // Use a stream, so that we do not have to load the entire file into memory.
3376ccdf4f0SGreg Roach        $stream = app(StreamFactoryInterface::class)->createStreamFromFile($temp_zip_file);
3385a78cd34SGreg Roach
339bed27cedSGreg Roach        /** @var ResponseFactoryInterface $response_factory */
340bed27cedSGreg Roach        $response_factory = app(ResponseFactoryInterface::class);
341bed27cedSGreg Roach
342bed27cedSGreg Roach        return $response_factory->createResponse()
3436ccdf4f0SGreg Roach            ->withBody($stream)
3441b3d4731SGreg Roach            ->withHeader('Content-Type', 'application/zip')
345bed27cedSGreg Roach            ->withHeader('Content-Disposition', 'attachment; filename="clippings.zip');
3468c2e8227SGreg Roach    }
3478c2e8227SGreg Roach
3488c2e8227SGreg Roach    /**
34957ab2231SGreg Roach     * @param ServerRequestInterface $request
35076692c8bSGreg Roach     *
3516ccdf4f0SGreg Roach     * @return ResponseInterface
3528c2e8227SGreg Roach     */
35357ab2231SGreg Roach    public function getDownloadFormAction(ServerRequestInterface $request): ResponseInterface
354c1010edaSGreg Roach    {
35557ab2231SGreg Roach        $tree = $request->getAttribute('tree');
3564ea62551SGreg Roach        assert($tree instanceof Tree);
3574ea62551SGreg Roach
35857ab2231SGreg Roach        $user  = $request->getAttribute('user');
3595a78cd34SGreg Roach        $title = I18N::translate('Family tree clippings cart') . ' — ' . I18N::translate('Download');
3608c2e8227SGreg Roach
3615a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/download', [
3625a78cd34SGreg Roach            'is_manager' => Auth::isManager($tree, $user),
3635a78cd34SGreg Roach            'is_member'  => Auth::isMember($tree, $user),
36471378461SGreg Roach            'module'     => $this->name(),
3655a78cd34SGreg Roach            'title'      => $title,
366f95e0480SGreg Roach            'tree'       => $tree,
3675a78cd34SGreg Roach        ]);
3688c2e8227SGreg Roach    }
3698c2e8227SGreg Roach
3705a78cd34SGreg Roach    /**
37157ab2231SGreg Roach     * @param ServerRequestInterface $request
3725a78cd34SGreg Roach     *
3736ccdf4f0SGreg Roach     * @return ResponseInterface
3745a78cd34SGreg Roach     */
37557ab2231SGreg Roach    public function getEmptyAction(ServerRequestInterface $request): ResponseInterface
376c1010edaSGreg Roach    {
37757ab2231SGreg Roach        $tree = $request->getAttribute('tree');
3784ea62551SGreg Roach        assert($tree instanceof Tree);
3794ea62551SGreg Roach
3805a78cd34SGreg Roach        $cart                = Session::get('cart', []);
381aa6f03bbSGreg Roach        $cart[$tree->name()] = [];
3825a78cd34SGreg Roach        Session::put('cart', $cart);
3838c2e8227SGreg Roach
384c1010edaSGreg Roach        $url = route('module', [
38526684e68SGreg Roach            'module' => $this->name(),
386c1010edaSGreg Roach            'action' => 'Show',
387d72b284aSGreg Roach            'tree'    => $tree->name(),
388c1010edaSGreg Roach        ]);
3895a78cd34SGreg Roach
3906ccdf4f0SGreg Roach        return redirect($url);
3915a78cd34SGreg Roach    }
3925a78cd34SGreg Roach
3935a78cd34SGreg Roach    /**
3946ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
3955a78cd34SGreg Roach     *
3966ccdf4f0SGreg Roach     * @return ResponseInterface
3975a78cd34SGreg Roach     */
39857ab2231SGreg Roach    public function postRemoveAction(ServerRequestInterface $request): ResponseInterface
399c1010edaSGreg Roach    {
40057ab2231SGreg Roach        $tree = $request->getAttribute('tree');
40175964c75SGreg Roach        assert($tree instanceof Tree);
4025229eadeSGreg Roach
403bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
4045a78cd34SGreg Roach
4055a78cd34SGreg Roach        $cart = Session::get('cart', []);
406aa6f03bbSGreg Roach        unset($cart[$tree->name()][$xref]);
4075a78cd34SGreg Roach        Session::put('cart', $cart);
4085a78cd34SGreg Roach
409c1010edaSGreg Roach        $url = route('module', [
41026684e68SGreg Roach            'module' => $this->name(),
411c1010edaSGreg Roach            'action' => 'Show',
412d72b284aSGreg Roach            'tree'    => $tree->name(),
413c1010edaSGreg Roach        ]);
4145a78cd34SGreg Roach
4156ccdf4f0SGreg Roach        return redirect($url);
4165a78cd34SGreg Roach    }
4175a78cd34SGreg Roach
4185a78cd34SGreg Roach    /**
41957ab2231SGreg Roach     * @param ServerRequestInterface $request
4205a78cd34SGreg Roach     *
4216ccdf4f0SGreg Roach     * @return ResponseInterface
4225a78cd34SGreg Roach     */
42357ab2231SGreg Roach    public function getShowAction(ServerRequestInterface $request): ResponseInterface
424c1010edaSGreg Roach    {
42557ab2231SGreg Roach        $tree = $request->getAttribute('tree');
42675964c75SGreg Roach        assert($tree instanceof Tree);
42757ab2231SGreg Roach
4285a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/show', [
4295a78cd34SGreg Roach            'records' => $this->allRecordsInCart($tree),
4305a78cd34SGreg Roach            'title'   => I18N::translate('Family tree clippings cart'),
4315a78cd34SGreg Roach            'tree'    => $tree,
4325a78cd34SGreg Roach        ]);
4335a78cd34SGreg Roach    }
4345a78cd34SGreg Roach
4355a78cd34SGreg Roach    /**
4366ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
4375a78cd34SGreg Roach     *
4386ccdf4f0SGreg Roach     * @return ResponseInterface
4395a78cd34SGreg Roach     */
44057ab2231SGreg Roach    public function getAddFamilyAction(ServerRequestInterface $request): ResponseInterface
441c1010edaSGreg Roach    {
44257ab2231SGreg Roach        $tree = $request->getAttribute('tree');
44375964c75SGreg Roach        assert($tree instanceof Tree);
4445229eadeSGreg Roach
445bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
4465a78cd34SGreg Roach
4475a78cd34SGreg Roach        $family = Family::getInstance($xref, $tree);
4485a78cd34SGreg Roach
4495a78cd34SGreg Roach        if ($family === null) {
45059f2f229SGreg Roach            throw new FamilyNotFoundException();
4515a78cd34SGreg Roach        }
4525a78cd34SGreg Roach
4535a78cd34SGreg Roach        $options = $this->familyOptions($family);
4545a78cd34SGreg Roach
45539ca88baSGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $family->fullName());
4565a78cd34SGreg Roach
4575a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
4585a78cd34SGreg Roach            'options' => $options,
4595a78cd34SGreg Roach            'default' => key($options),
4605a78cd34SGreg Roach            'record'  => $family,
4615a78cd34SGreg Roach            'title'   => $title,
4625a78cd34SGreg Roach            'tree'    => $tree,
4635a78cd34SGreg Roach        ]);
4645a78cd34SGreg Roach    }
4655a78cd34SGreg Roach
4665a78cd34SGreg Roach    /**
4675a78cd34SGreg Roach     * @param Family $family
4685a78cd34SGreg Roach     *
4695a78cd34SGreg Roach     * @return string[]
4705a78cd34SGreg Roach     */
471c1010edaSGreg Roach    private function familyOptions(Family $family): array
472c1010edaSGreg Roach    {
47339ca88baSGreg Roach        $name = strip_tags($family->fullName());
4745a78cd34SGreg Roach
4755a78cd34SGreg Roach        return [
4765a78cd34SGreg Roach            'parents'     => $name,
477bbb76c12SGreg Roach            /* I18N: %s is a family (husband + wife) */
478bbb76c12SGreg Roach            'members'     => I18N::translate('%s and their children', $name),
479bbb76c12SGreg Roach            /* I18N: %s is a family (husband + wife) */
480bbb76c12SGreg Roach            'descendants' => I18N::translate('%s and their descendants', $name),
4815a78cd34SGreg Roach        ];
4825a78cd34SGreg Roach    }
4835a78cd34SGreg Roach
4845a78cd34SGreg Roach    /**
4856ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
4865a78cd34SGreg Roach     *
4876ccdf4f0SGreg Roach     * @return ResponseInterface
4885a78cd34SGreg Roach     */
48957ab2231SGreg Roach    public function postAddFamilyAction(ServerRequestInterface $request): ResponseInterface
490c1010edaSGreg Roach    {
49157ab2231SGreg Roach        $tree = $request->getAttribute('tree');
4924ea62551SGreg Roach        assert($tree instanceof Tree);
4934ea62551SGreg Roach
494b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
495b46c87bdSGreg Roach
496b46c87bdSGreg Roach        $xref   = $params['xref'];
497b46c87bdSGreg Roach        $option = $params['option'];
4985a78cd34SGreg Roach
4995a78cd34SGreg Roach        $family = Family::getInstance($xref, $tree);
5005a78cd34SGreg Roach
5015a78cd34SGreg Roach        if ($family === null) {
50259f2f229SGreg Roach            throw new FamilyNotFoundException();
5035a78cd34SGreg Roach        }
5045a78cd34SGreg Roach
5055a78cd34SGreg Roach        switch ($option) {
5065a78cd34SGreg Roach            case 'parents':
5075a78cd34SGreg Roach                $this->addFamilyToCart($family);
5085a78cd34SGreg Roach                break;
5095a78cd34SGreg Roach
5105a78cd34SGreg Roach            case 'members':
5115a78cd34SGreg Roach                $this->addFamilyAndChildrenToCart($family);
5125a78cd34SGreg Roach                break;
5135a78cd34SGreg Roach
5145a78cd34SGreg Roach            case 'descendants':
5155a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($family);
5165a78cd34SGreg Roach                break;
5175a78cd34SGreg Roach        }
5185a78cd34SGreg Roach
5196ccdf4f0SGreg Roach        return redirect($family->url());
5205a78cd34SGreg Roach    }
5215a78cd34SGreg Roach
5225a78cd34SGreg Roach    /**
5235a78cd34SGreg Roach     * @param Family $family
52418d7a90dSGreg Roach     *
52518d7a90dSGreg Roach     * @return void
5265a78cd34SGreg Roach     */
527e364afe4SGreg Roach    private function addFamilyToCart(Family $family): void
528c1010edaSGreg Roach    {
5295a78cd34SGreg Roach        $this->addRecordToCart($family);
5305a78cd34SGreg Roach
53139ca88baSGreg Roach        foreach ($family->spouses() as $spouse) {
5325a78cd34SGreg Roach            $this->addRecordToCart($spouse);
5335a78cd34SGreg Roach        }
5345a78cd34SGreg Roach    }
5355a78cd34SGreg Roach
5365a78cd34SGreg Roach    /**
5375a78cd34SGreg Roach     * @param Family $family
53818d7a90dSGreg Roach     *
53918d7a90dSGreg Roach     * @return void
5405a78cd34SGreg Roach     */
541e364afe4SGreg Roach    private function addFamilyAndChildrenToCart(Family $family): void
542c1010edaSGreg Roach    {
5435a78cd34SGreg Roach        $this->addRecordToCart($family);
5445a78cd34SGreg Roach
54539ca88baSGreg Roach        foreach ($family->spouses() as $spouse) {
5465a78cd34SGreg Roach            $this->addRecordToCart($spouse);
5475a78cd34SGreg Roach        }
54839ca88baSGreg Roach        foreach ($family->children() as $child) {
5495a78cd34SGreg Roach            $this->addRecordToCart($child);
5505a78cd34SGreg Roach        }
5515a78cd34SGreg Roach    }
5525a78cd34SGreg Roach
5535a78cd34SGreg Roach    /**
5545a78cd34SGreg Roach     * @param Family $family
55518d7a90dSGreg Roach     *
55618d7a90dSGreg Roach     * @return void
5575a78cd34SGreg Roach     */
558e364afe4SGreg Roach    private function addFamilyAndDescendantsToCart(Family $family): void
559c1010edaSGreg Roach    {
5605a78cd34SGreg Roach        $this->addRecordToCart($family);
5615a78cd34SGreg Roach
56239ca88baSGreg Roach        foreach ($family->spouses() as $spouse) {
5635a78cd34SGreg Roach            $this->addRecordToCart($spouse);
5645a78cd34SGreg Roach        }
56539ca88baSGreg Roach        foreach ($family->children() as $child) {
5665a78cd34SGreg Roach            $this->addRecordToCart($child);
56739ca88baSGreg Roach            foreach ($child->spouseFamilies() as $child_family) {
5685a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($child_family);
5695a78cd34SGreg Roach            }
5705a78cd34SGreg Roach        }
5715a78cd34SGreg Roach    }
5725a78cd34SGreg Roach
5735a78cd34SGreg Roach    /**
5746ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
5755a78cd34SGreg Roach     *
5766ccdf4f0SGreg Roach     * @return ResponseInterface
5775a78cd34SGreg Roach     */
57857ab2231SGreg Roach    public function getAddIndividualAction(ServerRequestInterface $request): ResponseInterface
579c1010edaSGreg Roach    {
58057ab2231SGreg Roach        $tree = $request->getAttribute('tree');
58175964c75SGreg Roach        assert($tree instanceof Tree);
5825229eadeSGreg Roach
583bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
5845a78cd34SGreg Roach
5855a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
5865a78cd34SGreg Roach
5875a78cd34SGreg Roach        if ($individual === null) {
58859f2f229SGreg Roach            throw new IndividualNotFoundException();
5895a78cd34SGreg Roach        }
5905a78cd34SGreg Roach
5915a78cd34SGreg Roach        $options = $this->individualOptions($individual);
5925a78cd34SGreg Roach
59339ca88baSGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $individual->fullName());
5945a78cd34SGreg Roach
5955a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
5965a78cd34SGreg Roach            'options' => $options,
5975a78cd34SGreg Roach            'default' => key($options),
5985a78cd34SGreg Roach            'record'  => $individual,
5995a78cd34SGreg Roach            'title'   => $title,
6005a78cd34SGreg Roach            'tree'    => $tree,
6015a78cd34SGreg Roach        ]);
6025a78cd34SGreg Roach    }
6035a78cd34SGreg Roach
6045a78cd34SGreg Roach    /**
6055a78cd34SGreg Roach     * @param Individual $individual
6065a78cd34SGreg Roach     *
6075a78cd34SGreg Roach     * @return string[]
6085a78cd34SGreg Roach     */
609c1010edaSGreg Roach    private function individualOptions(Individual $individual): array
610c1010edaSGreg Roach    {
61139ca88baSGreg Roach        $name = strip_tags($individual->fullName());
6125a78cd34SGreg Roach
61339ca88baSGreg Roach        if ($individual->sex() === 'F') {
6145a78cd34SGreg Roach            return [
6155a78cd34SGreg Roach                'self'              => $name,
6165a78cd34SGreg Roach                'parents'           => I18N::translate('%s, her parents and siblings', $name),
6175a78cd34SGreg Roach                'spouses'           => I18N::translate('%s, her spouses and children', $name),
6185a78cd34SGreg Roach                'ancestors'         => I18N::translate('%s and her ancestors', $name),
6195a78cd34SGreg Roach                'ancestor_families' => I18N::translate('%s, her ancestors and their families', $name),
6205a78cd34SGreg Roach                'descendants'       => I18N::translate('%s, her spouses and descendants', $name),
6215a78cd34SGreg Roach            ];
622b2ce94c6SRico Sonntag        }
623b2ce94c6SRico Sonntag
6245a78cd34SGreg Roach        return [
6255a78cd34SGreg Roach            'self'              => $name,
6265a78cd34SGreg Roach            'parents'           => I18N::translate('%s, his parents and siblings', $name),
6275a78cd34SGreg Roach            'spouses'           => I18N::translate('%s, his spouses and children', $name),
6285a78cd34SGreg Roach            'ancestors'         => I18N::translate('%s and his ancestors', $name),
6295a78cd34SGreg Roach            'ancestor_families' => I18N::translate('%s, his ancestors and their families', $name),
6305a78cd34SGreg Roach            'descendants'       => I18N::translate('%s, his spouses and descendants', $name),
6315a78cd34SGreg Roach        ];
6325a78cd34SGreg Roach    }
6335a78cd34SGreg Roach
6345a78cd34SGreg Roach    /**
6356ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
6365a78cd34SGreg Roach     *
6376ccdf4f0SGreg Roach     * @return ResponseInterface
6385a78cd34SGreg Roach     */
63957ab2231SGreg Roach    public function postAddIndividualAction(ServerRequestInterface $request): ResponseInterface
640c1010edaSGreg Roach    {
64157ab2231SGreg Roach        $tree = $request->getAttribute('tree');
6424ea62551SGreg Roach        assert($tree instanceof Tree);
6434ea62551SGreg Roach
644b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
645b46c87bdSGreg Roach
646b46c87bdSGreg Roach        $xref   = $params['xref'];
647b46c87bdSGreg Roach        $option = $params['option'];
6485a78cd34SGreg Roach
6495a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
6505a78cd34SGreg Roach
6515a78cd34SGreg Roach        if ($individual === null) {
65259f2f229SGreg Roach            throw new IndividualNotFoundException();
6535a78cd34SGreg Roach        }
6545a78cd34SGreg Roach
6555a78cd34SGreg Roach        switch ($option) {
6565a78cd34SGreg Roach            case 'self':
6575a78cd34SGreg Roach                $this->addRecordToCart($individual);
6585a78cd34SGreg Roach                break;
6595a78cd34SGreg Roach
6605a78cd34SGreg Roach            case 'parents':
66139ca88baSGreg Roach                foreach ($individual->childFamilies() as $family) {
6625a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
6635a78cd34SGreg Roach                }
6645a78cd34SGreg Roach                break;
6655a78cd34SGreg Roach
6665a78cd34SGreg Roach            case 'spouses':
66739ca88baSGreg Roach                foreach ($individual->spouseFamilies() as $family) {
6685a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
6695a78cd34SGreg Roach                }
6705a78cd34SGreg Roach                break;
6715a78cd34SGreg Roach
6725a78cd34SGreg Roach            case 'ancestors':
6735a78cd34SGreg Roach                $this->addAncestorsToCart($individual);
6745a78cd34SGreg Roach                break;
6755a78cd34SGreg Roach
6765a78cd34SGreg Roach            case 'ancestor_families':
6775a78cd34SGreg Roach                $this->addAncestorFamiliesToCart($individual);
6785a78cd34SGreg Roach                break;
6795a78cd34SGreg Roach
6805a78cd34SGreg Roach            case 'descendants':
68139ca88baSGreg Roach                foreach ($individual->spouseFamilies() as $family) {
6825a78cd34SGreg Roach                    $this->addFamilyAndDescendantsToCart($family);
6835a78cd34SGreg Roach                }
6845a78cd34SGreg Roach                break;
6855a78cd34SGreg Roach        }
6865a78cd34SGreg Roach
6876ccdf4f0SGreg Roach        return redirect($individual->url());
6885a78cd34SGreg Roach    }
6895a78cd34SGreg Roach
6905a78cd34SGreg Roach    /**
6915a78cd34SGreg Roach     * @param Individual $individual
69218d7a90dSGreg Roach     *
69318d7a90dSGreg Roach     * @return void
6945a78cd34SGreg Roach     */
695e364afe4SGreg Roach    private function addAncestorsToCart(Individual $individual): void
696c1010edaSGreg Roach    {
6975a78cd34SGreg Roach        $this->addRecordToCart($individual);
6985a78cd34SGreg Roach
69939ca88baSGreg Roach        foreach ($individual->childFamilies() as $family) {
70039ca88baSGreg Roach            foreach ($family->spouses() as $parent) {
7015a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
7025a78cd34SGreg Roach            }
7035a78cd34SGreg Roach        }
7045a78cd34SGreg Roach    }
7055a78cd34SGreg Roach
7065a78cd34SGreg Roach    /**
7075a78cd34SGreg Roach     * @param Individual $individual
70818d7a90dSGreg Roach     *
70918d7a90dSGreg Roach     * @return void
7105a78cd34SGreg Roach     */
711e364afe4SGreg Roach    private function addAncestorFamiliesToCart(Individual $individual): void
712c1010edaSGreg Roach    {
71339ca88baSGreg Roach        foreach ($individual->childFamilies() as $family) {
7145a78cd34SGreg Roach            $this->addFamilyAndChildrenToCart($family);
71539ca88baSGreg Roach            foreach ($family->spouses() as $parent) {
716cad6d3f3SGreg Roach                $this->addAncestorFamiliesToCart($parent);
7175a78cd34SGreg Roach            }
7185a78cd34SGreg Roach        }
7195a78cd34SGreg Roach    }
7205a78cd34SGreg Roach
7215a78cd34SGreg Roach    /**
7226ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
7235a78cd34SGreg Roach     *
7246ccdf4f0SGreg Roach     * @return ResponseInterface
7255a78cd34SGreg Roach     */
72657ab2231SGreg Roach    public function getAddMediaAction(ServerRequestInterface $request): ResponseInterface
727c1010edaSGreg Roach    {
72857ab2231SGreg Roach        $tree = $request->getAttribute('tree');
72975964c75SGreg Roach        assert($tree instanceof Tree);
7305229eadeSGreg Roach
731bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
7325a78cd34SGreg Roach
7335a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
7345a78cd34SGreg Roach
7355a78cd34SGreg Roach        if ($media === null) {
73659f2f229SGreg Roach            throw new MediaNotFoundException();
7375a78cd34SGreg Roach        }
7385a78cd34SGreg Roach
7395a78cd34SGreg Roach        $options = $this->mediaOptions($media);
7405a78cd34SGreg Roach
74139ca88baSGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $media->fullName());
7425a78cd34SGreg Roach
7435a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
7445a78cd34SGreg Roach            'options' => $options,
7455a78cd34SGreg Roach            'default' => key($options),
7465a78cd34SGreg Roach            'record'  => $media,
7475a78cd34SGreg Roach            'title'   => $title,
7485a78cd34SGreg Roach            'tree'    => $tree,
7495a78cd34SGreg Roach        ]);
7505a78cd34SGreg Roach    }
7515a78cd34SGreg Roach
7525a78cd34SGreg Roach    /**
7535a78cd34SGreg Roach     * @param Media $media
7545a78cd34SGreg Roach     *
7555a78cd34SGreg Roach     * @return string[]
7565a78cd34SGreg Roach     */
757c1010edaSGreg Roach    private function mediaOptions(Media $media): array
758c1010edaSGreg Roach    {
75939ca88baSGreg Roach        $name = strip_tags($media->fullName());
7605a78cd34SGreg Roach
7615a78cd34SGreg Roach        return [
7625a78cd34SGreg Roach            'self' => $name,
7635a78cd34SGreg Roach        ];
7645a78cd34SGreg Roach    }
7655a78cd34SGreg Roach
7665a78cd34SGreg Roach    /**
7676ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
7685a78cd34SGreg Roach     *
7696ccdf4f0SGreg Roach     * @return ResponseInterface
7705a78cd34SGreg Roach     */
77157ab2231SGreg Roach    public function postAddMediaAction(ServerRequestInterface $request): ResponseInterface
772c1010edaSGreg Roach    {
77357ab2231SGreg Roach        $tree = $request->getAttribute('tree');
77475964c75SGreg Roach        assert($tree instanceof Tree);
7755229eadeSGreg Roach
776bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
7775a78cd34SGreg Roach
7785a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
7795a78cd34SGreg Roach
7805a78cd34SGreg Roach        if ($media === null) {
78159f2f229SGreg Roach            throw new MediaNotFoundException();
7825a78cd34SGreg Roach        }
7835a78cd34SGreg Roach
7845a78cd34SGreg Roach        $this->addRecordToCart($media);
7855a78cd34SGreg Roach
7866ccdf4f0SGreg Roach        return redirect($media->url());
7875a78cd34SGreg Roach    }
7885a78cd34SGreg Roach
7895a78cd34SGreg Roach    /**
7906ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
7915a78cd34SGreg Roach     *
7926ccdf4f0SGreg Roach     * @return ResponseInterface
7935a78cd34SGreg Roach     */
79457ab2231SGreg Roach    public function getAddNoteAction(ServerRequestInterface $request): ResponseInterface
795c1010edaSGreg Roach    {
79657ab2231SGreg Roach        $tree = $request->getAttribute('tree');
79775964c75SGreg Roach        assert($tree instanceof Tree);
7985229eadeSGreg Roach
799bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
8005a78cd34SGreg Roach
8015a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
8025a78cd34SGreg Roach
8035a78cd34SGreg Roach        if ($note === null) {
80459f2f229SGreg Roach            throw new NoteNotFoundException();
8055a78cd34SGreg Roach        }
8065a78cd34SGreg Roach
8075a78cd34SGreg Roach        $options = $this->noteOptions($note);
8085a78cd34SGreg Roach
80939ca88baSGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $note->fullName());
8105a78cd34SGreg Roach
8115a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
8125a78cd34SGreg Roach            'options' => $options,
8135a78cd34SGreg Roach            'default' => key($options),
8145a78cd34SGreg Roach            'record'  => $note,
8155a78cd34SGreg Roach            'title'   => $title,
8165a78cd34SGreg Roach            'tree'    => $tree,
8175a78cd34SGreg Roach        ]);
8185a78cd34SGreg Roach    }
8195a78cd34SGreg Roach
8205a78cd34SGreg Roach    /**
8215a78cd34SGreg Roach     * @param Note $note
8225a78cd34SGreg Roach     *
8235a78cd34SGreg Roach     * @return string[]
8245a78cd34SGreg Roach     */
825c1010edaSGreg Roach    private function noteOptions(Note $note): array
826c1010edaSGreg Roach    {
82739ca88baSGreg Roach        $name = strip_tags($note->fullName());
8285a78cd34SGreg Roach
8295a78cd34SGreg Roach        return [
8305a78cd34SGreg Roach            'self' => $name,
8315a78cd34SGreg Roach        ];
8325a78cd34SGreg Roach    }
8335a78cd34SGreg Roach
8345a78cd34SGreg Roach    /**
8356ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
8365a78cd34SGreg Roach     *
8376ccdf4f0SGreg Roach     * @return ResponseInterface
8385a78cd34SGreg Roach     */
83957ab2231SGreg Roach    public function postAddNoteAction(ServerRequestInterface $request): ResponseInterface
840c1010edaSGreg Roach    {
84157ab2231SGreg Roach        $tree = $request->getAttribute('tree');
84275964c75SGreg Roach        assert($tree instanceof Tree);
8435229eadeSGreg Roach
844bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
8455a78cd34SGreg Roach
8465a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
8475a78cd34SGreg Roach
8485a78cd34SGreg Roach        if ($note === null) {
84959f2f229SGreg Roach            throw new NoteNotFoundException();
8505a78cd34SGreg Roach        }
8515a78cd34SGreg Roach
8525a78cd34SGreg Roach        $this->addRecordToCart($note);
8535a78cd34SGreg Roach
8546ccdf4f0SGreg Roach        return redirect($note->url());
8555a78cd34SGreg Roach    }
8565a78cd34SGreg Roach
8575a78cd34SGreg Roach    /**
8586ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
8595a78cd34SGreg Roach     *
8606ccdf4f0SGreg Roach     * @return ResponseInterface
8615a78cd34SGreg Roach     */
86257ab2231SGreg Roach    public function getAddRepositoryAction(ServerRequestInterface $request): ResponseInterface
863c1010edaSGreg Roach    {
86457ab2231SGreg Roach        $tree = $request->getAttribute('tree');
86575964c75SGreg Roach        assert($tree instanceof Tree);
8665229eadeSGreg Roach
867bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
8685a78cd34SGreg Roach
8695a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
8705a78cd34SGreg Roach
8715a78cd34SGreg Roach        if ($repository === null) {
87259f2f229SGreg Roach            throw new RepositoryNotFoundException();
8735a78cd34SGreg Roach        }
8745a78cd34SGreg Roach
8755a78cd34SGreg Roach        $options = $this->repositoryOptions($repository);
8765a78cd34SGreg Roach
87739ca88baSGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $repository->fullName());
8785a78cd34SGreg Roach
8795a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
8805a78cd34SGreg Roach            'options' => $options,
8815a78cd34SGreg Roach            'default' => key($options),
8825a78cd34SGreg Roach            'record'  => $repository,
8835a78cd34SGreg Roach            'title'   => $title,
8845a78cd34SGreg Roach            'tree'    => $tree,
8855a78cd34SGreg Roach        ]);
8865a78cd34SGreg Roach    }
8875a78cd34SGreg Roach
8885a78cd34SGreg Roach    /**
8895a78cd34SGreg Roach     * @param Repository $repository
8905a78cd34SGreg Roach     *
8915a78cd34SGreg Roach     * @return string[]
8925a78cd34SGreg Roach     */
893c1010edaSGreg Roach    private function repositoryOptions(Repository $repository): array
894c1010edaSGreg Roach    {
89539ca88baSGreg Roach        $name = strip_tags($repository->fullName());
8965a78cd34SGreg Roach
8975a78cd34SGreg Roach        return [
8985a78cd34SGreg Roach            'self' => $name,
8995a78cd34SGreg Roach        ];
9005a78cd34SGreg Roach    }
9015a78cd34SGreg Roach
9025a78cd34SGreg Roach    /**
9036ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
9045a78cd34SGreg Roach     *
9056ccdf4f0SGreg Roach     * @return ResponseInterface
9065a78cd34SGreg Roach     */
90757ab2231SGreg Roach    public function postAddRepositoryAction(ServerRequestInterface $request): ResponseInterface
908c1010edaSGreg Roach    {
90957ab2231SGreg Roach        $tree = $request->getAttribute('tree');
91075964c75SGreg Roach        assert($tree instanceof Tree);
9115229eadeSGreg Roach
912bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
9135a78cd34SGreg Roach
9145a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
9155a78cd34SGreg Roach
9165a78cd34SGreg Roach        if ($repository === null) {
91759f2f229SGreg Roach            throw new RepositoryNotFoundException();
9185a78cd34SGreg Roach        }
9195a78cd34SGreg Roach
9205a78cd34SGreg Roach        $this->addRecordToCart($repository);
9215a78cd34SGreg Roach
9226ccdf4f0SGreg Roach        return redirect($repository->url());
9235a78cd34SGreg Roach    }
9245a78cd34SGreg Roach
9255a78cd34SGreg Roach    /**
9266ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
9275a78cd34SGreg Roach     *
9286ccdf4f0SGreg Roach     * @return ResponseInterface
9295a78cd34SGreg Roach     */
93057ab2231SGreg Roach    public function getAddSourceAction(ServerRequestInterface $request): ResponseInterface
931c1010edaSGreg Roach    {
93257ab2231SGreg Roach        $tree = $request->getAttribute('tree');
93375964c75SGreg Roach        assert($tree instanceof Tree);
9345229eadeSGreg Roach
935bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
9365a78cd34SGreg Roach
9375a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
9385a78cd34SGreg Roach
9395a78cd34SGreg Roach        if ($source === null) {
94059f2f229SGreg Roach            throw new SourceNotFoundException();
9415a78cd34SGreg Roach        }
9425a78cd34SGreg Roach
9435a78cd34SGreg Roach        $options = $this->sourceOptions($source);
9445a78cd34SGreg Roach
94539ca88baSGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $source->fullName());
9465a78cd34SGreg Roach
9475a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
9485a78cd34SGreg Roach            'options' => $options,
9495a78cd34SGreg Roach            'default' => key($options),
9505a78cd34SGreg Roach            'record'  => $source,
9515a78cd34SGreg Roach            'title'   => $title,
9525a78cd34SGreg Roach            'tree'    => $tree,
9535a78cd34SGreg Roach        ]);
9545a78cd34SGreg Roach    }
9555a78cd34SGreg Roach
9565a78cd34SGreg Roach    /**
9575a78cd34SGreg Roach     * @param Source $source
9585a78cd34SGreg Roach     *
9595a78cd34SGreg Roach     * @return string[]
9605a78cd34SGreg Roach     */
961c1010edaSGreg Roach    private function sourceOptions(Source $source): array
962c1010edaSGreg Roach    {
96339ca88baSGreg Roach        $name = strip_tags($source->fullName());
9645a78cd34SGreg Roach
9655a78cd34SGreg Roach        return [
96639ca88baSGreg Roach            'only'   => strip_tags($source->fullName()),
9675a78cd34SGreg Roach            'linked' => I18N::translate('%s and the individuals that reference it.', $name),
9685a78cd34SGreg Roach        ];
9695a78cd34SGreg Roach    }
9705a78cd34SGreg Roach
9715a78cd34SGreg Roach    /**
9726ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
9735a78cd34SGreg Roach     *
9746ccdf4f0SGreg Roach     * @return ResponseInterface
9755a78cd34SGreg Roach     */
97657ab2231SGreg Roach    public function postAddSourceAction(ServerRequestInterface $request): ResponseInterface
977c1010edaSGreg Roach    {
97857ab2231SGreg Roach        $tree = $request->getAttribute('tree');
97975964c75SGreg Roach        assert($tree instanceof Tree);
9805229eadeSGreg Roach
981b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
982b46c87bdSGreg Roach
983b46c87bdSGreg Roach        $xref   = $params['xref'];
984b46c87bdSGreg Roach        $option = $params['option'];
9855a78cd34SGreg Roach
9865a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
9875a78cd34SGreg Roach
9885a78cd34SGreg Roach        if ($source === null) {
98959f2f229SGreg Roach            throw new SourceNotFoundException();
9905a78cd34SGreg Roach        }
9915a78cd34SGreg Roach
9925a78cd34SGreg Roach        $this->addRecordToCart($source);
9935a78cd34SGreg Roach
9945a78cd34SGreg Roach        if ($option === 'linked') {
9955a78cd34SGreg Roach            foreach ($source->linkedIndividuals('SOUR') as $individual) {
9965a78cd34SGreg Roach                $this->addRecordToCart($individual);
9975a78cd34SGreg Roach            }
9985a78cd34SGreg Roach            foreach ($source->linkedFamilies('SOUR') as $family) {
9995a78cd34SGreg Roach                $this->addRecordToCart($family);
10005a78cd34SGreg Roach            }
10015a78cd34SGreg Roach        }
10025a78cd34SGreg Roach
10036ccdf4f0SGreg Roach        return redirect($source->url());
10045a78cd34SGreg Roach    }
10055a78cd34SGreg Roach
10065a78cd34SGreg Roach    /**
10075a78cd34SGreg Roach     * Get all the records in the cart.
10085a78cd34SGreg Roach     *
10095a78cd34SGreg Roach     * @param Tree $tree
10105a78cd34SGreg Roach     *
10115a78cd34SGreg Roach     * @return GedcomRecord[]
10125a78cd34SGreg Roach     */
1013c1010edaSGreg Roach    private function allRecordsInCart(Tree $tree): array
1014c1010edaSGreg Roach    {
10155a78cd34SGreg Roach        $cart = Session::get('cart', []);
10165a78cd34SGreg Roach
1017aa6f03bbSGreg Roach        $xrefs = array_keys($cart[$tree->name()] ?? []);
10185a78cd34SGreg Roach
10195a78cd34SGreg Roach        // Fetch all the records in the cart.
1020bed27cedSGreg Roach        $records = array_map(static function (string $xref) use ($tree): ?GedcomRecord {
10215a78cd34SGreg Roach            return GedcomRecord::getInstance($xref, $tree);
10225a78cd34SGreg Roach        }, $xrefs);
10235a78cd34SGreg Roach
10245a78cd34SGreg Roach        // Some records may have been deleted after they were added to the cart.
10255a78cd34SGreg Roach        $records = array_filter($records);
10265a78cd34SGreg Roach
10275a78cd34SGreg Roach        // Group and sort.
10280b5fd0a6SGreg Roach        uasort($records, static function (GedcomRecord $x, GedcomRecord $y): int {
1029c156e8f5SGreg Roach            return $x::RECORD_TYPE <=> $y::RECORD_TYPE ?: GedcomRecord::nameComparator()($x, $y);
10305a78cd34SGreg Roach        });
10315a78cd34SGreg Roach
10325a78cd34SGreg Roach        return $records;
10335a78cd34SGreg Roach    }
10345a78cd34SGreg Roach
10355a78cd34SGreg Roach    /**
10365a78cd34SGreg Roach     * Add a record (and direclty linked sources, notes, etc. to the cart.
10375a78cd34SGreg Roach     *
10385a78cd34SGreg Roach     * @param GedcomRecord $record
103918d7a90dSGreg Roach     *
104018d7a90dSGreg Roach     * @return void
10415a78cd34SGreg Roach     */
1042e364afe4SGreg Roach    private function addRecordToCart(GedcomRecord $record): void
1043c1010edaSGreg Roach    {
10445a78cd34SGreg Roach        $cart = Session::get('cart', []);
10455a78cd34SGreg Roach
1046f4afa648SGreg Roach        $tree_name = $record->tree()->name();
10475a78cd34SGreg Roach
10485a78cd34SGreg Roach        // Add this record
1049c0935879SGreg Roach        $cart[$tree_name][$record->xref()] = true;
10505a78cd34SGreg Roach
10515a78cd34SGreg Roach        // Add directly linked media, notes, repositories and sources.
10528d0ebef0SGreg Roach        preg_match_all('/\n\d (?:OBJE|NOTE|SOUR|REPO) @(' . Gedcom::REGEX_XREF . ')@/', $record->gedcom(), $matches);
10535a78cd34SGreg Roach
10545a78cd34SGreg Roach        foreach ($matches[1] as $match) {
10555a78cd34SGreg Roach            $cart[$tree_name][$match] = true;
10565a78cd34SGreg Roach        }
10575a78cd34SGreg Roach
10585a78cd34SGreg Roach        Session::put('cart', $cart);
10595a78cd34SGreg Roach    }
10605a78cd34SGreg Roach
10615a78cd34SGreg Roach    /**
10625a78cd34SGreg Roach     * @param Tree $tree
10635a78cd34SGreg Roach     *
10645a78cd34SGreg Roach     * @return bool
10655a78cd34SGreg Roach     */
1066c1010edaSGreg Roach    private function isCartEmpty(Tree $tree): bool
1067c1010edaSGreg Roach    {
10685a78cd34SGreg Roach        $cart     = Session::get('cart', []);
1069a91af26aSGreg Roach        $contents = $cart[$tree->name()] ?? [];
10705a78cd34SGreg Roach
1071a91af26aSGreg Roach        return $contents === [];
10725a78cd34SGreg Roach    }
10738c2e8227SGreg Roach}
1074