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