xref: /webtrees/app/Module/MediaTabModule.php (revision 225e381f36d59b49c8cdac0060465fa5af2fc308)
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\Fact;
203d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\Functions;
213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintFacts;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
23*225e381fSGreg Roachuse Fisharebest\Webtrees\Individual;
248c2e8227SGreg Roach
258c2e8227SGreg Roach/**
268c2e8227SGreg Roach * Class MediaTabModule
278c2e8227SGreg Roach */
28e2a378d3SGreg Roachclass MediaTabModule extends AbstractModule implements ModuleTabInterface {
2976692c8bSGreg Roach	/** @var  Fact[] A list of facts with media objects. */
308c2e8227SGreg Roach	private $facts;
318c2e8227SGreg Roach
328c2e8227SGreg Roach	/** {@inheritdoc} */
338c2e8227SGreg Roach	public function getTitle() {
348c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('Media');
358c2e8227SGreg Roach	}
368c2e8227SGreg Roach
378c2e8227SGreg Roach	/** {@inheritdoc} */
388c2e8227SGreg Roach	public function getDescription() {
398c2e8227SGreg Roach		return /* I18N: Description of the “Media” module */ I18N::translate('A tab showing the media objects linked to an individual.');
408c2e8227SGreg Roach	}
418c2e8227SGreg Roach
428c2e8227SGreg Roach	/** {@inheritdoc} */
438c2e8227SGreg Roach	public function defaultTabOrder() {
448c2e8227SGreg Roach		return 50;
458c2e8227SGreg Roach	}
468c2e8227SGreg Roach
478c2e8227SGreg Roach	/** {@inheritdoc} */
48*225e381fSGreg Roach	public function hasTabContent(Individual $individual) {
49*225e381fSGreg Roach		return $individual->canEdit() || $this->getFactsWithMedia($individual);
508c2e8227SGreg Roach	}
518c2e8227SGreg Roach
528c2e8227SGreg Roach	/** {@inheritdoc} */
53*225e381fSGreg Roach	public function isGrayedOut(Individual $individual) {
54*225e381fSGreg Roach		return !$this->getFactsWithMedia($individual);
558c2e8227SGreg Roach	}
568c2e8227SGreg Roach
578c2e8227SGreg Roach	/** {@inheritdoc} */
58*225e381fSGreg Roach	public function getTabContent(Individual $individual) {
59*225e381fSGreg Roach		return view('tabs/media', [
60*225e381fSGreg Roach			'can_edit'   => $individual->canEdit(),
61*225e381fSGreg Roach			'individual' => $individual,
62*225e381fSGreg Roach			'facts'      => $this->getFactsWithMedia($individual),
63*225e381fSGreg Roach		]);
648c2e8227SGreg Roach	}
658c2e8227SGreg Roach
668c2e8227SGreg Roach	/**
678c2e8227SGreg Roach	 * Get all the facts for an individual which contain media objects.
688c2e8227SGreg Roach	 *
69*225e381fSGreg Roach	 * @param Individual $individual
70*225e381fSGreg Roach	 *
718c2e8227SGreg Roach	 * @return Fact[]
728c2e8227SGreg Roach	 */
73*225e381fSGreg Roach	private function getFactsWithMedia(Individual $individual) {
748c2e8227SGreg Roach		if ($this->facts === null) {
75*225e381fSGreg Roach			$facts = $individual->getFacts();
76*225e381fSGreg Roach			foreach ($individual->getSpouseFamilies() as $family) {
778c2e8227SGreg Roach				if ($family->canShow()) {
788c2e8227SGreg Roach					foreach ($family->getFacts() as $fact) {
798c2e8227SGreg Roach						$facts[] = $fact;
808c2e8227SGreg Roach					}
818c2e8227SGreg Roach				}
828c2e8227SGreg Roach			}
8313abd6f3SGreg Roach			$this->facts = [];
848c2e8227SGreg Roach			foreach ($facts as $fact) {
858c2e8227SGreg Roach				if (preg_match('/(?:^1|\n\d) OBJE @' . WT_REGEX_XREF . '@/', $fact->getGedcom())) {
868c2e8227SGreg Roach					$this->facts[] = $fact;
878c2e8227SGreg Roach				}
888c2e8227SGreg Roach			}
893d7a8a4cSGreg Roach			Functions::sortFacts($this->facts);
908c2e8227SGreg Roach		}
918c2e8227SGreg Roach
928c2e8227SGreg Roach		return $this->facts;
938c2e8227SGreg Roach	}
948c2e8227SGreg Roach
958c2e8227SGreg Roach	/** {@inheritdoc} */
968c2e8227SGreg Roach	public function canLoadAjax() {
9715d603e7SGreg Roach		return false;
988c2e8227SGreg Roach	}
998c2e8227SGreg Roach}
100