xref: /webtrees/app/Module/ClippingsCartModule.php (revision c156e8f563be94a0ce9cac0af05ca20d19b86053)
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
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
210bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\FamilyNotFoundException;
220bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualNotFoundException;
230bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\MediaNotFoundException;
240bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\NoteNotFoundException;
250bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\RepositoryNotFoundException;
260bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\SourceNotFoundException;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Family;
285a78cd34SGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsExport;
295a78cd34SGreg Roachuse Fisharebest\Webtrees\Gedcom;
300e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
310e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
320e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
335a78cd34SGreg Roachuse Fisharebest\Webtrees\Media;
340e62c4b8SGreg Roachuse Fisharebest\Webtrees\Menu;
351eca16b3SGreg Roachuse Fisharebest\Webtrees\Module;
365a78cd34SGreg Roachuse Fisharebest\Webtrees\Note;
375a78cd34SGreg Roachuse Fisharebest\Webtrees\Repository;
380e62c4b8SGreg Roachuse Fisharebest\Webtrees\Session;
395a78cd34SGreg Roachuse Fisharebest\Webtrees\Source;
40aee13b6dSGreg Roachuse Fisharebest\Webtrees\Tree;
415a78cd34SGreg Roachuse Fisharebest\Webtrees\User;
425a78cd34SGreg Roachuse League\Flysystem\Filesystem;
435a78cd34SGreg Roachuse League\Flysystem\ZipArchive\ZipArchiveAdapter;
44a3d8780cSGreg Roachuse function str_replace;
455a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\BinaryFileResponse;
465a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse;
475a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\Request;
485a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\Response;
495a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\ResponseHeaderBag;
505a78cd34SGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
518c2e8227SGreg Roach
528c2e8227SGreg Roach/**
538c2e8227SGreg Roach * Class ClippingsCartModule
548c2e8227SGreg Roach */
55c1010edaSGreg Roachclass ClippingsCartModule extends AbstractModule implements ModuleMenuInterface
56c1010edaSGreg 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
678c2e8227SGreg Roach    /** {@inheritdoc} */
688f53f488SRico Sonntag    public function getTitle(): string
69c1010edaSGreg Roach    {
70bbb76c12SGreg Roach        /* I18N: Name of a module */
71bbb76c12SGreg Roach        return I18N::translate('Clippings cart');
728c2e8227SGreg Roach    }
738c2e8227SGreg Roach
748c2e8227SGreg Roach    /** {@inheritdoc} */
758f53f488SRico Sonntag    public function getDescription(): string
76c1010edaSGreg Roach    {
77bbb76c12SGreg Roach        /* I18N: Description of the “Clippings cart” module */
78bbb76c12SGreg Roach        return I18N::translate('Select records from your family tree and save them as a GEDCOM file.');
798c2e8227SGreg Roach    }
808c2e8227SGreg Roach
810ee13198SGreg Roach    /**
820ee13198SGreg Roach     * What is the default access level for this module?
830ee13198SGreg Roach     *
840ee13198SGreg Roach     * Some modules are aimed at admins or managers, and are not generally shown to users.
850ee13198SGreg Roach     *
860ee13198SGreg Roach     * @return int
870ee13198SGreg Roach     */
888f53f488SRico Sonntag    public function defaultAccessLevel(): int
89c1010edaSGreg Roach    {
904b9ff166SGreg Roach        return Auth::PRIV_USER;
918c2e8227SGreg Roach    }
928c2e8227SGreg Roach
9376692c8bSGreg Roach    /**
940ee13198SGreg Roach     * The user can re-order menus. Until they do, they are shown in this order.
950ee13198SGreg Roach     *
960ee13198SGreg Roach     * @return int
970ee13198SGreg Roach     */
988f53f488SRico Sonntag    public function defaultMenuOrder(): int
99c1010edaSGreg Roach    {
1008c2e8227SGreg Roach        return 20;
1018c2e8227SGreg Roach    }
1028c2e8227SGreg Roach
1030ee13198SGreg Roach    /**
1040ee13198SGreg Roach     * A menu, to be added to the main application menu.
1050ee13198SGreg Roach     *
106aee13b6dSGreg Roach     * @param Tree $tree
107aee13b6dSGreg Roach     *
1080ee13198SGreg Roach     * @return Menu|null
1090ee13198SGreg Roach     */
110c1010edaSGreg Roach    public function getMenu(Tree $tree)
111c1010edaSGreg Roach    {
1125a78cd34SGreg Roach        $request = Request::createFromGlobals();
1138c2e8227SGreg Roach
1149e648e55SGreg Roach        $route = $request->get('route', '');
1155a78cd34SGreg Roach
1165a78cd34SGreg Roach        $submenus = [
117c1010edaSGreg Roach            new Menu($this->getTitle(), route('module', [
118c1010edaSGreg Roach                'module' => 'clippings',
119c1010edaSGreg Roach                'action' => 'Show',
120aa6f03bbSGreg Roach                'ged'    => $tree->name(),
121c1010edaSGreg Roach            ]), 'menu-clippings-cart', ['rel' => 'nofollow']),
1225a78cd34SGreg Roach        ];
1235a78cd34SGreg Roach
1245a78cd34SGreg Roach        if (in_array($route, self::ROUTES_WITH_RECORDS)) {
1259e648e55SGreg Roach            $xref      = $request->get('xref', '');
1265a78cd34SGreg Roach            $action    = 'Add' . ucfirst($route);
127c1010edaSGreg Roach            $add_route = route('module', [
128c1010edaSGreg Roach                'module' => 'clippings',
129c1010edaSGreg Roach                'action' => $action,
130c1010edaSGreg Roach                'xref'   => $xref,
131aa6f03bbSGreg Roach                'ged'    => $tree->name(),
132c1010edaSGreg Roach            ]);
1335a78cd34SGreg Roach
13425b2dde3SGreg Roach            $submenus[] = new Menu(I18N::translate('Add to the clippings cart'), $add_route, 'menu-clippings-add', ['rel' => 'nofollow']);
1358c2e8227SGreg Roach        }
136cbc1590aSGreg Roach
1375a78cd34SGreg Roach        if (!$this->isCartEmpty($tree)) {
138c1010edaSGreg Roach            $submenus[] = new Menu(I18N::translate('Empty the clippings cart'), route('module', [
139c1010edaSGreg Roach                'module' => 'clippings',
140c1010edaSGreg Roach                'action' => 'Empty',
141aa6f03bbSGreg Roach                'ged'    => $tree->name(),
142c1010edaSGreg Roach            ]), 'menu-clippings-empty', ['rel' => 'nofollow']);
143c1010edaSGreg Roach            $submenus[] = new Menu(I18N::translate('Download'), route('module', [
144c1010edaSGreg Roach                'module' => 'clippings',
145c1010edaSGreg Roach                'action' => 'DownloadForm',
146aa6f03bbSGreg Roach                'ged'    => $tree->name(),
147c1010edaSGreg Roach            ]), 'menu-clippings-download', ['rel' => 'nofollow']);
1485a78cd34SGreg Roach        }
1495a78cd34SGreg Roach
15013abd6f3SGreg Roach        return new Menu($this->getTitle(), '#', 'menu-clippings', ['rel' => 'nofollow'], $submenus);
1518c2e8227SGreg Roach    }
1528c2e8227SGreg Roach
15376692c8bSGreg Roach    /**
1545a78cd34SGreg Roach     * @param Request $request
155b6db7c1fSGreg Roach     * @param Tree    $tree
15676692c8bSGreg Roach     *
1575a78cd34SGreg Roach     * @return BinaryFileResponse
15876692c8bSGreg Roach     */
159b6db7c1fSGreg Roach    public function getDownloadAction(Request $request, Tree $tree): BinaryFileResponse
160c1010edaSGreg Roach    {
1615a78cd34SGreg Roach        $this->checkModuleAccess($tree);
1628c2e8227SGreg Roach
1639e648e55SGreg Roach        $privatize_export = $request->get('privatize_export', '');
1645a78cd34SGreg Roach        $convert          = (bool) $request->get('convert');
1658c2e8227SGreg Roach
16613abd6f3SGreg Roach        $cart = Session::get('cart', []);
1678c2e8227SGreg Roach
168aa6f03bbSGreg Roach        $xrefs = array_keys($cart[$tree->name()] ?? []);
1695a78cd34SGreg Roach
1705a78cd34SGreg Roach        // Create a new/empty .ZIP file
1715a78cd34SGreg Roach        $temp_zip_file  = tempnam(sys_get_temp_dir(), 'webtrees-zip-');
1725a78cd34SGreg Roach        $zip_filesystem = new Filesystem(new ZipArchiveAdapter($temp_zip_file));
1735a78cd34SGreg Roach
1745a78cd34SGreg Roach        // Media file prefix
1755a78cd34SGreg Roach        $path = $tree->getPreference('MEDIA_DIRECTORY');
1765a78cd34SGreg Roach
1775a78cd34SGreg Roach        // GEDCOM file header
178a3d8780cSGreg Roach        $filetext = FunctionsExport::gedcomHeader($tree, $convert ? 'ANSI' : 'UTF-8');
1795a78cd34SGreg Roach
1805a78cd34SGreg Roach        switch ($privatize_export) {
1815a78cd34SGreg Roach            case 'gedadmin':
1825a78cd34SGreg Roach                $access_level = Auth::PRIV_NONE;
1835a78cd34SGreg Roach                break;
1845a78cd34SGreg Roach            case 'user':
1855a78cd34SGreg Roach                $access_level = Auth::PRIV_USER;
1865a78cd34SGreg Roach                break;
1875a78cd34SGreg Roach            case 'visitor':
1885a78cd34SGreg Roach                $access_level = Auth::PRIV_PRIVATE;
1895a78cd34SGreg Roach                break;
1905a78cd34SGreg Roach            case 'none':
1915a78cd34SGreg Roach            default:
1925a78cd34SGreg Roach                $access_level = Auth::PRIV_HIDE;
1935a78cd34SGreg Roach                break;
1945a78cd34SGreg Roach        }
1955a78cd34SGreg Roach
1965a78cd34SGreg Roach        foreach ($xrefs as $xref) {
1975a78cd34SGreg Roach            $object = GedcomRecord::getInstance($xref, $tree);
1985a78cd34SGreg Roach            // The object may have been deleted since we added it to the cart....
1995a78cd34SGreg Roach            if ($object) {
2005a78cd34SGreg Roach                $record = $object->privatizeGedcom($access_level);
2015a78cd34SGreg Roach                // Remove links to objects that aren't in the cart
2028d0ebef0SGreg Roach                preg_match_all('/\n1 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[2-9].*)*/', $record, $matches, PREG_SET_ORDER);
2035a78cd34SGreg Roach                foreach ($matches as $match) {
2045a78cd34SGreg Roach                    if (!array_key_exists($match[1], $xrefs)) {
2055a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2065a78cd34SGreg Roach                    }
2075a78cd34SGreg Roach                }
2088d0ebef0SGreg Roach                preg_match_all('/\n2 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[3-9].*)*/', $record, $matches, PREG_SET_ORDER);
2095a78cd34SGreg Roach                foreach ($matches as $match) {
2105a78cd34SGreg Roach                    if (!array_key_exists($match[1], $xrefs)) {
2115a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2125a78cd34SGreg Roach                    }
2135a78cd34SGreg Roach                }
2148d0ebef0SGreg Roach                preg_match_all('/\n3 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[4-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                }
2205a78cd34SGreg Roach
22155167344SGreg Roach                if ($object instanceof Individual || $object instanceof Family) {
2225a78cd34SGreg Roach                    $filetext .= $record . "\n";
2235a78cd34SGreg Roach                    $filetext .= "1 SOUR @WEBTREES@\n";
2241f273236SGreg Roach                    $filetext .= '2 PAGE ' . $object->url() . "\n";
22555167344SGreg Roach                } elseif ($object instanceof Source) {
2265a78cd34SGreg Roach                    $filetext .= $record . "\n";
2271f273236SGreg Roach                    $filetext .= '1 NOTE ' . $object->url() . "\n";
22855167344SGreg Roach                } elseif ($object instanceof Media) {
22955167344SGreg Roach                    // Add the media files to the archive
2305a78cd34SGreg Roach                    foreach ($object->mediaFiles() as $media_file) {
2315a78cd34SGreg Roach                        if (file_exists($media_file->getServerFilename())) {
2325a78cd34SGreg Roach                            $fp = fopen($media_file->getServerFilename(), 'r');
2335a78cd34SGreg Roach                            $zip_filesystem->writeStream($path . $media_file->filename(), $fp);
2345a78cd34SGreg Roach                            fclose($fp);
2355a78cd34SGreg Roach                        }
2365a78cd34SGreg Roach                    }
2375a78cd34SGreg Roach                    $filetext .= $record . "\n";
23855167344SGreg Roach                } else {
2395a78cd34SGreg Roach                    $filetext .= $record . "\n";
2408c2e8227SGreg Roach                }
2418c2e8227SGreg Roach            }
2428c2e8227SGreg Roach        }
2438c2e8227SGreg Roach
2445a78cd34SGreg Roach        // Create a source, to indicate the source of the data.
2455a78cd34SGreg Roach        $filetext .= "0 @WEBTREES@ SOUR\n1 TITL " . WT_BASE_URL . "\n";
2467fcd9838SGreg Roach        $author = User::find((int) $tree->getPreference('CONTACT_USER_ID'));
2475a78cd34SGreg Roach        if ($author !== null) {
2485a78cd34SGreg Roach            $filetext .= '1 AUTH ' . $author->getRealName() . "\n";
2495a78cd34SGreg Roach        }
2505a78cd34SGreg Roach        $filetext .= "0 TRLR\n";
2515a78cd34SGreg Roach
2525a78cd34SGreg Roach        // Make sure the preferred line endings are used
253a3d8780cSGreg Roach        $filetext = str_replace('\n', Gedcom::EOL, $filetext);
2545a78cd34SGreg Roach
25555167344SGreg Roach        if ($convert) {
2565a78cd34SGreg Roach            $filetext = utf8_decode($filetext);
2578c2e8227SGreg Roach        }
258cbc1590aSGreg Roach
2595a78cd34SGreg Roach        // Finally add the GEDCOM file to the .ZIP file.
2605a78cd34SGreg Roach        $zip_filesystem->write('clippings.ged', $filetext);
2615a78cd34SGreg Roach
2625a78cd34SGreg Roach        // Need to force-close the filesystem
26302a92f80SGreg Roach        unset($zip_filesystem);
2645a78cd34SGreg Roach
2655a78cd34SGreg Roach        $response = new BinaryFileResponse($temp_zip_file);
2665a78cd34SGreg Roach        $response->deleteFileAfterSend(true);
2675a78cd34SGreg Roach
2685a78cd34SGreg Roach        $response->headers->set('Content-Type', 'application/zip');
2695a78cd34SGreg Roach        $response->setContentDisposition(
2705a78cd34SGreg Roach            ResponseHeaderBag::DISPOSITION_ATTACHMENT,
2715a78cd34SGreg Roach            'clippings.zip'
2725a78cd34SGreg Roach        );
2735a78cd34SGreg Roach
2745a78cd34SGreg Roach        return $response;
2758c2e8227SGreg Roach    }
2768c2e8227SGreg Roach
2778c2e8227SGreg Roach    /**
278b6db7c1fSGreg Roach     * @param Tree $tree
279b6db7c1fSGreg Roach     * @param User $user
28076692c8bSGreg Roach     *
2815a78cd34SGreg Roach     * @return Response
2828c2e8227SGreg Roach     */
283b6db7c1fSGreg Roach    public function getDownloadFormAction(Tree $tree, User $user): Response
284c1010edaSGreg Roach    {
2855a78cd34SGreg Roach        $title = I18N::translate('Family tree clippings cart') . ' — ' . I18N::translate('Download');
2868c2e8227SGreg Roach
2875a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/download', [
2885a78cd34SGreg Roach            'is_manager' => Auth::isManager($tree, $user),
2895a78cd34SGreg Roach            'is_member'  => Auth::isMember($tree, $user),
2905a78cd34SGreg Roach            'title'      => $title,
2915a78cd34SGreg Roach        ]);
2928c2e8227SGreg Roach    }
2938c2e8227SGreg Roach
2945a78cd34SGreg Roach    /**
295b6db7c1fSGreg Roach     * @param Tree $tree
2965a78cd34SGreg Roach     *
2975a78cd34SGreg Roach     * @return RedirectResponse
2985a78cd34SGreg Roach     */
299b6db7c1fSGreg Roach    public function getEmptyAction(Tree $tree): RedirectResponse
300c1010edaSGreg Roach    {
3015a78cd34SGreg Roach        $cart                = Session::get('cart', []);
302aa6f03bbSGreg Roach        $cart[$tree->name()] = [];
3035a78cd34SGreg Roach        Session::put('cart', $cart);
3048c2e8227SGreg Roach
305c1010edaSGreg Roach        $url = route('module', [
306c1010edaSGreg Roach            'module' => 'clippings',
307c1010edaSGreg Roach            'action' => 'Show',
308aa6f03bbSGreg Roach            'ged'    => $tree->name(),
309c1010edaSGreg Roach        ]);
3105a78cd34SGreg Roach
3115a78cd34SGreg Roach        return new RedirectResponse($url);
3125a78cd34SGreg Roach    }
3135a78cd34SGreg Roach
3145a78cd34SGreg Roach    /**
3155a78cd34SGreg Roach     * @param Request $request
316b6db7c1fSGreg Roach     * @param Tree    $tree
3175a78cd34SGreg Roach     *
3185a78cd34SGreg Roach     * @return RedirectResponse
3195a78cd34SGreg Roach     */
320b6db7c1fSGreg Roach    public function postRemoveAction(Request $request, Tree $tree): RedirectResponse
321c1010edaSGreg Roach    {
3229e648e55SGreg Roach        $xref = $request->get('xref', '');
3235a78cd34SGreg Roach
3245a78cd34SGreg Roach        $cart = Session::get('cart', []);
325aa6f03bbSGreg Roach        unset($cart[$tree->name()][$xref]);
3265a78cd34SGreg Roach        Session::put('cart', $cart);
3275a78cd34SGreg Roach
328c1010edaSGreg Roach        $url = route('module', [
329c1010edaSGreg Roach            'module' => 'clippings',
330c1010edaSGreg Roach            'action' => 'Show',
331aa6f03bbSGreg Roach            'ged'    => $tree->name(),
332c1010edaSGreg Roach        ]);
3335a78cd34SGreg Roach
3345a78cd34SGreg Roach        return new RedirectResponse($url);
3355a78cd34SGreg Roach    }
3365a78cd34SGreg Roach
3375a78cd34SGreg Roach    /**
338b6db7c1fSGreg Roach     * @param Tree $tree
3395a78cd34SGreg Roach     *
3405a78cd34SGreg Roach     * @return Response
3415a78cd34SGreg Roach     */
342b6db7c1fSGreg Roach    public function getShowAction(Tree $tree): Response
343c1010edaSGreg Roach    {
3445a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/show', [
3455a78cd34SGreg Roach            'records' => $this->allRecordsInCart($tree),
3465a78cd34SGreg Roach            'title'   => I18N::translate('Family tree clippings cart'),
3475a78cd34SGreg Roach            'tree'    => $tree,
3485a78cd34SGreg Roach        ]);
3495a78cd34SGreg Roach    }
3505a78cd34SGreg Roach
3515a78cd34SGreg Roach    /**
3525a78cd34SGreg Roach     * @param Request $request
353b6db7c1fSGreg Roach     * @param Tree    $tree
3545a78cd34SGreg Roach     *
3555a78cd34SGreg Roach     * @return Response
3565a78cd34SGreg Roach     */
357b6db7c1fSGreg Roach    public function getAddFamilyAction(Request $request, Tree $tree): Response
358c1010edaSGreg Roach    {
3599e648e55SGreg Roach        $xref = $request->get('xref', '');
3605a78cd34SGreg Roach
3615a78cd34SGreg Roach        $family = Family::getInstance($xref, $tree);
3625a78cd34SGreg Roach
3635a78cd34SGreg Roach        if ($family === null) {
36459f2f229SGreg Roach            throw new FamilyNotFoundException();
3655a78cd34SGreg Roach        }
3665a78cd34SGreg Roach
3675a78cd34SGreg Roach        $options = $this->familyOptions($family);
3685a78cd34SGreg Roach
3695a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $family->getFullName());
3705a78cd34SGreg Roach
3715a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
3725a78cd34SGreg Roach            'options' => $options,
3735a78cd34SGreg Roach            'default' => key($options),
3745a78cd34SGreg Roach            'record'  => $family,
3755a78cd34SGreg Roach            'title'   => $title,
3765a78cd34SGreg Roach            'tree'    => $tree,
3775a78cd34SGreg Roach        ]);
3785a78cd34SGreg Roach    }
3795a78cd34SGreg Roach
3805a78cd34SGreg Roach    /**
3815a78cd34SGreg Roach     * @param Family $family
3825a78cd34SGreg Roach     *
3835a78cd34SGreg Roach     * @return string[]
3845a78cd34SGreg Roach     */
385c1010edaSGreg Roach    private function familyOptions(Family $family): array
386c1010edaSGreg Roach    {
3875a78cd34SGreg Roach        $name = strip_tags($family->getFullName());
3885a78cd34SGreg Roach
3895a78cd34SGreg Roach        return [
3905a78cd34SGreg Roach            'parents'     => $name,
391bbb76c12SGreg Roach            /* I18N: %s is a family (husband + wife) */
392bbb76c12SGreg Roach            'members'     => I18N::translate('%s and their children', $name),
393bbb76c12SGreg Roach            /* I18N: %s is a family (husband + wife) */
394bbb76c12SGreg Roach            'descendants' => I18N::translate('%s and their descendants', $name),
3955a78cd34SGreg Roach        ];
3965a78cd34SGreg Roach    }
3975a78cd34SGreg Roach
3985a78cd34SGreg Roach    /**
3995a78cd34SGreg Roach     * @param Request $request
400b6db7c1fSGreg Roach     * @param Tree    $tree
4015a78cd34SGreg Roach     *
4025a78cd34SGreg Roach     * @return RedirectResponse
4035a78cd34SGreg Roach     */
404b6db7c1fSGreg Roach    public function postAddFamilyAction(Request $request, Tree $tree): RedirectResponse
405c1010edaSGreg Roach    {
4069e648e55SGreg Roach        $xref   = $request->get('xref', '');
4079e648e55SGreg Roach        $option = $request->get('option', '');
4085a78cd34SGreg Roach
4095a78cd34SGreg Roach        $family = Family::getInstance($xref, $tree);
4105a78cd34SGreg Roach
4115a78cd34SGreg Roach        if ($family === null) {
41259f2f229SGreg Roach            throw new FamilyNotFoundException();
4135a78cd34SGreg Roach        }
4145a78cd34SGreg Roach
4155a78cd34SGreg Roach        switch ($option) {
4165a78cd34SGreg Roach            case 'parents':
4175a78cd34SGreg Roach                $this->addFamilyToCart($family);
4185a78cd34SGreg Roach                break;
4195a78cd34SGreg Roach
4205a78cd34SGreg Roach            case 'members':
4215a78cd34SGreg Roach                $this->addFamilyAndChildrenToCart($family);
4225a78cd34SGreg Roach                break;
4235a78cd34SGreg Roach
4245a78cd34SGreg Roach            case 'descendants':
4255a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($family);
4265a78cd34SGreg Roach                break;
4275a78cd34SGreg Roach        }
4285a78cd34SGreg Roach
4295a78cd34SGreg Roach        return new RedirectResponse($family->url());
4305a78cd34SGreg Roach    }
4315a78cd34SGreg Roach
4325a78cd34SGreg Roach    /**
4335a78cd34SGreg Roach     * @param Family $family
43418d7a90dSGreg Roach     *
43518d7a90dSGreg Roach     * @return void
4365a78cd34SGreg Roach     */
437c1010edaSGreg Roach    private function addFamilyToCart(Family $family)
438c1010edaSGreg Roach    {
4395a78cd34SGreg Roach        $this->addRecordToCart($family);
4405a78cd34SGreg Roach
4415a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
4425a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4435a78cd34SGreg Roach        }
4445a78cd34SGreg Roach    }
4455a78cd34SGreg Roach
4465a78cd34SGreg Roach    /**
4475a78cd34SGreg Roach     * @param Family $family
44818d7a90dSGreg Roach     *
44918d7a90dSGreg Roach     * @return void
4505a78cd34SGreg Roach     */
451c1010edaSGreg Roach    private function addFamilyAndChildrenToCart(Family $family)
452c1010edaSGreg Roach    {
4535a78cd34SGreg Roach        $this->addRecordToCart($family);
4545a78cd34SGreg Roach
4555a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
4565a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4575a78cd34SGreg Roach        }
4585a78cd34SGreg Roach        foreach ($family->getChildren() as $child) {
4595a78cd34SGreg Roach            $this->addRecordToCart($child);
4605a78cd34SGreg Roach        }
4615a78cd34SGreg Roach    }
4625a78cd34SGreg Roach
4635a78cd34SGreg Roach    /**
4645a78cd34SGreg Roach     * @param Family $family
46518d7a90dSGreg Roach     *
46618d7a90dSGreg Roach     * @return void
4675a78cd34SGreg Roach     */
468c1010edaSGreg Roach    private function addFamilyAndDescendantsToCart(Family $family)
469c1010edaSGreg Roach    {
4705a78cd34SGreg Roach        $this->addRecordToCart($family);
4715a78cd34SGreg Roach
4725a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
4735a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4745a78cd34SGreg Roach        }
4755a78cd34SGreg Roach        foreach ($family->getChildren() as $child) {
4765a78cd34SGreg Roach            $this->addRecordToCart($child);
4775a78cd34SGreg Roach            foreach ($child->getSpouseFamilies() as $child_family) {
4785a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($child_family);
4795a78cd34SGreg Roach            }
4805a78cd34SGreg Roach        }
4815a78cd34SGreg Roach    }
4825a78cd34SGreg Roach
4835a78cd34SGreg Roach    /**
4845a78cd34SGreg Roach     * @param Request $request
485b6db7c1fSGreg Roach     * @param Tree    $tree
4865a78cd34SGreg Roach     *
4875a78cd34SGreg Roach     * @return Response
4885a78cd34SGreg Roach     */
489b6db7c1fSGreg Roach    public function getAddIndividualAction(Request $request, Tree $tree): Response
490c1010edaSGreg Roach    {
4919e648e55SGreg Roach        $xref = $request->get('xref', '');
4925a78cd34SGreg Roach
4935a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
4945a78cd34SGreg Roach
4955a78cd34SGreg Roach        if ($individual === null) {
49659f2f229SGreg Roach            throw new IndividualNotFoundException();
4975a78cd34SGreg Roach        }
4985a78cd34SGreg Roach
4995a78cd34SGreg Roach        $options = $this->individualOptions($individual);
5005a78cd34SGreg Roach
5015a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $individual->getFullName());
5025a78cd34SGreg Roach
5035a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
5045a78cd34SGreg Roach            'options' => $options,
5055a78cd34SGreg Roach            'default' => key($options),
5065a78cd34SGreg Roach            'record'  => $individual,
5075a78cd34SGreg Roach            'title'   => $title,
5085a78cd34SGreg Roach            'tree'    => $tree,
5095a78cd34SGreg Roach        ]);
5105a78cd34SGreg Roach    }
5115a78cd34SGreg Roach
5125a78cd34SGreg Roach    /**
5135a78cd34SGreg Roach     * @param Individual $individual
5145a78cd34SGreg Roach     *
5155a78cd34SGreg Roach     * @return string[]
5165a78cd34SGreg Roach     */
517c1010edaSGreg Roach    private function individualOptions(Individual $individual): array
518c1010edaSGreg Roach    {
5195a78cd34SGreg Roach        $name = strip_tags($individual->getFullName());
5205a78cd34SGreg Roach
5215a78cd34SGreg Roach        if ($individual->getSex() === 'F') {
5225a78cd34SGreg Roach            return [
5235a78cd34SGreg Roach                'self'              => $name,
5245a78cd34SGreg Roach                'parents'           => I18N::translate('%s, her parents and siblings', $name),
5255a78cd34SGreg Roach                'spouses'           => I18N::translate('%s, her spouses and children', $name),
5265a78cd34SGreg Roach                'ancestors'         => I18N::translate('%s and her ancestors', $name),
5275a78cd34SGreg Roach                'ancestor_families' => I18N::translate('%s, her ancestors and their families', $name),
5285a78cd34SGreg Roach                'descendants'       => I18N::translate('%s, her spouses and descendants', $name),
5295a78cd34SGreg Roach            ];
530b2ce94c6SRico Sonntag        }
531b2ce94c6SRico Sonntag
5325a78cd34SGreg Roach        return [
5335a78cd34SGreg Roach            'self'              => $name,
5345a78cd34SGreg Roach            'parents'           => I18N::translate('%s, his parents and siblings', $name),
5355a78cd34SGreg Roach            'spouses'           => I18N::translate('%s, his spouses and children', $name),
5365a78cd34SGreg Roach            'ancestors'         => I18N::translate('%s and his ancestors', $name),
5375a78cd34SGreg Roach            'ancestor_families' => I18N::translate('%s, his ancestors and their families', $name),
5385a78cd34SGreg Roach            'descendants'       => I18N::translate('%s, his spouses and descendants', $name),
5395a78cd34SGreg Roach        ];
5405a78cd34SGreg Roach    }
5415a78cd34SGreg Roach
5425a78cd34SGreg Roach    /**
5435a78cd34SGreg Roach     * @param Request $request
544b6db7c1fSGreg Roach     * @param Tree    $tree
5455a78cd34SGreg Roach     *
5465a78cd34SGreg Roach     * @return RedirectResponse
5475a78cd34SGreg Roach     */
548b6db7c1fSGreg Roach    public function postAddIndividualAction(Request $request, Tree $tree): RedirectResponse
549c1010edaSGreg Roach    {
5509e648e55SGreg Roach        $xref   = $request->get('xref', '');
5519e648e55SGreg Roach        $option = $request->get('option', '');
5525a78cd34SGreg Roach
5535a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
5545a78cd34SGreg Roach
5555a78cd34SGreg Roach        if ($individual === null) {
55659f2f229SGreg Roach            throw new IndividualNotFoundException();
5575a78cd34SGreg Roach        }
5585a78cd34SGreg Roach
5595a78cd34SGreg Roach        switch ($option) {
5605a78cd34SGreg Roach            case 'self':
5615a78cd34SGreg Roach                $this->addRecordToCart($individual);
5625a78cd34SGreg Roach                break;
5635a78cd34SGreg Roach
5645a78cd34SGreg Roach            case 'parents':
5655a78cd34SGreg Roach                foreach ($individual->getChildFamilies() as $family) {
5665a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
5675a78cd34SGreg Roach                }
5685a78cd34SGreg Roach                break;
5695a78cd34SGreg Roach
5705a78cd34SGreg Roach            case 'spouses':
5715a78cd34SGreg Roach                foreach ($individual->getSpouseFamilies() as $family) {
5725a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
5735a78cd34SGreg Roach                }
5745a78cd34SGreg Roach                break;
5755a78cd34SGreg Roach
5765a78cd34SGreg Roach            case 'ancestors':
5775a78cd34SGreg Roach                $this->addAncestorsToCart($individual);
5785a78cd34SGreg Roach                break;
5795a78cd34SGreg Roach
5805a78cd34SGreg Roach            case 'ancestor_families':
5815a78cd34SGreg Roach                $this->addAncestorFamiliesToCart($individual);
5825a78cd34SGreg Roach                break;
5835a78cd34SGreg Roach
5845a78cd34SGreg Roach            case 'descendants':
5855a78cd34SGreg Roach                foreach ($individual->getSpouseFamilies() as $family) {
5865a78cd34SGreg Roach                    $this->addFamilyAndDescendantsToCart($family);
5875a78cd34SGreg Roach                }
5885a78cd34SGreg Roach                break;
5895a78cd34SGreg Roach        }
5905a78cd34SGreg Roach
5915a78cd34SGreg Roach        return new RedirectResponse($individual->url());
5925a78cd34SGreg Roach    }
5935a78cd34SGreg Roach
5945a78cd34SGreg Roach    /**
5955a78cd34SGreg Roach     * @param Individual $individual
59618d7a90dSGreg Roach     *
59718d7a90dSGreg Roach     * @return void
5985a78cd34SGreg Roach     */
599c1010edaSGreg Roach    private function addAncestorsToCart(Individual $individual)
600c1010edaSGreg Roach    {
6015a78cd34SGreg Roach        $this->addRecordToCart($individual);
6025a78cd34SGreg Roach
6035a78cd34SGreg Roach        foreach ($individual->getChildFamilies() as $family) {
6045a78cd34SGreg Roach            foreach ($family->getSpouses() as $parent) {
6055a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
6065a78cd34SGreg Roach            }
6075a78cd34SGreg Roach        }
6085a78cd34SGreg Roach    }
6095a78cd34SGreg Roach
6105a78cd34SGreg Roach    /**
6115a78cd34SGreg Roach     * @param Individual $individual
61218d7a90dSGreg Roach     *
61318d7a90dSGreg Roach     * @return void
6145a78cd34SGreg Roach     */
615c1010edaSGreg Roach    private function addAncestorFamiliesToCart(Individual $individual)
616c1010edaSGreg Roach    {
6175a78cd34SGreg Roach        foreach ($individual->getChildFamilies() as $family) {
6185a78cd34SGreg Roach            $this->addFamilyAndChildrenToCart($family);
6195a78cd34SGreg Roach            foreach ($family->getSpouses() as $parent) {
6205a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
6215a78cd34SGreg Roach            }
6225a78cd34SGreg Roach        }
6235a78cd34SGreg Roach    }
6245a78cd34SGreg Roach
6255a78cd34SGreg Roach    /**
6265a78cd34SGreg Roach     * @param Request $request
627b6db7c1fSGreg Roach     * @param Tree    $tree
6285a78cd34SGreg Roach     *
6295a78cd34SGreg Roach     * @return Response
6305a78cd34SGreg Roach     */
631b6db7c1fSGreg Roach    public function getAddMediaAction(Request $request, Tree $tree): Response
632c1010edaSGreg Roach    {
6339e648e55SGreg Roach        $xref = $request->get('xref', '');
6345a78cd34SGreg Roach
6355a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
6365a78cd34SGreg Roach
6375a78cd34SGreg Roach        if ($media === null) {
63859f2f229SGreg Roach            throw new MediaNotFoundException();
6395a78cd34SGreg Roach        }
6405a78cd34SGreg Roach
6415a78cd34SGreg Roach        $options = $this->mediaOptions($media);
6425a78cd34SGreg Roach
6435a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $media->getFullName());
6445a78cd34SGreg Roach
6455a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
6465a78cd34SGreg Roach            'options' => $options,
6475a78cd34SGreg Roach            'default' => key($options),
6485a78cd34SGreg Roach            'record'  => $media,
6495a78cd34SGreg Roach            'title'   => $title,
6505a78cd34SGreg Roach            'tree'    => $tree,
6515a78cd34SGreg Roach        ]);
6525a78cd34SGreg Roach    }
6535a78cd34SGreg Roach
6545a78cd34SGreg Roach    /**
6555a78cd34SGreg Roach     * @param Media $media
6565a78cd34SGreg Roach     *
6575a78cd34SGreg Roach     * @return string[]
6585a78cd34SGreg Roach     */
659c1010edaSGreg Roach    private function mediaOptions(Media $media): array
660c1010edaSGreg Roach    {
6615a78cd34SGreg Roach        $name = strip_tags($media->getFullName());
6625a78cd34SGreg Roach
6635a78cd34SGreg Roach        return [
6645a78cd34SGreg Roach            'self' => $name,
6655a78cd34SGreg Roach        ];
6665a78cd34SGreg Roach    }
6675a78cd34SGreg Roach
6685a78cd34SGreg Roach    /**
6695a78cd34SGreg Roach     * @param Request $request
670b6db7c1fSGreg Roach     * @param Tree    $tree
6715a78cd34SGreg Roach     *
6725a78cd34SGreg Roach     * @return RedirectResponse
6735a78cd34SGreg Roach     */
674b6db7c1fSGreg Roach    public function postAddMediaAction(Request $request, Tree $tree): RedirectResponse
675c1010edaSGreg Roach    {
6769e648e55SGreg Roach        $xref = $request->get('xref', '');
6775a78cd34SGreg Roach
6785a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
6795a78cd34SGreg Roach
6805a78cd34SGreg Roach        if ($media === null) {
68159f2f229SGreg Roach            throw new MediaNotFoundException();
6825a78cd34SGreg Roach        }
6835a78cd34SGreg Roach
6845a78cd34SGreg Roach        $this->addRecordToCart($media);
6855a78cd34SGreg Roach
6865a78cd34SGreg Roach        return new RedirectResponse($media->url());
6875a78cd34SGreg Roach    }
6885a78cd34SGreg Roach
6895a78cd34SGreg Roach    /**
6905a78cd34SGreg Roach     * @param Request $request
691b6db7c1fSGreg Roach     * @param Tree    $tree
6925a78cd34SGreg Roach     *
6935a78cd34SGreg Roach     * @return Response
6945a78cd34SGreg Roach     */
695b6db7c1fSGreg Roach    public function getAddNoteAction(Request $request, Tree $tree): Response
696c1010edaSGreg Roach    {
6979e648e55SGreg Roach        $xref = $request->get('xref', '');
6985a78cd34SGreg Roach
6995a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
7005a78cd34SGreg Roach
7015a78cd34SGreg Roach        if ($note === null) {
70259f2f229SGreg Roach            throw new NoteNotFoundException();
7035a78cd34SGreg Roach        }
7045a78cd34SGreg Roach
7055a78cd34SGreg Roach        $options = $this->noteOptions($note);
7065a78cd34SGreg Roach
7075a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $note->getFullName());
7085a78cd34SGreg Roach
7095a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
7105a78cd34SGreg Roach            'options' => $options,
7115a78cd34SGreg Roach            'default' => key($options),
7125a78cd34SGreg Roach            'record'  => $note,
7135a78cd34SGreg Roach            'title'   => $title,
7145a78cd34SGreg Roach            'tree'    => $tree,
7155a78cd34SGreg Roach        ]);
7165a78cd34SGreg Roach    }
7175a78cd34SGreg Roach
7185a78cd34SGreg Roach    /**
7195a78cd34SGreg Roach     * @param Note $note
7205a78cd34SGreg Roach     *
7215a78cd34SGreg Roach     * @return string[]
7225a78cd34SGreg Roach     */
723c1010edaSGreg Roach    private function noteOptions(Note $note): array
724c1010edaSGreg Roach    {
7255a78cd34SGreg Roach        $name = strip_tags($note->getFullName());
7265a78cd34SGreg Roach
7275a78cd34SGreg Roach        return [
7285a78cd34SGreg Roach            'self' => $name,
7295a78cd34SGreg Roach        ];
7305a78cd34SGreg Roach    }
7315a78cd34SGreg Roach
7325a78cd34SGreg Roach    /**
7335a78cd34SGreg Roach     * @param Request $request
734b6db7c1fSGreg Roach     * @param Tree    $tree
7355a78cd34SGreg Roach     *
7365a78cd34SGreg Roach     * @return RedirectResponse
7375a78cd34SGreg Roach     */
738b6db7c1fSGreg Roach    public function postAddNoteAction(Request $request, Tree $tree): RedirectResponse
739c1010edaSGreg Roach    {
7409e648e55SGreg Roach        $xref = $request->get('xref', '');
7415a78cd34SGreg Roach
7425a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
7435a78cd34SGreg Roach
7445a78cd34SGreg Roach        if ($note === null) {
74559f2f229SGreg Roach            throw new NoteNotFoundException();
7465a78cd34SGreg Roach        }
7475a78cd34SGreg Roach
7485a78cd34SGreg Roach        $this->addRecordToCart($note);
7495a78cd34SGreg Roach
7505a78cd34SGreg Roach        return new RedirectResponse($note->url());
7515a78cd34SGreg Roach    }
7525a78cd34SGreg Roach
7535a78cd34SGreg Roach    /**
7545a78cd34SGreg Roach     * @param Request $request
755b6db7c1fSGreg Roach     * @param Tree    $tree
7565a78cd34SGreg Roach     *
7575a78cd34SGreg Roach     * @return Response
7585a78cd34SGreg Roach     */
759b6db7c1fSGreg Roach    public function getAddRepositoryAction(Request $request, Tree $tree): Response
760c1010edaSGreg Roach    {
7619e648e55SGreg Roach        $xref = $request->get('xref', '');
7625a78cd34SGreg Roach
7635a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
7645a78cd34SGreg Roach
7655a78cd34SGreg Roach        if ($repository === null) {
76659f2f229SGreg Roach            throw new RepositoryNotFoundException();
7675a78cd34SGreg Roach        }
7685a78cd34SGreg Roach
7695a78cd34SGreg Roach        $options = $this->repositoryOptions($repository);
7705a78cd34SGreg Roach
7715a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $repository->getFullName());
7725a78cd34SGreg Roach
7735a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
7745a78cd34SGreg Roach            'options' => $options,
7755a78cd34SGreg Roach            'default' => key($options),
7765a78cd34SGreg Roach            'record'  => $repository,
7775a78cd34SGreg Roach            'title'   => $title,
7785a78cd34SGreg Roach            'tree'    => $tree,
7795a78cd34SGreg Roach        ]);
7805a78cd34SGreg Roach    }
7815a78cd34SGreg Roach
7825a78cd34SGreg Roach    /**
7835a78cd34SGreg Roach     * @param Repository $repository
7845a78cd34SGreg Roach     *
7855a78cd34SGreg Roach     * @return string[]
7865a78cd34SGreg Roach     */
787c1010edaSGreg Roach    private function repositoryOptions(Repository $repository): array
788c1010edaSGreg Roach    {
7895a78cd34SGreg Roach        $name = strip_tags($repository->getFullName());
7905a78cd34SGreg Roach
7915a78cd34SGreg Roach        return [
7925a78cd34SGreg Roach            'self' => $name,
7935a78cd34SGreg Roach        ];
7945a78cd34SGreg Roach    }
7955a78cd34SGreg Roach
7965a78cd34SGreg Roach    /**
7975a78cd34SGreg Roach     * @param Request $request
798b6db7c1fSGreg Roach     * @param Tree    $tree
7995a78cd34SGreg Roach     *
8005a78cd34SGreg Roach     * @return RedirectResponse
8015a78cd34SGreg Roach     */
802b6db7c1fSGreg Roach    public function postAddRepositoryAction(Request $request, Tree $tree): RedirectResponse
803c1010edaSGreg Roach    {
8049e648e55SGreg Roach        $xref = $request->get('xref', '');
8055a78cd34SGreg Roach
8065a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
8075a78cd34SGreg Roach
8085a78cd34SGreg Roach        if ($repository === null) {
80959f2f229SGreg Roach            throw new RepositoryNotFoundException();
8105a78cd34SGreg Roach        }
8115a78cd34SGreg Roach
8125a78cd34SGreg Roach        $this->addRecordToCart($repository);
8135a78cd34SGreg Roach
8145a78cd34SGreg Roach        return new RedirectResponse($repository->url());
8155a78cd34SGreg Roach    }
8165a78cd34SGreg Roach
8175a78cd34SGreg Roach    /**
8185a78cd34SGreg Roach     * @param Request $request
819b6db7c1fSGreg Roach     * @param Tree    $tree
8205a78cd34SGreg Roach     *
8215a78cd34SGreg Roach     * @return Response
8225a78cd34SGreg Roach     */
823b6db7c1fSGreg Roach    public function getAddSourceAction(Request $request, Tree $tree): Response
824c1010edaSGreg Roach    {
8259e648e55SGreg Roach        $xref = $request->get('xref', '');
8265a78cd34SGreg Roach
8275a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
8285a78cd34SGreg Roach
8295a78cd34SGreg Roach        if ($source === null) {
83059f2f229SGreg Roach            throw new SourceNotFoundException();
8315a78cd34SGreg Roach        }
8325a78cd34SGreg Roach
8335a78cd34SGreg Roach        $options = $this->sourceOptions($source);
8345a78cd34SGreg Roach
8355a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $source->getFullName());
8365a78cd34SGreg Roach
8375a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
8385a78cd34SGreg Roach            'options' => $options,
8395a78cd34SGreg Roach            'default' => key($options),
8405a78cd34SGreg Roach            'record'  => $source,
8415a78cd34SGreg Roach            'title'   => $title,
8425a78cd34SGreg Roach            'tree'    => $tree,
8435a78cd34SGreg Roach        ]);
8445a78cd34SGreg Roach    }
8455a78cd34SGreg Roach
8465a78cd34SGreg Roach    /**
8475a78cd34SGreg Roach     * @param Source $source
8485a78cd34SGreg Roach     *
8495a78cd34SGreg Roach     * @return string[]
8505a78cd34SGreg Roach     */
851c1010edaSGreg Roach    private function sourceOptions(Source $source): array
852c1010edaSGreg Roach    {
8535a78cd34SGreg Roach        $name = strip_tags($source->getFullName());
8545a78cd34SGreg Roach
8555a78cd34SGreg Roach        return [
8565a78cd34SGreg Roach            'only'   => strip_tags($source->getFullName()),
8575a78cd34SGreg Roach            'linked' => I18N::translate('%s and the individuals that reference it.', $name),
8585a78cd34SGreg Roach        ];
8595a78cd34SGreg Roach    }
8605a78cd34SGreg Roach
8615a78cd34SGreg Roach    /**
8625a78cd34SGreg Roach     * @param Request $request
863b6db7c1fSGreg Roach     * @param Tree    $tree
8645a78cd34SGreg Roach     *
8655a78cd34SGreg Roach     * @return RedirectResponse
8665a78cd34SGreg Roach     */
867b6db7c1fSGreg Roach    public function postAddSourceAction(Request $request, Tree $tree): RedirectResponse
868c1010edaSGreg Roach    {
8699e648e55SGreg Roach        $xref   = $request->get('xref', '');
8709e648e55SGreg Roach        $option = $request->get('option', '');
8715a78cd34SGreg Roach
8725a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
8735a78cd34SGreg Roach
8745a78cd34SGreg Roach        if ($source === null) {
87559f2f229SGreg Roach            throw new SourceNotFoundException();
8765a78cd34SGreg Roach        }
8775a78cd34SGreg Roach
8785a78cd34SGreg Roach        $this->addRecordToCart($source);
8795a78cd34SGreg Roach
8805a78cd34SGreg Roach        if ($option === 'linked') {
8815a78cd34SGreg Roach            foreach ($source->linkedIndividuals('SOUR') as $individual) {
8825a78cd34SGreg Roach                $this->addRecordToCart($individual);
8835a78cd34SGreg Roach            }
8845a78cd34SGreg Roach            foreach ($source->linkedFamilies('SOUR') as $family) {
8855a78cd34SGreg Roach                $this->addRecordToCart($family);
8865a78cd34SGreg Roach            }
8875a78cd34SGreg Roach        }
8885a78cd34SGreg Roach
8895a78cd34SGreg Roach        return new RedirectResponse($source->url());
8905a78cd34SGreg Roach    }
8915a78cd34SGreg Roach
8925a78cd34SGreg Roach    /**
8935a78cd34SGreg Roach     * Get all the records in the cart.
8945a78cd34SGreg Roach     *
8955a78cd34SGreg Roach     * @param Tree $tree
8965a78cd34SGreg Roach     *
8975a78cd34SGreg Roach     * @return GedcomRecord[]
8985a78cd34SGreg Roach     */
899c1010edaSGreg Roach    private function allRecordsInCart(Tree $tree): array
900c1010edaSGreg Roach    {
9015a78cd34SGreg Roach        $cart = Session::get('cart', []);
9025a78cd34SGreg Roach
903aa6f03bbSGreg Roach        $xrefs = array_keys($cart[$tree->name()] ?? []);
9045a78cd34SGreg Roach
9055a78cd34SGreg Roach        // Fetch all the records in the cart.
90618d7a90dSGreg Roach        $records = array_map(function (string $xref) use ($tree): GedcomRecord {
9075a78cd34SGreg Roach            return GedcomRecord::getInstance($xref, $tree);
9085a78cd34SGreg Roach        }, $xrefs);
9095a78cd34SGreg Roach
9105a78cd34SGreg Roach        // Some records may have been deleted after they were added to the cart.
9115a78cd34SGreg Roach        $records = array_filter($records);
9125a78cd34SGreg Roach
9135a78cd34SGreg Roach        // Group and sort.
91418d7a90dSGreg Roach        uasort($records, function (GedcomRecord $x, GedcomRecord $y): int {
915*c156e8f5SGreg Roach            return $x::RECORD_TYPE <=> $y::RECORD_TYPE ?: GedcomRecord::nameComparator()($x, $y);
9165a78cd34SGreg Roach        });
9175a78cd34SGreg Roach
9185a78cd34SGreg Roach        return $records;
9195a78cd34SGreg Roach    }
9205a78cd34SGreg Roach
9215a78cd34SGreg Roach    /**
9225a78cd34SGreg Roach     * Add a record (and direclty linked sources, notes, etc. to the cart.
9235a78cd34SGreg Roach     *
9245a78cd34SGreg Roach     * @param GedcomRecord $record
92518d7a90dSGreg Roach     *
92618d7a90dSGreg Roach     * @return void
9275a78cd34SGreg Roach     */
928c1010edaSGreg Roach    private function addRecordToCart(GedcomRecord $record)
929c1010edaSGreg Roach    {
9305a78cd34SGreg Roach        $cart = Session::get('cart', []);
9315a78cd34SGreg Roach
932f4afa648SGreg Roach        $tree_name = $record->tree()->name();
9335a78cd34SGreg Roach
9345a78cd34SGreg Roach        // Add this record
935c0935879SGreg Roach        $cart[$tree_name][$record->xref()] = true;
9365a78cd34SGreg Roach
9375a78cd34SGreg Roach        // Add directly linked media, notes, repositories and sources.
9388d0ebef0SGreg Roach        preg_match_all('/\n\d (?:OBJE|NOTE|SOUR|REPO) @(' . Gedcom::REGEX_XREF . ')@/', $record->gedcom(), $matches);
9395a78cd34SGreg Roach
9405a78cd34SGreg Roach        foreach ($matches[1] as $match) {
9415a78cd34SGreg Roach            $cart[$tree_name][$match] = true;
9425a78cd34SGreg Roach        }
9435a78cd34SGreg Roach
9445a78cd34SGreg Roach        Session::put('cart', $cart);
9455a78cd34SGreg Roach    }
9465a78cd34SGreg Roach
9475a78cd34SGreg Roach    /**
9485a78cd34SGreg Roach     * @param Tree $tree
9495a78cd34SGreg Roach     *
9505a78cd34SGreg Roach     * @return bool
9515a78cd34SGreg Roach     */
952c1010edaSGreg Roach    private function isCartEmpty(Tree $tree): bool
953c1010edaSGreg Roach    {
9545a78cd34SGreg Roach        $cart = Session::get('cart', []);
9555a78cd34SGreg Roach
956aa6f03bbSGreg Roach        return empty($cart[$tree->name()]);
9575a78cd34SGreg Roach    }
9585a78cd34SGreg Roach
9595a78cd34SGreg Roach    /**
9605a78cd34SGreg Roach     * Only allow access to the routes/functions if the menu is active
9615a78cd34SGreg Roach     *
9625a78cd34SGreg Roach     * @param Tree $tree
96318d7a90dSGreg Roach     *
96418d7a90dSGreg Roach     * @return void
96518d7a90dSGreg Roach     *
96618d7a90dSGreg Roach     * @throws NoteNotFoundException
9675a78cd34SGreg Roach     */
968c1010edaSGreg Roach    private function checkModuleAccess(Tree $tree)
969c1010edaSGreg Roach    {
9705a78cd34SGreg Roach        if (!array_key_exists($this->getName(), Module::getActiveMenus($tree))) {
97159f2f229SGreg Roach            throw new NotFoundHttpException();
9725a78cd34SGreg Roach        }
9738c2e8227SGreg Roach    }
9748c2e8227SGreg Roach}
975