xref: /webtrees/app/Module/ClippingsCartModule.php (revision 46295629d71d2c598181587a342547db33bf65db)
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;
355a78cd34SGreg Roachuse Fisharebest\Webtrees\Note;
365a78cd34SGreg Roachuse Fisharebest\Webtrees\Repository;
370e62c4b8SGreg Roachuse Fisharebest\Webtrees\Session;
385a78cd34SGreg Roachuse Fisharebest\Webtrees\Source;
39aee13b6dSGreg Roachuse Fisharebest\Webtrees\Tree;
405a78cd34SGreg Roachuse Fisharebest\Webtrees\User;
415a78cd34SGreg Roachuse League\Flysystem\Filesystem;
425a78cd34SGreg Roachuse League\Flysystem\ZipArchive\ZipArchiveAdapter;
43a3d8780cSGreg Roachuse function str_replace;
445a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\BinaryFileResponse;
455a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse;
465a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\Request;
475a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\Response;
485a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\ResponseHeaderBag;
498c2e8227SGreg Roach
508c2e8227SGreg Roach/**
518c2e8227SGreg Roach * Class ClippingsCartModule
528c2e8227SGreg Roach */
5349a243cbSGreg Roachclass ClippingsCartModule extends AbstractModule implements ModuleInterface, ModuleMenuInterface
54c1010edaSGreg Roach{
5549a243cbSGreg Roach    use ModuleMenuTrait;
5649a243cbSGreg Roach
575a78cd34SGreg Roach    // Routes that have a record which can be added to the clipboard
5816d6367aSGreg Roach    private const ROUTES_WITH_RECORDS = [
59c1010edaSGreg Roach        'family',
60c1010edaSGreg Roach        'individual',
61c1010edaSGreg Roach        'media',
62c1010edaSGreg Roach        'note',
63c1010edaSGreg Roach        'repository',
64c1010edaSGreg Roach        'source',
65c1010edaSGreg Roach    ];
665a78cd34SGreg Roach
6749a243cbSGreg Roach    /** @var int The default access level for this module.  It can be changed in the control panel. */
6849a243cbSGreg Roach    protected $access_level = Auth::PRIV_USER;
6949a243cbSGreg Roach
708c2e8227SGreg Roach    /** {@inheritdoc} */
7149a243cbSGreg Roach    public function title(): string
72c1010edaSGreg Roach    {
73bbb76c12SGreg Roach        /* I18N: Name of a module */
74bbb76c12SGreg Roach        return I18N::translate('Clippings cart');
758c2e8227SGreg Roach    }
768c2e8227SGreg Roach
778c2e8227SGreg Roach    /** {@inheritdoc} */
7849a243cbSGreg Roach    public function description(): string
79c1010edaSGreg Roach    {
80bbb76c12SGreg Roach        /* I18N: Description of the “Clippings cart” module */
81bbb76c12SGreg Roach        return I18N::translate('Select records from your family tree and save them as a GEDCOM file.');
828c2e8227SGreg Roach    }
838c2e8227SGreg Roach
840ee13198SGreg Roach    /**
8549a243cbSGreg Roach     * The default position for this menu.  It can be changed in the control panel.
860ee13198SGreg Roach     *
870ee13198SGreg Roach     * @return int
880ee13198SGreg Roach     */
898f53f488SRico Sonntag    public function defaultMenuOrder(): int
90c1010edaSGreg Roach    {
91*46295629SGreg Roach        return 60;
928c2e8227SGreg Roach    }
938c2e8227SGreg Roach
940ee13198SGreg Roach    /**
950ee13198SGreg Roach     * A menu, to be added to the main application menu.
960ee13198SGreg Roach     *
97aee13b6dSGreg Roach     * @param Tree $tree
98aee13b6dSGreg Roach     *
990ee13198SGreg Roach     * @return Menu|null
1000ee13198SGreg Roach     */
101*46295629SGreg Roach    public function getMenu(Tree $tree): ?Menu
102c1010edaSGreg Roach    {
1035a78cd34SGreg Roach        $request = Request::createFromGlobals();
1048c2e8227SGreg Roach
1059e648e55SGreg Roach        $route = $request->get('route', '');
1065a78cd34SGreg Roach
1075a78cd34SGreg Roach        $submenus = [
10849a243cbSGreg Roach            new Menu($this->title(), route('module', [
10949a243cbSGreg Roach                'module' => $this->getName(),
110c1010edaSGreg Roach                'action' => 'Show',
111aa6f03bbSGreg Roach                'ged'    => $tree->name(),
112c1010edaSGreg Roach            ]), 'menu-clippings-cart', ['rel' => 'nofollow']),
1135a78cd34SGreg Roach        ];
1145a78cd34SGreg Roach
1155a78cd34SGreg Roach        if (in_array($route, self::ROUTES_WITH_RECORDS)) {
1169e648e55SGreg Roach            $xref      = $request->get('xref', '');
1175a78cd34SGreg Roach            $action    = 'Add' . ucfirst($route);
118c1010edaSGreg Roach            $add_route = route('module', [
11949a243cbSGreg Roach                'module' => $this->getName(),
120c1010edaSGreg Roach                'action' => $action,
121c1010edaSGreg Roach                'xref'   => $xref,
122aa6f03bbSGreg Roach                'ged'    => $tree->name(),
123c1010edaSGreg Roach            ]);
1245a78cd34SGreg Roach
12525b2dde3SGreg Roach            $submenus[] = new Menu(I18N::translate('Add to the clippings cart'), $add_route, 'menu-clippings-add', ['rel' => 'nofollow']);
1268c2e8227SGreg Roach        }
127cbc1590aSGreg Roach
1285a78cd34SGreg Roach        if (!$this->isCartEmpty($tree)) {
129c1010edaSGreg Roach            $submenus[] = new Menu(I18N::translate('Empty the clippings cart'), route('module', [
13049a243cbSGreg Roach                'module' => $this->getName(),
131c1010edaSGreg Roach                'action' => 'Empty',
132aa6f03bbSGreg Roach                'ged'    => $tree->name(),
133c1010edaSGreg Roach            ]), 'menu-clippings-empty', ['rel' => 'nofollow']);
134c1010edaSGreg Roach            $submenus[] = new Menu(I18N::translate('Download'), route('module', [
13549a243cbSGreg Roach                'module' => $this->getName(),
136c1010edaSGreg Roach                'action' => 'DownloadForm',
137aa6f03bbSGreg Roach                'ged'    => $tree->name(),
138c1010edaSGreg Roach            ]), 'menu-clippings-download', ['rel' => 'nofollow']);
1395a78cd34SGreg Roach        }
1405a78cd34SGreg Roach
14149a243cbSGreg Roach        return new Menu($this->title(), '#', 'menu-clippings', ['rel' => 'nofollow'], $submenus);
1428c2e8227SGreg Roach    }
1438c2e8227SGreg Roach
14476692c8bSGreg Roach    /**
1455a78cd34SGreg Roach     * @param Request $request
146b6db7c1fSGreg Roach     * @param Tree    $tree
14776692c8bSGreg Roach     *
1485a78cd34SGreg Roach     * @return BinaryFileResponse
14976692c8bSGreg Roach     */
150b6db7c1fSGreg Roach    public function getDownloadAction(Request $request, Tree $tree): BinaryFileResponse
151c1010edaSGreg Roach    {
1529e648e55SGreg Roach        $privatize_export = $request->get('privatize_export', '');
1535a78cd34SGreg Roach        $convert          = (bool) $request->get('convert');
1548c2e8227SGreg Roach
15513abd6f3SGreg Roach        $cart = Session::get('cart', []);
1568c2e8227SGreg Roach
157aa6f03bbSGreg Roach        $xrefs = array_keys($cart[$tree->name()] ?? []);
1585a78cd34SGreg Roach
1595a78cd34SGreg Roach        // Create a new/empty .ZIP file
1605a78cd34SGreg Roach        $temp_zip_file  = tempnam(sys_get_temp_dir(), 'webtrees-zip-');
1615a78cd34SGreg Roach        $zip_filesystem = new Filesystem(new ZipArchiveAdapter($temp_zip_file));
1625a78cd34SGreg Roach
1635a78cd34SGreg Roach        // Media file prefix
1645a78cd34SGreg Roach        $path = $tree->getPreference('MEDIA_DIRECTORY');
1655a78cd34SGreg Roach
1665a78cd34SGreg Roach        // GEDCOM file header
167a3d8780cSGreg Roach        $filetext = FunctionsExport::gedcomHeader($tree, $convert ? 'ANSI' : 'UTF-8');
1685a78cd34SGreg Roach
1695a78cd34SGreg Roach        switch ($privatize_export) {
1705a78cd34SGreg Roach            case 'gedadmin':
1715a78cd34SGreg Roach                $access_level = Auth::PRIV_NONE;
1725a78cd34SGreg Roach                break;
1735a78cd34SGreg Roach            case 'user':
1745a78cd34SGreg Roach                $access_level = Auth::PRIV_USER;
1755a78cd34SGreg Roach                break;
1765a78cd34SGreg Roach            case 'visitor':
1775a78cd34SGreg Roach                $access_level = Auth::PRIV_PRIVATE;
1785a78cd34SGreg Roach                break;
1795a78cd34SGreg Roach            case 'none':
1805a78cd34SGreg Roach            default:
1815a78cd34SGreg Roach                $access_level = Auth::PRIV_HIDE;
1825a78cd34SGreg Roach                break;
1835a78cd34SGreg Roach        }
1845a78cd34SGreg Roach
1855a78cd34SGreg Roach        foreach ($xrefs as $xref) {
1865a78cd34SGreg Roach            $object = GedcomRecord::getInstance($xref, $tree);
1875a78cd34SGreg Roach            // The object may have been deleted since we added it to the cart....
1885a78cd34SGreg Roach            if ($object) {
1895a78cd34SGreg Roach                $record = $object->privatizeGedcom($access_level);
1905a78cd34SGreg Roach                // Remove links to objects that aren't in the cart
1918d0ebef0SGreg Roach                preg_match_all('/\n1 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[2-9].*)*/', $record, $matches, PREG_SET_ORDER);
1925a78cd34SGreg Roach                foreach ($matches as $match) {
1935a78cd34SGreg Roach                    if (!array_key_exists($match[1], $xrefs)) {
1945a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
1955a78cd34SGreg Roach                    }
1965a78cd34SGreg Roach                }
1978d0ebef0SGreg Roach                preg_match_all('/\n2 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[3-9].*)*/', $record, $matches, PREG_SET_ORDER);
1985a78cd34SGreg Roach                foreach ($matches as $match) {
1995a78cd34SGreg Roach                    if (!array_key_exists($match[1], $xrefs)) {
2005a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2015a78cd34SGreg Roach                    }
2025a78cd34SGreg Roach                }
2038d0ebef0SGreg Roach                preg_match_all('/\n3 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[4-9].*)*/', $record, $matches, PREG_SET_ORDER);
2045a78cd34SGreg Roach                foreach ($matches as $match) {
2055a78cd34SGreg Roach                    if (!array_key_exists($match[1], $xrefs)) {
2065a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2075a78cd34SGreg Roach                    }
2085a78cd34SGreg Roach                }
2095a78cd34SGreg Roach
21055167344SGreg Roach                if ($object instanceof Individual || $object instanceof Family) {
2115a78cd34SGreg Roach                    $filetext .= $record . "\n";
2125a78cd34SGreg Roach                    $filetext .= "1 SOUR @WEBTREES@\n";
2131f273236SGreg Roach                    $filetext .= '2 PAGE ' . $object->url() . "\n";
21455167344SGreg Roach                } elseif ($object instanceof Source) {
2155a78cd34SGreg Roach                    $filetext .= $record . "\n";
2161f273236SGreg Roach                    $filetext .= '1 NOTE ' . $object->url() . "\n";
21755167344SGreg Roach                } elseif ($object instanceof Media) {
21855167344SGreg Roach                    // Add the media files to the archive
2195a78cd34SGreg Roach                    foreach ($object->mediaFiles() as $media_file) {
2205a78cd34SGreg Roach                        if (file_exists($media_file->getServerFilename())) {
2215a78cd34SGreg Roach                            $fp = fopen($media_file->getServerFilename(), 'r');
2225a78cd34SGreg Roach                            $zip_filesystem->writeStream($path . $media_file->filename(), $fp);
2235a78cd34SGreg Roach                            fclose($fp);
2245a78cd34SGreg Roach                        }
2255a78cd34SGreg Roach                    }
2265a78cd34SGreg Roach                    $filetext .= $record . "\n";
22755167344SGreg Roach                } else {
2285a78cd34SGreg Roach                    $filetext .= $record . "\n";
2298c2e8227SGreg Roach                }
2308c2e8227SGreg Roach            }
2318c2e8227SGreg Roach        }
2328c2e8227SGreg Roach
2335a78cd34SGreg Roach        // Create a source, to indicate the source of the data.
2345a78cd34SGreg Roach        $filetext .= "0 @WEBTREES@ SOUR\n1 TITL " . WT_BASE_URL . "\n";
2357fcd9838SGreg Roach        $author = User::find((int) $tree->getPreference('CONTACT_USER_ID'));
2365a78cd34SGreg Roach        if ($author !== null) {
2375a78cd34SGreg Roach            $filetext .= '1 AUTH ' . $author->getRealName() . "\n";
2385a78cd34SGreg Roach        }
2395a78cd34SGreg Roach        $filetext .= "0 TRLR\n";
2405a78cd34SGreg Roach
2415a78cd34SGreg Roach        // Make sure the preferred line endings are used
242a3d8780cSGreg Roach        $filetext = str_replace('\n', Gedcom::EOL, $filetext);
2435a78cd34SGreg Roach
24455167344SGreg Roach        if ($convert) {
2455a78cd34SGreg Roach            $filetext = utf8_decode($filetext);
2468c2e8227SGreg Roach        }
247cbc1590aSGreg Roach
2485a78cd34SGreg Roach        // Finally add the GEDCOM file to the .ZIP file.
2495a78cd34SGreg Roach        $zip_filesystem->write('clippings.ged', $filetext);
2505a78cd34SGreg Roach
2515a78cd34SGreg Roach        // Need to force-close the filesystem
25202a92f80SGreg Roach        unset($zip_filesystem);
2535a78cd34SGreg Roach
2545a78cd34SGreg Roach        $response = new BinaryFileResponse($temp_zip_file);
2555a78cd34SGreg Roach        $response->deleteFileAfterSend(true);
2565a78cd34SGreg Roach
2575a78cd34SGreg Roach        $response->headers->set('Content-Type', 'application/zip');
2585a78cd34SGreg Roach        $response->setContentDisposition(
2595a78cd34SGreg Roach            ResponseHeaderBag::DISPOSITION_ATTACHMENT,
2605a78cd34SGreg Roach            'clippings.zip'
2615a78cd34SGreg Roach        );
2625a78cd34SGreg Roach
2635a78cd34SGreg Roach        return $response;
2648c2e8227SGreg Roach    }
2658c2e8227SGreg Roach
2668c2e8227SGreg Roach    /**
267b6db7c1fSGreg Roach     * @param Tree $tree
268b6db7c1fSGreg Roach     * @param User $user
26976692c8bSGreg Roach     *
2705a78cd34SGreg Roach     * @return Response
2718c2e8227SGreg Roach     */
272b6db7c1fSGreg Roach    public function getDownloadFormAction(Tree $tree, User $user): Response
273c1010edaSGreg Roach    {
2745a78cd34SGreg Roach        $title = I18N::translate('Family tree clippings cart') . ' — ' . I18N::translate('Download');
2758c2e8227SGreg Roach
2765a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/download', [
2775a78cd34SGreg Roach            'is_manager' => Auth::isManager($tree, $user),
2785a78cd34SGreg Roach            'is_member'  => Auth::isMember($tree, $user),
2795a78cd34SGreg Roach            'title'      => $title,
2805a78cd34SGreg Roach        ]);
2818c2e8227SGreg Roach    }
2828c2e8227SGreg Roach
2835a78cd34SGreg Roach    /**
284b6db7c1fSGreg Roach     * @param Tree $tree
2855a78cd34SGreg Roach     *
2865a78cd34SGreg Roach     * @return RedirectResponse
2875a78cd34SGreg Roach     */
288b6db7c1fSGreg Roach    public function getEmptyAction(Tree $tree): RedirectResponse
289c1010edaSGreg Roach    {
2905a78cd34SGreg Roach        $cart                = Session::get('cart', []);
291aa6f03bbSGreg Roach        $cart[$tree->name()] = [];
2925a78cd34SGreg Roach        Session::put('cart', $cart);
2938c2e8227SGreg Roach
294c1010edaSGreg Roach        $url = route('module', [
29549a243cbSGreg Roach            'module' => $this->getName(),
296c1010edaSGreg Roach            'action' => 'Show',
297aa6f03bbSGreg Roach            'ged'    => $tree->name(),
298c1010edaSGreg Roach        ]);
2995a78cd34SGreg Roach
3005a78cd34SGreg Roach        return new RedirectResponse($url);
3015a78cd34SGreg Roach    }
3025a78cd34SGreg Roach
3035a78cd34SGreg Roach    /**
3045a78cd34SGreg Roach     * @param Request $request
305b6db7c1fSGreg Roach     * @param Tree    $tree
3065a78cd34SGreg Roach     *
3075a78cd34SGreg Roach     * @return RedirectResponse
3085a78cd34SGreg Roach     */
309b6db7c1fSGreg Roach    public function postRemoveAction(Request $request, Tree $tree): RedirectResponse
310c1010edaSGreg Roach    {
3119e648e55SGreg Roach        $xref = $request->get('xref', '');
3125a78cd34SGreg Roach
3135a78cd34SGreg Roach        $cart = Session::get('cart', []);
314aa6f03bbSGreg Roach        unset($cart[$tree->name()][$xref]);
3155a78cd34SGreg Roach        Session::put('cart', $cart);
3165a78cd34SGreg Roach
317c1010edaSGreg Roach        $url = route('module', [
31849a243cbSGreg Roach            'module' => $this->getName(),
319c1010edaSGreg Roach            'action' => 'Show',
320aa6f03bbSGreg Roach            'ged'    => $tree->name(),
321c1010edaSGreg Roach        ]);
3225a78cd34SGreg Roach
3235a78cd34SGreg Roach        return new RedirectResponse($url);
3245a78cd34SGreg Roach    }
3255a78cd34SGreg Roach
3265a78cd34SGreg Roach    /**
327b6db7c1fSGreg Roach     * @param Tree $tree
3285a78cd34SGreg Roach     *
3295a78cd34SGreg Roach     * @return Response
3305a78cd34SGreg Roach     */
331b6db7c1fSGreg Roach    public function getShowAction(Tree $tree): Response
332c1010edaSGreg Roach    {
3335a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/show', [
3345a78cd34SGreg Roach            'records' => $this->allRecordsInCart($tree),
3355a78cd34SGreg Roach            'title'   => I18N::translate('Family tree clippings cart'),
3365a78cd34SGreg Roach            'tree'    => $tree,
3375a78cd34SGreg Roach        ]);
3385a78cd34SGreg Roach    }
3395a78cd34SGreg Roach
3405a78cd34SGreg Roach    /**
3415a78cd34SGreg Roach     * @param Request $request
342b6db7c1fSGreg Roach     * @param Tree    $tree
3435a78cd34SGreg Roach     *
3445a78cd34SGreg Roach     * @return Response
3455a78cd34SGreg Roach     */
346b6db7c1fSGreg Roach    public function getAddFamilyAction(Request $request, Tree $tree): Response
347c1010edaSGreg Roach    {
3489e648e55SGreg Roach        $xref = $request->get('xref', '');
3495a78cd34SGreg Roach
3505a78cd34SGreg Roach        $family = Family::getInstance($xref, $tree);
3515a78cd34SGreg Roach
3525a78cd34SGreg Roach        if ($family === null) {
35359f2f229SGreg Roach            throw new FamilyNotFoundException();
3545a78cd34SGreg Roach        }
3555a78cd34SGreg Roach
3565a78cd34SGreg Roach        $options = $this->familyOptions($family);
3575a78cd34SGreg Roach
3585a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $family->getFullName());
3595a78cd34SGreg Roach
3605a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
3615a78cd34SGreg Roach            'options' => $options,
3625a78cd34SGreg Roach            'default' => key($options),
3635a78cd34SGreg Roach            'record'  => $family,
3645a78cd34SGreg Roach            'title'   => $title,
3655a78cd34SGreg Roach            'tree'    => $tree,
3665a78cd34SGreg Roach        ]);
3675a78cd34SGreg Roach    }
3685a78cd34SGreg Roach
3695a78cd34SGreg Roach    /**
3705a78cd34SGreg Roach     * @param Family $family
3715a78cd34SGreg Roach     *
3725a78cd34SGreg Roach     * @return string[]
3735a78cd34SGreg Roach     */
374c1010edaSGreg Roach    private function familyOptions(Family $family): array
375c1010edaSGreg Roach    {
3765a78cd34SGreg Roach        $name = strip_tags($family->getFullName());
3775a78cd34SGreg Roach
3785a78cd34SGreg Roach        return [
3795a78cd34SGreg Roach            'parents'     => $name,
380bbb76c12SGreg Roach            /* I18N: %s is a family (husband + wife) */
381bbb76c12SGreg Roach            'members'     => I18N::translate('%s and their children', $name),
382bbb76c12SGreg Roach            /* I18N: %s is a family (husband + wife) */
383bbb76c12SGreg Roach            'descendants' => I18N::translate('%s and their descendants', $name),
3845a78cd34SGreg Roach        ];
3855a78cd34SGreg Roach    }
3865a78cd34SGreg Roach
3875a78cd34SGreg Roach    /**
3885a78cd34SGreg Roach     * @param Request $request
389b6db7c1fSGreg Roach     * @param Tree    $tree
3905a78cd34SGreg Roach     *
3915a78cd34SGreg Roach     * @return RedirectResponse
3925a78cd34SGreg Roach     */
393b6db7c1fSGreg Roach    public function postAddFamilyAction(Request $request, Tree $tree): RedirectResponse
394c1010edaSGreg Roach    {
3959e648e55SGreg Roach        $xref   = $request->get('xref', '');
3969e648e55SGreg Roach        $option = $request->get('option', '');
3975a78cd34SGreg Roach
3985a78cd34SGreg Roach        $family = Family::getInstance($xref, $tree);
3995a78cd34SGreg Roach
4005a78cd34SGreg Roach        if ($family === null) {
40159f2f229SGreg Roach            throw new FamilyNotFoundException();
4025a78cd34SGreg Roach        }
4035a78cd34SGreg Roach
4045a78cd34SGreg Roach        switch ($option) {
4055a78cd34SGreg Roach            case 'parents':
4065a78cd34SGreg Roach                $this->addFamilyToCart($family);
4075a78cd34SGreg Roach                break;
4085a78cd34SGreg Roach
4095a78cd34SGreg Roach            case 'members':
4105a78cd34SGreg Roach                $this->addFamilyAndChildrenToCart($family);
4115a78cd34SGreg Roach                break;
4125a78cd34SGreg Roach
4135a78cd34SGreg Roach            case 'descendants':
4145a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($family);
4155a78cd34SGreg Roach                break;
4165a78cd34SGreg Roach        }
4175a78cd34SGreg Roach
4185a78cd34SGreg Roach        return new RedirectResponse($family->url());
4195a78cd34SGreg Roach    }
4205a78cd34SGreg Roach
4215a78cd34SGreg Roach    /**
4225a78cd34SGreg Roach     * @param Family $family
42318d7a90dSGreg Roach     *
42418d7a90dSGreg Roach     * @return void
4255a78cd34SGreg Roach     */
426c1010edaSGreg Roach    private function addFamilyToCart(Family $family)
427c1010edaSGreg Roach    {
4285a78cd34SGreg Roach        $this->addRecordToCart($family);
4295a78cd34SGreg Roach
4305a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
4315a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4325a78cd34SGreg Roach        }
4335a78cd34SGreg Roach    }
4345a78cd34SGreg Roach
4355a78cd34SGreg Roach    /**
4365a78cd34SGreg Roach     * @param Family $family
43718d7a90dSGreg Roach     *
43818d7a90dSGreg Roach     * @return void
4395a78cd34SGreg Roach     */
440c1010edaSGreg Roach    private function addFamilyAndChildrenToCart(Family $family)
441c1010edaSGreg Roach    {
4425a78cd34SGreg Roach        $this->addRecordToCart($family);
4435a78cd34SGreg Roach
4445a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
4455a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4465a78cd34SGreg Roach        }
4475a78cd34SGreg Roach        foreach ($family->getChildren() as $child) {
4485a78cd34SGreg Roach            $this->addRecordToCart($child);
4495a78cd34SGreg Roach        }
4505a78cd34SGreg Roach    }
4515a78cd34SGreg Roach
4525a78cd34SGreg Roach    /**
4535a78cd34SGreg Roach     * @param Family $family
45418d7a90dSGreg Roach     *
45518d7a90dSGreg Roach     * @return void
4565a78cd34SGreg Roach     */
457c1010edaSGreg Roach    private function addFamilyAndDescendantsToCart(Family $family)
458c1010edaSGreg Roach    {
4595a78cd34SGreg Roach        $this->addRecordToCart($family);
4605a78cd34SGreg Roach
4615a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
4625a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4635a78cd34SGreg Roach        }
4645a78cd34SGreg Roach        foreach ($family->getChildren() as $child) {
4655a78cd34SGreg Roach            $this->addRecordToCart($child);
4665a78cd34SGreg Roach            foreach ($child->getSpouseFamilies() as $child_family) {
4675a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($child_family);
4685a78cd34SGreg Roach            }
4695a78cd34SGreg Roach        }
4705a78cd34SGreg Roach    }
4715a78cd34SGreg Roach
4725a78cd34SGreg Roach    /**
4735a78cd34SGreg Roach     * @param Request $request
474b6db7c1fSGreg Roach     * @param Tree    $tree
4755a78cd34SGreg Roach     *
4765a78cd34SGreg Roach     * @return Response
4775a78cd34SGreg Roach     */
478b6db7c1fSGreg Roach    public function getAddIndividualAction(Request $request, Tree $tree): Response
479c1010edaSGreg Roach    {
4809e648e55SGreg Roach        $xref = $request->get('xref', '');
4815a78cd34SGreg Roach
4825a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
4835a78cd34SGreg Roach
4845a78cd34SGreg Roach        if ($individual === null) {
48559f2f229SGreg Roach            throw new IndividualNotFoundException();
4865a78cd34SGreg Roach        }
4875a78cd34SGreg Roach
4885a78cd34SGreg Roach        $options = $this->individualOptions($individual);
4895a78cd34SGreg Roach
4905a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $individual->getFullName());
4915a78cd34SGreg Roach
4925a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
4935a78cd34SGreg Roach            'options' => $options,
4945a78cd34SGreg Roach            'default' => key($options),
4955a78cd34SGreg Roach            'record'  => $individual,
4965a78cd34SGreg Roach            'title'   => $title,
4975a78cd34SGreg Roach            'tree'    => $tree,
4985a78cd34SGreg Roach        ]);
4995a78cd34SGreg Roach    }
5005a78cd34SGreg Roach
5015a78cd34SGreg Roach    /**
5025a78cd34SGreg Roach     * @param Individual $individual
5035a78cd34SGreg Roach     *
5045a78cd34SGreg Roach     * @return string[]
5055a78cd34SGreg Roach     */
506c1010edaSGreg Roach    private function individualOptions(Individual $individual): array
507c1010edaSGreg Roach    {
5085a78cd34SGreg Roach        $name = strip_tags($individual->getFullName());
5095a78cd34SGreg Roach
5105a78cd34SGreg Roach        if ($individual->getSex() === 'F') {
5115a78cd34SGreg Roach            return [
5125a78cd34SGreg Roach                'self'              => $name,
5135a78cd34SGreg Roach                'parents'           => I18N::translate('%s, her parents and siblings', $name),
5145a78cd34SGreg Roach                'spouses'           => I18N::translate('%s, her spouses and children', $name),
5155a78cd34SGreg Roach                'ancestors'         => I18N::translate('%s and her ancestors', $name),
5165a78cd34SGreg Roach                'ancestor_families' => I18N::translate('%s, her ancestors and their families', $name),
5175a78cd34SGreg Roach                'descendants'       => I18N::translate('%s, her spouses and descendants', $name),
5185a78cd34SGreg Roach            ];
519b2ce94c6SRico Sonntag        }
520b2ce94c6SRico Sonntag
5215a78cd34SGreg Roach        return [
5225a78cd34SGreg Roach            'self'              => $name,
5235a78cd34SGreg Roach            'parents'           => I18N::translate('%s, his parents and siblings', $name),
5245a78cd34SGreg Roach            'spouses'           => I18N::translate('%s, his spouses and children', $name),
5255a78cd34SGreg Roach            'ancestors'         => I18N::translate('%s and his ancestors', $name),
5265a78cd34SGreg Roach            'ancestor_families' => I18N::translate('%s, his ancestors and their families', $name),
5275a78cd34SGreg Roach            'descendants'       => I18N::translate('%s, his spouses and descendants', $name),
5285a78cd34SGreg Roach        ];
5295a78cd34SGreg Roach    }
5305a78cd34SGreg Roach
5315a78cd34SGreg Roach    /**
5325a78cd34SGreg Roach     * @param Request $request
533b6db7c1fSGreg Roach     * @param Tree    $tree
5345a78cd34SGreg Roach     *
5355a78cd34SGreg Roach     * @return RedirectResponse
5365a78cd34SGreg Roach     */
537b6db7c1fSGreg Roach    public function postAddIndividualAction(Request $request, Tree $tree): RedirectResponse
538c1010edaSGreg Roach    {
5399e648e55SGreg Roach        $xref   = $request->get('xref', '');
5409e648e55SGreg Roach        $option = $request->get('option', '');
5415a78cd34SGreg Roach
5425a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
5435a78cd34SGreg Roach
5445a78cd34SGreg Roach        if ($individual === null) {
54559f2f229SGreg Roach            throw new IndividualNotFoundException();
5465a78cd34SGreg Roach        }
5475a78cd34SGreg Roach
5485a78cd34SGreg Roach        switch ($option) {
5495a78cd34SGreg Roach            case 'self':
5505a78cd34SGreg Roach                $this->addRecordToCart($individual);
5515a78cd34SGreg Roach                break;
5525a78cd34SGreg Roach
5535a78cd34SGreg Roach            case 'parents':
5545a78cd34SGreg Roach                foreach ($individual->getChildFamilies() as $family) {
5555a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
5565a78cd34SGreg Roach                }
5575a78cd34SGreg Roach                break;
5585a78cd34SGreg Roach
5595a78cd34SGreg Roach            case 'spouses':
5605a78cd34SGreg Roach                foreach ($individual->getSpouseFamilies() as $family) {
5615a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
5625a78cd34SGreg Roach                }
5635a78cd34SGreg Roach                break;
5645a78cd34SGreg Roach
5655a78cd34SGreg Roach            case 'ancestors':
5665a78cd34SGreg Roach                $this->addAncestorsToCart($individual);
5675a78cd34SGreg Roach                break;
5685a78cd34SGreg Roach
5695a78cd34SGreg Roach            case 'ancestor_families':
5705a78cd34SGreg Roach                $this->addAncestorFamiliesToCart($individual);
5715a78cd34SGreg Roach                break;
5725a78cd34SGreg Roach
5735a78cd34SGreg Roach            case 'descendants':
5745a78cd34SGreg Roach                foreach ($individual->getSpouseFamilies() as $family) {
5755a78cd34SGreg Roach                    $this->addFamilyAndDescendantsToCart($family);
5765a78cd34SGreg Roach                }
5775a78cd34SGreg Roach                break;
5785a78cd34SGreg Roach        }
5795a78cd34SGreg Roach
5805a78cd34SGreg Roach        return new RedirectResponse($individual->url());
5815a78cd34SGreg Roach    }
5825a78cd34SGreg Roach
5835a78cd34SGreg Roach    /**
5845a78cd34SGreg Roach     * @param Individual $individual
58518d7a90dSGreg Roach     *
58618d7a90dSGreg Roach     * @return void
5875a78cd34SGreg Roach     */
588c1010edaSGreg Roach    private function addAncestorsToCart(Individual $individual)
589c1010edaSGreg Roach    {
5905a78cd34SGreg Roach        $this->addRecordToCart($individual);
5915a78cd34SGreg Roach
5925a78cd34SGreg Roach        foreach ($individual->getChildFamilies() as $family) {
5935a78cd34SGreg Roach            foreach ($family->getSpouses() as $parent) {
5945a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
5955a78cd34SGreg Roach            }
5965a78cd34SGreg Roach        }
5975a78cd34SGreg Roach    }
5985a78cd34SGreg Roach
5995a78cd34SGreg Roach    /**
6005a78cd34SGreg Roach     * @param Individual $individual
60118d7a90dSGreg Roach     *
60218d7a90dSGreg Roach     * @return void
6035a78cd34SGreg Roach     */
604c1010edaSGreg Roach    private function addAncestorFamiliesToCart(Individual $individual)
605c1010edaSGreg Roach    {
6065a78cd34SGreg Roach        foreach ($individual->getChildFamilies() as $family) {
6075a78cd34SGreg Roach            $this->addFamilyAndChildrenToCart($family);
6085a78cd34SGreg Roach            foreach ($family->getSpouses() as $parent) {
6095a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
6105a78cd34SGreg Roach            }
6115a78cd34SGreg Roach        }
6125a78cd34SGreg Roach    }
6135a78cd34SGreg Roach
6145a78cd34SGreg Roach    /**
6155a78cd34SGreg Roach     * @param Request $request
616b6db7c1fSGreg Roach     * @param Tree    $tree
6175a78cd34SGreg Roach     *
6185a78cd34SGreg Roach     * @return Response
6195a78cd34SGreg Roach     */
620b6db7c1fSGreg Roach    public function getAddMediaAction(Request $request, Tree $tree): Response
621c1010edaSGreg Roach    {
6229e648e55SGreg Roach        $xref = $request->get('xref', '');
6235a78cd34SGreg Roach
6245a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
6255a78cd34SGreg Roach
6265a78cd34SGreg Roach        if ($media === null) {
62759f2f229SGreg Roach            throw new MediaNotFoundException();
6285a78cd34SGreg Roach        }
6295a78cd34SGreg Roach
6305a78cd34SGreg Roach        $options = $this->mediaOptions($media);
6315a78cd34SGreg Roach
6325a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $media->getFullName());
6335a78cd34SGreg Roach
6345a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
6355a78cd34SGreg Roach            'options' => $options,
6365a78cd34SGreg Roach            'default' => key($options),
6375a78cd34SGreg Roach            'record'  => $media,
6385a78cd34SGreg Roach            'title'   => $title,
6395a78cd34SGreg Roach            'tree'    => $tree,
6405a78cd34SGreg Roach        ]);
6415a78cd34SGreg Roach    }
6425a78cd34SGreg Roach
6435a78cd34SGreg Roach    /**
6445a78cd34SGreg Roach     * @param Media $media
6455a78cd34SGreg Roach     *
6465a78cd34SGreg Roach     * @return string[]
6475a78cd34SGreg Roach     */
648c1010edaSGreg Roach    private function mediaOptions(Media $media): array
649c1010edaSGreg Roach    {
6505a78cd34SGreg Roach        $name = strip_tags($media->getFullName());
6515a78cd34SGreg Roach
6525a78cd34SGreg Roach        return [
6535a78cd34SGreg Roach            'self' => $name,
6545a78cd34SGreg Roach        ];
6555a78cd34SGreg Roach    }
6565a78cd34SGreg Roach
6575a78cd34SGreg Roach    /**
6585a78cd34SGreg Roach     * @param Request $request
659b6db7c1fSGreg Roach     * @param Tree    $tree
6605a78cd34SGreg Roach     *
6615a78cd34SGreg Roach     * @return RedirectResponse
6625a78cd34SGreg Roach     */
663b6db7c1fSGreg Roach    public function postAddMediaAction(Request $request, Tree $tree): RedirectResponse
664c1010edaSGreg Roach    {
6659e648e55SGreg Roach        $xref = $request->get('xref', '');
6665a78cd34SGreg Roach
6675a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
6685a78cd34SGreg Roach
6695a78cd34SGreg Roach        if ($media === null) {
67059f2f229SGreg Roach            throw new MediaNotFoundException();
6715a78cd34SGreg Roach        }
6725a78cd34SGreg Roach
6735a78cd34SGreg Roach        $this->addRecordToCart($media);
6745a78cd34SGreg Roach
6755a78cd34SGreg Roach        return new RedirectResponse($media->url());
6765a78cd34SGreg Roach    }
6775a78cd34SGreg Roach
6785a78cd34SGreg Roach    /**
6795a78cd34SGreg Roach     * @param Request $request
680b6db7c1fSGreg Roach     * @param Tree    $tree
6815a78cd34SGreg Roach     *
6825a78cd34SGreg Roach     * @return Response
6835a78cd34SGreg Roach     */
684b6db7c1fSGreg Roach    public function getAddNoteAction(Request $request, Tree $tree): Response
685c1010edaSGreg Roach    {
6869e648e55SGreg Roach        $xref = $request->get('xref', '');
6875a78cd34SGreg Roach
6885a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
6895a78cd34SGreg Roach
6905a78cd34SGreg Roach        if ($note === null) {
69159f2f229SGreg Roach            throw new NoteNotFoundException();
6925a78cd34SGreg Roach        }
6935a78cd34SGreg Roach
6945a78cd34SGreg Roach        $options = $this->noteOptions($note);
6955a78cd34SGreg Roach
6965a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $note->getFullName());
6975a78cd34SGreg Roach
6985a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
6995a78cd34SGreg Roach            'options' => $options,
7005a78cd34SGreg Roach            'default' => key($options),
7015a78cd34SGreg Roach            'record'  => $note,
7025a78cd34SGreg Roach            'title'   => $title,
7035a78cd34SGreg Roach            'tree'    => $tree,
7045a78cd34SGreg Roach        ]);
7055a78cd34SGreg Roach    }
7065a78cd34SGreg Roach
7075a78cd34SGreg Roach    /**
7085a78cd34SGreg Roach     * @param Note $note
7095a78cd34SGreg Roach     *
7105a78cd34SGreg Roach     * @return string[]
7115a78cd34SGreg Roach     */
712c1010edaSGreg Roach    private function noteOptions(Note $note): array
713c1010edaSGreg Roach    {
7145a78cd34SGreg Roach        $name = strip_tags($note->getFullName());
7155a78cd34SGreg Roach
7165a78cd34SGreg Roach        return [
7175a78cd34SGreg Roach            'self' => $name,
7185a78cd34SGreg Roach        ];
7195a78cd34SGreg Roach    }
7205a78cd34SGreg Roach
7215a78cd34SGreg Roach    /**
7225a78cd34SGreg Roach     * @param Request $request
723b6db7c1fSGreg Roach     * @param Tree    $tree
7245a78cd34SGreg Roach     *
7255a78cd34SGreg Roach     * @return RedirectResponse
7265a78cd34SGreg Roach     */
727b6db7c1fSGreg Roach    public function postAddNoteAction(Request $request, Tree $tree): RedirectResponse
728c1010edaSGreg Roach    {
7299e648e55SGreg Roach        $xref = $request->get('xref', '');
7305a78cd34SGreg Roach
7315a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
7325a78cd34SGreg Roach
7335a78cd34SGreg Roach        if ($note === null) {
73459f2f229SGreg Roach            throw new NoteNotFoundException();
7355a78cd34SGreg Roach        }
7365a78cd34SGreg Roach
7375a78cd34SGreg Roach        $this->addRecordToCart($note);
7385a78cd34SGreg Roach
7395a78cd34SGreg Roach        return new RedirectResponse($note->url());
7405a78cd34SGreg Roach    }
7415a78cd34SGreg Roach
7425a78cd34SGreg Roach    /**
7435a78cd34SGreg Roach     * @param Request $request
744b6db7c1fSGreg Roach     * @param Tree    $tree
7455a78cd34SGreg Roach     *
7465a78cd34SGreg Roach     * @return Response
7475a78cd34SGreg Roach     */
748b6db7c1fSGreg Roach    public function getAddRepositoryAction(Request $request, Tree $tree): Response
749c1010edaSGreg Roach    {
7509e648e55SGreg Roach        $xref = $request->get('xref', '');
7515a78cd34SGreg Roach
7525a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
7535a78cd34SGreg Roach
7545a78cd34SGreg Roach        if ($repository === null) {
75559f2f229SGreg Roach            throw new RepositoryNotFoundException();
7565a78cd34SGreg Roach        }
7575a78cd34SGreg Roach
7585a78cd34SGreg Roach        $options = $this->repositoryOptions($repository);
7595a78cd34SGreg Roach
7605a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $repository->getFullName());
7615a78cd34SGreg Roach
7625a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
7635a78cd34SGreg Roach            'options' => $options,
7645a78cd34SGreg Roach            'default' => key($options),
7655a78cd34SGreg Roach            'record'  => $repository,
7665a78cd34SGreg Roach            'title'   => $title,
7675a78cd34SGreg Roach            'tree'    => $tree,
7685a78cd34SGreg Roach        ]);
7695a78cd34SGreg Roach    }
7705a78cd34SGreg Roach
7715a78cd34SGreg Roach    /**
7725a78cd34SGreg Roach     * @param Repository $repository
7735a78cd34SGreg Roach     *
7745a78cd34SGreg Roach     * @return string[]
7755a78cd34SGreg Roach     */
776c1010edaSGreg Roach    private function repositoryOptions(Repository $repository): array
777c1010edaSGreg Roach    {
7785a78cd34SGreg Roach        $name = strip_tags($repository->getFullName());
7795a78cd34SGreg Roach
7805a78cd34SGreg Roach        return [
7815a78cd34SGreg Roach            'self' => $name,
7825a78cd34SGreg Roach        ];
7835a78cd34SGreg Roach    }
7845a78cd34SGreg Roach
7855a78cd34SGreg Roach    /**
7865a78cd34SGreg Roach     * @param Request $request
787b6db7c1fSGreg Roach     * @param Tree    $tree
7885a78cd34SGreg Roach     *
7895a78cd34SGreg Roach     * @return RedirectResponse
7905a78cd34SGreg Roach     */
791b6db7c1fSGreg Roach    public function postAddRepositoryAction(Request $request, Tree $tree): RedirectResponse
792c1010edaSGreg Roach    {
7939e648e55SGreg Roach        $xref = $request->get('xref', '');
7945a78cd34SGreg Roach
7955a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
7965a78cd34SGreg Roach
7975a78cd34SGreg Roach        if ($repository === null) {
79859f2f229SGreg Roach            throw new RepositoryNotFoundException();
7995a78cd34SGreg Roach        }
8005a78cd34SGreg Roach
8015a78cd34SGreg Roach        $this->addRecordToCart($repository);
8025a78cd34SGreg Roach
8035a78cd34SGreg Roach        return new RedirectResponse($repository->url());
8045a78cd34SGreg Roach    }
8055a78cd34SGreg Roach
8065a78cd34SGreg Roach    /**
8075a78cd34SGreg Roach     * @param Request $request
808b6db7c1fSGreg Roach     * @param Tree    $tree
8095a78cd34SGreg Roach     *
8105a78cd34SGreg Roach     * @return Response
8115a78cd34SGreg Roach     */
812b6db7c1fSGreg Roach    public function getAddSourceAction(Request $request, Tree $tree): Response
813c1010edaSGreg Roach    {
8149e648e55SGreg Roach        $xref = $request->get('xref', '');
8155a78cd34SGreg Roach
8165a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
8175a78cd34SGreg Roach
8185a78cd34SGreg Roach        if ($source === null) {
81959f2f229SGreg Roach            throw new SourceNotFoundException();
8205a78cd34SGreg Roach        }
8215a78cd34SGreg Roach
8225a78cd34SGreg Roach        $options = $this->sourceOptions($source);
8235a78cd34SGreg Roach
8245a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $source->getFullName());
8255a78cd34SGreg Roach
8265a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
8275a78cd34SGreg Roach            'options' => $options,
8285a78cd34SGreg Roach            'default' => key($options),
8295a78cd34SGreg Roach            'record'  => $source,
8305a78cd34SGreg Roach            'title'   => $title,
8315a78cd34SGreg Roach            'tree'    => $tree,
8325a78cd34SGreg Roach        ]);
8335a78cd34SGreg Roach    }
8345a78cd34SGreg Roach
8355a78cd34SGreg Roach    /**
8365a78cd34SGreg Roach     * @param Source $source
8375a78cd34SGreg Roach     *
8385a78cd34SGreg Roach     * @return string[]
8395a78cd34SGreg Roach     */
840c1010edaSGreg Roach    private function sourceOptions(Source $source): array
841c1010edaSGreg Roach    {
8425a78cd34SGreg Roach        $name = strip_tags($source->getFullName());
8435a78cd34SGreg Roach
8445a78cd34SGreg Roach        return [
8455a78cd34SGreg Roach            'only'   => strip_tags($source->getFullName()),
8465a78cd34SGreg Roach            'linked' => I18N::translate('%s and the individuals that reference it.', $name),
8475a78cd34SGreg Roach        ];
8485a78cd34SGreg Roach    }
8495a78cd34SGreg Roach
8505a78cd34SGreg Roach    /**
8515a78cd34SGreg Roach     * @param Request $request
852b6db7c1fSGreg Roach     * @param Tree    $tree
8535a78cd34SGreg Roach     *
8545a78cd34SGreg Roach     * @return RedirectResponse
8555a78cd34SGreg Roach     */
856b6db7c1fSGreg Roach    public function postAddSourceAction(Request $request, Tree $tree): RedirectResponse
857c1010edaSGreg Roach    {
8589e648e55SGreg Roach        $xref   = $request->get('xref', '');
8599e648e55SGreg Roach        $option = $request->get('option', '');
8605a78cd34SGreg Roach
8615a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
8625a78cd34SGreg Roach
8635a78cd34SGreg Roach        if ($source === null) {
86459f2f229SGreg Roach            throw new SourceNotFoundException();
8655a78cd34SGreg Roach        }
8665a78cd34SGreg Roach
8675a78cd34SGreg Roach        $this->addRecordToCart($source);
8685a78cd34SGreg Roach
8695a78cd34SGreg Roach        if ($option === 'linked') {
8705a78cd34SGreg Roach            foreach ($source->linkedIndividuals('SOUR') as $individual) {
8715a78cd34SGreg Roach                $this->addRecordToCart($individual);
8725a78cd34SGreg Roach            }
8735a78cd34SGreg Roach            foreach ($source->linkedFamilies('SOUR') as $family) {
8745a78cd34SGreg Roach                $this->addRecordToCart($family);
8755a78cd34SGreg Roach            }
8765a78cd34SGreg Roach        }
8775a78cd34SGreg Roach
8785a78cd34SGreg Roach        return new RedirectResponse($source->url());
8795a78cd34SGreg Roach    }
8805a78cd34SGreg Roach
8815a78cd34SGreg Roach    /**
8825a78cd34SGreg Roach     * Get all the records in the cart.
8835a78cd34SGreg Roach     *
8845a78cd34SGreg Roach     * @param Tree $tree
8855a78cd34SGreg Roach     *
8865a78cd34SGreg Roach     * @return GedcomRecord[]
8875a78cd34SGreg Roach     */
888c1010edaSGreg Roach    private function allRecordsInCart(Tree $tree): array
889c1010edaSGreg Roach    {
8905a78cd34SGreg Roach        $cart = Session::get('cart', []);
8915a78cd34SGreg Roach
892aa6f03bbSGreg Roach        $xrefs = array_keys($cart[$tree->name()] ?? []);
8935a78cd34SGreg Roach
8945a78cd34SGreg Roach        // Fetch all the records in the cart.
89518d7a90dSGreg Roach        $records = array_map(function (string $xref) use ($tree): GedcomRecord {
8965a78cd34SGreg Roach            return GedcomRecord::getInstance($xref, $tree);
8975a78cd34SGreg Roach        }, $xrefs);
8985a78cd34SGreg Roach
8995a78cd34SGreg Roach        // Some records may have been deleted after they were added to the cart.
9005a78cd34SGreg Roach        $records = array_filter($records);
9015a78cd34SGreg Roach
9025a78cd34SGreg Roach        // Group and sort.
90318d7a90dSGreg Roach        uasort($records, function (GedcomRecord $x, GedcomRecord $y): int {
904c156e8f5SGreg Roach            return $x::RECORD_TYPE <=> $y::RECORD_TYPE ?: GedcomRecord::nameComparator()($x, $y);
9055a78cd34SGreg Roach        });
9065a78cd34SGreg Roach
9075a78cd34SGreg Roach        return $records;
9085a78cd34SGreg Roach    }
9095a78cd34SGreg Roach
9105a78cd34SGreg Roach    /**
9115a78cd34SGreg Roach     * Add a record (and direclty linked sources, notes, etc. to the cart.
9125a78cd34SGreg Roach     *
9135a78cd34SGreg Roach     * @param GedcomRecord $record
91418d7a90dSGreg Roach     *
91518d7a90dSGreg Roach     * @return void
9165a78cd34SGreg Roach     */
917c1010edaSGreg Roach    private function addRecordToCart(GedcomRecord $record)
918c1010edaSGreg Roach    {
9195a78cd34SGreg Roach        $cart = Session::get('cart', []);
9205a78cd34SGreg Roach
921f4afa648SGreg Roach        $tree_name = $record->tree()->name();
9225a78cd34SGreg Roach
9235a78cd34SGreg Roach        // Add this record
924c0935879SGreg Roach        $cart[$tree_name][$record->xref()] = true;
9255a78cd34SGreg Roach
9265a78cd34SGreg Roach        // Add directly linked media, notes, repositories and sources.
9278d0ebef0SGreg Roach        preg_match_all('/\n\d (?:OBJE|NOTE|SOUR|REPO) @(' . Gedcom::REGEX_XREF . ')@/', $record->gedcom(), $matches);
9285a78cd34SGreg Roach
9295a78cd34SGreg Roach        foreach ($matches[1] as $match) {
9305a78cd34SGreg Roach            $cart[$tree_name][$match] = true;
9315a78cd34SGreg Roach        }
9325a78cd34SGreg Roach
9335a78cd34SGreg Roach        Session::put('cart', $cart);
9345a78cd34SGreg Roach    }
9355a78cd34SGreg Roach
9365a78cd34SGreg Roach    /**
9375a78cd34SGreg Roach     * @param Tree $tree
9385a78cd34SGreg Roach     *
9395a78cd34SGreg Roach     * @return bool
9405a78cd34SGreg Roach     */
941c1010edaSGreg Roach    private function isCartEmpty(Tree $tree): bool
942c1010edaSGreg Roach    {
9435a78cd34SGreg Roach        $cart = Session::get('cart', []);
9445a78cd34SGreg Roach
945aa6f03bbSGreg Roach        return empty($cart[$tree->name()]);
9465a78cd34SGreg Roach    }
9478c2e8227SGreg Roach}
948