xref: /webtrees/app/Module/AlbumModule.php (revision f4c767fd89cdb62ee54edec032285924cd767af7)
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\Factory;
23use Fisharebest\Webtrees\Gedcom;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Individual;
26use Fisharebest\Webtrees\Media;
27
28/**
29 * Class AlbumModule
30 */
31class AlbumModule extends AbstractModule implements ModuleTabInterface
32{
33    use ModuleTabTrait;
34
35    /** @var Media[] List of media objects. */
36    private $media_list;
37
38    /**
39     * How should this module be identified in the control panel, etc.?
40     *
41     * @return string
42     */
43    public function title(): string
44    {
45        /* I18N: Name of a module */
46        return I18N::translate('Album');
47    }
48
49    /**
50     * A sentence describing what this module does.
51     *
52     * @return string
53     */
54    public function description(): string
55    {
56        /* I18N: Description of the “Album” module */
57        return I18N::translate('An alternative to the “media” tab, and an enhanced image viewer.');
58    }
59
60    /**
61     * The default position for this tab.  It can be changed in the control panel.
62     *
63     * @return int
64     */
65    public function defaultTabOrder(): int
66    {
67        return 6;
68    }
69
70    /**
71     * Is this tab empty? If so, we don't always need to display it.
72     *
73     * @param Individual $individual
74     *
75     * @return bool
76     */
77    public function hasTabContent(Individual $individual): bool
78    {
79        return $individual->canEdit() || $this->getMedia($individual);
80    }
81
82    /**
83     * A greyed out tab has no actual content, but may perhaps have
84     * options to create content.
85     *
86     * @param Individual $individual
87     *
88     * @return bool
89     */
90    public function isGrayedOut(Individual $individual): bool
91    {
92        return !$this->getMedia($individual);
93    }
94
95    /**
96     * Generate the HTML content of this tab.
97     *
98     * @param Individual $individual
99     *
100     * @return string
101     */
102    public function getTabContent(Individual $individual): string
103    {
104        return view('modules/lightbox/tab', [
105            'media_list' => $this->getMedia($individual),
106        ]);
107    }
108
109    /**
110     * Get all facts containing media links for this person and their spouse-family records
111     *
112     * @param Individual $individual
113     *
114     * @return Media[]
115     */
116    private function getMedia(Individual $individual): array
117    {
118        if ($this->media_list === null) {
119            // Use facts from this individual and all their spouses
120            $facts = $individual->facts();
121            foreach ($individual->spouseFamilies() as $family) {
122                foreach ($family->facts() as $fact) {
123                    $facts->push($fact);
124                }
125            }
126            // Use all media from each fact
127            $this->media_list = [];
128            foreach ($facts as $fact) {
129                // Don't show pending edits, as the user just sees duplicates
130                if (!$fact->isPendingDeletion()) {
131                    preg_match_all('/(?:^1|\n\d) OBJE @(' . Gedcom::REGEX_XREF . ')@/', $fact->gedcom(), $matches);
132                    foreach ($matches[1] as $match) {
133                        $media = Factory::media()->make($match, $individual->tree());
134                        if ($media && $media->canShow()) {
135                            $this->media_list[] = $media;
136                        }
137                    }
138                }
139            }
140            // If a media object is linked twice, only show it once
141            $this->media_list = array_unique($this->media_list);
142            // Sort these using _WT_OBJE_SORT
143            $wt_obje_sort = [];
144            foreach ($individual->facts(['_WT_OBJE_SORT']) as $fact) {
145                $wt_obje_sort[] = trim($fact->value(), '@');
146            }
147            usort($this->media_list, static function (Media $x, Media $y) use ($wt_obje_sort): int {
148                return array_search($x->xref(), $wt_obje_sort, true) - array_search($y->xref(), $wt_obje_sort, true);
149            });
150        }
151
152        return $this->media_list;
153    }
154
155    /**
156     * Can this tab load asynchronously?
157     *
158     * @return bool
159     */
160    public function canLoadAjax(): bool
161    {
162        return false;
163    }
164}
165