xref: /webtrees/app/Module/ClippingsCartModule.php (revision 18d7a90d8a3b33b218801c0b68eb1a5140d7b4e7)
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} */
668f53f488SRico Sonntag    public function getTitle(): string
67c1010edaSGreg Roach    {
68bbb76c12SGreg Roach        /* I18N: Name of a module */
69bbb76c12SGreg Roach        return I18N::translate('Clippings cart');
708c2e8227SGreg Roach    }
718c2e8227SGreg Roach
728c2e8227SGreg Roach    /** {@inheritdoc} */
738f53f488SRico Sonntag    public function getDescription(): string
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     */
868f53f488SRico Sonntag    public function defaultAccessLevel(): int
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     */
968f53f488SRico Sonntag    public function defaultMenuOrder(): int
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) {
39459f2f229SGreg 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) {
44259f2f229SGreg 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
464*18d7a90dSGreg Roach     *
465*18d7a90dSGreg Roach     * @return void
4665a78cd34SGreg Roach     */
467c1010edaSGreg Roach    private function addFamilyToCart(Family $family)
468c1010edaSGreg Roach    {
4695a78cd34SGreg Roach        $this->addRecordToCart($family);
4705a78cd34SGreg Roach
4715a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
4725a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4735a78cd34SGreg Roach        }
4745a78cd34SGreg Roach    }
4755a78cd34SGreg Roach
4765a78cd34SGreg Roach    /**
4775a78cd34SGreg Roach     * @param Family $family
478*18d7a90dSGreg Roach     *
479*18d7a90dSGreg Roach     * @return void
4805a78cd34SGreg Roach     */
481c1010edaSGreg Roach    private function addFamilyAndChildrenToCart(Family $family)
482c1010edaSGreg Roach    {
4835a78cd34SGreg Roach        $this->addRecordToCart($family);
4845a78cd34SGreg Roach
4855a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
4865a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4875a78cd34SGreg Roach        }
4885a78cd34SGreg Roach        foreach ($family->getChildren() as $child) {
4895a78cd34SGreg Roach            $this->addRecordToCart($child);
4905a78cd34SGreg Roach        }
4915a78cd34SGreg Roach    }
4925a78cd34SGreg Roach
4935a78cd34SGreg Roach    /**
4945a78cd34SGreg Roach     * @param Family $family
495*18d7a90dSGreg Roach     *
496*18d7a90dSGreg Roach     * @return void
4975a78cd34SGreg Roach     */
498c1010edaSGreg Roach    private function addFamilyAndDescendantsToCart(Family $family)
499c1010edaSGreg Roach    {
5005a78cd34SGreg Roach        $this->addRecordToCart($family);
5015a78cd34SGreg Roach
5025a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
5035a78cd34SGreg Roach            $this->addRecordToCart($spouse);
5045a78cd34SGreg Roach        }
5055a78cd34SGreg Roach        foreach ($family->getChildren() as $child) {
5065a78cd34SGreg Roach            $this->addRecordToCart($child);
5075a78cd34SGreg Roach            foreach ($child->getSpouseFamilies() as $child_family) {
5085a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($child_family);
5095a78cd34SGreg Roach            }
5105a78cd34SGreg Roach        }
5115a78cd34SGreg Roach    }
5125a78cd34SGreg Roach
5135a78cd34SGreg Roach    /**
5145a78cd34SGreg Roach     * @param Request $request
515b6db7c1fSGreg Roach     * @param Tree    $tree
5165a78cd34SGreg Roach     *
5175a78cd34SGreg Roach     * @return Response
5185a78cd34SGreg Roach     */
519b6db7c1fSGreg Roach    public function getAddIndividualAction(Request $request, Tree $tree): Response
520c1010edaSGreg Roach    {
5215a78cd34SGreg Roach        $xref = $request->get('xref');
5225a78cd34SGreg Roach
5235a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
5245a78cd34SGreg Roach
5255a78cd34SGreg Roach        if ($individual === null) {
52659f2f229SGreg Roach            throw new IndividualNotFoundException();
5275a78cd34SGreg Roach        }
5285a78cd34SGreg Roach
5295a78cd34SGreg Roach        $options = $this->individualOptions($individual);
5305a78cd34SGreg Roach
5315a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $individual->getFullName());
5325a78cd34SGreg Roach
5335a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
5345a78cd34SGreg Roach            'options' => $options,
5355a78cd34SGreg Roach            'default' => key($options),
5365a78cd34SGreg Roach            'record'  => $individual,
5375a78cd34SGreg Roach            'title'   => $title,
5385a78cd34SGreg Roach            'tree'    => $tree,
5395a78cd34SGreg Roach        ]);
5405a78cd34SGreg Roach    }
5415a78cd34SGreg Roach
5425a78cd34SGreg Roach    /**
5435a78cd34SGreg Roach     * @param Individual $individual
5445a78cd34SGreg Roach     *
5455a78cd34SGreg Roach     * @return string[]
5465a78cd34SGreg Roach     */
547c1010edaSGreg Roach    private function individualOptions(Individual $individual): array
548c1010edaSGreg Roach    {
5495a78cd34SGreg Roach        $name = strip_tags($individual->getFullName());
5505a78cd34SGreg Roach
5515a78cd34SGreg Roach        if ($individual->getSex() === 'F') {
5525a78cd34SGreg Roach            return [
5535a78cd34SGreg Roach                'self'              => $name,
5545a78cd34SGreg Roach                'parents'           => I18N::translate('%s, her parents and siblings', $name),
5555a78cd34SGreg Roach                'spouses'           => I18N::translate('%s, her spouses and children', $name),
5565a78cd34SGreg Roach                'ancestors'         => I18N::translate('%s and her ancestors', $name),
5575a78cd34SGreg Roach                'ancestor_families' => I18N::translate('%s, her ancestors and their families', $name),
5585a78cd34SGreg Roach                'descendants'       => I18N::translate('%s, her spouses and descendants', $name),
5595a78cd34SGreg Roach            ];
560b2ce94c6SRico Sonntag        }
561b2ce94c6SRico Sonntag
5625a78cd34SGreg Roach        return [
5635a78cd34SGreg Roach            'self'              => $name,
5645a78cd34SGreg Roach            'parents'           => I18N::translate('%s, his parents and siblings', $name),
5655a78cd34SGreg Roach            'spouses'           => I18N::translate('%s, his spouses and children', $name),
5665a78cd34SGreg Roach            'ancestors'         => I18N::translate('%s and his ancestors', $name),
5675a78cd34SGreg Roach            'ancestor_families' => I18N::translate('%s, his ancestors and their families', $name),
5685a78cd34SGreg Roach            'descendants'       => I18N::translate('%s, his spouses and descendants', $name),
5695a78cd34SGreg Roach        ];
5705a78cd34SGreg Roach    }
5715a78cd34SGreg Roach
5725a78cd34SGreg Roach    /**
5735a78cd34SGreg Roach     * @param Request $request
574b6db7c1fSGreg Roach     * @param Tree    $tree
5755a78cd34SGreg Roach     *
5765a78cd34SGreg Roach     * @return RedirectResponse
5775a78cd34SGreg Roach     */
578b6db7c1fSGreg Roach    public function postAddIndividualAction(Request $request, Tree $tree): RedirectResponse
579c1010edaSGreg Roach    {
5805a78cd34SGreg Roach        $xref   = $request->get('xref');
5815a78cd34SGreg Roach        $option = $request->get('option');
5825a78cd34SGreg Roach
5835a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
5845a78cd34SGreg Roach
5855a78cd34SGreg Roach        if ($individual === null) {
58659f2f229SGreg Roach            throw new IndividualNotFoundException();
5875a78cd34SGreg Roach        }
5885a78cd34SGreg Roach
5895a78cd34SGreg Roach        switch ($option) {
5905a78cd34SGreg Roach            case 'self':
5915a78cd34SGreg Roach                $this->addRecordToCart($individual);
5925a78cd34SGreg Roach                break;
5935a78cd34SGreg Roach
5945a78cd34SGreg Roach            case 'parents':
5955a78cd34SGreg Roach                foreach ($individual->getChildFamilies() as $family) {
5965a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
5975a78cd34SGreg Roach                }
5985a78cd34SGreg Roach                break;
5995a78cd34SGreg Roach
6005a78cd34SGreg Roach            case 'spouses':
6015a78cd34SGreg Roach                foreach ($individual->getSpouseFamilies() as $family) {
6025a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
6035a78cd34SGreg Roach                }
6045a78cd34SGreg Roach                break;
6055a78cd34SGreg Roach
6065a78cd34SGreg Roach            case 'ancestors':
6075a78cd34SGreg Roach                $this->addAncestorsToCart($individual);
6085a78cd34SGreg Roach                break;
6095a78cd34SGreg Roach
6105a78cd34SGreg Roach            case 'ancestor_families':
6115a78cd34SGreg Roach                $this->addAncestorFamiliesToCart($individual);
6125a78cd34SGreg Roach                break;
6135a78cd34SGreg Roach
6145a78cd34SGreg Roach            case 'descendants':
6155a78cd34SGreg Roach                foreach ($individual->getSpouseFamilies() as $family) {
6165a78cd34SGreg Roach                    $this->addFamilyAndDescendantsToCart($family);
6175a78cd34SGreg Roach                }
6185a78cd34SGreg Roach                break;
6195a78cd34SGreg Roach        }
6205a78cd34SGreg Roach
6215a78cd34SGreg Roach        return new RedirectResponse($individual->url());
6225a78cd34SGreg Roach    }
6235a78cd34SGreg Roach
6245a78cd34SGreg Roach    /**
6255a78cd34SGreg Roach     * @param Individual $individual
626*18d7a90dSGreg Roach     *
627*18d7a90dSGreg Roach     * @return void
6285a78cd34SGreg Roach     */
629c1010edaSGreg Roach    private function addAncestorsToCart(Individual $individual)
630c1010edaSGreg Roach    {
6315a78cd34SGreg Roach        $this->addRecordToCart($individual);
6325a78cd34SGreg Roach
6335a78cd34SGreg Roach        foreach ($individual->getChildFamilies() as $family) {
6345a78cd34SGreg Roach            foreach ($family->getSpouses() as $parent) {
6355a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
6365a78cd34SGreg Roach            }
6375a78cd34SGreg Roach        }
6385a78cd34SGreg Roach    }
6395a78cd34SGreg Roach
6405a78cd34SGreg Roach    /**
6415a78cd34SGreg Roach     * @param Individual $individual
642*18d7a90dSGreg Roach     *
643*18d7a90dSGreg Roach     * @return void
6445a78cd34SGreg Roach     */
645c1010edaSGreg Roach    private function addAncestorFamiliesToCart(Individual $individual)
646c1010edaSGreg Roach    {
6475a78cd34SGreg Roach        foreach ($individual->getChildFamilies() as $family) {
6485a78cd34SGreg Roach            $this->addFamilyAndChildrenToCart($family);
6495a78cd34SGreg Roach            foreach ($family->getSpouses() as $parent) {
6505a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
6515a78cd34SGreg Roach            }
6525a78cd34SGreg Roach        }
6535a78cd34SGreg Roach    }
6545a78cd34SGreg Roach
6555a78cd34SGreg Roach    /**
6565a78cd34SGreg Roach     * @param Request $request
657b6db7c1fSGreg Roach     * @param Tree    $tree
6585a78cd34SGreg Roach     *
6595a78cd34SGreg Roach     * @return Response
6605a78cd34SGreg Roach     */
661b6db7c1fSGreg Roach    public function getAddMediaAction(Request $request, Tree $tree): Response
662c1010edaSGreg Roach    {
6635a78cd34SGreg Roach        $xref = $request->get('xref');
6645a78cd34SGreg Roach
6655a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
6665a78cd34SGreg Roach
6675a78cd34SGreg Roach        if ($media === null) {
66859f2f229SGreg Roach            throw new MediaNotFoundException();
6695a78cd34SGreg Roach        }
6705a78cd34SGreg Roach
6715a78cd34SGreg Roach        $options = $this->mediaOptions($media);
6725a78cd34SGreg Roach
6735a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $media->getFullName());
6745a78cd34SGreg Roach
6755a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
6765a78cd34SGreg Roach            'options' => $options,
6775a78cd34SGreg Roach            'default' => key($options),
6785a78cd34SGreg Roach            'record'  => $media,
6795a78cd34SGreg Roach            'title'   => $title,
6805a78cd34SGreg Roach            'tree'    => $tree,
6815a78cd34SGreg Roach        ]);
6825a78cd34SGreg Roach    }
6835a78cd34SGreg Roach
6845a78cd34SGreg Roach    /**
6855a78cd34SGreg Roach     * @param Media $media
6865a78cd34SGreg Roach     *
6875a78cd34SGreg Roach     * @return string[]
6885a78cd34SGreg Roach     */
689c1010edaSGreg Roach    private function mediaOptions(Media $media): array
690c1010edaSGreg Roach    {
6915a78cd34SGreg Roach        $name = strip_tags($media->getFullName());
6925a78cd34SGreg Roach
6935a78cd34SGreg Roach        return [
6945a78cd34SGreg Roach            'self' => $name,
6955a78cd34SGreg Roach        ];
6965a78cd34SGreg Roach    }
6975a78cd34SGreg Roach
6985a78cd34SGreg Roach    /**
6995a78cd34SGreg Roach     * @param Request $request
700b6db7c1fSGreg Roach     * @param Tree    $tree
7015a78cd34SGreg Roach     *
7025a78cd34SGreg Roach     * @return RedirectResponse
7035a78cd34SGreg Roach     */
704b6db7c1fSGreg Roach    public function postAddMediaAction(Request $request, Tree $tree): RedirectResponse
705c1010edaSGreg Roach    {
7065a78cd34SGreg Roach        $xref = $request->get('xref');
7075a78cd34SGreg Roach
7085a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
7095a78cd34SGreg Roach
7105a78cd34SGreg Roach        if ($media === null) {
71159f2f229SGreg Roach            throw new MediaNotFoundException();
7125a78cd34SGreg Roach        }
7135a78cd34SGreg Roach
7145a78cd34SGreg Roach        $this->addRecordToCart($media);
7155a78cd34SGreg Roach
7165a78cd34SGreg Roach        return new RedirectResponse($media->url());
7175a78cd34SGreg Roach    }
7185a78cd34SGreg Roach
7195a78cd34SGreg Roach    /**
7205a78cd34SGreg Roach     * @param Request $request
721b6db7c1fSGreg Roach     * @param Tree    $tree
7225a78cd34SGreg Roach     *
7235a78cd34SGreg Roach     * @return Response
7245a78cd34SGreg Roach     */
725b6db7c1fSGreg Roach    public function getAddNoteAction(Request $request, Tree $tree): Response
726c1010edaSGreg Roach    {
7275a78cd34SGreg Roach        $xref = $request->get('xref');
7285a78cd34SGreg Roach
7295a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
7305a78cd34SGreg Roach
7315a78cd34SGreg Roach        if ($note === null) {
73259f2f229SGreg Roach            throw new NoteNotFoundException();
7335a78cd34SGreg Roach        }
7345a78cd34SGreg Roach
7355a78cd34SGreg Roach        $options = $this->noteOptions($note);
7365a78cd34SGreg Roach
7375a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $note->getFullName());
7385a78cd34SGreg Roach
7395a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
7405a78cd34SGreg Roach            'options' => $options,
7415a78cd34SGreg Roach            'default' => key($options),
7425a78cd34SGreg Roach            'record'  => $note,
7435a78cd34SGreg Roach            'title'   => $title,
7445a78cd34SGreg Roach            'tree'    => $tree,
7455a78cd34SGreg Roach        ]);
7465a78cd34SGreg Roach    }
7475a78cd34SGreg Roach
7485a78cd34SGreg Roach    /**
7495a78cd34SGreg Roach     * @param Note $note
7505a78cd34SGreg Roach     *
7515a78cd34SGreg Roach     * @return string[]
7525a78cd34SGreg Roach     */
753c1010edaSGreg Roach    private function noteOptions(Note $note): array
754c1010edaSGreg Roach    {
7555a78cd34SGreg Roach        $name = strip_tags($note->getFullName());
7565a78cd34SGreg Roach
7575a78cd34SGreg Roach        return [
7585a78cd34SGreg Roach            'self' => $name,
7595a78cd34SGreg Roach        ];
7605a78cd34SGreg Roach    }
7615a78cd34SGreg Roach
7625a78cd34SGreg Roach    /**
7635a78cd34SGreg Roach     * @param Request $request
764b6db7c1fSGreg Roach     * @param Tree    $tree
7655a78cd34SGreg Roach     *
7665a78cd34SGreg Roach     * @return RedirectResponse
7675a78cd34SGreg Roach     */
768b6db7c1fSGreg Roach    public function postAddNoteAction(Request $request, Tree $tree): RedirectResponse
769c1010edaSGreg Roach    {
7705a78cd34SGreg Roach        $xref = $request->get('xref');
7715a78cd34SGreg Roach
7725a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
7735a78cd34SGreg Roach
7745a78cd34SGreg Roach        if ($note === null) {
77559f2f229SGreg Roach            throw new NoteNotFoundException();
7765a78cd34SGreg Roach        }
7775a78cd34SGreg Roach
7785a78cd34SGreg Roach        $this->addRecordToCart($note);
7795a78cd34SGreg Roach
7805a78cd34SGreg Roach        return new RedirectResponse($note->url());
7815a78cd34SGreg Roach    }
7825a78cd34SGreg Roach
7835a78cd34SGreg Roach    /**
7845a78cd34SGreg Roach     * @param Request $request
785b6db7c1fSGreg Roach     * @param Tree    $tree
7865a78cd34SGreg Roach     *
7875a78cd34SGreg Roach     * @return Response
7885a78cd34SGreg Roach     */
789b6db7c1fSGreg Roach    public function getAddRepositoryAction(Request $request, Tree $tree): Response
790c1010edaSGreg Roach    {
7915a78cd34SGreg Roach        $xref = $request->get('xref');
7925a78cd34SGreg Roach
7935a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
7945a78cd34SGreg Roach
7955a78cd34SGreg Roach        if ($repository === null) {
79659f2f229SGreg Roach            throw new RepositoryNotFoundException();
7975a78cd34SGreg Roach        }
7985a78cd34SGreg Roach
7995a78cd34SGreg Roach        $options = $this->repositoryOptions($repository);
8005a78cd34SGreg Roach
8015a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $repository->getFullName());
8025a78cd34SGreg Roach
8035a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
8045a78cd34SGreg Roach            'options' => $options,
8055a78cd34SGreg Roach            'default' => key($options),
8065a78cd34SGreg Roach            'record'  => $repository,
8075a78cd34SGreg Roach            'title'   => $title,
8085a78cd34SGreg Roach            'tree'    => $tree,
8095a78cd34SGreg Roach        ]);
8105a78cd34SGreg Roach    }
8115a78cd34SGreg Roach
8125a78cd34SGreg Roach    /**
8135a78cd34SGreg Roach     * @param Repository $repository
8145a78cd34SGreg Roach     *
8155a78cd34SGreg Roach     * @return string[]
8165a78cd34SGreg Roach     */
817c1010edaSGreg Roach    private function repositoryOptions(Repository $repository): array
818c1010edaSGreg Roach    {
8195a78cd34SGreg Roach        $name = strip_tags($repository->getFullName());
8205a78cd34SGreg Roach
8215a78cd34SGreg Roach        return [
8225a78cd34SGreg Roach            'self' => $name,
8235a78cd34SGreg Roach        ];
8245a78cd34SGreg Roach    }
8255a78cd34SGreg Roach
8265a78cd34SGreg Roach    /**
8275a78cd34SGreg Roach     * @param Request $request
828b6db7c1fSGreg Roach     * @param Tree    $tree
8295a78cd34SGreg Roach     *
8305a78cd34SGreg Roach     * @return RedirectResponse
8315a78cd34SGreg Roach     */
832b6db7c1fSGreg Roach    public function postAddRepositoryAction(Request $request, Tree $tree): RedirectResponse
833c1010edaSGreg Roach    {
8345a78cd34SGreg Roach        $xref = $request->get('xref');
8355a78cd34SGreg Roach
8365a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
8375a78cd34SGreg Roach
8385a78cd34SGreg Roach        if ($repository === null) {
83959f2f229SGreg Roach            throw new RepositoryNotFoundException();
8405a78cd34SGreg Roach        }
8415a78cd34SGreg Roach
8425a78cd34SGreg Roach        $this->addRecordToCart($repository);
8435a78cd34SGreg Roach
8445a78cd34SGreg Roach        return new RedirectResponse($repository->url());
8455a78cd34SGreg Roach    }
8465a78cd34SGreg Roach
8475a78cd34SGreg Roach    /**
8485a78cd34SGreg Roach     * @param Request $request
849b6db7c1fSGreg Roach     * @param Tree    $tree
8505a78cd34SGreg Roach     *
8515a78cd34SGreg Roach     * @return Response
8525a78cd34SGreg Roach     */
853b6db7c1fSGreg Roach    public function getAddSourceAction(Request $request, Tree $tree): Response
854c1010edaSGreg Roach    {
8555a78cd34SGreg Roach        $xref = $request->get('xref');
8565a78cd34SGreg Roach
8575a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
8585a78cd34SGreg Roach
8595a78cd34SGreg Roach        if ($source === null) {
86059f2f229SGreg Roach            throw new SourceNotFoundException();
8615a78cd34SGreg Roach        }
8625a78cd34SGreg Roach
8635a78cd34SGreg Roach        $options = $this->sourceOptions($source);
8645a78cd34SGreg Roach
8655a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $source->getFullName());
8665a78cd34SGreg Roach
8675a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
8685a78cd34SGreg Roach            'options' => $options,
8695a78cd34SGreg Roach            'default' => key($options),
8705a78cd34SGreg Roach            'record'  => $source,
8715a78cd34SGreg Roach            'title'   => $title,
8725a78cd34SGreg Roach            'tree'    => $tree,
8735a78cd34SGreg Roach        ]);
8745a78cd34SGreg Roach    }
8755a78cd34SGreg Roach
8765a78cd34SGreg Roach    /**
8775a78cd34SGreg Roach     * @param Source $source
8785a78cd34SGreg Roach     *
8795a78cd34SGreg Roach     * @return string[]
8805a78cd34SGreg Roach     */
881c1010edaSGreg Roach    private function sourceOptions(Source $source): array
882c1010edaSGreg Roach    {
8835a78cd34SGreg Roach        $name = strip_tags($source->getFullName());
8845a78cd34SGreg Roach
8855a78cd34SGreg Roach        return [
8865a78cd34SGreg Roach            'only'   => strip_tags($source->getFullName()),
8875a78cd34SGreg Roach            'linked' => I18N::translate('%s and the individuals that reference it.', $name),
8885a78cd34SGreg Roach        ];
8895a78cd34SGreg Roach    }
8905a78cd34SGreg Roach
8915a78cd34SGreg Roach    /**
8925a78cd34SGreg Roach     * @param Request $request
893b6db7c1fSGreg Roach     * @param Tree    $tree
8945a78cd34SGreg Roach     *
8955a78cd34SGreg Roach     * @return RedirectResponse
8965a78cd34SGreg Roach     */
897b6db7c1fSGreg Roach    public function postAddSourceAction(Request $request, Tree $tree): RedirectResponse
898c1010edaSGreg Roach    {
8995a78cd34SGreg Roach        $xref   = $request->get('xref');
9005a78cd34SGreg Roach        $option = $request->get('option');
9015a78cd34SGreg Roach
9025a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
9035a78cd34SGreg Roach
9045a78cd34SGreg Roach        if ($source === null) {
90559f2f229SGreg Roach            throw new SourceNotFoundException();
9065a78cd34SGreg Roach        }
9075a78cd34SGreg Roach
9085a78cd34SGreg Roach        $this->addRecordToCart($source);
9095a78cd34SGreg Roach
9105a78cd34SGreg Roach        if ($option === 'linked') {
9115a78cd34SGreg Roach            foreach ($source->linkedIndividuals('SOUR') as $individual) {
9125a78cd34SGreg Roach                $this->addRecordToCart($individual);
9135a78cd34SGreg Roach            }
9145a78cd34SGreg Roach            foreach ($source->linkedFamilies('SOUR') as $family) {
9155a78cd34SGreg Roach                $this->addRecordToCart($family);
9165a78cd34SGreg Roach            }
9175a78cd34SGreg Roach        }
9185a78cd34SGreg Roach
9195a78cd34SGreg Roach        return new RedirectResponse($source->url());
9205a78cd34SGreg Roach    }
9215a78cd34SGreg Roach
9225a78cd34SGreg Roach    /**
9235a78cd34SGreg Roach     * Get all the records in the cart.
9245a78cd34SGreg Roach     *
9255a78cd34SGreg Roach     * @param Tree $tree
9265a78cd34SGreg Roach     *
9275a78cd34SGreg Roach     * @return GedcomRecord[]
9285a78cd34SGreg Roach     */
929c1010edaSGreg Roach    private function allRecordsInCart(Tree $tree): array
930c1010edaSGreg Roach    {
9315a78cd34SGreg Roach        $cart = Session::get('cart', []);
9325a78cd34SGreg Roach
9335a78cd34SGreg Roach        $xrefs = array_keys($cart[$tree->getName()] ?? []);
9345a78cd34SGreg Roach
9355a78cd34SGreg Roach        // Fetch all the records in the cart.
936*18d7a90dSGreg Roach        $records = array_map(function (string $xref) use ($tree): GedcomRecord {
9375a78cd34SGreg Roach            return GedcomRecord::getInstance($xref, $tree);
9385a78cd34SGreg Roach        }, $xrefs);
9395a78cd34SGreg Roach
9405a78cd34SGreg Roach        // Some records may have been deleted after they were added to the cart.
9415a78cd34SGreg Roach        $records = array_filter($records);
9425a78cd34SGreg Roach
9435a78cd34SGreg Roach        // Group and sort.
944*18d7a90dSGreg Roach        uasort($records, function (GedcomRecord $x, GedcomRecord $y): int {
9455a78cd34SGreg Roach            return $x::RECORD_TYPE <=> $y::RECORD_TYPE ?: GedcomRecord::compare($x, $y);
9465a78cd34SGreg Roach        });
9475a78cd34SGreg Roach
9485a78cd34SGreg Roach        return $records;
9495a78cd34SGreg Roach    }
9505a78cd34SGreg Roach
9515a78cd34SGreg Roach    /**
9525a78cd34SGreg Roach     * Add a record (and direclty linked sources, notes, etc. to the cart.
9535a78cd34SGreg Roach     *
9545a78cd34SGreg Roach     * @param GedcomRecord $record
955*18d7a90dSGreg Roach     *
956*18d7a90dSGreg Roach     * @return void
9575a78cd34SGreg Roach     */
958c1010edaSGreg Roach    private function addRecordToCart(GedcomRecord $record)
959c1010edaSGreg Roach    {
9605a78cd34SGreg Roach        $cart = Session::get('cart', []);
9615a78cd34SGreg Roach
9625a78cd34SGreg Roach        $tree_name = $record->getTree()->getName();
9635a78cd34SGreg Roach
9645a78cd34SGreg Roach        // Add this record
9655a78cd34SGreg Roach        $cart[$tree_name][$record->getXref()] = true;
9665a78cd34SGreg Roach
9675a78cd34SGreg Roach        // Add directly linked media, notes, repositories and sources.
9685a78cd34SGreg Roach        preg_match_all('/\n\d (?:OBJE|NOTE|SOUR|REPO) @(' . WT_REGEX_XREF . ')@/', $record->getGedcom(), $matches);
9695a78cd34SGreg Roach
9705a78cd34SGreg Roach        foreach ($matches[1] as $match) {
9715a78cd34SGreg Roach            $cart[$tree_name][$match] = true;
9725a78cd34SGreg Roach        }
9735a78cd34SGreg Roach
9745a78cd34SGreg Roach        Session::put('cart', $cart);
9755a78cd34SGreg Roach    }
9765a78cd34SGreg Roach
9775a78cd34SGreg Roach    /**
9785a78cd34SGreg Roach     * @param Tree $tree
9795a78cd34SGreg Roach     *
9805a78cd34SGreg Roach     * @return bool
9815a78cd34SGreg Roach     */
982c1010edaSGreg Roach    private function isCartEmpty(Tree $tree): bool
983c1010edaSGreg Roach    {
9845a78cd34SGreg Roach        $cart = Session::get('cart', []);
9855a78cd34SGreg Roach
9865a78cd34SGreg Roach        return empty($cart[$tree->getName()]);
9875a78cd34SGreg Roach    }
9885a78cd34SGreg Roach
9895a78cd34SGreg Roach    /**
9905a78cd34SGreg Roach     * Only allow access to the routes/functions if the menu is active
9915a78cd34SGreg Roach     *
9925a78cd34SGreg Roach     * @param Tree $tree
993*18d7a90dSGreg Roach     *
994*18d7a90dSGreg Roach     * @return void
995*18d7a90dSGreg Roach     *
996*18d7a90dSGreg Roach     * @throws NoteNotFoundException
9975a78cd34SGreg Roach     */
998c1010edaSGreg Roach    private function checkModuleAccess(Tree $tree)
999c1010edaSGreg Roach    {
10005a78cd34SGreg Roach        if (!array_key_exists($this->getName(), Module::getActiveMenus($tree))) {
100159f2f229SGreg Roach            throw new NotFoundHttpException();
10025a78cd34SGreg Roach        }
10038c2e8227SGreg Roach    }
10048c2e8227SGreg Roach}
1005