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 */ 1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 1776692c8bSGreg Roach 180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 190e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 20*225e381fSGreg Roachuse Fisharebest\Webtrees\Individual; 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media; 228c2e8227SGreg Roach 238c2e8227SGreg Roach/** 248c2e8227SGreg Roach * Class AlbumModule 258c2e8227SGreg Roach */ 26e2a378d3SGreg Roachclass AlbumModule extends AbstractModule implements ModuleTabInterface { 2776692c8bSGreg Roach /** @var Media[] List of media objects. */ 288c2e8227SGreg Roach private $media_list; 298c2e8227SGreg Roach 3076692c8bSGreg Roach /** 3176692c8bSGreg Roach * How should this module be labelled on tabs, menus, etc.? 3276692c8bSGreg Roach * 3376692c8bSGreg Roach * @return string 3476692c8bSGreg Roach */ 358c2e8227SGreg Roach public function getTitle() { 368c2e8227SGreg Roach return /* I18N: Name of a module */ I18N::translate('Album'); 378c2e8227SGreg Roach } 388c2e8227SGreg Roach 3976692c8bSGreg Roach /** 4076692c8bSGreg Roach * A sentence describing what this module does. 4176692c8bSGreg Roach * 4276692c8bSGreg Roach * @return string 4376692c8bSGreg Roach */ 448c2e8227SGreg Roach public function getDescription() { 458c2e8227SGreg Roach return /* I18N: Description of the “Album” module */ I18N::translate('An alternative to the “media” tab, and an enhanced image viewer.'); 468c2e8227SGreg Roach } 478c2e8227SGreg Roach 4876692c8bSGreg Roach /** 4976692c8bSGreg Roach * The user can re-arrange the tab order, but until they do, this 5076692c8bSGreg Roach * is the order in which tabs are shown. 5176692c8bSGreg Roach * 5276692c8bSGreg Roach * @return int 5376692c8bSGreg Roach */ 548c2e8227SGreg Roach public function defaultTabOrder() { 558c2e8227SGreg Roach return 60; 568c2e8227SGreg Roach } 578c2e8227SGreg Roach 5876692c8bSGreg Roach /** 5976692c8bSGreg Roach * Is this tab empty? If so, we don't always need to display it. 6076692c8bSGreg Roach * 6176692c8bSGreg Roach * @return bool 6276692c8bSGreg Roach */ 63*225e381fSGreg Roach public function hasTabContent(Individual $individual) { 64*225e381fSGreg Roach return $individual->canEdit() || $this->getMedia($individual); 658c2e8227SGreg Roach } 668c2e8227SGreg Roach 6776692c8bSGreg Roach /** 6876692c8bSGreg Roach * A greyed out tab has no actual content, but may perhaps have 6976692c8bSGreg Roach * options to create content. 7076692c8bSGreg Roach * 7176692c8bSGreg Roach * @return bool 7276692c8bSGreg Roach */ 73*225e381fSGreg Roach public function isGrayedOut(Individual $individual) { 74*225e381fSGreg Roach return !$this->getMedia($individual); 758c2e8227SGreg Roach } 768c2e8227SGreg Roach 7776692c8bSGreg Roach /** 7876692c8bSGreg Roach * Generate the HTML content of this tab. 7976692c8bSGreg Roach * 80*225e381fSGreg Roach * @param Individual $individual 81*225e381fSGreg Roach * 8276692c8bSGreg Roach * @return string 8376692c8bSGreg Roach */ 84*225e381fSGreg Roach public function getTabContent(Individual $individual) { 85*225e381fSGreg Roach return view('tabs/album', [ 86*225e381fSGreg Roach 'media_list' => $this->getMedia($individual) 87d14df12bSGreg Roach ]); 888c2e8227SGreg Roach } 898c2e8227SGreg Roach 908c2e8227SGreg Roach /** 918c2e8227SGreg Roach * Get all facts containing media links for this person and their spouse-family records 928c2e8227SGreg Roach * 93*225e381fSGreg Roach * @param Individual $individual 94*225e381fSGreg Roach * 958c2e8227SGreg Roach * @return Media[] 968c2e8227SGreg Roach */ 97*225e381fSGreg Roach private function getMedia(Individual $individual) { 988c2e8227SGreg Roach if ($this->media_list === null) { 998c2e8227SGreg Roach // Use facts from this individual and all their spouses 100*225e381fSGreg Roach $facts = $individual->getFacts(); 101*225e381fSGreg Roach foreach ($individual->getSpouseFamilies() as $family) { 1028c2e8227SGreg Roach foreach ($family->getFacts() as $fact) { 1038c2e8227SGreg Roach $facts[] = $fact; 1048c2e8227SGreg Roach } 1058c2e8227SGreg Roach } 1068c2e8227SGreg Roach // Use all media from each fact 10713abd6f3SGreg Roach $this->media_list = []; 1088c2e8227SGreg Roach foreach ($facts as $fact) { 1098c2e8227SGreg Roach // Don't show pending edits, as the user just sees duplicates 1108c2e8227SGreg Roach if (!$fact->isPendingDeletion()) { 1118c2e8227SGreg Roach preg_match_all('/(?:^1|\n\d) OBJE @(' . WT_REGEX_XREF . ')@/', $fact->getGedcom(), $matches); 1128c2e8227SGreg Roach foreach ($matches[1] as $match) { 113*225e381fSGreg Roach $media = Media::getInstance($match, $individual->getTree()); 1148c2e8227SGreg Roach if ($media && $media->canShow()) { 1158c2e8227SGreg Roach $this->media_list[] = $media; 1168c2e8227SGreg Roach } 1178c2e8227SGreg Roach } 1188c2e8227SGreg Roach } 1198c2e8227SGreg Roach } 1208c2e8227SGreg Roach // If a media object is linked twice, only show it once 1218c2e8227SGreg Roach $this->media_list = array_unique($this->media_list); 1228c2e8227SGreg Roach // Sort these using _WT_OBJE_SORT 12313abd6f3SGreg Roach $wt_obje_sort = []; 124*225e381fSGreg Roach foreach ($individual->getFacts('_WT_OBJE_SORT') as $fact) { 1258c2e8227SGreg Roach $wt_obje_sort[] = trim($fact->getValue(), '@'); 1268c2e8227SGreg Roach } 1278c2e8227SGreg Roach usort($this->media_list, function (Media $x, Media $y) use ($wt_obje_sort) { 1288c2e8227SGreg Roach return array_search($x->getXref(), $wt_obje_sort) - array_search($y->getXref(), $wt_obje_sort); 1298c2e8227SGreg Roach }); 1308c2e8227SGreg Roach } 131cbc1590aSGreg Roach 1328c2e8227SGreg Roach return $this->media_list; 1338c2e8227SGreg Roach } 1348c2e8227SGreg Roach 13576692c8bSGreg Roach /** 13676692c8bSGreg Roach * Can this tab load asynchronously? 13776692c8bSGreg Roach * 13876692c8bSGreg Roach * @return bool 13976692c8bSGreg Roach */ 1408c2e8227SGreg Roach public function canLoadAjax() { 14115d603e7SGreg Roach return false; 1428c2e8227SGreg Roach } 1438c2e8227SGreg Roach} 144