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