xref: /webtrees/app/Module/AlbumModule.php (revision 22d65e5ad7724941da33d875027b68b86648a321)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg 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 */
2837eb8894SGreg Roachclass AlbumModule extends AbstractModule implements ModuleTabInterface
29c1010edaSGreg Roach{
3049a243cbSGreg Roach    use ModuleTabTrait;
3149a243cbSGreg Roach
3276692c8bSGreg Roach    /** @var Media[] List of media objects. */
338c2e8227SGreg Roach    private $media_list;
348c2e8227SGreg Roach
3576692c8bSGreg Roach    /**
360cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
3776692c8bSGreg Roach     *
3876692c8bSGreg Roach     * @return string
3976692c8bSGreg Roach     */
4049a243cbSGreg Roach    public function title(): string
41c1010edaSGreg Roach    {
42bbb76c12SGreg Roach        /* I18N: Name of a module */
43bbb76c12SGreg Roach        return I18N::translate('Album');
448c2e8227SGreg Roach    }
458c2e8227SGreg Roach
4676692c8bSGreg Roach    /**
4776692c8bSGreg Roach     * A sentence describing what this module does.
4876692c8bSGreg Roach     *
4976692c8bSGreg Roach     * @return string
5076692c8bSGreg Roach     */
5149a243cbSGreg Roach    public function description(): string
52c1010edaSGreg Roach    {
53bbb76c12SGreg Roach        /* I18N: Description of the “Album” module */
54bbb76c12SGreg Roach        return I18N::translate('An alternative to the “media” tab, and an enhanced image viewer.');
558c2e8227SGreg Roach    }
568c2e8227SGreg Roach
5776692c8bSGreg Roach    /**
5849a243cbSGreg Roach     * The default position for this tab.  It can be changed in the control panel.
5976692c8bSGreg Roach     *
6076692c8bSGreg Roach     * @return int
6176692c8bSGreg Roach     */
62cbf4b7faSGreg Roach    public function defaultTabOrder(): int
63cbf4b7faSGreg Roach    {
64fb7a0427SGreg Roach        return 6;
658c2e8227SGreg Roach    }
668c2e8227SGreg Roach
6776692c8bSGreg Roach    /**
6876692c8bSGreg Roach     * Is this tab empty? If so, we don't always need to display it.
6976692c8bSGreg Roach     *
70f9f36b89SGreg Roach     * @param Individual $individual
71f9f36b89SGreg Roach     *
7276692c8bSGreg Roach     * @return bool
7376692c8bSGreg Roach     */
748f53f488SRico Sonntag    public function hasTabContent(Individual $individual): bool
75c1010edaSGreg Roach    {
76225e381fSGreg Roach        return $individual->canEdit() || $this->getMedia($individual);
778c2e8227SGreg Roach    }
788c2e8227SGreg Roach
7976692c8bSGreg Roach    /**
8076692c8bSGreg Roach     * A greyed out tab has no actual content, but may perhaps have
8176692c8bSGreg Roach     * options to create content.
8276692c8bSGreg Roach     *
83f9f36b89SGreg Roach     * @param Individual $individual
84f9f36b89SGreg Roach     *
8576692c8bSGreg Roach     * @return bool
8676692c8bSGreg Roach     */
878f53f488SRico Sonntag    public function isGrayedOut(Individual $individual): bool
88c1010edaSGreg Roach    {
89225e381fSGreg Roach        return !$this->getMedia($individual);
908c2e8227SGreg Roach    }
918c2e8227SGreg Roach
9276692c8bSGreg Roach    /**
9376692c8bSGreg Roach     * Generate the HTML content of this tab.
9476692c8bSGreg Roach     *
95225e381fSGreg Roach     * @param Individual $individual
96225e381fSGreg Roach     *
9776692c8bSGreg Roach     * @return string
9876692c8bSGreg Roach     */
998f53f488SRico Sonntag    public function getTabContent(Individual $individual): string
100c1010edaSGreg Roach    {
101a8cd57e1SGreg Roach        return view('modules/lightbox/tab', [
102c1010edaSGreg Roach            'media_list' => $this->getMedia($individual),
103d14df12bSGreg Roach        ]);
1048c2e8227SGreg Roach    }
1058c2e8227SGreg Roach
1068c2e8227SGreg Roach    /**
1078c2e8227SGreg Roach     * Get all facts containing media links for this person and their spouse-family records
1088c2e8227SGreg Roach     *
109225e381fSGreg Roach     * @param Individual $individual
110225e381fSGreg Roach     *
1118c2e8227SGreg Roach     * @return Media[]
1128c2e8227SGreg Roach     */
1138f53f488SRico Sonntag    private function getMedia(Individual $individual): array
114c1010edaSGreg Roach    {
1158c2e8227SGreg Roach        if ($this->media_list === null) {
1168c2e8227SGreg Roach            // Use facts from this individual and all their spouses
11730158ae7SGreg Roach            $facts = $individual->facts();
11839ca88baSGreg Roach            foreach ($individual->spouseFamilies() as $family) {
11930158ae7SGreg Roach                foreach ($family->facts() as $fact) {
12039ca88baSGreg Roach                    $facts->push($fact);
1218c2e8227SGreg Roach                }
1228c2e8227SGreg Roach            }
1238c2e8227SGreg Roach            // Use all media from each fact
12413abd6f3SGreg Roach            $this->media_list = [];
1258c2e8227SGreg Roach            foreach ($facts as $fact) {
1268c2e8227SGreg Roach                // Don't show pending edits, as the user just sees duplicates
1278c2e8227SGreg Roach                if (!$fact->isPendingDeletion()) {
1288d0ebef0SGreg Roach                    preg_match_all('/(?:^1|\n\d) OBJE @(' . Gedcom::REGEX_XREF . ')@/', $fact->gedcom(), $matches);
1298c2e8227SGreg Roach                    foreach ($matches[1] as $match) {
130f4afa648SGreg Roach                        $media = Media::getInstance($match, $individual->tree());
1318c2e8227SGreg Roach                        if ($media && $media->canShow()) {
1328c2e8227SGreg Roach                            $this->media_list[] = $media;
1338c2e8227SGreg Roach                        }
1348c2e8227SGreg Roach                    }
1358c2e8227SGreg Roach                }
1368c2e8227SGreg Roach            }
1378c2e8227SGreg Roach            // If a media object is linked twice, only show it once
1388c2e8227SGreg Roach            $this->media_list = array_unique($this->media_list);
1398c2e8227SGreg Roach            // Sort these using _WT_OBJE_SORT
14013abd6f3SGreg Roach            $wt_obje_sort = [];
1418d0ebef0SGreg Roach            foreach ($individual->facts(['_WT_OBJE_SORT']) as $fact) {
14284586c02SGreg Roach                $wt_obje_sort[] = trim($fact->value(), '@');
1438c2e8227SGreg Roach            }
1440b5fd0a6SGreg Roach            usort($this->media_list, static function (Media $x, Media $y) use ($wt_obje_sort): int {
145*22d65e5aSGreg Roach                return array_search($x->xref(), $wt_obje_sort, true) - array_search($y->xref(), $wt_obje_sort, true);
1468c2e8227SGreg Roach            });
1478c2e8227SGreg Roach        }
148cbc1590aSGreg Roach
1498c2e8227SGreg Roach        return $this->media_list;
1508c2e8227SGreg Roach    }
1518c2e8227SGreg Roach
15276692c8bSGreg Roach    /**
15376692c8bSGreg Roach     * Can this tab load asynchronously?
15476692c8bSGreg Roach     *
15576692c8bSGreg Roach     * @return bool
15676692c8bSGreg Roach     */
1578f53f488SRico Sonntag    public function canLoadAjax(): bool
158c1010edaSGreg Roach    {
15915d603e7SGreg Roach        return false;
1608c2e8227SGreg Roach    }
1618c2e8227SGreg Roach}
162