xref: /webtrees/app/Module/AlbumModule.php (revision 84586c024c68dbfa3b15df636ed2b74037428442)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
21225e381fSGreg Roachuse Fisharebest\Webtrees\Individual;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media;
238c2e8227SGreg Roach
248c2e8227SGreg Roach/**
258c2e8227SGreg Roach * Class AlbumModule
268c2e8227SGreg Roach */
27c1010edaSGreg Roachclass AlbumModule extends AbstractModule implements ModuleTabInterface
28c1010edaSGreg Roach{
2976692c8bSGreg Roach    /** @var Media[] List of media objects. */
308c2e8227SGreg Roach    private $media_list;
318c2e8227SGreg Roach
3276692c8bSGreg Roach    /**
3376692c8bSGreg Roach     * How should this module be labelled on tabs, menus, etc.?
3476692c8bSGreg Roach     *
3576692c8bSGreg Roach     * @return string
3676692c8bSGreg Roach     */
378f53f488SRico Sonntag    public function getTitle(): string
38c1010edaSGreg Roach    {
39bbb76c12SGreg Roach        /* I18N: Name of a module */
40bbb76c12SGreg Roach        return I18N::translate('Album');
418c2e8227SGreg Roach    }
428c2e8227SGreg Roach
4376692c8bSGreg Roach    /**
4476692c8bSGreg Roach     * A sentence describing what this module does.
4576692c8bSGreg Roach     *
4676692c8bSGreg Roach     * @return string
4776692c8bSGreg Roach     */
488f53f488SRico Sonntag    public function getDescription(): string
49c1010edaSGreg Roach    {
50bbb76c12SGreg Roach        /* I18N: Description of the “Album” module */
51bbb76c12SGreg Roach        return I18N::translate('An alternative to the “media” tab, and an enhanced image viewer.');
528c2e8227SGreg Roach    }
538c2e8227SGreg Roach
5476692c8bSGreg Roach    /**
5576692c8bSGreg Roach     * The user can re-arrange the tab order, but until they do, this
5676692c8bSGreg Roach     * is the order in which tabs are shown.
5776692c8bSGreg Roach     *
5876692c8bSGreg Roach     * @return int
5976692c8bSGreg Roach     */
608f53f488SRico Sonntag    public function defaultTabOrder(): int
61c1010edaSGreg Roach    {
628c2e8227SGreg Roach        return 60;
638c2e8227SGreg Roach    }
648c2e8227SGreg Roach
6576692c8bSGreg Roach    /**
6676692c8bSGreg Roach     * Is this tab empty? If so, we don't always need to display it.
6776692c8bSGreg Roach     *
68f9f36b89SGreg Roach     * @param Individual $individual
69f9f36b89SGreg Roach     *
7076692c8bSGreg Roach     * @return bool
7176692c8bSGreg Roach     */
728f53f488SRico Sonntag    public function hasTabContent(Individual $individual): bool
73c1010edaSGreg Roach    {
74225e381fSGreg Roach        return $individual->canEdit() || $this->getMedia($individual);
758c2e8227SGreg Roach    }
768c2e8227SGreg Roach
7776692c8bSGreg Roach    /**
7876692c8bSGreg Roach     * A greyed out tab has no actual content, but may perhaps have
7976692c8bSGreg Roach     * options to create content.
8076692c8bSGreg Roach     *
81f9f36b89SGreg Roach     * @param Individual $individual
82f9f36b89SGreg Roach     *
8376692c8bSGreg Roach     * @return bool
8476692c8bSGreg Roach     */
858f53f488SRico Sonntag    public function isGrayedOut(Individual $individual): bool
86c1010edaSGreg Roach    {
87225e381fSGreg Roach        return !$this->getMedia($individual);
888c2e8227SGreg Roach    }
898c2e8227SGreg Roach
9076692c8bSGreg Roach    /**
9176692c8bSGreg Roach     * Generate the HTML content of this tab.
9276692c8bSGreg Roach     *
93225e381fSGreg Roach     * @param Individual $individual
94225e381fSGreg Roach     *
9576692c8bSGreg Roach     * @return string
9676692c8bSGreg Roach     */
978f53f488SRico Sonntag    public function getTabContent(Individual $individual): string
98c1010edaSGreg Roach    {
99a8cd57e1SGreg Roach        return view('modules/lightbox/tab', [
100c1010edaSGreg Roach            'media_list' => $this->getMedia($individual),
101d14df12bSGreg Roach        ]);
1028c2e8227SGreg Roach    }
1038c2e8227SGreg Roach
1048c2e8227SGreg Roach    /**
1058c2e8227SGreg Roach     * Get all facts containing media links for this person and their spouse-family records
1068c2e8227SGreg Roach     *
107225e381fSGreg Roach     * @param Individual $individual
108225e381fSGreg Roach     *
1098c2e8227SGreg Roach     * @return Media[]
1108c2e8227SGreg Roach     */
1118f53f488SRico Sonntag    private function getMedia(Individual $individual): array
112c1010edaSGreg Roach    {
1138c2e8227SGreg Roach        if ($this->media_list === null) {
1148c2e8227SGreg Roach            // Use facts from this individual and all their spouses
115225e381fSGreg Roach            $facts = $individual->getFacts();
116225e381fSGreg Roach            foreach ($individual->getSpouseFamilies() as $family) {
1178c2e8227SGreg Roach                foreach ($family->getFacts() as $fact) {
1188c2e8227SGreg Roach                    $facts[] = $fact;
1198c2e8227SGreg Roach                }
1208c2e8227SGreg Roach            }
1218c2e8227SGreg Roach            // Use all media from each fact
12213abd6f3SGreg Roach            $this->media_list = [];
1238c2e8227SGreg Roach            foreach ($facts as $fact) {
1248c2e8227SGreg Roach                // Don't show pending edits, as the user just sees duplicates
1258c2e8227SGreg Roach                if (!$fact->isPendingDeletion()) {
1268c2e8227SGreg Roach                    preg_match_all('/(?:^1|\n\d) OBJE @(' . WT_REGEX_XREF . ')@/', $fact->getGedcom(), $matches);
1278c2e8227SGreg Roach                    foreach ($matches[1] as $match) {
128225e381fSGreg Roach                        $media = Media::getInstance($match, $individual->getTree());
1298c2e8227SGreg Roach                        if ($media && $media->canShow()) {
1308c2e8227SGreg Roach                            $this->media_list[] = $media;
1318c2e8227SGreg Roach                        }
1328c2e8227SGreg Roach                    }
1338c2e8227SGreg Roach                }
1348c2e8227SGreg Roach            }
1358c2e8227SGreg Roach            // If a media object is linked twice, only show it once
1368c2e8227SGreg Roach            $this->media_list = array_unique($this->media_list);
1378c2e8227SGreg Roach            // Sort these using _WT_OBJE_SORT
13813abd6f3SGreg Roach            $wt_obje_sort = [];
139225e381fSGreg Roach            foreach ($individual->getFacts('_WT_OBJE_SORT') as $fact) {
140*84586c02SGreg Roach                $wt_obje_sort[] = trim($fact->value(), '@');
1418c2e8227SGreg Roach            }
14218d7a90dSGreg Roach            usort($this->media_list, function (Media $x, Media $y) use ($wt_obje_sort): int {
1438c2e8227SGreg Roach                return array_search($x->getXref(), $wt_obje_sort) - array_search($y->getXref(), $wt_obje_sort);
1448c2e8227SGreg Roach            });
1458c2e8227SGreg Roach        }
146cbc1590aSGreg Roach
1478c2e8227SGreg Roach        return $this->media_list;
1488c2e8227SGreg Roach    }
1498c2e8227SGreg Roach
15076692c8bSGreg Roach    /**
15176692c8bSGreg Roach     * Can this tab load asynchronously?
15276692c8bSGreg Roach     *
15376692c8bSGreg Roach     * @return bool
15476692c8bSGreg Roach     */
1558f53f488SRico Sonntag    public function canLoadAjax(): bool
156c1010edaSGreg Roach    {
15715d603e7SGreg Roach        return false;
1588c2e8227SGreg Roach    }
1598c2e8227SGreg Roach}
160