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