xref: /webtrees/app/Module/ClippingsCartModule.php (revision 61bf91b20f8728415e016d6c15191fd05657801e)
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;
21e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
220bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\FamilyNotFoundException;
230bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualNotFoundException;
240bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\MediaNotFoundException;
250bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\NoteNotFoundException;
260bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\RepositoryNotFoundException;
270bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\SourceNotFoundException;
280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Family;
295a78cd34SGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsExport;
305a78cd34SGreg Roachuse Fisharebest\Webtrees\Gedcom;
310e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
320e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
330e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
345a78cd34SGreg Roachuse Fisharebest\Webtrees\Media;
350e62c4b8SGreg Roachuse Fisharebest\Webtrees\Menu;
365a78cd34SGreg Roachuse Fisharebest\Webtrees\Note;
375a78cd34SGreg Roachuse Fisharebest\Webtrees\Repository;
38e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
390e62c4b8SGreg Roachuse Fisharebest\Webtrees\Session;
405a78cd34SGreg Roachuse Fisharebest\Webtrees\Source;
41aee13b6dSGreg Roachuse Fisharebest\Webtrees\Tree;
425a78cd34SGreg Roachuse League\Flysystem\Filesystem;
43*61bf91b2SGreg Roachuse League\Flysystem\MountManager;
445a78cd34SGreg Roachuse League\Flysystem\ZipArchive\ZipArchiveAdapter;
45bed27cedSGreg Roachuse Psr\Http\Message\ResponseFactoryInterface;
466ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
476ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
486ccdf4f0SGreg Roachuse Psr\Http\Message\StreamFactoryInterface;
49eb235819SGreg Roachuse function app;
50bf80ec58SGreg Roachuse function array_filter;
51bf80ec58SGreg Roachuse function array_keys;
52bf80ec58SGreg Roachuse function array_map;
53bf80ec58SGreg Roachuse function in_array;
54bf80ec58SGreg Roachuse function key;
55bf80ec58SGreg Roachuse function preg_match_all;
56bf80ec58SGreg Roachuse function redirect;
57bf80ec58SGreg Roachuse function route;
58e5a6b4d4SGreg Roachuse function str_replace;
59bf80ec58SGreg Roachuse function strip_tags;
60bf80ec58SGreg Roachuse function sys_get_temp_dir;
61bf80ec58SGreg Roachuse function tempnam;
62bf80ec58SGreg Roachuse function ucfirst;
63bf80ec58SGreg Roachuse function utf8_decode;
648c2e8227SGreg Roach
658c2e8227SGreg Roach/**
668c2e8227SGreg Roach * Class ClippingsCartModule
678c2e8227SGreg Roach */
6837eb8894SGreg Roachclass ClippingsCartModule extends AbstractModule implements ModuleMenuInterface
69c1010edaSGreg Roach{
7049a243cbSGreg Roach    use ModuleMenuTrait;
7149a243cbSGreg Roach
725a78cd34SGreg Roach    // Routes that have a record which can be added to the clipboard
7316d6367aSGreg Roach    private const ROUTES_WITH_RECORDS = [
74c1010edaSGreg Roach        'family',
75c1010edaSGreg Roach        'individual',
76c1010edaSGreg Roach        'media',
77c1010edaSGreg Roach        'note',
78c1010edaSGreg Roach        'repository',
79c1010edaSGreg Roach        'source',
80c1010edaSGreg Roach    ];
815a78cd34SGreg Roach
8249a243cbSGreg Roach    /** @var int The default access level for this module.  It can be changed in the control panel. */
8349a243cbSGreg Roach    protected $access_level = Auth::PRIV_USER;
8449a243cbSGreg Roach
85961ec755SGreg Roach    /**
86e5a6b4d4SGreg Roach     * @var UserService
87e5a6b4d4SGreg Roach     */
88e5a6b4d4SGreg Roach    private $user_service;
89e5a6b4d4SGreg Roach
90e5a6b4d4SGreg Roach    /**
91e5a6b4d4SGreg Roach     * ClippingsCartModule constructor.
92e5a6b4d4SGreg Roach     *
93e5a6b4d4SGreg Roach     * @param UserService $user_service
94e5a6b4d4SGreg Roach     */
95e5a6b4d4SGreg Roach    public function __construct(UserService $user_service)
96e5a6b4d4SGreg Roach    {
97e5a6b4d4SGreg Roach        $this->user_service = $user_service;
98e5a6b4d4SGreg Roach    }
99e5a6b4d4SGreg Roach
100e5a6b4d4SGreg Roach    /**
1010cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
102961ec755SGreg Roach     *
103961ec755SGreg Roach     * @return string
104961ec755SGreg Roach     */
10549a243cbSGreg Roach    public function title(): string
106c1010edaSGreg Roach    {
107bbb76c12SGreg Roach        /* I18N: Name of a module */
108bbb76c12SGreg Roach        return I18N::translate('Clippings cart');
1098c2e8227SGreg Roach    }
1108c2e8227SGreg Roach
111961ec755SGreg Roach    /**
112961ec755SGreg Roach     * A sentence describing what this module does.
113961ec755SGreg Roach     *
114961ec755SGreg Roach     * @return string
115961ec755SGreg Roach     */
11649a243cbSGreg Roach    public function description(): string
117c1010edaSGreg Roach    {
118bbb76c12SGreg Roach        /* I18N: Description of the “Clippings cart” module */
119bbb76c12SGreg Roach        return I18N::translate('Select records from your family tree and save them as a GEDCOM file.');
1208c2e8227SGreg Roach    }
1218c2e8227SGreg Roach
1220ee13198SGreg Roach    /**
12349a243cbSGreg Roach     * The default position for this menu.  It can be changed in the control panel.
1240ee13198SGreg Roach     *
1250ee13198SGreg Roach     * @return int
1260ee13198SGreg Roach     */
1278f53f488SRico Sonntag    public function defaultMenuOrder(): int
128c1010edaSGreg Roach    {
129353b36abSGreg Roach        return 6;
1308c2e8227SGreg Roach    }
1318c2e8227SGreg Roach
1320ee13198SGreg Roach    /**
1330ee13198SGreg Roach     * A menu, to be added to the main application menu.
1340ee13198SGreg Roach     *
135aee13b6dSGreg Roach     * @param Tree $tree
136aee13b6dSGreg Roach     *
1370ee13198SGreg Roach     * @return Menu|null
1380ee13198SGreg Roach     */
13946295629SGreg Roach    public function getMenu(Tree $tree): ?Menu
140c1010edaSGreg Roach    {
141eb235819SGreg Roach        /** @var ServerRequestInterface $request */
1426ccdf4f0SGreg Roach        $request = app(ServerRequestInterface::class);
1438c2e8227SGreg Roach
144eb235819SGreg Roach        $route = $request->getQueryParams()['route'] ?? '';
1455a78cd34SGreg Roach
1465a78cd34SGreg Roach        $submenus = [
14749a243cbSGreg Roach            new Menu($this->title(), route('module', [
14826684e68SGreg Roach                'module' => $this->name(),
149c1010edaSGreg Roach                'action' => 'Show',
150aa6f03bbSGreg Roach                'ged'    => $tree->name(),
151c1010edaSGreg Roach            ]), 'menu-clippings-cart', ['rel' => 'nofollow']),
1525a78cd34SGreg Roach        ];
1535a78cd34SGreg Roach
15422d65e5aSGreg Roach        if (in_array($route, self::ROUTES_WITH_RECORDS, true)) {
155bed27cedSGreg Roach            $xref      = $request->getQueryParams()['xref'] ?? '';
1565a78cd34SGreg Roach            $action    = 'Add' . ucfirst($route);
157c1010edaSGreg Roach            $add_route = route('module', [
15826684e68SGreg Roach                'module' => $this->name(),
159c1010edaSGreg Roach                'action' => $action,
160c1010edaSGreg Roach                'xref'   => $xref,
161aa6f03bbSGreg Roach                'ged'    => $tree->name(),
162c1010edaSGreg Roach            ]);
1635a78cd34SGreg Roach
16425b2dde3SGreg Roach            $submenus[] = new Menu(I18N::translate('Add to the clippings cart'), $add_route, 'menu-clippings-add', ['rel' => 'nofollow']);
1658c2e8227SGreg Roach        }
166cbc1590aSGreg Roach
1675a78cd34SGreg Roach        if (!$this->isCartEmpty($tree)) {
168c1010edaSGreg Roach            $submenus[] = new Menu(I18N::translate('Empty the clippings cart'), route('module', [
16926684e68SGreg Roach                'module' => $this->name(),
170c1010edaSGreg Roach                'action' => 'Empty',
171aa6f03bbSGreg Roach                'ged'    => $tree->name(),
172c1010edaSGreg Roach            ]), 'menu-clippings-empty', ['rel' => 'nofollow']);
173c1010edaSGreg Roach            $submenus[] = new Menu(I18N::translate('Download'), route('module', [
17426684e68SGreg Roach                'module' => $this->name(),
175c1010edaSGreg Roach                'action' => 'DownloadForm',
176aa6f03bbSGreg Roach                'ged'    => $tree->name(),
177c1010edaSGreg Roach            ]), 'menu-clippings-download', ['rel' => 'nofollow']);
1785a78cd34SGreg Roach        }
1795a78cd34SGreg Roach
18049a243cbSGreg Roach        return new Menu($this->title(), '#', 'menu-clippings', ['rel' => 'nofollow'], $submenus);
1818c2e8227SGreg Roach    }
1828c2e8227SGreg Roach
18376692c8bSGreg Roach    /**
1846ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
185b6db7c1fSGreg Roach     * @param Tree                   $tree
18676692c8bSGreg Roach     *
1876ccdf4f0SGreg Roach     * @return ResponseInterface
18876692c8bSGreg Roach     */
1896ccdf4f0SGreg Roach    public function getDownloadAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
190c1010edaSGreg Roach    {
191bed27cedSGreg Roach        $params = $request->getQueryParams();
192bed27cedSGreg Roach
193bed27cedSGreg Roach        $privatize_export = $params['privatize_export'];
194bed27cedSGreg Roach        $convert          = (bool) ($params['convert'] ?? false);
1958c2e8227SGreg Roach
19613abd6f3SGreg Roach        $cart = Session::get('cart', []);
1978c2e8227SGreg Roach
198aa6f03bbSGreg Roach        $xrefs = array_keys($cart[$tree->name()] ?? []);
1995a78cd34SGreg Roach
2005a78cd34SGreg Roach        // Create a new/empty .ZIP file
2015a78cd34SGreg Roach        $temp_zip_file  = tempnam(sys_get_temp_dir(), 'webtrees-zip-');
2025a78cd34SGreg Roach        $zip_filesystem = new Filesystem(new ZipArchiveAdapter($temp_zip_file));
2035a78cd34SGreg Roach
204*61bf91b2SGreg Roach        $manager = new MountManager([
205*61bf91b2SGreg Roach            'media' => $tree->mediaFilesystem(),
206*61bf91b2SGreg Roach            'zip'   => $zip_filesystem,
207*61bf91b2SGreg Roach        ]);
208*61bf91b2SGreg Roach
2095a78cd34SGreg Roach        // Media file prefix
2105a78cd34SGreg Roach        $path = $tree->getPreference('MEDIA_DIRECTORY');
2115a78cd34SGreg Roach
2125a78cd34SGreg Roach        // GEDCOM file header
213a3d8780cSGreg Roach        $filetext = FunctionsExport::gedcomHeader($tree, $convert ? 'ANSI' : 'UTF-8');
2145a78cd34SGreg Roach
2155a78cd34SGreg Roach        switch ($privatize_export) {
2165a78cd34SGreg Roach            case 'gedadmin':
2175a78cd34SGreg Roach                $access_level = Auth::PRIV_NONE;
2185a78cd34SGreg Roach                break;
2195a78cd34SGreg Roach            case 'user':
2205a78cd34SGreg Roach                $access_level = Auth::PRIV_USER;
2215a78cd34SGreg Roach                break;
2225a78cd34SGreg Roach            case 'visitor':
2235a78cd34SGreg Roach                $access_level = Auth::PRIV_PRIVATE;
2245a78cd34SGreg Roach                break;
2255a78cd34SGreg Roach            case 'none':
2265a78cd34SGreg Roach            default:
2275a78cd34SGreg Roach                $access_level = Auth::PRIV_HIDE;
2285a78cd34SGreg Roach                break;
2295a78cd34SGreg Roach        }
2305a78cd34SGreg Roach
2315a78cd34SGreg Roach        foreach ($xrefs as $xref) {
2325a78cd34SGreg Roach            $object = GedcomRecord::getInstance($xref, $tree);
2335a78cd34SGreg Roach            // The object may have been deleted since we added it to the cart....
234bed27cedSGreg Roach            if ($object instanceof  GedcomRecord) {
2355a78cd34SGreg Roach                $record = $object->privatizeGedcom($access_level);
2365a78cd34SGreg Roach                // Remove links to objects that aren't in the cart
2378d0ebef0SGreg Roach                preg_match_all('/\n1 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[2-9].*)*/', $record, $matches, PREG_SET_ORDER);
2385a78cd34SGreg Roach                foreach ($matches as $match) {
239bf80ec58SGreg Roach                    if (!in_array($match[1], $xrefs, true)) {
2405a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2415a78cd34SGreg Roach                    }
2425a78cd34SGreg Roach                }
2438d0ebef0SGreg Roach                preg_match_all('/\n2 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[3-9].*)*/', $record, $matches, PREG_SET_ORDER);
2445a78cd34SGreg Roach                foreach ($matches as $match) {
245bf80ec58SGreg Roach                    if (!in_array($match[1], $xrefs, true)) {
2465a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2475a78cd34SGreg Roach                    }
2485a78cd34SGreg Roach                }
2498d0ebef0SGreg Roach                preg_match_all('/\n3 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[4-9].*)*/', $record, $matches, PREG_SET_ORDER);
2505a78cd34SGreg Roach                foreach ($matches as $match) {
251bf80ec58SGreg Roach                    if (!in_array($match[1], $xrefs, true)) {
2525a78cd34SGreg Roach                        $record = str_replace($match[0], '', $record);
2535a78cd34SGreg Roach                    }
2545a78cd34SGreg Roach                }
2555a78cd34SGreg Roach
25655167344SGreg Roach                if ($object instanceof Individual || $object instanceof Family) {
2575a78cd34SGreg Roach                    $filetext .= $record . "\n";
2585a78cd34SGreg Roach                    $filetext .= "1 SOUR @WEBTREES@\n";
2591f273236SGreg Roach                    $filetext .= '2 PAGE ' . $object->url() . "\n";
26055167344SGreg Roach                } elseif ($object instanceof Source) {
2615a78cd34SGreg Roach                    $filetext .= $record . "\n";
2621f273236SGreg Roach                    $filetext .= '1 NOTE ' . $object->url() . "\n";
26355167344SGreg Roach                } elseif ($object instanceof Media) {
26455167344SGreg Roach                    // Add the media files to the archive
2655a78cd34SGreg Roach                    foreach ($object->mediaFiles() as $media_file) {
266*61bf91b2SGreg Roach                        $from = 'media://' . $media_file->filename();
267*61bf91b2SGreg Roach                        $to   = 'zip://' . $path . $media_file->filename();
268*61bf91b2SGreg Roach                        if (!$media_file->isExternal() && $manager->has($from)) {
269*61bf91b2SGreg Roach                            $manager->copy($from, $to);
2705a78cd34SGreg Roach                        }
2715a78cd34SGreg Roach                    }
2725a78cd34SGreg Roach                    $filetext .= $record . "\n";
27355167344SGreg Roach                } else {
2745a78cd34SGreg Roach                    $filetext .= $record . "\n";
2758c2e8227SGreg Roach                }
2768c2e8227SGreg Roach            }
2778c2e8227SGreg Roach        }
2788c2e8227SGreg Roach
2799b93b7c3SGreg Roach        $base_url = $request->getAttribute('base_url');
2809b93b7c3SGreg Roach
2815a78cd34SGreg Roach        // Create a source, to indicate the source of the data.
2829b93b7c3SGreg Roach        $filetext .= "0 @WEBTREES@ SOUR\n1 TITL " . $base_url . "\n";
283e5a6b4d4SGreg Roach        $author   = $this->user_service->find((int) $tree->getPreference('CONTACT_USER_ID'));
2845a78cd34SGreg Roach        if ($author !== null) {
285e5a6b4d4SGreg Roach            $filetext .= '1 AUTH ' . $author->realName() . "\n";
2865a78cd34SGreg Roach        }
2875a78cd34SGreg Roach        $filetext .= "0 TRLR\n";
2885a78cd34SGreg Roach
2895a78cd34SGreg Roach        // Make sure the preferred line endings are used
290a3d8780cSGreg Roach        $filetext = str_replace('\n', Gedcom::EOL, $filetext);
2915a78cd34SGreg Roach
29255167344SGreg Roach        if ($convert) {
2935a78cd34SGreg Roach            $filetext = utf8_decode($filetext);
2948c2e8227SGreg Roach        }
295cbc1590aSGreg Roach
2965a78cd34SGreg Roach        // Finally add the GEDCOM file to the .ZIP file.
2975a78cd34SGreg Roach        $zip_filesystem->write('clippings.ged', $filetext);
2985a78cd34SGreg Roach
299*61bf91b2SGreg Roach        // Need to force-close ZipArchive filesystems.
300*61bf91b2SGreg Roach        $zip_filesystem->getAdapter()->getArchive()->close();
3015a78cd34SGreg Roach
3026ccdf4f0SGreg Roach        // Use a stream, so that we do not have to load the entire file into memory.
3036ccdf4f0SGreg Roach        $stream = app(StreamFactoryInterface::class)->createStreamFromFile($temp_zip_file);
3045a78cd34SGreg Roach
305bed27cedSGreg Roach        /** @var ResponseFactoryInterface $response_factory */
306bed27cedSGreg Roach        $response_factory = app(ResponseFactoryInterface::class);
307bed27cedSGreg Roach
308bed27cedSGreg Roach        return $response_factory->createResponse()
3096ccdf4f0SGreg Roach            ->withBody($stream)
3101b3d4731SGreg Roach            ->withHeader('Content-Type', 'application/zip')
311bed27cedSGreg Roach            ->withHeader('Content-Disposition', 'attachment; filename="clippings.zip');
3128c2e8227SGreg Roach    }
3138c2e8227SGreg Roach
3148c2e8227SGreg Roach    /**
315b6db7c1fSGreg Roach     * @param Tree          $tree
316e5a6b4d4SGreg Roach     * @param UserInterface $user
31776692c8bSGreg Roach     *
3186ccdf4f0SGreg Roach     * @return ResponseInterface
3198c2e8227SGreg Roach     */
3206ccdf4f0SGreg Roach    public function getDownloadFormAction(Tree $tree, UserInterface $user): ResponseInterface
321c1010edaSGreg Roach    {
3225a78cd34SGreg Roach        $title = I18N::translate('Family tree clippings cart') . ' — ' . I18N::translate('Download');
3238c2e8227SGreg Roach
3245a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/download', [
3255a78cd34SGreg Roach            'is_manager' => Auth::isManager($tree, $user),
3265a78cd34SGreg Roach            'is_member'  => Auth::isMember($tree, $user),
3275a78cd34SGreg Roach            'title'      => $title,
3285a78cd34SGreg Roach        ]);
3298c2e8227SGreg Roach    }
3308c2e8227SGreg Roach
3315a78cd34SGreg Roach    /**
332b6db7c1fSGreg Roach     * @param Tree $tree
3335a78cd34SGreg Roach     *
3346ccdf4f0SGreg Roach     * @return ResponseInterface
3355a78cd34SGreg Roach     */
3366ccdf4f0SGreg Roach    public function getEmptyAction(Tree $tree): ResponseInterface
337c1010edaSGreg Roach    {
3385a78cd34SGreg Roach        $cart                = Session::get('cart', []);
339aa6f03bbSGreg Roach        $cart[$tree->name()] = [];
3405a78cd34SGreg Roach        Session::put('cart', $cart);
3418c2e8227SGreg Roach
342c1010edaSGreg Roach        $url = route('module', [
34326684e68SGreg Roach            'module' => $this->name(),
344c1010edaSGreg Roach            'action' => 'Show',
345aa6f03bbSGreg Roach            'ged'    => $tree->name(),
346c1010edaSGreg Roach        ]);
3475a78cd34SGreg Roach
3486ccdf4f0SGreg Roach        return redirect($url);
3495a78cd34SGreg Roach    }
3505a78cd34SGreg Roach
3515a78cd34SGreg Roach    /**
3526ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
353b6db7c1fSGreg Roach     * @param Tree                   $tree
3545a78cd34SGreg Roach     *
3556ccdf4f0SGreg Roach     * @return ResponseInterface
3565a78cd34SGreg Roach     */
3576ccdf4f0SGreg Roach    public function postRemoveAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
358c1010edaSGreg Roach    {
359bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
3605a78cd34SGreg Roach
3615a78cd34SGreg Roach        $cart = Session::get('cart', []);
362aa6f03bbSGreg Roach        unset($cart[$tree->name()][$xref]);
3635a78cd34SGreg Roach        Session::put('cart', $cart);
3645a78cd34SGreg Roach
365c1010edaSGreg Roach        $url = route('module', [
36626684e68SGreg Roach            'module' => $this->name(),
367c1010edaSGreg Roach            'action' => 'Show',
368aa6f03bbSGreg Roach            'ged'    => $tree->name(),
369c1010edaSGreg Roach        ]);
3705a78cd34SGreg Roach
3716ccdf4f0SGreg Roach        return redirect($url);
3725a78cd34SGreg Roach    }
3735a78cd34SGreg Roach
3745a78cd34SGreg Roach    /**
375b6db7c1fSGreg Roach     * @param Tree $tree
3765a78cd34SGreg Roach     *
3776ccdf4f0SGreg Roach     * @return ResponseInterface
3785a78cd34SGreg Roach     */
3796ccdf4f0SGreg Roach    public function getShowAction(Tree $tree): ResponseInterface
380c1010edaSGreg Roach    {
3815a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/show', [
3825a78cd34SGreg Roach            'records' => $this->allRecordsInCart($tree),
3835a78cd34SGreg Roach            'title'   => I18N::translate('Family tree clippings cart'),
3845a78cd34SGreg Roach            'tree'    => $tree,
3855a78cd34SGreg Roach        ]);
3865a78cd34SGreg Roach    }
3875a78cd34SGreg Roach
3885a78cd34SGreg Roach    /**
3896ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
390b6db7c1fSGreg Roach     * @param Tree                   $tree
3915a78cd34SGreg Roach     *
3926ccdf4f0SGreg Roach     * @return ResponseInterface
3935a78cd34SGreg Roach     */
3946ccdf4f0SGreg Roach    public function getAddFamilyAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
395c1010edaSGreg Roach    {
396bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
3975a78cd34SGreg Roach
3985a78cd34SGreg Roach        $family = Family::getInstance($xref, $tree);
3995a78cd34SGreg Roach
4005a78cd34SGreg Roach        if ($family === null) {
40159f2f229SGreg Roach            throw new FamilyNotFoundException();
4025a78cd34SGreg Roach        }
4035a78cd34SGreg Roach
4045a78cd34SGreg Roach        $options = $this->familyOptions($family);
4055a78cd34SGreg Roach
40639ca88baSGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $family->fullName());
4075a78cd34SGreg Roach
4085a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
4095a78cd34SGreg Roach            'options' => $options,
4105a78cd34SGreg Roach            'default' => key($options),
4115a78cd34SGreg Roach            'record'  => $family,
4125a78cd34SGreg Roach            'title'   => $title,
4135a78cd34SGreg Roach            'tree'    => $tree,
4145a78cd34SGreg Roach        ]);
4155a78cd34SGreg Roach    }
4165a78cd34SGreg Roach
4175a78cd34SGreg Roach    /**
4185a78cd34SGreg Roach     * @param Family $family
4195a78cd34SGreg Roach     *
4205a78cd34SGreg Roach     * @return string[]
4215a78cd34SGreg Roach     */
422c1010edaSGreg Roach    private function familyOptions(Family $family): array
423c1010edaSGreg Roach    {
42439ca88baSGreg Roach        $name = strip_tags($family->fullName());
4255a78cd34SGreg Roach
4265a78cd34SGreg Roach        return [
4275a78cd34SGreg Roach            'parents'     => $name,
428bbb76c12SGreg Roach            /* I18N: %s is a family (husband + wife) */
429bbb76c12SGreg Roach            'members'     => I18N::translate('%s and their children', $name),
430bbb76c12SGreg Roach            /* I18N: %s is a family (husband + wife) */
431bbb76c12SGreg Roach            'descendants' => I18N::translate('%s and their descendants', $name),
4325a78cd34SGreg Roach        ];
4335a78cd34SGreg Roach    }
4345a78cd34SGreg Roach
4355a78cd34SGreg Roach    /**
4366ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
437b6db7c1fSGreg Roach     * @param Tree                   $tree
4385a78cd34SGreg Roach     *
4396ccdf4f0SGreg Roach     * @return ResponseInterface
4405a78cd34SGreg Roach     */
4416ccdf4f0SGreg Roach    public function postAddFamilyAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
442c1010edaSGreg Roach    {
443bed27cedSGreg Roach        $xref   = $request->getQueryParams()['xref'];
444bed27cedSGreg Roach        $option = $request->getParsedBody()['option'];
4455a78cd34SGreg Roach
4465a78cd34SGreg Roach        $family = Family::getInstance($xref, $tree);
4475a78cd34SGreg Roach
4485a78cd34SGreg Roach        if ($family === null) {
44959f2f229SGreg Roach            throw new FamilyNotFoundException();
4505a78cd34SGreg Roach        }
4515a78cd34SGreg Roach
4525a78cd34SGreg Roach        switch ($option) {
4535a78cd34SGreg Roach            case 'parents':
4545a78cd34SGreg Roach                $this->addFamilyToCart($family);
4555a78cd34SGreg Roach                break;
4565a78cd34SGreg Roach
4575a78cd34SGreg Roach            case 'members':
4585a78cd34SGreg Roach                $this->addFamilyAndChildrenToCart($family);
4595a78cd34SGreg Roach                break;
4605a78cd34SGreg Roach
4615a78cd34SGreg Roach            case 'descendants':
4625a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($family);
4635a78cd34SGreg Roach                break;
4645a78cd34SGreg Roach        }
4655a78cd34SGreg Roach
4666ccdf4f0SGreg Roach        return redirect($family->url());
4675a78cd34SGreg Roach    }
4685a78cd34SGreg Roach
4695a78cd34SGreg Roach    /**
4705a78cd34SGreg Roach     * @param Family $family
47118d7a90dSGreg Roach     *
47218d7a90dSGreg Roach     * @return void
4735a78cd34SGreg Roach     */
474e364afe4SGreg Roach    private function addFamilyToCart(Family $family): void
475c1010edaSGreg Roach    {
4765a78cd34SGreg Roach        $this->addRecordToCart($family);
4775a78cd34SGreg Roach
47839ca88baSGreg Roach        foreach ($family->spouses() as $spouse) {
4795a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4805a78cd34SGreg Roach        }
4815a78cd34SGreg Roach    }
4825a78cd34SGreg Roach
4835a78cd34SGreg Roach    /**
4845a78cd34SGreg Roach     * @param Family $family
48518d7a90dSGreg Roach     *
48618d7a90dSGreg Roach     * @return void
4875a78cd34SGreg Roach     */
488e364afe4SGreg Roach    private function addFamilyAndChildrenToCart(Family $family): void
489c1010edaSGreg Roach    {
4905a78cd34SGreg Roach        $this->addRecordToCart($family);
4915a78cd34SGreg Roach
49239ca88baSGreg Roach        foreach ($family->spouses() as $spouse) {
4935a78cd34SGreg Roach            $this->addRecordToCart($spouse);
4945a78cd34SGreg Roach        }
49539ca88baSGreg Roach        foreach ($family->children() as $child) {
4965a78cd34SGreg Roach            $this->addRecordToCart($child);
4975a78cd34SGreg Roach        }
4985a78cd34SGreg Roach    }
4995a78cd34SGreg Roach
5005a78cd34SGreg Roach    /**
5015a78cd34SGreg Roach     * @param Family $family
50218d7a90dSGreg Roach     *
50318d7a90dSGreg Roach     * @return void
5045a78cd34SGreg Roach     */
505e364afe4SGreg Roach    private function addFamilyAndDescendantsToCart(Family $family): void
506c1010edaSGreg Roach    {
5075a78cd34SGreg Roach        $this->addRecordToCart($family);
5085a78cd34SGreg Roach
50939ca88baSGreg Roach        foreach ($family->spouses() as $spouse) {
5105a78cd34SGreg Roach            $this->addRecordToCart($spouse);
5115a78cd34SGreg Roach        }
51239ca88baSGreg Roach        foreach ($family->children() as $child) {
5135a78cd34SGreg Roach            $this->addRecordToCart($child);
51439ca88baSGreg Roach            foreach ($child->spouseFamilies() as $child_family) {
5155a78cd34SGreg Roach                $this->addFamilyAndDescendantsToCart($child_family);
5165a78cd34SGreg Roach            }
5175a78cd34SGreg Roach        }
5185a78cd34SGreg Roach    }
5195a78cd34SGreg Roach
5205a78cd34SGreg Roach    /**
5216ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
522b6db7c1fSGreg Roach     * @param Tree                   $tree
5235a78cd34SGreg Roach     *
5246ccdf4f0SGreg Roach     * @return ResponseInterface
5255a78cd34SGreg Roach     */
5266ccdf4f0SGreg Roach    public function getAddIndividualAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
527c1010edaSGreg Roach    {
528bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
5295a78cd34SGreg Roach
5305a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
5315a78cd34SGreg Roach
5325a78cd34SGreg Roach        if ($individual === null) {
53359f2f229SGreg Roach            throw new IndividualNotFoundException();
5345a78cd34SGreg Roach        }
5355a78cd34SGreg Roach
5365a78cd34SGreg Roach        $options = $this->individualOptions($individual);
5375a78cd34SGreg Roach
53839ca88baSGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $individual->fullName());
5395a78cd34SGreg Roach
5405a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
5415a78cd34SGreg Roach            'options' => $options,
5425a78cd34SGreg Roach            'default' => key($options),
5435a78cd34SGreg Roach            'record'  => $individual,
5445a78cd34SGreg Roach            'title'   => $title,
5455a78cd34SGreg Roach            'tree'    => $tree,
5465a78cd34SGreg Roach        ]);
5475a78cd34SGreg Roach    }
5485a78cd34SGreg Roach
5495a78cd34SGreg Roach    /**
5505a78cd34SGreg Roach     * @param Individual $individual
5515a78cd34SGreg Roach     *
5525a78cd34SGreg Roach     * @return string[]
5535a78cd34SGreg Roach     */
554c1010edaSGreg Roach    private function individualOptions(Individual $individual): array
555c1010edaSGreg Roach    {
55639ca88baSGreg Roach        $name = strip_tags($individual->fullName());
5575a78cd34SGreg Roach
55839ca88baSGreg Roach        if ($individual->sex() === 'F') {
5595a78cd34SGreg Roach            return [
5605a78cd34SGreg Roach                'self'              => $name,
5615a78cd34SGreg Roach                'parents'           => I18N::translate('%s, her parents and siblings', $name),
5625a78cd34SGreg Roach                'spouses'           => I18N::translate('%s, her spouses and children', $name),
5635a78cd34SGreg Roach                'ancestors'         => I18N::translate('%s and her ancestors', $name),
5645a78cd34SGreg Roach                'ancestor_families' => I18N::translate('%s, her ancestors and their families', $name),
5655a78cd34SGreg Roach                'descendants'       => I18N::translate('%s, her spouses and descendants', $name),
5665a78cd34SGreg Roach            ];
567b2ce94c6SRico Sonntag        }
568b2ce94c6SRico Sonntag
5695a78cd34SGreg Roach        return [
5705a78cd34SGreg Roach            'self'              => $name,
5715a78cd34SGreg Roach            'parents'           => I18N::translate('%s, his parents and siblings', $name),
5725a78cd34SGreg Roach            'spouses'           => I18N::translate('%s, his spouses and children', $name),
5735a78cd34SGreg Roach            'ancestors'         => I18N::translate('%s and his ancestors', $name),
5745a78cd34SGreg Roach            'ancestor_families' => I18N::translate('%s, his ancestors and their families', $name),
5755a78cd34SGreg Roach            'descendants'       => I18N::translate('%s, his spouses and descendants', $name),
5765a78cd34SGreg Roach        ];
5775a78cd34SGreg Roach    }
5785a78cd34SGreg Roach
5795a78cd34SGreg Roach    /**
5806ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
581b6db7c1fSGreg Roach     * @param Tree                   $tree
5825a78cd34SGreg Roach     *
5836ccdf4f0SGreg Roach     * @return ResponseInterface
5845a78cd34SGreg Roach     */
5856ccdf4f0SGreg Roach    public function postAddIndividualAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
586c1010edaSGreg Roach    {
587bed27cedSGreg Roach        $xref   = $request->getQueryParams()['xref'];
588bed27cedSGreg Roach        $option = $request->getParsedBody()['option'];
5895a78cd34SGreg Roach
5905a78cd34SGreg Roach        $individual = Individual::getInstance($xref, $tree);
5915a78cd34SGreg Roach
5925a78cd34SGreg Roach        if ($individual === null) {
59359f2f229SGreg Roach            throw new IndividualNotFoundException();
5945a78cd34SGreg Roach        }
5955a78cd34SGreg Roach
5965a78cd34SGreg Roach        switch ($option) {
5975a78cd34SGreg Roach            case 'self':
5985a78cd34SGreg Roach                $this->addRecordToCart($individual);
5995a78cd34SGreg Roach                break;
6005a78cd34SGreg Roach
6015a78cd34SGreg Roach            case 'parents':
60239ca88baSGreg Roach                foreach ($individual->childFamilies() as $family) {
6035a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
6045a78cd34SGreg Roach                }
6055a78cd34SGreg Roach                break;
6065a78cd34SGreg Roach
6075a78cd34SGreg Roach            case 'spouses':
60839ca88baSGreg Roach                foreach ($individual->spouseFamilies() as $family) {
6095a78cd34SGreg Roach                    $this->addFamilyAndChildrenToCart($family);
6105a78cd34SGreg Roach                }
6115a78cd34SGreg Roach                break;
6125a78cd34SGreg Roach
6135a78cd34SGreg Roach            case 'ancestors':
6145a78cd34SGreg Roach                $this->addAncestorsToCart($individual);
6155a78cd34SGreg Roach                break;
6165a78cd34SGreg Roach
6175a78cd34SGreg Roach            case 'ancestor_families':
6185a78cd34SGreg Roach                $this->addAncestorFamiliesToCart($individual);
6195a78cd34SGreg Roach                break;
6205a78cd34SGreg Roach
6215a78cd34SGreg Roach            case 'descendants':
62239ca88baSGreg Roach                foreach ($individual->spouseFamilies() as $family) {
6235a78cd34SGreg Roach                    $this->addFamilyAndDescendantsToCart($family);
6245a78cd34SGreg Roach                }
6255a78cd34SGreg Roach                break;
6265a78cd34SGreg Roach        }
6275a78cd34SGreg Roach
6286ccdf4f0SGreg Roach        return redirect($individual->url());
6295a78cd34SGreg Roach    }
6305a78cd34SGreg Roach
6315a78cd34SGreg Roach    /**
6325a78cd34SGreg Roach     * @param Individual $individual
63318d7a90dSGreg Roach     *
63418d7a90dSGreg Roach     * @return void
6355a78cd34SGreg Roach     */
636e364afe4SGreg Roach    private function addAncestorsToCart(Individual $individual): void
637c1010edaSGreg Roach    {
6385a78cd34SGreg Roach        $this->addRecordToCart($individual);
6395a78cd34SGreg Roach
64039ca88baSGreg Roach        foreach ($individual->childFamilies() as $family) {
64139ca88baSGreg Roach            foreach ($family->spouses() as $parent) {
6425a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
6435a78cd34SGreg Roach            }
6445a78cd34SGreg Roach        }
6455a78cd34SGreg Roach    }
6465a78cd34SGreg Roach
6475a78cd34SGreg Roach    /**
6485a78cd34SGreg Roach     * @param Individual $individual
64918d7a90dSGreg Roach     *
65018d7a90dSGreg Roach     * @return void
6515a78cd34SGreg Roach     */
652e364afe4SGreg Roach    private function addAncestorFamiliesToCart(Individual $individual): void
653c1010edaSGreg Roach    {
65439ca88baSGreg Roach        foreach ($individual->childFamilies() as $family) {
6555a78cd34SGreg Roach            $this->addFamilyAndChildrenToCart($family);
65639ca88baSGreg Roach            foreach ($family->spouses() as $parent) {
6575a78cd34SGreg Roach                $this->addAncestorsToCart($parent);
6585a78cd34SGreg Roach            }
6595a78cd34SGreg Roach        }
6605a78cd34SGreg Roach    }
6615a78cd34SGreg Roach
6625a78cd34SGreg Roach    /**
6636ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
664b6db7c1fSGreg Roach     * @param Tree                   $tree
6655a78cd34SGreg Roach     *
6666ccdf4f0SGreg Roach     * @return ResponseInterface
6675a78cd34SGreg Roach     */
6686ccdf4f0SGreg Roach    public function getAddMediaAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
669c1010edaSGreg Roach    {
670bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
6715a78cd34SGreg Roach
6725a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
6735a78cd34SGreg Roach
6745a78cd34SGreg Roach        if ($media === null) {
67559f2f229SGreg Roach            throw new MediaNotFoundException();
6765a78cd34SGreg Roach        }
6775a78cd34SGreg Roach
6785a78cd34SGreg Roach        $options = $this->mediaOptions($media);
6795a78cd34SGreg Roach
68039ca88baSGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $media->fullName());
6815a78cd34SGreg Roach
6825a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
6835a78cd34SGreg Roach            'options' => $options,
6845a78cd34SGreg Roach            'default' => key($options),
6855a78cd34SGreg Roach            'record'  => $media,
6865a78cd34SGreg Roach            'title'   => $title,
6875a78cd34SGreg Roach            'tree'    => $tree,
6885a78cd34SGreg Roach        ]);
6895a78cd34SGreg Roach    }
6905a78cd34SGreg Roach
6915a78cd34SGreg Roach    /**
6925a78cd34SGreg Roach     * @param Media $media
6935a78cd34SGreg Roach     *
6945a78cd34SGreg Roach     * @return string[]
6955a78cd34SGreg Roach     */
696c1010edaSGreg Roach    private function mediaOptions(Media $media): array
697c1010edaSGreg Roach    {
69839ca88baSGreg Roach        $name = strip_tags($media->fullName());
6995a78cd34SGreg Roach
7005a78cd34SGreg Roach        return [
7015a78cd34SGreg Roach            'self' => $name,
7025a78cd34SGreg Roach        ];
7035a78cd34SGreg Roach    }
7045a78cd34SGreg Roach
7055a78cd34SGreg Roach    /**
7066ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
707b6db7c1fSGreg Roach     * @param Tree                   $tree
7085a78cd34SGreg Roach     *
7096ccdf4f0SGreg Roach     * @return ResponseInterface
7105a78cd34SGreg Roach     */
7116ccdf4f0SGreg Roach    public function postAddMediaAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
712c1010edaSGreg Roach    {
713bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
7145a78cd34SGreg Roach
7155a78cd34SGreg Roach        $media = Media::getInstance($xref, $tree);
7165a78cd34SGreg Roach
7175a78cd34SGreg Roach        if ($media === null) {
71859f2f229SGreg Roach            throw new MediaNotFoundException();
7195a78cd34SGreg Roach        }
7205a78cd34SGreg Roach
7215a78cd34SGreg Roach        $this->addRecordToCart($media);
7225a78cd34SGreg Roach
7236ccdf4f0SGreg Roach        return redirect($media->url());
7245a78cd34SGreg Roach    }
7255a78cd34SGreg Roach
7265a78cd34SGreg Roach    /**
7276ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
728b6db7c1fSGreg Roach     * @param Tree                   $tree
7295a78cd34SGreg Roach     *
7306ccdf4f0SGreg Roach     * @return ResponseInterface
7315a78cd34SGreg Roach     */
7326ccdf4f0SGreg Roach    public function getAddNoteAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
733c1010edaSGreg Roach    {
734bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
7355a78cd34SGreg Roach
7365a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
7375a78cd34SGreg Roach
7385a78cd34SGreg Roach        if ($note === null) {
73959f2f229SGreg Roach            throw new NoteNotFoundException();
7405a78cd34SGreg Roach        }
7415a78cd34SGreg Roach
7425a78cd34SGreg Roach        $options = $this->noteOptions($note);
7435a78cd34SGreg Roach
74439ca88baSGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $note->fullName());
7455a78cd34SGreg Roach
7465a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
7475a78cd34SGreg Roach            'options' => $options,
7485a78cd34SGreg Roach            'default' => key($options),
7495a78cd34SGreg Roach            'record'  => $note,
7505a78cd34SGreg Roach            'title'   => $title,
7515a78cd34SGreg Roach            'tree'    => $tree,
7525a78cd34SGreg Roach        ]);
7535a78cd34SGreg Roach    }
7545a78cd34SGreg Roach
7555a78cd34SGreg Roach    /**
7565a78cd34SGreg Roach     * @param Note $note
7575a78cd34SGreg Roach     *
7585a78cd34SGreg Roach     * @return string[]
7595a78cd34SGreg Roach     */
760c1010edaSGreg Roach    private function noteOptions(Note $note): array
761c1010edaSGreg Roach    {
76239ca88baSGreg Roach        $name = strip_tags($note->fullName());
7635a78cd34SGreg Roach
7645a78cd34SGreg Roach        return [
7655a78cd34SGreg Roach            'self' => $name,
7665a78cd34SGreg Roach        ];
7675a78cd34SGreg Roach    }
7685a78cd34SGreg Roach
7695a78cd34SGreg Roach    /**
7706ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
771b6db7c1fSGreg Roach     * @param Tree                   $tree
7725a78cd34SGreg Roach     *
7736ccdf4f0SGreg Roach     * @return ResponseInterface
7745a78cd34SGreg Roach     */
7756ccdf4f0SGreg Roach    public function postAddNoteAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
776c1010edaSGreg Roach    {
777bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
7785a78cd34SGreg Roach
7795a78cd34SGreg Roach        $note = Note::getInstance($xref, $tree);
7805a78cd34SGreg Roach
7815a78cd34SGreg Roach        if ($note === null) {
78259f2f229SGreg Roach            throw new NoteNotFoundException();
7835a78cd34SGreg Roach        }
7845a78cd34SGreg Roach
7855a78cd34SGreg Roach        $this->addRecordToCart($note);
7865a78cd34SGreg Roach
7876ccdf4f0SGreg Roach        return redirect($note->url());
7885a78cd34SGreg Roach    }
7895a78cd34SGreg Roach
7905a78cd34SGreg Roach    /**
7916ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
792b6db7c1fSGreg Roach     * @param Tree                   $tree
7935a78cd34SGreg Roach     *
7946ccdf4f0SGreg Roach     * @return ResponseInterface
7955a78cd34SGreg Roach     */
7966ccdf4f0SGreg Roach    public function getAddRepositoryAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
797c1010edaSGreg Roach    {
798bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
7995a78cd34SGreg Roach
8005a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
8015a78cd34SGreg Roach
8025a78cd34SGreg Roach        if ($repository === null) {
80359f2f229SGreg Roach            throw new RepositoryNotFoundException();
8045a78cd34SGreg Roach        }
8055a78cd34SGreg Roach
8065a78cd34SGreg Roach        $options = $this->repositoryOptions($repository);
8075a78cd34SGreg Roach
80839ca88baSGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $repository->fullName());
8095a78cd34SGreg Roach
8105a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
8115a78cd34SGreg Roach            'options' => $options,
8125a78cd34SGreg Roach            'default' => key($options),
8135a78cd34SGreg Roach            'record'  => $repository,
8145a78cd34SGreg Roach            'title'   => $title,
8155a78cd34SGreg Roach            'tree'    => $tree,
8165a78cd34SGreg Roach        ]);
8175a78cd34SGreg Roach    }
8185a78cd34SGreg Roach
8195a78cd34SGreg Roach    /**
8205a78cd34SGreg Roach     * @param Repository $repository
8215a78cd34SGreg Roach     *
8225a78cd34SGreg Roach     * @return string[]
8235a78cd34SGreg Roach     */
824c1010edaSGreg Roach    private function repositoryOptions(Repository $repository): array
825c1010edaSGreg Roach    {
82639ca88baSGreg Roach        $name = strip_tags($repository->fullName());
8275a78cd34SGreg Roach
8285a78cd34SGreg Roach        return [
8295a78cd34SGreg Roach            'self' => $name,
8305a78cd34SGreg Roach        ];
8315a78cd34SGreg Roach    }
8325a78cd34SGreg Roach
8335a78cd34SGreg Roach    /**
8346ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
835b6db7c1fSGreg Roach     * @param Tree                   $tree
8365a78cd34SGreg Roach     *
8376ccdf4f0SGreg Roach     * @return ResponseInterface
8385a78cd34SGreg Roach     */
8396ccdf4f0SGreg Roach    public function postAddRepositoryAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
840c1010edaSGreg Roach    {
841bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
8425a78cd34SGreg Roach
8435a78cd34SGreg Roach        $repository = Repository::getInstance($xref, $tree);
8445a78cd34SGreg Roach
8455a78cd34SGreg Roach        if ($repository === null) {
84659f2f229SGreg Roach            throw new RepositoryNotFoundException();
8475a78cd34SGreg Roach        }
8485a78cd34SGreg Roach
8495a78cd34SGreg Roach        $this->addRecordToCart($repository);
8505a78cd34SGreg Roach
8516ccdf4f0SGreg Roach        return redirect($repository->url());
8525a78cd34SGreg Roach    }
8535a78cd34SGreg Roach
8545a78cd34SGreg Roach    /**
8556ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
856b6db7c1fSGreg Roach     * @param Tree                   $tree
8575a78cd34SGreg Roach     *
8586ccdf4f0SGreg Roach     * @return ResponseInterface
8595a78cd34SGreg Roach     */
8606ccdf4f0SGreg Roach    public function getAddSourceAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
861c1010edaSGreg Roach    {
862bed27cedSGreg Roach        $xref = $request->getQueryParams()['xref'];
8635a78cd34SGreg Roach
8645a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
8655a78cd34SGreg Roach
8665a78cd34SGreg Roach        if ($source === null) {
86759f2f229SGreg Roach            throw new SourceNotFoundException();
8685a78cd34SGreg Roach        }
8695a78cd34SGreg Roach
8705a78cd34SGreg Roach        $options = $this->sourceOptions($source);
8715a78cd34SGreg Roach
87239ca88baSGreg Roach        $title = I18N::translate('Add %s to the clippings cart', $source->fullName());
8735a78cd34SGreg Roach
8745a78cd34SGreg Roach        return $this->viewResponse('modules/clippings/add-options', [
8755a78cd34SGreg Roach            'options' => $options,
8765a78cd34SGreg Roach            'default' => key($options),
8775a78cd34SGreg Roach            'record'  => $source,
8785a78cd34SGreg Roach            'title'   => $title,
8795a78cd34SGreg Roach            'tree'    => $tree,
8805a78cd34SGreg Roach        ]);
8815a78cd34SGreg Roach    }
8825a78cd34SGreg Roach
8835a78cd34SGreg Roach    /**
8845a78cd34SGreg Roach     * @param Source $source
8855a78cd34SGreg Roach     *
8865a78cd34SGreg Roach     * @return string[]
8875a78cd34SGreg Roach     */
888c1010edaSGreg Roach    private function sourceOptions(Source $source): array
889c1010edaSGreg Roach    {
89039ca88baSGreg Roach        $name = strip_tags($source->fullName());
8915a78cd34SGreg Roach
8925a78cd34SGreg Roach        return [
89339ca88baSGreg Roach            'only'   => strip_tags($source->fullName()),
8945a78cd34SGreg Roach            'linked' => I18N::translate('%s and the individuals that reference it.', $name),
8955a78cd34SGreg Roach        ];
8965a78cd34SGreg Roach    }
8975a78cd34SGreg Roach
8985a78cd34SGreg Roach    /**
8996ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
900b6db7c1fSGreg Roach     * @param Tree                   $tree
9015a78cd34SGreg Roach     *
9026ccdf4f0SGreg Roach     * @return ResponseInterface
9035a78cd34SGreg Roach     */
9046ccdf4f0SGreg Roach    public function postAddSourceAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
905c1010edaSGreg Roach    {
906bed27cedSGreg Roach        $xref   = $request->getQueryParams()['xref'];
907bed27cedSGreg Roach        $option = $request->getParsedBody()['option'];
9085a78cd34SGreg Roach
9095a78cd34SGreg Roach        $source = Source::getInstance($xref, $tree);
9105a78cd34SGreg Roach
9115a78cd34SGreg Roach        if ($source === null) {
91259f2f229SGreg Roach            throw new SourceNotFoundException();
9135a78cd34SGreg Roach        }
9145a78cd34SGreg Roach
9155a78cd34SGreg Roach        $this->addRecordToCart($source);
9165a78cd34SGreg Roach
9175a78cd34SGreg Roach        if ($option === 'linked') {
9185a78cd34SGreg Roach            foreach ($source->linkedIndividuals('SOUR') as $individual) {
9195a78cd34SGreg Roach                $this->addRecordToCart($individual);
9205a78cd34SGreg Roach            }
9215a78cd34SGreg Roach            foreach ($source->linkedFamilies('SOUR') as $family) {
9225a78cd34SGreg Roach                $this->addRecordToCart($family);
9235a78cd34SGreg Roach            }
9245a78cd34SGreg Roach        }
9255a78cd34SGreg Roach
9266ccdf4f0SGreg Roach        return redirect($source->url());
9275a78cd34SGreg Roach    }
9285a78cd34SGreg Roach
9295a78cd34SGreg Roach    /**
9305a78cd34SGreg Roach     * Get all the records in the cart.
9315a78cd34SGreg Roach     *
9325a78cd34SGreg Roach     * @param Tree $tree
9335a78cd34SGreg Roach     *
9345a78cd34SGreg Roach     * @return GedcomRecord[]
9355a78cd34SGreg Roach     */
936c1010edaSGreg Roach    private function allRecordsInCart(Tree $tree): array
937c1010edaSGreg Roach    {
9385a78cd34SGreg Roach        $cart = Session::get('cart', []);
9395a78cd34SGreg Roach
940aa6f03bbSGreg Roach        $xrefs = array_keys($cart[$tree->name()] ?? []);
9415a78cd34SGreg Roach
9425a78cd34SGreg Roach        // Fetch all the records in the cart.
943bed27cedSGreg Roach        $records = array_map(static function (string $xref) use ($tree): ?GedcomRecord {
9445a78cd34SGreg Roach            return GedcomRecord::getInstance($xref, $tree);
9455a78cd34SGreg Roach        }, $xrefs);
9465a78cd34SGreg Roach
9475a78cd34SGreg Roach        // Some records may have been deleted after they were added to the cart.
9485a78cd34SGreg Roach        $records = array_filter($records);
9495a78cd34SGreg Roach
9505a78cd34SGreg Roach        // Group and sort.
9510b5fd0a6SGreg Roach        uasort($records, static function (GedcomRecord $x, GedcomRecord $y): int {
952c156e8f5SGreg Roach            return $x::RECORD_TYPE <=> $y::RECORD_TYPE ?: GedcomRecord::nameComparator()($x, $y);
9535a78cd34SGreg Roach        });
9545a78cd34SGreg Roach
9555a78cd34SGreg Roach        return $records;
9565a78cd34SGreg Roach    }
9575a78cd34SGreg Roach
9585a78cd34SGreg Roach    /**
9595a78cd34SGreg Roach     * Add a record (and direclty linked sources, notes, etc. to the cart.
9605a78cd34SGreg Roach     *
9615a78cd34SGreg Roach     * @param GedcomRecord $record
96218d7a90dSGreg Roach     *
96318d7a90dSGreg Roach     * @return void
9645a78cd34SGreg Roach     */
965e364afe4SGreg Roach    private function addRecordToCart(GedcomRecord $record): void
966c1010edaSGreg Roach    {
9675a78cd34SGreg Roach        $cart = Session::get('cart', []);
9685a78cd34SGreg Roach
969f4afa648SGreg Roach        $tree_name = $record->tree()->name();
9705a78cd34SGreg Roach
9715a78cd34SGreg Roach        // Add this record
972c0935879SGreg Roach        $cart[$tree_name][$record->xref()] = true;
9735a78cd34SGreg Roach
9745a78cd34SGreg Roach        // Add directly linked media, notes, repositories and sources.
9758d0ebef0SGreg Roach        preg_match_all('/\n\d (?:OBJE|NOTE|SOUR|REPO) @(' . Gedcom::REGEX_XREF . ')@/', $record->gedcom(), $matches);
9765a78cd34SGreg Roach
9775a78cd34SGreg Roach        foreach ($matches[1] as $match) {
9785a78cd34SGreg Roach            $cart[$tree_name][$match] = true;
9795a78cd34SGreg Roach        }
9805a78cd34SGreg Roach
9815a78cd34SGreg Roach        Session::put('cart', $cart);
9825a78cd34SGreg Roach    }
9835a78cd34SGreg Roach
9845a78cd34SGreg Roach    /**
9855a78cd34SGreg Roach     * @param Tree $tree
9865a78cd34SGreg Roach     *
9875a78cd34SGreg Roach     * @return bool
9885a78cd34SGreg Roach     */
989c1010edaSGreg Roach    private function isCartEmpty(Tree $tree): bool
990c1010edaSGreg Roach    {
9915a78cd34SGreg Roach        $cart = Session::get('cart', []);
9925a78cd34SGreg Roach
993aa6f03bbSGreg Roach        return empty($cart[$tree->name()]);
9945a78cd34SGreg Roach    }
9958c2e8227SGreg Roach}
996