xref: /webtrees/app/Module/ClippingsCartModule.php (revision d72b284a0846ca045e548a1c77ad11813bcbab92)
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 */
17e7f56f2aSGreg Roachdeclare(strict_types=1);
18e7f56f2aSGreg Roach
1976692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2076692c8bSGreg Roach
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
220bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\FamilyNotFoundException;
230bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualNotFoundException;
240bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\MediaNotFoundException;
250bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\NoteNotFoundException;
260bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\RepositoryNotFoundException;
270bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\SourceNotFoundException;
280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Family;
295a78cd34SGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsExport;
305a78cd34SGreg Roachuse Fisharebest\Webtrees\Gedcom;
310e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
320e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
330e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
345a78cd34SGreg Roachuse Fisharebest\Webtrees\Media;
350e62c4b8SGreg Roachuse Fisharebest\Webtrees\Menu;
365a78cd34SGreg Roachuse Fisharebest\Webtrees\Note;
375a78cd34SGreg Roachuse Fisharebest\Webtrees\Repository;
38e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
390e62c4b8SGreg Roachuse Fisharebest\Webtrees\Session;
405a78cd34SGreg Roachuse Fisharebest\Webtrees\Source;
41aee13b6dSGreg Roachuse Fisharebest\Webtrees\Tree;
425a78cd34SGreg Roachuse League\Flysystem\Filesystem;
4361bf91b2SGreg Roachuse League\Flysystem\MountManager;
445a78cd34SGreg Roachuse League\Flysystem\ZipArchive\ZipArchiveAdapter;
45bed27cedSGreg Roachuse Psr\Http\Message\ResponseFactoryInterface;
466ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
476ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
486ccdf4f0SGreg Roachuse Psr\Http\Message\StreamFactoryInterface;
493976b470SGreg Roach
50eb235819SGreg Roachuse function app;
51bf80ec58SGreg Roachuse function array_filter;
52bf80ec58SGreg Roachuse function array_keys;
53bf80ec58SGreg Roachuse function array_map;
54bf80ec58SGreg Roachuse function in_array;
55bf80ec58SGreg Roachuse function key;
56bf80ec58SGreg Roachuse function preg_match_all;
57bf80ec58SGreg Roachuse function redirect;
58bf80ec58SGreg Roachuse function route;
59e5a6b4d4SGreg Roachuse function str_replace;
60bf80ec58SGreg Roachuse function strip_tags;
61bf80ec58SGreg Roachuse function sys_get_temp_dir;
62bf80ec58SGreg Roachuse function tempnam;
63bf80ec58SGreg Roachuse function ucfirst;
64bf80ec58SGreg Roachuse function utf8_decode;
658c2e8227SGreg Roach
668c2e8227SGreg Roach/**
678c2e8227SGreg Roach * Class ClippingsCartModule
688c2e8227SGreg Roach */
6937eb8894SGreg Roachclass ClippingsCartModule extends AbstractModule implements ModuleMenuInterface
70c1010edaSGreg Roach{
7149a243cbSGreg Roach    use ModuleMenuTrait;
7249a243cbSGreg Roach
735a78cd34SGreg Roach    // Routes that have a record which can be added to the clipboard
7416d6367aSGreg Roach    private const ROUTES_WITH_RECORDS = [
75c1010edaSGreg Roach        'family',
76c1010edaSGreg Roach        'individual',
77c1010edaSGreg Roach        'media',
78c1010edaSGreg Roach        'note',
79c1010edaSGreg Roach        'repository',
80c1010edaSGreg Roach        'source',
81c1010edaSGreg Roach    ];
825a78cd34SGreg Roach
8349a243cbSGreg Roach    /** @var int The default access level for this module.  It can be changed in the control panel. */
8449a243cbSGreg Roach    protected $access_level = Auth::PRIV_USER;
8549a243cbSGreg Roach
86961ec755SGreg Roach    /**
87e5a6b4d4SGreg Roach     * @var UserService
88e5a6b4d4SGreg Roach     */
89e5a6b4d4SGreg Roach    private $user_service;
90e5a6b4d4SGreg Roach
91e5a6b4d4SGreg Roach    /**
92e5a6b4d4SGreg Roach     * ClippingsCartModule constructor.
93e5a6b4d4SGreg Roach     *
94e5a6b4d4SGreg Roach     * @param UserService $user_service
95e5a6b4d4SGreg Roach     */
96e5a6b4d4SGreg Roach    public function __construct(UserService $user_service)
97e5a6b4d4SGreg Roach    {
98e5a6b4d4SGreg Roach        $this->user_service = $user_service;
99e5a6b4d4SGreg Roach    }
100e5a6b4d4SGreg Roach
101e5a6b4d4SGreg Roach    /**
1020cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
103961ec755SGreg Roach     *
104961ec755SGreg Roach     * @return string
105961ec755SGreg Roach     */
10649a243cbSGreg Roach    public function title(): string
107c1010edaSGreg Roach    {
108bbb76c12SGreg Roach        /* I18N: Name of a module */
109bbb76c12SGreg Roach        return I18N::translate('Clippings cart');
1108c2e8227SGreg Roach    }
1118c2e8227SGreg Roach
112961ec755SGreg Roach    /**
113961ec755SGreg Roach     * A sentence describing what this module does.
114961ec755SGreg Roach     *
115961ec755SGreg Roach     * @return string
116961ec755SGreg Roach     */
11749a243cbSGreg Roach    public function description(): string
118c1010edaSGreg Roach    {
119bbb76c12SGreg Roach        /* I18N: Description of the “Clippings cart” module */
120bbb76c12SGreg Roach        return I18N::translate('Select records from your family tree and save them as a GEDCOM file.');
1218c2e8227SGreg Roach    }
1228c2e8227SGreg Roach
1230ee13198SGreg Roach    /**
12449a243cbSGreg Roach     * The default position for this menu.  It can be changed in the control panel.
1250ee13198SGreg Roach     *
1260ee13198SGreg Roach     * @return int
1270ee13198SGreg Roach     */
1288f53f488SRico Sonntag    public function defaultMenuOrder(): int
129c1010edaSGreg Roach    {
130353b36abSGreg Roach        return 6;
1318c2e8227SGreg Roach    }
1328c2e8227SGreg Roach
1330ee13198SGreg Roach    /**
1340ee13198SGreg Roach     * A menu, to be added to the main application menu.
1350ee13198SGreg Roach     *
136aee13b6dSGreg Roach     * @param Tree $tree
137aee13b6dSGreg Roach     *
1380ee13198SGreg Roach     * @return Menu|null
1390ee13198SGreg Roach     */
14046295629SGreg Roach    public function getMenu(Tree $tree): ?Menu
141c1010edaSGreg Roach    {
142eb235819SGreg Roach        /** @var ServerRequestInterface $request */
1436ccdf4f0SGreg Roach        $request = app(ServerRequestInterface::class);
1448c2e8227SGreg Roach
145eb235819SGreg Roach        $route = $request->getQueryParams()['route'] ?? '';
1465a78cd34SGreg Roach
1475a78cd34SGreg Roach        $submenus = [
14849a243cbSGreg Roach            new Menu($this->title(), route('module', [
14926684e68SGreg Roach                'module' => $this->name(),
150c1010edaSGreg Roach                'action' => 'Show',
151*d72b284aSGreg Roach                'tree'    => $tree->name(),
152c1010edaSGreg Roach            ]), 'menu-clippings-cart', ['rel' => 'nofollow']),
1535a78cd34SGreg Roach        ];
1545a78cd34SGreg Roach
15522d65e5aSGreg Roach        if (in_array($route, self::ROUTES_WITH_RECORDS, true)) {
156bed27cedSGreg Roach            $xref      = $request->getQueryParams()['xref'] ?? '';
1575a78cd34SGreg Roach            $action    = 'Add' . ucfirst($route);
158c1010edaSGreg Roach            $add_route = route('module', [
15926684e68SGreg Roach                'module' => $this->name(),
160c1010edaSGreg Roach                'action' => $action,
161c1010edaSGreg Roach                'xref'   => $xref,
162*d72b284aSGreg Roach                'tree'    => $tree->name(),
163c1010edaSGreg Roach            ]);
1645a78cd34SGreg Roach
16525b2dde3SGreg Roach            $submenus[] = new Menu(I18N::translate('Add to the clippings cart'), $add_route, 'menu-clippings-add', ['rel' => 'nofollow']);
1668c2e8227SGreg Roach        }
167cbc1590aSGreg Roach
1685a78cd34SGreg Roach        if (!$this->isCartEmpty($tree)) {
169c1010edaSGreg Roach            $submenus[] = new Menu(I18N::translate('Empty the clippings cart'), route('module', [
17026684e68SGreg Roach                'module' => $this->name(),
171c1010edaSGreg Roach                'action' => 'Empty',
172*d72b284aSGreg Roach                'tree'    => $tree->name(),
173c1010edaSGreg Roach            ]), 'menu-clippings-empty', ['rel' => 'nofollow']);
174c1010edaSGreg Roach            $submenus[] = new Menu(I18N::translate('Download'), route('module', [
17526684e68SGreg Roach                'module' => $this->name(),
176c1010edaSGreg Roach                'action' => 'DownloadForm',
177*d72b284aSGreg Roach                'tree'    => $tree->name(),
178c1010edaSGreg Roach            ]), 'menu-clippings-download', ['rel' => 'nofollow']);
1795a78cd34SGreg Roach        }
1805a78cd34SGreg Roach
18149a243cbSGreg Roach        return new Menu($this->title(), '#', 'menu-clippings', ['rel' => 'nofollow'], $submenus);
1828c2e8227SGreg Roach    }
1838c2e8227SGreg Roach
18476692c8bSGreg Roach    /**
1856ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
18676692c8bSGreg Roach     *
1876ccdf4f0SGreg Roach     * @return ResponseInterface
18876692c8bSGreg Roach     */
18957ab2231SGreg Roach    public function getDownloadAction(ServerRequestInterface $request): ResponseInterface
190c1010edaSGreg Roach    {
19157ab2231SGreg Roach        $tree   = $request->getAttribute('tree');
192bed27cedSGreg Roach        $params = $request->getQueryParams();
193bed27cedSGreg Roach
194bed27cedSGreg Roach        $privatize_export = $params['privatize_export'];
195bed27cedSGreg Roach        $convert          = (bool) ($params['convert'] ?? false);
1968c2e8227SGreg Roach
19713abd6f3SGreg Roach        $cart = Session::get('cart', []);
1988c2e8227SGreg Roach
199aa6f03bbSGreg Roach        $xrefs = array_keys($cart[$tree->name()] ?? []);
2005a78cd34SGreg Roach
2015a78cd34SGreg Roach        // Create a new/empty .ZIP file
2025a78cd34SGreg Roach        $temp_zip_file  = tempnam(sys_get_temp_dir(), 'webtrees-zip-');
2035a78cd34SGreg Roach        $zip_filesystem = new Filesystem(new ZipArchiveAdapter($temp_zip_file));
2045a78cd34SGreg Roach
20561bf91b2SGreg Roach        $manager = new MountManager([
20661bf91b2SGreg Roach            'media' => $tree->mediaFilesystem(),
20761bf91b2SGreg Roach            'zip'   => $zip_filesystem,
20861bf91b2SGreg Roach        ]);
20961bf91b2SGreg Roach
2105a78cd34SGreg Roach        // Media file prefix
2115a78cd34SGreg Roach        $path = $tree->getPreference('MEDIA_DIRECTORY');
2125a78cd34SGreg Roach
2135a78cd34SGreg Roach        // GEDCOM file header
214a3d8780cSGreg Roach        $filetext = FunctionsExport::gedcomHeader($tree, $convert ? 'ANSI' : 'UTF-8');
2155a78cd34SGreg Roach
2165a78cd34SGreg Roach        switch ($privatize_export) {
2175a78cd34SGreg Roach            case 'gedadmin':
2185a78cd34SGreg Roach                $access_level = Auth::PRIV_NONE;
2195a78cd34SGreg Roach                break;
2205a78cd34SGreg Roach            case 'user':
2215a78cd34SGreg Roach                $access_level = Auth::PRIV_USER;
2225a78cd34SGreg Roach                break;
2235a78cd34SGreg Roach            case 'visitor':
2245a78cd34SGreg Roach                $access_level = Auth::PRIV_PRIVATE;
2255a78cd34SGreg Roach                break;
2265a78cd34SGreg Roach            case 'none':
2275a78cd34SGreg Roach            default:
2285a78cd34SGreg Roach                $access_level = Auth::PRIV_HIDE;
2295a78cd34SGreg Roach                break;
2305a78cd34SGreg Roach        }
2315a78cd34SGreg Roach
2325a78cd34SGreg Roach        foreach ($xrefs as $xref) {
2335a78cd34SGreg Roach            $object = GedcomRecord::getInstance($xref, $tree);
2345a78cd34SGreg Roach            // The object may have been deleted since we added it to the cart....
235bed27cedSGreg Roach            if ($object instanceof  GedcomRecord) {
2365a78cd34SGreg Roach                $record = $object->privatizeGedcom($access_level);
2375a78cd34SGreg Roach                // Remove links to objects that aren't in the cart
2388d0ebef0SGreg Roach                preg_match_all('/\n1 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[2-9].*)*/', $record, $matches, PREG_SET_ORDER);
2395a78cd34SGreg Roach                foreach ($matches as $match) {
240bf80ec58SGreg Roach                    if (!in_array($match[1], $xrefs, true)) {
2415a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2425a78cd34SGreg Roach                    }
2435a78cd34SGreg Roach                }
2448d0ebef0SGreg Roach                preg_match_all('/\n2 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[3-9].*)*/', $record, $matches, PREG_SET_ORDER);
2455a78cd34SGreg Roach                foreach ($matches as $match) {
246bf80ec58SGreg Roach                    if (!in_array($match[1], $xrefs, true)) {
2475a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2485a78cd34SGreg Roach                    }
2495a78cd34SGreg Roach                }
2508d0ebef0SGreg Roach                preg_match_all('/\n3 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[4-9].*)*/', $record, $matches, PREG_SET_ORDER);
2515a78cd34SGreg Roach                foreach ($matches as $match) {
252bf80ec58SGreg Roach                    if (!in_array($match[1], $xrefs, true)) {
2535a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2545a78cd34SGreg Roach                    }
2555a78cd34SGreg Roach                }
2565a78cd34SGreg Roach
25755167344SGreg Roach                if ($object instanceof Individual || $object instanceof Family) {
2585a78cd34SGreg Roach                    $filetext .= $record . "\n";
2595a78cd34SGreg Roach                    $filetext .= "1 SOUR @WEBTREES@\n";
2601f273236SGreg Roach                    $filetext .= '2 PAGE ' . $object->url() . "\n";
26155167344SGreg Roach                } elseif ($object instanceof Source) {
2625a78cd34SGreg Roach                    $filetext .= $record . "\n";
2631f273236SGreg Roach                    $filetext .= '1 NOTE ' . $object->url() . "\n";
26455167344SGreg Roach                } elseif ($object instanceof Media) {
26555167344SGreg Roach                    // Add the media files to the archive
2665a78cd34SGreg Roach                    foreach ($object->mediaFiles() as $media_file) {
26761bf91b2SGreg Roach                        $from = 'media://' . $media_file->filename();
26861bf91b2SGreg Roach                        $to   = 'zip://' . $path . $media_file->filename();
26961bf91b2SGreg Roach                        if (!$media_file->isExternal() && $manager->has($from)) {
27061bf91b2SGreg Roach                            $manager->copy($from, $to);
2715a78cd34SGreg Roach                        }
2725a78cd34SGreg Roach                    }
2735a78cd34SGreg Roach                    $filetext .= $record . "\n";
27455167344SGreg Roach                } else {
2755a78cd34SGreg Roach                    $filetext .= $record . "\n";
2768c2e8227SGreg Roach                }
2778c2e8227SGreg Roach            }
2788c2e8227SGreg Roach        }
2798c2e8227SGreg Roach
2809b93b7c3SGreg Roach        $base_url = $request->getAttribute('base_url');
2819b93b7c3SGreg Roach
2825a78cd34SGreg Roach        // Create a source, to indicate the source of the data.
2839b93b7c3SGreg Roach        $filetext .= "0 @WEBTREES@ SOUR\n1 TITL " . $base_url . "\n";
284e5a6b4d4SGreg Roach        $author   = $this->user_service->find((int) $tree->getPreference('CONTACT_USER_ID'));
2855a78cd34SGreg Roach        if ($author !== null) {
286e5a6b4d4SGreg Roach            $filetext .= '1 AUTH ' . $author->realName() . "\n";
2875a78cd34SGreg Roach        }
2885a78cd34SGreg Roach        $filetext .= "0 TRLR\n";
2895a78cd34SGreg Roach
2905a78cd34SGreg Roach        // Make sure the preferred line endings are used
291a3d8780cSGreg Roach        $filetext = str_replace('\n', Gedcom::EOL, $filetext);
2925a78cd34SGreg Roach
29355167344SGreg Roach        if ($convert) {
2945a78cd34SGreg Roach            $filetext = utf8_decode($filetext);
2958c2e8227SGreg Roach        }
296cbc1590aSGreg Roach
2975a78cd34SGreg Roach        // Finally add the GEDCOM file to the .ZIP file.
2985a78cd34SGreg Roach        $zip_filesystem->write('clippings.ged', $filetext);
2995a78cd34SGreg Roach
30061bf91b2SGreg Roach        // Need to force-close ZipArchive filesystems.
30161bf91b2SGreg Roach        $zip_filesystem->getAdapter()->getArchive()->close();
3025a78cd34SGreg Roach
3036ccdf4f0SGreg Roach        // Use a stream, so that we do not have to load the entire file into memory.
3046ccdf4f0SGreg Roach        $stream = app(StreamFactoryInterface::class)->createStreamFromFile($temp_zip_file);
3055a78cd34SGreg Roach
306bed27cedSGreg Roach        /** @var ResponseFactoryInterface $response_factory */
307bed27cedSGreg Roach        $response_factory = app(ResponseFactoryInterface::class);
308bed27cedSGreg Roach
309bed27cedSGreg Roach        return $response_factory->createResponse()
3106ccdf4f0SGreg Roach            ->withBody($stream)
3111b3d4731SGreg Roach            ->withHeader('Content-Type', 'application/zip')
312bed27cedSGreg Roach            ->withHeader('Content-Disposition', 'attachment; filename="clippings.zip');
3138c2e8227SGreg Roach    }
3148c2e8227SGreg Roach
3158c2e8227SGreg Roach    /**
31657ab2231SGreg Roach     * @param ServerRequestInterface $request
31776692c8bSGreg Roach     *
3186ccdf4f0SGreg Roach     * @return ResponseInterface
3198c2e8227SGreg Roach     */
32057ab2231SGreg Roach    public function getDownloadFormAction(ServerRequestInterface $request): ResponseInterface
321c1010edaSGreg Roach    {
32257ab2231SGreg Roach        $tree  = $request->getAttribute('tree');
32357ab2231SGreg Roach        $user  = $request->getAttribute('user');
3245a78cd34SGreg Roach        $title = I18N::translate('Family tree clippings cart') . ' — ' . I18N::translate('Download');
3258c2e8227SGreg Roach
3265a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/download', [
3275a78cd34SGreg Roach            'is_manager' => Auth::isManager($tree, $user),
3285a78cd34SGreg Roach            'is_member'  => Auth::isMember($tree, $user),
32971378461SGreg Roach            'module'     => $this->name(),
3305a78cd34SGreg Roach            'title'      => $title,
3315a78cd34SGreg Roach        ]);
3328c2e8227SGreg Roach    }
3338c2e8227SGreg Roach
3345a78cd34SGreg Roach    /**
33557ab2231SGreg Roach     * @param ServerRequestInterface $request
3365a78cd34SGreg Roach     *
3376ccdf4f0SGreg Roach     * @return ResponseInterface
3385a78cd34SGreg Roach     */
33957ab2231SGreg Roach    public function getEmptyAction(ServerRequestInterface $request): ResponseInterface
340c1010edaSGreg Roach    {
34157ab2231SGreg Roach        $tree                = $request->getAttribute('tree');
3425a78cd34SGreg Roach        $cart                = Session::get('cart', []);
343aa6f03bbSGreg Roach        $cart[$tree->name()] = [];
3445a78cd34SGreg Roach        Session::put('cart', $cart);
3458c2e8227SGreg Roach
346c1010edaSGreg Roach        $url = route('module', [
34726684e68SGreg Roach            'module' => $this->name(),
348c1010edaSGreg Roach            'action' => 'Show',
349*d72b284aSGreg Roach            'tree'    => $tree->name(),
350c1010edaSGreg Roach        ]);
3515a78cd34SGreg Roach
3526ccdf4f0SGreg Roach        return redirect($url);
3535a78cd34SGreg Roach    }
3545a78cd34SGreg Roach
3555a78cd34SGreg Roach    /**
3566ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
3575a78cd34SGreg Roach     *
3586ccdf4f0SGreg Roach     * @return ResponseInterface
3595a78cd34SGreg Roach     */
36057ab2231SGreg Roach    public function postRemoveAction(ServerRequestInterface $request): ResponseInterface
361c1010edaSGreg Roach    {
36257ab2231SGreg Roach        $tree = $request->getAttribute('tree');
363bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
3645a78cd34SGreg Roach
3655a78cd34SGreg Roach        $cart = Session::get('cart', []);
366aa6f03bbSGreg Roach        unset($cart[$tree->name()][$xref]);
3675a78cd34SGreg Roach        Session::put('cart', $cart);
3685a78cd34SGreg Roach
369c1010edaSGreg Roach        $url = route('module', [
37026684e68SGreg Roach            'module' => $this->name(),
371c1010edaSGreg Roach            'action' => 'Show',
372*d72b284aSGreg Roach            'tree'    => $tree->name(),
373c1010edaSGreg Roach        ]);
3745a78cd34SGreg Roach
3756ccdf4f0SGreg Roach        return redirect($url);
3765a78cd34SGreg Roach    }
3775a78cd34SGreg Roach
3785a78cd34SGreg Roach    /**
37957ab2231SGreg Roach     * @param ServerRequestInterface $request
3805a78cd34SGreg Roach     *
3816ccdf4f0SGreg Roach     * @return ResponseInterface
3825a78cd34SGreg Roach     */
38357ab2231SGreg Roach    public function getShowAction(ServerRequestInterface $request): ResponseInterface
384c1010edaSGreg Roach    {
38557ab2231SGreg Roach        $tree = $request->getAttribute('tree');
38657ab2231SGreg Roach
3875a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/show', [
3885a78cd34SGreg Roach            'records' => $this->allRecordsInCart($tree),
3895a78cd34SGreg Roach            'title'   => I18N::translate('Family tree clippings cart'),
3905a78cd34SGreg Roach            'tree'    => $tree,
3915a78cd34SGreg Roach        ]);
3925a78cd34SGreg Roach    }
3935a78cd34SGreg Roach
3945a78cd34SGreg Roach    /**
3956ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
3965a78cd34SGreg Roach     *
3976ccdf4f0SGreg Roach     * @return ResponseInterface
3985a78cd34SGreg Roach     */
39957ab2231SGreg Roach    public function getAddFamilyAction(ServerRequestInterface $request): ResponseInterface
400c1010edaSGreg Roach    {
40157ab2231SGreg Roach        $tree = $request->getAttribute('tree');
402bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
4035a78cd34SGreg Roach
4045a78cd34SGreg Roach        $family = Family::getInstance($xref, $tree);
4055a78cd34SGreg Roach
4065a78cd34SGreg Roach        if ($family === null) {
40759f2f229SGreg Roach            throw new FamilyNotFoundException();
4085a78cd34SGreg Roach        }
4095a78cd34SGreg Roach
4105a78cd34SGreg Roach        $options = $this->familyOptions($family);
4115a78cd34SGreg Roach
41239ca88baSGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $family->fullName());
4135a78cd34SGreg Roach
4145a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
41583615acfSGreg Roach            'action'  => route('module', ['module' => $this->name(), 'action' => 'AddFamily']),
4165a78cd34SGreg Roach            'options' => $options,
4175a78cd34SGreg Roach            'default' => key($options),
4185a78cd34SGreg Roach            'record'  => $family,
4195a78cd34SGreg Roach            'title'   => $title,
4205a78cd34SGreg Roach            'tree'    => $tree,
4215a78cd34SGreg Roach        ]);
4225a78cd34SGreg Roach    }
4235a78cd34SGreg Roach
4245a78cd34SGreg Roach    /**
4255a78cd34SGreg Roach     * @param Family $family
4265a78cd34SGreg Roach     *
4275a78cd34SGreg Roach     * @return string[]
4285a78cd34SGreg Roach     */
429c1010edaSGreg Roach    private function familyOptions(Family $family): array
430c1010edaSGreg Roach    {
43139ca88baSGreg Roach        $name = strip_tags($family->fullName());
4325a78cd34SGreg Roach
4335a78cd34SGreg Roach        return [
4345a78cd34SGreg Roach            'parents'     => $name,
435bbb76c12SGreg Roach            /* I18N: %s is a family (husband + wife) */
436bbb76c12SGreg Roach            'members'     => I18N::translate('%s and their children', $name),
437bbb76c12SGreg Roach            /* I18N: %s is a family (husband + wife) */
438bbb76c12SGreg Roach            'descendants' => I18N::translate('%s and their descendants', $name),
4395a78cd34SGreg Roach        ];
4405a78cd34SGreg Roach    }
4415a78cd34SGreg Roach
4425a78cd34SGreg Roach    /**
4436ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
4445a78cd34SGreg Roach     *
4456ccdf4f0SGreg Roach     * @return ResponseInterface
4465a78cd34SGreg Roach     */
44757ab2231SGreg Roach    public function postAddFamilyAction(ServerRequestInterface $request): ResponseInterface
448c1010edaSGreg Roach    {
44957ab2231SGreg Roach        $tree   = $request->getAttribute('tree');
450bed27cedSGreg Roach        $xref   = $request->getQueryParams()['xref'];
451bed27cedSGreg Roach        $option = $request->getParsedBody()['option'];
4525a78cd34SGreg Roach
4535a78cd34SGreg Roach        $family = Family::getInstance($xref, $tree);
4545a78cd34SGreg Roach
4555a78cd34SGreg Roach        if ($family === null) {
45659f2f229SGreg Roach            throw new FamilyNotFoundException();
4575a78cd34SGreg Roach        }
4585a78cd34SGreg Roach
4595a78cd34SGreg Roach        switch ($option) {
4605a78cd34SGreg Roach            case 'parents':
4615a78cd34SGreg Roach                $this->addFamilyToCart($family);
4625a78cd34SGreg Roach                break;
4635a78cd34SGreg Roach
4645a78cd34SGreg Roach            case 'members':
4655a78cd34SGreg Roach                $this->addFamilyAndChildrenToCart($family);
4665a78cd34SGreg Roach                break;
4675a78cd34SGreg Roach
4685a78cd34SGreg Roach            case 'descendants':
4695a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($family);
4705a78cd34SGreg Roach                break;
4715a78cd34SGreg Roach        }
4725a78cd34SGreg Roach
4736ccdf4f0SGreg Roach        return redirect($family->url());
4745a78cd34SGreg Roach    }
4755a78cd34SGreg Roach
4765a78cd34SGreg Roach    /**
4775a78cd34SGreg Roach     * @param Family $family
47818d7a90dSGreg Roach     *
47918d7a90dSGreg Roach     * @return void
4805a78cd34SGreg Roach     */
481e364afe4SGreg Roach    private function addFamilyToCart(Family $family): void
482c1010edaSGreg Roach    {
4835a78cd34SGreg Roach        $this->addRecordToCart($family);
4845a78cd34SGreg Roach
48539ca88baSGreg Roach        foreach ($family->spouses() as $spouse) {
4865a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4875a78cd34SGreg Roach        }
4885a78cd34SGreg Roach    }
4895a78cd34SGreg Roach
4905a78cd34SGreg Roach    /**
4915a78cd34SGreg Roach     * @param Family $family
49218d7a90dSGreg Roach     *
49318d7a90dSGreg Roach     * @return void
4945a78cd34SGreg Roach     */
495e364afe4SGreg Roach    private function addFamilyAndChildrenToCart(Family $family): void
496c1010edaSGreg Roach    {
4975a78cd34SGreg Roach        $this->addRecordToCart($family);
4985a78cd34SGreg Roach
49939ca88baSGreg Roach        foreach ($family->spouses() as $spouse) {
5005a78cd34SGreg Roach            $this->addRecordToCart($spouse);
5015a78cd34SGreg Roach        }
50239ca88baSGreg Roach        foreach ($family->children() as $child) {
5035a78cd34SGreg Roach            $this->addRecordToCart($child);
5045a78cd34SGreg Roach        }
5055a78cd34SGreg Roach    }
5065a78cd34SGreg Roach
5075a78cd34SGreg Roach    /**
5085a78cd34SGreg Roach     * @param Family $family
50918d7a90dSGreg Roach     *
51018d7a90dSGreg Roach     * @return void
5115a78cd34SGreg Roach     */
512e364afe4SGreg Roach    private function addFamilyAndDescendantsToCart(Family $family): void
513c1010edaSGreg Roach    {
5145a78cd34SGreg Roach        $this->addRecordToCart($family);
5155a78cd34SGreg Roach
51639ca88baSGreg Roach        foreach ($family->spouses() as $spouse) {
5175a78cd34SGreg Roach            $this->addRecordToCart($spouse);
5185a78cd34SGreg Roach        }
51939ca88baSGreg Roach        foreach ($family->children() as $child) {
5205a78cd34SGreg Roach            $this->addRecordToCart($child);
52139ca88baSGreg Roach            foreach ($child->spouseFamilies() as $child_family) {
5225a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($child_family);
5235a78cd34SGreg Roach            }
5245a78cd34SGreg Roach        }
5255a78cd34SGreg Roach    }
5265a78cd34SGreg Roach
5275a78cd34SGreg Roach    /**
5286ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
5295a78cd34SGreg Roach     *
5306ccdf4f0SGreg Roach     * @return ResponseInterface
5315a78cd34SGreg Roach     */
53257ab2231SGreg Roach    public function getAddIndividualAction(ServerRequestInterface $request): ResponseInterface
533c1010edaSGreg Roach    {
53457ab2231SGreg Roach        $tree = $request->getAttribute('tree');
535bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
5365a78cd34SGreg Roach
5375a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
5385a78cd34SGreg Roach
5395a78cd34SGreg Roach        if ($individual === null) {
54059f2f229SGreg Roach            throw new IndividualNotFoundException();
5415a78cd34SGreg Roach        }
5425a78cd34SGreg Roach
5435a78cd34SGreg Roach        $options = $this->individualOptions($individual);
5445a78cd34SGreg Roach
54539ca88baSGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $individual->fullName());
5465a78cd34SGreg Roach
5475a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
54883615acfSGreg Roach            'action'  => route('module', ['module' => $this->name(), 'action' => 'AddIndividual']),
5495a78cd34SGreg Roach            'options' => $options,
5505a78cd34SGreg Roach            'default' => key($options),
5515a78cd34SGreg Roach            'record'  => $individual,
5525a78cd34SGreg Roach            'title'   => $title,
5535a78cd34SGreg Roach            'tree'    => $tree,
5545a78cd34SGreg Roach        ]);
5555a78cd34SGreg Roach    }
5565a78cd34SGreg Roach
5575a78cd34SGreg Roach    /**
5585a78cd34SGreg Roach     * @param Individual $individual
5595a78cd34SGreg Roach     *
5605a78cd34SGreg Roach     * @return string[]
5615a78cd34SGreg Roach     */
562c1010edaSGreg Roach    private function individualOptions(Individual $individual): array
563c1010edaSGreg Roach    {
56439ca88baSGreg Roach        $name = strip_tags($individual->fullName());
5655a78cd34SGreg Roach
56639ca88baSGreg Roach        if ($individual->sex() === 'F') {
5675a78cd34SGreg Roach            return [
5685a78cd34SGreg Roach                'self'              => $name,
5695a78cd34SGreg Roach                'parents'           => I18N::translate('%s, her parents and siblings', $name),
5705a78cd34SGreg Roach                'spouses'           => I18N::translate('%s, her spouses and children', $name),
5715a78cd34SGreg Roach                'ancestors'         => I18N::translate('%s and her ancestors', $name),
5725a78cd34SGreg Roach                'ancestor_families' => I18N::translate('%s, her ancestors and their families', $name),
5735a78cd34SGreg Roach                'descendants'       => I18N::translate('%s, her spouses and descendants', $name),
5745a78cd34SGreg Roach            ];
575b2ce94c6SRico Sonntag        }
576b2ce94c6SRico Sonntag
5775a78cd34SGreg Roach        return [
5785a78cd34SGreg Roach            'self'              => $name,
5795a78cd34SGreg Roach            'parents'           => I18N::translate('%s, his parents and siblings', $name),
5805a78cd34SGreg Roach            'spouses'           => I18N::translate('%s, his spouses and children', $name),
5815a78cd34SGreg Roach            'ancestors'         => I18N::translate('%s and his ancestors', $name),
5825a78cd34SGreg Roach            'ancestor_families' => I18N::translate('%s, his ancestors and their families', $name),
5835a78cd34SGreg Roach            'descendants'       => I18N::translate('%s, his spouses and descendants', $name),
5845a78cd34SGreg Roach        ];
5855a78cd34SGreg Roach    }
5865a78cd34SGreg Roach
5875a78cd34SGreg Roach    /**
5886ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
5895a78cd34SGreg Roach     *
5906ccdf4f0SGreg Roach     * @return ResponseInterface
5915a78cd34SGreg Roach     */
59257ab2231SGreg Roach    public function postAddIndividualAction(ServerRequestInterface $request): ResponseInterface
593c1010edaSGreg Roach    {
59457ab2231SGreg Roach        $tree   = $request->getAttribute('tree');
595bed27cedSGreg Roach        $xref   = $request->getQueryParams()['xref'];
596bed27cedSGreg Roach        $option = $request->getParsedBody()['option'];
5975a78cd34SGreg Roach
5985a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
5995a78cd34SGreg Roach
6005a78cd34SGreg Roach        if ($individual === null) {
60159f2f229SGreg Roach            throw new IndividualNotFoundException();
6025a78cd34SGreg Roach        }
6035a78cd34SGreg Roach
6045a78cd34SGreg Roach        switch ($option) {
6055a78cd34SGreg Roach            case 'self':
6065a78cd34SGreg Roach                $this->addRecordToCart($individual);
6075a78cd34SGreg Roach                break;
6085a78cd34SGreg Roach
6095a78cd34SGreg Roach            case 'parents':
61039ca88baSGreg Roach                foreach ($individual->childFamilies() as $family) {
6115a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
6125a78cd34SGreg Roach                }
6135a78cd34SGreg Roach                break;
6145a78cd34SGreg Roach
6155a78cd34SGreg Roach            case 'spouses':
61639ca88baSGreg Roach                foreach ($individual->spouseFamilies() as $family) {
6175a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
6185a78cd34SGreg Roach                }
6195a78cd34SGreg Roach                break;
6205a78cd34SGreg Roach
6215a78cd34SGreg Roach            case 'ancestors':
6225a78cd34SGreg Roach                $this->addAncestorsToCart($individual);
6235a78cd34SGreg Roach                break;
6245a78cd34SGreg Roach
6255a78cd34SGreg Roach            case 'ancestor_families':
6265a78cd34SGreg Roach                $this->addAncestorFamiliesToCart($individual);
6275a78cd34SGreg Roach                break;
6285a78cd34SGreg Roach
6295a78cd34SGreg Roach            case 'descendants':
63039ca88baSGreg Roach                foreach ($individual->spouseFamilies() as $family) {
6315a78cd34SGreg Roach                    $this->addFamilyAndDescendantsToCart($family);
6325a78cd34SGreg Roach                }
6335a78cd34SGreg Roach                break;
6345a78cd34SGreg Roach        }
6355a78cd34SGreg Roach
6366ccdf4f0SGreg Roach        return redirect($individual->url());
6375a78cd34SGreg Roach    }
6385a78cd34SGreg Roach
6395a78cd34SGreg Roach    /**
6405a78cd34SGreg Roach     * @param Individual $individual
64118d7a90dSGreg Roach     *
64218d7a90dSGreg Roach     * @return void
6435a78cd34SGreg Roach     */
644e364afe4SGreg Roach    private function addAncestorsToCart(Individual $individual): void
645c1010edaSGreg Roach    {
6465a78cd34SGreg Roach        $this->addRecordToCart($individual);
6475a78cd34SGreg Roach
64839ca88baSGreg Roach        foreach ($individual->childFamilies() as $family) {
64939ca88baSGreg Roach            foreach ($family->spouses() as $parent) {
6505a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
6515a78cd34SGreg Roach            }
6525a78cd34SGreg Roach        }
6535a78cd34SGreg Roach    }
6545a78cd34SGreg Roach
6555a78cd34SGreg Roach    /**
6565a78cd34SGreg Roach     * @param Individual $individual
65718d7a90dSGreg Roach     *
65818d7a90dSGreg Roach     * @return void
6595a78cd34SGreg Roach     */
660e364afe4SGreg Roach    private function addAncestorFamiliesToCart(Individual $individual): void
661c1010edaSGreg Roach    {
66239ca88baSGreg Roach        foreach ($individual->childFamilies() as $family) {
6635a78cd34SGreg Roach            $this->addFamilyAndChildrenToCart($family);
66439ca88baSGreg Roach            foreach ($family->spouses() as $parent) {
6655a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
6665a78cd34SGreg Roach            }
6675a78cd34SGreg Roach        }
6685a78cd34SGreg Roach    }
6695a78cd34SGreg Roach
6705a78cd34SGreg Roach    /**
6716ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
6725a78cd34SGreg Roach     *
6736ccdf4f0SGreg Roach     * @return ResponseInterface
6745a78cd34SGreg Roach     */
67557ab2231SGreg Roach    public function getAddMediaAction(ServerRequestInterface $request): ResponseInterface
676c1010edaSGreg Roach    {
67757ab2231SGreg Roach        $tree = $request->getAttribute('tree');
678bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
6795a78cd34SGreg Roach
6805a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
6815a78cd34SGreg Roach
6825a78cd34SGreg Roach        if ($media === null) {
68359f2f229SGreg Roach            throw new MediaNotFoundException();
6845a78cd34SGreg Roach        }
6855a78cd34SGreg Roach
6865a78cd34SGreg Roach        $options = $this->mediaOptions($media);
6875a78cd34SGreg Roach
68839ca88baSGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $media->fullName());
6895a78cd34SGreg Roach
6905a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
69183615acfSGreg Roach            'action'  => route('module', ['module' => $this->name(), 'action' => 'AddMedia']),
6925a78cd34SGreg Roach            'options' => $options,
6935a78cd34SGreg Roach            'default' => key($options),
6945a78cd34SGreg Roach            'record'  => $media,
6955a78cd34SGreg Roach            'title'   => $title,
6965a78cd34SGreg Roach            'tree'    => $tree,
6975a78cd34SGreg Roach        ]);
6985a78cd34SGreg Roach    }
6995a78cd34SGreg Roach
7005a78cd34SGreg Roach    /**
7015a78cd34SGreg Roach     * @param Media $media
7025a78cd34SGreg Roach     *
7035a78cd34SGreg Roach     * @return string[]
7045a78cd34SGreg Roach     */
705c1010edaSGreg Roach    private function mediaOptions(Media $media): array
706c1010edaSGreg Roach    {
70739ca88baSGreg Roach        $name = strip_tags($media->fullName());
7085a78cd34SGreg Roach
7095a78cd34SGreg Roach        return [
7105a78cd34SGreg Roach            'self' => $name,
7115a78cd34SGreg Roach        ];
7125a78cd34SGreg Roach    }
7135a78cd34SGreg Roach
7145a78cd34SGreg Roach    /**
7156ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
7165a78cd34SGreg Roach     *
7176ccdf4f0SGreg Roach     * @return ResponseInterface
7185a78cd34SGreg Roach     */
71957ab2231SGreg Roach    public function postAddMediaAction(ServerRequestInterface $request): ResponseInterface
720c1010edaSGreg Roach    {
72157ab2231SGreg Roach        $tree = $request->getAttribute('tree');
722bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
7235a78cd34SGreg Roach
7245a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
7255a78cd34SGreg Roach
7265a78cd34SGreg Roach        if ($media === null) {
72759f2f229SGreg Roach            throw new MediaNotFoundException();
7285a78cd34SGreg Roach        }
7295a78cd34SGreg Roach
7305a78cd34SGreg Roach        $this->addRecordToCart($media);
7315a78cd34SGreg Roach
7326ccdf4f0SGreg Roach        return redirect($media->url());
7335a78cd34SGreg Roach    }
7345a78cd34SGreg Roach
7355a78cd34SGreg Roach    /**
7366ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
7375a78cd34SGreg Roach     *
7386ccdf4f0SGreg Roach     * @return ResponseInterface
7395a78cd34SGreg Roach     */
74057ab2231SGreg Roach    public function getAddNoteAction(ServerRequestInterface $request): ResponseInterface
741c1010edaSGreg Roach    {
74257ab2231SGreg Roach        $tree = $request->getAttribute('tree');
743bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
7445a78cd34SGreg Roach
7455a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
7465a78cd34SGreg Roach
7475a78cd34SGreg Roach        if ($note === null) {
74859f2f229SGreg Roach            throw new NoteNotFoundException();
7495a78cd34SGreg Roach        }
7505a78cd34SGreg Roach
7515a78cd34SGreg Roach        $options = $this->noteOptions($note);
7525a78cd34SGreg Roach
75339ca88baSGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $note->fullName());
7545a78cd34SGreg Roach
7555a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
75683615acfSGreg Roach            'action'  => route('module', ['module' => $this->name(), 'action' => 'AddNote']),
7575a78cd34SGreg Roach            'options' => $options,
7585a78cd34SGreg Roach            'default' => key($options),
7595a78cd34SGreg Roach            'record'  => $note,
7605a78cd34SGreg Roach            'title'   => $title,
7615a78cd34SGreg Roach            'tree'    => $tree,
7625a78cd34SGreg Roach        ]);
7635a78cd34SGreg Roach    }
7645a78cd34SGreg Roach
7655a78cd34SGreg Roach    /**
7665a78cd34SGreg Roach     * @param Note $note
7675a78cd34SGreg Roach     *
7685a78cd34SGreg Roach     * @return string[]
7695a78cd34SGreg Roach     */
770c1010edaSGreg Roach    private function noteOptions(Note $note): array
771c1010edaSGreg Roach    {
77239ca88baSGreg Roach        $name = strip_tags($note->fullName());
7735a78cd34SGreg Roach
7745a78cd34SGreg Roach        return [
7755a78cd34SGreg Roach            'self' => $name,
7765a78cd34SGreg Roach        ];
7775a78cd34SGreg Roach    }
7785a78cd34SGreg Roach
7795a78cd34SGreg Roach    /**
7806ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
7815a78cd34SGreg Roach     *
7826ccdf4f0SGreg Roach     * @return ResponseInterface
7835a78cd34SGreg Roach     */
78457ab2231SGreg Roach    public function postAddNoteAction(ServerRequestInterface $request): ResponseInterface
785c1010edaSGreg Roach    {
78657ab2231SGreg Roach        $tree = $request->getAttribute('tree');
787bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
7885a78cd34SGreg Roach
7895a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
7905a78cd34SGreg Roach
7915a78cd34SGreg Roach        if ($note === null) {
79259f2f229SGreg Roach            throw new NoteNotFoundException();
7935a78cd34SGreg Roach        }
7945a78cd34SGreg Roach
7955a78cd34SGreg Roach        $this->addRecordToCart($note);
7965a78cd34SGreg Roach
7976ccdf4f0SGreg Roach        return redirect($note->url());
7985a78cd34SGreg Roach    }
7995a78cd34SGreg Roach
8005a78cd34SGreg Roach    /**
8016ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
8025a78cd34SGreg Roach     *
8036ccdf4f0SGreg Roach     * @return ResponseInterface
8045a78cd34SGreg Roach     */
80557ab2231SGreg Roach    public function getAddRepositoryAction(ServerRequestInterface $request): ResponseInterface
806c1010edaSGreg Roach    {
80757ab2231SGreg Roach        $tree = $request->getAttribute('tree');
808bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
8095a78cd34SGreg Roach
8105a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
8115a78cd34SGreg Roach
8125a78cd34SGreg Roach        if ($repository === null) {
81359f2f229SGreg Roach            throw new RepositoryNotFoundException();
8145a78cd34SGreg Roach        }
8155a78cd34SGreg Roach
8165a78cd34SGreg Roach        $options = $this->repositoryOptions($repository);
8175a78cd34SGreg Roach
81839ca88baSGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $repository->fullName());
8195a78cd34SGreg Roach
8205a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
82183615acfSGreg Roach            'action'  => route('module', ['module' => $this->name(), 'action' => 'AddRepository']),
8225a78cd34SGreg Roach            'options' => $options,
8235a78cd34SGreg Roach            'default' => key($options),
8245a78cd34SGreg Roach            'record'  => $repository,
8255a78cd34SGreg Roach            'title'   => $title,
8265a78cd34SGreg Roach            'tree'    => $tree,
8275a78cd34SGreg Roach        ]);
8285a78cd34SGreg Roach    }
8295a78cd34SGreg Roach
8305a78cd34SGreg Roach    /**
8315a78cd34SGreg Roach     * @param Repository $repository
8325a78cd34SGreg Roach     *
8335a78cd34SGreg Roach     * @return string[]
8345a78cd34SGreg Roach     */
835c1010edaSGreg Roach    private function repositoryOptions(Repository $repository): array
836c1010edaSGreg Roach    {
83739ca88baSGreg Roach        $name = strip_tags($repository->fullName());
8385a78cd34SGreg Roach
8395a78cd34SGreg Roach        return [
8405a78cd34SGreg Roach            'self' => $name,
8415a78cd34SGreg Roach        ];
8425a78cd34SGreg Roach    }
8435a78cd34SGreg Roach
8445a78cd34SGreg Roach    /**
8456ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
8465a78cd34SGreg Roach     *
8476ccdf4f0SGreg Roach     * @return ResponseInterface
8485a78cd34SGreg Roach     */
84957ab2231SGreg Roach    public function postAddRepositoryAction(ServerRequestInterface $request): ResponseInterface
850c1010edaSGreg Roach    {
85157ab2231SGreg Roach        $tree = $request->getAttribute('tree');
852bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
8535a78cd34SGreg Roach
8545a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
8555a78cd34SGreg Roach
8565a78cd34SGreg Roach        if ($repository === null) {
85759f2f229SGreg Roach            throw new RepositoryNotFoundException();
8585a78cd34SGreg Roach        }
8595a78cd34SGreg Roach
8605a78cd34SGreg Roach        $this->addRecordToCart($repository);
8615a78cd34SGreg Roach
8626ccdf4f0SGreg Roach        return redirect($repository->url());
8635a78cd34SGreg Roach    }
8645a78cd34SGreg Roach
8655a78cd34SGreg Roach    /**
8666ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
8675a78cd34SGreg Roach     *
8686ccdf4f0SGreg Roach     * @return ResponseInterface
8695a78cd34SGreg Roach     */
87057ab2231SGreg Roach    public function getAddSourceAction(ServerRequestInterface $request): ResponseInterface
871c1010edaSGreg Roach    {
87257ab2231SGreg Roach        $tree = $request->getAttribute('tree');
873bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
8745a78cd34SGreg Roach
8755a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
8765a78cd34SGreg Roach
8775a78cd34SGreg Roach        if ($source === null) {
87859f2f229SGreg Roach            throw new SourceNotFoundException();
8795a78cd34SGreg Roach        }
8805a78cd34SGreg Roach
8815a78cd34SGreg Roach        $options = $this->sourceOptions($source);
8825a78cd34SGreg Roach
88339ca88baSGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $source->fullName());
8845a78cd34SGreg Roach
8855a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
88683615acfSGreg Roach            'action'  => route('module', ['module' => $this->name(), 'action' => 'AddSource']),
8875a78cd34SGreg Roach            'options' => $options,
8885a78cd34SGreg Roach            'default' => key($options),
8895a78cd34SGreg Roach            'record'  => $source,
8905a78cd34SGreg Roach            'title'   => $title,
8915a78cd34SGreg Roach            'tree'    => $tree,
8925a78cd34SGreg Roach        ]);
8935a78cd34SGreg Roach    }
8945a78cd34SGreg Roach
8955a78cd34SGreg Roach    /**
8965a78cd34SGreg Roach     * @param Source $source
8975a78cd34SGreg Roach     *
8985a78cd34SGreg Roach     * @return string[]
8995a78cd34SGreg Roach     */
900c1010edaSGreg Roach    private function sourceOptions(Source $source): array
901c1010edaSGreg Roach    {
90239ca88baSGreg Roach        $name = strip_tags($source->fullName());
9035a78cd34SGreg Roach
9045a78cd34SGreg Roach        return [
90539ca88baSGreg Roach            'only'   => strip_tags($source->fullName()),
9065a78cd34SGreg Roach            'linked' => I18N::translate('%s and the individuals that reference it.', $name),
9075a78cd34SGreg Roach        ];
9085a78cd34SGreg Roach    }
9095a78cd34SGreg Roach
9105a78cd34SGreg Roach    /**
9116ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
9125a78cd34SGreg Roach     *
9136ccdf4f0SGreg Roach     * @return ResponseInterface
9145a78cd34SGreg Roach     */
91557ab2231SGreg Roach    public function postAddSourceAction(ServerRequestInterface $request): ResponseInterface
916c1010edaSGreg Roach    {
91757ab2231SGreg Roach        $tree = $request->getAttribute('tree');
918bed27cedSGreg Roach        $xref   = $request->getQueryParams()['xref'];
919bed27cedSGreg Roach        $option = $request->getParsedBody()['option'];
9205a78cd34SGreg Roach
9215a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
9225a78cd34SGreg Roach
9235a78cd34SGreg Roach        if ($source === null) {
92459f2f229SGreg Roach            throw new SourceNotFoundException();
9255a78cd34SGreg Roach        }
9265a78cd34SGreg Roach
9275a78cd34SGreg Roach        $this->addRecordToCart($source);
9285a78cd34SGreg Roach
9295a78cd34SGreg Roach        if ($option === 'linked') {
9305a78cd34SGreg Roach            foreach ($source->linkedIndividuals('SOUR') as $individual) {
9315a78cd34SGreg Roach                $this->addRecordToCart($individual);
9325a78cd34SGreg Roach            }
9335a78cd34SGreg Roach            foreach ($source->linkedFamilies('SOUR') as $family) {
9345a78cd34SGreg Roach                $this->addRecordToCart($family);
9355a78cd34SGreg Roach            }
9365a78cd34SGreg Roach        }
9375a78cd34SGreg Roach
9386ccdf4f0SGreg Roach        return redirect($source->url());
9395a78cd34SGreg Roach    }
9405a78cd34SGreg Roach
9415a78cd34SGreg Roach    /**
9425a78cd34SGreg Roach     * Get all the records in the cart.
9435a78cd34SGreg Roach     *
9445a78cd34SGreg Roach     * @param Tree $tree
9455a78cd34SGreg Roach     *
9465a78cd34SGreg Roach     * @return GedcomRecord[]
9475a78cd34SGreg Roach     */
948c1010edaSGreg Roach    private function allRecordsInCart(Tree $tree): array
949c1010edaSGreg Roach    {
9505a78cd34SGreg Roach        $cart = Session::get('cart', []);
9515a78cd34SGreg Roach
952aa6f03bbSGreg Roach        $xrefs = array_keys($cart[$tree->name()] ?? []);
9535a78cd34SGreg Roach
9545a78cd34SGreg Roach        // Fetch all the records in the cart.
955bed27cedSGreg Roach        $records = array_map(static function (string $xref) use ($tree): ?GedcomRecord {
9565a78cd34SGreg Roach            return GedcomRecord::getInstance($xref, $tree);
9575a78cd34SGreg Roach        }, $xrefs);
9585a78cd34SGreg Roach
9595a78cd34SGreg Roach        // Some records may have been deleted after they were added to the cart.
9605a78cd34SGreg Roach        $records = array_filter($records);
9615a78cd34SGreg Roach
9625a78cd34SGreg Roach        // Group and sort.
9630b5fd0a6SGreg Roach        uasort($records, static function (GedcomRecord $x, GedcomRecord $y): int {
964c156e8f5SGreg Roach            return $x::RECORD_TYPE <=> $y::RECORD_TYPE ?: GedcomRecord::nameComparator()($x, $y);
9655a78cd34SGreg Roach        });
9665a78cd34SGreg Roach
9675a78cd34SGreg Roach        return $records;
9685a78cd34SGreg Roach    }
9695a78cd34SGreg Roach
9705a78cd34SGreg Roach    /**
9715a78cd34SGreg Roach     * Add a record (and direclty linked sources, notes, etc. to the cart.
9725a78cd34SGreg Roach     *
9735a78cd34SGreg Roach     * @param GedcomRecord $record
97418d7a90dSGreg Roach     *
97518d7a90dSGreg Roach     * @return void
9765a78cd34SGreg Roach     */
977e364afe4SGreg Roach    private function addRecordToCart(GedcomRecord $record): void
978c1010edaSGreg Roach    {
9795a78cd34SGreg Roach        $cart = Session::get('cart', []);
9805a78cd34SGreg Roach
981f4afa648SGreg Roach        $tree_name = $record->tree()->name();
9825a78cd34SGreg Roach
9835a78cd34SGreg Roach        // Add this record
984c0935879SGreg Roach        $cart[$tree_name][$record->xref()] = true;
9855a78cd34SGreg Roach
9865a78cd34SGreg Roach        // Add directly linked media, notes, repositories and sources.
9878d0ebef0SGreg Roach        preg_match_all('/\n\d (?:OBJE|NOTE|SOUR|REPO) @(' . Gedcom::REGEX_XREF . ')@/', $record->gedcom(), $matches);
9885a78cd34SGreg Roach
9895a78cd34SGreg Roach        foreach ($matches[1] as $match) {
9905a78cd34SGreg Roach            $cart[$tree_name][$match] = true;
9915a78cd34SGreg Roach        }
9925a78cd34SGreg Roach
9935a78cd34SGreg Roach        Session::put('cart', $cart);
9945a78cd34SGreg Roach    }
9955a78cd34SGreg Roach
9965a78cd34SGreg Roach    /**
9975a78cd34SGreg Roach     * @param Tree $tree
9985a78cd34SGreg Roach     *
9995a78cd34SGreg Roach     * @return bool
10005a78cd34SGreg Roach     */
1001c1010edaSGreg Roach    private function isCartEmpty(Tree $tree): bool
1002c1010edaSGreg Roach    {
10035a78cd34SGreg Roach        $cart = Session::get('cart', []);
10045a78cd34SGreg Roach
1005aa6f03bbSGreg Roach        return empty($cart[$tree->name()]);
10065a78cd34SGreg Roach    }
10078c2e8227SGreg Roach}
1008