xref: /webtrees/app/Module/ClippingsCartModule.php (revision 49a243cb5fe7c21b24e262552d556b018bfe3f41)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
210bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\FamilyNotFoundException;
220bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualNotFoundException;
230bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\MediaNotFoundException;
240bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\NoteNotFoundException;
250bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\RepositoryNotFoundException;
260bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\SourceNotFoundException;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Family;
285a78cd34SGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsExport;
295a78cd34SGreg Roachuse Fisharebest\Webtrees\Gedcom;
300e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
310e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
320e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
335a78cd34SGreg Roachuse Fisharebest\Webtrees\Media;
340e62c4b8SGreg Roachuse Fisharebest\Webtrees\Menu;
351eca16b3SGreg Roachuse Fisharebest\Webtrees\Module;
365a78cd34SGreg Roachuse Fisharebest\Webtrees\Note;
375a78cd34SGreg Roachuse Fisharebest\Webtrees\Repository;
380e62c4b8SGreg Roachuse Fisharebest\Webtrees\Session;
395a78cd34SGreg Roachuse Fisharebest\Webtrees\Source;
40aee13b6dSGreg Roachuse Fisharebest\Webtrees\Tree;
415a78cd34SGreg Roachuse Fisharebest\Webtrees\User;
425a78cd34SGreg Roachuse League\Flysystem\Filesystem;
435a78cd34SGreg Roachuse League\Flysystem\ZipArchive\ZipArchiveAdapter;
44a3d8780cSGreg Roachuse function str_replace;
455a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\BinaryFileResponse;
465a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse;
475a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\Request;
485a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\Response;
495a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\ResponseHeaderBag;
505a78cd34SGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
518c2e8227SGreg Roach
528c2e8227SGreg Roach/**
538c2e8227SGreg Roach * Class ClippingsCartModule
548c2e8227SGreg Roach */
55*49a243cbSGreg Roachclass ClippingsCartModule extends AbstractModule implements ModuleInterface, ModuleMenuInterface
56c1010edaSGreg Roach{
57*49a243cbSGreg Roach    use ModuleMenuTrait;
58*49a243cbSGreg Roach
595a78cd34SGreg Roach    // Routes that have a record which can be added to the clipboard
6016d6367aSGreg Roach    private const ROUTES_WITH_RECORDS = [
61c1010edaSGreg Roach        'family',
62c1010edaSGreg Roach        'individual',
63c1010edaSGreg Roach        'media',
64c1010edaSGreg Roach        'note',
65c1010edaSGreg Roach        'repository',
66c1010edaSGreg Roach        'source',
67c1010edaSGreg Roach    ];
685a78cd34SGreg Roach
69*49a243cbSGreg Roach    /** @var int The default access level for this module.  It can be changed in the control panel. */
70*49a243cbSGreg Roach    protected $access_level = Auth::PRIV_USER;
71*49a243cbSGreg Roach
728c2e8227SGreg Roach    /** {@inheritdoc} */
73*49a243cbSGreg Roach    public function title(): string
74c1010edaSGreg Roach    {
75bbb76c12SGreg Roach        /* I18N: Name of a module */
76bbb76c12SGreg Roach        return I18N::translate('Clippings cart');
778c2e8227SGreg Roach    }
788c2e8227SGreg Roach
798c2e8227SGreg Roach    /** {@inheritdoc} */
80*49a243cbSGreg Roach    public function description(): string
81c1010edaSGreg Roach    {
82bbb76c12SGreg Roach        /* I18N: Description of the “Clippings cart” module */
83bbb76c12SGreg Roach        return I18N::translate('Select records from your family tree and save them as a GEDCOM file.');
848c2e8227SGreg Roach    }
858c2e8227SGreg Roach
860ee13198SGreg Roach    /**
87*49a243cbSGreg Roach     * The default position for this menu.  It can be changed in the control panel.
880ee13198SGreg Roach     *
890ee13198SGreg Roach     * @return int
900ee13198SGreg Roach     */
918f53f488SRico Sonntag    public function defaultMenuOrder(): int
92c1010edaSGreg Roach    {
93*49a243cbSGreg Roach        return 10;
948c2e8227SGreg Roach    }
958c2e8227SGreg Roach
960ee13198SGreg Roach    /**
970ee13198SGreg Roach     * A menu, to be added to the main application menu.
980ee13198SGreg Roach     *
99aee13b6dSGreg Roach     * @param Tree $tree
100aee13b6dSGreg Roach     *
1010ee13198SGreg Roach     * @return Menu|null
1020ee13198SGreg Roach     */
103c1010edaSGreg Roach    public function getMenu(Tree $tree)
104c1010edaSGreg Roach    {
1055a78cd34SGreg Roach        $request = Request::createFromGlobals();
1068c2e8227SGreg Roach
1079e648e55SGreg Roach        $route = $request->get('route', '');
1085a78cd34SGreg Roach
1095a78cd34SGreg Roach        $submenus = [
110*49a243cbSGreg Roach            new Menu($this->title(), route('module', [
111*49a243cbSGreg Roach                'module' => $this->getName(),
112c1010edaSGreg Roach                'action' => 'Show',
113aa6f03bbSGreg Roach                'ged'    => $tree->name(),
114c1010edaSGreg Roach            ]), 'menu-clippings-cart', ['rel' => 'nofollow']),
1155a78cd34SGreg Roach        ];
1165a78cd34SGreg Roach
1175a78cd34SGreg Roach        if (in_array($route, self::ROUTES_WITH_RECORDS)) {
1189e648e55SGreg Roach            $xref      = $request->get('xref', '');
1195a78cd34SGreg Roach            $action    = 'Add' . ucfirst($route);
120c1010edaSGreg Roach            $add_route = route('module', [
121*49a243cbSGreg Roach                'module' => $this->getName(),
122c1010edaSGreg Roach                'action' => $action,
123c1010edaSGreg Roach                'xref'   => $xref,
124aa6f03bbSGreg Roach                'ged'    => $tree->name(),
125c1010edaSGreg Roach            ]);
1265a78cd34SGreg Roach
12725b2dde3SGreg Roach            $submenus[] = new Menu(I18N::translate('Add to the clippings cart'), $add_route, 'menu-clippings-add', ['rel' => 'nofollow']);
1288c2e8227SGreg Roach        }
129cbc1590aSGreg Roach
1305a78cd34SGreg Roach        if (!$this->isCartEmpty($tree)) {
131c1010edaSGreg Roach            $submenus[] = new Menu(I18N::translate('Empty the clippings cart'), route('module', [
132*49a243cbSGreg Roach                'module' => $this->getName(),
133c1010edaSGreg Roach                'action' => 'Empty',
134aa6f03bbSGreg Roach                'ged'    => $tree->name(),
135c1010edaSGreg Roach            ]), 'menu-clippings-empty', ['rel' => 'nofollow']);
136c1010edaSGreg Roach            $submenus[] = new Menu(I18N::translate('Download'), route('module', [
137*49a243cbSGreg Roach                'module' => $this->getName(),
138c1010edaSGreg Roach                'action' => 'DownloadForm',
139aa6f03bbSGreg Roach                'ged'    => $tree->name(),
140c1010edaSGreg Roach            ]), 'menu-clippings-download', ['rel' => 'nofollow']);
1415a78cd34SGreg Roach        }
1425a78cd34SGreg Roach
143*49a243cbSGreg Roach        return new Menu($this->title(), '#', 'menu-clippings', ['rel' => 'nofollow'], $submenus);
1448c2e8227SGreg Roach    }
1458c2e8227SGreg Roach
14676692c8bSGreg Roach    /**
1475a78cd34SGreg Roach     * @param Request $request
148b6db7c1fSGreg Roach     * @param Tree    $tree
14976692c8bSGreg Roach     *
1505a78cd34SGreg Roach     * @return BinaryFileResponse
15176692c8bSGreg Roach     */
152b6db7c1fSGreg Roach    public function getDownloadAction(Request $request, Tree $tree): BinaryFileResponse
153c1010edaSGreg Roach    {
1545a78cd34SGreg Roach        $this->checkModuleAccess($tree);
1558c2e8227SGreg Roach
1569e648e55SGreg Roach        $privatize_export = $request->get('privatize_export', '');
1575a78cd34SGreg Roach        $convert          = (bool) $request->get('convert');
1588c2e8227SGreg Roach
15913abd6f3SGreg Roach        $cart = Session::get('cart', []);
1608c2e8227SGreg Roach
161aa6f03bbSGreg Roach        $xrefs = array_keys($cart[$tree->name()] ?? []);
1625a78cd34SGreg Roach
1635a78cd34SGreg Roach        // Create a new/empty .ZIP file
1645a78cd34SGreg Roach        $temp_zip_file  = tempnam(sys_get_temp_dir(), 'webtrees-zip-');
1655a78cd34SGreg Roach        $zip_filesystem = new Filesystem(new ZipArchiveAdapter($temp_zip_file));
1665a78cd34SGreg Roach
1675a78cd34SGreg Roach        // Media file prefix
1685a78cd34SGreg Roach        $path = $tree->getPreference('MEDIA_DIRECTORY');
1695a78cd34SGreg Roach
1705a78cd34SGreg Roach        // GEDCOM file header
171a3d8780cSGreg Roach        $filetext = FunctionsExport::gedcomHeader($tree, $convert ? 'ANSI' : 'UTF-8');
1725a78cd34SGreg Roach
1735a78cd34SGreg Roach        switch ($privatize_export) {
1745a78cd34SGreg Roach            case 'gedadmin':
1755a78cd34SGreg Roach                $access_level = Auth::PRIV_NONE;
1765a78cd34SGreg Roach                break;
1775a78cd34SGreg Roach            case 'user':
1785a78cd34SGreg Roach                $access_level = Auth::PRIV_USER;
1795a78cd34SGreg Roach                break;
1805a78cd34SGreg Roach            case 'visitor':
1815a78cd34SGreg Roach                $access_level = Auth::PRIV_PRIVATE;
1825a78cd34SGreg Roach                break;
1835a78cd34SGreg Roach            case 'none':
1845a78cd34SGreg Roach            default:
1855a78cd34SGreg Roach                $access_level = Auth::PRIV_HIDE;
1865a78cd34SGreg Roach                break;
1875a78cd34SGreg Roach        }
1885a78cd34SGreg Roach
1895a78cd34SGreg Roach        foreach ($xrefs as $xref) {
1905a78cd34SGreg Roach            $object = GedcomRecord::getInstance($xref, $tree);
1915a78cd34SGreg Roach            // The object may have been deleted since we added it to the cart....
1925a78cd34SGreg Roach            if ($object) {
1935a78cd34SGreg Roach                $record = $object->privatizeGedcom($access_level);
1945a78cd34SGreg Roach                // Remove links to objects that aren't in the cart
1958d0ebef0SGreg Roach                preg_match_all('/\n1 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[2-9].*)*/', $record, $matches, PREG_SET_ORDER);
1965a78cd34SGreg Roach                foreach ($matches as $match) {
1975a78cd34SGreg Roach                    if (!array_key_exists($match[1], $xrefs)) {
1985a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
1995a78cd34SGreg Roach                    }
2005a78cd34SGreg Roach                }
2018d0ebef0SGreg Roach                preg_match_all('/\n2 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[3-9].*)*/', $record, $matches, PREG_SET_ORDER);
2025a78cd34SGreg Roach                foreach ($matches as $match) {
2035a78cd34SGreg Roach                    if (!array_key_exists($match[1], $xrefs)) {
2045a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2055a78cd34SGreg Roach                    }
2065a78cd34SGreg Roach                }
2078d0ebef0SGreg Roach                preg_match_all('/\n3 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[4-9].*)*/', $record, $matches, PREG_SET_ORDER);
2085a78cd34SGreg Roach                foreach ($matches as $match) {
2095a78cd34SGreg Roach                    if (!array_key_exists($match[1], $xrefs)) {
2105a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2115a78cd34SGreg Roach                    }
2125a78cd34SGreg Roach                }
2135a78cd34SGreg Roach
21455167344SGreg Roach                if ($object instanceof Individual || $object instanceof Family) {
2155a78cd34SGreg Roach                    $filetext .= $record . "\n";
2165a78cd34SGreg Roach                    $filetext .= "1 SOUR @WEBTREES@\n";
2171f273236SGreg Roach                    $filetext .= '2 PAGE ' . $object->url() . "\n";
21855167344SGreg Roach                } elseif ($object instanceof Source) {
2195a78cd34SGreg Roach                    $filetext .= $record . "\n";
2201f273236SGreg Roach                    $filetext .= '1 NOTE ' . $object->url() . "\n";
22155167344SGreg Roach                } elseif ($object instanceof Media) {
22255167344SGreg Roach                    // Add the media files to the archive
2235a78cd34SGreg Roach                    foreach ($object->mediaFiles() as $media_file) {
2245a78cd34SGreg Roach                        if (file_exists($media_file->getServerFilename())) {
2255a78cd34SGreg Roach                            $fp = fopen($media_file->getServerFilename(), 'r');
2265a78cd34SGreg Roach                            $zip_filesystem->writeStream($path . $media_file->filename(), $fp);
2275a78cd34SGreg Roach                            fclose($fp);
2285a78cd34SGreg Roach                        }
2295a78cd34SGreg Roach                    }
2305a78cd34SGreg Roach                    $filetext .= $record . "\n";
23155167344SGreg Roach                } else {
2325a78cd34SGreg Roach                    $filetext .= $record . "\n";
2338c2e8227SGreg Roach                }
2348c2e8227SGreg Roach            }
2358c2e8227SGreg Roach        }
2368c2e8227SGreg Roach
2375a78cd34SGreg Roach        // Create a source, to indicate the source of the data.
2385a78cd34SGreg Roach        $filetext .= "0 @WEBTREES@ SOUR\n1 TITL " . WT_BASE_URL . "\n";
2397fcd9838SGreg Roach        $author = User::find((int) $tree->getPreference('CONTACT_USER_ID'));
2405a78cd34SGreg Roach        if ($author !== null) {
2415a78cd34SGreg Roach            $filetext .= '1 AUTH ' . $author->getRealName() . "\n";
2425a78cd34SGreg Roach        }
2435a78cd34SGreg Roach        $filetext .= "0 TRLR\n";
2445a78cd34SGreg Roach
2455a78cd34SGreg Roach        // Make sure the preferred line endings are used
246a3d8780cSGreg Roach        $filetext = str_replace('\n', Gedcom::EOL, $filetext);
2475a78cd34SGreg Roach
24855167344SGreg Roach        if ($convert) {
2495a78cd34SGreg Roach            $filetext = utf8_decode($filetext);
2508c2e8227SGreg Roach        }
251cbc1590aSGreg Roach
2525a78cd34SGreg Roach        // Finally add the GEDCOM file to the .ZIP file.
2535a78cd34SGreg Roach        $zip_filesystem->write('clippings.ged', $filetext);
2545a78cd34SGreg Roach
2555a78cd34SGreg Roach        // Need to force-close the filesystem
25602a92f80SGreg Roach        unset($zip_filesystem);
2575a78cd34SGreg Roach
2585a78cd34SGreg Roach        $response = new BinaryFileResponse($temp_zip_file);
2595a78cd34SGreg Roach        $response->deleteFileAfterSend(true);
2605a78cd34SGreg Roach
2615a78cd34SGreg Roach        $response->headers->set('Content-Type', 'application/zip');
2625a78cd34SGreg Roach        $response->setContentDisposition(
2635a78cd34SGreg Roach            ResponseHeaderBag::DISPOSITION_ATTACHMENT,
2645a78cd34SGreg Roach            'clippings.zip'
2655a78cd34SGreg Roach        );
2665a78cd34SGreg Roach
2675a78cd34SGreg Roach        return $response;
2688c2e8227SGreg Roach    }
2698c2e8227SGreg Roach
2708c2e8227SGreg Roach    /**
271b6db7c1fSGreg Roach     * @param Tree $tree
272b6db7c1fSGreg Roach     * @param User $user
27376692c8bSGreg Roach     *
2745a78cd34SGreg Roach     * @return Response
2758c2e8227SGreg Roach     */
276b6db7c1fSGreg Roach    public function getDownloadFormAction(Tree $tree, User $user): Response
277c1010edaSGreg Roach    {
2785a78cd34SGreg Roach        $title = I18N::translate('Family tree clippings cart') . ' — ' . I18N::translate('Download');
2798c2e8227SGreg Roach
2805a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/download', [
2815a78cd34SGreg Roach            'is_manager' => Auth::isManager($tree, $user),
2825a78cd34SGreg Roach            'is_member'  => Auth::isMember($tree, $user),
2835a78cd34SGreg Roach            'title'      => $title,
2845a78cd34SGreg Roach        ]);
2858c2e8227SGreg Roach    }
2868c2e8227SGreg Roach
2875a78cd34SGreg Roach    /**
288b6db7c1fSGreg Roach     * @param Tree $tree
2895a78cd34SGreg Roach     *
2905a78cd34SGreg Roach     * @return RedirectResponse
2915a78cd34SGreg Roach     */
292b6db7c1fSGreg Roach    public function getEmptyAction(Tree $tree): RedirectResponse
293c1010edaSGreg Roach    {
2945a78cd34SGreg Roach        $cart                = Session::get('cart', []);
295aa6f03bbSGreg Roach        $cart[$tree->name()] = [];
2965a78cd34SGreg Roach        Session::put('cart', $cart);
2978c2e8227SGreg Roach
298c1010edaSGreg Roach        $url = route('module', [
299*49a243cbSGreg Roach            'module' => $this->getName(),
300c1010edaSGreg Roach            'action' => 'Show',
301aa6f03bbSGreg Roach            'ged'    => $tree->name(),
302c1010edaSGreg Roach        ]);
3035a78cd34SGreg Roach
3045a78cd34SGreg Roach        return new RedirectResponse($url);
3055a78cd34SGreg Roach    }
3065a78cd34SGreg Roach
3075a78cd34SGreg Roach    /**
3085a78cd34SGreg Roach     * @param Request $request
309b6db7c1fSGreg Roach     * @param Tree    $tree
3105a78cd34SGreg Roach     *
3115a78cd34SGreg Roach     * @return RedirectResponse
3125a78cd34SGreg Roach     */
313b6db7c1fSGreg Roach    public function postRemoveAction(Request $request, Tree $tree): RedirectResponse
314c1010edaSGreg Roach    {
3159e648e55SGreg Roach        $xref = $request->get('xref', '');
3165a78cd34SGreg Roach
3175a78cd34SGreg Roach        $cart = Session::get('cart', []);
318aa6f03bbSGreg Roach        unset($cart[$tree->name()][$xref]);
3195a78cd34SGreg Roach        Session::put('cart', $cart);
3205a78cd34SGreg Roach
321c1010edaSGreg Roach        $url = route('module', [
322*49a243cbSGreg Roach            'module' => $this->getName(),
323c1010edaSGreg Roach            'action' => 'Show',
324aa6f03bbSGreg Roach            'ged'    => $tree->name(),
325c1010edaSGreg Roach        ]);
3265a78cd34SGreg Roach
3275a78cd34SGreg Roach        return new RedirectResponse($url);
3285a78cd34SGreg Roach    }
3295a78cd34SGreg Roach
3305a78cd34SGreg Roach    /**
331b6db7c1fSGreg Roach     * @param Tree $tree
3325a78cd34SGreg Roach     *
3335a78cd34SGreg Roach     * @return Response
3345a78cd34SGreg Roach     */
335b6db7c1fSGreg Roach    public function getShowAction(Tree $tree): Response
336c1010edaSGreg Roach    {
3375a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/show', [
3385a78cd34SGreg Roach            'records' => $this->allRecordsInCart($tree),
3395a78cd34SGreg Roach            'title'   => I18N::translate('Family tree clippings cart'),
3405a78cd34SGreg Roach            'tree'    => $tree,
3415a78cd34SGreg Roach        ]);
3425a78cd34SGreg Roach    }
3435a78cd34SGreg Roach
3445a78cd34SGreg Roach    /**
3455a78cd34SGreg Roach     * @param Request $request
346b6db7c1fSGreg Roach     * @param Tree    $tree
3475a78cd34SGreg Roach     *
3485a78cd34SGreg Roach     * @return Response
3495a78cd34SGreg Roach     */
350b6db7c1fSGreg Roach    public function getAddFamilyAction(Request $request, Tree $tree): Response
351c1010edaSGreg Roach    {
3529e648e55SGreg Roach        $xref = $request->get('xref', '');
3535a78cd34SGreg Roach
3545a78cd34SGreg Roach        $family = Family::getInstance($xref, $tree);
3555a78cd34SGreg Roach
3565a78cd34SGreg Roach        if ($family === null) {
35759f2f229SGreg Roach            throw new FamilyNotFoundException();
3585a78cd34SGreg Roach        }
3595a78cd34SGreg Roach
3605a78cd34SGreg Roach        $options = $this->familyOptions($family);
3615a78cd34SGreg Roach
3625a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $family->getFullName());
3635a78cd34SGreg Roach
3645a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
3655a78cd34SGreg Roach            'options' => $options,
3665a78cd34SGreg Roach            'default' => key($options),
3675a78cd34SGreg Roach            'record'  => $family,
3685a78cd34SGreg Roach            'title'   => $title,
3695a78cd34SGreg Roach            'tree'    => $tree,
3705a78cd34SGreg Roach        ]);
3715a78cd34SGreg Roach    }
3725a78cd34SGreg Roach
3735a78cd34SGreg Roach    /**
3745a78cd34SGreg Roach     * @param Family $family
3755a78cd34SGreg Roach     *
3765a78cd34SGreg Roach     * @return string[]
3775a78cd34SGreg Roach     */
378c1010edaSGreg Roach    private function familyOptions(Family $family): array
379c1010edaSGreg Roach    {
3805a78cd34SGreg Roach        $name = strip_tags($family->getFullName());
3815a78cd34SGreg Roach
3825a78cd34SGreg Roach        return [
3835a78cd34SGreg Roach            'parents'     => $name,
384bbb76c12SGreg Roach            /* I18N: %s is a family (husband + wife) */
385bbb76c12SGreg Roach            'members'     => I18N::translate('%s and their children', $name),
386bbb76c12SGreg Roach            /* I18N: %s is a family (husband + wife) */
387bbb76c12SGreg Roach            'descendants' => I18N::translate('%s and their descendants', $name),
3885a78cd34SGreg Roach        ];
3895a78cd34SGreg Roach    }
3905a78cd34SGreg Roach
3915a78cd34SGreg Roach    /**
3925a78cd34SGreg Roach     * @param Request $request
393b6db7c1fSGreg Roach     * @param Tree    $tree
3945a78cd34SGreg Roach     *
3955a78cd34SGreg Roach     * @return RedirectResponse
3965a78cd34SGreg Roach     */
397b6db7c1fSGreg Roach    public function postAddFamilyAction(Request $request, Tree $tree): RedirectResponse
398c1010edaSGreg Roach    {
3999e648e55SGreg Roach        $xref   = $request->get('xref', '');
4009e648e55SGreg Roach        $option = $request->get('option', '');
4015a78cd34SGreg Roach
4025a78cd34SGreg Roach        $family = Family::getInstance($xref, $tree);
4035a78cd34SGreg Roach
4045a78cd34SGreg Roach        if ($family === null) {
40559f2f229SGreg Roach            throw new FamilyNotFoundException();
4065a78cd34SGreg Roach        }
4075a78cd34SGreg Roach
4085a78cd34SGreg Roach        switch ($option) {
4095a78cd34SGreg Roach            case 'parents':
4105a78cd34SGreg Roach                $this->addFamilyToCart($family);
4115a78cd34SGreg Roach                break;
4125a78cd34SGreg Roach
4135a78cd34SGreg Roach            case 'members':
4145a78cd34SGreg Roach                $this->addFamilyAndChildrenToCart($family);
4155a78cd34SGreg Roach                break;
4165a78cd34SGreg Roach
4175a78cd34SGreg Roach            case 'descendants':
4185a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($family);
4195a78cd34SGreg Roach                break;
4205a78cd34SGreg Roach        }
4215a78cd34SGreg Roach
4225a78cd34SGreg Roach        return new RedirectResponse($family->url());
4235a78cd34SGreg Roach    }
4245a78cd34SGreg Roach
4255a78cd34SGreg Roach    /**
4265a78cd34SGreg Roach     * @param Family $family
42718d7a90dSGreg Roach     *
42818d7a90dSGreg Roach     * @return void
4295a78cd34SGreg Roach     */
430c1010edaSGreg Roach    private function addFamilyToCart(Family $family)
431c1010edaSGreg Roach    {
4325a78cd34SGreg Roach        $this->addRecordToCart($family);
4335a78cd34SGreg Roach
4345a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
4355a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4365a78cd34SGreg Roach        }
4375a78cd34SGreg Roach    }
4385a78cd34SGreg Roach
4395a78cd34SGreg Roach    /**
4405a78cd34SGreg Roach     * @param Family $family
44118d7a90dSGreg Roach     *
44218d7a90dSGreg Roach     * @return void
4435a78cd34SGreg Roach     */
444c1010edaSGreg Roach    private function addFamilyAndChildrenToCart(Family $family)
445c1010edaSGreg Roach    {
4465a78cd34SGreg Roach        $this->addRecordToCart($family);
4475a78cd34SGreg Roach
4485a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
4495a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4505a78cd34SGreg Roach        }
4515a78cd34SGreg Roach        foreach ($family->getChildren() as $child) {
4525a78cd34SGreg Roach            $this->addRecordToCart($child);
4535a78cd34SGreg Roach        }
4545a78cd34SGreg Roach    }
4555a78cd34SGreg Roach
4565a78cd34SGreg Roach    /**
4575a78cd34SGreg Roach     * @param Family $family
45818d7a90dSGreg Roach     *
45918d7a90dSGreg Roach     * @return void
4605a78cd34SGreg Roach     */
461c1010edaSGreg Roach    private function addFamilyAndDescendantsToCart(Family $family)
462c1010edaSGreg Roach    {
4635a78cd34SGreg Roach        $this->addRecordToCart($family);
4645a78cd34SGreg Roach
4655a78cd34SGreg Roach        foreach ($family->getSpouses() as $spouse) {
4665a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4675a78cd34SGreg Roach        }
4685a78cd34SGreg Roach        foreach ($family->getChildren() as $child) {
4695a78cd34SGreg Roach            $this->addRecordToCart($child);
4705a78cd34SGreg Roach            foreach ($child->getSpouseFamilies() as $child_family) {
4715a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($child_family);
4725a78cd34SGreg Roach            }
4735a78cd34SGreg Roach        }
4745a78cd34SGreg Roach    }
4755a78cd34SGreg Roach
4765a78cd34SGreg Roach    /**
4775a78cd34SGreg Roach     * @param Request $request
478b6db7c1fSGreg Roach     * @param Tree    $tree
4795a78cd34SGreg Roach     *
4805a78cd34SGreg Roach     * @return Response
4815a78cd34SGreg Roach     */
482b6db7c1fSGreg Roach    public function getAddIndividualAction(Request $request, Tree $tree): Response
483c1010edaSGreg Roach    {
4849e648e55SGreg Roach        $xref = $request->get('xref', '');
4855a78cd34SGreg Roach
4865a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
4875a78cd34SGreg Roach
4885a78cd34SGreg Roach        if ($individual === null) {
48959f2f229SGreg Roach            throw new IndividualNotFoundException();
4905a78cd34SGreg Roach        }
4915a78cd34SGreg Roach
4925a78cd34SGreg Roach        $options = $this->individualOptions($individual);
4935a78cd34SGreg Roach
4945a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $individual->getFullName());
4955a78cd34SGreg Roach
4965a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
4975a78cd34SGreg Roach            'options' => $options,
4985a78cd34SGreg Roach            'default' => key($options),
4995a78cd34SGreg Roach            'record'  => $individual,
5005a78cd34SGreg Roach            'title'   => $title,
5015a78cd34SGreg Roach            'tree'    => $tree,
5025a78cd34SGreg Roach        ]);
5035a78cd34SGreg Roach    }
5045a78cd34SGreg Roach
5055a78cd34SGreg Roach    /**
5065a78cd34SGreg Roach     * @param Individual $individual
5075a78cd34SGreg Roach     *
5085a78cd34SGreg Roach     * @return string[]
5095a78cd34SGreg Roach     */
510c1010edaSGreg Roach    private function individualOptions(Individual $individual): array
511c1010edaSGreg Roach    {
5125a78cd34SGreg Roach        $name = strip_tags($individual->getFullName());
5135a78cd34SGreg Roach
5145a78cd34SGreg Roach        if ($individual->getSex() === 'F') {
5155a78cd34SGreg Roach            return [
5165a78cd34SGreg Roach                'self'              => $name,
5175a78cd34SGreg Roach                'parents'           => I18N::translate('%s, her parents and siblings', $name),
5185a78cd34SGreg Roach                'spouses'           => I18N::translate('%s, her spouses and children', $name),
5195a78cd34SGreg Roach                'ancestors'         => I18N::translate('%s and her ancestors', $name),
5205a78cd34SGreg Roach                'ancestor_families' => I18N::translate('%s, her ancestors and their families', $name),
5215a78cd34SGreg Roach                'descendants'       => I18N::translate('%s, her spouses and descendants', $name),
5225a78cd34SGreg Roach            ];
523b2ce94c6SRico Sonntag        }
524b2ce94c6SRico Sonntag
5255a78cd34SGreg Roach        return [
5265a78cd34SGreg Roach            'self'              => $name,
5275a78cd34SGreg Roach            'parents'           => I18N::translate('%s, his parents and siblings', $name),
5285a78cd34SGreg Roach            'spouses'           => I18N::translate('%s, his spouses and children', $name),
5295a78cd34SGreg Roach            'ancestors'         => I18N::translate('%s and his ancestors', $name),
5305a78cd34SGreg Roach            'ancestor_families' => I18N::translate('%s, his ancestors and their families', $name),
5315a78cd34SGreg Roach            'descendants'       => I18N::translate('%s, his spouses and descendants', $name),
5325a78cd34SGreg Roach        ];
5335a78cd34SGreg Roach    }
5345a78cd34SGreg Roach
5355a78cd34SGreg Roach    /**
5365a78cd34SGreg Roach     * @param Request $request
537b6db7c1fSGreg Roach     * @param Tree    $tree
5385a78cd34SGreg Roach     *
5395a78cd34SGreg Roach     * @return RedirectResponse
5405a78cd34SGreg Roach     */
541b6db7c1fSGreg Roach    public function postAddIndividualAction(Request $request, Tree $tree): RedirectResponse
542c1010edaSGreg Roach    {
5439e648e55SGreg Roach        $xref   = $request->get('xref', '');
5449e648e55SGreg Roach        $option = $request->get('option', '');
5455a78cd34SGreg Roach
5465a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
5475a78cd34SGreg Roach
5485a78cd34SGreg Roach        if ($individual === null) {
54959f2f229SGreg Roach            throw new IndividualNotFoundException();
5505a78cd34SGreg Roach        }
5515a78cd34SGreg Roach
5525a78cd34SGreg Roach        switch ($option) {
5535a78cd34SGreg Roach            case 'self':
5545a78cd34SGreg Roach                $this->addRecordToCart($individual);
5555a78cd34SGreg Roach                break;
5565a78cd34SGreg Roach
5575a78cd34SGreg Roach            case 'parents':
5585a78cd34SGreg Roach                foreach ($individual->getChildFamilies() as $family) {
5595a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
5605a78cd34SGreg Roach                }
5615a78cd34SGreg Roach                break;
5625a78cd34SGreg Roach
5635a78cd34SGreg Roach            case 'spouses':
5645a78cd34SGreg Roach                foreach ($individual->getSpouseFamilies() as $family) {
5655a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
5665a78cd34SGreg Roach                }
5675a78cd34SGreg Roach                break;
5685a78cd34SGreg Roach
5695a78cd34SGreg Roach            case 'ancestors':
5705a78cd34SGreg Roach                $this->addAncestorsToCart($individual);
5715a78cd34SGreg Roach                break;
5725a78cd34SGreg Roach
5735a78cd34SGreg Roach            case 'ancestor_families':
5745a78cd34SGreg Roach                $this->addAncestorFamiliesToCart($individual);
5755a78cd34SGreg Roach                break;
5765a78cd34SGreg Roach
5775a78cd34SGreg Roach            case 'descendants':
5785a78cd34SGreg Roach                foreach ($individual->getSpouseFamilies() as $family) {
5795a78cd34SGreg Roach                    $this->addFamilyAndDescendantsToCart($family);
5805a78cd34SGreg Roach                }
5815a78cd34SGreg Roach                break;
5825a78cd34SGreg Roach        }
5835a78cd34SGreg Roach
5845a78cd34SGreg Roach        return new RedirectResponse($individual->url());
5855a78cd34SGreg Roach    }
5865a78cd34SGreg Roach
5875a78cd34SGreg Roach    /**
5885a78cd34SGreg Roach     * @param Individual $individual
58918d7a90dSGreg Roach     *
59018d7a90dSGreg Roach     * @return void
5915a78cd34SGreg Roach     */
592c1010edaSGreg Roach    private function addAncestorsToCart(Individual $individual)
593c1010edaSGreg Roach    {
5945a78cd34SGreg Roach        $this->addRecordToCart($individual);
5955a78cd34SGreg Roach
5965a78cd34SGreg Roach        foreach ($individual->getChildFamilies() as $family) {
5975a78cd34SGreg Roach            foreach ($family->getSpouses() as $parent) {
5985a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
5995a78cd34SGreg Roach            }
6005a78cd34SGreg Roach        }
6015a78cd34SGreg Roach    }
6025a78cd34SGreg Roach
6035a78cd34SGreg Roach    /**
6045a78cd34SGreg Roach     * @param Individual $individual
60518d7a90dSGreg Roach     *
60618d7a90dSGreg Roach     * @return void
6075a78cd34SGreg Roach     */
608c1010edaSGreg Roach    private function addAncestorFamiliesToCart(Individual $individual)
609c1010edaSGreg Roach    {
6105a78cd34SGreg Roach        foreach ($individual->getChildFamilies() as $family) {
6115a78cd34SGreg Roach            $this->addFamilyAndChildrenToCart($family);
6125a78cd34SGreg Roach            foreach ($family->getSpouses() as $parent) {
6135a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
6145a78cd34SGreg Roach            }
6155a78cd34SGreg Roach        }
6165a78cd34SGreg Roach    }
6175a78cd34SGreg Roach
6185a78cd34SGreg Roach    /**
6195a78cd34SGreg Roach     * @param Request $request
620b6db7c1fSGreg Roach     * @param Tree    $tree
6215a78cd34SGreg Roach     *
6225a78cd34SGreg Roach     * @return Response
6235a78cd34SGreg Roach     */
624b6db7c1fSGreg Roach    public function getAddMediaAction(Request $request, Tree $tree): Response
625c1010edaSGreg Roach    {
6269e648e55SGreg Roach        $xref = $request->get('xref', '');
6275a78cd34SGreg Roach
6285a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
6295a78cd34SGreg Roach
6305a78cd34SGreg Roach        if ($media === null) {
63159f2f229SGreg Roach            throw new MediaNotFoundException();
6325a78cd34SGreg Roach        }
6335a78cd34SGreg Roach
6345a78cd34SGreg Roach        $options = $this->mediaOptions($media);
6355a78cd34SGreg Roach
6365a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $media->getFullName());
6375a78cd34SGreg Roach
6385a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
6395a78cd34SGreg Roach            'options' => $options,
6405a78cd34SGreg Roach            'default' => key($options),
6415a78cd34SGreg Roach            'record'  => $media,
6425a78cd34SGreg Roach            'title'   => $title,
6435a78cd34SGreg Roach            'tree'    => $tree,
6445a78cd34SGreg Roach        ]);
6455a78cd34SGreg Roach    }
6465a78cd34SGreg Roach
6475a78cd34SGreg Roach    /**
6485a78cd34SGreg Roach     * @param Media $media
6495a78cd34SGreg Roach     *
6505a78cd34SGreg Roach     * @return string[]
6515a78cd34SGreg Roach     */
652c1010edaSGreg Roach    private function mediaOptions(Media $media): array
653c1010edaSGreg Roach    {
6545a78cd34SGreg Roach        $name = strip_tags($media->getFullName());
6555a78cd34SGreg Roach
6565a78cd34SGreg Roach        return [
6575a78cd34SGreg Roach            'self' => $name,
6585a78cd34SGreg Roach        ];
6595a78cd34SGreg Roach    }
6605a78cd34SGreg Roach
6615a78cd34SGreg Roach    /**
6625a78cd34SGreg Roach     * @param Request $request
663b6db7c1fSGreg Roach     * @param Tree    $tree
6645a78cd34SGreg Roach     *
6655a78cd34SGreg Roach     * @return RedirectResponse
6665a78cd34SGreg Roach     */
667b6db7c1fSGreg Roach    public function postAddMediaAction(Request $request, Tree $tree): RedirectResponse
668c1010edaSGreg Roach    {
6699e648e55SGreg Roach        $xref = $request->get('xref', '');
6705a78cd34SGreg Roach
6715a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
6725a78cd34SGreg Roach
6735a78cd34SGreg Roach        if ($media === null) {
67459f2f229SGreg Roach            throw new MediaNotFoundException();
6755a78cd34SGreg Roach        }
6765a78cd34SGreg Roach
6775a78cd34SGreg Roach        $this->addRecordToCart($media);
6785a78cd34SGreg Roach
6795a78cd34SGreg Roach        return new RedirectResponse($media->url());
6805a78cd34SGreg Roach    }
6815a78cd34SGreg Roach
6825a78cd34SGreg Roach    /**
6835a78cd34SGreg Roach     * @param Request $request
684b6db7c1fSGreg Roach     * @param Tree    $tree
6855a78cd34SGreg Roach     *
6865a78cd34SGreg Roach     * @return Response
6875a78cd34SGreg Roach     */
688b6db7c1fSGreg Roach    public function getAddNoteAction(Request $request, Tree $tree): Response
689c1010edaSGreg Roach    {
6909e648e55SGreg Roach        $xref = $request->get('xref', '');
6915a78cd34SGreg Roach
6925a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
6935a78cd34SGreg Roach
6945a78cd34SGreg Roach        if ($note === null) {
69559f2f229SGreg Roach            throw new NoteNotFoundException();
6965a78cd34SGreg Roach        }
6975a78cd34SGreg Roach
6985a78cd34SGreg Roach        $options = $this->noteOptions($note);
6995a78cd34SGreg Roach
7005a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $note->getFullName());
7015a78cd34SGreg Roach
7025a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
7035a78cd34SGreg Roach            'options' => $options,
7045a78cd34SGreg Roach            'default' => key($options),
7055a78cd34SGreg Roach            'record'  => $note,
7065a78cd34SGreg Roach            'title'   => $title,
7075a78cd34SGreg Roach            'tree'    => $tree,
7085a78cd34SGreg Roach        ]);
7095a78cd34SGreg Roach    }
7105a78cd34SGreg Roach
7115a78cd34SGreg Roach    /**
7125a78cd34SGreg Roach     * @param Note $note
7135a78cd34SGreg Roach     *
7145a78cd34SGreg Roach     * @return string[]
7155a78cd34SGreg Roach     */
716c1010edaSGreg Roach    private function noteOptions(Note $note): array
717c1010edaSGreg Roach    {
7185a78cd34SGreg Roach        $name = strip_tags($note->getFullName());
7195a78cd34SGreg Roach
7205a78cd34SGreg Roach        return [
7215a78cd34SGreg Roach            'self' => $name,
7225a78cd34SGreg Roach        ];
7235a78cd34SGreg Roach    }
7245a78cd34SGreg Roach
7255a78cd34SGreg Roach    /**
7265a78cd34SGreg Roach     * @param Request $request
727b6db7c1fSGreg Roach     * @param Tree    $tree
7285a78cd34SGreg Roach     *
7295a78cd34SGreg Roach     * @return RedirectResponse
7305a78cd34SGreg Roach     */
731b6db7c1fSGreg Roach    public function postAddNoteAction(Request $request, Tree $tree): RedirectResponse
732c1010edaSGreg Roach    {
7339e648e55SGreg Roach        $xref = $request->get('xref', '');
7345a78cd34SGreg Roach
7355a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
7365a78cd34SGreg Roach
7375a78cd34SGreg Roach        if ($note === null) {
73859f2f229SGreg Roach            throw new NoteNotFoundException();
7395a78cd34SGreg Roach        }
7405a78cd34SGreg Roach
7415a78cd34SGreg Roach        $this->addRecordToCart($note);
7425a78cd34SGreg Roach
7435a78cd34SGreg Roach        return new RedirectResponse($note->url());
7445a78cd34SGreg Roach    }
7455a78cd34SGreg Roach
7465a78cd34SGreg Roach    /**
7475a78cd34SGreg Roach     * @param Request $request
748b6db7c1fSGreg Roach     * @param Tree    $tree
7495a78cd34SGreg Roach     *
7505a78cd34SGreg Roach     * @return Response
7515a78cd34SGreg Roach     */
752b6db7c1fSGreg Roach    public function getAddRepositoryAction(Request $request, Tree $tree): Response
753c1010edaSGreg Roach    {
7549e648e55SGreg Roach        $xref = $request->get('xref', '');
7555a78cd34SGreg Roach
7565a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
7575a78cd34SGreg Roach
7585a78cd34SGreg Roach        if ($repository === null) {
75959f2f229SGreg Roach            throw new RepositoryNotFoundException();
7605a78cd34SGreg Roach        }
7615a78cd34SGreg Roach
7625a78cd34SGreg Roach        $options = $this->repositoryOptions($repository);
7635a78cd34SGreg Roach
7645a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $repository->getFullName());
7655a78cd34SGreg Roach
7665a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
7675a78cd34SGreg Roach            'options' => $options,
7685a78cd34SGreg Roach            'default' => key($options),
7695a78cd34SGreg Roach            'record'  => $repository,
7705a78cd34SGreg Roach            'title'   => $title,
7715a78cd34SGreg Roach            'tree'    => $tree,
7725a78cd34SGreg Roach        ]);
7735a78cd34SGreg Roach    }
7745a78cd34SGreg Roach
7755a78cd34SGreg Roach    /**
7765a78cd34SGreg Roach     * @param Repository $repository
7775a78cd34SGreg Roach     *
7785a78cd34SGreg Roach     * @return string[]
7795a78cd34SGreg Roach     */
780c1010edaSGreg Roach    private function repositoryOptions(Repository $repository): array
781c1010edaSGreg Roach    {
7825a78cd34SGreg Roach        $name = strip_tags($repository->getFullName());
7835a78cd34SGreg Roach
7845a78cd34SGreg Roach        return [
7855a78cd34SGreg Roach            'self' => $name,
7865a78cd34SGreg Roach        ];
7875a78cd34SGreg Roach    }
7885a78cd34SGreg Roach
7895a78cd34SGreg Roach    /**
7905a78cd34SGreg Roach     * @param Request $request
791b6db7c1fSGreg Roach     * @param Tree    $tree
7925a78cd34SGreg Roach     *
7935a78cd34SGreg Roach     * @return RedirectResponse
7945a78cd34SGreg Roach     */
795b6db7c1fSGreg Roach    public function postAddRepositoryAction(Request $request, Tree $tree): RedirectResponse
796c1010edaSGreg Roach    {
7979e648e55SGreg Roach        $xref = $request->get('xref', '');
7985a78cd34SGreg Roach
7995a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
8005a78cd34SGreg Roach
8015a78cd34SGreg Roach        if ($repository === null) {
80259f2f229SGreg Roach            throw new RepositoryNotFoundException();
8035a78cd34SGreg Roach        }
8045a78cd34SGreg Roach
8055a78cd34SGreg Roach        $this->addRecordToCart($repository);
8065a78cd34SGreg Roach
8075a78cd34SGreg Roach        return new RedirectResponse($repository->url());
8085a78cd34SGreg Roach    }
8095a78cd34SGreg Roach
8105a78cd34SGreg Roach    /**
8115a78cd34SGreg Roach     * @param Request $request
812b6db7c1fSGreg Roach     * @param Tree    $tree
8135a78cd34SGreg Roach     *
8145a78cd34SGreg Roach     * @return Response
8155a78cd34SGreg Roach     */
816b6db7c1fSGreg Roach    public function getAddSourceAction(Request $request, Tree $tree): Response
817c1010edaSGreg Roach    {
8189e648e55SGreg Roach        $xref = $request->get('xref', '');
8195a78cd34SGreg Roach
8205a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
8215a78cd34SGreg Roach
8225a78cd34SGreg Roach        if ($source === null) {
82359f2f229SGreg Roach            throw new SourceNotFoundException();
8245a78cd34SGreg Roach        }
8255a78cd34SGreg Roach
8265a78cd34SGreg Roach        $options = $this->sourceOptions($source);
8275a78cd34SGreg Roach
8285a78cd34SGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $source->getFullName());
8295a78cd34SGreg Roach
8305a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
8315a78cd34SGreg Roach            'options' => $options,
8325a78cd34SGreg Roach            'default' => key($options),
8335a78cd34SGreg Roach            'record'  => $source,
8345a78cd34SGreg Roach            'title'   => $title,
8355a78cd34SGreg Roach            'tree'    => $tree,
8365a78cd34SGreg Roach        ]);
8375a78cd34SGreg Roach    }
8385a78cd34SGreg Roach
8395a78cd34SGreg Roach    /**
8405a78cd34SGreg Roach     * @param Source $source
8415a78cd34SGreg Roach     *
8425a78cd34SGreg Roach     * @return string[]
8435a78cd34SGreg Roach     */
844c1010edaSGreg Roach    private function sourceOptions(Source $source): array
845c1010edaSGreg Roach    {
8465a78cd34SGreg Roach        $name = strip_tags($source->getFullName());
8475a78cd34SGreg Roach
8485a78cd34SGreg Roach        return [
8495a78cd34SGreg Roach            'only'   => strip_tags($source->getFullName()),
8505a78cd34SGreg Roach            'linked' => I18N::translate('%s and the individuals that reference it.', $name),
8515a78cd34SGreg Roach        ];
8525a78cd34SGreg Roach    }
8535a78cd34SGreg Roach
8545a78cd34SGreg Roach    /**
8555a78cd34SGreg Roach     * @param Request $request
856b6db7c1fSGreg Roach     * @param Tree    $tree
8575a78cd34SGreg Roach     *
8585a78cd34SGreg Roach     * @return RedirectResponse
8595a78cd34SGreg Roach     */
860b6db7c1fSGreg Roach    public function postAddSourceAction(Request $request, Tree $tree): RedirectResponse
861c1010edaSGreg Roach    {
8629e648e55SGreg Roach        $xref   = $request->get('xref', '');
8639e648e55SGreg Roach        $option = $request->get('option', '');
8645a78cd34SGreg Roach
8655a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
8665a78cd34SGreg Roach
8675a78cd34SGreg Roach        if ($source === null) {
86859f2f229SGreg Roach            throw new SourceNotFoundException();
8695a78cd34SGreg Roach        }
8705a78cd34SGreg Roach
8715a78cd34SGreg Roach        $this->addRecordToCart($source);
8725a78cd34SGreg Roach
8735a78cd34SGreg Roach        if ($option === 'linked') {
8745a78cd34SGreg Roach            foreach ($source->linkedIndividuals('SOUR') as $individual) {
8755a78cd34SGreg Roach                $this->addRecordToCart($individual);
8765a78cd34SGreg Roach            }
8775a78cd34SGreg Roach            foreach ($source->linkedFamilies('SOUR') as $family) {
8785a78cd34SGreg Roach                $this->addRecordToCart($family);
8795a78cd34SGreg Roach            }
8805a78cd34SGreg Roach        }
8815a78cd34SGreg Roach
8825a78cd34SGreg Roach        return new RedirectResponse($source->url());
8835a78cd34SGreg Roach    }
8845a78cd34SGreg Roach
8855a78cd34SGreg Roach    /**
8865a78cd34SGreg Roach     * Get all the records in the cart.
8875a78cd34SGreg Roach     *
8885a78cd34SGreg Roach     * @param Tree $tree
8895a78cd34SGreg Roach     *
8905a78cd34SGreg Roach     * @return GedcomRecord[]
8915a78cd34SGreg Roach     */
892c1010edaSGreg Roach    private function allRecordsInCart(Tree $tree): array
893c1010edaSGreg Roach    {
8945a78cd34SGreg Roach        $cart = Session::get('cart', []);
8955a78cd34SGreg Roach
896aa6f03bbSGreg Roach        $xrefs = array_keys($cart[$tree->name()] ?? []);
8975a78cd34SGreg Roach
8985a78cd34SGreg Roach        // Fetch all the records in the cart.
89918d7a90dSGreg Roach        $records = array_map(function (string $xref) use ($tree): GedcomRecord {
9005a78cd34SGreg Roach            return GedcomRecord::getInstance($xref, $tree);
9015a78cd34SGreg Roach        }, $xrefs);
9025a78cd34SGreg Roach
9035a78cd34SGreg Roach        // Some records may have been deleted after they were added to the cart.
9045a78cd34SGreg Roach        $records = array_filter($records);
9055a78cd34SGreg Roach
9065a78cd34SGreg Roach        // Group and sort.
90718d7a90dSGreg Roach        uasort($records, function (GedcomRecord $x, GedcomRecord $y): int {
908c156e8f5SGreg Roach            return $x::RECORD_TYPE <=> $y::RECORD_TYPE ?: GedcomRecord::nameComparator()($x, $y);
9095a78cd34SGreg Roach        });
9105a78cd34SGreg Roach
9115a78cd34SGreg Roach        return $records;
9125a78cd34SGreg Roach    }
9135a78cd34SGreg Roach
9145a78cd34SGreg Roach    /**
9155a78cd34SGreg Roach     * Add a record (and direclty linked sources, notes, etc. to the cart.
9165a78cd34SGreg Roach     *
9175a78cd34SGreg Roach     * @param GedcomRecord $record
91818d7a90dSGreg Roach     *
91918d7a90dSGreg Roach     * @return void
9205a78cd34SGreg Roach     */
921c1010edaSGreg Roach    private function addRecordToCart(GedcomRecord $record)
922c1010edaSGreg Roach    {
9235a78cd34SGreg Roach        $cart = Session::get('cart', []);
9245a78cd34SGreg Roach
925f4afa648SGreg Roach        $tree_name = $record->tree()->name();
9265a78cd34SGreg Roach
9275a78cd34SGreg Roach        // Add this record
928c0935879SGreg Roach        $cart[$tree_name][$record->xref()] = true;
9295a78cd34SGreg Roach
9305a78cd34SGreg Roach        // Add directly linked media, notes, repositories and sources.
9318d0ebef0SGreg Roach        preg_match_all('/\n\d (?:OBJE|NOTE|SOUR|REPO) @(' . Gedcom::REGEX_XREF . ')@/', $record->gedcom(), $matches);
9325a78cd34SGreg Roach
9335a78cd34SGreg Roach        foreach ($matches[1] as $match) {
9345a78cd34SGreg Roach            $cart[$tree_name][$match] = true;
9355a78cd34SGreg Roach        }
9365a78cd34SGreg Roach
9375a78cd34SGreg Roach        Session::put('cart', $cart);
9385a78cd34SGreg Roach    }
9395a78cd34SGreg Roach
9405a78cd34SGreg Roach    /**
9415a78cd34SGreg Roach     * @param Tree $tree
9425a78cd34SGreg Roach     *
9435a78cd34SGreg Roach     * @return bool
9445a78cd34SGreg Roach     */
945c1010edaSGreg Roach    private function isCartEmpty(Tree $tree): bool
946c1010edaSGreg Roach    {
9475a78cd34SGreg Roach        $cart = Session::get('cart', []);
9485a78cd34SGreg Roach
949aa6f03bbSGreg Roach        return empty($cart[$tree->name()]);
9505a78cd34SGreg Roach    }
9515a78cd34SGreg Roach
9525a78cd34SGreg Roach    /**
9535a78cd34SGreg Roach     * Only allow access to the routes/functions if the menu is active
9545a78cd34SGreg Roach     *
9555a78cd34SGreg Roach     * @param Tree $tree
95618d7a90dSGreg Roach     *
95718d7a90dSGreg Roach     * @return void
95818d7a90dSGreg Roach     *
95918d7a90dSGreg Roach     * @throws NoteNotFoundException
9605a78cd34SGreg Roach     */
961c1010edaSGreg Roach    private function checkModuleAccess(Tree $tree)
962c1010edaSGreg Roach    {
963*49a243cbSGreg Roach        if (!array_key_exists($this->getName(), Module::activeMenus($tree))) {
96459f2f229SGreg Roach            throw new NotFoundHttpException();
9655a78cd34SGreg Roach        }
9668c2e8227SGreg Roach    }
9678c2e8227SGreg Roach}
968