xref: /webtrees/app/Module/ClippingsCartModule.php (revision 7fcd9838ea114967dcab7ada1d85a8bd12ee2aa7)
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                }
2465a78cd34SGreg Roach                switch ($object::RECORD_TYPE) {
2478c2e8227SGreg Roach                    case 'INDI':
2488c2e8227SGreg Roach                    case 'FAM':
2495a78cd34SGreg Roach                        $filetext .= $record . "\n";
2505a78cd34SGreg Roach                        $filetext .= "1 SOUR @WEBTREES@\n";
2515a78cd34SGreg Roach                        $filetext .= '2 PAGE ' . WT_BASE_URL . $object->url() . "\n";
2525a78cd34SGreg Roach                        break;
2535a78cd34SGreg Roach                    case 'SOUR':
2545a78cd34SGreg Roach                        $filetext .= $record . "\n";
2555a78cd34SGreg Roach                        $filetext .= '1 NOTE ' . WT_BASE_URL . $object->url() . "\n";
2565a78cd34SGreg Roach                        break;
2575a78cd34SGreg Roach                    case 'OBJE':
2585a78cd34SGreg Roach                        // Add the file to the archive
2595a78cd34SGreg Roach                        foreach ($object->mediaFiles() as $media_file) {
2605a78cd34SGreg Roach                            if (file_exists($media_file->getServerFilename())) {
2615a78cd34SGreg Roach                                $fp = fopen($media_file->getServerFilename(), 'r');
2625a78cd34SGreg Roach                                $zip_filesystem->writeStream($path . $media_file->filename(), $fp);
2635a78cd34SGreg Roach                                fclose($fp);
2645a78cd34SGreg Roach                            }
2655a78cd34SGreg Roach                        }
2665a78cd34SGreg Roach                        $filetext .= $record . "\n";
2675a78cd34SGreg Roach                        break;
2685a78cd34SGreg Roach                    default:
2695a78cd34SGreg Roach                        $filetext .= $record . "\n";
2708c2e8227SGreg Roach                        break;
2718c2e8227SGreg Roach                }
2728c2e8227SGreg Roach            }
2738c2e8227SGreg Roach        }
2748c2e8227SGreg Roach
2755a78cd34SGreg Roach        // Create a source, to indicate the source of the data.
2765a78cd34SGreg Roach        $filetext .= "0 @WEBTREES@ SOUR\n1 TITL " . WT_BASE_URL . "\n";
277*7fcd9838SGreg Roach        $author = User::find((int) $tree->getPreference('CONTACT_USER_ID'));
2785a78cd34SGreg Roach        if ($author !== null) {
2795a78cd34SGreg Roach            $filetext .= '1 AUTH ' . $author->getRealName() . "\n";
2805a78cd34SGreg Roach        }
2815a78cd34SGreg Roach        $filetext .= "0 TRLR\n";
2825a78cd34SGreg Roach
2835a78cd34SGreg Roach        // Make sure the preferred line endings are used
2845a78cd34SGreg Roach        $filetext = preg_replace("/[\r\n]+/", Gedcom::EOL, $filetext);
2855a78cd34SGreg Roach
2865a78cd34SGreg Roach        if ($convert === 'yes') {
2875a78cd34SGreg Roach            $filetext = str_replace('UTF-8', 'ANSI', $filetext);
2885a78cd34SGreg Roach            $filetext = utf8_decode($filetext);
2898c2e8227SGreg Roach        }
290cbc1590aSGreg Roach
2915a78cd34SGreg Roach        // Finally add the GEDCOM file to the .ZIP file.
2925a78cd34SGreg Roach        $zip_filesystem->write('clippings.ged', $filetext);
2935a78cd34SGreg Roach
2945a78cd34SGreg Roach        // Need to force-close the filesystem
2955a78cd34SGreg Roach        $zip_filesystem = null;
2965a78cd34SGreg Roach
2975a78cd34SGreg Roach        $response = new BinaryFileResponse($temp_zip_file);
2985a78cd34SGreg Roach        $response->deleteFileAfterSend(true);
2995a78cd34SGreg Roach
3005a78cd34SGreg Roach        $response->headers->set('Content-Type', 'application/zip');
3015a78cd34SGreg Roach        $response->setContentDisposition(
3025a78cd34SGreg Roach            ResponseHeaderBag::DISPOSITION_ATTACHMENT,
3035a78cd34SGreg Roach            'clippings.zip'
3045a78cd34SGreg Roach        );
3055a78cd34SGreg Roach
3065a78cd34SGreg Roach        return $response;
3078c2e8227SGreg Roach    }
3088c2e8227SGreg Roach
3098c2e8227SGreg Roach    /**
310b6db7c1fSGreg Roach     * @param Tree $tree
311b6db7c1fSGreg Roach     * @param User $user
31276692c8bSGreg Roach     *
3135a78cd34SGreg Roach     * @return Response
3148c2e8227SGreg Roach     */
315b6db7c1fSGreg Roach    public function getDownloadFormAction(Tree $tree, User $user): Response
316c1010edaSGreg Roach    {
3175a78cd34SGreg Roach        $title = I18N::translate('Family tree clippings cart') . ' — ' . I18N::translate('Download');
3188c2e8227SGreg Roach
3195a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/download', [
3205a78cd34SGreg Roach            'is_manager' => Auth::isManager($tree, $user),
3215a78cd34SGreg Roach            'is_member'  => Auth::isMember($tree, $user),
3225a78cd34SGreg Roach            'title'      => $title,
3235a78cd34SGreg Roach        ]);
3248c2e8227SGreg Roach    }
3258c2e8227SGreg Roach
3265a78cd34SGreg Roach    /**
327b6db7c1fSGreg Roach     * @param Tree $tree
3285a78cd34SGreg Roach     *
3295a78cd34SGreg Roach     * @return RedirectResponse
3305a78cd34SGreg Roach     */
331b6db7c1fSGreg Roach    public function getEmptyAction(Tree $tree): RedirectResponse
332c1010edaSGreg Roach    {
3335a78cd34SGreg Roach        $cart                   = Session::get('cart', []);
3345a78cd34SGreg Roach        $cart[$tree->getName()] = [];
3355a78cd34SGreg Roach        Session::put('cart', $cart);
3368c2e8227SGreg Roach
337c1010edaSGreg Roach        $url = route('module', [
338c1010edaSGreg Roach            'module' => 'clippings',
339c1010edaSGreg Roach            'action' => 'Show',
340c1010edaSGreg Roach            'ged'    => $tree->getName(),
341c1010edaSGreg Roach        ]);
3425a78cd34SGreg Roach
3435a78cd34SGreg Roach        return new RedirectResponse($url);
3445a78cd34SGreg Roach    }
3455a78cd34SGreg Roach
3465a78cd34SGreg Roach    /**
3475a78cd34SGreg Roach     * @param Request $request
348b6db7c1fSGreg Roach     * @param Tree    $tree
3495a78cd34SGreg Roach     *
3505a78cd34SGreg Roach     * @return RedirectResponse
3515a78cd34SGreg Roach     */
352b6db7c1fSGreg Roach    public function postRemoveAction(Request $request, Tree $tree): RedirectResponse
353c1010edaSGreg Roach    {
3545a78cd34SGreg Roach        $xref = $request->get('xref');
3555a78cd34SGreg Roach
3565a78cd34SGreg Roach        $cart = Session::get('cart', []);
3575a78cd34SGreg Roach        unset($cart[$tree->getName()][$xref]);
3585a78cd34SGreg Roach        Session::put('cart', $cart);
3595a78cd34SGreg Roach
360c1010edaSGreg Roach        $url = route('module', [
361c1010edaSGreg Roach            'module' => 'clippings',
362c1010edaSGreg Roach            'action' => 'Show',
363c1010edaSGreg Roach            'ged'    => $tree->getName(),
364c1010edaSGreg Roach        ]);
3655a78cd34SGreg Roach
3665a78cd34SGreg Roach        return new RedirectResponse($url);
3675a78cd34SGreg Roach    }
3685a78cd34SGreg Roach
3695a78cd34SGreg Roach    /**
370b6db7c1fSGreg Roach     * @param Tree $tree
3715a78cd34SGreg Roach     *
3725a78cd34SGreg Roach     * @return Response
3735a78cd34SGreg Roach     */
374b6db7c1fSGreg Roach    public function getShowAction(Tree $tree): Response
375c1010edaSGreg Roach    {
3765a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/show', [
3775a78cd34SGreg Roach            'records' => $this->allRecordsInCart($tree),
3785a78cd34SGreg Roach            'title'   => I18N::translate('Family tree clippings cart'),
3795a78cd34SGreg Roach            'tree'    => $tree,
3805a78cd34SGreg Roach        ]);
3815a78cd34SGreg Roach    }
3825a78cd34SGreg Roach
3835a78cd34SGreg Roach    /**
3845a78cd34SGreg Roach     * @param Request $request
385b6db7c1fSGreg Roach     * @param Tree    $tree
3865a78cd34SGreg Roach     *
3875a78cd34SGreg Roach     * @return Response
3885a78cd34SGreg Roach     */
389b6db7c1fSGreg Roach    public function getAddFamilyAction(Request $request, Tree $tree): Response
390c1010edaSGreg Roach    {
3915a78cd34SGreg Roach        $xref = $request->get('xref');
3925a78cd34SGreg Roach
3935a78cd34SGreg Roach        $family = Family::getInstance($xref, $tree);
3945a78cd34SGreg Roach
3955a78cd34SGreg Roach        if ($family === null) {
39659f2f229SGreg Roach            throw new FamilyNotFoundException();
3975a78cd34SGreg Roach        }
3985a78cd34SGreg Roach
3995a78cd34SGreg Roach        $options = $this->familyOptions($family);
4005a78cd34SGreg Roach
4015a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $family->getFullName());
4025a78cd34SGreg Roach
4035a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
4045a78cd34SGreg Roach            'options' => $options,
4055a78cd34SGreg Roach            'default' => key($options),
4065a78cd34SGreg Roach            'record'  => $family,
4075a78cd34SGreg Roach            'title'   => $title,
4085a78cd34SGreg Roach            'tree'    => $tree,
4095a78cd34SGreg Roach        ]);
4105a78cd34SGreg Roach    }
4115a78cd34SGreg Roach
4125a78cd34SGreg Roach    /**
4135a78cd34SGreg Roach     * @param Family $family
4145a78cd34SGreg Roach     *
4155a78cd34SGreg Roach     * @return string[]
4165a78cd34SGreg Roach     */
417c1010edaSGreg Roach    private function familyOptions(Family $family): array
418c1010edaSGreg Roach    {
4195a78cd34SGreg Roach        $name = strip_tags($family->getFullName());
4205a78cd34SGreg Roach
4215a78cd34SGreg Roach        return [
4225a78cd34SGreg Roach            'parents'     => $name,
423bbb76c12SGreg Roach            /* I18N: %s is a family (husband + wife) */
424bbb76c12SGreg Roach            'members'     => I18N::translate('%s and their children', $name),
425bbb76c12SGreg Roach            /* I18N: %s is a family (husband + wife) */
426bbb76c12SGreg Roach            'descendants' => I18N::translate('%s and their descendants', $name),
4275a78cd34SGreg Roach        ];
4285a78cd34SGreg Roach    }
4295a78cd34SGreg Roach
4305a78cd34SGreg Roach    /**
4315a78cd34SGreg Roach     * @param Request $request
432b6db7c1fSGreg Roach     * @param Tree    $tree
4335a78cd34SGreg Roach     *
4345a78cd34SGreg Roach     * @return RedirectResponse
4355a78cd34SGreg Roach     */
436b6db7c1fSGreg Roach    public function postAddFamilyAction(Request $request, Tree $tree): RedirectResponse
437c1010edaSGreg Roach    {
4385a78cd34SGreg Roach        $xref   = $request->get('xref');
4395a78cd34SGreg Roach        $option = $request->get('option');
4405a78cd34SGreg Roach
4415a78cd34SGreg Roach        $family = Family::getInstance($xref, $tree);
4425a78cd34SGreg Roach
4435a78cd34SGreg Roach        if ($family === null) {
44459f2f229SGreg Roach            throw new FamilyNotFoundException();
4455a78cd34SGreg Roach        }
4465a78cd34SGreg Roach
4475a78cd34SGreg Roach        switch ($option) {
4485a78cd34SGreg Roach            case 'parents':
4495a78cd34SGreg Roach                $this->addFamilyToCart($family);
4505a78cd34SGreg Roach                break;
4515a78cd34SGreg Roach
4525a78cd34SGreg Roach            case 'members':
4535a78cd34SGreg Roach                $this->addFamilyAndChildrenToCart($family);
4545a78cd34SGreg Roach                break;
4555a78cd34SGreg Roach
4565a78cd34SGreg Roach            case 'descendants':
4575a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($family);
4585a78cd34SGreg Roach                break;
4595a78cd34SGreg Roach        }
4605a78cd34SGreg Roach
4615a78cd34SGreg Roach        return new RedirectResponse($family->url());
4625a78cd34SGreg Roach    }
4635a78cd34SGreg Roach
4645a78cd34SGreg Roach    /**
4655a78cd34SGreg Roach     * @param Family $family
46618d7a90dSGreg Roach     *
46718d7a90dSGreg Roach     * @return void
4685a78cd34SGreg Roach     */
469c1010edaSGreg Roach    private function addFamilyToCart(Family $family)
470c1010edaSGreg Roach    {
4715a78cd34SGreg Roach        $this->addRecordToCart($family);
4725a78cd34SGreg Roach
4735a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
4745a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4755a78cd34SGreg Roach        }
4765a78cd34SGreg Roach    }
4775a78cd34SGreg Roach
4785a78cd34SGreg Roach    /**
4795a78cd34SGreg Roach     * @param Family $family
48018d7a90dSGreg Roach     *
48118d7a90dSGreg Roach     * @return void
4825a78cd34SGreg Roach     */
483c1010edaSGreg Roach    private function addFamilyAndChildrenToCart(Family $family)
484c1010edaSGreg Roach    {
4855a78cd34SGreg Roach        $this->addRecordToCart($family);
4865a78cd34SGreg Roach
4875a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
4885a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4895a78cd34SGreg Roach        }
4905a78cd34SGreg Roach        foreach ($family->getChildren() as $child) {
4915a78cd34SGreg Roach            $this->addRecordToCart($child);
4925a78cd34SGreg Roach        }
4935a78cd34SGreg Roach    }
4945a78cd34SGreg Roach
4955a78cd34SGreg Roach    /**
4965a78cd34SGreg Roach     * @param Family $family
49718d7a90dSGreg Roach     *
49818d7a90dSGreg Roach     * @return void
4995a78cd34SGreg Roach     */
500c1010edaSGreg Roach    private function addFamilyAndDescendantsToCart(Family $family)
501c1010edaSGreg Roach    {
5025a78cd34SGreg Roach        $this->addRecordToCart($family);
5035a78cd34SGreg Roach
5045a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
5055a78cd34SGreg Roach            $this->addRecordToCart($spouse);
5065a78cd34SGreg Roach        }
5075a78cd34SGreg Roach        foreach ($family->getChildren() as $child) {
5085a78cd34SGreg Roach            $this->addRecordToCart($child);
5095a78cd34SGreg Roach            foreach ($child->getSpouseFamilies() as $child_family) {
5105a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($child_family);
5115a78cd34SGreg Roach            }
5125a78cd34SGreg Roach        }
5135a78cd34SGreg Roach    }
5145a78cd34SGreg Roach
5155a78cd34SGreg Roach    /**
5165a78cd34SGreg Roach     * @param Request $request
517b6db7c1fSGreg Roach     * @param Tree    $tree
5185a78cd34SGreg Roach     *
5195a78cd34SGreg Roach     * @return Response
5205a78cd34SGreg Roach     */
521b6db7c1fSGreg Roach    public function getAddIndividualAction(Request $request, Tree $tree): Response
522c1010edaSGreg Roach    {
5235a78cd34SGreg Roach        $xref = $request->get('xref');
5245a78cd34SGreg Roach
5255a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
5265a78cd34SGreg Roach
5275a78cd34SGreg Roach        if ($individual === null) {
52859f2f229SGreg Roach            throw new IndividualNotFoundException();
5295a78cd34SGreg Roach        }
5305a78cd34SGreg Roach
5315a78cd34SGreg Roach        $options = $this->individualOptions($individual);
5325a78cd34SGreg Roach
5335a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $individual->getFullName());
5345a78cd34SGreg Roach
5355a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
5365a78cd34SGreg Roach            'options' => $options,
5375a78cd34SGreg Roach            'default' => key($options),
5385a78cd34SGreg Roach            'record'  => $individual,
5395a78cd34SGreg Roach            'title'   => $title,
5405a78cd34SGreg Roach            'tree'    => $tree,
5415a78cd34SGreg Roach        ]);
5425a78cd34SGreg Roach    }
5435a78cd34SGreg Roach
5445a78cd34SGreg Roach    /**
5455a78cd34SGreg Roach     * @param Individual $individual
5465a78cd34SGreg Roach     *
5475a78cd34SGreg Roach     * @return string[]
5485a78cd34SGreg Roach     */
549c1010edaSGreg Roach    private function individualOptions(Individual $individual): array
550c1010edaSGreg Roach    {
5515a78cd34SGreg Roach        $name = strip_tags($individual->getFullName());
5525a78cd34SGreg Roach
5535a78cd34SGreg Roach        if ($individual->getSex() === 'F') {
5545a78cd34SGreg Roach            return [
5555a78cd34SGreg Roach                'self'              => $name,
5565a78cd34SGreg Roach                'parents'           => I18N::translate('%s, her parents and siblings', $name),
5575a78cd34SGreg Roach                'spouses'           => I18N::translate('%s, her spouses and children', $name),
5585a78cd34SGreg Roach                'ancestors'         => I18N::translate('%s and her ancestors', $name),
5595a78cd34SGreg Roach                'ancestor_families' => I18N::translate('%s, her ancestors and their families', $name),
5605a78cd34SGreg Roach                'descendants'       => I18N::translate('%s, her spouses and descendants', $name),
5615a78cd34SGreg Roach            ];
562b2ce94c6SRico Sonntag        }
563b2ce94c6SRico Sonntag
5645a78cd34SGreg Roach        return [
5655a78cd34SGreg Roach            'self'              => $name,
5665a78cd34SGreg Roach            'parents'           => I18N::translate('%s, his parents and siblings', $name),
5675a78cd34SGreg Roach            'spouses'           => I18N::translate('%s, his spouses and children', $name),
5685a78cd34SGreg Roach            'ancestors'         => I18N::translate('%s and his ancestors', $name),
5695a78cd34SGreg Roach            'ancestor_families' => I18N::translate('%s, his ancestors and their families', $name),
5705a78cd34SGreg Roach            'descendants'       => I18N::translate('%s, his spouses and descendants', $name),
5715a78cd34SGreg Roach        ];
5725a78cd34SGreg Roach    }
5735a78cd34SGreg Roach
5745a78cd34SGreg Roach    /**
5755a78cd34SGreg Roach     * @param Request $request
576b6db7c1fSGreg Roach     * @param Tree    $tree
5775a78cd34SGreg Roach     *
5785a78cd34SGreg Roach     * @return RedirectResponse
5795a78cd34SGreg Roach     */
580b6db7c1fSGreg Roach    public function postAddIndividualAction(Request $request, Tree $tree): RedirectResponse
581c1010edaSGreg Roach    {
5825a78cd34SGreg Roach        $xref   = $request->get('xref');
5835a78cd34SGreg Roach        $option = $request->get('option');
5845a78cd34SGreg Roach
5855a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
5865a78cd34SGreg Roach
5875a78cd34SGreg Roach        if ($individual === null) {
58859f2f229SGreg Roach            throw new IndividualNotFoundException();
5895a78cd34SGreg Roach        }
5905a78cd34SGreg Roach
5915a78cd34SGreg Roach        switch ($option) {
5925a78cd34SGreg Roach            case 'self':
5935a78cd34SGreg Roach                $this->addRecordToCart($individual);
5945a78cd34SGreg Roach                break;
5955a78cd34SGreg Roach
5965a78cd34SGreg Roach            case 'parents':
5975a78cd34SGreg Roach                foreach ($individual->getChildFamilies() as $family) {
5985a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
5995a78cd34SGreg Roach                }
6005a78cd34SGreg Roach                break;
6015a78cd34SGreg Roach
6025a78cd34SGreg Roach            case 'spouses':
6035a78cd34SGreg Roach                foreach ($individual->getSpouseFamilies() as $family) {
6045a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
6055a78cd34SGreg Roach                }
6065a78cd34SGreg Roach                break;
6075a78cd34SGreg Roach
6085a78cd34SGreg Roach            case 'ancestors':
6095a78cd34SGreg Roach                $this->addAncestorsToCart($individual);
6105a78cd34SGreg Roach                break;
6115a78cd34SGreg Roach
6125a78cd34SGreg Roach            case 'ancestor_families':
6135a78cd34SGreg Roach                $this->addAncestorFamiliesToCart($individual);
6145a78cd34SGreg Roach                break;
6155a78cd34SGreg Roach
6165a78cd34SGreg Roach            case 'descendants':
6175a78cd34SGreg Roach                foreach ($individual->getSpouseFamilies() as $family) {
6185a78cd34SGreg Roach                    $this->addFamilyAndDescendantsToCart($family);
6195a78cd34SGreg Roach                }
6205a78cd34SGreg Roach                break;
6215a78cd34SGreg Roach        }
6225a78cd34SGreg Roach
6235a78cd34SGreg Roach        return new RedirectResponse($individual->url());
6245a78cd34SGreg Roach    }
6255a78cd34SGreg Roach
6265a78cd34SGreg Roach    /**
6275a78cd34SGreg Roach     * @param Individual $individual
62818d7a90dSGreg Roach     *
62918d7a90dSGreg Roach     * @return void
6305a78cd34SGreg Roach     */
631c1010edaSGreg Roach    private function addAncestorsToCart(Individual $individual)
632c1010edaSGreg Roach    {
6335a78cd34SGreg Roach        $this->addRecordToCart($individual);
6345a78cd34SGreg Roach
6355a78cd34SGreg Roach        foreach ($individual->getChildFamilies() as $family) {
6365a78cd34SGreg Roach            foreach ($family->getSpouses() as $parent) {
6375a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
6385a78cd34SGreg Roach            }
6395a78cd34SGreg Roach        }
6405a78cd34SGreg Roach    }
6415a78cd34SGreg Roach
6425a78cd34SGreg Roach    /**
6435a78cd34SGreg Roach     * @param Individual $individual
64418d7a90dSGreg Roach     *
64518d7a90dSGreg Roach     * @return void
6465a78cd34SGreg Roach     */
647c1010edaSGreg Roach    private function addAncestorFamiliesToCart(Individual $individual)
648c1010edaSGreg Roach    {
6495a78cd34SGreg Roach        foreach ($individual->getChildFamilies() as $family) {
6505a78cd34SGreg Roach            $this->addFamilyAndChildrenToCart($family);
6515a78cd34SGreg Roach            foreach ($family->getSpouses() as $parent) {
6525a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
6535a78cd34SGreg Roach            }
6545a78cd34SGreg Roach        }
6555a78cd34SGreg Roach    }
6565a78cd34SGreg Roach
6575a78cd34SGreg Roach    /**
6585a78cd34SGreg Roach     * @param Request $request
659b6db7c1fSGreg Roach     * @param Tree    $tree
6605a78cd34SGreg Roach     *
6615a78cd34SGreg Roach     * @return Response
6625a78cd34SGreg Roach     */
663b6db7c1fSGreg Roach    public function getAddMediaAction(Request $request, Tree $tree): Response
664c1010edaSGreg Roach    {
6655a78cd34SGreg Roach        $xref = $request->get('xref');
6665a78cd34SGreg Roach
6675a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
6685a78cd34SGreg Roach
6695a78cd34SGreg Roach        if ($media === null) {
67059f2f229SGreg Roach            throw new MediaNotFoundException();
6715a78cd34SGreg Roach        }
6725a78cd34SGreg Roach
6735a78cd34SGreg Roach        $options = $this->mediaOptions($media);
6745a78cd34SGreg Roach
6755a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $media->getFullName());
6765a78cd34SGreg Roach
6775a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
6785a78cd34SGreg Roach            'options' => $options,
6795a78cd34SGreg Roach            'default' => key($options),
6805a78cd34SGreg Roach            'record'  => $media,
6815a78cd34SGreg Roach            'title'   => $title,
6825a78cd34SGreg Roach            'tree'    => $tree,
6835a78cd34SGreg Roach        ]);
6845a78cd34SGreg Roach    }
6855a78cd34SGreg Roach
6865a78cd34SGreg Roach    /**
6875a78cd34SGreg Roach     * @param Media $media
6885a78cd34SGreg Roach     *
6895a78cd34SGreg Roach     * @return string[]
6905a78cd34SGreg Roach     */
691c1010edaSGreg Roach    private function mediaOptions(Media $media): array
692c1010edaSGreg Roach    {
6935a78cd34SGreg Roach        $name = strip_tags($media->getFullName());
6945a78cd34SGreg Roach
6955a78cd34SGreg Roach        return [
6965a78cd34SGreg Roach            'self' => $name,
6975a78cd34SGreg Roach        ];
6985a78cd34SGreg Roach    }
6995a78cd34SGreg Roach
7005a78cd34SGreg Roach    /**
7015a78cd34SGreg Roach     * @param Request $request
702b6db7c1fSGreg Roach     * @param Tree    $tree
7035a78cd34SGreg Roach     *
7045a78cd34SGreg Roach     * @return RedirectResponse
7055a78cd34SGreg Roach     */
706b6db7c1fSGreg Roach    public function postAddMediaAction(Request $request, Tree $tree): RedirectResponse
707c1010edaSGreg Roach    {
7085a78cd34SGreg Roach        $xref = $request->get('xref');
7095a78cd34SGreg Roach
7105a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
7115a78cd34SGreg Roach
7125a78cd34SGreg Roach        if ($media === null) {
71359f2f229SGreg Roach            throw new MediaNotFoundException();
7145a78cd34SGreg Roach        }
7155a78cd34SGreg Roach
7165a78cd34SGreg Roach        $this->addRecordToCart($media);
7175a78cd34SGreg Roach
7185a78cd34SGreg Roach        return new RedirectResponse($media->url());
7195a78cd34SGreg Roach    }
7205a78cd34SGreg Roach
7215a78cd34SGreg Roach    /**
7225a78cd34SGreg Roach     * @param Request $request
723b6db7c1fSGreg Roach     * @param Tree    $tree
7245a78cd34SGreg Roach     *
7255a78cd34SGreg Roach     * @return Response
7265a78cd34SGreg Roach     */
727b6db7c1fSGreg Roach    public function getAddNoteAction(Request $request, Tree $tree): Response
728c1010edaSGreg Roach    {
7295a78cd34SGreg Roach        $xref = $request->get('xref');
7305a78cd34SGreg Roach
7315a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
7325a78cd34SGreg Roach
7335a78cd34SGreg Roach        if ($note === null) {
73459f2f229SGreg Roach            throw new NoteNotFoundException();
7355a78cd34SGreg Roach        }
7365a78cd34SGreg Roach
7375a78cd34SGreg Roach        $options = $this->noteOptions($note);
7385a78cd34SGreg Roach
7395a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $note->getFullName());
7405a78cd34SGreg Roach
7415a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
7425a78cd34SGreg Roach            'options' => $options,
7435a78cd34SGreg Roach            'default' => key($options),
7445a78cd34SGreg Roach            'record'  => $note,
7455a78cd34SGreg Roach            'title'   => $title,
7465a78cd34SGreg Roach            'tree'    => $tree,
7475a78cd34SGreg Roach        ]);
7485a78cd34SGreg Roach    }
7495a78cd34SGreg Roach
7505a78cd34SGreg Roach    /**
7515a78cd34SGreg Roach     * @param Note $note
7525a78cd34SGreg Roach     *
7535a78cd34SGreg Roach     * @return string[]
7545a78cd34SGreg Roach     */
755c1010edaSGreg Roach    private function noteOptions(Note $note): array
756c1010edaSGreg Roach    {
7575a78cd34SGreg Roach        $name = strip_tags($note->getFullName());
7585a78cd34SGreg Roach
7595a78cd34SGreg Roach        return [
7605a78cd34SGreg Roach            'self' => $name,
7615a78cd34SGreg Roach        ];
7625a78cd34SGreg Roach    }
7635a78cd34SGreg Roach
7645a78cd34SGreg Roach    /**
7655a78cd34SGreg Roach     * @param Request $request
766b6db7c1fSGreg Roach     * @param Tree    $tree
7675a78cd34SGreg Roach     *
7685a78cd34SGreg Roach     * @return RedirectResponse
7695a78cd34SGreg Roach     */
770b6db7c1fSGreg Roach    public function postAddNoteAction(Request $request, Tree $tree): RedirectResponse
771c1010edaSGreg Roach    {
7725a78cd34SGreg Roach        $xref = $request->get('xref');
7735a78cd34SGreg Roach
7745a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
7755a78cd34SGreg Roach
7765a78cd34SGreg Roach        if ($note === null) {
77759f2f229SGreg Roach            throw new NoteNotFoundException();
7785a78cd34SGreg Roach        }
7795a78cd34SGreg Roach
7805a78cd34SGreg Roach        $this->addRecordToCart($note);
7815a78cd34SGreg Roach
7825a78cd34SGreg Roach        return new RedirectResponse($note->url());
7835a78cd34SGreg Roach    }
7845a78cd34SGreg Roach
7855a78cd34SGreg Roach    /**
7865a78cd34SGreg Roach     * @param Request $request
787b6db7c1fSGreg Roach     * @param Tree    $tree
7885a78cd34SGreg Roach     *
7895a78cd34SGreg Roach     * @return Response
7905a78cd34SGreg Roach     */
791b6db7c1fSGreg Roach    public function getAddRepositoryAction(Request $request, Tree $tree): Response
792c1010edaSGreg Roach    {
7935a78cd34SGreg Roach        $xref = $request->get('xref');
7945a78cd34SGreg Roach
7955a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
7965a78cd34SGreg Roach
7975a78cd34SGreg Roach        if ($repository === null) {
79859f2f229SGreg Roach            throw new RepositoryNotFoundException();
7995a78cd34SGreg Roach        }
8005a78cd34SGreg Roach
8015a78cd34SGreg Roach        $options = $this->repositoryOptions($repository);
8025a78cd34SGreg Roach
8035a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $repository->getFullName());
8045a78cd34SGreg Roach
8055a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
8065a78cd34SGreg Roach            'options' => $options,
8075a78cd34SGreg Roach            'default' => key($options),
8085a78cd34SGreg Roach            'record'  => $repository,
8095a78cd34SGreg Roach            'title'   => $title,
8105a78cd34SGreg Roach            'tree'    => $tree,
8115a78cd34SGreg Roach        ]);
8125a78cd34SGreg Roach    }
8135a78cd34SGreg Roach
8145a78cd34SGreg Roach    /**
8155a78cd34SGreg Roach     * @param Repository $repository
8165a78cd34SGreg Roach     *
8175a78cd34SGreg Roach     * @return string[]
8185a78cd34SGreg Roach     */
819c1010edaSGreg Roach    private function repositoryOptions(Repository $repository): array
820c1010edaSGreg Roach    {
8215a78cd34SGreg Roach        $name = strip_tags($repository->getFullName());
8225a78cd34SGreg Roach
8235a78cd34SGreg Roach        return [
8245a78cd34SGreg Roach            'self' => $name,
8255a78cd34SGreg Roach        ];
8265a78cd34SGreg Roach    }
8275a78cd34SGreg Roach
8285a78cd34SGreg Roach    /**
8295a78cd34SGreg Roach     * @param Request $request
830b6db7c1fSGreg Roach     * @param Tree    $tree
8315a78cd34SGreg Roach     *
8325a78cd34SGreg Roach     * @return RedirectResponse
8335a78cd34SGreg Roach     */
834b6db7c1fSGreg Roach    public function postAddRepositoryAction(Request $request, Tree $tree): RedirectResponse
835c1010edaSGreg Roach    {
8365a78cd34SGreg Roach        $xref = $request->get('xref');
8375a78cd34SGreg Roach
8385a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
8395a78cd34SGreg Roach
8405a78cd34SGreg Roach        if ($repository === null) {
84159f2f229SGreg Roach            throw new RepositoryNotFoundException();
8425a78cd34SGreg Roach        }
8435a78cd34SGreg Roach
8445a78cd34SGreg Roach        $this->addRecordToCart($repository);
8455a78cd34SGreg Roach
8465a78cd34SGreg Roach        return new RedirectResponse($repository->url());
8475a78cd34SGreg Roach    }
8485a78cd34SGreg Roach
8495a78cd34SGreg Roach    /**
8505a78cd34SGreg Roach     * @param Request $request
851b6db7c1fSGreg Roach     * @param Tree    $tree
8525a78cd34SGreg Roach     *
8535a78cd34SGreg Roach     * @return Response
8545a78cd34SGreg Roach     */
855b6db7c1fSGreg Roach    public function getAddSourceAction(Request $request, Tree $tree): Response
856c1010edaSGreg Roach    {
8575a78cd34SGreg Roach        $xref = $request->get('xref');
8585a78cd34SGreg Roach
8595a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
8605a78cd34SGreg Roach
8615a78cd34SGreg Roach        if ($source === null) {
86259f2f229SGreg Roach            throw new SourceNotFoundException();
8635a78cd34SGreg Roach        }
8645a78cd34SGreg Roach
8655a78cd34SGreg Roach        $options = $this->sourceOptions($source);
8665a78cd34SGreg Roach
8675a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $source->getFullName());
8685a78cd34SGreg Roach
8695a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
8705a78cd34SGreg Roach            'options' => $options,
8715a78cd34SGreg Roach            'default' => key($options),
8725a78cd34SGreg Roach            'record'  => $source,
8735a78cd34SGreg Roach            'title'   => $title,
8745a78cd34SGreg Roach            'tree'    => $tree,
8755a78cd34SGreg Roach        ]);
8765a78cd34SGreg Roach    }
8775a78cd34SGreg Roach
8785a78cd34SGreg Roach    /**
8795a78cd34SGreg Roach     * @param Source $source
8805a78cd34SGreg Roach     *
8815a78cd34SGreg Roach     * @return string[]
8825a78cd34SGreg Roach     */
883c1010edaSGreg Roach    private function sourceOptions(Source $source): array
884c1010edaSGreg Roach    {
8855a78cd34SGreg Roach        $name = strip_tags($source->getFullName());
8865a78cd34SGreg Roach
8875a78cd34SGreg Roach        return [
8885a78cd34SGreg Roach            'only'   => strip_tags($source->getFullName()),
8895a78cd34SGreg Roach            'linked' => I18N::translate('%s and the individuals that reference it.', $name),
8905a78cd34SGreg Roach        ];
8915a78cd34SGreg Roach    }
8925a78cd34SGreg Roach
8935a78cd34SGreg Roach    /**
8945a78cd34SGreg Roach     * @param Request $request
895b6db7c1fSGreg Roach     * @param Tree    $tree
8965a78cd34SGreg Roach     *
8975a78cd34SGreg Roach     * @return RedirectResponse
8985a78cd34SGreg Roach     */
899b6db7c1fSGreg Roach    public function postAddSourceAction(Request $request, Tree $tree): RedirectResponse
900c1010edaSGreg Roach    {
9015a78cd34SGreg Roach        $xref   = $request->get('xref');
9025a78cd34SGreg Roach        $option = $request->get('option');
9035a78cd34SGreg Roach
9045a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
9055a78cd34SGreg Roach
9065a78cd34SGreg Roach        if ($source === null) {
90759f2f229SGreg Roach            throw new SourceNotFoundException();
9085a78cd34SGreg Roach        }
9095a78cd34SGreg Roach
9105a78cd34SGreg Roach        $this->addRecordToCart($source);
9115a78cd34SGreg Roach
9125a78cd34SGreg Roach        if ($option === 'linked') {
9135a78cd34SGreg Roach            foreach ($source->linkedIndividuals('SOUR') as $individual) {
9145a78cd34SGreg Roach                $this->addRecordToCart($individual);
9155a78cd34SGreg Roach            }
9165a78cd34SGreg Roach            foreach ($source->linkedFamilies('SOUR') as $family) {
9175a78cd34SGreg Roach                $this->addRecordToCart($family);
9185a78cd34SGreg Roach            }
9195a78cd34SGreg Roach        }
9205a78cd34SGreg Roach
9215a78cd34SGreg Roach        return new RedirectResponse($source->url());
9225a78cd34SGreg Roach    }
9235a78cd34SGreg Roach
9245a78cd34SGreg Roach    /**
9255a78cd34SGreg Roach     * Get all the records in the cart.
9265a78cd34SGreg Roach     *
9275a78cd34SGreg Roach     * @param Tree $tree
9285a78cd34SGreg Roach     *
9295a78cd34SGreg Roach     * @return GedcomRecord[]
9305a78cd34SGreg Roach     */
931c1010edaSGreg Roach    private function allRecordsInCart(Tree $tree): array
932c1010edaSGreg Roach    {
9335a78cd34SGreg Roach        $cart = Session::get('cart', []);
9345a78cd34SGreg Roach
9355a78cd34SGreg Roach        $xrefs = array_keys($cart[$tree->getName()] ?? []);
9365a78cd34SGreg Roach
9375a78cd34SGreg Roach        // Fetch all the records in the cart.
93818d7a90dSGreg Roach        $records = array_map(function (string $xref) use ($tree): GedcomRecord {
9395a78cd34SGreg Roach            return GedcomRecord::getInstance($xref, $tree);
9405a78cd34SGreg Roach        }, $xrefs);
9415a78cd34SGreg Roach
9425a78cd34SGreg Roach        // Some records may have been deleted after they were added to the cart.
9435a78cd34SGreg Roach        $records = array_filter($records);
9445a78cd34SGreg Roach
9455a78cd34SGreg Roach        // Group and sort.
94618d7a90dSGreg Roach        uasort($records, function (GedcomRecord $x, GedcomRecord $y): int {
9475a78cd34SGreg Roach            return $x::RECORD_TYPE <=> $y::RECORD_TYPE ?: GedcomRecord::compare($x, $y);
9485a78cd34SGreg Roach        });
9495a78cd34SGreg Roach
9505a78cd34SGreg Roach        return $records;
9515a78cd34SGreg Roach    }
9525a78cd34SGreg Roach
9535a78cd34SGreg Roach    /**
9545a78cd34SGreg Roach     * Add a record (and direclty linked sources, notes, etc. to the cart.
9555a78cd34SGreg Roach     *
9565a78cd34SGreg Roach     * @param GedcomRecord $record
95718d7a90dSGreg Roach     *
95818d7a90dSGreg Roach     * @return void
9595a78cd34SGreg Roach     */
960c1010edaSGreg Roach    private function addRecordToCart(GedcomRecord $record)
961c1010edaSGreg Roach    {
9625a78cd34SGreg Roach        $cart = Session::get('cart', []);
9635a78cd34SGreg Roach
9645a78cd34SGreg Roach        $tree_name = $record->getTree()->getName();
9655a78cd34SGreg Roach
9665a78cd34SGreg Roach        // Add this record
9675a78cd34SGreg Roach        $cart[$tree_name][$record->getXref()] = true;
9685a78cd34SGreg Roach
9695a78cd34SGreg Roach        // Add directly linked media, notes, repositories and sources.
9705a78cd34SGreg Roach        preg_match_all('/\n\d (?:OBJE|NOTE|SOUR|REPO) @(' . WT_REGEX_XREF . ')@/', $record->getGedcom(), $matches);
9715a78cd34SGreg Roach
9725a78cd34SGreg Roach        foreach ($matches[1] as $match) {
9735a78cd34SGreg Roach            $cart[$tree_name][$match] = true;
9745a78cd34SGreg Roach        }
9755a78cd34SGreg Roach
9765a78cd34SGreg Roach        Session::put('cart', $cart);
9775a78cd34SGreg Roach    }
9785a78cd34SGreg Roach
9795a78cd34SGreg Roach    /**
9805a78cd34SGreg Roach     * @param Tree $tree
9815a78cd34SGreg Roach     *
9825a78cd34SGreg Roach     * @return bool
9835a78cd34SGreg Roach     */
984c1010edaSGreg Roach    private function isCartEmpty(Tree $tree): bool
985c1010edaSGreg Roach    {
9865a78cd34SGreg Roach        $cart = Session::get('cart', []);
9875a78cd34SGreg Roach
9885a78cd34SGreg Roach        return empty($cart[$tree->getName()]);
9895a78cd34SGreg Roach    }
9905a78cd34SGreg Roach
9915a78cd34SGreg Roach    /**
9925a78cd34SGreg Roach     * Only allow access to the routes/functions if the menu is active
9935a78cd34SGreg Roach     *
9945a78cd34SGreg Roach     * @param Tree $tree
99518d7a90dSGreg Roach     *
99618d7a90dSGreg Roach     * @return void
99718d7a90dSGreg Roach     *
99818d7a90dSGreg Roach     * @throws NoteNotFoundException
9995a78cd34SGreg Roach     */
1000c1010edaSGreg Roach    private function checkModuleAccess(Tree $tree)
1001c1010edaSGreg Roach    {
10025a78cd34SGreg Roach        if (!array_key_exists($this->getName(), Module::getActiveMenus($tree))) {
100359f2f229SGreg Roach            throw new NotFoundHttpException();
10045a78cd34SGreg Roach        }
10058c2e8227SGreg Roach    }
10068c2e8227SGreg Roach}
1007