xref: /webtrees/app/Module/ClippingsCartModule.php (revision cf7c85896a54223772c76826381a9151d4a21e10)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2020 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Aura\Router\Route;
23use Fisharebest\Webtrees\Auth;
24use Fisharebest\Webtrees\Exceptions\FamilyNotFoundException;
25use Fisharebest\Webtrees\Exceptions\IndividualNotFoundException;
26use Fisharebest\Webtrees\Exceptions\MediaNotFoundException;
27use Fisharebest\Webtrees\Exceptions\NoteNotFoundException;
28use Fisharebest\Webtrees\Exceptions\RepositoryNotFoundException;
29use Fisharebest\Webtrees\Exceptions\SourceNotFoundException;
30use Fisharebest\Webtrees\Factory;
31use Fisharebest\Webtrees\Family;
32use Fisharebest\Webtrees\Functions\FunctionsExport;
33use Fisharebest\Webtrees\Gedcom;
34use Fisharebest\Webtrees\GedcomRecord;
35use Fisharebest\Webtrees\Http\RequestHandlers\FamilyPage;
36use Fisharebest\Webtrees\Http\RequestHandlers\IndividualPage;
37use Fisharebest\Webtrees\Http\RequestHandlers\MediaPage;
38use Fisharebest\Webtrees\Http\RequestHandlers\NotePage;
39use Fisharebest\Webtrees\Http\RequestHandlers\RepositoryPage;
40use Fisharebest\Webtrees\Http\RequestHandlers\SourcePage;
41use Fisharebest\Webtrees\I18N;
42use Fisharebest\Webtrees\Individual;
43use Fisharebest\Webtrees\Media;
44use Fisharebest\Webtrees\Menu;
45use Fisharebest\Webtrees\Note;
46use Fisharebest\Webtrees\Repository;
47use Fisharebest\Webtrees\Services\UserService;
48use Fisharebest\Webtrees\Session;
49use Fisharebest\Webtrees\Source;
50use Fisharebest\Webtrees\Tree;
51use League\Flysystem\Filesystem;
52use League\Flysystem\FilesystemInterface;
53use League\Flysystem\MountManager;
54use League\Flysystem\ZipArchive\ZipArchiveAdapter;
55use Psr\Http\Message\ResponseFactoryInterface;
56use Psr\Http\Message\ResponseInterface;
57use Psr\Http\Message\ServerRequestInterface;
58use Psr\Http\Message\StreamFactoryInterface;
59
60use function app;
61use function array_filter;
62use function array_keys;
63use function array_map;
64use function assert;
65use function in_array;
66use function is_string;
67use function key;
68use function preg_match_all;
69use function redirect;
70use function route;
71use function str_replace;
72use function strip_tags;
73use function utf8_decode;
74
75/**
76 * Class ClippingsCartModule
77 */
78class ClippingsCartModule extends AbstractModule implements ModuleMenuInterface
79{
80    use ModuleMenuTrait;
81
82    // Routes that have a record which can be added to the clipboard
83    private const ROUTES_WITH_RECORDS = [
84        'Family'     => FamilyPage::class,
85        'Individual' => IndividualPage::class,
86        'Media'      => MediaPage::class,
87        'Note'       => NotePage::class,
88        'Repository' => RepositoryPage::class,
89        'Source'     => SourcePage::class,
90    ];
91
92    /** @var int The default access level for this module.  It can be changed in the control panel. */
93    protected $access_level = Auth::PRIV_USER;
94
95    /**
96     * @var UserService
97     */
98    private $user_service;
99
100    /**
101     * ClippingsCartModule constructor.
102     *
103     * @param UserService $user_service
104     */
105    public function __construct(UserService $user_service)
106    {
107        $this->user_service = $user_service;
108    }
109
110    /**
111     * How should this module be identified in the control panel, etc.?
112     *
113     * @return string
114     */
115    public function title(): string
116    {
117        /* I18N: Name of a module */
118        return I18N::translate('Clippings cart');
119    }
120
121    /**
122     * A sentence describing what this module does.
123     *
124     * @return string
125     */
126    public function description(): string
127    {
128        /* I18N: Description of the “Clippings cart” module */
129        return I18N::translate('Select records from your family tree and save them as a GEDCOM file.');
130    }
131
132    /**
133     * The default position for this menu.  It can be changed in the control panel.
134     *
135     * @return int
136     */
137    public function defaultMenuOrder(): int
138    {
139        return 6;
140    }
141
142    /**
143     * A menu, to be added to the main application menu.
144     *
145     * @param Tree $tree
146     *
147     * @return Menu|null
148     */
149    public function getMenu(Tree $tree): ?Menu
150    {
151        /** @var ServerRequestInterface $request */
152        $request = app(ServerRequestInterface::class);
153
154        $route = $request->getAttribute('route');
155        assert($route instanceof Route);
156
157        $submenus = [
158            new Menu($this->title(), route('module', [
159                'module' => $this->name(),
160                'action' => 'Show',
161                'tree'    => $tree->name(),
162            ]), 'menu-clippings-cart', ['rel' => 'nofollow']),
163        ];
164
165        $action = array_search($route->name, self::ROUTES_WITH_RECORDS, true);
166        if ($action !== false) {
167            $xref = $route->attributes['xref'];
168            assert(is_string($xref));
169
170            $add_route = route('module', [
171                'module' => $this->name(),
172                'action' => 'Add' . $action,
173                'xref'   => $xref,
174                'tree'    => $tree->name(),
175            ]);
176
177            $submenus[] = new Menu(I18N::translate('Add to the clippings cart'), $add_route, 'menu-clippings-add', ['rel' => 'nofollow']);
178        }
179
180        if (!$this->isCartEmpty($tree)) {
181            $submenus[] = new Menu(I18N::translate('Empty the clippings cart'), route('module', [
182                'module' => $this->name(),
183                'action' => 'Empty',
184                'tree'    => $tree->name(),
185            ]), 'menu-clippings-empty', ['rel' => 'nofollow']);
186
187            $submenus[] = new Menu(I18N::translate('Download'), route('module', [
188                'module' => $this->name(),
189                'action' => 'DownloadForm',
190                'tree'    => $tree->name(),
191            ]), 'menu-clippings-download', ['rel' => 'nofollow']);
192        }
193
194        return new Menu($this->title(), '#', 'menu-clippings', ['rel' => 'nofollow'], $submenus);
195    }
196
197    /**
198     * @param ServerRequestInterface $request
199     *
200     * @return ResponseInterface
201     */
202    public function postDownloadAction(ServerRequestInterface $request): ResponseInterface
203    {
204        $tree = $request->getAttribute('tree');
205        assert($tree instanceof Tree);
206
207        $data_filesystem = $request->getAttribute('filesystem.data');
208        assert($data_filesystem instanceof FilesystemInterface);
209
210        $params = (array) $request->getParsedBody();
211
212        $privatize_export = $params['privatize_export'];
213
214        if ($privatize_export === 'none' && !Auth::isManager($tree)) {
215            $privatize_export = 'member';
216        }
217
218        if ($privatize_export === 'gedadmin' && !Auth::isManager($tree)) {
219            $privatize_export = 'member';
220        }
221
222        if ($privatize_export === 'user' && !Auth::isMember($tree)) {
223            $privatize_export = 'visitor';
224        }
225
226        $convert = (bool) ($params['convert'] ?? false);
227
228        $cart = Session::get('cart', []);
229
230        $xrefs = array_keys($cart[$tree->name()] ?? []);
231        $xrefs = array_map('strval', $xrefs); // PHP converts numeric keys to integers.
232
233        // Create a new/empty .ZIP file
234        $temp_zip_file  = stream_get_meta_data(tmpfile())['uri'];
235        $zip_adapter    = new ZipArchiveAdapter($temp_zip_file);
236        $zip_filesystem = new Filesystem($zip_adapter);
237
238        $manager = new MountManager([
239            'media' => $tree->mediaFilesystem($data_filesystem),
240            'zip'   => $zip_filesystem,
241        ]);
242
243        // Media file prefix
244        $path = $tree->getPreference('MEDIA_DIRECTORY');
245
246        // GEDCOM file header
247        $filetext = FunctionsExport::gedcomHeader($tree, $convert ? 'ANSI' : 'UTF-8');
248
249        switch ($privatize_export) {
250            case 'gedadmin':
251                $access_level = Auth::PRIV_NONE;
252                break;
253            case 'user':
254                $access_level = Auth::PRIV_USER;
255                break;
256            case 'visitor':
257                $access_level = Auth::PRIV_PRIVATE;
258                break;
259            case 'none':
260            default:
261                $access_level = Auth::PRIV_HIDE;
262                break;
263        }
264
265        foreach ($xrefs as $xref) {
266            $object = Factory::gedcomRecord()->make($xref, $tree);
267            // The object may have been deleted since we added it to the cart....
268            if ($object instanceof  GedcomRecord) {
269                $record = $object->privatizeGedcom($access_level);
270                // Remove links to objects that aren't in the cart
271                preg_match_all('/\n1 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[2-9].*)*/', $record, $matches, PREG_SET_ORDER);
272                foreach ($matches as $match) {
273                    if (!in_array($match[1], $xrefs, true)) {
274                        $record = str_replace($match[0], '', $record);
275                    }
276                }
277                preg_match_all('/\n2 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[3-9].*)*/', $record, $matches, PREG_SET_ORDER);
278                foreach ($matches as $match) {
279                    if (!in_array($match[1], $xrefs, true)) {
280                        $record = str_replace($match[0], '', $record);
281                    }
282                }
283                preg_match_all('/\n3 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[4-9].*)*/', $record, $matches, PREG_SET_ORDER);
284                foreach ($matches as $match) {
285                    if (!in_array($match[1], $xrefs, true)) {
286                        $record = str_replace($match[0], '', $record);
287                    }
288                }
289
290                if ($object instanceof Individual || $object instanceof Family) {
291                    $filetext .= $record . "\n";
292                    $filetext .= "1 SOUR @WEBTREES@\n";
293                    $filetext .= '2 PAGE ' . $object->url() . "\n";
294                } elseif ($object instanceof Source) {
295                    $filetext .= $record . "\n";
296                    $filetext .= '1 NOTE ' . $object->url() . "\n";
297                } elseif ($object instanceof Media) {
298                    // Add the media files to the archive
299                    foreach ($object->mediaFiles() as $media_file) {
300                        $from = 'media://' . $media_file->filename();
301                        $to   = 'zip://' . $path . $media_file->filename();
302                        if (!$media_file->isExternal() && $manager->has($from) && !$manager->has($to)) {
303                            $manager->copy($from, $to);
304                        }
305                    }
306                    $filetext .= $record . "\n";
307                } else {
308                    $filetext .= $record . "\n";
309                }
310            }
311        }
312
313        $base_url = $request->getAttribute('base_url');
314
315        // Create a source, to indicate the source of the data.
316        $filetext .= "0 @WEBTREES@ SOUR\n1 TITL " . $base_url . "\n";
317        $author   = $this->user_service->find((int) $tree->getPreference('CONTACT_USER_ID'));
318        if ($author !== null) {
319            $filetext .= '1 AUTH ' . $author->realName() . "\n";
320        }
321        $filetext .= "0 TRLR\n";
322
323        // Make sure the preferred line endings are used
324        $filetext = strtr($filetext, ["\n" => Gedcom::EOL]);
325
326        if ($convert) {
327            $filetext = utf8_decode($filetext);
328        }
329
330        // Finally add the GEDCOM file to the .ZIP file.
331        $zip_filesystem->write('clippings.ged', $filetext);
332
333        // Need to force-close ZipArchive filesystems.
334        $zip_adapter->getArchive()->close();
335
336        // Use a stream, so that we do not have to load the entire file into memory.
337        $stream = app(StreamFactoryInterface::class)->createStreamFromFile($temp_zip_file);
338
339        /** @var ResponseFactoryInterface $response_factory */
340        $response_factory = app(ResponseFactoryInterface::class);
341
342        return $response_factory->createResponse()
343            ->withBody($stream)
344            ->withHeader('Content-Type', 'application/zip')
345            ->withHeader('Content-Disposition', 'attachment; filename="clippings.zip');
346    }
347
348    /**
349     * @param ServerRequestInterface $request
350     *
351     * @return ResponseInterface
352     */
353    public function getDownloadFormAction(ServerRequestInterface $request): ResponseInterface
354    {
355        $tree = $request->getAttribute('tree');
356        assert($tree instanceof Tree);
357
358        $user  = $request->getAttribute('user');
359        $title = I18N::translate('Family tree clippings cart') . ' — ' . I18N::translate('Download');
360
361        return $this->viewResponse('modules/clippings/download', [
362            'is_manager' => Auth::isManager($tree, $user),
363            'is_member'  => Auth::isMember($tree, $user),
364            'module'     => $this->name(),
365            'title'      => $title,
366            'tree'       => $tree,
367        ]);
368    }
369
370    /**
371     * @param ServerRequestInterface $request
372     *
373     * @return ResponseInterface
374     */
375    public function getEmptyAction(ServerRequestInterface $request): ResponseInterface
376    {
377        $tree = $request->getAttribute('tree');
378        assert($tree instanceof Tree);
379
380        $cart                = Session::get('cart', []);
381        $cart[$tree->name()] = [];
382        Session::put('cart', $cart);
383
384        $url = route('module', [
385            'module' => $this->name(),
386            'action' => 'Show',
387            'tree'    => $tree->name(),
388        ]);
389
390        return redirect($url);
391    }
392
393    /**
394     * @param ServerRequestInterface $request
395     *
396     * @return ResponseInterface
397     */
398    public function postRemoveAction(ServerRequestInterface $request): ResponseInterface
399    {
400        $tree = $request->getAttribute('tree');
401        assert($tree instanceof Tree);
402
403        $xref = $request->getQueryParams()['xref'];
404
405        $cart = Session::get('cart', []);
406        unset($cart[$tree->name()][$xref]);
407        Session::put('cart', $cart);
408
409        $url = route('module', [
410            'module' => $this->name(),
411            'action' => 'Show',
412            'tree'    => $tree->name(),
413        ]);
414
415        return redirect($url);
416    }
417
418    /**
419     * @param ServerRequestInterface $request
420     *
421     * @return ResponseInterface
422     */
423    public function getShowAction(ServerRequestInterface $request): ResponseInterface
424    {
425        $tree = $request->getAttribute('tree');
426        assert($tree instanceof Tree);
427
428        return $this->viewResponse('modules/clippings/show', [
429            'records' => $this->allRecordsInCart($tree),
430            'title'   => I18N::translate('Family tree clippings cart'),
431            'tree'    => $tree,
432        ]);
433    }
434
435    /**
436     * @param ServerRequestInterface $request
437     *
438     * @return ResponseInterface
439     */
440    public function getAddFamilyAction(ServerRequestInterface $request): ResponseInterface
441    {
442        $tree = $request->getAttribute('tree');
443        assert($tree instanceof Tree);
444
445        $xref = $request->getQueryParams()['xref'];
446
447        $family = Factory::family()->make($xref, $tree);
448
449        if ($family === null) {
450            throw new FamilyNotFoundException();
451        }
452
453        $options = $this->familyOptions($family);
454
455        $title = I18N::translate('Add %s to the clippings cart', $family->fullName());
456
457        return $this->viewResponse('modules/clippings/add-options', [
458            'options' => $options,
459            'default' => key($options),
460            'record'  => $family,
461            'title'   => $title,
462            'tree'    => $tree,
463        ]);
464    }
465
466    /**
467     * @param Family $family
468     *
469     * @return string[]
470     */
471    private function familyOptions(Family $family): array
472    {
473        $name = strip_tags($family->fullName());
474
475        return [
476            'parents'     => $name,
477            /* I18N: %s is a family (husband + wife) */
478            'members'     => I18N::translate('%s and their children', $name),
479            /* I18N: %s is a family (husband + wife) */
480            'descendants' => I18N::translate('%s and their descendants', $name),
481        ];
482    }
483
484    /**
485     * @param ServerRequestInterface $request
486     *
487     * @return ResponseInterface
488     */
489    public function postAddFamilyAction(ServerRequestInterface $request): ResponseInterface
490    {
491        $tree = $request->getAttribute('tree');
492        assert($tree instanceof Tree);
493
494        $params = (array) $request->getParsedBody();
495
496        $xref   = $params['xref'];
497        $option = $params['option'];
498
499        $family = Factory::family()->make($xref, $tree);
500
501        if ($family === null) {
502            throw new FamilyNotFoundException();
503        }
504
505        switch ($option) {
506            case 'parents':
507                $this->addFamilyToCart($family);
508                break;
509
510            case 'members':
511                $this->addFamilyAndChildrenToCart($family);
512                break;
513
514            case 'descendants':
515                $this->addFamilyAndDescendantsToCart($family);
516                break;
517        }
518
519        return redirect($family->url());
520    }
521
522    /**
523     * @param Family $family
524     *
525     * @return void
526     */
527    private function addFamilyToCart(Family $family): void
528    {
529        $this->addRecordToCart($family);
530
531        foreach ($family->spouses() as $spouse) {
532            $this->addRecordToCart($spouse);
533        }
534    }
535
536    /**
537     * @param Family $family
538     *
539     * @return void
540     */
541    private function addFamilyAndChildrenToCart(Family $family): void
542    {
543        $this->addRecordToCart($family);
544
545        foreach ($family->spouses() as $spouse) {
546            $this->addRecordToCart($spouse);
547        }
548        foreach ($family->children() as $child) {
549            $this->addRecordToCart($child);
550        }
551    }
552
553    /**
554     * @param Family $family
555     *
556     * @return void
557     */
558    private function addFamilyAndDescendantsToCart(Family $family): void
559    {
560        $this->addRecordToCart($family);
561
562        foreach ($family->spouses() as $spouse) {
563            $this->addRecordToCart($spouse);
564        }
565        foreach ($family->children() as $child) {
566            $this->addRecordToCart($child);
567            foreach ($child->spouseFamilies() as $child_family) {
568                $this->addFamilyAndDescendantsToCart($child_family);
569            }
570        }
571    }
572
573    /**
574     * @param ServerRequestInterface $request
575     *
576     * @return ResponseInterface
577     */
578    public function getAddIndividualAction(ServerRequestInterface $request): ResponseInterface
579    {
580        $tree = $request->getAttribute('tree');
581        assert($tree instanceof Tree);
582
583        $xref = $request->getQueryParams()['xref'];
584
585        $individual = Factory::individual()->make($xref, $tree);
586
587        if ($individual === null) {
588            throw new IndividualNotFoundException();
589        }
590
591        $options = $this->individualOptions($individual);
592
593        $title = I18N::translate('Add %s to the clippings cart', $individual->fullName());
594
595        return $this->viewResponse('modules/clippings/add-options', [
596            'options' => $options,
597            'default' => key($options),
598            'record'  => $individual,
599            'title'   => $title,
600            'tree'    => $tree,
601        ]);
602    }
603
604    /**
605     * @param Individual $individual
606     *
607     * @return string[]
608     */
609    private function individualOptions(Individual $individual): array
610    {
611        $name = strip_tags($individual->fullName());
612
613        if ($individual->sex() === 'F') {
614            return [
615                'self'              => $name,
616                'parents'           => I18N::translate('%s, her parents and siblings', $name),
617                'spouses'           => I18N::translate('%s, her spouses and children', $name),
618                'ancestors'         => I18N::translate('%s and her ancestors', $name),
619                'ancestor_families' => I18N::translate('%s, her ancestors and their families', $name),
620                'descendants'       => I18N::translate('%s, her spouses and descendants', $name),
621            ];
622        }
623
624        return [
625            'self'              => $name,
626            'parents'           => I18N::translate('%s, his parents and siblings', $name),
627            'spouses'           => I18N::translate('%s, his spouses and children', $name),
628            'ancestors'         => I18N::translate('%s and his ancestors', $name),
629            'ancestor_families' => I18N::translate('%s, his ancestors and their families', $name),
630            'descendants'       => I18N::translate('%s, his spouses and descendants', $name),
631        ];
632    }
633
634    /**
635     * @param ServerRequestInterface $request
636     *
637     * @return ResponseInterface
638     */
639    public function postAddIndividualAction(ServerRequestInterface $request): ResponseInterface
640    {
641        $tree = $request->getAttribute('tree');
642        assert($tree instanceof Tree);
643
644        $params = (array) $request->getParsedBody();
645
646        $xref   = $params['xref'];
647        $option = $params['option'];
648
649        $individual = Factory::individual()->make($xref, $tree);
650
651        if ($individual === null) {
652            throw new IndividualNotFoundException();
653        }
654
655        switch ($option) {
656            case 'self':
657                $this->addRecordToCart($individual);
658                break;
659
660            case 'parents':
661                foreach ($individual->childFamilies() as $family) {
662                    $this->addFamilyAndChildrenToCart($family);
663                }
664                break;
665
666            case 'spouses':
667                foreach ($individual->spouseFamilies() as $family) {
668                    $this->addFamilyAndChildrenToCart($family);
669                }
670                break;
671
672            case 'ancestors':
673                $this->addAncestorsToCart($individual);
674                break;
675
676            case 'ancestor_families':
677                $this->addAncestorFamiliesToCart($individual);
678                break;
679
680            case 'descendants':
681                foreach ($individual->spouseFamilies() as $family) {
682                    $this->addFamilyAndDescendantsToCart($family);
683                }
684                break;
685        }
686
687        return redirect($individual->url());
688    }
689
690    /**
691     * @param Individual $individual
692     *
693     * @return void
694     */
695    private function addAncestorsToCart(Individual $individual): void
696    {
697        $this->addRecordToCart($individual);
698
699        foreach ($individual->childFamilies() as $family) {
700            $this->addRecordToCart($family);
701
702            foreach ($family->spouses() as $parent) {
703                $this->addAncestorsToCart($parent);
704            }
705        }
706    }
707
708    /**
709     * @param Individual $individual
710     *
711     * @return void
712     */
713    private function addAncestorFamiliesToCart(Individual $individual): void
714    {
715        foreach ($individual->childFamilies() as $family) {
716            $this->addFamilyAndChildrenToCart($family);
717
718            foreach ($family->spouses() as $parent) {
719                $this->addAncestorFamiliesToCart($parent);
720            }
721        }
722    }
723
724    /**
725     * @param ServerRequestInterface $request
726     *
727     * @return ResponseInterface
728     */
729    public function getAddMediaAction(ServerRequestInterface $request): ResponseInterface
730    {
731        $tree = $request->getAttribute('tree');
732        assert($tree instanceof Tree);
733
734        $xref = $request->getQueryParams()['xref'];
735
736        $media = Factory::media()->make($xref, $tree);
737
738        if ($media === null) {
739            throw new MediaNotFoundException();
740        }
741
742        $options = $this->mediaOptions($media);
743
744        $title = I18N::translate('Add %s to the clippings cart', $media->fullName());
745
746        return $this->viewResponse('modules/clippings/add-options', [
747            'options' => $options,
748            'default' => key($options),
749            'record'  => $media,
750            'title'   => $title,
751            'tree'    => $tree,
752        ]);
753    }
754
755    /**
756     * @param Media $media
757     *
758     * @return string[]
759     */
760    private function mediaOptions(Media $media): array
761    {
762        $name = strip_tags($media->fullName());
763
764        return [
765            'self' => $name,
766        ];
767    }
768
769    /**
770     * @param ServerRequestInterface $request
771     *
772     * @return ResponseInterface
773     */
774    public function postAddMediaAction(ServerRequestInterface $request): ResponseInterface
775    {
776        $tree = $request->getAttribute('tree');
777        assert($tree instanceof Tree);
778
779        $xref = $request->getQueryParams()['xref'];
780
781        $media = Factory::media()->make($xref, $tree);
782
783        if ($media === null) {
784            throw new MediaNotFoundException();
785        }
786
787        $this->addRecordToCart($media);
788
789        return redirect($media->url());
790    }
791
792    /**
793     * @param ServerRequestInterface $request
794     *
795     * @return ResponseInterface
796     */
797    public function getAddNoteAction(ServerRequestInterface $request): ResponseInterface
798    {
799        $tree = $request->getAttribute('tree');
800        assert($tree instanceof Tree);
801
802        $xref = $request->getQueryParams()['xref'];
803
804        $note = Factory::note()->make($xref, $tree);
805
806        if ($note === null) {
807            throw new NoteNotFoundException();
808        }
809
810        $options = $this->noteOptions($note);
811
812        $title = I18N::translate('Add %s to the clippings cart', $note->fullName());
813
814        return $this->viewResponse('modules/clippings/add-options', [
815            'options' => $options,
816            'default' => key($options),
817            'record'  => $note,
818            'title'   => $title,
819            'tree'    => $tree,
820        ]);
821    }
822
823    /**
824     * @param Note $note
825     *
826     * @return string[]
827     */
828    private function noteOptions(Note $note): array
829    {
830        $name = strip_tags($note->fullName());
831
832        return [
833            'self' => $name,
834        ];
835    }
836
837    /**
838     * @param ServerRequestInterface $request
839     *
840     * @return ResponseInterface
841     */
842    public function postAddNoteAction(ServerRequestInterface $request): ResponseInterface
843    {
844        $tree = $request->getAttribute('tree');
845        assert($tree instanceof Tree);
846
847        $xref = $request->getQueryParams()['xref'];
848
849        $note = Factory::note()->make($xref, $tree);
850
851        if ($note === null) {
852            throw new NoteNotFoundException();
853        }
854
855        $this->addRecordToCart($note);
856
857        return redirect($note->url());
858    }
859
860    /**
861     * @param ServerRequestInterface $request
862     *
863     * @return ResponseInterface
864     */
865    public function getAddRepositoryAction(ServerRequestInterface $request): ResponseInterface
866    {
867        $tree = $request->getAttribute('tree');
868        assert($tree instanceof Tree);
869
870        $xref = $request->getQueryParams()['xref'];
871
872        $repository = Factory::repository()->make($xref, $tree);
873
874        if ($repository === null) {
875            throw new RepositoryNotFoundException();
876        }
877
878        $options = $this->repositoryOptions($repository);
879
880        $title = I18N::translate('Add %s to the clippings cart', $repository->fullName());
881
882        return $this->viewResponse('modules/clippings/add-options', [
883            'options' => $options,
884            'default' => key($options),
885            'record'  => $repository,
886            'title'   => $title,
887            'tree'    => $tree,
888        ]);
889    }
890
891    /**
892     * @param Repository $repository
893     *
894     * @return string[]
895     */
896    private function repositoryOptions(Repository $repository): array
897    {
898        $name = strip_tags($repository->fullName());
899
900        return [
901            'self' => $name,
902        ];
903    }
904
905    /**
906     * @param ServerRequestInterface $request
907     *
908     * @return ResponseInterface
909     */
910    public function postAddRepositoryAction(ServerRequestInterface $request): ResponseInterface
911    {
912        $tree = $request->getAttribute('tree');
913        assert($tree instanceof Tree);
914
915        $xref = $request->getQueryParams()['xref'];
916
917        $repository = Factory::repository()->make($xref, $tree);
918
919        if ($repository === null) {
920            throw new RepositoryNotFoundException();
921        }
922
923        $this->addRecordToCart($repository);
924
925        return redirect($repository->url());
926    }
927
928    /**
929     * @param ServerRequestInterface $request
930     *
931     * @return ResponseInterface
932     */
933    public function getAddSourceAction(ServerRequestInterface $request): ResponseInterface
934    {
935        $tree = $request->getAttribute('tree');
936        assert($tree instanceof Tree);
937
938        $xref = $request->getQueryParams()['xref'];
939
940        $source = Factory::source()->make($xref, $tree);
941
942        if ($source === null) {
943            throw new SourceNotFoundException();
944        }
945
946        $options = $this->sourceOptions($source);
947
948        $title = I18N::translate('Add %s to the clippings cart', $source->fullName());
949
950        return $this->viewResponse('modules/clippings/add-options', [
951            'options' => $options,
952            'default' => key($options),
953            'record'  => $source,
954            'title'   => $title,
955            'tree'    => $tree,
956        ]);
957    }
958
959    /**
960     * @param Source $source
961     *
962     * @return string[]
963     */
964    private function sourceOptions(Source $source): array
965    {
966        $name = strip_tags($source->fullName());
967
968        return [
969            'only'   => strip_tags($source->fullName()),
970            'linked' => I18N::translate('%s and the individuals that reference it.', $name),
971        ];
972    }
973
974    /**
975     * @param ServerRequestInterface $request
976     *
977     * @return ResponseInterface
978     */
979    public function postAddSourceAction(ServerRequestInterface $request): ResponseInterface
980    {
981        $tree = $request->getAttribute('tree');
982        assert($tree instanceof Tree);
983
984        $params = (array) $request->getParsedBody();
985
986        $xref   = $params['xref'];
987        $option = $params['option'];
988
989        $source = Factory::source()->make($xref, $tree);
990
991        if ($source === null) {
992            throw new SourceNotFoundException();
993        }
994
995        $this->addRecordToCart($source);
996
997        if ($option === 'linked') {
998            foreach ($source->linkedIndividuals('SOUR') as $individual) {
999                $this->addRecordToCart($individual);
1000            }
1001            foreach ($source->linkedFamilies('SOUR') as $family) {
1002                $this->addRecordToCart($family);
1003            }
1004        }
1005
1006        return redirect($source->url());
1007    }
1008
1009    /**
1010     * Get all the records in the cart.
1011     *
1012     * @param Tree $tree
1013     *
1014     * @return GedcomRecord[]
1015     */
1016    private function allRecordsInCart(Tree $tree): array
1017    {
1018        $cart = Session::get('cart', []);
1019
1020        $xrefs = array_keys($cart[$tree->name()] ?? []);
1021        $xrefs = array_map('strval', $xrefs); // PHP converts numeric keys to integers.
1022
1023        // Fetch all the records in the cart.
1024        $records = array_map(static function (string $xref) use ($tree): ?GedcomRecord {
1025            return Factory::gedcomRecord()->make($xref, $tree);
1026        }, $xrefs);
1027
1028        // Some records may have been deleted after they were added to the cart.
1029        $records = array_filter($records);
1030
1031        // Group and sort.
1032        uasort($records, static function (GedcomRecord $x, GedcomRecord $y): int {
1033            return $x::RECORD_TYPE <=> $y::RECORD_TYPE ?: GedcomRecord::nameComparator()($x, $y);
1034        });
1035
1036        return $records;
1037    }
1038
1039    /**
1040     * Add a record (and direclty linked sources, notes, etc. to the cart.
1041     *
1042     * @param GedcomRecord $record
1043     *
1044     * @return void
1045     */
1046    private function addRecordToCart(GedcomRecord $record): void
1047    {
1048        $cart = Session::get('cart', []);
1049
1050        $tree_name = $record->tree()->name();
1051
1052        // Add this record
1053        $cart[$tree_name][$record->xref()] = true;
1054
1055        // Add directly linked media, notes, repositories and sources.
1056        preg_match_all('/\n\d (?:OBJE|NOTE|SOUR|REPO) @(' . Gedcom::REGEX_XREF . ')@/', $record->gedcom(), $matches);
1057
1058        foreach ($matches[1] as $match) {
1059            $cart[$tree_name][$match] = true;
1060        }
1061
1062        Session::put('cart', $cart);
1063    }
1064
1065    /**
1066     * @param Tree $tree
1067     *
1068     * @return bool
1069     */
1070    private function isCartEmpty(Tree $tree): bool
1071    {
1072        $cart     = Session::get('cart', []);
1073        $contents = $cart[$tree->name()] ?? [];
1074
1075        return $contents === [];
1076    }
1077}
1078