xref: /webtrees/app/Module/ClippingsCartModule.php (revision 59f2f229057fe08ec1b09bd435699190641eed6a)
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 */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
195a78cd34SGreg Roachuse Fisharebest\Webtrees\Database;
200bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\FamilyNotFoundException;
210bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualNotFoundException;
220bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\MediaNotFoundException;
230bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\NoteNotFoundException;
240bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\RepositoryNotFoundException;
250bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\SourceNotFoundException;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Family;
275a78cd34SGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsExport;
285a78cd34SGreg Roachuse Fisharebest\Webtrees\Gedcom;
290e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
300e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
310e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
325a78cd34SGreg Roachuse Fisharebest\Webtrees\Media;
330e62c4b8SGreg Roachuse Fisharebest\Webtrees\Menu;
341eca16b3SGreg Roachuse Fisharebest\Webtrees\Module;
355a78cd34SGreg Roachuse Fisharebest\Webtrees\Note;
365a78cd34SGreg Roachuse Fisharebest\Webtrees\Repository;
370e62c4b8SGreg Roachuse Fisharebest\Webtrees\Session;
385a78cd34SGreg Roachuse Fisharebest\Webtrees\Source;
39aee13b6dSGreg Roachuse Fisharebest\Webtrees\Tree;
405a78cd34SGreg Roachuse Fisharebest\Webtrees\User;
415a78cd34SGreg Roachuse League\Flysystem\Filesystem;
425a78cd34SGreg Roachuse League\Flysystem\ZipArchive\ZipArchiveAdapter;
435a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\BinaryFileResponse;
445a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse;
455a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\Request;
465a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\Response;
475a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\ResponseHeaderBag;
485a78cd34SGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
498c2e8227SGreg Roach
508c2e8227SGreg Roach/**
518c2e8227SGreg Roach * Class ClippingsCartModule
528c2e8227SGreg Roach */
53c1010edaSGreg Roachclass ClippingsCartModule extends AbstractModule implements ModuleMenuInterface
54c1010edaSGreg Roach{
555a78cd34SGreg Roach    // Routes that have a record which can be added to the clipboard
56c1010edaSGreg Roach    const ROUTES_WITH_RECORDS = [
57c1010edaSGreg Roach        'family',
58c1010edaSGreg Roach        'individual',
59c1010edaSGreg Roach        'media',
60c1010edaSGreg Roach        'note',
61c1010edaSGreg Roach        'repository',
62c1010edaSGreg Roach        'source',
63c1010edaSGreg Roach    ];
645a78cd34SGreg Roach
658c2e8227SGreg Roach    /** {@inheritdoc} */
66c1010edaSGreg Roach    public function getTitle()
67c1010edaSGreg Roach    {
68bbb76c12SGreg Roach        /* I18N: Name of a module */
69bbb76c12SGreg Roach        return I18N::translate('Clippings cart');
708c2e8227SGreg Roach    }
718c2e8227SGreg Roach
728c2e8227SGreg Roach    /** {@inheritdoc} */
73c1010edaSGreg Roach    public function getDescription()
74c1010edaSGreg Roach    {
75bbb76c12SGreg Roach        /* I18N: Description of the “Clippings cart” module */
76bbb76c12SGreg Roach        return I18N::translate('Select records from your family tree and save them as a GEDCOM file.');
778c2e8227SGreg Roach    }
788c2e8227SGreg Roach
790ee13198SGreg Roach    /**
800ee13198SGreg Roach     * What is the default access level for this module?
810ee13198SGreg Roach     *
820ee13198SGreg Roach     * Some modules are aimed at admins or managers, and are not generally shown to users.
830ee13198SGreg Roach     *
840ee13198SGreg Roach     * @return int
850ee13198SGreg Roach     */
86c1010edaSGreg Roach    public function defaultAccessLevel()
87c1010edaSGreg Roach    {
884b9ff166SGreg Roach        return Auth::PRIV_USER;
898c2e8227SGreg Roach    }
908c2e8227SGreg Roach
9176692c8bSGreg Roach    /**
920ee13198SGreg Roach     * The user can re-order menus. Until they do, they are shown in this order.
930ee13198SGreg Roach     *
940ee13198SGreg Roach     * @return int
950ee13198SGreg Roach     */
96c1010edaSGreg Roach    public function defaultMenuOrder()
97c1010edaSGreg Roach    {
988c2e8227SGreg Roach        return 20;
998c2e8227SGreg Roach    }
1008c2e8227SGreg Roach
1010ee13198SGreg Roach    /**
1020ee13198SGreg Roach     * A menu, to be added to the main application menu.
1030ee13198SGreg Roach     *
104aee13b6dSGreg Roach     * @param Tree $tree
105aee13b6dSGreg Roach     *
1060ee13198SGreg Roach     * @return Menu|null
1070ee13198SGreg Roach     */
108c1010edaSGreg Roach    public function getMenu(Tree $tree)
109c1010edaSGreg Roach    {
1105a78cd34SGreg Roach        $request = Request::createFromGlobals();
1118c2e8227SGreg Roach
1125a78cd34SGreg Roach        $route = $request->get('route');
1135a78cd34SGreg Roach
1145a78cd34SGreg Roach        $submenus = [
115c1010edaSGreg Roach            new Menu($this->getTitle(), route('module', [
116c1010edaSGreg Roach                'module' => 'clippings',
117c1010edaSGreg Roach                'action' => 'Show',
118c1010edaSGreg Roach                'ged'    => $tree->getName(),
119c1010edaSGreg Roach            ]), 'menu-clippings-cart', ['rel' => 'nofollow']),
1205a78cd34SGreg Roach        ];
1215a78cd34SGreg Roach
1225a78cd34SGreg Roach        if (in_array($route, self::ROUTES_WITH_RECORDS)) {
1235a78cd34SGreg Roach            $xref      = $request->get('xref');
1245a78cd34SGreg Roach            $action    = 'Add' . ucfirst($route);
125c1010edaSGreg Roach            $add_route = route('module', [
126c1010edaSGreg Roach                'module' => 'clippings',
127c1010edaSGreg Roach                'action' => $action,
128c1010edaSGreg Roach                'xref'   => $xref,
129c1010edaSGreg Roach                'ged'    => $tree->getName(),
130c1010edaSGreg Roach            ]);
1315a78cd34SGreg Roach
13225b2dde3SGreg Roach            $submenus[] = new Menu(I18N::translate('Add to the clippings cart'), $add_route, 'menu-clippings-add', ['rel' => 'nofollow']);
1338c2e8227SGreg Roach        }
134cbc1590aSGreg Roach
1355a78cd34SGreg Roach        if (!$this->isCartEmpty($tree)) {
136c1010edaSGreg Roach            $submenus[] = new Menu(I18N::translate('Empty the clippings cart'), route('module', [
137c1010edaSGreg Roach                'module' => 'clippings',
138c1010edaSGreg Roach                'action' => 'Empty',
139c1010edaSGreg Roach                'ged'    => $tree->getName(),
140c1010edaSGreg Roach            ]), 'menu-clippings-empty', ['rel' => 'nofollow']);
141c1010edaSGreg Roach            $submenus[] = new Menu(I18N::translate('Download'), route('module', [
142c1010edaSGreg Roach                'module' => 'clippings',
143c1010edaSGreg Roach                'action' => 'DownloadForm',
144c1010edaSGreg Roach                'ged'    => $tree->getName(),
145c1010edaSGreg Roach            ]), 'menu-clippings-download', ['rel' => 'nofollow']);
1465a78cd34SGreg Roach        }
1475a78cd34SGreg Roach
14813abd6f3SGreg Roach        return new Menu($this->getTitle(), '#', 'menu-clippings', ['rel' => 'nofollow'], $submenus);
1498c2e8227SGreg Roach    }
1508c2e8227SGreg Roach
15176692c8bSGreg Roach    /**
1525a78cd34SGreg Roach     * @param Request $request
153b6db7c1fSGreg Roach     * @param Tree    $tree
15476692c8bSGreg Roach     *
1555a78cd34SGreg Roach     * @return BinaryFileResponse
15676692c8bSGreg Roach     */
157b6db7c1fSGreg Roach    public function getDownloadAction(Request $request, Tree $tree): BinaryFileResponse
158c1010edaSGreg Roach    {
1595a78cd34SGreg Roach        $this->checkModuleAccess($tree);
1608c2e8227SGreg Roach
1615a78cd34SGreg Roach        $privatize_export = $request->get('privatize_export');
1625a78cd34SGreg Roach        $convert          = (bool)$request->get('convert');
1638c2e8227SGreg Roach
16413abd6f3SGreg Roach        $cart = Session::get('cart', []);
1658c2e8227SGreg Roach
1665a78cd34SGreg Roach        $xrefs = array_keys($cart[$tree->getName()] ?? []);
1675a78cd34SGreg Roach
1685a78cd34SGreg Roach        // Create a new/empty .ZIP file
1695a78cd34SGreg Roach        $temp_zip_file  = tempnam(sys_get_temp_dir(), 'webtrees-zip-');
1705a78cd34SGreg Roach        $zip_filesystem = new Filesystem(new ZipArchiveAdapter($temp_zip_file));
1715a78cd34SGreg Roach
1725a78cd34SGreg Roach        // Media file prefix
1735a78cd34SGreg Roach        $path = $tree->getPreference('MEDIA_DIRECTORY');
1745a78cd34SGreg Roach
1755a78cd34SGreg Roach        // GEDCOM file header
1765a78cd34SGreg Roach        $filetext = FunctionsExport::gedcomHeader($tree);
1775a78cd34SGreg Roach
1785a78cd34SGreg Roach        // Include SUBM/SUBN records, if they exist
1795a78cd34SGreg Roach        $subn =
1805a78cd34SGreg Roach            Database::prepare("SELECT o_gedcom FROM `##other` WHERE o_type=? AND o_file=?")
181c1010edaSGreg Roach                ->execute([
182c1010edaSGreg Roach                    'SUBN',
183c1010edaSGreg Roach                    $tree->getName(),
184c1010edaSGreg Roach                ])
1855a78cd34SGreg Roach                ->fetchOne();
1865a78cd34SGreg Roach        if ($subn) {
1875a78cd34SGreg Roach            $filetext .= $subn . "\n";
188b6468bb9SGreg Roach        }
1895a78cd34SGreg Roach        $subm =
1905a78cd34SGreg Roach            Database::prepare("SELECT o_gedcom FROM `##other` WHERE o_type=? AND o_file=?")
191c1010edaSGreg Roach                ->execute([
192c1010edaSGreg Roach                    'SUBM',
193c1010edaSGreg Roach                    $tree->getName(),
194c1010edaSGreg Roach                ])
1955a78cd34SGreg Roach                ->fetchOne();
1965a78cd34SGreg Roach        if ($subm) {
1975a78cd34SGreg Roach            $filetext .= $subm . "\n";
1985a78cd34SGreg Roach        }
1995a78cd34SGreg Roach
2005a78cd34SGreg Roach        switch ($privatize_export) {
2015a78cd34SGreg Roach            case 'gedadmin':
2025a78cd34SGreg Roach                $access_level = Auth::PRIV_NONE;
2035a78cd34SGreg Roach                break;
2045a78cd34SGreg Roach            case 'user':
2055a78cd34SGreg Roach                $access_level = Auth::PRIV_USER;
2065a78cd34SGreg Roach                break;
2075a78cd34SGreg Roach            case 'visitor':
2085a78cd34SGreg Roach                $access_level = Auth::PRIV_PRIVATE;
2095a78cd34SGreg Roach                break;
2105a78cd34SGreg Roach            case 'none':
2115a78cd34SGreg Roach            default:
2125a78cd34SGreg Roach                $access_level = Auth::PRIV_HIDE;
2135a78cd34SGreg Roach                break;
2145a78cd34SGreg Roach        }
2155a78cd34SGreg Roach
2165a78cd34SGreg Roach        foreach ($xrefs as $xref) {
2175a78cd34SGreg Roach            $object = GedcomRecord::getInstance($xref, $tree);
2185a78cd34SGreg Roach            // The object may have been deleted since we added it to the cart....
2195a78cd34SGreg Roach            if ($object) {
2205a78cd34SGreg Roach                $record = $object->privatizeGedcom($access_level);
2215a78cd34SGreg Roach                // Remove links to objects that aren't in the cart
2225a78cd34SGreg Roach                preg_match_all('/\n1 ' . WT_REGEX_TAG . ' @(' . WT_REGEX_XREF . ')@(\n[2-9].*)*/', $record, $matches, PREG_SET_ORDER);
2235a78cd34SGreg Roach                foreach ($matches as $match) {
2245a78cd34SGreg Roach                    if (!array_key_exists($match[1], $xrefs)) {
2255a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2265a78cd34SGreg Roach                    }
2275a78cd34SGreg Roach                }
2285a78cd34SGreg Roach                preg_match_all('/\n2 ' . WT_REGEX_TAG . ' @(' . WT_REGEX_XREF . ')@(\n[3-9].*)*/', $record, $matches, PREG_SET_ORDER);
2295a78cd34SGreg Roach                foreach ($matches as $match) {
2305a78cd34SGreg Roach                    if (!array_key_exists($match[1], $xrefs)) {
2315a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2325a78cd34SGreg Roach                    }
2335a78cd34SGreg Roach                }
2345a78cd34SGreg Roach                preg_match_all('/\n3 ' . WT_REGEX_TAG . ' @(' . WT_REGEX_XREF . ')@(\n[4-9].*)*/', $record, $matches, PREG_SET_ORDER);
2355a78cd34SGreg Roach                foreach ($matches as $match) {
2365a78cd34SGreg Roach                    if (!array_key_exists($match[1], $xrefs)) {
2375a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2385a78cd34SGreg Roach                    }
2395a78cd34SGreg Roach                }
2405a78cd34SGreg Roach
2415a78cd34SGreg Roach                if ($convert) {
2425a78cd34SGreg Roach                    $record = utf8_decode($record);
2435a78cd34SGreg Roach                }
2445a78cd34SGreg Roach                switch ($object::RECORD_TYPE) {
2458c2e8227SGreg Roach                    case 'INDI':
2468c2e8227SGreg Roach                    case 'FAM':
2475a78cd34SGreg Roach                        $filetext .= $record . "\n";
2485a78cd34SGreg Roach                        $filetext .= "1 SOUR @WEBTREES@\n";
2495a78cd34SGreg Roach                        $filetext .= '2 PAGE ' . WT_BASE_URL . $object->url() . "\n";
2505a78cd34SGreg Roach                        break;
2515a78cd34SGreg Roach                    case 'SOUR':
2525a78cd34SGreg Roach                        $filetext .= $record . "\n";
2535a78cd34SGreg Roach                        $filetext .= '1 NOTE ' . WT_BASE_URL . $object->url() . "\n";
2545a78cd34SGreg Roach                        break;
2555a78cd34SGreg Roach                    case 'OBJE':
2565a78cd34SGreg Roach                        // Add the file to the archive
2575a78cd34SGreg Roach                        foreach ($object->mediaFiles() as $media_file) {
2585a78cd34SGreg Roach                            if (file_exists($media_file->getServerFilename())) {
2595a78cd34SGreg Roach                                $fp = fopen($media_file->getServerFilename(), 'r');
2605a78cd34SGreg Roach                                $zip_filesystem->writeStream($path . $media_file->filename(), $fp);
2615a78cd34SGreg Roach                                fclose($fp);
2625a78cd34SGreg Roach                            }
2635a78cd34SGreg Roach                        }
2645a78cd34SGreg Roach                        $filetext .= $record . "\n";
2655a78cd34SGreg Roach                        break;
2665a78cd34SGreg Roach                    default:
2675a78cd34SGreg Roach                        $filetext .= $record . "\n";
2688c2e8227SGreg Roach                        break;
2698c2e8227SGreg Roach                }
2708c2e8227SGreg Roach            }
2718c2e8227SGreg Roach        }
2728c2e8227SGreg Roach
2735a78cd34SGreg Roach        // Create a source, to indicate the source of the data.
2745a78cd34SGreg Roach        $filetext .= "0 @WEBTREES@ SOUR\n1 TITL " . WT_BASE_URL . "\n";
2755a78cd34SGreg Roach        $author   = User::find($tree->getPreference('CONTACT_EMAIL'));
2765a78cd34SGreg Roach        if ($author !== null) {
2775a78cd34SGreg Roach            $filetext .= '1 AUTH ' . $author->getRealName() . "\n";
2785a78cd34SGreg Roach        }
2795a78cd34SGreg Roach        $filetext .= "0 TRLR\n";
2805a78cd34SGreg Roach
2815a78cd34SGreg Roach        // Make sure the preferred line endings are used
2825a78cd34SGreg Roach        $filetext = preg_replace("/[\r\n]+/", Gedcom::EOL, $filetext);
2835a78cd34SGreg Roach
2845a78cd34SGreg Roach        if ($convert === 'yes') {
2855a78cd34SGreg Roach            $filetext = str_replace('UTF-8', 'ANSI', $filetext);
2865a78cd34SGreg Roach            $filetext = utf8_decode($filetext);
2878c2e8227SGreg Roach        }
288cbc1590aSGreg Roach
2895a78cd34SGreg Roach        // Finally add the GEDCOM file to the .ZIP file.
2905a78cd34SGreg Roach        $zip_filesystem->write('clippings.ged', $filetext);
2915a78cd34SGreg Roach
2925a78cd34SGreg Roach        // Need to force-close the filesystem
2935a78cd34SGreg Roach        $zip_filesystem = null;
2945a78cd34SGreg Roach
2955a78cd34SGreg Roach        $response = new BinaryFileResponse($temp_zip_file);
2965a78cd34SGreg Roach        $response->deleteFileAfterSend(true);
2975a78cd34SGreg Roach
2985a78cd34SGreg Roach        $response->headers->set('Content-Type', 'application/zip');
2995a78cd34SGreg Roach        $response->setContentDisposition(
3005a78cd34SGreg Roach            ResponseHeaderBag::DISPOSITION_ATTACHMENT,
3015a78cd34SGreg Roach            'clippings.zip'
3025a78cd34SGreg Roach        );
3035a78cd34SGreg Roach
3045a78cd34SGreg Roach        return $response;
3058c2e8227SGreg Roach    }
3068c2e8227SGreg Roach
3078c2e8227SGreg Roach    /**
308b6db7c1fSGreg Roach     * @param Tree $tree
309b6db7c1fSGreg Roach     * @param User $user
31076692c8bSGreg Roach     *
3115a78cd34SGreg Roach     * @return Response
3128c2e8227SGreg Roach     */
313b6db7c1fSGreg Roach    public function getDownloadFormAction(Tree $tree, User $user): Response
314c1010edaSGreg Roach    {
3155a78cd34SGreg Roach        $title = I18N::translate('Family tree clippings cart') . ' — ' . I18N::translate('Download');
3168c2e8227SGreg Roach
3175a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/download', [
3185a78cd34SGreg Roach            'is_manager' => Auth::isManager($tree, $user),
3195a78cd34SGreg Roach            'is_member'  => Auth::isMember($tree, $user),
3205a78cd34SGreg Roach            'title'      => $title,
3215a78cd34SGreg Roach        ]);
3228c2e8227SGreg Roach    }
3238c2e8227SGreg Roach
3245a78cd34SGreg Roach    /**
325b6db7c1fSGreg Roach     * @param Tree $tree
3265a78cd34SGreg Roach     *
3275a78cd34SGreg Roach     * @return RedirectResponse
3285a78cd34SGreg Roach     */
329b6db7c1fSGreg Roach    public function getEmptyAction(Tree $tree): RedirectResponse
330c1010edaSGreg Roach    {
3315a78cd34SGreg Roach        $cart                   = Session::get('cart', []);
3325a78cd34SGreg Roach        $cart[$tree->getName()] = [];
3335a78cd34SGreg Roach        Session::put('cart', $cart);
3348c2e8227SGreg Roach
335c1010edaSGreg Roach        $url = route('module', [
336c1010edaSGreg Roach            'module' => 'clippings',
337c1010edaSGreg Roach            'action' => 'Show',
338c1010edaSGreg Roach            'ged'    => $tree->getName(),
339c1010edaSGreg Roach        ]);
3405a78cd34SGreg Roach
3415a78cd34SGreg Roach        return new RedirectResponse($url);
3425a78cd34SGreg Roach    }
3435a78cd34SGreg Roach
3445a78cd34SGreg Roach    /**
3455a78cd34SGreg Roach     * @param Request $request
346b6db7c1fSGreg Roach     * @param Tree    $tree
3475a78cd34SGreg Roach     *
3485a78cd34SGreg Roach     * @return RedirectResponse
3495a78cd34SGreg Roach     */
350b6db7c1fSGreg Roach    public function postRemoveAction(Request $request, Tree $tree): RedirectResponse
351c1010edaSGreg Roach    {
3525a78cd34SGreg Roach        $xref = $request->get('xref');
3535a78cd34SGreg Roach
3545a78cd34SGreg Roach        $cart = Session::get('cart', []);
3555a78cd34SGreg Roach        unset($cart[$tree->getName()][$xref]);
3565a78cd34SGreg Roach        Session::put('cart', $cart);
3575a78cd34SGreg Roach
358c1010edaSGreg Roach        $url = route('module', [
359c1010edaSGreg Roach            'module' => 'clippings',
360c1010edaSGreg Roach            'action' => 'Show',
361c1010edaSGreg Roach            'ged'    => $tree->getName(),
362c1010edaSGreg Roach        ]);
3635a78cd34SGreg Roach
3645a78cd34SGreg Roach        return new RedirectResponse($url);
3655a78cd34SGreg Roach    }
3665a78cd34SGreg Roach
3675a78cd34SGreg Roach    /**
368b6db7c1fSGreg Roach     * @param Tree $tree
3695a78cd34SGreg Roach     *
3705a78cd34SGreg Roach     * @return Response
3715a78cd34SGreg Roach     */
372b6db7c1fSGreg Roach    public function getShowAction(Tree $tree): Response
373c1010edaSGreg Roach    {
3745a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/show', [
3755a78cd34SGreg Roach            'records' => $this->allRecordsInCart($tree),
3765a78cd34SGreg Roach            'title'   => I18N::translate('Family tree clippings cart'),
3775a78cd34SGreg Roach            'tree'    => $tree,
3785a78cd34SGreg Roach        ]);
3795a78cd34SGreg Roach    }
3805a78cd34SGreg Roach
3815a78cd34SGreg Roach    /**
3825a78cd34SGreg Roach     * @param Request $request
383b6db7c1fSGreg Roach     * @param Tree    $tree
3845a78cd34SGreg Roach     *
3855a78cd34SGreg Roach     * @return Response
3865a78cd34SGreg Roach     */
387b6db7c1fSGreg Roach    public function getAddFamilyAction(Request $request, Tree $tree): Response
388c1010edaSGreg Roach    {
3895a78cd34SGreg Roach        $xref = $request->get('xref');
3905a78cd34SGreg Roach
3915a78cd34SGreg Roach        $family = Family::getInstance($xref, $tree);
3925a78cd34SGreg Roach
3935a78cd34SGreg Roach        if ($family === null) {
394*59f2f229SGreg Roach            throw new FamilyNotFoundException();
3955a78cd34SGreg Roach        }
3965a78cd34SGreg Roach
3975a78cd34SGreg Roach        $options = $this->familyOptions($family);
3985a78cd34SGreg Roach
3995a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $family->getFullName());
4005a78cd34SGreg Roach
4015a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
4025a78cd34SGreg Roach            'options' => $options,
4035a78cd34SGreg Roach            'default' => key($options),
4045a78cd34SGreg Roach            'record'  => $family,
4055a78cd34SGreg Roach            'title'   => $title,
4065a78cd34SGreg Roach            'tree'    => $tree,
4075a78cd34SGreg Roach        ]);
4085a78cd34SGreg Roach    }
4095a78cd34SGreg Roach
4105a78cd34SGreg Roach    /**
4115a78cd34SGreg Roach     * @param Family $family
4125a78cd34SGreg Roach     *
4135a78cd34SGreg Roach     * @return string[]
4145a78cd34SGreg Roach     */
415c1010edaSGreg Roach    private function familyOptions(Family $family): array
416c1010edaSGreg Roach    {
4175a78cd34SGreg Roach        $name = strip_tags($family->getFullName());
4185a78cd34SGreg Roach
4195a78cd34SGreg Roach        return [
4205a78cd34SGreg Roach            'parents'     => $name,
421bbb76c12SGreg Roach            /* I18N: %s is a family (husband + wife) */
422bbb76c12SGreg Roach            'members'     => I18N::translate('%s and their children', $name),
423bbb76c12SGreg Roach            /* I18N: %s is a family (husband + wife) */
424bbb76c12SGreg Roach            'descendants' => I18N::translate('%s and their descendants', $name),
4255a78cd34SGreg Roach        ];
4265a78cd34SGreg Roach    }
4275a78cd34SGreg Roach
4285a78cd34SGreg Roach    /**
4295a78cd34SGreg Roach     * @param Request $request
430b6db7c1fSGreg Roach     * @param Tree    $tree
4315a78cd34SGreg Roach     *
4325a78cd34SGreg Roach     * @return RedirectResponse
4335a78cd34SGreg Roach     */
434b6db7c1fSGreg Roach    public function postAddFamilyAction(Request $request, Tree $tree): RedirectResponse
435c1010edaSGreg Roach    {
4365a78cd34SGreg Roach        $xref   = $request->get('xref');
4375a78cd34SGreg Roach        $option = $request->get('option');
4385a78cd34SGreg Roach
4395a78cd34SGreg Roach        $family = Family::getInstance($xref, $tree);
4405a78cd34SGreg Roach
4415a78cd34SGreg Roach        if ($family === null) {
442*59f2f229SGreg Roach            throw new FamilyNotFoundException();
4435a78cd34SGreg Roach        }
4445a78cd34SGreg Roach
4455a78cd34SGreg Roach        switch ($option) {
4465a78cd34SGreg Roach            case 'parents':
4475a78cd34SGreg Roach                $this->addFamilyToCart($family);
4485a78cd34SGreg Roach                break;
4495a78cd34SGreg Roach
4505a78cd34SGreg Roach            case 'members':
4515a78cd34SGreg Roach                $this->addFamilyAndChildrenToCart($family);
4525a78cd34SGreg Roach                break;
4535a78cd34SGreg Roach
4545a78cd34SGreg Roach            case 'descendants':
4555a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($family);
4565a78cd34SGreg Roach                break;
4575a78cd34SGreg Roach        }
4585a78cd34SGreg Roach
4595a78cd34SGreg Roach        return new RedirectResponse($family->url());
4605a78cd34SGreg Roach    }
4615a78cd34SGreg Roach
4625a78cd34SGreg Roach    /**
4635a78cd34SGreg Roach     * @param Family $family
4645a78cd34SGreg Roach     */
465c1010edaSGreg Roach    private function addFamilyToCart(Family $family)
466c1010edaSGreg Roach    {
4675a78cd34SGreg Roach        $this->addRecordToCart($family);
4685a78cd34SGreg Roach
4695a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
4705a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4715a78cd34SGreg Roach        }
4725a78cd34SGreg Roach    }
4735a78cd34SGreg Roach
4745a78cd34SGreg Roach    /**
4755a78cd34SGreg Roach     * @param Family $family
4765a78cd34SGreg Roach     */
477c1010edaSGreg Roach    private function addFamilyAndChildrenToCart(Family $family)
478c1010edaSGreg Roach    {
4795a78cd34SGreg Roach        $this->addRecordToCart($family);
4805a78cd34SGreg Roach
4815a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
4825a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4835a78cd34SGreg Roach        }
4845a78cd34SGreg Roach        foreach ($family->getChildren() as $child) {
4855a78cd34SGreg Roach            $this->addRecordToCart($child);
4865a78cd34SGreg Roach        }
4875a78cd34SGreg Roach    }
4885a78cd34SGreg Roach
4895a78cd34SGreg Roach    /**
4905a78cd34SGreg Roach     * @param Family $family
4915a78cd34SGreg Roach     */
492c1010edaSGreg Roach    private function addFamilyAndDescendantsToCart(Family $family)
493c1010edaSGreg Roach    {
4945a78cd34SGreg Roach        $this->addRecordToCart($family);
4955a78cd34SGreg Roach
4965a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
4975a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4985a78cd34SGreg Roach        }
4995a78cd34SGreg Roach        foreach ($family->getChildren() as $child) {
5005a78cd34SGreg Roach            $this->addRecordToCart($child);
5015a78cd34SGreg Roach            foreach ($child->getSpouseFamilies() as $child_family) {
5025a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($child_family);
5035a78cd34SGreg Roach            }
5045a78cd34SGreg Roach        }
5055a78cd34SGreg Roach    }
5065a78cd34SGreg Roach
5075a78cd34SGreg Roach    /**
5085a78cd34SGreg Roach     * @param Request $request
509b6db7c1fSGreg Roach     * @param Tree    $tree
5105a78cd34SGreg Roach     *
5115a78cd34SGreg Roach     * @return Response
5125a78cd34SGreg Roach     */
513b6db7c1fSGreg Roach    public function getAddIndividualAction(Request $request, Tree $tree): Response
514c1010edaSGreg Roach    {
5155a78cd34SGreg Roach        $xref = $request->get('xref');
5165a78cd34SGreg Roach
5175a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
5185a78cd34SGreg Roach
5195a78cd34SGreg Roach        if ($individual === null) {
520*59f2f229SGreg Roach            throw new IndividualNotFoundException();
5215a78cd34SGreg Roach        }
5225a78cd34SGreg Roach
5235a78cd34SGreg Roach        $options = $this->individualOptions($individual);
5245a78cd34SGreg Roach
5255a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $individual->getFullName());
5265a78cd34SGreg Roach
5275a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
5285a78cd34SGreg Roach            'options' => $options,
5295a78cd34SGreg Roach            'default' => key($options),
5305a78cd34SGreg Roach            'record'  => $individual,
5315a78cd34SGreg Roach            'title'   => $title,
5325a78cd34SGreg Roach            'tree'    => $tree,
5335a78cd34SGreg Roach        ]);
5345a78cd34SGreg Roach    }
5355a78cd34SGreg Roach
5365a78cd34SGreg Roach    /**
5375a78cd34SGreg Roach     * @param Individual $individual
5385a78cd34SGreg Roach     *
5395a78cd34SGreg Roach     * @return string[]
5405a78cd34SGreg Roach     */
541c1010edaSGreg Roach    private function individualOptions(Individual $individual): array
542c1010edaSGreg Roach    {
5435a78cd34SGreg Roach        $name = strip_tags($individual->getFullName());
5445a78cd34SGreg Roach
5455a78cd34SGreg Roach        if ($individual->getSex() === 'F') {
5465a78cd34SGreg Roach            return [
5475a78cd34SGreg Roach                'self'              => $name,
5485a78cd34SGreg Roach                'parents'           => I18N::translate('%s, her parents and siblings', $name),
5495a78cd34SGreg Roach                'spouses'           => I18N::translate('%s, her spouses and children', $name),
5505a78cd34SGreg Roach                'ancestors'         => I18N::translate('%s and her ancestors', $name),
5515a78cd34SGreg Roach                'ancestor_families' => I18N::translate('%s, her ancestors and their families', $name),
5525a78cd34SGreg Roach                'descendants'       => I18N::translate('%s, her spouses and descendants', $name),
5535a78cd34SGreg Roach            ];
5545a78cd34SGreg Roach        } else {
5555a78cd34SGreg Roach            return [
5565a78cd34SGreg Roach                'self'              => $name,
5575a78cd34SGreg Roach                'parents'           => I18N::translate('%s, his parents and siblings', $name),
5585a78cd34SGreg Roach                'spouses'           => I18N::translate('%s, his spouses and children', $name),
5595a78cd34SGreg Roach                'ancestors'         => I18N::translate('%s and his ancestors', $name),
5605a78cd34SGreg Roach                'ancestor_families' => I18N::translate('%s, his ancestors and their families', $name),
5615a78cd34SGreg Roach                'descendants'       => I18N::translate('%s, his spouses and descendants', $name),
5625a78cd34SGreg Roach            ];
5635a78cd34SGreg Roach        }
5645a78cd34SGreg Roach    }
5655a78cd34SGreg Roach
5665a78cd34SGreg Roach    /**
5675a78cd34SGreg Roach     * @param Request $request
568b6db7c1fSGreg Roach     * @param Tree    $tree
5695a78cd34SGreg Roach     *
5705a78cd34SGreg Roach     * @return RedirectResponse
5715a78cd34SGreg Roach     */
572b6db7c1fSGreg Roach    public function postAddIndividualAction(Request $request, Tree $tree): RedirectResponse
573c1010edaSGreg Roach    {
5745a78cd34SGreg Roach        $xref   = $request->get('xref');
5755a78cd34SGreg Roach        $option = $request->get('option');
5765a78cd34SGreg Roach
5775a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
5785a78cd34SGreg Roach
5795a78cd34SGreg Roach        if ($individual === null) {
580*59f2f229SGreg Roach            throw new IndividualNotFoundException();
5815a78cd34SGreg Roach        }
5825a78cd34SGreg Roach
5835a78cd34SGreg Roach        switch ($option) {
5845a78cd34SGreg Roach            case 'self':
5855a78cd34SGreg Roach                $this->addRecordToCart($individual);
5865a78cd34SGreg Roach                break;
5875a78cd34SGreg Roach
5885a78cd34SGreg Roach            case 'parents':
5895a78cd34SGreg Roach                foreach ($individual->getChildFamilies() as $family) {
5905a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
5915a78cd34SGreg Roach                }
5925a78cd34SGreg Roach                break;
5935a78cd34SGreg Roach
5945a78cd34SGreg Roach            case 'spouses':
5955a78cd34SGreg Roach                foreach ($individual->getSpouseFamilies() as $family) {
5965a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
5975a78cd34SGreg Roach                }
5985a78cd34SGreg Roach                break;
5995a78cd34SGreg Roach
6005a78cd34SGreg Roach            case 'ancestors':
6015a78cd34SGreg Roach                $this->addAncestorsToCart($individual);
6025a78cd34SGreg Roach                break;
6035a78cd34SGreg Roach
6045a78cd34SGreg Roach            case 'ancestor_families':
6055a78cd34SGreg Roach                $this->addAncestorFamiliesToCart($individual);
6065a78cd34SGreg Roach                break;
6075a78cd34SGreg Roach
6085a78cd34SGreg Roach            case 'descendants':
6095a78cd34SGreg Roach                foreach ($individual->getSpouseFamilies() as $family) {
6105a78cd34SGreg Roach                    $this->addFamilyAndDescendantsToCart($family);
6115a78cd34SGreg Roach                }
6125a78cd34SGreg Roach                break;
6135a78cd34SGreg Roach        }
6145a78cd34SGreg Roach
6155a78cd34SGreg Roach        return new RedirectResponse($individual->url());
6165a78cd34SGreg Roach    }
6175a78cd34SGreg Roach
6185a78cd34SGreg Roach    /**
6195a78cd34SGreg Roach     * @param Individual $individual
6205a78cd34SGreg Roach     */
621c1010edaSGreg Roach    private function addAncestorsToCart(Individual $individual)
622c1010edaSGreg Roach    {
6235a78cd34SGreg Roach        $this->addRecordToCart($individual);
6245a78cd34SGreg Roach
6255a78cd34SGreg Roach        foreach ($individual->getChildFamilies() as $family) {
6265a78cd34SGreg Roach            foreach ($family->getSpouses() as $parent) {
6275a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
6285a78cd34SGreg Roach            }
6295a78cd34SGreg Roach        }
6305a78cd34SGreg Roach    }
6315a78cd34SGreg Roach
6325a78cd34SGreg Roach    /**
6335a78cd34SGreg Roach     * @param Individual $individual
6345a78cd34SGreg Roach     */
635c1010edaSGreg Roach    private function addAncestorFamiliesToCart(Individual $individual)
636c1010edaSGreg Roach    {
6375a78cd34SGreg Roach        foreach ($individual->getChildFamilies() as $family) {
6385a78cd34SGreg Roach            $this->addFamilyAndChildrenToCart($family);
6395a78cd34SGreg Roach            foreach ($family->getSpouses() as $parent) {
6405a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
6415a78cd34SGreg Roach            }
6425a78cd34SGreg Roach        }
6435a78cd34SGreg Roach    }
6445a78cd34SGreg Roach
6455a78cd34SGreg Roach    /**
6465a78cd34SGreg Roach     * @param Request $request
647b6db7c1fSGreg Roach     * @param Tree    $tree
6485a78cd34SGreg Roach     *
6495a78cd34SGreg Roach     * @return Response
6505a78cd34SGreg Roach     */
651b6db7c1fSGreg Roach    public function getAddMediaAction(Request $request, Tree $tree): Response
652c1010edaSGreg Roach    {
6535a78cd34SGreg Roach        $xref = $request->get('xref');
6545a78cd34SGreg Roach
6555a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
6565a78cd34SGreg Roach
6575a78cd34SGreg Roach        if ($media === null) {
658*59f2f229SGreg Roach            throw new MediaNotFoundException();
6595a78cd34SGreg Roach        }
6605a78cd34SGreg Roach
6615a78cd34SGreg Roach        $options = $this->mediaOptions($media);
6625a78cd34SGreg Roach
6635a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $media->getFullName());
6645a78cd34SGreg Roach
6655a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
6665a78cd34SGreg Roach            'options' => $options,
6675a78cd34SGreg Roach            'default' => key($options),
6685a78cd34SGreg Roach            'record'  => $media,
6695a78cd34SGreg Roach            'title'   => $title,
6705a78cd34SGreg Roach            'tree'    => $tree,
6715a78cd34SGreg Roach        ]);
6725a78cd34SGreg Roach    }
6735a78cd34SGreg Roach
6745a78cd34SGreg Roach    /**
6755a78cd34SGreg Roach     * @param Media $media
6765a78cd34SGreg Roach     *
6775a78cd34SGreg Roach     * @return string[]
6785a78cd34SGreg Roach     */
679c1010edaSGreg Roach    private function mediaOptions(Media $media): array
680c1010edaSGreg Roach    {
6815a78cd34SGreg Roach        $name = strip_tags($media->getFullName());
6825a78cd34SGreg Roach
6835a78cd34SGreg Roach        return [
6845a78cd34SGreg Roach            'self' => $name,
6855a78cd34SGreg Roach        ];
6865a78cd34SGreg Roach    }
6875a78cd34SGreg Roach
6885a78cd34SGreg Roach    /**
6895a78cd34SGreg Roach     * @param Request $request
690b6db7c1fSGreg Roach     * @param Tree    $tree
6915a78cd34SGreg Roach     *
6925a78cd34SGreg Roach     * @return RedirectResponse
6935a78cd34SGreg Roach     */
694b6db7c1fSGreg Roach    public function postAddMediaAction(Request $request, Tree $tree): RedirectResponse
695c1010edaSGreg Roach    {
6965a78cd34SGreg Roach        $xref = $request->get('xref');
6975a78cd34SGreg Roach
6985a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
6995a78cd34SGreg Roach
7005a78cd34SGreg Roach        if ($media === null) {
701*59f2f229SGreg Roach            throw new MediaNotFoundException();
7025a78cd34SGreg Roach        }
7035a78cd34SGreg Roach
7045a78cd34SGreg Roach        $this->addRecordToCart($media);
7055a78cd34SGreg Roach
7065a78cd34SGreg Roach        return new RedirectResponse($media->url());
7075a78cd34SGreg Roach    }
7085a78cd34SGreg Roach
7095a78cd34SGreg Roach    /**
7105a78cd34SGreg Roach     * @param Request $request
711b6db7c1fSGreg Roach     * @param Tree    $tree
7125a78cd34SGreg Roach     *
7135a78cd34SGreg Roach     * @return Response
7145a78cd34SGreg Roach     */
715b6db7c1fSGreg Roach    public function getAddNoteAction(Request $request, Tree $tree): Response
716c1010edaSGreg Roach    {
7175a78cd34SGreg Roach        $xref = $request->get('xref');
7185a78cd34SGreg Roach
7195a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
7205a78cd34SGreg Roach
7215a78cd34SGreg Roach        if ($note === null) {
722*59f2f229SGreg Roach            throw new NoteNotFoundException();
7235a78cd34SGreg Roach        }
7245a78cd34SGreg Roach
7255a78cd34SGreg Roach        $options = $this->noteOptions($note);
7265a78cd34SGreg Roach
7275a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $note->getFullName());
7285a78cd34SGreg Roach
7295a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
7305a78cd34SGreg Roach            'options' => $options,
7315a78cd34SGreg Roach            'default' => key($options),
7325a78cd34SGreg Roach            'record'  => $note,
7335a78cd34SGreg Roach            'title'   => $title,
7345a78cd34SGreg Roach            'tree'    => $tree,
7355a78cd34SGreg Roach        ]);
7365a78cd34SGreg Roach    }
7375a78cd34SGreg Roach
7385a78cd34SGreg Roach    /**
7395a78cd34SGreg Roach     * @param Note $note
7405a78cd34SGreg Roach     *
7415a78cd34SGreg Roach     * @return string[]
7425a78cd34SGreg Roach     */
743c1010edaSGreg Roach    private function noteOptions(Note $note): array
744c1010edaSGreg Roach    {
7455a78cd34SGreg Roach        $name = strip_tags($note->getFullName());
7465a78cd34SGreg Roach
7475a78cd34SGreg Roach        return [
7485a78cd34SGreg Roach            'self' => $name,
7495a78cd34SGreg Roach        ];
7505a78cd34SGreg Roach    }
7515a78cd34SGreg Roach
7525a78cd34SGreg Roach    /**
7535a78cd34SGreg Roach     * @param Request $request
754b6db7c1fSGreg Roach     * @param Tree    $tree
7555a78cd34SGreg Roach     *
7565a78cd34SGreg Roach     * @return RedirectResponse
7575a78cd34SGreg Roach     */
758b6db7c1fSGreg Roach    public function postAddNoteAction(Request $request, Tree $tree): RedirectResponse
759c1010edaSGreg Roach    {
7605a78cd34SGreg Roach        $xref = $request->get('xref');
7615a78cd34SGreg Roach
7625a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
7635a78cd34SGreg Roach
7645a78cd34SGreg Roach        if ($note === null) {
765*59f2f229SGreg Roach            throw new NoteNotFoundException();
7665a78cd34SGreg Roach        }
7675a78cd34SGreg Roach
7685a78cd34SGreg Roach        $this->addRecordToCart($note);
7695a78cd34SGreg Roach
7705a78cd34SGreg Roach        return new RedirectResponse($note->url());
7715a78cd34SGreg Roach    }
7725a78cd34SGreg Roach
7735a78cd34SGreg Roach    /**
7745a78cd34SGreg Roach     * @param Request $request
775b6db7c1fSGreg Roach     * @param Tree    $tree
7765a78cd34SGreg Roach     *
7775a78cd34SGreg Roach     * @return Response
7785a78cd34SGreg Roach     */
779b6db7c1fSGreg Roach    public function getAddRepositoryAction(Request $request, Tree $tree): Response
780c1010edaSGreg Roach    {
7815a78cd34SGreg Roach        $xref = $request->get('xref');
7825a78cd34SGreg Roach
7835a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
7845a78cd34SGreg Roach
7855a78cd34SGreg Roach        if ($repository === null) {
786*59f2f229SGreg Roach            throw new RepositoryNotFoundException();
7875a78cd34SGreg Roach        }
7885a78cd34SGreg Roach
7895a78cd34SGreg Roach        $options = $this->repositoryOptions($repository);
7905a78cd34SGreg Roach
7915a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $repository->getFullName());
7925a78cd34SGreg Roach
7935a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
7945a78cd34SGreg Roach            'options' => $options,
7955a78cd34SGreg Roach            'default' => key($options),
7965a78cd34SGreg Roach            'record'  => $repository,
7975a78cd34SGreg Roach            'title'   => $title,
7985a78cd34SGreg Roach            'tree'    => $tree,
7995a78cd34SGreg Roach        ]);
8005a78cd34SGreg Roach    }
8015a78cd34SGreg Roach
8025a78cd34SGreg Roach    /**
8035a78cd34SGreg Roach     * @param Repository $repository
8045a78cd34SGreg Roach     *
8055a78cd34SGreg Roach     * @return string[]
8065a78cd34SGreg Roach     */
807c1010edaSGreg Roach    private function repositoryOptions(Repository $repository): array
808c1010edaSGreg Roach    {
8095a78cd34SGreg Roach        $name = strip_tags($repository->getFullName());
8105a78cd34SGreg Roach
8115a78cd34SGreg Roach        return [
8125a78cd34SGreg Roach            'self' => $name,
8135a78cd34SGreg Roach        ];
8145a78cd34SGreg Roach    }
8155a78cd34SGreg Roach
8165a78cd34SGreg Roach    /**
8175a78cd34SGreg Roach     * @param Request $request
818b6db7c1fSGreg Roach     * @param Tree    $tree
8195a78cd34SGreg Roach     *
8205a78cd34SGreg Roach     * @return RedirectResponse
8215a78cd34SGreg Roach     */
822b6db7c1fSGreg Roach    public function postAddRepositoryAction(Request $request, Tree $tree): RedirectResponse
823c1010edaSGreg Roach    {
8245a78cd34SGreg Roach        $xref = $request->get('xref');
8255a78cd34SGreg Roach
8265a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
8275a78cd34SGreg Roach
8285a78cd34SGreg Roach        if ($repository === null) {
829*59f2f229SGreg Roach            throw new RepositoryNotFoundException();
8305a78cd34SGreg Roach        }
8315a78cd34SGreg Roach
8325a78cd34SGreg Roach        $this->addRecordToCart($repository);
8335a78cd34SGreg Roach
8345a78cd34SGreg Roach        return new RedirectResponse($repository->url());
8355a78cd34SGreg Roach    }
8365a78cd34SGreg Roach
8375a78cd34SGreg Roach    /**
8385a78cd34SGreg Roach     * @param Request $request
839b6db7c1fSGreg Roach     * @param Tree    $tree
8405a78cd34SGreg Roach     *
8415a78cd34SGreg Roach     * @return Response
8425a78cd34SGreg Roach     */
843b6db7c1fSGreg Roach    public function getAddSourceAction(Request $request, Tree $tree): Response
844c1010edaSGreg Roach    {
8455a78cd34SGreg Roach        $xref = $request->get('xref');
8465a78cd34SGreg Roach
8475a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
8485a78cd34SGreg Roach
8495a78cd34SGreg Roach        if ($source === null) {
850*59f2f229SGreg Roach            throw new SourceNotFoundException();
8515a78cd34SGreg Roach        }
8525a78cd34SGreg Roach
8535a78cd34SGreg Roach        $options = $this->sourceOptions($source);
8545a78cd34SGreg Roach
8555a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $source->getFullName());
8565a78cd34SGreg Roach
8575a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
8585a78cd34SGreg Roach            'options' => $options,
8595a78cd34SGreg Roach            'default' => key($options),
8605a78cd34SGreg Roach            'record'  => $source,
8615a78cd34SGreg Roach            'title'   => $title,
8625a78cd34SGreg Roach            'tree'    => $tree,
8635a78cd34SGreg Roach        ]);
8645a78cd34SGreg Roach    }
8655a78cd34SGreg Roach
8665a78cd34SGreg Roach    /**
8675a78cd34SGreg Roach     * @param Source $source
8685a78cd34SGreg Roach     *
8695a78cd34SGreg Roach     * @return string[]
8705a78cd34SGreg Roach     */
871c1010edaSGreg Roach    private function sourceOptions(Source $source): array
872c1010edaSGreg Roach    {
8735a78cd34SGreg Roach        $name = strip_tags($source->getFullName());
8745a78cd34SGreg Roach
8755a78cd34SGreg Roach        return [
8765a78cd34SGreg Roach            'only'   => strip_tags($source->getFullName()),
8775a78cd34SGreg Roach            'linked' => I18N::translate('%s and the individuals that reference it.', $name),
8785a78cd34SGreg Roach        ];
8795a78cd34SGreg Roach    }
8805a78cd34SGreg Roach
8815a78cd34SGreg Roach    /**
8825a78cd34SGreg Roach     * @param Request $request
883b6db7c1fSGreg Roach     * @param Tree    $tree
8845a78cd34SGreg Roach     *
8855a78cd34SGreg Roach     * @return RedirectResponse
8865a78cd34SGreg Roach     */
887b6db7c1fSGreg Roach    public function postAddSourceAction(Request $request, Tree $tree): RedirectResponse
888c1010edaSGreg Roach    {
8895a78cd34SGreg Roach        $xref   = $request->get('xref');
8905a78cd34SGreg Roach        $option = $request->get('option');
8915a78cd34SGreg Roach
8925a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
8935a78cd34SGreg Roach
8945a78cd34SGreg Roach        if ($source === null) {
895*59f2f229SGreg Roach            throw new SourceNotFoundException();
8965a78cd34SGreg Roach        }
8975a78cd34SGreg Roach
8985a78cd34SGreg Roach        $this->addRecordToCart($source);
8995a78cd34SGreg Roach
9005a78cd34SGreg Roach        if ($option === 'linked') {
9015a78cd34SGreg Roach            foreach ($source->linkedIndividuals('SOUR') as $individual) {
9025a78cd34SGreg Roach                $this->addRecordToCart($individual);
9035a78cd34SGreg Roach            }
9045a78cd34SGreg Roach            foreach ($source->linkedFamilies('SOUR') as $family) {
9055a78cd34SGreg Roach                $this->addRecordToCart($family);
9065a78cd34SGreg Roach            }
9075a78cd34SGreg Roach        }
9085a78cd34SGreg Roach
9095a78cd34SGreg Roach        return new RedirectResponse($source->url());
9105a78cd34SGreg Roach    }
9115a78cd34SGreg Roach
9125a78cd34SGreg Roach    /**
9135a78cd34SGreg Roach     * Get all the records in the cart.
9145a78cd34SGreg Roach     *
9155a78cd34SGreg Roach     * @param Tree $tree
9165a78cd34SGreg Roach     *
9175a78cd34SGreg Roach     * @return GedcomRecord[]
9185a78cd34SGreg Roach     */
919c1010edaSGreg Roach    private function allRecordsInCart(Tree $tree): array
920c1010edaSGreg Roach    {
9215a78cd34SGreg Roach        $cart = Session::get('cart', []);
9225a78cd34SGreg Roach
9235a78cd34SGreg Roach        $xrefs = array_keys($cart[$tree->getName()] ?? []);
9245a78cd34SGreg Roach
9255a78cd34SGreg Roach        // Fetch all the records in the cart.
9265a78cd34SGreg Roach        $records = array_map(function (string $xref) use ($tree) {
9275a78cd34SGreg Roach            return GedcomRecord::getInstance($xref, $tree);
9285a78cd34SGreg Roach        }, $xrefs);
9295a78cd34SGreg Roach
9305a78cd34SGreg Roach        // Some records may have been deleted after they were added to the cart.
9315a78cd34SGreg Roach        $records = array_filter($records);
9325a78cd34SGreg Roach
9335a78cd34SGreg Roach        // Group and sort.
9345a78cd34SGreg Roach        uasort($records, function (GedcomRecord $x, GedcomRecord $y) {
9355a78cd34SGreg Roach            return $x::RECORD_TYPE <=> $y::RECORD_TYPE ?: GedcomRecord::compare($x, $y);
9365a78cd34SGreg Roach        });
9375a78cd34SGreg Roach
9385a78cd34SGreg Roach        return $records;
9395a78cd34SGreg Roach    }
9405a78cd34SGreg Roach
9415a78cd34SGreg Roach    /**
9425a78cd34SGreg Roach     * Add a record (and direclty linked sources, notes, etc. to the cart.
9435a78cd34SGreg Roach     *
9445a78cd34SGreg Roach     * @param GedcomRecord $record
9455a78cd34SGreg Roach     */
946c1010edaSGreg Roach    private function addRecordToCart(GedcomRecord $record)
947c1010edaSGreg Roach    {
9485a78cd34SGreg Roach        $cart = Session::get('cart', []);
9495a78cd34SGreg Roach
9505a78cd34SGreg Roach        $tree_name = $record->getTree()->getName();
9515a78cd34SGreg Roach
9525a78cd34SGreg Roach        // Add this record
9535a78cd34SGreg Roach        $cart[$tree_name][$record->getXref()] = true;
9545a78cd34SGreg Roach
9555a78cd34SGreg Roach        // Add directly linked media, notes, repositories and sources.
9565a78cd34SGreg Roach        preg_match_all('/\n\d (?:OBJE|NOTE|SOUR|REPO) @(' . WT_REGEX_XREF . ')@/', $record->getGedcom(), $matches);
9575a78cd34SGreg Roach
9585a78cd34SGreg Roach        foreach ($matches[1] as $match) {
9595a78cd34SGreg Roach            $cart[$tree_name][$match] = true;
9605a78cd34SGreg Roach        }
9615a78cd34SGreg Roach
9625a78cd34SGreg Roach        Session::put('cart', $cart);
9635a78cd34SGreg Roach    }
9645a78cd34SGreg Roach
9655a78cd34SGreg Roach    /**
9665a78cd34SGreg Roach     * @param Tree $tree
9675a78cd34SGreg Roach     *
9685a78cd34SGreg Roach     * @return bool
9695a78cd34SGreg Roach     */
970c1010edaSGreg Roach    private function isCartEmpty(Tree $tree): bool
971c1010edaSGreg Roach    {
9725a78cd34SGreg Roach        $cart = Session::get('cart', []);
9735a78cd34SGreg Roach
9745a78cd34SGreg Roach        return empty($cart[$tree->getName()]);
9755a78cd34SGreg Roach    }
9765a78cd34SGreg Roach
9775a78cd34SGreg Roach    /**
9785a78cd34SGreg Roach     * Only allow access to the routes/functions if the menu is active
9795a78cd34SGreg Roach     *
9805a78cd34SGreg Roach     * @param Tree $tree
9815a78cd34SGreg Roach     */
982c1010edaSGreg Roach    private function checkModuleAccess(Tree $tree)
983c1010edaSGreg Roach    {
9845a78cd34SGreg Roach        if (!array_key_exists($this->getName(), Module::getActiveMenus($tree))) {
985*59f2f229SGreg Roach            throw new NotFoundHttpException();
9865a78cd34SGreg Roach        }
9878c2e8227SGreg Roach    }
9888c2e8227SGreg Roach}
989