xref: /webtrees/app/Module/MediaTabModule.php (revision 309092efc56540a95d8117fad7180fe470aadff6)
1<?php
2namespace Fisharebest\Webtrees;
3
4/**
5 * webtrees: online genealogy
6 * Copyright (C) 2015 webtrees development team
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/**
20 * Class MediaTabModule
21 */
22class MediaTabModule extends AbstractModule implements ModuleTabInterface {
23	private $facts;
24
25	/** {@inheritdoc} */
26	public function getTitle() {
27		return /* I18N: Name of a module */ I18N::translate('Media');
28	}
29
30	/** {@inheritdoc} */
31	public function getDescription() {
32		return /* I18N: Description of the “Media” module */ I18N::translate('A tab showing the media objects linked to an individual.');
33	}
34
35	/** {@inheritdoc} */
36	public function defaultTabOrder() {
37		return 50;
38	}
39
40	/** {@inheritdoc} */
41	public function hasTabContent() {
42		global $WT_TREE;
43
44		return Auth::isEditor($WT_TREE) || $this->getFactsWithMedia();
45	}
46
47	/** {@inheritdoc} */
48	public function isGrayedOut() {
49		return !$this->getFactsWithMedia();
50	}
51
52	/** {@inheritdoc} */
53	public function getTabContent() {
54		global $WT_TREE, $controller;
55
56		ob_start();
57		echo '<table class="facts_table">';
58		foreach ($this->getFactsWithMedia() as $fact) {
59			if ($fact->getTag() == 'OBJE') {
60				print_main_media($fact, 1);
61			} else {
62				for ($i = 2; $i < 4; ++$i) {
63					print_main_media($fact, $i);
64				}
65			}
66		}
67		if (!$this->getFactsWithMedia()) {
68			echo '<tr><td id="no_tab4" colspan="2" class="facts_value">', I18N::translate('There are no media objects for this individual.'), '</td></tr>';
69		}
70		// New media link
71		if ($controller->record->canEdit() && $WT_TREE->getPreference('MEDIA_UPLOAD') >= Auth::accessLevel($controller->record->getTree())) {
72			?>
73			<tr>
74				<td class="facts_label">
75					<?php echo GedcomTag::getLabel('OBJE'); ?>
76				</td>
77				<td class="facts_value">
78					<a href="#" onclick="window.open('addmedia.php?action=showmediaform&amp;linktoid=<?php echo $controller->record->getXref(); ?>&amp;ged=<?php echo $controller->record->getTree()->getNameUrl(); ?>', '_blank', edit_window_specs); return false;">
79						<?php echo I18N::translate('Add a new media object'); ?>
80					</a>
81					<?php echo help_link('OBJE'); ?>
82					<br>
83					<a href="#" onclick="window.open('inverselink.php?linktoid=<?php echo $controller->record->getXref(); ?>&amp;ged=<?php echo $WT_TREE->getNameUrl(); ?>&amp;linkto=person', '_blank', find_window_specs); return false;">
84						<?php echo I18N::translate('Link to an existing media object'); ?>
85					</a>
86				</td>
87			</tr>
88		<?php
89		}
90		?>
91		</table>
92		<?php
93		return '<div id="' . $this->getName() . '_content">' . ob_get_clean() . '</div>';
94	}
95
96	/**
97	 * Get all the facts for an individual which contain media objects.
98	 *
99	 * @return Fact[]
100	 */
101	private function getFactsWithMedia() {
102		global $controller;
103
104		if ($this->facts === null) {
105			$facts = $controller->record->getFacts();
106			foreach ($controller->record->getSpouseFamilies() as $family) {
107				if ($family->canShow()) {
108					foreach ($family->getFacts() as $fact) {
109						$facts[] = $fact;
110					}
111				}
112			}
113			$this->facts = array();
114			foreach ($facts as $fact) {
115				if (preg_match('/(?:^1|\n\d) OBJE @' . WT_REGEX_XREF . '@/', $fact->getGedcom())) {
116					$this->facts[] = $fact;
117				}
118			}
119			sort_facts($this->facts);
120		}
121
122		return $this->facts;
123	}
124
125	/** {@inheritdoc} */
126	public function canLoadAjax() {
127		return !Auth::isSearchEngine(); // Search engines cannot use AJAX
128	}
129
130	/** {@inheritdoc} */
131	public function getPreLoadContent() {
132		return '';
133	}
134}
135