xref: /webtrees/app/Module/MediaTabModule.php (revision d2681c37325a35ab01be82034f4afd3b58010fb8)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16namespace Fisharebest\Webtrees\Module;
17
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Fact;
20use Fisharebest\Webtrees\Functions\Functions;
21use Fisharebest\Webtrees\Functions\FunctionsPrintFacts;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Individual;
24
25/**
26 * Class MediaTabModule
27 */
28class MediaTabModule extends AbstractModule implements ModuleTabInterface {
29	/** @var  Fact[] A list of facts with media objects. */
30	private $facts;
31
32	/** {@inheritdoc} */
33	public function getTitle() {
34		return /* I18N: Name of a module */ I18N::translate('Media');
35	}
36
37	/** {@inheritdoc} */
38	public function getDescription() {
39		return /* I18N: Description of the “Media” module */ I18N::translate('A tab showing the media objects linked to an individual.');
40	}
41
42	/** {@inheritdoc} */
43	public function defaultTabOrder() {
44		return 50;
45	}
46
47	/** {@inheritdoc} */
48	public function hasTabContent(Individual $individual) {
49		return $individual->canEdit() || $this->getFactsWithMedia($individual);
50	}
51
52	/** {@inheritdoc} */
53	public function isGrayedOut(Individual $individual) {
54		return !$this->getFactsWithMedia($individual);
55	}
56
57	/** {@inheritdoc} */
58	public function getTabContent(Individual $individual) {
59		return view('tabs/media', [
60			'can_edit'   => $individual->canEdit(),
61			'individual' => $individual,
62			'facts'      => $this->getFactsWithMedia($individual),
63		]);
64	}
65
66	/**
67	 * Get all the facts for an individual which contain media objects.
68	 *
69	 * @param Individual $individual
70	 *
71	 * @return Fact[]
72	 */
73	private function getFactsWithMedia(Individual $individual) {
74		if ($this->facts === null) {
75			$facts = $individual->getFacts();
76			foreach ($individual->getSpouseFamilies() as $family) {
77				if ($family->canShow()) {
78					foreach ($family->getFacts() as $fact) {
79						$facts[] = $fact;
80					}
81				}
82			}
83			$this->facts = [];
84			foreach ($facts as $fact) {
85				if (preg_match('/(?:^1|\n\d) OBJE @' . WT_REGEX_XREF . '@/', $fact->getGedcom())) {
86					$this->facts[] = $fact;
87				}
88			}
89			Functions::sortFacts($this->facts);
90		}
91
92		return $this->facts;
93	}
94
95	/** {@inheritdoc} */
96	public function canLoadAjax() {
97		return false;
98	}
99}
100