xref: /webtrees/app/Module/ClippingsCartModule.php (revision 55167344c2bae40e7bcf00769d5d0dae993dc5ec)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
215a78cd34SGreg Roachuse Fisharebest\Webtrees\Database;
220bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\FamilyNotFoundException;
230bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualNotFoundException;
240bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\MediaNotFoundException;
250bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\NoteNotFoundException;
260bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\RepositoryNotFoundException;
270bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\SourceNotFoundException;
280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Family;
295a78cd34SGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsExport;
305a78cd34SGreg Roachuse Fisharebest\Webtrees\Gedcom;
310e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
320e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
330e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
345a78cd34SGreg Roachuse Fisharebest\Webtrees\Media;
350e62c4b8SGreg Roachuse Fisharebest\Webtrees\Menu;
361eca16b3SGreg Roachuse Fisharebest\Webtrees\Module;
375a78cd34SGreg Roachuse Fisharebest\Webtrees\Note;
385a78cd34SGreg Roachuse Fisharebest\Webtrees\Repository;
390e62c4b8SGreg Roachuse Fisharebest\Webtrees\Session;
405a78cd34SGreg Roachuse Fisharebest\Webtrees\Source;
41aee13b6dSGreg Roachuse Fisharebest\Webtrees\Tree;
425a78cd34SGreg Roachuse Fisharebest\Webtrees\User;
435a78cd34SGreg Roachuse League\Flysystem\Filesystem;
445a78cd34SGreg Roachuse League\Flysystem\ZipArchive\ZipArchiveAdapter;
455a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\BinaryFileResponse;
465a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse;
475a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\Request;
485a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\Response;
495a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\ResponseHeaderBag;
505a78cd34SGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
518c2e8227SGreg Roach
528c2e8227SGreg Roach/**
538c2e8227SGreg Roach * Class ClippingsCartModule
548c2e8227SGreg Roach */
55c1010edaSGreg Roachclass ClippingsCartModule extends AbstractModule implements ModuleMenuInterface
56c1010edaSGreg Roach{
575a78cd34SGreg Roach    // Routes that have a record which can be added to the clipboard
58c1010edaSGreg Roach    const ROUTES_WITH_RECORDS = [
59c1010edaSGreg Roach        'family',
60c1010edaSGreg Roach        'individual',
61c1010edaSGreg Roach        'media',
62c1010edaSGreg Roach        'note',
63c1010edaSGreg Roach        'repository',
64c1010edaSGreg Roach        'source',
65c1010edaSGreg Roach    ];
665a78cd34SGreg Roach
678c2e8227SGreg Roach    /** {@inheritdoc} */
688f53f488SRico Sonntag    public function getTitle(): string
69c1010edaSGreg Roach    {
70bbb76c12SGreg Roach        /* I18N: Name of a module */
71bbb76c12SGreg Roach        return I18N::translate('Clippings cart');
728c2e8227SGreg Roach    }
738c2e8227SGreg Roach
748c2e8227SGreg Roach    /** {@inheritdoc} */
758f53f488SRico Sonntag    public function getDescription(): string
76c1010edaSGreg Roach    {
77bbb76c12SGreg Roach        /* I18N: Description of the “Clippings cart” module */
78bbb76c12SGreg Roach        return I18N::translate('Select records from your family tree and save them as a GEDCOM file.');
798c2e8227SGreg Roach    }
808c2e8227SGreg Roach
810ee13198SGreg Roach    /**
820ee13198SGreg Roach     * What is the default access level for this module?
830ee13198SGreg Roach     *
840ee13198SGreg Roach     * Some modules are aimed at admins or managers, and are not generally shown to users.
850ee13198SGreg Roach     *
860ee13198SGreg Roach     * @return int
870ee13198SGreg Roach     */
888f53f488SRico Sonntag    public function defaultAccessLevel(): int
89c1010edaSGreg Roach    {
904b9ff166SGreg Roach        return Auth::PRIV_USER;
918c2e8227SGreg Roach    }
928c2e8227SGreg Roach
9376692c8bSGreg Roach    /**
940ee13198SGreg Roach     * The user can re-order menus. Until they do, they are shown in this order.
950ee13198SGreg Roach     *
960ee13198SGreg Roach     * @return int
970ee13198SGreg Roach     */
988f53f488SRico Sonntag    public function defaultMenuOrder(): int
99c1010edaSGreg Roach    {
1008c2e8227SGreg Roach        return 20;
1018c2e8227SGreg Roach    }
1028c2e8227SGreg Roach
1030ee13198SGreg Roach    /**
1040ee13198SGreg Roach     * A menu, to be added to the main application menu.
1050ee13198SGreg Roach     *
106aee13b6dSGreg Roach     * @param Tree $tree
107aee13b6dSGreg Roach     *
1080ee13198SGreg Roach     * @return Menu|null
1090ee13198SGreg Roach     */
110c1010edaSGreg Roach    public function getMenu(Tree $tree)
111c1010edaSGreg Roach    {
1125a78cd34SGreg Roach        $request = Request::createFromGlobals();
1138c2e8227SGreg Roach
1145a78cd34SGreg Roach        $route = $request->get('route');
1155a78cd34SGreg Roach
1165a78cd34SGreg Roach        $submenus = [
117c1010edaSGreg Roach            new Menu($this->getTitle(), route('module', [
118c1010edaSGreg Roach                'module' => 'clippings',
119c1010edaSGreg Roach                'action' => 'Show',
120c1010edaSGreg Roach                'ged'    => $tree->getName(),
121c1010edaSGreg Roach            ]), 'menu-clippings-cart', ['rel' => 'nofollow']),
1225a78cd34SGreg Roach        ];
1235a78cd34SGreg Roach
1245a78cd34SGreg Roach        if (in_array($route, self::ROUTES_WITH_RECORDS)) {
1255a78cd34SGreg Roach            $xref      = $request->get('xref');
1265a78cd34SGreg Roach            $action    = 'Add' . ucfirst($route);
127c1010edaSGreg Roach            $add_route = route('module', [
128c1010edaSGreg Roach                'module' => 'clippings',
129c1010edaSGreg Roach                'action' => $action,
130c1010edaSGreg Roach                'xref'   => $xref,
131c1010edaSGreg Roach                'ged'    => $tree->getName(),
132c1010edaSGreg Roach            ]);
1335a78cd34SGreg Roach
13425b2dde3SGreg Roach            $submenus[] = new Menu(I18N::translate('Add to the clippings cart'), $add_route, 'menu-clippings-add', ['rel' => 'nofollow']);
1358c2e8227SGreg Roach        }
136cbc1590aSGreg Roach
1375a78cd34SGreg Roach        if (!$this->isCartEmpty($tree)) {
138c1010edaSGreg Roach            $submenus[] = new Menu(I18N::translate('Empty the clippings cart'), route('module', [
139c1010edaSGreg Roach                'module' => 'clippings',
140c1010edaSGreg Roach                'action' => 'Empty',
141c1010edaSGreg Roach                'ged'    => $tree->getName(),
142c1010edaSGreg Roach            ]), 'menu-clippings-empty', ['rel' => 'nofollow']);
143c1010edaSGreg Roach            $submenus[] = new Menu(I18N::translate('Download'), route('module', [
144c1010edaSGreg Roach                'module' => 'clippings',
145c1010edaSGreg Roach                'action' => 'DownloadForm',
146c1010edaSGreg Roach                'ged'    => $tree->getName(),
147c1010edaSGreg Roach            ]), 'menu-clippings-download', ['rel' => 'nofollow']);
1485a78cd34SGreg Roach        }
1495a78cd34SGreg Roach
15013abd6f3SGreg Roach        return new Menu($this->getTitle(), '#', 'menu-clippings', ['rel' => 'nofollow'], $submenus);
1518c2e8227SGreg Roach    }
1528c2e8227SGreg Roach
15376692c8bSGreg Roach    /**
1545a78cd34SGreg Roach     * @param Request $request
155b6db7c1fSGreg Roach     * @param Tree    $tree
15676692c8bSGreg Roach     *
1575a78cd34SGreg Roach     * @return BinaryFileResponse
15876692c8bSGreg Roach     */
159b6db7c1fSGreg Roach    public function getDownloadAction(Request $request, Tree $tree): BinaryFileResponse
160c1010edaSGreg Roach    {
1615a78cd34SGreg Roach        $this->checkModuleAccess($tree);
1628c2e8227SGreg Roach
1635a78cd34SGreg Roach        $privatize_export = $request->get('privatize_export');
1645a78cd34SGreg Roach        $convert          = (bool) $request->get('convert');
1658c2e8227SGreg Roach
16613abd6f3SGreg Roach        $cart = Session::get('cart', []);
1678c2e8227SGreg Roach
1685a78cd34SGreg Roach        $xrefs = array_keys($cart[$tree->getName()] ?? []);
1695a78cd34SGreg Roach
1705a78cd34SGreg Roach        // Create a new/empty .ZIP file
1715a78cd34SGreg Roach        $temp_zip_file  = tempnam(sys_get_temp_dir(), 'webtrees-zip-');
1725a78cd34SGreg Roach        $zip_filesystem = new Filesystem(new ZipArchiveAdapter($temp_zip_file));
1735a78cd34SGreg Roach
1745a78cd34SGreg Roach        // Media file prefix
1755a78cd34SGreg Roach        $path = $tree->getPreference('MEDIA_DIRECTORY');
1765a78cd34SGreg Roach
1775a78cd34SGreg Roach        // GEDCOM file header
1785a78cd34SGreg Roach        $filetext = FunctionsExport::gedcomHeader($tree);
1795a78cd34SGreg Roach
1805a78cd34SGreg Roach        // Include SUBM/SUBN records, if they exist
1815a78cd34SGreg Roach        $subn =
1825a78cd34SGreg Roach            Database::prepare("SELECT o_gedcom FROM `##other` WHERE o_type=? AND o_file=?")
183c1010edaSGreg Roach                ->execute([
184c1010edaSGreg Roach                    'SUBN',
185c1010edaSGreg Roach                    $tree->getName(),
186c1010edaSGreg Roach                ])
1875a78cd34SGreg Roach                ->fetchOne();
1885a78cd34SGreg Roach        if ($subn) {
1895a78cd34SGreg Roach            $filetext .= $subn . "\n";
190b6468bb9SGreg Roach        }
1915a78cd34SGreg Roach        $subm =
1925a78cd34SGreg Roach            Database::prepare("SELECT o_gedcom FROM `##other` WHERE o_type=? AND o_file=?")
193c1010edaSGreg Roach                ->execute([
194c1010edaSGreg Roach                    'SUBM',
195c1010edaSGreg Roach                    $tree->getName(),
196c1010edaSGreg Roach                ])
1975a78cd34SGreg Roach                ->fetchOne();
1985a78cd34SGreg Roach        if ($subm) {
1995a78cd34SGreg Roach            $filetext .= $subm . "\n";
2005a78cd34SGreg Roach        }
2015a78cd34SGreg Roach
2025a78cd34SGreg Roach        switch ($privatize_export) {
2035a78cd34SGreg Roach            case 'gedadmin':
2045a78cd34SGreg Roach                $access_level = Auth::PRIV_NONE;
2055a78cd34SGreg Roach                break;
2065a78cd34SGreg Roach            case 'user':
2075a78cd34SGreg Roach                $access_level = Auth::PRIV_USER;
2085a78cd34SGreg Roach                break;
2095a78cd34SGreg Roach            case 'visitor':
2105a78cd34SGreg Roach                $access_level = Auth::PRIV_PRIVATE;
2115a78cd34SGreg Roach                break;
2125a78cd34SGreg Roach            case 'none':
2135a78cd34SGreg Roach            default:
2145a78cd34SGreg Roach                $access_level = Auth::PRIV_HIDE;
2155a78cd34SGreg Roach                break;
2165a78cd34SGreg Roach        }
2175a78cd34SGreg Roach
2185a78cd34SGreg Roach        foreach ($xrefs as $xref) {
2195a78cd34SGreg Roach            $object = GedcomRecord::getInstance($xref, $tree);
2205a78cd34SGreg Roach            // The object may have been deleted since we added it to the cart....
2215a78cd34SGreg Roach            if ($object) {
2225a78cd34SGreg Roach                $record = $object->privatizeGedcom($access_level);
2235a78cd34SGreg Roach                // Remove links to objects that aren't in the cart
2245a78cd34SGreg Roach                preg_match_all('/\n1 ' . WT_REGEX_TAG . ' @(' . WT_REGEX_XREF . ')@(\n[2-9].*)*/', $record, $matches, PREG_SET_ORDER);
2255a78cd34SGreg Roach                foreach ($matches as $match) {
2265a78cd34SGreg Roach                    if (!array_key_exists($match[1], $xrefs)) {
2275a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2285a78cd34SGreg Roach                    }
2295a78cd34SGreg Roach                }
2305a78cd34SGreg Roach                preg_match_all('/\n2 ' . WT_REGEX_TAG . ' @(' . WT_REGEX_XREF . ')@(\n[3-9].*)*/', $record, $matches, PREG_SET_ORDER);
2315a78cd34SGreg Roach                foreach ($matches as $match) {
2325a78cd34SGreg Roach                    if (!array_key_exists($match[1], $xrefs)) {
2335a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2345a78cd34SGreg Roach                    }
2355a78cd34SGreg Roach                }
2365a78cd34SGreg Roach                preg_match_all('/\n3 ' . WT_REGEX_TAG . ' @(' . WT_REGEX_XREF . ')@(\n[4-9].*)*/', $record, $matches, PREG_SET_ORDER);
2375a78cd34SGreg Roach                foreach ($matches as $match) {
2385a78cd34SGreg Roach                    if (!array_key_exists($match[1], $xrefs)) {
2395a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2405a78cd34SGreg Roach                    }
2415a78cd34SGreg Roach                }
2425a78cd34SGreg Roach
2435a78cd34SGreg Roach                if ($convert) {
2445a78cd34SGreg Roach                    $record = utf8_decode($record);
2455a78cd34SGreg Roach                }
246*55167344SGreg Roach
247*55167344SGreg Roach                if  ($object instanceof Individual || $object instanceof Family) {
2485a78cd34SGreg Roach                    $filetext .= $record . "\n";
2495a78cd34SGreg Roach                    $filetext .= "1 SOUR @WEBTREES@\n";
2505a78cd34SGreg Roach                    $filetext .= '2 PAGE ' . WT_BASE_URL . $object->url() . "\n";
251*55167344SGreg Roach                } elseif ($object instanceof Source) {
2525a78cd34SGreg Roach                    $filetext .= $record . "\n";
2535a78cd34SGreg Roach                    $filetext .= '1 NOTE ' . WT_BASE_URL . $object->url() . "\n";
254*55167344SGreg Roach                } elseif ($object instanceof Media) {
255*55167344SGreg Roach                    // Add the media files to the archive
2565a78cd34SGreg Roach                    foreach ($object->mediaFiles() as $media_file) {
2575a78cd34SGreg Roach                        if (file_exists($media_file->getServerFilename())) {
2585a78cd34SGreg Roach                            $fp = fopen($media_file->getServerFilename(), 'r');
2595a78cd34SGreg Roach                            $zip_filesystem->writeStream($path . $media_file->filename(), $fp);
2605a78cd34SGreg Roach                            fclose($fp);
2615a78cd34SGreg Roach                        }
2625a78cd34SGreg Roach                    }
2635a78cd34SGreg Roach                    $filetext .= $record . "\n";
264*55167344SGreg Roach                } else {
2655a78cd34SGreg Roach                    $filetext .= $record . "\n";
2668c2e8227SGreg Roach                }
2678c2e8227SGreg Roach            }
2688c2e8227SGreg Roach        }
2698c2e8227SGreg Roach
2705a78cd34SGreg Roach        // Create a source, to indicate the source of the data.
2715a78cd34SGreg Roach        $filetext .= "0 @WEBTREES@ SOUR\n1 TITL " . WT_BASE_URL . "\n";
2727fcd9838SGreg Roach        $author = User::find((int) $tree->getPreference('CONTACT_USER_ID'));
2735a78cd34SGreg Roach        if ($author !== null) {
2745a78cd34SGreg Roach            $filetext .= '1 AUTH ' . $author->getRealName() . "\n";
2755a78cd34SGreg Roach        }
2765a78cd34SGreg Roach        $filetext .= "0 TRLR\n";
2775a78cd34SGreg Roach
2785a78cd34SGreg Roach        // Make sure the preferred line endings are used
2795a78cd34SGreg Roach        $filetext = preg_replace("/[\r\n]+/", Gedcom::EOL, $filetext);
2805a78cd34SGreg Roach
281*55167344SGreg Roach        if ($convert) {
2825a78cd34SGreg Roach            $filetext = str_replace('UTF-8', 'ANSI', $filetext);
2835a78cd34SGreg Roach            $filetext = utf8_decode($filetext);
2848c2e8227SGreg Roach        }
285cbc1590aSGreg Roach
2865a78cd34SGreg Roach        // Finally add the GEDCOM file to the .ZIP file.
2875a78cd34SGreg Roach        $zip_filesystem->write('clippings.ged', $filetext);
2885a78cd34SGreg Roach
2895a78cd34SGreg Roach        // Need to force-close the filesystem
2905a78cd34SGreg Roach        $zip_filesystem = null;
2915a78cd34SGreg Roach
2925a78cd34SGreg Roach        $response = new BinaryFileResponse($temp_zip_file);
2935a78cd34SGreg Roach        $response->deleteFileAfterSend(true);
2945a78cd34SGreg Roach
2955a78cd34SGreg Roach        $response->headers->set('Content-Type', 'application/zip');
2965a78cd34SGreg Roach        $response->setContentDisposition(
2975a78cd34SGreg Roach            ResponseHeaderBag::DISPOSITION_ATTACHMENT,
2985a78cd34SGreg Roach            'clippings.zip'
2995a78cd34SGreg Roach        );
3005a78cd34SGreg Roach
3015a78cd34SGreg Roach        return $response;
3028c2e8227SGreg Roach    }
3038c2e8227SGreg Roach
3048c2e8227SGreg Roach    /**
305b6db7c1fSGreg Roach     * @param Tree $tree
306b6db7c1fSGreg Roach     * @param User $user
30776692c8bSGreg Roach     *
3085a78cd34SGreg Roach     * @return Response
3098c2e8227SGreg Roach     */
310b6db7c1fSGreg Roach    public function getDownloadFormAction(Tree $tree, User $user): Response
311c1010edaSGreg Roach    {
3125a78cd34SGreg Roach        $title = I18N::translate('Family tree clippings cart') . ' — ' . I18N::translate('Download');
3138c2e8227SGreg Roach
3145a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/download', [
3155a78cd34SGreg Roach            'is_manager' => Auth::isManager($tree, $user),
3165a78cd34SGreg Roach            'is_member'  => Auth::isMember($tree, $user),
3175a78cd34SGreg Roach            'title'      => $title,
3185a78cd34SGreg Roach        ]);
3198c2e8227SGreg Roach    }
3208c2e8227SGreg Roach
3215a78cd34SGreg Roach    /**
322b6db7c1fSGreg Roach     * @param Tree $tree
3235a78cd34SGreg Roach     *
3245a78cd34SGreg Roach     * @return RedirectResponse
3255a78cd34SGreg Roach     */
326b6db7c1fSGreg Roach    public function getEmptyAction(Tree $tree): RedirectResponse
327c1010edaSGreg Roach    {
3285a78cd34SGreg Roach        $cart                   = Session::get('cart', []);
3295a78cd34SGreg Roach        $cart[$tree->getName()] = [];
3305a78cd34SGreg Roach        Session::put('cart', $cart);
3318c2e8227SGreg Roach
332c1010edaSGreg Roach        $url = route('module', [
333c1010edaSGreg Roach            'module' => 'clippings',
334c1010edaSGreg Roach            'action' => 'Show',
335c1010edaSGreg Roach            'ged'    => $tree->getName(),
336c1010edaSGreg Roach        ]);
3375a78cd34SGreg Roach
3385a78cd34SGreg Roach        return new RedirectResponse($url);
3395a78cd34SGreg Roach    }
3405a78cd34SGreg Roach
3415a78cd34SGreg Roach    /**
3425a78cd34SGreg Roach     * @param Request $request
343b6db7c1fSGreg Roach     * @param Tree    $tree
3445a78cd34SGreg Roach     *
3455a78cd34SGreg Roach     * @return RedirectResponse
3465a78cd34SGreg Roach     */
347b6db7c1fSGreg Roach    public function postRemoveAction(Request $request, Tree $tree): RedirectResponse
348c1010edaSGreg Roach    {
3495a78cd34SGreg Roach        $xref = $request->get('xref');
3505a78cd34SGreg Roach
3515a78cd34SGreg Roach        $cart = Session::get('cart', []);
3525a78cd34SGreg Roach        unset($cart[$tree->getName()][$xref]);
3535a78cd34SGreg Roach        Session::put('cart', $cart);
3545a78cd34SGreg Roach
355c1010edaSGreg Roach        $url = route('module', [
356c1010edaSGreg Roach            'module' => 'clippings',
357c1010edaSGreg Roach            'action' => 'Show',
358c1010edaSGreg Roach            'ged'    => $tree->getName(),
359c1010edaSGreg Roach        ]);
3605a78cd34SGreg Roach
3615a78cd34SGreg Roach        return new RedirectResponse($url);
3625a78cd34SGreg Roach    }
3635a78cd34SGreg Roach
3645a78cd34SGreg Roach    /**
365b6db7c1fSGreg Roach     * @param Tree $tree
3665a78cd34SGreg Roach     *
3675a78cd34SGreg Roach     * @return Response
3685a78cd34SGreg Roach     */
369b6db7c1fSGreg Roach    public function getShowAction(Tree $tree): Response
370c1010edaSGreg Roach    {
3715a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/show', [
3725a78cd34SGreg Roach            'records' => $this->allRecordsInCart($tree),
3735a78cd34SGreg Roach            'title'   => I18N::translate('Family tree clippings cart'),
3745a78cd34SGreg Roach            'tree'    => $tree,
3755a78cd34SGreg Roach        ]);
3765a78cd34SGreg Roach    }
3775a78cd34SGreg Roach
3785a78cd34SGreg Roach    /**
3795a78cd34SGreg Roach     * @param Request $request
380b6db7c1fSGreg Roach     * @param Tree    $tree
3815a78cd34SGreg Roach     *
3825a78cd34SGreg Roach     * @return Response
3835a78cd34SGreg Roach     */
384b6db7c1fSGreg Roach    public function getAddFamilyAction(Request $request, Tree $tree): Response
385c1010edaSGreg Roach    {
3865a78cd34SGreg Roach        $xref = $request->get('xref');
3875a78cd34SGreg Roach
3885a78cd34SGreg Roach        $family = Family::getInstance($xref, $tree);
3895a78cd34SGreg Roach
3905a78cd34SGreg Roach        if ($family === null) {
39159f2f229SGreg Roach            throw new FamilyNotFoundException();
3925a78cd34SGreg Roach        }
3935a78cd34SGreg Roach
3945a78cd34SGreg Roach        $options = $this->familyOptions($family);
3955a78cd34SGreg Roach
3965a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $family->getFullName());
3975a78cd34SGreg Roach
3985a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
3995a78cd34SGreg Roach            'options' => $options,
4005a78cd34SGreg Roach            'default' => key($options),
4015a78cd34SGreg Roach            'record'  => $family,
4025a78cd34SGreg Roach            'title'   => $title,
4035a78cd34SGreg Roach            'tree'    => $tree,
4045a78cd34SGreg Roach        ]);
4055a78cd34SGreg Roach    }
4065a78cd34SGreg Roach
4075a78cd34SGreg Roach    /**
4085a78cd34SGreg Roach     * @param Family $family
4095a78cd34SGreg Roach     *
4105a78cd34SGreg Roach     * @return string[]
4115a78cd34SGreg Roach     */
412c1010edaSGreg Roach    private function familyOptions(Family $family): array
413c1010edaSGreg Roach    {
4145a78cd34SGreg Roach        $name = strip_tags($family->getFullName());
4155a78cd34SGreg Roach
4165a78cd34SGreg Roach        return [
4175a78cd34SGreg Roach            'parents'     => $name,
418bbb76c12SGreg Roach            /* I18N: %s is a family (husband + wife) */
419bbb76c12SGreg Roach            'members'     => I18N::translate('%s and their children', $name),
420bbb76c12SGreg Roach            /* I18N: %s is a family (husband + wife) */
421bbb76c12SGreg Roach            'descendants' => I18N::translate('%s and their descendants', $name),
4225a78cd34SGreg Roach        ];
4235a78cd34SGreg Roach    }
4245a78cd34SGreg Roach
4255a78cd34SGreg Roach    /**
4265a78cd34SGreg Roach     * @param Request $request
427b6db7c1fSGreg Roach     * @param Tree    $tree
4285a78cd34SGreg Roach     *
4295a78cd34SGreg Roach     * @return RedirectResponse
4305a78cd34SGreg Roach     */
431b6db7c1fSGreg Roach    public function postAddFamilyAction(Request $request, Tree $tree): RedirectResponse
432c1010edaSGreg Roach    {
4335a78cd34SGreg Roach        $xref   = $request->get('xref');
4345a78cd34SGreg Roach        $option = $request->get('option');
4355a78cd34SGreg Roach
4365a78cd34SGreg Roach        $family = Family::getInstance($xref, $tree);
4375a78cd34SGreg Roach
4385a78cd34SGreg Roach        if ($family === null) {
43959f2f229SGreg Roach            throw new FamilyNotFoundException();
4405a78cd34SGreg Roach        }
4415a78cd34SGreg Roach
4425a78cd34SGreg Roach        switch ($option) {
4435a78cd34SGreg Roach            case 'parents':
4445a78cd34SGreg Roach                $this->addFamilyToCart($family);
4455a78cd34SGreg Roach                break;
4465a78cd34SGreg Roach
4475a78cd34SGreg Roach            case 'members':
4485a78cd34SGreg Roach                $this->addFamilyAndChildrenToCart($family);
4495a78cd34SGreg Roach                break;
4505a78cd34SGreg Roach
4515a78cd34SGreg Roach            case 'descendants':
4525a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($family);
4535a78cd34SGreg Roach                break;
4545a78cd34SGreg Roach        }
4555a78cd34SGreg Roach
4565a78cd34SGreg Roach        return new RedirectResponse($family->url());
4575a78cd34SGreg Roach    }
4585a78cd34SGreg Roach
4595a78cd34SGreg Roach    /**
4605a78cd34SGreg Roach     * @param Family $family
46118d7a90dSGreg Roach     *
46218d7a90dSGreg Roach     * @return void
4635a78cd34SGreg Roach     */
464c1010edaSGreg Roach    private function addFamilyToCart(Family $family)
465c1010edaSGreg Roach    {
4665a78cd34SGreg Roach        $this->addRecordToCart($family);
4675a78cd34SGreg Roach
4685a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
4695a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4705a78cd34SGreg Roach        }
4715a78cd34SGreg Roach    }
4725a78cd34SGreg Roach
4735a78cd34SGreg Roach    /**
4745a78cd34SGreg Roach     * @param Family $family
47518d7a90dSGreg Roach     *
47618d7a90dSGreg Roach     * @return void
4775a78cd34SGreg Roach     */
478c1010edaSGreg Roach    private function addFamilyAndChildrenToCart(Family $family)
479c1010edaSGreg Roach    {
4805a78cd34SGreg Roach        $this->addRecordToCart($family);
4815a78cd34SGreg Roach
4825a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
4835a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4845a78cd34SGreg Roach        }
4855a78cd34SGreg Roach        foreach ($family->getChildren() as $child) {
4865a78cd34SGreg Roach            $this->addRecordToCart($child);
4875a78cd34SGreg Roach        }
4885a78cd34SGreg Roach    }
4895a78cd34SGreg Roach
4905a78cd34SGreg Roach    /**
4915a78cd34SGreg Roach     * @param Family $family
49218d7a90dSGreg Roach     *
49318d7a90dSGreg Roach     * @return void
4945a78cd34SGreg Roach     */
495c1010edaSGreg Roach    private function addFamilyAndDescendantsToCart(Family $family)
496c1010edaSGreg Roach    {
4975a78cd34SGreg Roach        $this->addRecordToCart($family);
4985a78cd34SGreg Roach
4995a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
5005a78cd34SGreg Roach            $this->addRecordToCart($spouse);
5015a78cd34SGreg Roach        }
5025a78cd34SGreg Roach        foreach ($family->getChildren() as $child) {
5035a78cd34SGreg Roach            $this->addRecordToCart($child);
5045a78cd34SGreg Roach            foreach ($child->getSpouseFamilies() as $child_family) {
5055a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($child_family);
5065a78cd34SGreg Roach            }
5075a78cd34SGreg Roach        }
5085a78cd34SGreg Roach    }
5095a78cd34SGreg Roach
5105a78cd34SGreg Roach    /**
5115a78cd34SGreg Roach     * @param Request $request
512b6db7c1fSGreg Roach     * @param Tree    $tree
5135a78cd34SGreg Roach     *
5145a78cd34SGreg Roach     * @return Response
5155a78cd34SGreg Roach     */
516b6db7c1fSGreg Roach    public function getAddIndividualAction(Request $request, Tree $tree): Response
517c1010edaSGreg Roach    {
5185a78cd34SGreg Roach        $xref = $request->get('xref');
5195a78cd34SGreg Roach
5205a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
5215a78cd34SGreg Roach
5225a78cd34SGreg Roach        if ($individual === null) {
52359f2f229SGreg Roach            throw new IndividualNotFoundException();
5245a78cd34SGreg Roach        }
5255a78cd34SGreg Roach
5265a78cd34SGreg Roach        $options = $this->individualOptions($individual);
5275a78cd34SGreg Roach
5285a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $individual->getFullName());
5295a78cd34SGreg Roach
5305a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
5315a78cd34SGreg Roach            'options' => $options,
5325a78cd34SGreg Roach            'default' => key($options),
5335a78cd34SGreg Roach            'record'  => $individual,
5345a78cd34SGreg Roach            'title'   => $title,
5355a78cd34SGreg Roach            'tree'    => $tree,
5365a78cd34SGreg Roach        ]);
5375a78cd34SGreg Roach    }
5385a78cd34SGreg Roach
5395a78cd34SGreg Roach    /**
5405a78cd34SGreg Roach     * @param Individual $individual
5415a78cd34SGreg Roach     *
5425a78cd34SGreg Roach     * @return string[]
5435a78cd34SGreg Roach     */
544c1010edaSGreg Roach    private function individualOptions(Individual $individual): array
545c1010edaSGreg Roach    {
5465a78cd34SGreg Roach        $name = strip_tags($individual->getFullName());
5475a78cd34SGreg Roach
5485a78cd34SGreg Roach        if ($individual->getSex() === 'F') {
5495a78cd34SGreg Roach            return [
5505a78cd34SGreg Roach                'self'              => $name,
5515a78cd34SGreg Roach                'parents'           => I18N::translate('%s, her parents and siblings', $name),
5525a78cd34SGreg Roach                'spouses'           => I18N::translate('%s, her spouses and children', $name),
5535a78cd34SGreg Roach                'ancestors'         => I18N::translate('%s and her ancestors', $name),
5545a78cd34SGreg Roach                'ancestor_families' => I18N::translate('%s, her ancestors and their families', $name),
5555a78cd34SGreg Roach                'descendants'       => I18N::translate('%s, her spouses and descendants', $name),
5565a78cd34SGreg Roach            ];
557b2ce94c6SRico Sonntag        }
558b2ce94c6SRico Sonntag
5595a78cd34SGreg Roach        return [
5605a78cd34SGreg Roach            'self'              => $name,
5615a78cd34SGreg Roach            'parents'           => I18N::translate('%s, his parents and siblings', $name),
5625a78cd34SGreg Roach            'spouses'           => I18N::translate('%s, his spouses and children', $name),
5635a78cd34SGreg Roach            'ancestors'         => I18N::translate('%s and his ancestors', $name),
5645a78cd34SGreg Roach            'ancestor_families' => I18N::translate('%s, his ancestors and their families', $name),
5655a78cd34SGreg Roach            'descendants'       => I18N::translate('%s, his spouses and descendants', $name),
5665a78cd34SGreg Roach        ];
5675a78cd34SGreg Roach    }
5685a78cd34SGreg Roach
5695a78cd34SGreg Roach    /**
5705a78cd34SGreg Roach     * @param Request $request
571b6db7c1fSGreg Roach     * @param Tree    $tree
5725a78cd34SGreg Roach     *
5735a78cd34SGreg Roach     * @return RedirectResponse
5745a78cd34SGreg Roach     */
575b6db7c1fSGreg Roach    public function postAddIndividualAction(Request $request, Tree $tree): RedirectResponse
576c1010edaSGreg Roach    {
5775a78cd34SGreg Roach        $xref   = $request->get('xref');
5785a78cd34SGreg Roach        $option = $request->get('option');
5795a78cd34SGreg Roach
5805a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
5815a78cd34SGreg Roach
5825a78cd34SGreg Roach        if ($individual === null) {
58359f2f229SGreg Roach            throw new IndividualNotFoundException();
5845a78cd34SGreg Roach        }
5855a78cd34SGreg Roach
5865a78cd34SGreg Roach        switch ($option) {
5875a78cd34SGreg Roach            case 'self':
5885a78cd34SGreg Roach                $this->addRecordToCart($individual);
5895a78cd34SGreg Roach                break;
5905a78cd34SGreg Roach
5915a78cd34SGreg Roach            case 'parents':
5925a78cd34SGreg Roach                foreach ($individual->getChildFamilies() as $family) {
5935a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
5945a78cd34SGreg Roach                }
5955a78cd34SGreg Roach                break;
5965a78cd34SGreg Roach
5975a78cd34SGreg Roach            case 'spouses':
5985a78cd34SGreg Roach                foreach ($individual->getSpouseFamilies() as $family) {
5995a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
6005a78cd34SGreg Roach                }
6015a78cd34SGreg Roach                break;
6025a78cd34SGreg Roach
6035a78cd34SGreg Roach            case 'ancestors':
6045a78cd34SGreg Roach                $this->addAncestorsToCart($individual);
6055a78cd34SGreg Roach                break;
6065a78cd34SGreg Roach
6075a78cd34SGreg Roach            case 'ancestor_families':
6085a78cd34SGreg Roach                $this->addAncestorFamiliesToCart($individual);
6095a78cd34SGreg Roach                break;
6105a78cd34SGreg Roach
6115a78cd34SGreg Roach            case 'descendants':
6125a78cd34SGreg Roach                foreach ($individual->getSpouseFamilies() as $family) {
6135a78cd34SGreg Roach                    $this->addFamilyAndDescendantsToCart($family);
6145a78cd34SGreg Roach                }
6155a78cd34SGreg Roach                break;
6165a78cd34SGreg Roach        }
6175a78cd34SGreg Roach
6185a78cd34SGreg Roach        return new RedirectResponse($individual->url());
6195a78cd34SGreg Roach    }
6205a78cd34SGreg Roach
6215a78cd34SGreg Roach    /**
6225a78cd34SGreg Roach     * @param Individual $individual
62318d7a90dSGreg Roach     *
62418d7a90dSGreg Roach     * @return void
6255a78cd34SGreg Roach     */
626c1010edaSGreg Roach    private function addAncestorsToCart(Individual $individual)
627c1010edaSGreg Roach    {
6285a78cd34SGreg Roach        $this->addRecordToCart($individual);
6295a78cd34SGreg Roach
6305a78cd34SGreg Roach        foreach ($individual->getChildFamilies() as $family) {
6315a78cd34SGreg Roach            foreach ($family->getSpouses() as $parent) {
6325a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
6335a78cd34SGreg Roach            }
6345a78cd34SGreg Roach        }
6355a78cd34SGreg Roach    }
6365a78cd34SGreg Roach
6375a78cd34SGreg Roach    /**
6385a78cd34SGreg Roach     * @param Individual $individual
63918d7a90dSGreg Roach     *
64018d7a90dSGreg Roach     * @return void
6415a78cd34SGreg Roach     */
642c1010edaSGreg Roach    private function addAncestorFamiliesToCart(Individual $individual)
643c1010edaSGreg Roach    {
6445a78cd34SGreg Roach        foreach ($individual->getChildFamilies() as $family) {
6455a78cd34SGreg Roach            $this->addFamilyAndChildrenToCart($family);
6465a78cd34SGreg Roach            foreach ($family->getSpouses() as $parent) {
6475a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
6485a78cd34SGreg Roach            }
6495a78cd34SGreg Roach        }
6505a78cd34SGreg Roach    }
6515a78cd34SGreg Roach
6525a78cd34SGreg Roach    /**
6535a78cd34SGreg Roach     * @param Request $request
654b6db7c1fSGreg Roach     * @param Tree    $tree
6555a78cd34SGreg Roach     *
6565a78cd34SGreg Roach     * @return Response
6575a78cd34SGreg Roach     */
658b6db7c1fSGreg Roach    public function getAddMediaAction(Request $request, Tree $tree): Response
659c1010edaSGreg Roach    {
6605a78cd34SGreg Roach        $xref = $request->get('xref');
6615a78cd34SGreg Roach
6625a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
6635a78cd34SGreg Roach
6645a78cd34SGreg Roach        if ($media === null) {
66559f2f229SGreg Roach            throw new MediaNotFoundException();
6665a78cd34SGreg Roach        }
6675a78cd34SGreg Roach
6685a78cd34SGreg Roach        $options = $this->mediaOptions($media);
6695a78cd34SGreg Roach
6705a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $media->getFullName());
6715a78cd34SGreg Roach
6725a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
6735a78cd34SGreg Roach            'options' => $options,
6745a78cd34SGreg Roach            'default' => key($options),
6755a78cd34SGreg Roach            'record'  => $media,
6765a78cd34SGreg Roach            'title'   => $title,
6775a78cd34SGreg Roach            'tree'    => $tree,
6785a78cd34SGreg Roach        ]);
6795a78cd34SGreg Roach    }
6805a78cd34SGreg Roach
6815a78cd34SGreg Roach    /**
6825a78cd34SGreg Roach     * @param Media $media
6835a78cd34SGreg Roach     *
6845a78cd34SGreg Roach     * @return string[]
6855a78cd34SGreg Roach     */
686c1010edaSGreg Roach    private function mediaOptions(Media $media): array
687c1010edaSGreg Roach    {
6885a78cd34SGreg Roach        $name = strip_tags($media->getFullName());
6895a78cd34SGreg Roach
6905a78cd34SGreg Roach        return [
6915a78cd34SGreg Roach            'self' => $name,
6925a78cd34SGreg Roach        ];
6935a78cd34SGreg Roach    }
6945a78cd34SGreg Roach
6955a78cd34SGreg Roach    /**
6965a78cd34SGreg Roach     * @param Request $request
697b6db7c1fSGreg Roach     * @param Tree    $tree
6985a78cd34SGreg Roach     *
6995a78cd34SGreg Roach     * @return RedirectResponse
7005a78cd34SGreg Roach     */
701b6db7c1fSGreg Roach    public function postAddMediaAction(Request $request, Tree $tree): RedirectResponse
702c1010edaSGreg Roach    {
7035a78cd34SGreg Roach        $xref = $request->get('xref');
7045a78cd34SGreg Roach
7055a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
7065a78cd34SGreg Roach
7075a78cd34SGreg Roach        if ($media === null) {
70859f2f229SGreg Roach            throw new MediaNotFoundException();
7095a78cd34SGreg Roach        }
7105a78cd34SGreg Roach
7115a78cd34SGreg Roach        $this->addRecordToCart($media);
7125a78cd34SGreg Roach
7135a78cd34SGreg Roach        return new RedirectResponse($media->url());
7145a78cd34SGreg Roach    }
7155a78cd34SGreg Roach
7165a78cd34SGreg Roach    /**
7175a78cd34SGreg Roach     * @param Request $request
718b6db7c1fSGreg Roach     * @param Tree    $tree
7195a78cd34SGreg Roach     *
7205a78cd34SGreg Roach     * @return Response
7215a78cd34SGreg Roach     */
722b6db7c1fSGreg Roach    public function getAddNoteAction(Request $request, Tree $tree): Response
723c1010edaSGreg Roach    {
7245a78cd34SGreg Roach        $xref = $request->get('xref');
7255a78cd34SGreg Roach
7265a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
7275a78cd34SGreg Roach
7285a78cd34SGreg Roach        if ($note === null) {
72959f2f229SGreg Roach            throw new NoteNotFoundException();
7305a78cd34SGreg Roach        }
7315a78cd34SGreg Roach
7325a78cd34SGreg Roach        $options = $this->noteOptions($note);
7335a78cd34SGreg Roach
7345a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $note->getFullName());
7355a78cd34SGreg Roach
7365a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
7375a78cd34SGreg Roach            'options' => $options,
7385a78cd34SGreg Roach            'default' => key($options),
7395a78cd34SGreg Roach            'record'  => $note,
7405a78cd34SGreg Roach            'title'   => $title,
7415a78cd34SGreg Roach            'tree'    => $tree,
7425a78cd34SGreg Roach        ]);
7435a78cd34SGreg Roach    }
7445a78cd34SGreg Roach
7455a78cd34SGreg Roach    /**
7465a78cd34SGreg Roach     * @param Note $note
7475a78cd34SGreg Roach     *
7485a78cd34SGreg Roach     * @return string[]
7495a78cd34SGreg Roach     */
750c1010edaSGreg Roach    private function noteOptions(Note $note): array
751c1010edaSGreg Roach    {
7525a78cd34SGreg Roach        $name = strip_tags($note->getFullName());
7535a78cd34SGreg Roach
7545a78cd34SGreg Roach        return [
7555a78cd34SGreg Roach            'self' => $name,
7565a78cd34SGreg Roach        ];
7575a78cd34SGreg Roach    }
7585a78cd34SGreg Roach
7595a78cd34SGreg Roach    /**
7605a78cd34SGreg Roach     * @param Request $request
761b6db7c1fSGreg Roach     * @param Tree    $tree
7625a78cd34SGreg Roach     *
7635a78cd34SGreg Roach     * @return RedirectResponse
7645a78cd34SGreg Roach     */
765b6db7c1fSGreg Roach    public function postAddNoteAction(Request $request, Tree $tree): RedirectResponse
766c1010edaSGreg Roach    {
7675a78cd34SGreg Roach        $xref = $request->get('xref');
7685a78cd34SGreg Roach
7695a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
7705a78cd34SGreg Roach
7715a78cd34SGreg Roach        if ($note === null) {
77259f2f229SGreg Roach            throw new NoteNotFoundException();
7735a78cd34SGreg Roach        }
7745a78cd34SGreg Roach
7755a78cd34SGreg Roach        $this->addRecordToCart($note);
7765a78cd34SGreg Roach
7775a78cd34SGreg Roach        return new RedirectResponse($note->url());
7785a78cd34SGreg Roach    }
7795a78cd34SGreg Roach
7805a78cd34SGreg Roach    /**
7815a78cd34SGreg Roach     * @param Request $request
782b6db7c1fSGreg Roach     * @param Tree    $tree
7835a78cd34SGreg Roach     *
7845a78cd34SGreg Roach     * @return Response
7855a78cd34SGreg Roach     */
786b6db7c1fSGreg Roach    public function getAddRepositoryAction(Request $request, Tree $tree): Response
787c1010edaSGreg Roach    {
7885a78cd34SGreg Roach        $xref = $request->get('xref');
7895a78cd34SGreg Roach
7905a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
7915a78cd34SGreg Roach
7925a78cd34SGreg Roach        if ($repository === null) {
79359f2f229SGreg Roach            throw new RepositoryNotFoundException();
7945a78cd34SGreg Roach        }
7955a78cd34SGreg Roach
7965a78cd34SGreg Roach        $options = $this->repositoryOptions($repository);
7975a78cd34SGreg Roach
7985a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $repository->getFullName());
7995a78cd34SGreg Roach
8005a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
8015a78cd34SGreg Roach            'options' => $options,
8025a78cd34SGreg Roach            'default' => key($options),
8035a78cd34SGreg Roach            'record'  => $repository,
8045a78cd34SGreg Roach            'title'   => $title,
8055a78cd34SGreg Roach            'tree'    => $tree,
8065a78cd34SGreg Roach        ]);
8075a78cd34SGreg Roach    }
8085a78cd34SGreg Roach
8095a78cd34SGreg Roach    /**
8105a78cd34SGreg Roach     * @param Repository $repository
8115a78cd34SGreg Roach     *
8125a78cd34SGreg Roach     * @return string[]
8135a78cd34SGreg Roach     */
814c1010edaSGreg Roach    private function repositoryOptions(Repository $repository): array
815c1010edaSGreg Roach    {
8165a78cd34SGreg Roach        $name = strip_tags($repository->getFullName());
8175a78cd34SGreg Roach
8185a78cd34SGreg Roach        return [
8195a78cd34SGreg Roach            'self' => $name,
8205a78cd34SGreg Roach        ];
8215a78cd34SGreg Roach    }
8225a78cd34SGreg Roach
8235a78cd34SGreg Roach    /**
8245a78cd34SGreg Roach     * @param Request $request
825b6db7c1fSGreg Roach     * @param Tree    $tree
8265a78cd34SGreg Roach     *
8275a78cd34SGreg Roach     * @return RedirectResponse
8285a78cd34SGreg Roach     */
829b6db7c1fSGreg Roach    public function postAddRepositoryAction(Request $request, Tree $tree): RedirectResponse
830c1010edaSGreg Roach    {
8315a78cd34SGreg Roach        $xref = $request->get('xref');
8325a78cd34SGreg Roach
8335a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
8345a78cd34SGreg Roach
8355a78cd34SGreg Roach        if ($repository === null) {
83659f2f229SGreg Roach            throw new RepositoryNotFoundException();
8375a78cd34SGreg Roach        }
8385a78cd34SGreg Roach
8395a78cd34SGreg Roach        $this->addRecordToCart($repository);
8405a78cd34SGreg Roach
8415a78cd34SGreg Roach        return new RedirectResponse($repository->url());
8425a78cd34SGreg Roach    }
8435a78cd34SGreg Roach
8445a78cd34SGreg Roach    /**
8455a78cd34SGreg Roach     * @param Request $request
846b6db7c1fSGreg Roach     * @param Tree    $tree
8475a78cd34SGreg Roach     *
8485a78cd34SGreg Roach     * @return Response
8495a78cd34SGreg Roach     */
850b6db7c1fSGreg Roach    public function getAddSourceAction(Request $request, Tree $tree): Response
851c1010edaSGreg Roach    {
8525a78cd34SGreg Roach        $xref = $request->get('xref');
8535a78cd34SGreg Roach
8545a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
8555a78cd34SGreg Roach
8565a78cd34SGreg Roach        if ($source === null) {
85759f2f229SGreg Roach            throw new SourceNotFoundException();
8585a78cd34SGreg Roach        }
8595a78cd34SGreg Roach
8605a78cd34SGreg Roach        $options = $this->sourceOptions($source);
8615a78cd34SGreg Roach
8625a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $source->getFullName());
8635a78cd34SGreg Roach
8645a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
8655a78cd34SGreg Roach            'options' => $options,
8665a78cd34SGreg Roach            'default' => key($options),
8675a78cd34SGreg Roach            'record'  => $source,
8685a78cd34SGreg Roach            'title'   => $title,
8695a78cd34SGreg Roach            'tree'    => $tree,
8705a78cd34SGreg Roach        ]);
8715a78cd34SGreg Roach    }
8725a78cd34SGreg Roach
8735a78cd34SGreg Roach    /**
8745a78cd34SGreg Roach     * @param Source $source
8755a78cd34SGreg Roach     *
8765a78cd34SGreg Roach     * @return string[]
8775a78cd34SGreg Roach     */
878c1010edaSGreg Roach    private function sourceOptions(Source $source): array
879c1010edaSGreg Roach    {
8805a78cd34SGreg Roach        $name = strip_tags($source->getFullName());
8815a78cd34SGreg Roach
8825a78cd34SGreg Roach        return [
8835a78cd34SGreg Roach            'only'   => strip_tags($source->getFullName()),
8845a78cd34SGreg Roach            'linked' => I18N::translate('%s and the individuals that reference it.', $name),
8855a78cd34SGreg Roach        ];
8865a78cd34SGreg Roach    }
8875a78cd34SGreg Roach
8885a78cd34SGreg Roach    /**
8895a78cd34SGreg Roach     * @param Request $request
890b6db7c1fSGreg Roach     * @param Tree    $tree
8915a78cd34SGreg Roach     *
8925a78cd34SGreg Roach     * @return RedirectResponse
8935a78cd34SGreg Roach     */
894b6db7c1fSGreg Roach    public function postAddSourceAction(Request $request, Tree $tree): RedirectResponse
895c1010edaSGreg Roach    {
8965a78cd34SGreg Roach        $xref   = $request->get('xref');
8975a78cd34SGreg Roach        $option = $request->get('option');
8985a78cd34SGreg Roach
8995a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
9005a78cd34SGreg Roach
9015a78cd34SGreg Roach        if ($source === null) {
90259f2f229SGreg Roach            throw new SourceNotFoundException();
9035a78cd34SGreg Roach        }
9045a78cd34SGreg Roach
9055a78cd34SGreg Roach        $this->addRecordToCart($source);
9065a78cd34SGreg Roach
9075a78cd34SGreg Roach        if ($option === 'linked') {
9085a78cd34SGreg Roach            foreach ($source->linkedIndividuals('SOUR') as $individual) {
9095a78cd34SGreg Roach                $this->addRecordToCart($individual);
9105a78cd34SGreg Roach            }
9115a78cd34SGreg Roach            foreach ($source->linkedFamilies('SOUR') as $family) {
9125a78cd34SGreg Roach                $this->addRecordToCart($family);
9135a78cd34SGreg Roach            }
9145a78cd34SGreg Roach        }
9155a78cd34SGreg Roach
9165a78cd34SGreg Roach        return new RedirectResponse($source->url());
9175a78cd34SGreg Roach    }
9185a78cd34SGreg Roach
9195a78cd34SGreg Roach    /**
9205a78cd34SGreg Roach     * Get all the records in the cart.
9215a78cd34SGreg Roach     *
9225a78cd34SGreg Roach     * @param Tree $tree
9235a78cd34SGreg Roach     *
9245a78cd34SGreg Roach     * @return GedcomRecord[]
9255a78cd34SGreg Roach     */
926c1010edaSGreg Roach    private function allRecordsInCart(Tree $tree): array
927c1010edaSGreg Roach    {
9285a78cd34SGreg Roach        $cart = Session::get('cart', []);
9295a78cd34SGreg Roach
9305a78cd34SGreg Roach        $xrefs = array_keys($cart[$tree->getName()] ?? []);
9315a78cd34SGreg Roach
9325a78cd34SGreg Roach        // Fetch all the records in the cart.
93318d7a90dSGreg Roach        $records = array_map(function (string $xref) use ($tree): GedcomRecord {
9345a78cd34SGreg Roach            return GedcomRecord::getInstance($xref, $tree);
9355a78cd34SGreg Roach        }, $xrefs);
9365a78cd34SGreg Roach
9375a78cd34SGreg Roach        // Some records may have been deleted after they were added to the cart.
9385a78cd34SGreg Roach        $records = array_filter($records);
9395a78cd34SGreg Roach
9405a78cd34SGreg Roach        // Group and sort.
94118d7a90dSGreg Roach        uasort($records, function (GedcomRecord $x, GedcomRecord $y): int {
9425a78cd34SGreg Roach            return $x::RECORD_TYPE <=> $y::RECORD_TYPE ?: GedcomRecord::compare($x, $y);
9435a78cd34SGreg Roach        });
9445a78cd34SGreg Roach
9455a78cd34SGreg Roach        return $records;
9465a78cd34SGreg Roach    }
9475a78cd34SGreg Roach
9485a78cd34SGreg Roach    /**
9495a78cd34SGreg Roach     * Add a record (and direclty linked sources, notes, etc. to the cart.
9505a78cd34SGreg Roach     *
9515a78cd34SGreg Roach     * @param GedcomRecord $record
95218d7a90dSGreg Roach     *
95318d7a90dSGreg Roach     * @return void
9545a78cd34SGreg Roach     */
955c1010edaSGreg Roach    private function addRecordToCart(GedcomRecord $record)
956c1010edaSGreg Roach    {
9575a78cd34SGreg Roach        $cart = Session::get('cart', []);
9585a78cd34SGreg Roach
9595a78cd34SGreg Roach        $tree_name = $record->getTree()->getName();
9605a78cd34SGreg Roach
9615a78cd34SGreg Roach        // Add this record
9625a78cd34SGreg Roach        $cart[$tree_name][$record->getXref()] = true;
9635a78cd34SGreg Roach
9645a78cd34SGreg Roach        // Add directly linked media, notes, repositories and sources.
9655a78cd34SGreg Roach        preg_match_all('/\n\d (?:OBJE|NOTE|SOUR|REPO) @(' . WT_REGEX_XREF . ')@/', $record->getGedcom(), $matches);
9665a78cd34SGreg Roach
9675a78cd34SGreg Roach        foreach ($matches[1] as $match) {
9685a78cd34SGreg Roach            $cart[$tree_name][$match] = true;
9695a78cd34SGreg Roach        }
9705a78cd34SGreg Roach
9715a78cd34SGreg Roach        Session::put('cart', $cart);
9725a78cd34SGreg Roach    }
9735a78cd34SGreg Roach
9745a78cd34SGreg Roach    /**
9755a78cd34SGreg Roach     * @param Tree $tree
9765a78cd34SGreg Roach     *
9775a78cd34SGreg Roach     * @return bool
9785a78cd34SGreg Roach     */
979c1010edaSGreg Roach    private function isCartEmpty(Tree $tree): bool
980c1010edaSGreg Roach    {
9815a78cd34SGreg Roach        $cart = Session::get('cart', []);
9825a78cd34SGreg Roach
9835a78cd34SGreg Roach        return empty($cart[$tree->getName()]);
9845a78cd34SGreg Roach    }
9855a78cd34SGreg Roach
9865a78cd34SGreg Roach    /**
9875a78cd34SGreg Roach     * Only allow access to the routes/functions if the menu is active
9885a78cd34SGreg Roach     *
9895a78cd34SGreg Roach     * @param Tree $tree
99018d7a90dSGreg Roach     *
99118d7a90dSGreg Roach     * @return void
99218d7a90dSGreg Roach     *
99318d7a90dSGreg Roach     * @throws NoteNotFoundException
9945a78cd34SGreg Roach     */
995c1010edaSGreg Roach    private function checkModuleAccess(Tree $tree)
996c1010edaSGreg Roach    {
9975a78cd34SGreg Roach        if (!array_key_exists($this->getName(), Module::getActiveMenus($tree))) {
99859f2f229SGreg Roach            throw new NotFoundHttpException();
9995a78cd34SGreg Roach        }
10008c2e8227SGreg Roach    }
10018c2e8227SGreg Roach}
1002