xref: /webtrees/app/Module/MediaTabModule.php (revision 15d603e7c7c15d20f055d3d9c38d6b133453c5be)
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\FunctionsPrint;
22use Fisharebest\Webtrees\Functions\FunctionsPrintFacts;
23use Fisharebest\Webtrees\I18N;
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() {
49		global $WT_TREE;
50
51		return Auth::isEditor($WT_TREE) || $this->getFactsWithMedia();
52	}
53
54	/** {@inheritdoc} */
55	public function isGrayedOut() {
56		return !$this->getFactsWithMedia();
57	}
58
59	/** {@inheritdoc} */
60	public function getTabContent() {
61		global $WT_TREE, $controller;
62
63		ob_start();
64		echo '<table class="facts_table">';
65		foreach ($this->getFactsWithMedia() as $fact) {
66			if ($fact->getTag() == 'OBJE') {
67				FunctionsPrintFacts::printMainMedia($fact, 1);
68			} else {
69				for ($i = 2; $i < 4; ++$i) {
70					FunctionsPrintFacts::printMainMedia($fact, $i);
71				}
72			}
73		}
74		if (!$this->getFactsWithMedia()) {
75			echo '<tr><td id="no_tab4" colspan="2" class="facts_value">', I18N::translate('There are no media objects for this individual.'), '</td></tr>';
76		}
77		// New media link
78		if ($controller->record->canEdit() && $WT_TREE->getPreference('MEDIA_UPLOAD') >= Auth::accessLevel($controller->record->getTree())) {
79			?>
80			<tr>
81				<td class="facts_label">
82					<?= I18N::translate('Media object') ?>
83				</td>
84				<td class="facts_value">
85					<a href="#" onclick="window.open('addmedia.php?action=showmediaform&amp;linktoid=<?= $controller->record->getXref() ?>&amp;ged=<?= $controller->record->getTree()->getNameUrl() ?>', '_blank', edit_window_specs); return false;">
86						<?= I18N::translate('Add a media object') ?>
87					</a>
88					<?= FunctionsPrint::helpLink('OBJE') ?>
89					<br>
90					<a href="#" onclick="window.open('inverselink.php?linktoid=<?= $controller->record->getXref() ?>&amp;ged=<?= $WT_TREE->getNameUrl() ?>&amp;linkto=person', '_blank', find_window_specs); return false;">
91						<?= I18N::translate('Link to an existing media object') ?>
92					</a>
93				</td>
94			</tr>
95		<?php
96		}
97		?>
98		</table>
99		<?php
100		return '<div id="' . $this->getName() . '_content">' . ob_get_clean() . '</div>';
101	}
102
103	/**
104	 * Get all the facts for an individual which contain media objects.
105	 *
106	 * @return Fact[]
107	 */
108	private function getFactsWithMedia() {
109		global $controller;
110
111		if ($this->facts === null) {
112			$facts = $controller->record->getFacts();
113			foreach ($controller->record->getSpouseFamilies() as $family) {
114				if ($family->canShow()) {
115					foreach ($family->getFacts() as $fact) {
116						$facts[] = $fact;
117					}
118				}
119			}
120			$this->facts = [];
121			foreach ($facts as $fact) {
122				if (preg_match('/(?:^1|\n\d) OBJE @' . WT_REGEX_XREF . '@/', $fact->getGedcom())) {
123					$this->facts[] = $fact;
124				}
125			}
126			Functions::sortFacts($this->facts);
127		}
128
129		return $this->facts;
130	}
131
132	/** {@inheritdoc} */
133	public function canLoadAjax() {
134		return false;
135	}
136
137	/** {@inheritdoc} */
138	public function getPreLoadContent() {
139		return '';
140	}
141}
142