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