xref: /webtrees/app/Module/AlbumModule.php (revision e563f71a2ec59b5b8c8626ad0e824fc8d8c4553c)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2020 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Registry;
23use Fisharebest\Webtrees\Gedcom;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Individual;
26use Fisharebest\Webtrees\Media;
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    /**
46     * A sentence describing what this module does.
47     *
48     * @return string
49     */
50    public function description(): string
51    {
52        /* I18N: Description of the “Album” module */
53        return I18N::translate('An alternative to the “media” tab, and an enhanced image viewer.');
54    }
55
56    /**
57     * The default position for this tab.  It can be changed in the control panel.
58     *
59     * @return int
60     */
61    public function defaultTabOrder(): int
62    {
63        return 6;
64    }
65
66    /**
67     * Generate the HTML content of this tab.
68     *
69     * @param Individual $individual
70     *
71     * @return string
72     */
73    public function getTabContent(Individual $individual): string
74    {
75        return view('modules/lightbox/tab', [
76            'media_list' => $this->getMedia($individual),
77        ]);
78    }
79
80    /**
81     * Get the linked media objects.
82     *
83     * @param Individual $individual
84     *
85     * @return Collection<Media>
86     */
87    private function getMedia(Individual $individual): Collection
88    {
89        $media = new Collection();
90
91        foreach ($this->getFactsWithMedia($individual) as $fact) {
92            preg_match_all('/(?:^1|\n\d) OBJE @(' . Gedcom::REGEX_XREF . ')@/', $fact->gedcom(), $matches);
93
94            foreach ($matches[1] as $xref) {
95                if (!$media->has($xref)) {
96                    $media->push(Registry::mediaFactory()->make($xref, $individual->tree()));
97                }
98            }
99        }
100
101        return $media->filter()->filter(Media::accessFilter());
102    }
103}
104