xref: /webtrees/app/Module/AlbumModule.php (revision 8fcd0d32e56ee262912bbdb593202cfd1cbc1615)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
4*8fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
208d0ebef0SGreg Roachuse Fisharebest\Webtrees\Gedcom;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
22225e381fSGreg Roachuse Fisharebest\Webtrees\Individual;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media;
248c2e8227SGreg Roach
258c2e8227SGreg Roach/**
268c2e8227SGreg Roach * Class AlbumModule
278c2e8227SGreg Roach */
28c1010edaSGreg Roachclass AlbumModule extends AbstractModule implements ModuleTabInterface
29c1010edaSGreg Roach{
3076692c8bSGreg Roach    /** @var Media[] List of media objects. */
318c2e8227SGreg Roach    private $media_list;
328c2e8227SGreg Roach
3376692c8bSGreg Roach    /**
3476692c8bSGreg Roach     * How should this module be labelled on tabs, menus, etc.?
3576692c8bSGreg Roach     *
3676692c8bSGreg Roach     * @return string
3776692c8bSGreg Roach     */
388f53f488SRico Sonntag    public function getTitle(): string
39c1010edaSGreg Roach    {
40bbb76c12SGreg Roach        /* I18N: Name of a module */
41bbb76c12SGreg Roach        return I18N::translate('Album');
428c2e8227SGreg Roach    }
438c2e8227SGreg Roach
4476692c8bSGreg Roach    /**
4576692c8bSGreg Roach     * A sentence describing what this module does.
4676692c8bSGreg Roach     *
4776692c8bSGreg Roach     * @return string
4876692c8bSGreg Roach     */
498f53f488SRico Sonntag    public function getDescription(): string
50c1010edaSGreg Roach    {
51bbb76c12SGreg Roach        /* I18N: Description of the “Album” module */
52bbb76c12SGreg Roach        return I18N::translate('An alternative to the “media” tab, and an enhanced image viewer.');
538c2e8227SGreg Roach    }
548c2e8227SGreg Roach
5576692c8bSGreg Roach    /**
5676692c8bSGreg Roach     * The user can re-arrange the tab order, but until they do, this
5776692c8bSGreg Roach     * is the order in which tabs are shown.
5876692c8bSGreg Roach     *
5976692c8bSGreg Roach     * @return int
6076692c8bSGreg Roach     */
618f53f488SRico Sonntag    public function defaultTabOrder(): int
62c1010edaSGreg Roach    {
638c2e8227SGreg Roach        return 60;
648c2e8227SGreg Roach    }
658c2e8227SGreg Roach
6676692c8bSGreg Roach    /**
6776692c8bSGreg Roach     * Is this tab empty? If so, we don't always need to display it.
6876692c8bSGreg Roach     *
69f9f36b89SGreg Roach     * @param Individual $individual
70f9f36b89SGreg Roach     *
7176692c8bSGreg Roach     * @return bool
7276692c8bSGreg Roach     */
738f53f488SRico Sonntag    public function hasTabContent(Individual $individual): bool
74c1010edaSGreg Roach    {
75225e381fSGreg Roach        return $individual->canEdit() || $this->getMedia($individual);
768c2e8227SGreg Roach    }
778c2e8227SGreg Roach
7876692c8bSGreg Roach    /**
7976692c8bSGreg Roach     * A greyed out tab has no actual content, but may perhaps have
8076692c8bSGreg Roach     * options to create content.
8176692c8bSGreg Roach     *
82f9f36b89SGreg Roach     * @param Individual $individual
83f9f36b89SGreg Roach     *
8476692c8bSGreg Roach     * @return bool
8576692c8bSGreg Roach     */
868f53f488SRico Sonntag    public function isGrayedOut(Individual $individual): bool
87c1010edaSGreg Roach    {
88225e381fSGreg Roach        return !$this->getMedia($individual);
898c2e8227SGreg Roach    }
908c2e8227SGreg Roach
9176692c8bSGreg Roach    /**
9276692c8bSGreg Roach     * Generate the HTML content of this tab.
9376692c8bSGreg Roach     *
94225e381fSGreg Roach     * @param Individual $individual
95225e381fSGreg Roach     *
9676692c8bSGreg Roach     * @return string
9776692c8bSGreg Roach     */
988f53f488SRico Sonntag    public function getTabContent(Individual $individual): string
99c1010edaSGreg Roach    {
100a8cd57e1SGreg Roach        return view('modules/lightbox/tab', [
101c1010edaSGreg Roach            'media_list' => $this->getMedia($individual),
102d14df12bSGreg Roach        ]);
1038c2e8227SGreg Roach    }
1048c2e8227SGreg Roach
1058c2e8227SGreg Roach    /**
1068c2e8227SGreg Roach     * Get all facts containing media links for this person and their spouse-family records
1078c2e8227SGreg Roach     *
108225e381fSGreg Roach     * @param Individual $individual
109225e381fSGreg Roach     *
1108c2e8227SGreg Roach     * @return Media[]
1118c2e8227SGreg Roach     */
1128f53f488SRico Sonntag    private function getMedia(Individual $individual): array
113c1010edaSGreg Roach    {
1148c2e8227SGreg Roach        if ($this->media_list === null) {
1158c2e8227SGreg Roach            // Use facts from this individual and all their spouses
11630158ae7SGreg Roach            $facts = $individual->facts();
117225e381fSGreg Roach            foreach ($individual->getSpouseFamilies() as $family) {
11830158ae7SGreg Roach                foreach ($family->facts() as $fact) {
1198c2e8227SGreg Roach                    $facts[] = $fact;
1208c2e8227SGreg Roach                }
1218c2e8227SGreg Roach            }
1228c2e8227SGreg Roach            // Use all media from each fact
12313abd6f3SGreg Roach            $this->media_list = [];
1248c2e8227SGreg Roach            foreach ($facts as $fact) {
1258c2e8227SGreg Roach                // Don't show pending edits, as the user just sees duplicates
1268c2e8227SGreg Roach                if (!$fact->isPendingDeletion()) {
1278d0ebef0SGreg Roach                    preg_match_all('/(?:^1|\n\d) OBJE @(' . Gedcom::REGEX_XREF . ')@/', $fact->gedcom(), $matches);
1288c2e8227SGreg Roach                    foreach ($matches[1] as $match) {
129f4afa648SGreg Roach                        $media = Media::getInstance($match, $individual->tree());
1308c2e8227SGreg Roach                        if ($media && $media->canShow()) {
1318c2e8227SGreg Roach                            $this->media_list[] = $media;
1328c2e8227SGreg Roach                        }
1338c2e8227SGreg Roach                    }
1348c2e8227SGreg Roach                }
1358c2e8227SGreg Roach            }
1368c2e8227SGreg Roach            // If a media object is linked twice, only show it once
1378c2e8227SGreg Roach            $this->media_list = array_unique($this->media_list);
1388c2e8227SGreg Roach            // Sort these using _WT_OBJE_SORT
13913abd6f3SGreg Roach            $wt_obje_sort = [];
1408d0ebef0SGreg Roach            foreach ($individual->facts(['_WT_OBJE_SORT']) as $fact) {
14184586c02SGreg Roach                $wt_obje_sort[] = trim($fact->value(), '@');
1428c2e8227SGreg Roach            }
14318d7a90dSGreg Roach            usort($this->media_list, function (Media $x, Media $y) use ($wt_obje_sort): int {
144c0935879SGreg Roach                return array_search($x->xref(), $wt_obje_sort) - array_search($y->xref(), $wt_obje_sort);
1458c2e8227SGreg Roach            });
1468c2e8227SGreg Roach        }
147cbc1590aSGreg Roach
1488c2e8227SGreg Roach        return $this->media_list;
1498c2e8227SGreg Roach    }
1508c2e8227SGreg Roach
15176692c8bSGreg Roach    /**
15276692c8bSGreg Roach     * Can this tab load asynchronously?
15376692c8bSGreg Roach     *
15476692c8bSGreg Roach     * @return bool
15576692c8bSGreg Roach     */
1568f53f488SRico Sonntag    public function canLoadAjax(): bool
157c1010edaSGreg Roach    {
15815d603e7SGreg Roach        return false;
1598c2e8227SGreg Roach    }
1608c2e8227SGreg Roach}
161