xref: /webtrees/app/Module/MediaTabModule.php (revision 44d05b50b37837a977c66c3b1750262bf1421171)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 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;
23
24/**
25 * Class MediaTabModule
26 */
27class MediaTabModule extends AbstractModule implements ModuleTabInterface {
28	/** @var  Fact[] A list of facts with media objects. */
29	private $facts;
30
31	/** {@inheritdoc} */
32	public function getTitle() {
33		return /* I18N: Name of a module */ I18N::translate('Media');
34	}
35
36	/** {@inheritdoc} */
37	public function getDescription() {
38		return /* I18N: Description of the “Media” module */ I18N::translate('A tab showing the media objects linked to an individual.');
39	}
40
41	/** {@inheritdoc} */
42	public function defaultTabOrder() {
43		return 50;
44	}
45
46	/** {@inheritdoc} */
47	public function hasTabContent() {
48		global $WT_TREE;
49
50		return Auth::isEditor($WT_TREE) || $this->getFactsWithMedia();
51	}
52
53	/** {@inheritdoc} */
54	public function isGrayedOut() {
55		return !$this->getFactsWithMedia();
56	}
57
58	/** {@inheritdoc} */
59	public function getTabContent() {
60		ob_start();
61		echo '<table class="facts_table">';
62		foreach ($this->getFactsWithMedia() as $fact) {
63			if ($fact->getTag() == 'OBJE') {
64				FunctionsPrintFacts::printMainMedia($fact, 1);
65			} else {
66				for ($i = 2; $i < 4; ++$i) {
67					FunctionsPrintFacts::printMainMedia($fact, $i);
68				}
69			}
70		}
71		if (!$this->getFactsWithMedia()) {
72			echo '<tr><td id="no_tab4" colspan="2" class="facts_value">', I18N::translate('There are no media objects for this individual.'), '</td></tr>';
73		}
74		?>
75		</table>
76		<?php
77		return '<div id="' . $this->getName() . '_content">' . ob_get_clean() . '</div>';
78	}
79
80	/**
81	 * Get all the facts for an individual which contain media objects.
82	 *
83	 * @return Fact[]
84	 */
85	private function getFactsWithMedia() {
86		global $controller;
87
88		if ($this->facts === null) {
89			$facts = $controller->record->getFacts();
90			foreach ($controller->record->getSpouseFamilies() as $family) {
91				if ($family->canShow()) {
92					foreach ($family->getFacts() as $fact) {
93						$facts[] = $fact;
94					}
95				}
96			}
97			$this->facts = [];
98			foreach ($facts as $fact) {
99				if (preg_match('/(?:^1|\n\d) OBJE @' . WT_REGEX_XREF . '@/', $fact->getGedcom())) {
100					$this->facts[] = $fact;
101				}
102			}
103			Functions::sortFacts($this->facts);
104		}
105
106		return $this->facts;
107	}
108
109	/** {@inheritdoc} */
110	public function canLoadAjax() {
111		return false;
112	}
113
114	/** {@inheritdoc} */
115	public function getPreLoadContent() {
116		return '';
117	}
118}
119