xref: /webtrees/app/Module/ClippingsCartModule.php (revision 39b00ae97f08f1096ebe5846700f5ed4f140ea3b)
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        // Create a source, to indicate the source of the data.
261        $filetext .= "0 @WEBTREES@ SOUR\n1 TITL " . WT_BASE_URL . "\n";
262        $author   = $this->user_service->find((int) $tree->getPreference('CONTACT_USER_ID'));
263        if ($author !== null) {
264            $filetext .= '1 AUTH ' . $author->realName() . "\n";
265        }
266        $filetext .= "0 TRLR\n";
267
268        // Make sure the preferred line endings are used
269        $filetext = str_replace('\n', Gedcom::EOL, $filetext);
270
271        if ($convert) {
272            $filetext = utf8_decode($filetext);
273        }
274
275        // Finally add the GEDCOM file to the .ZIP file.
276        $zip_filesystem->write('clippings.ged', $filetext);
277
278        // Need to force-close the filesystem
279        unset($zip_filesystem);
280
281        // Use a stream, so that we do not have to load the entire file into memory.
282        $stream = app(StreamFactoryInterface::class)->createStreamFromFile($temp_zip_file);
283
284        /** @var ResponseFactoryInterface $response_factory */
285        $response_factory = app(ResponseFactoryInterface::class);
286
287        return $response_factory->createResponse()
288            ->withBody($stream)
289            ->withHeader('Content-Type', 'application/zip')
290            ->withHeader('Content-Disposition', 'attachment; filename="clippings.zip');
291    }
292
293    /**
294     * @param Tree          $tree
295     * @param UserInterface $user
296     *
297     * @return ResponseInterface
298     */
299    public function getDownloadFormAction(Tree $tree, UserInterface $user): ResponseInterface
300    {
301        $title = I18N::translate('Family tree clippings cart') . ' — ' . I18N::translate('Download');
302
303        return $this->viewResponse('modules/clippings/download', [
304            'is_manager' => Auth::isManager($tree, $user),
305            'is_member'  => Auth::isMember($tree, $user),
306            'title'      => $title,
307        ]);
308    }
309
310    /**
311     * @param Tree $tree
312     *
313     * @return ResponseInterface
314     */
315    public function getEmptyAction(Tree $tree): ResponseInterface
316    {
317        $cart                = Session::get('cart', []);
318        $cart[$tree->name()] = [];
319        Session::put('cart', $cart);
320
321        $url = route('module', [
322            'module' => $this->name(),
323            'action' => 'Show',
324            'ged'    => $tree->name(),
325        ]);
326
327        return redirect($url);
328    }
329
330    /**
331     * @param ServerRequestInterface $request
332     * @param Tree                   $tree
333     *
334     * @return ResponseInterface
335     */
336    public function postRemoveAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
337    {
338        $xref = $request->getQueryParams()['xref'];
339
340        $cart = Session::get('cart', []);
341        unset($cart[$tree->name()][$xref]);
342        Session::put('cart', $cart);
343
344        $url = route('module', [
345            'module' => $this->name(),
346            'action' => 'Show',
347            'ged'    => $tree->name(),
348        ]);
349
350        return redirect($url);
351    }
352
353    /**
354     * @param Tree $tree
355     *
356     * @return ResponseInterface
357     */
358    public function getShowAction(Tree $tree): ResponseInterface
359    {
360        return $this->viewResponse('modules/clippings/show', [
361            'records' => $this->allRecordsInCart($tree),
362            'title'   => I18N::translate('Family tree clippings cart'),
363            'tree'    => $tree,
364        ]);
365    }
366
367    /**
368     * @param ServerRequestInterface $request
369     * @param Tree                   $tree
370     *
371     * @return ResponseInterface
372     */
373    public function getAddFamilyAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
374    {
375        $xref = $request->getQueryParams()['xref'];
376
377        $family = Family::getInstance($xref, $tree);
378
379        if ($family === null) {
380            throw new FamilyNotFoundException();
381        }
382
383        $options = $this->familyOptions($family);
384
385        $title = I18N::translate('Add %s to the clippings cart', $family->fullName());
386
387        return $this->viewResponse('modules/clippings/add-options', [
388            'options' => $options,
389            'default' => key($options),
390            'record'  => $family,
391            'title'   => $title,
392            'tree'    => $tree,
393        ]);
394    }
395
396    /**
397     * @param Family $family
398     *
399     * @return string[]
400     */
401    private function familyOptions(Family $family): array
402    {
403        $name = strip_tags($family->fullName());
404
405        return [
406            'parents'     => $name,
407            /* I18N: %s is a family (husband + wife) */
408            'members'     => I18N::translate('%s and their children', $name),
409            /* I18N: %s is a family (husband + wife) */
410            'descendants' => I18N::translate('%s and their descendants', $name),
411        ];
412    }
413
414    /**
415     * @param ServerRequestInterface $request
416     * @param Tree                   $tree
417     *
418     * @return ResponseInterface
419     */
420    public function postAddFamilyAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
421    {
422        $xref   = $request->getQueryParams()['xref'];
423        $option = $request->getParsedBody()['option'];
424
425        $family = Family::getInstance($xref, $tree);
426
427        if ($family === null) {
428            throw new FamilyNotFoundException();
429        }
430
431        switch ($option) {
432            case 'parents':
433                $this->addFamilyToCart($family);
434                break;
435
436            case 'members':
437                $this->addFamilyAndChildrenToCart($family);
438                break;
439
440            case 'descendants':
441                $this->addFamilyAndDescendantsToCart($family);
442                break;
443        }
444
445        return redirect($family->url());
446    }
447
448    /**
449     * @param Family $family
450     *
451     * @return void
452     */
453    private function addFamilyToCart(Family $family): void
454    {
455        $this->addRecordToCart($family);
456
457        foreach ($family->spouses() as $spouse) {
458            $this->addRecordToCart($spouse);
459        }
460    }
461
462    /**
463     * @param Family $family
464     *
465     * @return void
466     */
467    private function addFamilyAndChildrenToCart(Family $family): void
468    {
469        $this->addRecordToCart($family);
470
471        foreach ($family->spouses() as $spouse) {
472            $this->addRecordToCart($spouse);
473        }
474        foreach ($family->children() as $child) {
475            $this->addRecordToCart($child);
476        }
477    }
478
479    /**
480     * @param Family $family
481     *
482     * @return void
483     */
484    private function addFamilyAndDescendantsToCart(Family $family): void
485    {
486        $this->addRecordToCart($family);
487
488        foreach ($family->spouses() as $spouse) {
489            $this->addRecordToCart($spouse);
490        }
491        foreach ($family->children() as $child) {
492            $this->addRecordToCart($child);
493            foreach ($child->spouseFamilies() as $child_family) {
494                $this->addFamilyAndDescendantsToCart($child_family);
495            }
496        }
497    }
498
499    /**
500     * @param ServerRequestInterface $request
501     * @param Tree                   $tree
502     *
503     * @return ResponseInterface
504     */
505    public function getAddIndividualAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
506    {
507        $xref = $request->getQueryParams()['xref'];
508
509        $individual = Individual::getInstance($xref, $tree);
510
511        if ($individual === null) {
512            throw new IndividualNotFoundException();
513        }
514
515        $options = $this->individualOptions($individual);
516
517        $title = I18N::translate('Add %s to the clippings cart', $individual->fullName());
518
519        return $this->viewResponse('modules/clippings/add-options', [
520            'options' => $options,
521            'default' => key($options),
522            'record'  => $individual,
523            'title'   => $title,
524            'tree'    => $tree,
525        ]);
526    }
527
528    /**
529     * @param Individual $individual
530     *
531     * @return string[]
532     */
533    private function individualOptions(Individual $individual): array
534    {
535        $name = strip_tags($individual->fullName());
536
537        if ($individual->sex() === 'F') {
538            return [
539                'self'              => $name,
540                'parents'           => I18N::translate('%s, her parents and siblings', $name),
541                'spouses'           => I18N::translate('%s, her spouses and children', $name),
542                'ancestors'         => I18N::translate('%s and her ancestors', $name),
543                'ancestor_families' => I18N::translate('%s, her ancestors and their families', $name),
544                'descendants'       => I18N::translate('%s, her spouses and descendants', $name),
545            ];
546        }
547
548        return [
549            'self'              => $name,
550            'parents'           => I18N::translate('%s, his parents and siblings', $name),
551            'spouses'           => I18N::translate('%s, his spouses and children', $name),
552            'ancestors'         => I18N::translate('%s and his ancestors', $name),
553            'ancestor_families' => I18N::translate('%s, his ancestors and their families', $name),
554            'descendants'       => I18N::translate('%s, his spouses and descendants', $name),
555        ];
556    }
557
558    /**
559     * @param ServerRequestInterface $request
560     * @param Tree                   $tree
561     *
562     * @return ResponseInterface
563     */
564    public function postAddIndividualAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
565    {
566        $xref   = $request->getQueryParams()['xref'];
567        $option = $request->getParsedBody()['option'];
568
569        $individual = Individual::getInstance($xref, $tree);
570
571        if ($individual === null) {
572            throw new IndividualNotFoundException();
573        }
574
575        switch ($option) {
576            case 'self':
577                $this->addRecordToCart($individual);
578                break;
579
580            case 'parents':
581                foreach ($individual->childFamilies() as $family) {
582                    $this->addFamilyAndChildrenToCart($family);
583                }
584                break;
585
586            case 'spouses':
587                foreach ($individual->spouseFamilies() as $family) {
588                    $this->addFamilyAndChildrenToCart($family);
589                }
590                break;
591
592            case 'ancestors':
593                $this->addAncestorsToCart($individual);
594                break;
595
596            case 'ancestor_families':
597                $this->addAncestorFamiliesToCart($individual);
598                break;
599
600            case 'descendants':
601                foreach ($individual->spouseFamilies() as $family) {
602                    $this->addFamilyAndDescendantsToCart($family);
603                }
604                break;
605        }
606
607        return redirect($individual->url());
608    }
609
610    /**
611     * @param Individual $individual
612     *
613     * @return void
614     */
615    private function addAncestorsToCart(Individual $individual): void
616    {
617        $this->addRecordToCart($individual);
618
619        foreach ($individual->childFamilies() as $family) {
620            foreach ($family->spouses() as $parent) {
621                $this->addAncestorsToCart($parent);
622            }
623        }
624    }
625
626    /**
627     * @param Individual $individual
628     *
629     * @return void
630     */
631    private function addAncestorFamiliesToCart(Individual $individual): void
632    {
633        foreach ($individual->childFamilies() as $family) {
634            $this->addFamilyAndChildrenToCart($family);
635            foreach ($family->spouses() as $parent) {
636                $this->addAncestorsToCart($parent);
637            }
638        }
639    }
640
641    /**
642     * @param ServerRequestInterface $request
643     * @param Tree                   $tree
644     *
645     * @return ResponseInterface
646     */
647    public function getAddMediaAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
648    {
649        $xref = $request->getQueryParams()['xref'];
650
651        $media = Media::getInstance($xref, $tree);
652
653        if ($media === null) {
654            throw new MediaNotFoundException();
655        }
656
657        $options = $this->mediaOptions($media);
658
659        $title = I18N::translate('Add %s to the clippings cart', $media->fullName());
660
661        return $this->viewResponse('modules/clippings/add-options', [
662            'options' => $options,
663            'default' => key($options),
664            'record'  => $media,
665            'title'   => $title,
666            'tree'    => $tree,
667        ]);
668    }
669
670    /**
671     * @param Media $media
672     *
673     * @return string[]
674     */
675    private function mediaOptions(Media $media): array
676    {
677        $name = strip_tags($media->fullName());
678
679        return [
680            'self' => $name,
681        ];
682    }
683
684    /**
685     * @param ServerRequestInterface $request
686     * @param Tree                   $tree
687     *
688     * @return ResponseInterface
689     */
690    public function postAddMediaAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
691    {
692        $xref = $request->getQueryParams()['xref'];
693
694        $media = Media::getInstance($xref, $tree);
695
696        if ($media === null) {
697            throw new MediaNotFoundException();
698        }
699
700        $this->addRecordToCart($media);
701
702        return redirect($media->url());
703    }
704
705    /**
706     * @param ServerRequestInterface $request
707     * @param Tree                   $tree
708     *
709     * @return ResponseInterface
710     */
711    public function getAddNoteAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
712    {
713        $xref = $request->getQueryParams()['xref'];
714
715        $note = Note::getInstance($xref, $tree);
716
717        if ($note === null) {
718            throw new NoteNotFoundException();
719        }
720
721        $options = $this->noteOptions($note);
722
723        $title = I18N::translate('Add %s to the clippings cart', $note->fullName());
724
725        return $this->viewResponse('modules/clippings/add-options', [
726            'options' => $options,
727            'default' => key($options),
728            'record'  => $note,
729            'title'   => $title,
730            'tree'    => $tree,
731        ]);
732    }
733
734    /**
735     * @param Note $note
736     *
737     * @return string[]
738     */
739    private function noteOptions(Note $note): array
740    {
741        $name = strip_tags($note->fullName());
742
743        return [
744            'self' => $name,
745        ];
746    }
747
748    /**
749     * @param ServerRequestInterface $request
750     * @param Tree                   $tree
751     *
752     * @return ResponseInterface
753     */
754    public function postAddNoteAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
755    {
756        $xref = $request->getQueryParams()['xref'];
757
758        $note = Note::getInstance($xref, $tree);
759
760        if ($note === null) {
761            throw new NoteNotFoundException();
762        }
763
764        $this->addRecordToCart($note);
765
766        return redirect($note->url());
767    }
768
769    /**
770     * @param ServerRequestInterface $request
771     * @param Tree                   $tree
772     *
773     * @return ResponseInterface
774     */
775    public function getAddRepositoryAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
776    {
777        $xref = $request->getQueryParams()['xref'];
778
779        $repository = Repository::getInstance($xref, $tree);
780
781        if ($repository === null) {
782            throw new RepositoryNotFoundException();
783        }
784
785        $options = $this->repositoryOptions($repository);
786
787        $title = I18N::translate('Add %s to the clippings cart', $repository->fullName());
788
789        return $this->viewResponse('modules/clippings/add-options', [
790            'options' => $options,
791            'default' => key($options),
792            'record'  => $repository,
793            'title'   => $title,
794            'tree'    => $tree,
795        ]);
796    }
797
798    /**
799     * @param Repository $repository
800     *
801     * @return string[]
802     */
803    private function repositoryOptions(Repository $repository): array
804    {
805        $name = strip_tags($repository->fullName());
806
807        return [
808            'self' => $name,
809        ];
810    }
811
812    /**
813     * @param ServerRequestInterface $request
814     * @param Tree                   $tree
815     *
816     * @return ResponseInterface
817     */
818    public function postAddRepositoryAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
819    {
820        $xref = $request->getQueryParams()['xref'];
821
822        $repository = Repository::getInstance($xref, $tree);
823
824        if ($repository === null) {
825            throw new RepositoryNotFoundException();
826        }
827
828        $this->addRecordToCart($repository);
829
830        return redirect($repository->url());
831    }
832
833    /**
834     * @param ServerRequestInterface $request
835     * @param Tree                   $tree
836     *
837     * @return ResponseInterface
838     */
839    public function getAddSourceAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
840    {
841        $xref = $request->getQueryParams()['xref'];
842
843        $source = Source::getInstance($xref, $tree);
844
845        if ($source === null) {
846            throw new SourceNotFoundException();
847        }
848
849        $options = $this->sourceOptions($source);
850
851        $title = I18N::translate('Add %s to the clippings cart', $source->fullName());
852
853        return $this->viewResponse('modules/clippings/add-options', [
854            'options' => $options,
855            'default' => key($options),
856            'record'  => $source,
857            'title'   => $title,
858            'tree'    => $tree,
859        ]);
860    }
861
862    /**
863     * @param Source $source
864     *
865     * @return string[]
866     */
867    private function sourceOptions(Source $source): array
868    {
869        $name = strip_tags($source->fullName());
870
871        return [
872            'only'   => strip_tags($source->fullName()),
873            'linked' => I18N::translate('%s and the individuals that reference it.', $name),
874        ];
875    }
876
877    /**
878     * @param ServerRequestInterface $request
879     * @param Tree                   $tree
880     *
881     * @return ResponseInterface
882     */
883    public function postAddSourceAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
884    {
885        $xref   = $request->getQueryParams()['xref'];
886        $option = $request->getParsedBody()['option'];
887
888        $source = Source::getInstance($xref, $tree);
889
890        if ($source === null) {
891            throw new SourceNotFoundException();
892        }
893
894        $this->addRecordToCart($source);
895
896        if ($option === 'linked') {
897            foreach ($source->linkedIndividuals('SOUR') as $individual) {
898                $this->addRecordToCart($individual);
899            }
900            foreach ($source->linkedFamilies('SOUR') as $family) {
901                $this->addRecordToCart($family);
902            }
903        }
904
905        return redirect($source->url());
906    }
907
908    /**
909     * Get all the records in the cart.
910     *
911     * @param Tree $tree
912     *
913     * @return GedcomRecord[]
914     */
915    private function allRecordsInCart(Tree $tree): array
916    {
917        $cart = Session::get('cart', []);
918
919        $xrefs = array_keys($cart[$tree->name()] ?? []);
920
921        // Fetch all the records in the cart.
922        $records = array_map(static function (string $xref) use ($tree): ?GedcomRecord {
923            return GedcomRecord::getInstance($xref, $tree);
924        }, $xrefs);
925
926        // Some records may have been deleted after they were added to the cart.
927        $records = array_filter($records);
928
929        // Group and sort.
930        uasort($records, static function (GedcomRecord $x, GedcomRecord $y): int {
931            return $x::RECORD_TYPE <=> $y::RECORD_TYPE ?: GedcomRecord::nameComparator()($x, $y);
932        });
933
934        return $records;
935    }
936
937    /**
938     * Add a record (and direclty linked sources, notes, etc. to the cart.
939     *
940     * @param GedcomRecord $record
941     *
942     * @return void
943     */
944    private function addRecordToCart(GedcomRecord $record): void
945    {
946        $cart = Session::get('cart', []);
947
948        $tree_name = $record->tree()->name();
949
950        // Add this record
951        $cart[$tree_name][$record->xref()] = true;
952
953        // Add directly linked media, notes, repositories and sources.
954        preg_match_all('/\n\d (?:OBJE|NOTE|SOUR|REPO) @(' . Gedcom::REGEX_XREF . ')@/', $record->gedcom(), $matches);
955
956        foreach ($matches[1] as $match) {
957            $cart[$tree_name][$match] = true;
958        }
959
960        Session::put('cart', $cart);
961    }
962
963    /**
964     * @param Tree $tree
965     *
966     * @return bool
967     */
968    private function isCartEmpty(Tree $tree): bool
969    {
970        $cart = Session::get('cart', []);
971
972        return empty($cart[$tree->name()]);
973    }
974}
975