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