xref: /webtrees/app/Module/AlbumModule.php (revision e873f434551745f888937263ff89e80db3b0f785)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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 <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Gedcom;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Individual;
25use Fisharebest\Webtrees\Media;
26use Fisharebest\Webtrees\Registry;
27use Illuminate\Support\Collection;
28
29/**
30 * Class AlbumModule
31 */
32class AlbumModule extends MediaTabModule
33{
34    /**
35     * How should this module be identified in the control panel, etc.?
36     *
37     * @return string
38     */
39    public function title(): string
40    {
41        /* I18N: Name of a module */
42        return I18N::translate('Album');
43    }
44
45    public function description(): string
46    {
47        /* I18N: Description of the “Album” module */
48        return I18N::translate('An alternative to the “media” tab, and an enhanced image viewer.');
49    }
50
51    /**
52     * The default position for this tab.  It can be changed in the control panel.
53     *
54     * @return int
55     */
56    public function defaultTabOrder(): int
57    {
58        return 6;
59    }
60
61    /**
62     * Generate the HTML content of this tab.
63     *
64     * @param Individual $individual
65     *
66     * @return string
67     */
68    public function getTabContent(Individual $individual): string
69    {
70        return view('modules/lightbox/tab', [
71            'media_list' => $this->getMedia($individual),
72        ]);
73    }
74
75    /**
76     * Get the linked media objects.
77     *
78     * @param Individual $individual
79     *
80     * @return Collection<int,Media>
81     */
82    private function getMedia(Individual $individual): Collection
83    {
84        $media = new Collection();
85
86        foreach ($this->getFactsWithMedia($individual) as $fact) {
87            preg_match_all('/(?:^1|\n\d) OBJE @(' . Gedcom::REGEX_XREF . ')@/', $fact->gedcom(), $matches);
88
89            foreach ($matches[1] as $xref) {
90                if (!$media->has($xref)) {
91                    $media->put($xref, Registry::mediaFactory()->make($xref, $individual->tree()));
92                }
93            }
94        }
95
96        return $media->filter()->filter(Media::accessFilter());
97    }
98}
99