xref: /webtrees/app/Module/ClippingsCartModule.php (revision 22d65e5ad7724941da33d875027b68b86648a321)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
206ccdf4f0SGreg Roachuse function app;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
22e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
230bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\FamilyNotFoundException;
240bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualNotFoundException;
250bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\MediaNotFoundException;
260bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\NoteNotFoundException;
270bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\RepositoryNotFoundException;
280bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\SourceNotFoundException;
290e62c4b8SGreg Roachuse Fisharebest\Webtrees\Family;
305a78cd34SGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsExport;
315a78cd34SGreg Roachuse Fisharebest\Webtrees\Gedcom;
320e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
330e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
340e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
355a78cd34SGreg Roachuse Fisharebest\Webtrees\Media;
360e62c4b8SGreg Roachuse Fisharebest\Webtrees\Menu;
375a78cd34SGreg Roachuse Fisharebest\Webtrees\Note;
385a78cd34SGreg Roachuse Fisharebest\Webtrees\Repository;
39e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
400e62c4b8SGreg Roachuse Fisharebest\Webtrees\Session;
415a78cd34SGreg Roachuse Fisharebest\Webtrees\Source;
42aee13b6dSGreg Roachuse Fisharebest\Webtrees\Tree;
435a78cd34SGreg Roachuse League\Flysystem\Filesystem;
445a78cd34SGreg Roachuse League\Flysystem\ZipArchive\ZipArchiveAdapter;
456ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
466ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
476ccdf4f0SGreg Roachuse Psr\Http\Message\StreamFactoryInterface;
48e5a6b4d4SGreg Roachuse function str_replace;
498c2e8227SGreg Roach
508c2e8227SGreg Roach/**
518c2e8227SGreg Roach * Class ClippingsCartModule
528c2e8227SGreg Roach */
5337eb8894SGreg Roachclass ClippingsCartModule extends AbstractModule implements ModuleMenuInterface
54c1010edaSGreg Roach{
5549a243cbSGreg Roach    use ModuleMenuTrait;
5649a243cbSGreg Roach
575a78cd34SGreg Roach    // Routes that have a record which can be added to the clipboard
5816d6367aSGreg Roach    private const ROUTES_WITH_RECORDS = [
59c1010edaSGreg Roach        'family',
60c1010edaSGreg Roach        'individual',
61c1010edaSGreg Roach        'media',
62c1010edaSGreg Roach        'note',
63c1010edaSGreg Roach        'repository',
64c1010edaSGreg Roach        'source',
65c1010edaSGreg Roach    ];
665a78cd34SGreg Roach
6749a243cbSGreg Roach    /** @var int The default access level for this module.  It can be changed in the control panel. */
6849a243cbSGreg Roach    protected $access_level = Auth::PRIV_USER;
6949a243cbSGreg Roach
70961ec755SGreg Roach    /**
71e5a6b4d4SGreg Roach     * @var UserService
72e5a6b4d4SGreg Roach     */
73e5a6b4d4SGreg Roach    private $user_service;
74e5a6b4d4SGreg Roach
75e5a6b4d4SGreg Roach    /**
76e5a6b4d4SGreg Roach     * ClippingsCartModule constructor.
77e5a6b4d4SGreg Roach     *
78e5a6b4d4SGreg Roach     * @param UserService $user_service
79e5a6b4d4SGreg Roach     */
80e5a6b4d4SGreg Roach    public function __construct(UserService $user_service)
81e5a6b4d4SGreg Roach    {
82e5a6b4d4SGreg Roach        $this->user_service = $user_service;
83e5a6b4d4SGreg Roach    }
84e5a6b4d4SGreg Roach
85e5a6b4d4SGreg Roach    /**
860cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
87961ec755SGreg Roach     *
88961ec755SGreg Roach     * @return string
89961ec755SGreg Roach     */
9049a243cbSGreg Roach    public function title(): string
91c1010edaSGreg Roach    {
92bbb76c12SGreg Roach        /* I18N: Name of a module */
93bbb76c12SGreg Roach        return I18N::translate('Clippings cart');
948c2e8227SGreg Roach    }
958c2e8227SGreg Roach
96961ec755SGreg Roach    /**
97961ec755SGreg Roach     * A sentence describing what this module does.
98961ec755SGreg Roach     *
99961ec755SGreg Roach     * @return string
100961ec755SGreg Roach     */
10149a243cbSGreg Roach    public function description(): string
102c1010edaSGreg Roach    {
103bbb76c12SGreg Roach        /* I18N: Description of the “Clippings cart” module */
104bbb76c12SGreg Roach        return I18N::translate('Select records from your family tree and save them as a GEDCOM file.');
1058c2e8227SGreg Roach    }
1068c2e8227SGreg Roach
1070ee13198SGreg Roach    /**
10849a243cbSGreg Roach     * The default position for this menu.  It can be changed in the control panel.
1090ee13198SGreg Roach     *
1100ee13198SGreg Roach     * @return int
1110ee13198SGreg Roach     */
1128f53f488SRico Sonntag    public function defaultMenuOrder(): int
113c1010edaSGreg Roach    {
114353b36abSGreg Roach        return 6;
1158c2e8227SGreg Roach    }
1168c2e8227SGreg Roach
1170ee13198SGreg Roach    /**
1180ee13198SGreg Roach     * A menu, to be added to the main application menu.
1190ee13198SGreg Roach     *
120aee13b6dSGreg Roach     * @param Tree $tree
121aee13b6dSGreg Roach     *
1220ee13198SGreg Roach     * @return Menu|null
1230ee13198SGreg Roach     */
12446295629SGreg Roach    public function getMenu(Tree $tree): ?Menu
125c1010edaSGreg Roach    {
1266ccdf4f0SGreg Roach        $request = app(ServerRequestInterface::class);
1278c2e8227SGreg Roach
1289e648e55SGreg Roach        $route = $request->get('route', '');
1295a78cd34SGreg Roach
1305a78cd34SGreg Roach        $submenus = [
13149a243cbSGreg Roach            new Menu($this->title(), route('module', [
13226684e68SGreg Roach                'module' => $this->name(),
133c1010edaSGreg Roach                'action' => 'Show',
134aa6f03bbSGreg Roach                'ged'    => $tree->name(),
135c1010edaSGreg Roach            ]), 'menu-clippings-cart', ['rel' => 'nofollow']),
1365a78cd34SGreg Roach        ];
1375a78cd34SGreg Roach
138*22d65e5aSGreg Roach        if (in_array($route, self::ROUTES_WITH_RECORDS, true)) {
1399e648e55SGreg Roach            $xref      = $request->get('xref', '');
1405a78cd34SGreg Roach            $action    = 'Add' . ucfirst($route);
141c1010edaSGreg Roach            $add_route = route('module', [
14226684e68SGreg Roach                'module' => $this->name(),
143c1010edaSGreg Roach                'action' => $action,
144c1010edaSGreg Roach                'xref'   => $xref,
145aa6f03bbSGreg Roach                'ged'    => $tree->name(),
146c1010edaSGreg Roach            ]);
1475a78cd34SGreg Roach
14825b2dde3SGreg Roach            $submenus[] = new Menu(I18N::translate('Add to the clippings cart'), $add_route, 'menu-clippings-add', ['rel' => 'nofollow']);
1498c2e8227SGreg Roach        }
150cbc1590aSGreg Roach
1515a78cd34SGreg Roach        if (!$this->isCartEmpty($tree)) {
152c1010edaSGreg Roach            $submenus[] = new Menu(I18N::translate('Empty the clippings cart'), route('module', [
15326684e68SGreg Roach                'module' => $this->name(),
154c1010edaSGreg Roach                'action' => 'Empty',
155aa6f03bbSGreg Roach                'ged'    => $tree->name(),
156c1010edaSGreg Roach            ]), 'menu-clippings-empty', ['rel' => 'nofollow']);
157c1010edaSGreg Roach            $submenus[] = new Menu(I18N::translate('Download'), route('module', [
15826684e68SGreg Roach                'module' => $this->name(),
159c1010edaSGreg Roach                'action' => 'DownloadForm',
160aa6f03bbSGreg Roach                'ged'    => $tree->name(),
161c1010edaSGreg Roach            ]), 'menu-clippings-download', ['rel' => 'nofollow']);
1625a78cd34SGreg Roach        }
1635a78cd34SGreg Roach
16449a243cbSGreg Roach        return new Menu($this->title(), '#', 'menu-clippings', ['rel' => 'nofollow'], $submenus);
1658c2e8227SGreg Roach    }
1668c2e8227SGreg Roach
16776692c8bSGreg Roach    /**
1686ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
169b6db7c1fSGreg Roach     * @param Tree                   $tree
17076692c8bSGreg Roach     *
1716ccdf4f0SGreg Roach     * @return ResponseInterface
17276692c8bSGreg Roach     */
1736ccdf4f0SGreg Roach    public function getDownloadAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
174c1010edaSGreg Roach    {
1759e648e55SGreg Roach        $privatize_export = $request->get('privatize_export', '');
1765a78cd34SGreg Roach        $convert          = (bool) $request->get('convert');
1778c2e8227SGreg Roach
17813abd6f3SGreg Roach        $cart = Session::get('cart', []);
1798c2e8227SGreg Roach
180aa6f03bbSGreg Roach        $xrefs = array_keys($cart[$tree->name()] ?? []);
1815a78cd34SGreg Roach
1825a78cd34SGreg Roach        // Create a new/empty .ZIP file
1835a78cd34SGreg Roach        $temp_zip_file  = tempnam(sys_get_temp_dir(), 'webtrees-zip-');
1845a78cd34SGreg Roach        $zip_filesystem = new Filesystem(new ZipArchiveAdapter($temp_zip_file));
1855a78cd34SGreg Roach
1865a78cd34SGreg Roach        // Media file prefix
1875a78cd34SGreg Roach        $path = $tree->getPreference('MEDIA_DIRECTORY');
1885a78cd34SGreg Roach
1895a78cd34SGreg Roach        // GEDCOM file header
190a3d8780cSGreg Roach        $filetext = FunctionsExport::gedcomHeader($tree, $convert ? 'ANSI' : 'UTF-8');
1915a78cd34SGreg Roach
1925a78cd34SGreg Roach        switch ($privatize_export) {
1935a78cd34SGreg Roach            case 'gedadmin':
1945a78cd34SGreg Roach                $access_level = Auth::PRIV_NONE;
1955a78cd34SGreg Roach                break;
1965a78cd34SGreg Roach            case 'user':
1975a78cd34SGreg Roach                $access_level = Auth::PRIV_USER;
1985a78cd34SGreg Roach                break;
1995a78cd34SGreg Roach            case 'visitor':
2005a78cd34SGreg Roach                $access_level = Auth::PRIV_PRIVATE;
2015a78cd34SGreg Roach                break;
2025a78cd34SGreg Roach            case 'none':
2035a78cd34SGreg Roach            default:
2045a78cd34SGreg Roach                $access_level = Auth::PRIV_HIDE;
2055a78cd34SGreg Roach                break;
2065a78cd34SGreg Roach        }
2075a78cd34SGreg Roach
2085a78cd34SGreg Roach        foreach ($xrefs as $xref) {
2095a78cd34SGreg Roach            $object = GedcomRecord::getInstance($xref, $tree);
2105a78cd34SGreg Roach            // The object may have been deleted since we added it to the cart....
2115a78cd34SGreg Roach            if ($object) {
2125a78cd34SGreg Roach                $record = $object->privatizeGedcom($access_level);
2135a78cd34SGreg Roach                // Remove links to objects that aren't in the cart
2148d0ebef0SGreg Roach                preg_match_all('/\n1 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[2-9].*)*/', $record, $matches, PREG_SET_ORDER);
2155a78cd34SGreg Roach                foreach ($matches as $match) {
2165a78cd34SGreg Roach                    if (!array_key_exists($match[1], $xrefs)) {
2175a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2185a78cd34SGreg Roach                    }
2195a78cd34SGreg Roach                }
2208d0ebef0SGreg Roach                preg_match_all('/\n2 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[3-9].*)*/', $record, $matches, PREG_SET_ORDER);
2215a78cd34SGreg Roach                foreach ($matches as $match) {
2225a78cd34SGreg Roach                    if (!array_key_exists($match[1], $xrefs)) {
2235a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2245a78cd34SGreg Roach                    }
2255a78cd34SGreg Roach                }
2268d0ebef0SGreg Roach                preg_match_all('/\n3 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[4-9].*)*/', $record, $matches, PREG_SET_ORDER);
2275a78cd34SGreg Roach                foreach ($matches as $match) {
2285a78cd34SGreg Roach                    if (!array_key_exists($match[1], $xrefs)) {
2295a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2305a78cd34SGreg Roach                    }
2315a78cd34SGreg Roach                }
2325a78cd34SGreg Roach
23355167344SGreg Roach                if ($object instanceof Individual || $object instanceof Family) {
2345a78cd34SGreg Roach                    $filetext .= $record . "\n";
2355a78cd34SGreg Roach                    $filetext .= "1 SOUR @WEBTREES@\n";
2361f273236SGreg Roach                    $filetext .= '2 PAGE ' . $object->url() . "\n";
23755167344SGreg Roach                } elseif ($object instanceof Source) {
2385a78cd34SGreg Roach                    $filetext .= $record . "\n";
2391f273236SGreg Roach                    $filetext .= '1 NOTE ' . $object->url() . "\n";
24055167344SGreg Roach                } elseif ($object instanceof Media) {
24155167344SGreg Roach                    // Add the media files to the archive
2425a78cd34SGreg Roach                    foreach ($object->mediaFiles() as $media_file) {
2435a78cd34SGreg Roach                        if (file_exists($media_file->getServerFilename())) {
244e364afe4SGreg Roach                            $fp = fopen($media_file->getServerFilename(), 'rb');
2455a78cd34SGreg Roach                            $zip_filesystem->writeStream($path . $media_file->filename(), $fp);
2465a78cd34SGreg Roach                            fclose($fp);
2475a78cd34SGreg Roach                        }
2485a78cd34SGreg Roach                    }
2495a78cd34SGreg Roach                    $filetext .= $record . "\n";
25055167344SGreg Roach                } else {
2515a78cd34SGreg Roach                    $filetext .= $record . "\n";
2528c2e8227SGreg Roach                }
2538c2e8227SGreg Roach            }
2548c2e8227SGreg Roach        }
2558c2e8227SGreg Roach
2565a78cd34SGreg Roach        // Create a source, to indicate the source of the data.
2575a78cd34SGreg Roach        $filetext .= "0 @WEBTREES@ SOUR\n1 TITL " . WT_BASE_URL . "\n";
258e5a6b4d4SGreg Roach        $author   = $this->user_service->find((int) $tree->getPreference('CONTACT_USER_ID'));
2595a78cd34SGreg Roach        if ($author !== null) {
260e5a6b4d4SGreg Roach            $filetext .= '1 AUTH ' . $author->realName() . "\n";
2615a78cd34SGreg Roach        }
2625a78cd34SGreg Roach        $filetext .= "0 TRLR\n";
2635a78cd34SGreg Roach
2645a78cd34SGreg Roach        // Make sure the preferred line endings are used
265a3d8780cSGreg Roach        $filetext = str_replace('\n', Gedcom::EOL, $filetext);
2665a78cd34SGreg Roach
26755167344SGreg Roach        if ($convert) {
2685a78cd34SGreg Roach            $filetext = utf8_decode($filetext);
2698c2e8227SGreg Roach        }
270cbc1590aSGreg Roach
2715a78cd34SGreg Roach        // Finally add the GEDCOM file to the .ZIP file.
2725a78cd34SGreg Roach        $zip_filesystem->write('clippings.ged', $filetext);
2735a78cd34SGreg Roach
2745a78cd34SGreg Roach        // Need to force-close the filesystem
27502a92f80SGreg Roach        unset($zip_filesystem);
2765a78cd34SGreg Roach
2776ccdf4f0SGreg Roach        // Use a stream, so that we do not have to load the entire file into memory.
2786ccdf4f0SGreg Roach        $stream = app(StreamFactoryInterface::class)->createStreamFromFile($temp_zip_file);
2795a78cd34SGreg Roach
2806ccdf4f0SGreg Roach        return response()
2816ccdf4f0SGreg Roach            ->withBody($stream)
2821b3d4731SGreg Roach            ->withHeader('Content-Type', 'application/zip')
2831b3d4731SGreg Roach            ->withHeader('Content-Tisposition', 'attachment; filename="clippings.zip');
2848c2e8227SGreg Roach    }
2858c2e8227SGreg Roach
2868c2e8227SGreg Roach    /**
287b6db7c1fSGreg Roach     * @param Tree          $tree
288e5a6b4d4SGreg Roach     * @param UserInterface $user
28976692c8bSGreg Roach     *
2906ccdf4f0SGreg Roach     * @return ResponseInterface
2918c2e8227SGreg Roach     */
2926ccdf4f0SGreg Roach    public function getDownloadFormAction(Tree $tree, UserInterface $user): ResponseInterface
293c1010edaSGreg Roach    {
2945a78cd34SGreg Roach        $title = I18N::translate('Family tree clippings cart') . ' — ' . I18N::translate('Download');
2958c2e8227SGreg Roach
2965a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/download', [
2975a78cd34SGreg Roach            'is_manager' => Auth::isManager($tree, $user),
2985a78cd34SGreg Roach            'is_member'  => Auth::isMember($tree, $user),
2995a78cd34SGreg Roach            'title'      => $title,
3005a78cd34SGreg Roach        ]);
3018c2e8227SGreg Roach    }
3028c2e8227SGreg Roach
3035a78cd34SGreg Roach    /**
304b6db7c1fSGreg Roach     * @param Tree $tree
3055a78cd34SGreg Roach     *
3066ccdf4f0SGreg Roach     * @return ResponseInterface
3075a78cd34SGreg Roach     */
3086ccdf4f0SGreg Roach    public function getEmptyAction(Tree $tree): ResponseInterface
309c1010edaSGreg Roach    {
3105a78cd34SGreg Roach        $cart                = Session::get('cart', []);
311aa6f03bbSGreg Roach        $cart[$tree->name()] = [];
3125a78cd34SGreg Roach        Session::put('cart', $cart);
3138c2e8227SGreg Roach
314c1010edaSGreg Roach        $url = route('module', [
31526684e68SGreg Roach            'module' => $this->name(),
316c1010edaSGreg Roach            'action' => 'Show',
317aa6f03bbSGreg Roach            'ged'    => $tree->name(),
318c1010edaSGreg Roach        ]);
3195a78cd34SGreg Roach
3206ccdf4f0SGreg Roach        return redirect($url);
3215a78cd34SGreg Roach    }
3225a78cd34SGreg Roach
3235a78cd34SGreg Roach    /**
3246ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
325b6db7c1fSGreg Roach     * @param Tree                   $tree
3265a78cd34SGreg Roach     *
3276ccdf4f0SGreg Roach     * @return ResponseInterface
3285a78cd34SGreg Roach     */
3296ccdf4f0SGreg Roach    public function postRemoveAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
330c1010edaSGreg Roach    {
3319e648e55SGreg Roach        $xref = $request->get('xref', '');
3325a78cd34SGreg Roach
3335a78cd34SGreg Roach        $cart = Session::get('cart', []);
334aa6f03bbSGreg Roach        unset($cart[$tree->name()][$xref]);
3355a78cd34SGreg Roach        Session::put('cart', $cart);
3365a78cd34SGreg Roach
337c1010edaSGreg Roach        $url = route('module', [
33826684e68SGreg Roach            'module' => $this->name(),
339c1010edaSGreg Roach            'action' => 'Show',
340aa6f03bbSGreg Roach            'ged'    => $tree->name(),
341c1010edaSGreg Roach        ]);
3425a78cd34SGreg Roach
3436ccdf4f0SGreg Roach        return redirect($url);
3445a78cd34SGreg Roach    }
3455a78cd34SGreg Roach
3465a78cd34SGreg Roach    /**
347b6db7c1fSGreg Roach     * @param Tree $tree
3485a78cd34SGreg Roach     *
3496ccdf4f0SGreg Roach     * @return ResponseInterface
3505a78cd34SGreg Roach     */
3516ccdf4f0SGreg Roach    public function getShowAction(Tree $tree): ResponseInterface
352c1010edaSGreg Roach    {
3535a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/show', [
3545a78cd34SGreg Roach            'records' => $this->allRecordsInCart($tree),
3555a78cd34SGreg Roach            'title'   => I18N::translate('Family tree clippings cart'),
3565a78cd34SGreg Roach            'tree'    => $tree,
3575a78cd34SGreg Roach        ]);
3585a78cd34SGreg Roach    }
3595a78cd34SGreg Roach
3605a78cd34SGreg Roach    /**
3616ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
362b6db7c1fSGreg Roach     * @param Tree                   $tree
3635a78cd34SGreg Roach     *
3646ccdf4f0SGreg Roach     * @return ResponseInterface
3655a78cd34SGreg Roach     */
3666ccdf4f0SGreg Roach    public function getAddFamilyAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
367c1010edaSGreg Roach    {
3689e648e55SGreg Roach        $xref = $request->get('xref', '');
3695a78cd34SGreg Roach
3705a78cd34SGreg Roach        $family = Family::getInstance($xref, $tree);
3715a78cd34SGreg Roach
3725a78cd34SGreg Roach        if ($family === null) {
37359f2f229SGreg Roach            throw new FamilyNotFoundException();
3745a78cd34SGreg Roach        }
3755a78cd34SGreg Roach
3765a78cd34SGreg Roach        $options = $this->familyOptions($family);
3775a78cd34SGreg Roach
37839ca88baSGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $family->fullName());
3795a78cd34SGreg Roach
3805a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
3815a78cd34SGreg Roach            'options' => $options,
3825a78cd34SGreg Roach            'default' => key($options),
3835a78cd34SGreg Roach            'record'  => $family,
3845a78cd34SGreg Roach            'title'   => $title,
3855a78cd34SGreg Roach            'tree'    => $tree,
3865a78cd34SGreg Roach        ]);
3875a78cd34SGreg Roach    }
3885a78cd34SGreg Roach
3895a78cd34SGreg Roach    /**
3905a78cd34SGreg Roach     * @param Family $family
3915a78cd34SGreg Roach     *
3925a78cd34SGreg Roach     * @return string[]
3935a78cd34SGreg Roach     */
394c1010edaSGreg Roach    private function familyOptions(Family $family): array
395c1010edaSGreg Roach    {
39639ca88baSGreg Roach        $name = strip_tags($family->fullName());
3975a78cd34SGreg Roach
3985a78cd34SGreg Roach        return [
3995a78cd34SGreg Roach            'parents'     => $name,
400bbb76c12SGreg Roach            /* I18N: %s is a family (husband + wife) */
401bbb76c12SGreg Roach            'members'     => I18N::translate('%s and their children', $name),
402bbb76c12SGreg Roach            /* I18N: %s is a family (husband + wife) */
403bbb76c12SGreg Roach            'descendants' => I18N::translate('%s and their descendants', $name),
4045a78cd34SGreg Roach        ];
4055a78cd34SGreg Roach    }
4065a78cd34SGreg Roach
4075a78cd34SGreg Roach    /**
4086ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
409b6db7c1fSGreg Roach     * @param Tree                   $tree
4105a78cd34SGreg Roach     *
4116ccdf4f0SGreg Roach     * @return ResponseInterface
4125a78cd34SGreg Roach     */
4136ccdf4f0SGreg Roach    public function postAddFamilyAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
414c1010edaSGreg Roach    {
4159e648e55SGreg Roach        $xref   = $request->get('xref', '');
4169e648e55SGreg Roach        $option = $request->get('option', '');
4175a78cd34SGreg Roach
4185a78cd34SGreg Roach        $family = Family::getInstance($xref, $tree);
4195a78cd34SGreg Roach
4205a78cd34SGreg Roach        if ($family === null) {
42159f2f229SGreg Roach            throw new FamilyNotFoundException();
4225a78cd34SGreg Roach        }
4235a78cd34SGreg Roach
4245a78cd34SGreg Roach        switch ($option) {
4255a78cd34SGreg Roach            case 'parents':
4265a78cd34SGreg Roach                $this->addFamilyToCart($family);
4275a78cd34SGreg Roach                break;
4285a78cd34SGreg Roach
4295a78cd34SGreg Roach            case 'members':
4305a78cd34SGreg Roach                $this->addFamilyAndChildrenToCart($family);
4315a78cd34SGreg Roach                break;
4325a78cd34SGreg Roach
4335a78cd34SGreg Roach            case 'descendants':
4345a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($family);
4355a78cd34SGreg Roach                break;
4365a78cd34SGreg Roach        }
4375a78cd34SGreg Roach
4386ccdf4f0SGreg Roach        return redirect($family->url());
4395a78cd34SGreg Roach    }
4405a78cd34SGreg Roach
4415a78cd34SGreg Roach    /**
4425a78cd34SGreg Roach     * @param Family $family
44318d7a90dSGreg Roach     *
44418d7a90dSGreg Roach     * @return void
4455a78cd34SGreg Roach     */
446e364afe4SGreg Roach    private function addFamilyToCart(Family $family): void
447c1010edaSGreg Roach    {
4485a78cd34SGreg Roach        $this->addRecordToCart($family);
4495a78cd34SGreg Roach
45039ca88baSGreg Roach        foreach ($family->spouses() as $spouse) {
4515a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4525a78cd34SGreg Roach        }
4535a78cd34SGreg Roach    }
4545a78cd34SGreg Roach
4555a78cd34SGreg Roach    /**
4565a78cd34SGreg Roach     * @param Family $family
45718d7a90dSGreg Roach     *
45818d7a90dSGreg Roach     * @return void
4595a78cd34SGreg Roach     */
460e364afe4SGreg Roach    private function addFamilyAndChildrenToCart(Family $family): void
461c1010edaSGreg Roach    {
4625a78cd34SGreg Roach        $this->addRecordToCart($family);
4635a78cd34SGreg Roach
46439ca88baSGreg Roach        foreach ($family->spouses() as $spouse) {
4655a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4665a78cd34SGreg Roach        }
46739ca88baSGreg Roach        foreach ($family->children() as $child) {
4685a78cd34SGreg Roach            $this->addRecordToCart($child);
4695a78cd34SGreg Roach        }
4705a78cd34SGreg Roach    }
4715a78cd34SGreg Roach
4725a78cd34SGreg Roach    /**
4735a78cd34SGreg Roach     * @param Family $family
47418d7a90dSGreg Roach     *
47518d7a90dSGreg Roach     * @return void
4765a78cd34SGreg Roach     */
477e364afe4SGreg Roach    private function addFamilyAndDescendantsToCart(Family $family): void
478c1010edaSGreg Roach    {
4795a78cd34SGreg Roach        $this->addRecordToCart($family);
4805a78cd34SGreg Roach
48139ca88baSGreg Roach        foreach ($family->spouses() as $spouse) {
4825a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4835a78cd34SGreg Roach        }
48439ca88baSGreg Roach        foreach ($family->children() as $child) {
4855a78cd34SGreg Roach            $this->addRecordToCart($child);
48639ca88baSGreg Roach            foreach ($child->spouseFamilies() as $child_family) {
4875a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($child_family);
4885a78cd34SGreg Roach            }
4895a78cd34SGreg Roach        }
4905a78cd34SGreg Roach    }
4915a78cd34SGreg Roach
4925a78cd34SGreg Roach    /**
4936ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
494b6db7c1fSGreg Roach     * @param Tree                   $tree
4955a78cd34SGreg Roach     *
4966ccdf4f0SGreg Roach     * @return ResponseInterface
4975a78cd34SGreg Roach     */
4986ccdf4f0SGreg Roach    public function getAddIndividualAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
499c1010edaSGreg Roach    {
5009e648e55SGreg Roach        $xref = $request->get('xref', '');
5015a78cd34SGreg Roach
5025a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
5035a78cd34SGreg Roach
5045a78cd34SGreg Roach        if ($individual === null) {
50559f2f229SGreg Roach            throw new IndividualNotFoundException();
5065a78cd34SGreg Roach        }
5075a78cd34SGreg Roach
5085a78cd34SGreg Roach        $options = $this->individualOptions($individual);
5095a78cd34SGreg Roach
51039ca88baSGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $individual->fullName());
5115a78cd34SGreg Roach
5125a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
5135a78cd34SGreg Roach            'options' => $options,
5145a78cd34SGreg Roach            'default' => key($options),
5155a78cd34SGreg Roach            'record'  => $individual,
5165a78cd34SGreg Roach            'title'   => $title,
5175a78cd34SGreg Roach            'tree'    => $tree,
5185a78cd34SGreg Roach        ]);
5195a78cd34SGreg Roach    }
5205a78cd34SGreg Roach
5215a78cd34SGreg Roach    /**
5225a78cd34SGreg Roach     * @param Individual $individual
5235a78cd34SGreg Roach     *
5245a78cd34SGreg Roach     * @return string[]
5255a78cd34SGreg Roach     */
526c1010edaSGreg Roach    private function individualOptions(Individual $individual): array
527c1010edaSGreg Roach    {
52839ca88baSGreg Roach        $name = strip_tags($individual->fullName());
5295a78cd34SGreg Roach
53039ca88baSGreg Roach        if ($individual->sex() === 'F') {
5315a78cd34SGreg Roach            return [
5325a78cd34SGreg Roach                'self'              => $name,
5335a78cd34SGreg Roach                'parents'           => I18N::translate('%s, her parents and siblings', $name),
5345a78cd34SGreg Roach                'spouses'           => I18N::translate('%s, her spouses and children', $name),
5355a78cd34SGreg Roach                'ancestors'         => I18N::translate('%s and her ancestors', $name),
5365a78cd34SGreg Roach                'ancestor_families' => I18N::translate('%s, her ancestors and their families', $name),
5375a78cd34SGreg Roach                'descendants'       => I18N::translate('%s, her spouses and descendants', $name),
5385a78cd34SGreg Roach            ];
539b2ce94c6SRico Sonntag        }
540b2ce94c6SRico Sonntag
5415a78cd34SGreg Roach        return [
5425a78cd34SGreg Roach            'self'              => $name,
5435a78cd34SGreg Roach            'parents'           => I18N::translate('%s, his parents and siblings', $name),
5445a78cd34SGreg Roach            'spouses'           => I18N::translate('%s, his spouses and children', $name),
5455a78cd34SGreg Roach            'ancestors'         => I18N::translate('%s and his ancestors', $name),
5465a78cd34SGreg Roach            'ancestor_families' => I18N::translate('%s, his ancestors and their families', $name),
5475a78cd34SGreg Roach            'descendants'       => I18N::translate('%s, his spouses and descendants', $name),
5485a78cd34SGreg Roach        ];
5495a78cd34SGreg Roach    }
5505a78cd34SGreg Roach
5515a78cd34SGreg Roach    /**
5526ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
553b6db7c1fSGreg Roach     * @param Tree                   $tree
5545a78cd34SGreg Roach     *
5556ccdf4f0SGreg Roach     * @return ResponseInterface
5565a78cd34SGreg Roach     */
5576ccdf4f0SGreg Roach    public function postAddIndividualAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
558c1010edaSGreg Roach    {
5599e648e55SGreg Roach        $xref   = $request->get('xref', '');
5609e648e55SGreg Roach        $option = $request->get('option', '');
5615a78cd34SGreg Roach
5625a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
5635a78cd34SGreg Roach
5645a78cd34SGreg Roach        if ($individual === null) {
56559f2f229SGreg Roach            throw new IndividualNotFoundException();
5665a78cd34SGreg Roach        }
5675a78cd34SGreg Roach
5685a78cd34SGreg Roach        switch ($option) {
5695a78cd34SGreg Roach            case 'self':
5705a78cd34SGreg Roach                $this->addRecordToCart($individual);
5715a78cd34SGreg Roach                break;
5725a78cd34SGreg Roach
5735a78cd34SGreg Roach            case 'parents':
57439ca88baSGreg Roach                foreach ($individual->childFamilies() as $family) {
5755a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
5765a78cd34SGreg Roach                }
5775a78cd34SGreg Roach                break;
5785a78cd34SGreg Roach
5795a78cd34SGreg Roach            case 'spouses':
58039ca88baSGreg Roach                foreach ($individual->spouseFamilies() as $family) {
5815a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
5825a78cd34SGreg Roach                }
5835a78cd34SGreg Roach                break;
5845a78cd34SGreg Roach
5855a78cd34SGreg Roach            case 'ancestors':
5865a78cd34SGreg Roach                $this->addAncestorsToCart($individual);
5875a78cd34SGreg Roach                break;
5885a78cd34SGreg Roach
5895a78cd34SGreg Roach            case 'ancestor_families':
5905a78cd34SGreg Roach                $this->addAncestorFamiliesToCart($individual);
5915a78cd34SGreg Roach                break;
5925a78cd34SGreg Roach
5935a78cd34SGreg Roach            case 'descendants':
59439ca88baSGreg Roach                foreach ($individual->spouseFamilies() as $family) {
5955a78cd34SGreg Roach                    $this->addFamilyAndDescendantsToCart($family);
5965a78cd34SGreg Roach                }
5975a78cd34SGreg Roach                break;
5985a78cd34SGreg Roach        }
5995a78cd34SGreg Roach
6006ccdf4f0SGreg Roach        return redirect($individual->url());
6015a78cd34SGreg Roach    }
6025a78cd34SGreg Roach
6035a78cd34SGreg Roach    /**
6045a78cd34SGreg Roach     * @param Individual $individual
60518d7a90dSGreg Roach     *
60618d7a90dSGreg Roach     * @return void
6075a78cd34SGreg Roach     */
608e364afe4SGreg Roach    private function addAncestorsToCart(Individual $individual): void
609c1010edaSGreg Roach    {
6105a78cd34SGreg Roach        $this->addRecordToCart($individual);
6115a78cd34SGreg Roach
61239ca88baSGreg Roach        foreach ($individual->childFamilies() as $family) {
61339ca88baSGreg Roach            foreach ($family->spouses() as $parent) {
6145a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
6155a78cd34SGreg Roach            }
6165a78cd34SGreg Roach        }
6175a78cd34SGreg Roach    }
6185a78cd34SGreg Roach
6195a78cd34SGreg Roach    /**
6205a78cd34SGreg Roach     * @param Individual $individual
62118d7a90dSGreg Roach     *
62218d7a90dSGreg Roach     * @return void
6235a78cd34SGreg Roach     */
624e364afe4SGreg Roach    private function addAncestorFamiliesToCart(Individual $individual): void
625c1010edaSGreg Roach    {
62639ca88baSGreg Roach        foreach ($individual->childFamilies() as $family) {
6275a78cd34SGreg Roach            $this->addFamilyAndChildrenToCart($family);
62839ca88baSGreg Roach            foreach ($family->spouses() as $parent) {
6295a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
6305a78cd34SGreg Roach            }
6315a78cd34SGreg Roach        }
6325a78cd34SGreg Roach    }
6335a78cd34SGreg Roach
6345a78cd34SGreg Roach    /**
6356ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
636b6db7c1fSGreg Roach     * @param Tree                   $tree
6375a78cd34SGreg Roach     *
6386ccdf4f0SGreg Roach     * @return ResponseInterface
6395a78cd34SGreg Roach     */
6406ccdf4f0SGreg Roach    public function getAddMediaAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
641c1010edaSGreg Roach    {
6429e648e55SGreg Roach        $xref = $request->get('xref', '');
6435a78cd34SGreg Roach
6445a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
6455a78cd34SGreg Roach
6465a78cd34SGreg Roach        if ($media === null) {
64759f2f229SGreg Roach            throw new MediaNotFoundException();
6485a78cd34SGreg Roach        }
6495a78cd34SGreg Roach
6505a78cd34SGreg Roach        $options = $this->mediaOptions($media);
6515a78cd34SGreg Roach
65239ca88baSGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $media->fullName());
6535a78cd34SGreg Roach
6545a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
6555a78cd34SGreg Roach            'options' => $options,
6565a78cd34SGreg Roach            'default' => key($options),
6575a78cd34SGreg Roach            'record'  => $media,
6585a78cd34SGreg Roach            'title'   => $title,
6595a78cd34SGreg Roach            'tree'    => $tree,
6605a78cd34SGreg Roach        ]);
6615a78cd34SGreg Roach    }
6625a78cd34SGreg Roach
6635a78cd34SGreg Roach    /**
6645a78cd34SGreg Roach     * @param Media $media
6655a78cd34SGreg Roach     *
6665a78cd34SGreg Roach     * @return string[]
6675a78cd34SGreg Roach     */
668c1010edaSGreg Roach    private function mediaOptions(Media $media): array
669c1010edaSGreg Roach    {
67039ca88baSGreg Roach        $name = strip_tags($media->fullName());
6715a78cd34SGreg Roach
6725a78cd34SGreg Roach        return [
6735a78cd34SGreg Roach            'self' => $name,
6745a78cd34SGreg Roach        ];
6755a78cd34SGreg Roach    }
6765a78cd34SGreg Roach
6775a78cd34SGreg Roach    /**
6786ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
679b6db7c1fSGreg Roach     * @param Tree                   $tree
6805a78cd34SGreg Roach     *
6816ccdf4f0SGreg Roach     * @return ResponseInterface
6825a78cd34SGreg Roach     */
6836ccdf4f0SGreg Roach    public function postAddMediaAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
684c1010edaSGreg Roach    {
6859e648e55SGreg Roach        $xref = $request->get('xref', '');
6865a78cd34SGreg Roach
6875a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
6885a78cd34SGreg Roach
6895a78cd34SGreg Roach        if ($media === null) {
69059f2f229SGreg Roach            throw new MediaNotFoundException();
6915a78cd34SGreg Roach        }
6925a78cd34SGreg Roach
6935a78cd34SGreg Roach        $this->addRecordToCart($media);
6945a78cd34SGreg Roach
6956ccdf4f0SGreg Roach        return redirect($media->url());
6965a78cd34SGreg Roach    }
6975a78cd34SGreg Roach
6985a78cd34SGreg Roach    /**
6996ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
700b6db7c1fSGreg Roach     * @param Tree                   $tree
7015a78cd34SGreg Roach     *
7026ccdf4f0SGreg Roach     * @return ResponseInterface
7035a78cd34SGreg Roach     */
7046ccdf4f0SGreg Roach    public function getAddNoteAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
705c1010edaSGreg Roach    {
7069e648e55SGreg Roach        $xref = $request->get('xref', '');
7075a78cd34SGreg Roach
7085a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
7095a78cd34SGreg Roach
7105a78cd34SGreg Roach        if ($note === null) {
71159f2f229SGreg Roach            throw new NoteNotFoundException();
7125a78cd34SGreg Roach        }
7135a78cd34SGreg Roach
7145a78cd34SGreg Roach        $options = $this->noteOptions($note);
7155a78cd34SGreg Roach
71639ca88baSGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $note->fullName());
7175a78cd34SGreg Roach
7185a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
7195a78cd34SGreg Roach            'options' => $options,
7205a78cd34SGreg Roach            'default' => key($options),
7215a78cd34SGreg Roach            'record'  => $note,
7225a78cd34SGreg Roach            'title'   => $title,
7235a78cd34SGreg Roach            'tree'    => $tree,
7245a78cd34SGreg Roach        ]);
7255a78cd34SGreg Roach    }
7265a78cd34SGreg Roach
7275a78cd34SGreg Roach    /**
7285a78cd34SGreg Roach     * @param Note $note
7295a78cd34SGreg Roach     *
7305a78cd34SGreg Roach     * @return string[]
7315a78cd34SGreg Roach     */
732c1010edaSGreg Roach    private function noteOptions(Note $note): array
733c1010edaSGreg Roach    {
73439ca88baSGreg Roach        $name = strip_tags($note->fullName());
7355a78cd34SGreg Roach
7365a78cd34SGreg Roach        return [
7375a78cd34SGreg Roach            'self' => $name,
7385a78cd34SGreg Roach        ];
7395a78cd34SGreg Roach    }
7405a78cd34SGreg Roach
7415a78cd34SGreg Roach    /**
7426ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
743b6db7c1fSGreg Roach     * @param Tree                   $tree
7445a78cd34SGreg Roach     *
7456ccdf4f0SGreg Roach     * @return ResponseInterface
7465a78cd34SGreg Roach     */
7476ccdf4f0SGreg Roach    public function postAddNoteAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
748c1010edaSGreg Roach    {
7499e648e55SGreg Roach        $xref = $request->get('xref', '');
7505a78cd34SGreg Roach
7515a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
7525a78cd34SGreg Roach
7535a78cd34SGreg Roach        if ($note === null) {
75459f2f229SGreg Roach            throw new NoteNotFoundException();
7555a78cd34SGreg Roach        }
7565a78cd34SGreg Roach
7575a78cd34SGreg Roach        $this->addRecordToCart($note);
7585a78cd34SGreg Roach
7596ccdf4f0SGreg Roach        return redirect($note->url());
7605a78cd34SGreg Roach    }
7615a78cd34SGreg Roach
7625a78cd34SGreg Roach    /**
7636ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
764b6db7c1fSGreg Roach     * @param Tree                   $tree
7655a78cd34SGreg Roach     *
7666ccdf4f0SGreg Roach     * @return ResponseInterface
7675a78cd34SGreg Roach     */
7686ccdf4f0SGreg Roach    public function getAddRepositoryAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
769c1010edaSGreg Roach    {
7709e648e55SGreg Roach        $xref = $request->get('xref', '');
7715a78cd34SGreg Roach
7725a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
7735a78cd34SGreg Roach
7745a78cd34SGreg Roach        if ($repository === null) {
77559f2f229SGreg Roach            throw new RepositoryNotFoundException();
7765a78cd34SGreg Roach        }
7775a78cd34SGreg Roach
7785a78cd34SGreg Roach        $options = $this->repositoryOptions($repository);
7795a78cd34SGreg Roach
78039ca88baSGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $repository->fullName());
7815a78cd34SGreg Roach
7825a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
7835a78cd34SGreg Roach            'options' => $options,
7845a78cd34SGreg Roach            'default' => key($options),
7855a78cd34SGreg Roach            'record'  => $repository,
7865a78cd34SGreg Roach            'title'   => $title,
7875a78cd34SGreg Roach            'tree'    => $tree,
7885a78cd34SGreg Roach        ]);
7895a78cd34SGreg Roach    }
7905a78cd34SGreg Roach
7915a78cd34SGreg Roach    /**
7925a78cd34SGreg Roach     * @param Repository $repository
7935a78cd34SGreg Roach     *
7945a78cd34SGreg Roach     * @return string[]
7955a78cd34SGreg Roach     */
796c1010edaSGreg Roach    private function repositoryOptions(Repository $repository): array
797c1010edaSGreg Roach    {
79839ca88baSGreg Roach        $name = strip_tags($repository->fullName());
7995a78cd34SGreg Roach
8005a78cd34SGreg Roach        return [
8015a78cd34SGreg Roach            'self' => $name,
8025a78cd34SGreg Roach        ];
8035a78cd34SGreg Roach    }
8045a78cd34SGreg Roach
8055a78cd34SGreg Roach    /**
8066ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
807b6db7c1fSGreg Roach     * @param Tree                   $tree
8085a78cd34SGreg Roach     *
8096ccdf4f0SGreg Roach     * @return ResponseInterface
8105a78cd34SGreg Roach     */
8116ccdf4f0SGreg Roach    public function postAddRepositoryAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
812c1010edaSGreg Roach    {
8139e648e55SGreg Roach        $xref = $request->get('xref', '');
8145a78cd34SGreg Roach
8155a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
8165a78cd34SGreg Roach
8175a78cd34SGreg Roach        if ($repository === null) {
81859f2f229SGreg Roach            throw new RepositoryNotFoundException();
8195a78cd34SGreg Roach        }
8205a78cd34SGreg Roach
8215a78cd34SGreg Roach        $this->addRecordToCart($repository);
8225a78cd34SGreg Roach
8236ccdf4f0SGreg Roach        return redirect($repository->url());
8245a78cd34SGreg Roach    }
8255a78cd34SGreg Roach
8265a78cd34SGreg Roach    /**
8276ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
828b6db7c1fSGreg Roach     * @param Tree                   $tree
8295a78cd34SGreg Roach     *
8306ccdf4f0SGreg Roach     * @return ResponseInterface
8315a78cd34SGreg Roach     */
8326ccdf4f0SGreg Roach    public function getAddSourceAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
833c1010edaSGreg Roach    {
8349e648e55SGreg Roach        $xref = $request->get('xref', '');
8355a78cd34SGreg Roach
8365a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
8375a78cd34SGreg Roach
8385a78cd34SGreg Roach        if ($source === null) {
83959f2f229SGreg Roach            throw new SourceNotFoundException();
8405a78cd34SGreg Roach        }
8415a78cd34SGreg Roach
8425a78cd34SGreg Roach        $options = $this->sourceOptions($source);
8435a78cd34SGreg Roach
84439ca88baSGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $source->fullName());
8455a78cd34SGreg Roach
8465a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
8475a78cd34SGreg Roach            'options' => $options,
8485a78cd34SGreg Roach            'default' => key($options),
8495a78cd34SGreg Roach            'record'  => $source,
8505a78cd34SGreg Roach            'title'   => $title,
8515a78cd34SGreg Roach            'tree'    => $tree,
8525a78cd34SGreg Roach        ]);
8535a78cd34SGreg Roach    }
8545a78cd34SGreg Roach
8555a78cd34SGreg Roach    /**
8565a78cd34SGreg Roach     * @param Source $source
8575a78cd34SGreg Roach     *
8585a78cd34SGreg Roach     * @return string[]
8595a78cd34SGreg Roach     */
860c1010edaSGreg Roach    private function sourceOptions(Source $source): array
861c1010edaSGreg Roach    {
86239ca88baSGreg Roach        $name = strip_tags($source->fullName());
8635a78cd34SGreg Roach
8645a78cd34SGreg Roach        return [
86539ca88baSGreg Roach            'only'   => strip_tags($source->fullName()),
8665a78cd34SGreg Roach            'linked' => I18N::translate('%s and the individuals that reference it.', $name),
8675a78cd34SGreg Roach        ];
8685a78cd34SGreg Roach    }
8695a78cd34SGreg Roach
8705a78cd34SGreg Roach    /**
8716ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
872b6db7c1fSGreg Roach     * @param Tree                   $tree
8735a78cd34SGreg Roach     *
8746ccdf4f0SGreg Roach     * @return ResponseInterface
8755a78cd34SGreg Roach     */
8766ccdf4f0SGreg Roach    public function postAddSourceAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
877c1010edaSGreg Roach    {
8789e648e55SGreg Roach        $xref   = $request->get('xref', '');
8799e648e55SGreg Roach        $option = $request->get('option', '');
8805a78cd34SGreg Roach
8815a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
8825a78cd34SGreg Roach
8835a78cd34SGreg Roach        if ($source === null) {
88459f2f229SGreg Roach            throw new SourceNotFoundException();
8855a78cd34SGreg Roach        }
8865a78cd34SGreg Roach
8875a78cd34SGreg Roach        $this->addRecordToCart($source);
8885a78cd34SGreg Roach
8895a78cd34SGreg Roach        if ($option === 'linked') {
8905a78cd34SGreg Roach            foreach ($source->linkedIndividuals('SOUR') as $individual) {
8915a78cd34SGreg Roach                $this->addRecordToCart($individual);
8925a78cd34SGreg Roach            }
8935a78cd34SGreg Roach            foreach ($source->linkedFamilies('SOUR') as $family) {
8945a78cd34SGreg Roach                $this->addRecordToCart($family);
8955a78cd34SGreg Roach            }
8965a78cd34SGreg Roach        }
8975a78cd34SGreg Roach
8986ccdf4f0SGreg Roach        return redirect($source->url());
8995a78cd34SGreg Roach    }
9005a78cd34SGreg Roach
9015a78cd34SGreg Roach    /**
9025a78cd34SGreg Roach     * Get all the records in the cart.
9035a78cd34SGreg Roach     *
9045a78cd34SGreg Roach     * @param Tree $tree
9055a78cd34SGreg Roach     *
9065a78cd34SGreg Roach     * @return GedcomRecord[]
9075a78cd34SGreg Roach     */
908c1010edaSGreg Roach    private function allRecordsInCart(Tree $tree): array
909c1010edaSGreg Roach    {
9105a78cd34SGreg Roach        $cart = Session::get('cart', []);
9115a78cd34SGreg Roach
912aa6f03bbSGreg Roach        $xrefs = array_keys($cart[$tree->name()] ?? []);
9135a78cd34SGreg Roach
9145a78cd34SGreg Roach        // Fetch all the records in the cart.
9150b5fd0a6SGreg Roach        $records = array_map(static function (string $xref) use ($tree): GedcomRecord {
9165a78cd34SGreg Roach            return GedcomRecord::getInstance($xref, $tree);
9175a78cd34SGreg Roach        }, $xrefs);
9185a78cd34SGreg Roach
9195a78cd34SGreg Roach        // Some records may have been deleted after they were added to the cart.
9205a78cd34SGreg Roach        $records = array_filter($records);
9215a78cd34SGreg Roach
9225a78cd34SGreg Roach        // Group and sort.
9230b5fd0a6SGreg Roach        uasort($records, static function (GedcomRecord $x, GedcomRecord $y): int {
924c156e8f5SGreg Roach            return $x::RECORD_TYPE <=> $y::RECORD_TYPE ?: GedcomRecord::nameComparator()($x, $y);
9255a78cd34SGreg Roach        });
9265a78cd34SGreg Roach
9275a78cd34SGreg Roach        return $records;
9285a78cd34SGreg Roach    }
9295a78cd34SGreg Roach
9305a78cd34SGreg Roach    /**
9315a78cd34SGreg Roach     * Add a record (and direclty linked sources, notes, etc. to the cart.
9325a78cd34SGreg Roach     *
9335a78cd34SGreg Roach     * @param GedcomRecord $record
93418d7a90dSGreg Roach     *
93518d7a90dSGreg Roach     * @return void
9365a78cd34SGreg Roach     */
937e364afe4SGreg Roach    private function addRecordToCart(GedcomRecord $record): void
938c1010edaSGreg Roach    {
9395a78cd34SGreg Roach        $cart = Session::get('cart', []);
9405a78cd34SGreg Roach
941f4afa648SGreg Roach        $tree_name = $record->tree()->name();
9425a78cd34SGreg Roach
9435a78cd34SGreg Roach        // Add this record
944c0935879SGreg Roach        $cart[$tree_name][$record->xref()] = true;
9455a78cd34SGreg Roach
9465a78cd34SGreg Roach        // Add directly linked media, notes, repositories and sources.
9478d0ebef0SGreg Roach        preg_match_all('/\n\d (?:OBJE|NOTE|SOUR|REPO) @(' . Gedcom::REGEX_XREF . ')@/', $record->gedcom(), $matches);
9485a78cd34SGreg Roach
9495a78cd34SGreg Roach        foreach ($matches[1] as $match) {
9505a78cd34SGreg Roach            $cart[$tree_name][$match] = true;
9515a78cd34SGreg Roach        }
9525a78cd34SGreg Roach
9535a78cd34SGreg Roach        Session::put('cart', $cart);
9545a78cd34SGreg Roach    }
9555a78cd34SGreg Roach
9565a78cd34SGreg Roach    /**
9575a78cd34SGreg Roach     * @param Tree $tree
9585a78cd34SGreg Roach     *
9595a78cd34SGreg Roach     * @return bool
9605a78cd34SGreg Roach     */
961c1010edaSGreg Roach    private function isCartEmpty(Tree $tree): bool
962c1010edaSGreg Roach    {
9635a78cd34SGreg Roach        $cart = Session::get('cart', []);
9645a78cd34SGreg Roach
965aa6f03bbSGreg Roach        return empty($cart[$tree->name()]);
9665a78cd34SGreg Roach    }
9678c2e8227SGreg Roach}
968