xref: /webtrees/app/Module/ClippingsCartModule.php (revision 9e648e55bdb9c7ae6c0fddad630edb2908e56b1c)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
215a78cd34SGreg Roachuse Fisharebest\Webtrees\Database;
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;
361eca16b3SGreg Roachuse Fisharebest\Webtrees\Module;
375a78cd34SGreg Roachuse Fisharebest\Webtrees\Note;
385a78cd34SGreg Roachuse Fisharebest\Webtrees\Repository;
390e62c4b8SGreg Roachuse Fisharebest\Webtrees\Session;
405a78cd34SGreg Roachuse Fisharebest\Webtrees\Source;
41aee13b6dSGreg Roachuse Fisharebest\Webtrees\Tree;
425a78cd34SGreg Roachuse Fisharebest\Webtrees\User;
435a78cd34SGreg Roachuse League\Flysystem\Filesystem;
445a78cd34SGreg Roachuse League\Flysystem\ZipArchive\ZipArchiveAdapter;
45a3d8780cSGreg Roachuse function str_replace;
465a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\BinaryFileResponse;
475a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse;
485a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\Request;
495a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\Response;
505a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\ResponseHeaderBag;
515a78cd34SGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
528c2e8227SGreg Roach
538c2e8227SGreg Roach/**
548c2e8227SGreg Roach * Class ClippingsCartModule
558c2e8227SGreg Roach */
56c1010edaSGreg Roachclass ClippingsCartModule extends AbstractModule implements ModuleMenuInterface
57c1010edaSGreg Roach{
585a78cd34SGreg Roach    // Routes that have a record which can be added to the clipboard
59c1010edaSGreg Roach    const ROUTES_WITH_RECORDS = [
60c1010edaSGreg Roach        'family',
61c1010edaSGreg Roach        'individual',
62c1010edaSGreg Roach        'media',
63c1010edaSGreg Roach        'note',
64c1010edaSGreg Roach        'repository',
65c1010edaSGreg Roach        'source',
66c1010edaSGreg Roach    ];
675a78cd34SGreg Roach
688c2e8227SGreg Roach    /** {@inheritdoc} */
698f53f488SRico Sonntag    public function getTitle(): string
70c1010edaSGreg Roach    {
71bbb76c12SGreg Roach        /* I18N: Name of a module */
72bbb76c12SGreg Roach        return I18N::translate('Clippings cart');
738c2e8227SGreg Roach    }
748c2e8227SGreg Roach
758c2e8227SGreg Roach    /** {@inheritdoc} */
768f53f488SRico Sonntag    public function getDescription(): string
77c1010edaSGreg Roach    {
78bbb76c12SGreg Roach        /* I18N: Description of the “Clippings cart” module */
79bbb76c12SGreg Roach        return I18N::translate('Select records from your family tree and save them as a GEDCOM file.');
808c2e8227SGreg Roach    }
818c2e8227SGreg Roach
820ee13198SGreg Roach    /**
830ee13198SGreg Roach     * What is the default access level for this module?
840ee13198SGreg Roach     *
850ee13198SGreg Roach     * Some modules are aimed at admins or managers, and are not generally shown to users.
860ee13198SGreg Roach     *
870ee13198SGreg Roach     * @return int
880ee13198SGreg Roach     */
898f53f488SRico Sonntag    public function defaultAccessLevel(): int
90c1010edaSGreg Roach    {
914b9ff166SGreg Roach        return Auth::PRIV_USER;
928c2e8227SGreg Roach    }
938c2e8227SGreg Roach
9476692c8bSGreg Roach    /**
950ee13198SGreg Roach     * The user can re-order menus. Until they do, they are shown in this order.
960ee13198SGreg Roach     *
970ee13198SGreg Roach     * @return int
980ee13198SGreg Roach     */
998f53f488SRico Sonntag    public function defaultMenuOrder(): int
100c1010edaSGreg Roach    {
1018c2e8227SGreg Roach        return 20;
1028c2e8227SGreg Roach    }
1038c2e8227SGreg Roach
1040ee13198SGreg Roach    /**
1050ee13198SGreg Roach     * A menu, to be added to the main application menu.
1060ee13198SGreg Roach     *
107aee13b6dSGreg Roach     * @param Tree $tree
108aee13b6dSGreg Roach     *
1090ee13198SGreg Roach     * @return Menu|null
1100ee13198SGreg Roach     */
111c1010edaSGreg Roach    public function getMenu(Tree $tree)
112c1010edaSGreg Roach    {
1135a78cd34SGreg Roach        $request = Request::createFromGlobals();
1148c2e8227SGreg Roach
115*9e648e55SGreg Roach        $route = $request->get('route', '');
1165a78cd34SGreg Roach
1175a78cd34SGreg Roach        $submenus = [
118c1010edaSGreg Roach            new Menu($this->getTitle(), route('module', [
119c1010edaSGreg Roach                'module' => 'clippings',
120c1010edaSGreg Roach                'action' => 'Show',
121c1010edaSGreg Roach                'ged'    => $tree->getName(),
122c1010edaSGreg Roach            ]), 'menu-clippings-cart', ['rel' => 'nofollow']),
1235a78cd34SGreg Roach        ];
1245a78cd34SGreg Roach
1255a78cd34SGreg Roach        if (in_array($route, self::ROUTES_WITH_RECORDS)) {
126*9e648e55SGreg Roach            $xref      = $request->get('xref', '');
1275a78cd34SGreg Roach            $action    = 'Add' . ucfirst($route);
128c1010edaSGreg Roach            $add_route = route('module', [
129c1010edaSGreg Roach                'module' => 'clippings',
130c1010edaSGreg Roach                'action' => $action,
131c1010edaSGreg Roach                'xref'   => $xref,
132c1010edaSGreg Roach                'ged'    => $tree->getName(),
133c1010edaSGreg Roach            ]);
1345a78cd34SGreg Roach
13525b2dde3SGreg Roach            $submenus[] = new Menu(I18N::translate('Add to the clippings cart'), $add_route, 'menu-clippings-add', ['rel' => 'nofollow']);
1368c2e8227SGreg Roach        }
137cbc1590aSGreg Roach
1385a78cd34SGreg Roach        if (!$this->isCartEmpty($tree)) {
139c1010edaSGreg Roach            $submenus[] = new Menu(I18N::translate('Empty the clippings cart'), route('module', [
140c1010edaSGreg Roach                'module' => 'clippings',
141c1010edaSGreg Roach                'action' => 'Empty',
142c1010edaSGreg Roach                'ged'    => $tree->getName(),
143c1010edaSGreg Roach            ]), 'menu-clippings-empty', ['rel' => 'nofollow']);
144c1010edaSGreg Roach            $submenus[] = new Menu(I18N::translate('Download'), route('module', [
145c1010edaSGreg Roach                'module' => 'clippings',
146c1010edaSGreg Roach                'action' => 'DownloadForm',
147c1010edaSGreg Roach                'ged'    => $tree->getName(),
148c1010edaSGreg Roach            ]), 'menu-clippings-download', ['rel' => 'nofollow']);
1495a78cd34SGreg Roach        }
1505a78cd34SGreg Roach
15113abd6f3SGreg Roach        return new Menu($this->getTitle(), '#', 'menu-clippings', ['rel' => 'nofollow'], $submenus);
1528c2e8227SGreg Roach    }
1538c2e8227SGreg Roach
15476692c8bSGreg Roach    /**
1555a78cd34SGreg Roach     * @param Request $request
156b6db7c1fSGreg Roach     * @param Tree    $tree
15776692c8bSGreg Roach     *
1585a78cd34SGreg Roach     * @return BinaryFileResponse
15976692c8bSGreg Roach     */
160b6db7c1fSGreg Roach    public function getDownloadAction(Request $request, Tree $tree): BinaryFileResponse
161c1010edaSGreg Roach    {
1625a78cd34SGreg Roach        $this->checkModuleAccess($tree);
1638c2e8227SGreg Roach
164*9e648e55SGreg Roach        $privatize_export = $request->get('privatize_export', '');
1655a78cd34SGreg Roach        $convert          = (bool) $request->get('convert');
1668c2e8227SGreg Roach
16713abd6f3SGreg Roach        $cart = Session::get('cart', []);
1688c2e8227SGreg Roach
1695a78cd34SGreg Roach        $xrefs = array_keys($cart[$tree->getName()] ?? []);
1705a78cd34SGreg Roach
1715a78cd34SGreg Roach        // Create a new/empty .ZIP file
1725a78cd34SGreg Roach        $temp_zip_file  = tempnam(sys_get_temp_dir(), 'webtrees-zip-');
1735a78cd34SGreg Roach        $zip_filesystem = new Filesystem(new ZipArchiveAdapter($temp_zip_file));
1745a78cd34SGreg Roach
1755a78cd34SGreg Roach        // Media file prefix
1765a78cd34SGreg Roach        $path = $tree->getPreference('MEDIA_DIRECTORY');
1775a78cd34SGreg Roach
1785a78cd34SGreg Roach        // GEDCOM file header
179a3d8780cSGreg Roach        $filetext = FunctionsExport::gedcomHeader($tree, $convert ? 'ANSI' : 'UTF-8');
1805a78cd34SGreg Roach
1815a78cd34SGreg Roach        // Include SUBM/SUBN records, if they exist
1825a78cd34SGreg Roach        $subn =
1835a78cd34SGreg Roach            Database::prepare("SELECT o_gedcom FROM `##other` WHERE o_type=? AND o_file=?")
184c1010edaSGreg Roach                ->execute([
185c1010edaSGreg Roach                    'SUBN',
186c1010edaSGreg Roach                    $tree->getName(),
187c1010edaSGreg Roach                ])
1885a78cd34SGreg Roach                ->fetchOne();
1895a78cd34SGreg Roach        if ($subn) {
1905a78cd34SGreg Roach            $filetext .= $subn . "\n";
191b6468bb9SGreg Roach        }
1925a78cd34SGreg Roach        $subm =
1935a78cd34SGreg Roach            Database::prepare("SELECT o_gedcom FROM `##other` WHERE o_type=? AND o_file=?")
194c1010edaSGreg Roach                ->execute([
195c1010edaSGreg Roach                    'SUBM',
196c1010edaSGreg Roach                    $tree->getName(),
197c1010edaSGreg Roach                ])
1985a78cd34SGreg Roach                ->fetchOne();
1995a78cd34SGreg Roach        if ($subm) {
2005a78cd34SGreg Roach            $filetext .= $subm . "\n";
2015a78cd34SGreg Roach        }
2025a78cd34SGreg Roach
2035a78cd34SGreg Roach        switch ($privatize_export) {
2045a78cd34SGreg Roach            case 'gedadmin':
2055a78cd34SGreg Roach                $access_level = Auth::PRIV_NONE;
2065a78cd34SGreg Roach                break;
2075a78cd34SGreg Roach            case 'user':
2085a78cd34SGreg Roach                $access_level = Auth::PRIV_USER;
2095a78cd34SGreg Roach                break;
2105a78cd34SGreg Roach            case 'visitor':
2115a78cd34SGreg Roach                $access_level = Auth::PRIV_PRIVATE;
2125a78cd34SGreg Roach                break;
2135a78cd34SGreg Roach            case 'none':
2145a78cd34SGreg Roach            default:
2155a78cd34SGreg Roach                $access_level = Auth::PRIV_HIDE;
2165a78cd34SGreg Roach                break;
2175a78cd34SGreg Roach        }
2185a78cd34SGreg Roach
2195a78cd34SGreg Roach        foreach ($xrefs as $xref) {
2205a78cd34SGreg Roach            $object = GedcomRecord::getInstance($xref, $tree);
2215a78cd34SGreg Roach            // The object may have been deleted since we added it to the cart....
2225a78cd34SGreg Roach            if ($object) {
2235a78cd34SGreg Roach                $record = $object->privatizeGedcom($access_level);
2245a78cd34SGreg Roach                // Remove links to objects that aren't in the cart
2255a78cd34SGreg Roach                preg_match_all('/\n1 ' . WT_REGEX_TAG . ' @(' . WT_REGEX_XREF . ')@(\n[2-9].*)*/', $record, $matches, PREG_SET_ORDER);
2265a78cd34SGreg Roach                foreach ($matches as $match) {
2275a78cd34SGreg Roach                    if (!array_key_exists($match[1], $xrefs)) {
2285a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2295a78cd34SGreg Roach                    }
2305a78cd34SGreg Roach                }
2315a78cd34SGreg Roach                preg_match_all('/\n2 ' . WT_REGEX_TAG . ' @(' . WT_REGEX_XREF . ')@(\n[3-9].*)*/', $record, $matches, PREG_SET_ORDER);
2325a78cd34SGreg Roach                foreach ($matches as $match) {
2335a78cd34SGreg Roach                    if (!array_key_exists($match[1], $xrefs)) {
2345a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2355a78cd34SGreg Roach                    }
2365a78cd34SGreg Roach                }
2375a78cd34SGreg Roach                preg_match_all('/\n3 ' . WT_REGEX_TAG . ' @(' . WT_REGEX_XREF . ')@(\n[4-9].*)*/', $record, $matches, PREG_SET_ORDER);
2385a78cd34SGreg Roach                foreach ($matches as $match) {
2395a78cd34SGreg Roach                    if (!array_key_exists($match[1], $xrefs)) {
2405a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2415a78cd34SGreg Roach                    }
2425a78cd34SGreg Roach                }
2435a78cd34SGreg Roach
24455167344SGreg Roach                if ($object instanceof Individual || $object instanceof Family) {
2455a78cd34SGreg Roach                    $filetext .= $record . "\n";
2465a78cd34SGreg Roach                    $filetext .= "1 SOUR @WEBTREES@\n";
2475a78cd34SGreg Roach                    $filetext .= '2 PAGE ' . WT_BASE_URL . $object->url() . "\n";
24855167344SGreg Roach                } elseif ($object instanceof Source) {
2495a78cd34SGreg Roach                    $filetext .= $record . "\n";
2505a78cd34SGreg Roach                    $filetext .= '1 NOTE ' . WT_BASE_URL . $object->url() . "\n";
25155167344SGreg Roach                } elseif ($object instanceof Media) {
25255167344SGreg Roach                    // Add the media files to the archive
2535a78cd34SGreg Roach                    foreach ($object->mediaFiles() as $media_file) {
2545a78cd34SGreg Roach                        if (file_exists($media_file->getServerFilename())) {
2555a78cd34SGreg Roach                            $fp = fopen($media_file->getServerFilename(), 'r');
2565a78cd34SGreg Roach                            $zip_filesystem->writeStream($path . $media_file->filename(), $fp);
2575a78cd34SGreg Roach                            fclose($fp);
2585a78cd34SGreg Roach                        }
2595a78cd34SGreg Roach                    }
2605a78cd34SGreg Roach                    $filetext .= $record . "\n";
26155167344SGreg Roach                } else {
2625a78cd34SGreg Roach                    $filetext .= $record . "\n";
2638c2e8227SGreg Roach                }
2648c2e8227SGreg Roach            }
2658c2e8227SGreg Roach        }
2668c2e8227SGreg Roach
2675a78cd34SGreg Roach        // Create a source, to indicate the source of the data.
2685a78cd34SGreg Roach        $filetext .= "0 @WEBTREES@ SOUR\n1 TITL " . WT_BASE_URL . "\n";
2697fcd9838SGreg Roach        $author = User::find((int) $tree->getPreference('CONTACT_USER_ID'));
2705a78cd34SGreg Roach        if ($author !== null) {
2715a78cd34SGreg Roach            $filetext .= '1 AUTH ' . $author->getRealName() . "\n";
2725a78cd34SGreg Roach        }
2735a78cd34SGreg Roach        $filetext .= "0 TRLR\n";
2745a78cd34SGreg Roach
2755a78cd34SGreg Roach        // Make sure the preferred line endings are used
276a3d8780cSGreg Roach        $filetext = str_replace('\n', Gedcom::EOL, $filetext);
2775a78cd34SGreg Roach
27855167344SGreg Roach        if ($convert) {
2795a78cd34SGreg Roach            $filetext = utf8_decode($filetext);
2808c2e8227SGreg Roach        }
281cbc1590aSGreg Roach
2825a78cd34SGreg Roach        // Finally add the GEDCOM file to the .ZIP file.
2835a78cd34SGreg Roach        $zip_filesystem->write('clippings.ged', $filetext);
2845a78cd34SGreg Roach
2855a78cd34SGreg Roach        // Need to force-close the filesystem
28602a92f80SGreg Roach        unset($zip_filesystem);
2875a78cd34SGreg Roach
2885a78cd34SGreg Roach        $response = new BinaryFileResponse($temp_zip_file);
2895a78cd34SGreg Roach        $response->deleteFileAfterSend(true);
2905a78cd34SGreg Roach
2915a78cd34SGreg Roach        $response->headers->set('Content-Type', 'application/zip');
2925a78cd34SGreg Roach        $response->setContentDisposition(
2935a78cd34SGreg Roach            ResponseHeaderBag::DISPOSITION_ATTACHMENT,
2945a78cd34SGreg Roach            'clippings.zip'
2955a78cd34SGreg Roach        );
2965a78cd34SGreg Roach
2975a78cd34SGreg Roach        return $response;
2988c2e8227SGreg Roach    }
2998c2e8227SGreg Roach
3008c2e8227SGreg Roach    /**
301b6db7c1fSGreg Roach     * @param Tree $tree
302b6db7c1fSGreg Roach     * @param User $user
30376692c8bSGreg Roach     *
3045a78cd34SGreg Roach     * @return Response
3058c2e8227SGreg Roach     */
306b6db7c1fSGreg Roach    public function getDownloadFormAction(Tree $tree, User $user): Response
307c1010edaSGreg Roach    {
3085a78cd34SGreg Roach        $title = I18N::translate('Family tree clippings cart') . ' — ' . I18N::translate('Download');
3098c2e8227SGreg Roach
3105a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/download', [
3115a78cd34SGreg Roach            'is_manager' => Auth::isManager($tree, $user),
3125a78cd34SGreg Roach            'is_member'  => Auth::isMember($tree, $user),
3135a78cd34SGreg Roach            'title'      => $title,
3145a78cd34SGreg Roach        ]);
3158c2e8227SGreg Roach    }
3168c2e8227SGreg Roach
3175a78cd34SGreg Roach    /**
318b6db7c1fSGreg Roach     * @param Tree $tree
3195a78cd34SGreg Roach     *
3205a78cd34SGreg Roach     * @return RedirectResponse
3215a78cd34SGreg Roach     */
322b6db7c1fSGreg Roach    public function getEmptyAction(Tree $tree): RedirectResponse
323c1010edaSGreg Roach    {
3245a78cd34SGreg Roach        $cart                   = Session::get('cart', []);
3255a78cd34SGreg Roach        $cart[$tree->getName()] = [];
3265a78cd34SGreg Roach        Session::put('cart', $cart);
3278c2e8227SGreg Roach
328c1010edaSGreg Roach        $url = route('module', [
329c1010edaSGreg Roach            'module' => 'clippings',
330c1010edaSGreg Roach            'action' => 'Show',
331c1010edaSGreg Roach            'ged'    => $tree->getName(),
332c1010edaSGreg Roach        ]);
3335a78cd34SGreg Roach
3345a78cd34SGreg Roach        return new RedirectResponse($url);
3355a78cd34SGreg Roach    }
3365a78cd34SGreg Roach
3375a78cd34SGreg Roach    /**
3385a78cd34SGreg Roach     * @param Request $request
339b6db7c1fSGreg Roach     * @param Tree    $tree
3405a78cd34SGreg Roach     *
3415a78cd34SGreg Roach     * @return RedirectResponse
3425a78cd34SGreg Roach     */
343b6db7c1fSGreg Roach    public function postRemoveAction(Request $request, Tree $tree): RedirectResponse
344c1010edaSGreg Roach    {
345*9e648e55SGreg Roach        $xref = $request->get('xref', '');
3465a78cd34SGreg Roach
3475a78cd34SGreg Roach        $cart = Session::get('cart', []);
3485a78cd34SGreg Roach        unset($cart[$tree->getName()][$xref]);
3495a78cd34SGreg Roach        Session::put('cart', $cart);
3505a78cd34SGreg Roach
351c1010edaSGreg Roach        $url = route('module', [
352c1010edaSGreg Roach            'module' => 'clippings',
353c1010edaSGreg Roach            'action' => 'Show',
354c1010edaSGreg Roach            'ged'    => $tree->getName(),
355c1010edaSGreg Roach        ]);
3565a78cd34SGreg Roach
3575a78cd34SGreg Roach        return new RedirectResponse($url);
3585a78cd34SGreg Roach    }
3595a78cd34SGreg Roach
3605a78cd34SGreg Roach    /**
361b6db7c1fSGreg Roach     * @param Tree $tree
3625a78cd34SGreg Roach     *
3635a78cd34SGreg Roach     * @return Response
3645a78cd34SGreg Roach     */
365b6db7c1fSGreg Roach    public function getShowAction(Tree $tree): Response
366c1010edaSGreg Roach    {
3675a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/show', [
3685a78cd34SGreg Roach            'records' => $this->allRecordsInCart($tree),
3695a78cd34SGreg Roach            'title'   => I18N::translate('Family tree clippings cart'),
3705a78cd34SGreg Roach            'tree'    => $tree,
3715a78cd34SGreg Roach        ]);
3725a78cd34SGreg Roach    }
3735a78cd34SGreg Roach
3745a78cd34SGreg Roach    /**
3755a78cd34SGreg Roach     * @param Request $request
376b6db7c1fSGreg Roach     * @param Tree    $tree
3775a78cd34SGreg Roach     *
3785a78cd34SGreg Roach     * @return Response
3795a78cd34SGreg Roach     */
380b6db7c1fSGreg Roach    public function getAddFamilyAction(Request $request, Tree $tree): Response
381c1010edaSGreg Roach    {
382*9e648e55SGreg Roach        $xref = $request->get('xref', '');
3835a78cd34SGreg Roach
3845a78cd34SGreg Roach        $family = Family::getInstance($xref, $tree);
3855a78cd34SGreg Roach
3865a78cd34SGreg Roach        if ($family === null) {
38759f2f229SGreg Roach            throw new FamilyNotFoundException();
3885a78cd34SGreg Roach        }
3895a78cd34SGreg Roach
3905a78cd34SGreg Roach        $options = $this->familyOptions($family);
3915a78cd34SGreg Roach
3925a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $family->getFullName());
3935a78cd34SGreg Roach
3945a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
3955a78cd34SGreg Roach            'options' => $options,
3965a78cd34SGreg Roach            'default' => key($options),
3975a78cd34SGreg Roach            'record'  => $family,
3985a78cd34SGreg Roach            'title'   => $title,
3995a78cd34SGreg Roach            'tree'    => $tree,
4005a78cd34SGreg Roach        ]);
4015a78cd34SGreg Roach    }
4025a78cd34SGreg Roach
4035a78cd34SGreg Roach    /**
4045a78cd34SGreg Roach     * @param Family $family
4055a78cd34SGreg Roach     *
4065a78cd34SGreg Roach     * @return string[]
4075a78cd34SGreg Roach     */
408c1010edaSGreg Roach    private function familyOptions(Family $family): array
409c1010edaSGreg Roach    {
4105a78cd34SGreg Roach        $name = strip_tags($family->getFullName());
4115a78cd34SGreg Roach
4125a78cd34SGreg Roach        return [
4135a78cd34SGreg Roach            'parents'     => $name,
414bbb76c12SGreg Roach            /* I18N: %s is a family (husband + wife) */
415bbb76c12SGreg Roach            'members'     => I18N::translate('%s and their children', $name),
416bbb76c12SGreg Roach            /* I18N: %s is a family (husband + wife) */
417bbb76c12SGreg Roach            'descendants' => I18N::translate('%s and their descendants', $name),
4185a78cd34SGreg Roach        ];
4195a78cd34SGreg Roach    }
4205a78cd34SGreg Roach
4215a78cd34SGreg Roach    /**
4225a78cd34SGreg Roach     * @param Request $request
423b6db7c1fSGreg Roach     * @param Tree    $tree
4245a78cd34SGreg Roach     *
4255a78cd34SGreg Roach     * @return RedirectResponse
4265a78cd34SGreg Roach     */
427b6db7c1fSGreg Roach    public function postAddFamilyAction(Request $request, Tree $tree): RedirectResponse
428c1010edaSGreg Roach    {
429*9e648e55SGreg Roach        $xref   = $request->get('xref', '');
430*9e648e55SGreg Roach        $option = $request->get('option', '');
4315a78cd34SGreg Roach
4325a78cd34SGreg Roach        $family = Family::getInstance($xref, $tree);
4335a78cd34SGreg Roach
4345a78cd34SGreg Roach        if ($family === null) {
43559f2f229SGreg Roach            throw new FamilyNotFoundException();
4365a78cd34SGreg Roach        }
4375a78cd34SGreg Roach
4385a78cd34SGreg Roach        switch ($option) {
4395a78cd34SGreg Roach            case 'parents':
4405a78cd34SGreg Roach                $this->addFamilyToCart($family);
4415a78cd34SGreg Roach                break;
4425a78cd34SGreg Roach
4435a78cd34SGreg Roach            case 'members':
4445a78cd34SGreg Roach                $this->addFamilyAndChildrenToCart($family);
4455a78cd34SGreg Roach                break;
4465a78cd34SGreg Roach
4475a78cd34SGreg Roach            case 'descendants':
4485a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($family);
4495a78cd34SGreg Roach                break;
4505a78cd34SGreg Roach        }
4515a78cd34SGreg Roach
4525a78cd34SGreg Roach        return new RedirectResponse($family->url());
4535a78cd34SGreg Roach    }
4545a78cd34SGreg Roach
4555a78cd34SGreg Roach    /**
4565a78cd34SGreg Roach     * @param Family $family
45718d7a90dSGreg Roach     *
45818d7a90dSGreg Roach     * @return void
4595a78cd34SGreg Roach     */
460c1010edaSGreg Roach    private function addFamilyToCart(Family $family)
461c1010edaSGreg Roach    {
4625a78cd34SGreg Roach        $this->addRecordToCart($family);
4635a78cd34SGreg Roach
4645a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
4655a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4665a78cd34SGreg Roach        }
4675a78cd34SGreg Roach    }
4685a78cd34SGreg Roach
4695a78cd34SGreg Roach    /**
4705a78cd34SGreg Roach     * @param Family $family
47118d7a90dSGreg Roach     *
47218d7a90dSGreg Roach     * @return void
4735a78cd34SGreg Roach     */
474c1010edaSGreg Roach    private function addFamilyAndChildrenToCart(Family $family)
475c1010edaSGreg Roach    {
4765a78cd34SGreg Roach        $this->addRecordToCart($family);
4775a78cd34SGreg Roach
4785a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
4795a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4805a78cd34SGreg Roach        }
4815a78cd34SGreg Roach        foreach ($family->getChildren() as $child) {
4825a78cd34SGreg Roach            $this->addRecordToCart($child);
4835a78cd34SGreg Roach        }
4845a78cd34SGreg Roach    }
4855a78cd34SGreg Roach
4865a78cd34SGreg Roach    /**
4875a78cd34SGreg Roach     * @param Family $family
48818d7a90dSGreg Roach     *
48918d7a90dSGreg Roach     * @return void
4905a78cd34SGreg Roach     */
491c1010edaSGreg Roach    private function addFamilyAndDescendantsToCart(Family $family)
492c1010edaSGreg Roach    {
4935a78cd34SGreg Roach        $this->addRecordToCart($family);
4945a78cd34SGreg Roach
4955a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
4965a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4975a78cd34SGreg Roach        }
4985a78cd34SGreg Roach        foreach ($family->getChildren() as $child) {
4995a78cd34SGreg Roach            $this->addRecordToCart($child);
5005a78cd34SGreg Roach            foreach ($child->getSpouseFamilies() as $child_family) {
5015a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($child_family);
5025a78cd34SGreg Roach            }
5035a78cd34SGreg Roach        }
5045a78cd34SGreg Roach    }
5055a78cd34SGreg Roach
5065a78cd34SGreg Roach    /**
5075a78cd34SGreg Roach     * @param Request $request
508b6db7c1fSGreg Roach     * @param Tree    $tree
5095a78cd34SGreg Roach     *
5105a78cd34SGreg Roach     * @return Response
5115a78cd34SGreg Roach     */
512b6db7c1fSGreg Roach    public function getAddIndividualAction(Request $request, Tree $tree): Response
513c1010edaSGreg Roach    {
514*9e648e55SGreg Roach        $xref = $request->get('xref', '');
5155a78cd34SGreg Roach
5165a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
5175a78cd34SGreg Roach
5185a78cd34SGreg Roach        if ($individual === null) {
51959f2f229SGreg Roach            throw new IndividualNotFoundException();
5205a78cd34SGreg Roach        }
5215a78cd34SGreg Roach
5225a78cd34SGreg Roach        $options = $this->individualOptions($individual);
5235a78cd34SGreg Roach
5245a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $individual->getFullName());
5255a78cd34SGreg Roach
5265a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
5275a78cd34SGreg Roach            'options' => $options,
5285a78cd34SGreg Roach            'default' => key($options),
5295a78cd34SGreg Roach            'record'  => $individual,
5305a78cd34SGreg Roach            'title'   => $title,
5315a78cd34SGreg Roach            'tree'    => $tree,
5325a78cd34SGreg Roach        ]);
5335a78cd34SGreg Roach    }
5345a78cd34SGreg Roach
5355a78cd34SGreg Roach    /**
5365a78cd34SGreg Roach     * @param Individual $individual
5375a78cd34SGreg Roach     *
5385a78cd34SGreg Roach     * @return string[]
5395a78cd34SGreg Roach     */
540c1010edaSGreg Roach    private function individualOptions(Individual $individual): array
541c1010edaSGreg Roach    {
5425a78cd34SGreg Roach        $name = strip_tags($individual->getFullName());
5435a78cd34SGreg Roach
5445a78cd34SGreg Roach        if ($individual->getSex() === 'F') {
5455a78cd34SGreg Roach            return [
5465a78cd34SGreg Roach                'self'              => $name,
5475a78cd34SGreg Roach                'parents'           => I18N::translate('%s, her parents and siblings', $name),
5485a78cd34SGreg Roach                'spouses'           => I18N::translate('%s, her spouses and children', $name),
5495a78cd34SGreg Roach                'ancestors'         => I18N::translate('%s and her ancestors', $name),
5505a78cd34SGreg Roach                'ancestor_families' => I18N::translate('%s, her ancestors and their families', $name),
5515a78cd34SGreg Roach                'descendants'       => I18N::translate('%s, her spouses and descendants', $name),
5525a78cd34SGreg Roach            ];
553b2ce94c6SRico Sonntag        }
554b2ce94c6SRico Sonntag
5555a78cd34SGreg Roach        return [
5565a78cd34SGreg Roach            'self'              => $name,
5575a78cd34SGreg Roach            'parents'           => I18N::translate('%s, his parents and siblings', $name),
5585a78cd34SGreg Roach            'spouses'           => I18N::translate('%s, his spouses and children', $name),
5595a78cd34SGreg Roach            'ancestors'         => I18N::translate('%s and his ancestors', $name),
5605a78cd34SGreg Roach            'ancestor_families' => I18N::translate('%s, his ancestors and their families', $name),
5615a78cd34SGreg Roach            'descendants'       => I18N::translate('%s, his spouses and descendants', $name),
5625a78cd34SGreg Roach        ];
5635a78cd34SGreg Roach    }
5645a78cd34SGreg Roach
5655a78cd34SGreg Roach    /**
5665a78cd34SGreg Roach     * @param Request $request
567b6db7c1fSGreg Roach     * @param Tree    $tree
5685a78cd34SGreg Roach     *
5695a78cd34SGreg Roach     * @return RedirectResponse
5705a78cd34SGreg Roach     */
571b6db7c1fSGreg Roach    public function postAddIndividualAction(Request $request, Tree $tree): RedirectResponse
572c1010edaSGreg Roach    {
573*9e648e55SGreg Roach        $xref   = $request->get('xref', '');
574*9e648e55SGreg Roach        $option = $request->get('option', '');
5755a78cd34SGreg Roach
5765a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
5775a78cd34SGreg Roach
5785a78cd34SGreg Roach        if ($individual === null) {
57959f2f229SGreg Roach            throw new IndividualNotFoundException();
5805a78cd34SGreg Roach        }
5815a78cd34SGreg Roach
5825a78cd34SGreg Roach        switch ($option) {
5835a78cd34SGreg Roach            case 'self':
5845a78cd34SGreg Roach                $this->addRecordToCart($individual);
5855a78cd34SGreg Roach                break;
5865a78cd34SGreg Roach
5875a78cd34SGreg Roach            case 'parents':
5885a78cd34SGreg Roach                foreach ($individual->getChildFamilies() as $family) {
5895a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
5905a78cd34SGreg Roach                }
5915a78cd34SGreg Roach                break;
5925a78cd34SGreg Roach
5935a78cd34SGreg Roach            case 'spouses':
5945a78cd34SGreg Roach                foreach ($individual->getSpouseFamilies() as $family) {
5955a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
5965a78cd34SGreg Roach                }
5975a78cd34SGreg Roach                break;
5985a78cd34SGreg Roach
5995a78cd34SGreg Roach            case 'ancestors':
6005a78cd34SGreg Roach                $this->addAncestorsToCart($individual);
6015a78cd34SGreg Roach                break;
6025a78cd34SGreg Roach
6035a78cd34SGreg Roach            case 'ancestor_families':
6045a78cd34SGreg Roach                $this->addAncestorFamiliesToCart($individual);
6055a78cd34SGreg Roach                break;
6065a78cd34SGreg Roach
6075a78cd34SGreg Roach            case 'descendants':
6085a78cd34SGreg Roach                foreach ($individual->getSpouseFamilies() as $family) {
6095a78cd34SGreg Roach                    $this->addFamilyAndDescendantsToCart($family);
6105a78cd34SGreg Roach                }
6115a78cd34SGreg Roach                break;
6125a78cd34SGreg Roach        }
6135a78cd34SGreg Roach
6145a78cd34SGreg Roach        return new RedirectResponse($individual->url());
6155a78cd34SGreg Roach    }
6165a78cd34SGreg Roach
6175a78cd34SGreg Roach    /**
6185a78cd34SGreg Roach     * @param Individual $individual
61918d7a90dSGreg Roach     *
62018d7a90dSGreg Roach     * @return void
6215a78cd34SGreg Roach     */
622c1010edaSGreg Roach    private function addAncestorsToCart(Individual $individual)
623c1010edaSGreg Roach    {
6245a78cd34SGreg Roach        $this->addRecordToCart($individual);
6255a78cd34SGreg Roach
6265a78cd34SGreg Roach        foreach ($individual->getChildFamilies() as $family) {
6275a78cd34SGreg Roach            foreach ($family->getSpouses() as $parent) {
6285a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
6295a78cd34SGreg Roach            }
6305a78cd34SGreg Roach        }
6315a78cd34SGreg Roach    }
6325a78cd34SGreg Roach
6335a78cd34SGreg Roach    /**
6345a78cd34SGreg Roach     * @param Individual $individual
63518d7a90dSGreg Roach     *
63618d7a90dSGreg Roach     * @return void
6375a78cd34SGreg Roach     */
638c1010edaSGreg Roach    private function addAncestorFamiliesToCart(Individual $individual)
639c1010edaSGreg Roach    {
6405a78cd34SGreg Roach        foreach ($individual->getChildFamilies() as $family) {
6415a78cd34SGreg Roach            $this->addFamilyAndChildrenToCart($family);
6425a78cd34SGreg Roach            foreach ($family->getSpouses() as $parent) {
6435a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
6445a78cd34SGreg Roach            }
6455a78cd34SGreg Roach        }
6465a78cd34SGreg Roach    }
6475a78cd34SGreg Roach
6485a78cd34SGreg Roach    /**
6495a78cd34SGreg Roach     * @param Request $request
650b6db7c1fSGreg Roach     * @param Tree    $tree
6515a78cd34SGreg Roach     *
6525a78cd34SGreg Roach     * @return Response
6535a78cd34SGreg Roach     */
654b6db7c1fSGreg Roach    public function getAddMediaAction(Request $request, Tree $tree): Response
655c1010edaSGreg Roach    {
656*9e648e55SGreg Roach        $xref = $request->get('xref', '');
6575a78cd34SGreg Roach
6585a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
6595a78cd34SGreg Roach
6605a78cd34SGreg Roach        if ($media === null) {
66159f2f229SGreg Roach            throw new MediaNotFoundException();
6625a78cd34SGreg Roach        }
6635a78cd34SGreg Roach
6645a78cd34SGreg Roach        $options = $this->mediaOptions($media);
6655a78cd34SGreg Roach
6665a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $media->getFullName());
6675a78cd34SGreg Roach
6685a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
6695a78cd34SGreg Roach            'options' => $options,
6705a78cd34SGreg Roach            'default' => key($options),
6715a78cd34SGreg Roach            'record'  => $media,
6725a78cd34SGreg Roach            'title'   => $title,
6735a78cd34SGreg Roach            'tree'    => $tree,
6745a78cd34SGreg Roach        ]);
6755a78cd34SGreg Roach    }
6765a78cd34SGreg Roach
6775a78cd34SGreg Roach    /**
6785a78cd34SGreg Roach     * @param Media $media
6795a78cd34SGreg Roach     *
6805a78cd34SGreg Roach     * @return string[]
6815a78cd34SGreg Roach     */
682c1010edaSGreg Roach    private function mediaOptions(Media $media): array
683c1010edaSGreg Roach    {
6845a78cd34SGreg Roach        $name = strip_tags($media->getFullName());
6855a78cd34SGreg Roach
6865a78cd34SGreg Roach        return [
6875a78cd34SGreg Roach            'self' => $name,
6885a78cd34SGreg Roach        ];
6895a78cd34SGreg Roach    }
6905a78cd34SGreg Roach
6915a78cd34SGreg Roach    /**
6925a78cd34SGreg Roach     * @param Request $request
693b6db7c1fSGreg Roach     * @param Tree    $tree
6945a78cd34SGreg Roach     *
6955a78cd34SGreg Roach     * @return RedirectResponse
6965a78cd34SGreg Roach     */
697b6db7c1fSGreg Roach    public function postAddMediaAction(Request $request, Tree $tree): RedirectResponse
698c1010edaSGreg Roach    {
699*9e648e55SGreg Roach        $xref = $request->get('xref', '');
7005a78cd34SGreg Roach
7015a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
7025a78cd34SGreg Roach
7035a78cd34SGreg Roach        if ($media === null) {
70459f2f229SGreg Roach            throw new MediaNotFoundException();
7055a78cd34SGreg Roach        }
7065a78cd34SGreg Roach
7075a78cd34SGreg Roach        $this->addRecordToCart($media);
7085a78cd34SGreg Roach
7095a78cd34SGreg Roach        return new RedirectResponse($media->url());
7105a78cd34SGreg Roach    }
7115a78cd34SGreg Roach
7125a78cd34SGreg Roach    /**
7135a78cd34SGreg Roach     * @param Request $request
714b6db7c1fSGreg Roach     * @param Tree    $tree
7155a78cd34SGreg Roach     *
7165a78cd34SGreg Roach     * @return Response
7175a78cd34SGreg Roach     */
718b6db7c1fSGreg Roach    public function getAddNoteAction(Request $request, Tree $tree): Response
719c1010edaSGreg Roach    {
720*9e648e55SGreg Roach        $xref = $request->get('xref', '');
7215a78cd34SGreg Roach
7225a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
7235a78cd34SGreg Roach
7245a78cd34SGreg Roach        if ($note === null) {
72559f2f229SGreg Roach            throw new NoteNotFoundException();
7265a78cd34SGreg Roach        }
7275a78cd34SGreg Roach
7285a78cd34SGreg Roach        $options = $this->noteOptions($note);
7295a78cd34SGreg Roach
7305a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $note->getFullName());
7315a78cd34SGreg Roach
7325a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
7335a78cd34SGreg Roach            'options' => $options,
7345a78cd34SGreg Roach            'default' => key($options),
7355a78cd34SGreg Roach            'record'  => $note,
7365a78cd34SGreg Roach            'title'   => $title,
7375a78cd34SGreg Roach            'tree'    => $tree,
7385a78cd34SGreg Roach        ]);
7395a78cd34SGreg Roach    }
7405a78cd34SGreg Roach
7415a78cd34SGreg Roach    /**
7425a78cd34SGreg Roach     * @param Note $note
7435a78cd34SGreg Roach     *
7445a78cd34SGreg Roach     * @return string[]
7455a78cd34SGreg Roach     */
746c1010edaSGreg Roach    private function noteOptions(Note $note): array
747c1010edaSGreg Roach    {
7485a78cd34SGreg Roach        $name = strip_tags($note->getFullName());
7495a78cd34SGreg Roach
7505a78cd34SGreg Roach        return [
7515a78cd34SGreg Roach            'self' => $name,
7525a78cd34SGreg Roach        ];
7535a78cd34SGreg Roach    }
7545a78cd34SGreg Roach
7555a78cd34SGreg Roach    /**
7565a78cd34SGreg Roach     * @param Request $request
757b6db7c1fSGreg Roach     * @param Tree    $tree
7585a78cd34SGreg Roach     *
7595a78cd34SGreg Roach     * @return RedirectResponse
7605a78cd34SGreg Roach     */
761b6db7c1fSGreg Roach    public function postAddNoteAction(Request $request, Tree $tree): RedirectResponse
762c1010edaSGreg Roach    {
763*9e648e55SGreg Roach        $xref = $request->get('xref', '');
7645a78cd34SGreg Roach
7655a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
7665a78cd34SGreg Roach
7675a78cd34SGreg Roach        if ($note === null) {
76859f2f229SGreg Roach            throw new NoteNotFoundException();
7695a78cd34SGreg Roach        }
7705a78cd34SGreg Roach
7715a78cd34SGreg Roach        $this->addRecordToCart($note);
7725a78cd34SGreg Roach
7735a78cd34SGreg Roach        return new RedirectResponse($note->url());
7745a78cd34SGreg Roach    }
7755a78cd34SGreg Roach
7765a78cd34SGreg Roach    /**
7775a78cd34SGreg Roach     * @param Request $request
778b6db7c1fSGreg Roach     * @param Tree    $tree
7795a78cd34SGreg Roach     *
7805a78cd34SGreg Roach     * @return Response
7815a78cd34SGreg Roach     */
782b6db7c1fSGreg Roach    public function getAddRepositoryAction(Request $request, Tree $tree): Response
783c1010edaSGreg Roach    {
784*9e648e55SGreg Roach        $xref = $request->get('xref', '');
7855a78cd34SGreg Roach
7865a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
7875a78cd34SGreg Roach
7885a78cd34SGreg Roach        if ($repository === null) {
78959f2f229SGreg Roach            throw new RepositoryNotFoundException();
7905a78cd34SGreg Roach        }
7915a78cd34SGreg Roach
7925a78cd34SGreg Roach        $options = $this->repositoryOptions($repository);
7935a78cd34SGreg Roach
7945a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $repository->getFullName());
7955a78cd34SGreg Roach
7965a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
7975a78cd34SGreg Roach            'options' => $options,
7985a78cd34SGreg Roach            'default' => key($options),
7995a78cd34SGreg Roach            'record'  => $repository,
8005a78cd34SGreg Roach            'title'   => $title,
8015a78cd34SGreg Roach            'tree'    => $tree,
8025a78cd34SGreg Roach        ]);
8035a78cd34SGreg Roach    }
8045a78cd34SGreg Roach
8055a78cd34SGreg Roach    /**
8065a78cd34SGreg Roach     * @param Repository $repository
8075a78cd34SGreg Roach     *
8085a78cd34SGreg Roach     * @return string[]
8095a78cd34SGreg Roach     */
810c1010edaSGreg Roach    private function repositoryOptions(Repository $repository): array
811c1010edaSGreg Roach    {
8125a78cd34SGreg Roach        $name = strip_tags($repository->getFullName());
8135a78cd34SGreg Roach
8145a78cd34SGreg Roach        return [
8155a78cd34SGreg Roach            'self' => $name,
8165a78cd34SGreg Roach        ];
8175a78cd34SGreg Roach    }
8185a78cd34SGreg Roach
8195a78cd34SGreg Roach    /**
8205a78cd34SGreg Roach     * @param Request $request
821b6db7c1fSGreg Roach     * @param Tree    $tree
8225a78cd34SGreg Roach     *
8235a78cd34SGreg Roach     * @return RedirectResponse
8245a78cd34SGreg Roach     */
825b6db7c1fSGreg Roach    public function postAddRepositoryAction(Request $request, Tree $tree): RedirectResponse
826c1010edaSGreg Roach    {
827*9e648e55SGreg Roach        $xref = $request->get('xref', '');
8285a78cd34SGreg Roach
8295a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
8305a78cd34SGreg Roach
8315a78cd34SGreg Roach        if ($repository === null) {
83259f2f229SGreg Roach            throw new RepositoryNotFoundException();
8335a78cd34SGreg Roach        }
8345a78cd34SGreg Roach
8355a78cd34SGreg Roach        $this->addRecordToCart($repository);
8365a78cd34SGreg Roach
8375a78cd34SGreg Roach        return new RedirectResponse($repository->url());
8385a78cd34SGreg Roach    }
8395a78cd34SGreg Roach
8405a78cd34SGreg Roach    /**
8415a78cd34SGreg Roach     * @param Request $request
842b6db7c1fSGreg Roach     * @param Tree    $tree
8435a78cd34SGreg Roach     *
8445a78cd34SGreg Roach     * @return Response
8455a78cd34SGreg Roach     */
846b6db7c1fSGreg Roach    public function getAddSourceAction(Request $request, Tree $tree): Response
847c1010edaSGreg Roach    {
848*9e648e55SGreg Roach        $xref = $request->get('xref', '');
8495a78cd34SGreg Roach
8505a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
8515a78cd34SGreg Roach
8525a78cd34SGreg Roach        if ($source === null) {
85359f2f229SGreg Roach            throw new SourceNotFoundException();
8545a78cd34SGreg Roach        }
8555a78cd34SGreg Roach
8565a78cd34SGreg Roach        $options = $this->sourceOptions($source);
8575a78cd34SGreg Roach
8585a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $source->getFullName());
8595a78cd34SGreg Roach
8605a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
8615a78cd34SGreg Roach            'options' => $options,
8625a78cd34SGreg Roach            'default' => key($options),
8635a78cd34SGreg Roach            'record'  => $source,
8645a78cd34SGreg Roach            'title'   => $title,
8655a78cd34SGreg Roach            'tree'    => $tree,
8665a78cd34SGreg Roach        ]);
8675a78cd34SGreg Roach    }
8685a78cd34SGreg Roach
8695a78cd34SGreg Roach    /**
8705a78cd34SGreg Roach     * @param Source $source
8715a78cd34SGreg Roach     *
8725a78cd34SGreg Roach     * @return string[]
8735a78cd34SGreg Roach     */
874c1010edaSGreg Roach    private function sourceOptions(Source $source): array
875c1010edaSGreg Roach    {
8765a78cd34SGreg Roach        $name = strip_tags($source->getFullName());
8775a78cd34SGreg Roach
8785a78cd34SGreg Roach        return [
8795a78cd34SGreg Roach            'only'   => strip_tags($source->getFullName()),
8805a78cd34SGreg Roach            'linked' => I18N::translate('%s and the individuals that reference it.', $name),
8815a78cd34SGreg Roach        ];
8825a78cd34SGreg Roach    }
8835a78cd34SGreg Roach
8845a78cd34SGreg Roach    /**
8855a78cd34SGreg Roach     * @param Request $request
886b6db7c1fSGreg Roach     * @param Tree    $tree
8875a78cd34SGreg Roach     *
8885a78cd34SGreg Roach     * @return RedirectResponse
8895a78cd34SGreg Roach     */
890b6db7c1fSGreg Roach    public function postAddSourceAction(Request $request, Tree $tree): RedirectResponse
891c1010edaSGreg Roach    {
892*9e648e55SGreg Roach        $xref   = $request->get('xref', '');
893*9e648e55SGreg Roach        $option = $request->get('option', '');
8945a78cd34SGreg Roach
8955a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
8965a78cd34SGreg Roach
8975a78cd34SGreg Roach        if ($source === null) {
89859f2f229SGreg Roach            throw new SourceNotFoundException();
8995a78cd34SGreg Roach        }
9005a78cd34SGreg Roach
9015a78cd34SGreg Roach        $this->addRecordToCart($source);
9025a78cd34SGreg Roach
9035a78cd34SGreg Roach        if ($option === 'linked') {
9045a78cd34SGreg Roach            foreach ($source->linkedIndividuals('SOUR') as $individual) {
9055a78cd34SGreg Roach                $this->addRecordToCart($individual);
9065a78cd34SGreg Roach            }
9075a78cd34SGreg Roach            foreach ($source->linkedFamilies('SOUR') as $family) {
9085a78cd34SGreg Roach                $this->addRecordToCart($family);
9095a78cd34SGreg Roach            }
9105a78cd34SGreg Roach        }
9115a78cd34SGreg Roach
9125a78cd34SGreg Roach        return new RedirectResponse($source->url());
9135a78cd34SGreg Roach    }
9145a78cd34SGreg Roach
9155a78cd34SGreg Roach    /**
9165a78cd34SGreg Roach     * Get all the records in the cart.
9175a78cd34SGreg Roach     *
9185a78cd34SGreg Roach     * @param Tree $tree
9195a78cd34SGreg Roach     *
9205a78cd34SGreg Roach     * @return GedcomRecord[]
9215a78cd34SGreg Roach     */
922c1010edaSGreg Roach    private function allRecordsInCart(Tree $tree): array
923c1010edaSGreg Roach    {
9245a78cd34SGreg Roach        $cart = Session::get('cart', []);
9255a78cd34SGreg Roach
9265a78cd34SGreg Roach        $xrefs = array_keys($cart[$tree->getName()] ?? []);
9275a78cd34SGreg Roach
9285a78cd34SGreg Roach        // Fetch all the records in the cart.
92918d7a90dSGreg Roach        $records = array_map(function (string $xref) use ($tree): GedcomRecord {
9305a78cd34SGreg Roach            return GedcomRecord::getInstance($xref, $tree);
9315a78cd34SGreg Roach        }, $xrefs);
9325a78cd34SGreg Roach
9335a78cd34SGreg Roach        // Some records may have been deleted after they were added to the cart.
9345a78cd34SGreg Roach        $records = array_filter($records);
9355a78cd34SGreg Roach
9365a78cd34SGreg Roach        // Group and sort.
93718d7a90dSGreg Roach        uasort($records, function (GedcomRecord $x, GedcomRecord $y): int {
9385a78cd34SGreg Roach            return $x::RECORD_TYPE <=> $y::RECORD_TYPE ?: GedcomRecord::compare($x, $y);
9395a78cd34SGreg Roach        });
9405a78cd34SGreg Roach
9415a78cd34SGreg Roach        return $records;
9425a78cd34SGreg Roach    }
9435a78cd34SGreg Roach
9445a78cd34SGreg Roach    /**
9455a78cd34SGreg Roach     * Add a record (and direclty linked sources, notes, etc. to the cart.
9465a78cd34SGreg Roach     *
9475a78cd34SGreg Roach     * @param GedcomRecord $record
94818d7a90dSGreg Roach     *
94918d7a90dSGreg Roach     * @return void
9505a78cd34SGreg Roach     */
951c1010edaSGreg Roach    private function addRecordToCart(GedcomRecord $record)
952c1010edaSGreg Roach    {
9535a78cd34SGreg Roach        $cart = Session::get('cart', []);
9545a78cd34SGreg Roach
9555a78cd34SGreg Roach        $tree_name = $record->getTree()->getName();
9565a78cd34SGreg Roach
9575a78cd34SGreg Roach        // Add this record
9585a78cd34SGreg Roach        $cart[$tree_name][$record->getXref()] = true;
9595a78cd34SGreg Roach
9605a78cd34SGreg Roach        // Add directly linked media, notes, repositories and sources.
9615a78cd34SGreg Roach        preg_match_all('/\n\d (?:OBJE|NOTE|SOUR|REPO) @(' . WT_REGEX_XREF . ')@/', $record->getGedcom(), $matches);
9625a78cd34SGreg Roach
9635a78cd34SGreg Roach        foreach ($matches[1] as $match) {
9645a78cd34SGreg Roach            $cart[$tree_name][$match] = true;
9655a78cd34SGreg Roach        }
9665a78cd34SGreg Roach
9675a78cd34SGreg Roach        Session::put('cart', $cart);
9685a78cd34SGreg Roach    }
9695a78cd34SGreg Roach
9705a78cd34SGreg Roach    /**
9715a78cd34SGreg Roach     * @param Tree $tree
9725a78cd34SGreg Roach     *
9735a78cd34SGreg Roach     * @return bool
9745a78cd34SGreg Roach     */
975c1010edaSGreg Roach    private function isCartEmpty(Tree $tree): bool
976c1010edaSGreg Roach    {
9775a78cd34SGreg Roach        $cart = Session::get('cart', []);
9785a78cd34SGreg Roach
9795a78cd34SGreg Roach        return empty($cart[$tree->getName()]);
9805a78cd34SGreg Roach    }
9815a78cd34SGreg Roach
9825a78cd34SGreg Roach    /**
9835a78cd34SGreg Roach     * Only allow access to the routes/functions if the menu is active
9845a78cd34SGreg Roach     *
9855a78cd34SGreg Roach     * @param Tree $tree
98618d7a90dSGreg Roach     *
98718d7a90dSGreg Roach     * @return void
98818d7a90dSGreg Roach     *
98918d7a90dSGreg Roach     * @throws NoteNotFoundException
9905a78cd34SGreg Roach     */
991c1010edaSGreg Roach    private function checkModuleAccess(Tree $tree)
992c1010edaSGreg Roach    {
9935a78cd34SGreg Roach        if (!array_key_exists($this->getName(), Module::getActiveMenus($tree))) {
99459f2f229SGreg Roach            throw new NotFoundHttpException();
9955a78cd34SGreg Roach        }
9968c2e8227SGreg Roach    }
9978c2e8227SGreg Roach}
998