xref: /webtrees/app/Module/ClippingsCartModule.php (revision 961ec7555e8fa1828ea16f2a1fd6a74f140ce707)
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
70*961ec755SGreg Roach    /**
71*961ec755SGreg Roach     * How should this module be labelled on tabs, menus, etc.?
72*961ec755SGreg Roach     *
73*961ec755SGreg Roach     * @return string
74*961ec755SGreg Roach     */
7549a243cbSGreg Roach    public function title(): string
76c1010edaSGreg Roach    {
77bbb76c12SGreg Roach        /* I18N: Name of a module */
78bbb76c12SGreg Roach        return I18N::translate('Clippings cart');
798c2e8227SGreg Roach    }
808c2e8227SGreg Roach
81*961ec755SGreg Roach    /**
82*961ec755SGreg Roach     * A sentence describing what this module does.
83*961ec755SGreg Roach     *
84*961ec755SGreg Roach     * @return string
85*961ec755SGreg Roach     */
8649a243cbSGreg Roach    public function description(): string
87c1010edaSGreg Roach    {
88bbb76c12SGreg Roach        /* I18N: Description of the “Clippings cart” module */
89bbb76c12SGreg Roach        return I18N::translate('Select records from your family tree and save them as a GEDCOM file.');
908c2e8227SGreg Roach    }
918c2e8227SGreg Roach
920ee13198SGreg Roach    /**
9349a243cbSGreg Roach     * The default position for this menu.  It can be changed in the control panel.
940ee13198SGreg Roach     *
950ee13198SGreg Roach     * @return int
960ee13198SGreg Roach     */
978f53f488SRico Sonntag    public function defaultMenuOrder(): int
98c1010edaSGreg Roach    {
9946295629SGreg Roach        return 60;
1008c2e8227SGreg Roach    }
1018c2e8227SGreg Roach
1020ee13198SGreg Roach    /**
1030ee13198SGreg Roach     * A menu, to be added to the main application menu.
1040ee13198SGreg Roach     *
105aee13b6dSGreg Roach     * @param Tree $tree
106aee13b6dSGreg Roach     *
1070ee13198SGreg Roach     * @return Menu|null
1080ee13198SGreg Roach     */
10946295629SGreg Roach    public function getMenu(Tree $tree): ?Menu
110c1010edaSGreg Roach    {
1115a78cd34SGreg Roach        $request = Request::createFromGlobals();
1128c2e8227SGreg Roach
1139e648e55SGreg Roach        $route = $request->get('route', '');
1145a78cd34SGreg Roach
1155a78cd34SGreg Roach        $submenus = [
11649a243cbSGreg Roach            new Menu($this->title(), route('module', [
11749a243cbSGreg Roach                'module' => $this->getName(),
118c1010edaSGreg Roach                'action' => 'Show',
119aa6f03bbSGreg Roach                'ged'    => $tree->name(),
120c1010edaSGreg Roach            ]), 'menu-clippings-cart', ['rel' => 'nofollow']),
1215a78cd34SGreg Roach        ];
1225a78cd34SGreg Roach
1235a78cd34SGreg Roach        if (in_array($route, self::ROUTES_WITH_RECORDS)) {
1249e648e55SGreg Roach            $xref      = $request->get('xref', '');
1255a78cd34SGreg Roach            $action    = 'Add' . ucfirst($route);
126c1010edaSGreg Roach            $add_route = route('module', [
12749a243cbSGreg Roach                'module' => $this->getName(),
128c1010edaSGreg Roach                'action' => $action,
129c1010edaSGreg Roach                'xref'   => $xref,
130aa6f03bbSGreg Roach                'ged'    => $tree->name(),
131c1010edaSGreg Roach            ]);
1325a78cd34SGreg Roach
13325b2dde3SGreg Roach            $submenus[] = new Menu(I18N::translate('Add to the clippings cart'), $add_route, 'menu-clippings-add', ['rel' => 'nofollow']);
1348c2e8227SGreg Roach        }
135cbc1590aSGreg Roach
1365a78cd34SGreg Roach        if (!$this->isCartEmpty($tree)) {
137c1010edaSGreg Roach            $submenus[] = new Menu(I18N::translate('Empty the clippings cart'), route('module', [
13849a243cbSGreg Roach                'module' => $this->getName(),
139c1010edaSGreg Roach                'action' => 'Empty',
140aa6f03bbSGreg Roach                'ged'    => $tree->name(),
141c1010edaSGreg Roach            ]), 'menu-clippings-empty', ['rel' => 'nofollow']);
142c1010edaSGreg Roach            $submenus[] = new Menu(I18N::translate('Download'), route('module', [
14349a243cbSGreg Roach                'module' => $this->getName(),
144c1010edaSGreg Roach                'action' => 'DownloadForm',
145aa6f03bbSGreg Roach                'ged'    => $tree->name(),
146c1010edaSGreg Roach            ]), 'menu-clippings-download', ['rel' => 'nofollow']);
1475a78cd34SGreg Roach        }
1485a78cd34SGreg Roach
14949a243cbSGreg Roach        return new Menu($this->title(), '#', 'menu-clippings', ['rel' => 'nofollow'], $submenus);
1508c2e8227SGreg Roach    }
1518c2e8227SGreg Roach
15276692c8bSGreg Roach    /**
1535a78cd34SGreg Roach     * @param Request $request
154b6db7c1fSGreg Roach     * @param Tree    $tree
15576692c8bSGreg Roach     *
1565a78cd34SGreg Roach     * @return BinaryFileResponse
15776692c8bSGreg Roach     */
158b6db7c1fSGreg Roach    public function getDownloadAction(Request $request, Tree $tree): BinaryFileResponse
159c1010edaSGreg Roach    {
1609e648e55SGreg Roach        $privatize_export = $request->get('privatize_export', '');
1615a78cd34SGreg Roach        $convert          = (bool) $request->get('convert');
1628c2e8227SGreg Roach
16313abd6f3SGreg Roach        $cart = Session::get('cart', []);
1648c2e8227SGreg Roach
165aa6f03bbSGreg Roach        $xrefs = array_keys($cart[$tree->name()] ?? []);
1665a78cd34SGreg Roach
1675a78cd34SGreg Roach        // Create a new/empty .ZIP file
1685a78cd34SGreg Roach        $temp_zip_file  = tempnam(sys_get_temp_dir(), 'webtrees-zip-');
1695a78cd34SGreg Roach        $zip_filesystem = new Filesystem(new ZipArchiveAdapter($temp_zip_file));
1705a78cd34SGreg Roach
1715a78cd34SGreg Roach        // Media file prefix
1725a78cd34SGreg Roach        $path = $tree->getPreference('MEDIA_DIRECTORY');
1735a78cd34SGreg Roach
1745a78cd34SGreg Roach        // GEDCOM file header
175a3d8780cSGreg Roach        $filetext = FunctionsExport::gedcomHeader($tree, $convert ? 'ANSI' : 'UTF-8');
1765a78cd34SGreg Roach
1775a78cd34SGreg Roach        switch ($privatize_export) {
1785a78cd34SGreg Roach            case 'gedadmin':
1795a78cd34SGreg Roach                $access_level = Auth::PRIV_NONE;
1805a78cd34SGreg Roach                break;
1815a78cd34SGreg Roach            case 'user':
1825a78cd34SGreg Roach                $access_level = Auth::PRIV_USER;
1835a78cd34SGreg Roach                break;
1845a78cd34SGreg Roach            case 'visitor':
1855a78cd34SGreg Roach                $access_level = Auth::PRIV_PRIVATE;
1865a78cd34SGreg Roach                break;
1875a78cd34SGreg Roach            case 'none':
1885a78cd34SGreg Roach            default:
1895a78cd34SGreg Roach                $access_level = Auth::PRIV_HIDE;
1905a78cd34SGreg Roach                break;
1915a78cd34SGreg Roach        }
1925a78cd34SGreg Roach
1935a78cd34SGreg Roach        foreach ($xrefs as $xref) {
1945a78cd34SGreg Roach            $object = GedcomRecord::getInstance($xref, $tree);
1955a78cd34SGreg Roach            // The object may have been deleted since we added it to the cart....
1965a78cd34SGreg Roach            if ($object) {
1975a78cd34SGreg Roach                $record = $object->privatizeGedcom($access_level);
1985a78cd34SGreg Roach                // Remove links to objects that aren't in the cart
1998d0ebef0SGreg Roach                preg_match_all('/\n1 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[2-9].*)*/', $record, $matches, PREG_SET_ORDER);
2005a78cd34SGreg Roach                foreach ($matches as $match) {
2015a78cd34SGreg Roach                    if (!array_key_exists($match[1], $xrefs)) {
2025a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2035a78cd34SGreg Roach                    }
2045a78cd34SGreg Roach                }
2058d0ebef0SGreg Roach                preg_match_all('/\n2 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[3-9].*)*/', $record, $matches, PREG_SET_ORDER);
2065a78cd34SGreg Roach                foreach ($matches as $match) {
2075a78cd34SGreg Roach                    if (!array_key_exists($match[1], $xrefs)) {
2085a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2095a78cd34SGreg Roach                    }
2105a78cd34SGreg Roach                }
2118d0ebef0SGreg Roach                preg_match_all('/\n3 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[4-9].*)*/', $record, $matches, PREG_SET_ORDER);
2125a78cd34SGreg Roach                foreach ($matches as $match) {
2135a78cd34SGreg Roach                    if (!array_key_exists($match[1], $xrefs)) {
2145a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2155a78cd34SGreg Roach                    }
2165a78cd34SGreg Roach                }
2175a78cd34SGreg Roach
21855167344SGreg Roach                if ($object instanceof Individual || $object instanceof Family) {
2195a78cd34SGreg Roach                    $filetext .= $record . "\n";
2205a78cd34SGreg Roach                    $filetext .= "1 SOUR @WEBTREES@\n";
2211f273236SGreg Roach                    $filetext .= '2 PAGE ' . $object->url() . "\n";
22255167344SGreg Roach                } elseif ($object instanceof Source) {
2235a78cd34SGreg Roach                    $filetext .= $record . "\n";
2241f273236SGreg Roach                    $filetext .= '1 NOTE ' . $object->url() . "\n";
22555167344SGreg Roach                } elseif ($object instanceof Media) {
22655167344SGreg Roach                    // Add the media files to the archive
2275a78cd34SGreg Roach                    foreach ($object->mediaFiles() as $media_file) {
2285a78cd34SGreg Roach                        if (file_exists($media_file->getServerFilename())) {
2295a78cd34SGreg Roach                            $fp = fopen($media_file->getServerFilename(), 'r');
2305a78cd34SGreg Roach                            $zip_filesystem->writeStream($path . $media_file->filename(), $fp);
2315a78cd34SGreg Roach                            fclose($fp);
2325a78cd34SGreg Roach                        }
2335a78cd34SGreg Roach                    }
2345a78cd34SGreg Roach                    $filetext .= $record . "\n";
23555167344SGreg Roach                } else {
2365a78cd34SGreg Roach                    $filetext .= $record . "\n";
2378c2e8227SGreg Roach                }
2388c2e8227SGreg Roach            }
2398c2e8227SGreg Roach        }
2408c2e8227SGreg Roach
2415a78cd34SGreg Roach        // Create a source, to indicate the source of the data.
2425a78cd34SGreg Roach        $filetext .= "0 @WEBTREES@ SOUR\n1 TITL " . WT_BASE_URL . "\n";
2437fcd9838SGreg Roach        $author = User::find((int) $tree->getPreference('CONTACT_USER_ID'));
2445a78cd34SGreg Roach        if ($author !== null) {
2455a78cd34SGreg Roach            $filetext .= '1 AUTH ' . $author->getRealName() . "\n";
2465a78cd34SGreg Roach        }
2475a78cd34SGreg Roach        $filetext .= "0 TRLR\n";
2485a78cd34SGreg Roach
2495a78cd34SGreg Roach        // Make sure the preferred line endings are used
250a3d8780cSGreg Roach        $filetext = str_replace('\n', Gedcom::EOL, $filetext);
2515a78cd34SGreg Roach
25255167344SGreg Roach        if ($convert) {
2535a78cd34SGreg Roach            $filetext = utf8_decode($filetext);
2548c2e8227SGreg Roach        }
255cbc1590aSGreg Roach
2565a78cd34SGreg Roach        // Finally add the GEDCOM file to the .ZIP file.
2575a78cd34SGreg Roach        $zip_filesystem->write('clippings.ged', $filetext);
2585a78cd34SGreg Roach
2595a78cd34SGreg Roach        // Need to force-close the filesystem
26002a92f80SGreg Roach        unset($zip_filesystem);
2615a78cd34SGreg Roach
2625a78cd34SGreg Roach        $response = new BinaryFileResponse($temp_zip_file);
2635a78cd34SGreg Roach        $response->deleteFileAfterSend(true);
2645a78cd34SGreg Roach
2655a78cd34SGreg Roach        $response->headers->set('Content-Type', 'application/zip');
2665a78cd34SGreg Roach        $response->setContentDisposition(
2675a78cd34SGreg Roach            ResponseHeaderBag::DISPOSITION_ATTACHMENT,
2685a78cd34SGreg Roach            'clippings.zip'
2695a78cd34SGreg Roach        );
2705a78cd34SGreg Roach
2715a78cd34SGreg Roach        return $response;
2728c2e8227SGreg Roach    }
2738c2e8227SGreg Roach
2748c2e8227SGreg Roach    /**
275b6db7c1fSGreg Roach     * @param Tree $tree
276b6db7c1fSGreg Roach     * @param User $user
27776692c8bSGreg Roach     *
2785a78cd34SGreg Roach     * @return Response
2798c2e8227SGreg Roach     */
280b6db7c1fSGreg Roach    public function getDownloadFormAction(Tree $tree, User $user): Response
281c1010edaSGreg Roach    {
2825a78cd34SGreg Roach        $title = I18N::translate('Family tree clippings cart') . ' — ' . I18N::translate('Download');
2838c2e8227SGreg Roach
2845a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/download', [
2855a78cd34SGreg Roach            'is_manager' => Auth::isManager($tree, $user),
2865a78cd34SGreg Roach            'is_member'  => Auth::isMember($tree, $user),
2875a78cd34SGreg Roach            'title'      => $title,
2885a78cd34SGreg Roach        ]);
2898c2e8227SGreg Roach    }
2908c2e8227SGreg Roach
2915a78cd34SGreg Roach    /**
292b6db7c1fSGreg Roach     * @param Tree $tree
2935a78cd34SGreg Roach     *
2945a78cd34SGreg Roach     * @return RedirectResponse
2955a78cd34SGreg Roach     */
296b6db7c1fSGreg Roach    public function getEmptyAction(Tree $tree): RedirectResponse
297c1010edaSGreg Roach    {
2985a78cd34SGreg Roach        $cart                = Session::get('cart', []);
299aa6f03bbSGreg Roach        $cart[$tree->name()] = [];
3005a78cd34SGreg Roach        Session::put('cart', $cart);
3018c2e8227SGreg Roach
302c1010edaSGreg Roach        $url = route('module', [
30349a243cbSGreg Roach            'module' => $this->getName(),
304c1010edaSGreg Roach            'action' => 'Show',
305aa6f03bbSGreg Roach            'ged'    => $tree->name(),
306c1010edaSGreg Roach        ]);
3075a78cd34SGreg Roach
3085a78cd34SGreg Roach        return new RedirectResponse($url);
3095a78cd34SGreg Roach    }
3105a78cd34SGreg Roach
3115a78cd34SGreg Roach    /**
3125a78cd34SGreg Roach     * @param Request $request
313b6db7c1fSGreg Roach     * @param Tree    $tree
3145a78cd34SGreg Roach     *
3155a78cd34SGreg Roach     * @return RedirectResponse
3165a78cd34SGreg Roach     */
317b6db7c1fSGreg Roach    public function postRemoveAction(Request $request, Tree $tree): RedirectResponse
318c1010edaSGreg Roach    {
3199e648e55SGreg Roach        $xref = $request->get('xref', '');
3205a78cd34SGreg Roach
3215a78cd34SGreg Roach        $cart = Session::get('cart', []);
322aa6f03bbSGreg Roach        unset($cart[$tree->name()][$xref]);
3235a78cd34SGreg Roach        Session::put('cart', $cart);
3245a78cd34SGreg Roach
325c1010edaSGreg Roach        $url = route('module', [
32649a243cbSGreg Roach            'module' => $this->getName(),
327c1010edaSGreg Roach            'action' => 'Show',
328aa6f03bbSGreg Roach            'ged'    => $tree->name(),
329c1010edaSGreg Roach        ]);
3305a78cd34SGreg Roach
3315a78cd34SGreg Roach        return new RedirectResponse($url);
3325a78cd34SGreg Roach    }
3335a78cd34SGreg Roach
3345a78cd34SGreg Roach    /**
335b6db7c1fSGreg Roach     * @param Tree $tree
3365a78cd34SGreg Roach     *
3375a78cd34SGreg Roach     * @return Response
3385a78cd34SGreg Roach     */
339b6db7c1fSGreg Roach    public function getShowAction(Tree $tree): Response
340c1010edaSGreg Roach    {
3415a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/show', [
3425a78cd34SGreg Roach            'records' => $this->allRecordsInCart($tree),
3435a78cd34SGreg Roach            'title'   => I18N::translate('Family tree clippings cart'),
3445a78cd34SGreg Roach            'tree'    => $tree,
3455a78cd34SGreg Roach        ]);
3465a78cd34SGreg Roach    }
3475a78cd34SGreg Roach
3485a78cd34SGreg Roach    /**
3495a78cd34SGreg Roach     * @param Request $request
350b6db7c1fSGreg Roach     * @param Tree    $tree
3515a78cd34SGreg Roach     *
3525a78cd34SGreg Roach     * @return Response
3535a78cd34SGreg Roach     */
354b6db7c1fSGreg Roach    public function getAddFamilyAction(Request $request, Tree $tree): Response
355c1010edaSGreg Roach    {
3569e648e55SGreg Roach        $xref = $request->get('xref', '');
3575a78cd34SGreg Roach
3585a78cd34SGreg Roach        $family = Family::getInstance($xref, $tree);
3595a78cd34SGreg Roach
3605a78cd34SGreg Roach        if ($family === null) {
36159f2f229SGreg Roach            throw new FamilyNotFoundException();
3625a78cd34SGreg Roach        }
3635a78cd34SGreg Roach
3645a78cd34SGreg Roach        $options = $this->familyOptions($family);
3655a78cd34SGreg Roach
3665a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $family->getFullName());
3675a78cd34SGreg Roach
3685a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
3695a78cd34SGreg Roach            'options' => $options,
3705a78cd34SGreg Roach            'default' => key($options),
3715a78cd34SGreg Roach            'record'  => $family,
3725a78cd34SGreg Roach            'title'   => $title,
3735a78cd34SGreg Roach            'tree'    => $tree,
3745a78cd34SGreg Roach        ]);
3755a78cd34SGreg Roach    }
3765a78cd34SGreg Roach
3775a78cd34SGreg Roach    /**
3785a78cd34SGreg Roach     * @param Family $family
3795a78cd34SGreg Roach     *
3805a78cd34SGreg Roach     * @return string[]
3815a78cd34SGreg Roach     */
382c1010edaSGreg Roach    private function familyOptions(Family $family): array
383c1010edaSGreg Roach    {
3845a78cd34SGreg Roach        $name = strip_tags($family->getFullName());
3855a78cd34SGreg Roach
3865a78cd34SGreg Roach        return [
3875a78cd34SGreg Roach            'parents'     => $name,
388bbb76c12SGreg Roach            /* I18N: %s is a family (husband + wife) */
389bbb76c12SGreg Roach            'members'     => I18N::translate('%s and their children', $name),
390bbb76c12SGreg Roach            /* I18N: %s is a family (husband + wife) */
391bbb76c12SGreg Roach            'descendants' => I18N::translate('%s and their descendants', $name),
3925a78cd34SGreg Roach        ];
3935a78cd34SGreg Roach    }
3945a78cd34SGreg Roach
3955a78cd34SGreg Roach    /**
3965a78cd34SGreg Roach     * @param Request $request
397b6db7c1fSGreg Roach     * @param Tree    $tree
3985a78cd34SGreg Roach     *
3995a78cd34SGreg Roach     * @return RedirectResponse
4005a78cd34SGreg Roach     */
401b6db7c1fSGreg Roach    public function postAddFamilyAction(Request $request, Tree $tree): RedirectResponse
402c1010edaSGreg Roach    {
4039e648e55SGreg Roach        $xref   = $request->get('xref', '');
4049e648e55SGreg Roach        $option = $request->get('option', '');
4055a78cd34SGreg Roach
4065a78cd34SGreg Roach        $family = Family::getInstance($xref, $tree);
4075a78cd34SGreg Roach
4085a78cd34SGreg Roach        if ($family === null) {
40959f2f229SGreg Roach            throw new FamilyNotFoundException();
4105a78cd34SGreg Roach        }
4115a78cd34SGreg Roach
4125a78cd34SGreg Roach        switch ($option) {
4135a78cd34SGreg Roach            case 'parents':
4145a78cd34SGreg Roach                $this->addFamilyToCart($family);
4155a78cd34SGreg Roach                break;
4165a78cd34SGreg Roach
4175a78cd34SGreg Roach            case 'members':
4185a78cd34SGreg Roach                $this->addFamilyAndChildrenToCart($family);
4195a78cd34SGreg Roach                break;
4205a78cd34SGreg Roach
4215a78cd34SGreg Roach            case 'descendants':
4225a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($family);
4235a78cd34SGreg Roach                break;
4245a78cd34SGreg Roach        }
4255a78cd34SGreg Roach
4265a78cd34SGreg Roach        return new RedirectResponse($family->url());
4275a78cd34SGreg Roach    }
4285a78cd34SGreg Roach
4295a78cd34SGreg Roach    /**
4305a78cd34SGreg Roach     * @param Family $family
43118d7a90dSGreg Roach     *
43218d7a90dSGreg Roach     * @return void
4335a78cd34SGreg Roach     */
434c1010edaSGreg Roach    private function addFamilyToCart(Family $family)
435c1010edaSGreg Roach    {
4365a78cd34SGreg Roach        $this->addRecordToCart($family);
4375a78cd34SGreg Roach
4385a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
4395a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4405a78cd34SGreg Roach        }
4415a78cd34SGreg Roach    }
4425a78cd34SGreg Roach
4435a78cd34SGreg Roach    /**
4445a78cd34SGreg Roach     * @param Family $family
44518d7a90dSGreg Roach     *
44618d7a90dSGreg Roach     * @return void
4475a78cd34SGreg Roach     */
448c1010edaSGreg Roach    private function addFamilyAndChildrenToCart(Family $family)
449c1010edaSGreg Roach    {
4505a78cd34SGreg Roach        $this->addRecordToCart($family);
4515a78cd34SGreg Roach
4525a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
4535a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4545a78cd34SGreg Roach        }
4555a78cd34SGreg Roach        foreach ($family->getChildren() as $child) {
4565a78cd34SGreg Roach            $this->addRecordToCart($child);
4575a78cd34SGreg Roach        }
4585a78cd34SGreg Roach    }
4595a78cd34SGreg Roach
4605a78cd34SGreg Roach    /**
4615a78cd34SGreg Roach     * @param Family $family
46218d7a90dSGreg Roach     *
46318d7a90dSGreg Roach     * @return void
4645a78cd34SGreg Roach     */
465c1010edaSGreg Roach    private function addFamilyAndDescendantsToCart(Family $family)
466c1010edaSGreg Roach    {
4675a78cd34SGreg Roach        $this->addRecordToCart($family);
4685a78cd34SGreg Roach
4695a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
4705a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4715a78cd34SGreg Roach        }
4725a78cd34SGreg Roach        foreach ($family->getChildren() as $child) {
4735a78cd34SGreg Roach            $this->addRecordToCart($child);
4745a78cd34SGreg Roach            foreach ($child->getSpouseFamilies() as $child_family) {
4755a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($child_family);
4765a78cd34SGreg Roach            }
4775a78cd34SGreg Roach        }
4785a78cd34SGreg Roach    }
4795a78cd34SGreg Roach
4805a78cd34SGreg Roach    /**
4815a78cd34SGreg Roach     * @param Request $request
482b6db7c1fSGreg Roach     * @param Tree    $tree
4835a78cd34SGreg Roach     *
4845a78cd34SGreg Roach     * @return Response
4855a78cd34SGreg Roach     */
486b6db7c1fSGreg Roach    public function getAddIndividualAction(Request $request, Tree $tree): Response
487c1010edaSGreg Roach    {
4889e648e55SGreg Roach        $xref = $request->get('xref', '');
4895a78cd34SGreg Roach
4905a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
4915a78cd34SGreg Roach
4925a78cd34SGreg Roach        if ($individual === null) {
49359f2f229SGreg Roach            throw new IndividualNotFoundException();
4945a78cd34SGreg Roach        }
4955a78cd34SGreg Roach
4965a78cd34SGreg Roach        $options = $this->individualOptions($individual);
4975a78cd34SGreg Roach
4985a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $individual->getFullName());
4995a78cd34SGreg Roach
5005a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
5015a78cd34SGreg Roach            'options' => $options,
5025a78cd34SGreg Roach            'default' => key($options),
5035a78cd34SGreg Roach            'record'  => $individual,
5045a78cd34SGreg Roach            'title'   => $title,
5055a78cd34SGreg Roach            'tree'    => $tree,
5065a78cd34SGreg Roach        ]);
5075a78cd34SGreg Roach    }
5085a78cd34SGreg Roach
5095a78cd34SGreg Roach    /**
5105a78cd34SGreg Roach     * @param Individual $individual
5115a78cd34SGreg Roach     *
5125a78cd34SGreg Roach     * @return string[]
5135a78cd34SGreg Roach     */
514c1010edaSGreg Roach    private function individualOptions(Individual $individual): array
515c1010edaSGreg Roach    {
5165a78cd34SGreg Roach        $name = strip_tags($individual->getFullName());
5175a78cd34SGreg Roach
5185a78cd34SGreg Roach        if ($individual->getSex() === 'F') {
5195a78cd34SGreg Roach            return [
5205a78cd34SGreg Roach                'self'              => $name,
5215a78cd34SGreg Roach                'parents'           => I18N::translate('%s, her parents and siblings', $name),
5225a78cd34SGreg Roach                'spouses'           => I18N::translate('%s, her spouses and children', $name),
5235a78cd34SGreg Roach                'ancestors'         => I18N::translate('%s and her ancestors', $name),
5245a78cd34SGreg Roach                'ancestor_families' => I18N::translate('%s, her ancestors and their families', $name),
5255a78cd34SGreg Roach                'descendants'       => I18N::translate('%s, her spouses and descendants', $name),
5265a78cd34SGreg Roach            ];
527b2ce94c6SRico Sonntag        }
528b2ce94c6SRico Sonntag
5295a78cd34SGreg Roach        return [
5305a78cd34SGreg Roach            'self'              => $name,
5315a78cd34SGreg Roach            'parents'           => I18N::translate('%s, his parents and siblings', $name),
5325a78cd34SGreg Roach            'spouses'           => I18N::translate('%s, his spouses and children', $name),
5335a78cd34SGreg Roach            'ancestors'         => I18N::translate('%s and his ancestors', $name),
5345a78cd34SGreg Roach            'ancestor_families' => I18N::translate('%s, his ancestors and their families', $name),
5355a78cd34SGreg Roach            'descendants'       => I18N::translate('%s, his spouses and descendants', $name),
5365a78cd34SGreg Roach        ];
5375a78cd34SGreg Roach    }
5385a78cd34SGreg Roach
5395a78cd34SGreg Roach    /**
5405a78cd34SGreg Roach     * @param Request $request
541b6db7c1fSGreg Roach     * @param Tree    $tree
5425a78cd34SGreg Roach     *
5435a78cd34SGreg Roach     * @return RedirectResponse
5445a78cd34SGreg Roach     */
545b6db7c1fSGreg Roach    public function postAddIndividualAction(Request $request, Tree $tree): RedirectResponse
546c1010edaSGreg Roach    {
5479e648e55SGreg Roach        $xref   = $request->get('xref', '');
5489e648e55SGreg Roach        $option = $request->get('option', '');
5495a78cd34SGreg Roach
5505a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
5515a78cd34SGreg Roach
5525a78cd34SGreg Roach        if ($individual === null) {
55359f2f229SGreg Roach            throw new IndividualNotFoundException();
5545a78cd34SGreg Roach        }
5555a78cd34SGreg Roach
5565a78cd34SGreg Roach        switch ($option) {
5575a78cd34SGreg Roach            case 'self':
5585a78cd34SGreg Roach                $this->addRecordToCart($individual);
5595a78cd34SGreg Roach                break;
5605a78cd34SGreg Roach
5615a78cd34SGreg Roach            case 'parents':
5625a78cd34SGreg Roach                foreach ($individual->getChildFamilies() as $family) {
5635a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
5645a78cd34SGreg Roach                }
5655a78cd34SGreg Roach                break;
5665a78cd34SGreg Roach
5675a78cd34SGreg Roach            case 'spouses':
5685a78cd34SGreg Roach                foreach ($individual->getSpouseFamilies() as $family) {
5695a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
5705a78cd34SGreg Roach                }
5715a78cd34SGreg Roach                break;
5725a78cd34SGreg Roach
5735a78cd34SGreg Roach            case 'ancestors':
5745a78cd34SGreg Roach                $this->addAncestorsToCart($individual);
5755a78cd34SGreg Roach                break;
5765a78cd34SGreg Roach
5775a78cd34SGreg Roach            case 'ancestor_families':
5785a78cd34SGreg Roach                $this->addAncestorFamiliesToCart($individual);
5795a78cd34SGreg Roach                break;
5805a78cd34SGreg Roach
5815a78cd34SGreg Roach            case 'descendants':
5825a78cd34SGreg Roach                foreach ($individual->getSpouseFamilies() as $family) {
5835a78cd34SGreg Roach                    $this->addFamilyAndDescendantsToCart($family);
5845a78cd34SGreg Roach                }
5855a78cd34SGreg Roach                break;
5865a78cd34SGreg Roach        }
5875a78cd34SGreg Roach
5885a78cd34SGreg Roach        return new RedirectResponse($individual->url());
5895a78cd34SGreg Roach    }
5905a78cd34SGreg Roach
5915a78cd34SGreg Roach    /**
5925a78cd34SGreg Roach     * @param Individual $individual
59318d7a90dSGreg Roach     *
59418d7a90dSGreg Roach     * @return void
5955a78cd34SGreg Roach     */
596c1010edaSGreg Roach    private function addAncestorsToCart(Individual $individual)
597c1010edaSGreg Roach    {
5985a78cd34SGreg Roach        $this->addRecordToCart($individual);
5995a78cd34SGreg Roach
6005a78cd34SGreg Roach        foreach ($individual->getChildFamilies() as $family) {
6015a78cd34SGreg Roach            foreach ($family->getSpouses() as $parent) {
6025a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
6035a78cd34SGreg Roach            }
6045a78cd34SGreg Roach        }
6055a78cd34SGreg Roach    }
6065a78cd34SGreg Roach
6075a78cd34SGreg Roach    /**
6085a78cd34SGreg Roach     * @param Individual $individual
60918d7a90dSGreg Roach     *
61018d7a90dSGreg Roach     * @return void
6115a78cd34SGreg Roach     */
612c1010edaSGreg Roach    private function addAncestorFamiliesToCart(Individual $individual)
613c1010edaSGreg Roach    {
6145a78cd34SGreg Roach        foreach ($individual->getChildFamilies() as $family) {
6155a78cd34SGreg Roach            $this->addFamilyAndChildrenToCart($family);
6165a78cd34SGreg Roach            foreach ($family->getSpouses() as $parent) {
6175a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
6185a78cd34SGreg Roach            }
6195a78cd34SGreg Roach        }
6205a78cd34SGreg Roach    }
6215a78cd34SGreg Roach
6225a78cd34SGreg Roach    /**
6235a78cd34SGreg Roach     * @param Request $request
624b6db7c1fSGreg Roach     * @param Tree    $tree
6255a78cd34SGreg Roach     *
6265a78cd34SGreg Roach     * @return Response
6275a78cd34SGreg Roach     */
628b6db7c1fSGreg Roach    public function getAddMediaAction(Request $request, Tree $tree): Response
629c1010edaSGreg Roach    {
6309e648e55SGreg Roach        $xref = $request->get('xref', '');
6315a78cd34SGreg Roach
6325a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
6335a78cd34SGreg Roach
6345a78cd34SGreg Roach        if ($media === null) {
63559f2f229SGreg Roach            throw new MediaNotFoundException();
6365a78cd34SGreg Roach        }
6375a78cd34SGreg Roach
6385a78cd34SGreg Roach        $options = $this->mediaOptions($media);
6395a78cd34SGreg Roach
6405a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $media->getFullName());
6415a78cd34SGreg Roach
6425a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
6435a78cd34SGreg Roach            'options' => $options,
6445a78cd34SGreg Roach            'default' => key($options),
6455a78cd34SGreg Roach            'record'  => $media,
6465a78cd34SGreg Roach            'title'   => $title,
6475a78cd34SGreg Roach            'tree'    => $tree,
6485a78cd34SGreg Roach        ]);
6495a78cd34SGreg Roach    }
6505a78cd34SGreg Roach
6515a78cd34SGreg Roach    /**
6525a78cd34SGreg Roach     * @param Media $media
6535a78cd34SGreg Roach     *
6545a78cd34SGreg Roach     * @return string[]
6555a78cd34SGreg Roach     */
656c1010edaSGreg Roach    private function mediaOptions(Media $media): array
657c1010edaSGreg Roach    {
6585a78cd34SGreg Roach        $name = strip_tags($media->getFullName());
6595a78cd34SGreg Roach
6605a78cd34SGreg Roach        return [
6615a78cd34SGreg Roach            'self' => $name,
6625a78cd34SGreg Roach        ];
6635a78cd34SGreg Roach    }
6645a78cd34SGreg Roach
6655a78cd34SGreg Roach    /**
6665a78cd34SGreg Roach     * @param Request $request
667b6db7c1fSGreg Roach     * @param Tree    $tree
6685a78cd34SGreg Roach     *
6695a78cd34SGreg Roach     * @return RedirectResponse
6705a78cd34SGreg Roach     */
671b6db7c1fSGreg Roach    public function postAddMediaAction(Request $request, Tree $tree): RedirectResponse
672c1010edaSGreg Roach    {
6739e648e55SGreg Roach        $xref = $request->get('xref', '');
6745a78cd34SGreg Roach
6755a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
6765a78cd34SGreg Roach
6775a78cd34SGreg Roach        if ($media === null) {
67859f2f229SGreg Roach            throw new MediaNotFoundException();
6795a78cd34SGreg Roach        }
6805a78cd34SGreg Roach
6815a78cd34SGreg Roach        $this->addRecordToCart($media);
6825a78cd34SGreg Roach
6835a78cd34SGreg Roach        return new RedirectResponse($media->url());
6845a78cd34SGreg Roach    }
6855a78cd34SGreg Roach
6865a78cd34SGreg Roach    /**
6875a78cd34SGreg Roach     * @param Request $request
688b6db7c1fSGreg Roach     * @param Tree    $tree
6895a78cd34SGreg Roach     *
6905a78cd34SGreg Roach     * @return Response
6915a78cd34SGreg Roach     */
692b6db7c1fSGreg Roach    public function getAddNoteAction(Request $request, Tree $tree): Response
693c1010edaSGreg Roach    {
6949e648e55SGreg Roach        $xref = $request->get('xref', '');
6955a78cd34SGreg Roach
6965a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
6975a78cd34SGreg Roach
6985a78cd34SGreg Roach        if ($note === null) {
69959f2f229SGreg Roach            throw new NoteNotFoundException();
7005a78cd34SGreg Roach        }
7015a78cd34SGreg Roach
7025a78cd34SGreg Roach        $options = $this->noteOptions($note);
7035a78cd34SGreg Roach
7045a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $note->getFullName());
7055a78cd34SGreg Roach
7065a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
7075a78cd34SGreg Roach            'options' => $options,
7085a78cd34SGreg Roach            'default' => key($options),
7095a78cd34SGreg Roach            'record'  => $note,
7105a78cd34SGreg Roach            'title'   => $title,
7115a78cd34SGreg Roach            'tree'    => $tree,
7125a78cd34SGreg Roach        ]);
7135a78cd34SGreg Roach    }
7145a78cd34SGreg Roach
7155a78cd34SGreg Roach    /**
7165a78cd34SGreg Roach     * @param Note $note
7175a78cd34SGreg Roach     *
7185a78cd34SGreg Roach     * @return string[]
7195a78cd34SGreg Roach     */
720c1010edaSGreg Roach    private function noteOptions(Note $note): array
721c1010edaSGreg Roach    {
7225a78cd34SGreg Roach        $name = strip_tags($note->getFullName());
7235a78cd34SGreg Roach
7245a78cd34SGreg Roach        return [
7255a78cd34SGreg Roach            'self' => $name,
7265a78cd34SGreg Roach        ];
7275a78cd34SGreg Roach    }
7285a78cd34SGreg Roach
7295a78cd34SGreg Roach    /**
7305a78cd34SGreg Roach     * @param Request $request
731b6db7c1fSGreg Roach     * @param Tree    $tree
7325a78cd34SGreg Roach     *
7335a78cd34SGreg Roach     * @return RedirectResponse
7345a78cd34SGreg Roach     */
735b6db7c1fSGreg Roach    public function postAddNoteAction(Request $request, Tree $tree): RedirectResponse
736c1010edaSGreg Roach    {
7379e648e55SGreg Roach        $xref = $request->get('xref', '');
7385a78cd34SGreg Roach
7395a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
7405a78cd34SGreg Roach
7415a78cd34SGreg Roach        if ($note === null) {
74259f2f229SGreg Roach            throw new NoteNotFoundException();
7435a78cd34SGreg Roach        }
7445a78cd34SGreg Roach
7455a78cd34SGreg Roach        $this->addRecordToCart($note);
7465a78cd34SGreg Roach
7475a78cd34SGreg Roach        return new RedirectResponse($note->url());
7485a78cd34SGreg Roach    }
7495a78cd34SGreg Roach
7505a78cd34SGreg Roach    /**
7515a78cd34SGreg Roach     * @param Request $request
752b6db7c1fSGreg Roach     * @param Tree    $tree
7535a78cd34SGreg Roach     *
7545a78cd34SGreg Roach     * @return Response
7555a78cd34SGreg Roach     */
756b6db7c1fSGreg Roach    public function getAddRepositoryAction(Request $request, Tree $tree): Response
757c1010edaSGreg Roach    {
7589e648e55SGreg Roach        $xref = $request->get('xref', '');
7595a78cd34SGreg Roach
7605a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
7615a78cd34SGreg Roach
7625a78cd34SGreg Roach        if ($repository === null) {
76359f2f229SGreg Roach            throw new RepositoryNotFoundException();
7645a78cd34SGreg Roach        }
7655a78cd34SGreg Roach
7665a78cd34SGreg Roach        $options = $this->repositoryOptions($repository);
7675a78cd34SGreg Roach
7685a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $repository->getFullName());
7695a78cd34SGreg Roach
7705a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
7715a78cd34SGreg Roach            'options' => $options,
7725a78cd34SGreg Roach            'default' => key($options),
7735a78cd34SGreg Roach            'record'  => $repository,
7745a78cd34SGreg Roach            'title'   => $title,
7755a78cd34SGreg Roach            'tree'    => $tree,
7765a78cd34SGreg Roach        ]);
7775a78cd34SGreg Roach    }
7785a78cd34SGreg Roach
7795a78cd34SGreg Roach    /**
7805a78cd34SGreg Roach     * @param Repository $repository
7815a78cd34SGreg Roach     *
7825a78cd34SGreg Roach     * @return string[]
7835a78cd34SGreg Roach     */
784c1010edaSGreg Roach    private function repositoryOptions(Repository $repository): array
785c1010edaSGreg Roach    {
7865a78cd34SGreg Roach        $name = strip_tags($repository->getFullName());
7875a78cd34SGreg Roach
7885a78cd34SGreg Roach        return [
7895a78cd34SGreg Roach            'self' => $name,
7905a78cd34SGreg Roach        ];
7915a78cd34SGreg Roach    }
7925a78cd34SGreg Roach
7935a78cd34SGreg Roach    /**
7945a78cd34SGreg Roach     * @param Request $request
795b6db7c1fSGreg Roach     * @param Tree    $tree
7965a78cd34SGreg Roach     *
7975a78cd34SGreg Roach     * @return RedirectResponse
7985a78cd34SGreg Roach     */
799b6db7c1fSGreg Roach    public function postAddRepositoryAction(Request $request, Tree $tree): RedirectResponse
800c1010edaSGreg Roach    {
8019e648e55SGreg Roach        $xref = $request->get('xref', '');
8025a78cd34SGreg Roach
8035a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
8045a78cd34SGreg Roach
8055a78cd34SGreg Roach        if ($repository === null) {
80659f2f229SGreg Roach            throw new RepositoryNotFoundException();
8075a78cd34SGreg Roach        }
8085a78cd34SGreg Roach
8095a78cd34SGreg Roach        $this->addRecordToCart($repository);
8105a78cd34SGreg Roach
8115a78cd34SGreg Roach        return new RedirectResponse($repository->url());
8125a78cd34SGreg Roach    }
8135a78cd34SGreg Roach
8145a78cd34SGreg Roach    /**
8155a78cd34SGreg Roach     * @param Request $request
816b6db7c1fSGreg Roach     * @param Tree    $tree
8175a78cd34SGreg Roach     *
8185a78cd34SGreg Roach     * @return Response
8195a78cd34SGreg Roach     */
820b6db7c1fSGreg Roach    public function getAddSourceAction(Request $request, Tree $tree): Response
821c1010edaSGreg Roach    {
8229e648e55SGreg Roach        $xref = $request->get('xref', '');
8235a78cd34SGreg Roach
8245a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
8255a78cd34SGreg Roach
8265a78cd34SGreg Roach        if ($source === null) {
82759f2f229SGreg Roach            throw new SourceNotFoundException();
8285a78cd34SGreg Roach        }
8295a78cd34SGreg Roach
8305a78cd34SGreg Roach        $options = $this->sourceOptions($source);
8315a78cd34SGreg Roach
8325a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $source->getFullName());
8335a78cd34SGreg Roach
8345a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
8355a78cd34SGreg Roach            'options' => $options,
8365a78cd34SGreg Roach            'default' => key($options),
8375a78cd34SGreg Roach            'record'  => $source,
8385a78cd34SGreg Roach            'title'   => $title,
8395a78cd34SGreg Roach            'tree'    => $tree,
8405a78cd34SGreg Roach        ]);
8415a78cd34SGreg Roach    }
8425a78cd34SGreg Roach
8435a78cd34SGreg Roach    /**
8445a78cd34SGreg Roach     * @param Source $source
8455a78cd34SGreg Roach     *
8465a78cd34SGreg Roach     * @return string[]
8475a78cd34SGreg Roach     */
848c1010edaSGreg Roach    private function sourceOptions(Source $source): array
849c1010edaSGreg Roach    {
8505a78cd34SGreg Roach        $name = strip_tags($source->getFullName());
8515a78cd34SGreg Roach
8525a78cd34SGreg Roach        return [
8535a78cd34SGreg Roach            'only'   => strip_tags($source->getFullName()),
8545a78cd34SGreg Roach            'linked' => I18N::translate('%s and the individuals that reference it.', $name),
8555a78cd34SGreg Roach        ];
8565a78cd34SGreg Roach    }
8575a78cd34SGreg Roach
8585a78cd34SGreg Roach    /**
8595a78cd34SGreg Roach     * @param Request $request
860b6db7c1fSGreg Roach     * @param Tree    $tree
8615a78cd34SGreg Roach     *
8625a78cd34SGreg Roach     * @return RedirectResponse
8635a78cd34SGreg Roach     */
864b6db7c1fSGreg Roach    public function postAddSourceAction(Request $request, Tree $tree): RedirectResponse
865c1010edaSGreg Roach    {
8669e648e55SGreg Roach        $xref   = $request->get('xref', '');
8679e648e55SGreg Roach        $option = $request->get('option', '');
8685a78cd34SGreg Roach
8695a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
8705a78cd34SGreg Roach
8715a78cd34SGreg Roach        if ($source === null) {
87259f2f229SGreg Roach            throw new SourceNotFoundException();
8735a78cd34SGreg Roach        }
8745a78cd34SGreg Roach
8755a78cd34SGreg Roach        $this->addRecordToCart($source);
8765a78cd34SGreg Roach
8775a78cd34SGreg Roach        if ($option === 'linked') {
8785a78cd34SGreg Roach            foreach ($source->linkedIndividuals('SOUR') as $individual) {
8795a78cd34SGreg Roach                $this->addRecordToCart($individual);
8805a78cd34SGreg Roach            }
8815a78cd34SGreg Roach            foreach ($source->linkedFamilies('SOUR') as $family) {
8825a78cd34SGreg Roach                $this->addRecordToCart($family);
8835a78cd34SGreg Roach            }
8845a78cd34SGreg Roach        }
8855a78cd34SGreg Roach
8865a78cd34SGreg Roach        return new RedirectResponse($source->url());
8875a78cd34SGreg Roach    }
8885a78cd34SGreg Roach
8895a78cd34SGreg Roach    /**
8905a78cd34SGreg Roach     * Get all the records in the cart.
8915a78cd34SGreg Roach     *
8925a78cd34SGreg Roach     * @param Tree $tree
8935a78cd34SGreg Roach     *
8945a78cd34SGreg Roach     * @return GedcomRecord[]
8955a78cd34SGreg Roach     */
896c1010edaSGreg Roach    private function allRecordsInCart(Tree $tree): array
897c1010edaSGreg Roach    {
8985a78cd34SGreg Roach        $cart = Session::get('cart', []);
8995a78cd34SGreg Roach
900aa6f03bbSGreg Roach        $xrefs = array_keys($cart[$tree->name()] ?? []);
9015a78cd34SGreg Roach
9025a78cd34SGreg Roach        // Fetch all the records in the cart.
90318d7a90dSGreg Roach        $records = array_map(function (string $xref) use ($tree): GedcomRecord {
9045a78cd34SGreg Roach            return GedcomRecord::getInstance($xref, $tree);
9055a78cd34SGreg Roach        }, $xrefs);
9065a78cd34SGreg Roach
9075a78cd34SGreg Roach        // Some records may have been deleted after they were added to the cart.
9085a78cd34SGreg Roach        $records = array_filter($records);
9095a78cd34SGreg Roach
9105a78cd34SGreg Roach        // Group and sort.
91118d7a90dSGreg Roach        uasort($records, function (GedcomRecord $x, GedcomRecord $y): int {
912c156e8f5SGreg Roach            return $x::RECORD_TYPE <=> $y::RECORD_TYPE ?: GedcomRecord::nameComparator()($x, $y);
9135a78cd34SGreg Roach        });
9145a78cd34SGreg Roach
9155a78cd34SGreg Roach        return $records;
9165a78cd34SGreg Roach    }
9175a78cd34SGreg Roach
9185a78cd34SGreg Roach    /**
9195a78cd34SGreg Roach     * Add a record (and direclty linked sources, notes, etc. to the cart.
9205a78cd34SGreg Roach     *
9215a78cd34SGreg Roach     * @param GedcomRecord $record
92218d7a90dSGreg Roach     *
92318d7a90dSGreg Roach     * @return void
9245a78cd34SGreg Roach     */
925c1010edaSGreg Roach    private function addRecordToCart(GedcomRecord $record)
926c1010edaSGreg Roach    {
9275a78cd34SGreg Roach        $cart = Session::get('cart', []);
9285a78cd34SGreg Roach
929f4afa648SGreg Roach        $tree_name = $record->tree()->name();
9305a78cd34SGreg Roach
9315a78cd34SGreg Roach        // Add this record
932c0935879SGreg Roach        $cart[$tree_name][$record->xref()] = true;
9335a78cd34SGreg Roach
9345a78cd34SGreg Roach        // Add directly linked media, notes, repositories and sources.
9358d0ebef0SGreg Roach        preg_match_all('/\n\d (?:OBJE|NOTE|SOUR|REPO) @(' . Gedcom::REGEX_XREF . ')@/', $record->gedcom(), $matches);
9365a78cd34SGreg Roach
9375a78cd34SGreg Roach        foreach ($matches[1] as $match) {
9385a78cd34SGreg Roach            $cart[$tree_name][$match] = true;
9395a78cd34SGreg Roach        }
9405a78cd34SGreg Roach
9415a78cd34SGreg Roach        Session::put('cart', $cart);
9425a78cd34SGreg Roach    }
9435a78cd34SGreg Roach
9445a78cd34SGreg Roach    /**
9455a78cd34SGreg Roach     * @param Tree $tree
9465a78cd34SGreg Roach     *
9475a78cd34SGreg Roach     * @return bool
9485a78cd34SGreg Roach     */
949c1010edaSGreg Roach    private function isCartEmpty(Tree $tree): bool
950c1010edaSGreg Roach    {
9515a78cd34SGreg Roach        $cart = Session::get('cart', []);
9525a78cd34SGreg Roach
953aa6f03bbSGreg Roach        return empty($cart[$tree->name()]);
9545a78cd34SGreg Roach    }
9558c2e8227SGreg Roach}
956