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