xref: /webtrees/app/Module/MediaTabModule.php (revision 76a5e7c78a07f5736a3cd5b7437c8f18dc196a5e)
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="edit_interface.php?action=add-media-link&amp;ged=<?= $controller->record->getTree()->getNameHtml() ?>&amp;xref=<?= $controller->record->getXref() ?>">
86						<?= I18N::translate('Add a media object') ?>
87					</a>
88				</td>
89			</tr>
90		<?php
91		}
92		?>
93		</table>
94		<?php
95		return '<div id="' . $this->getName() . '_content">' . ob_get_clean() . '</div>';
96	}
97
98	/**
99	 * Get all the facts for an individual which contain media objects.
100	 *
101	 * @return Fact[]
102	 */
103	private function getFactsWithMedia() {
104		global $controller;
105
106		if ($this->facts === null) {
107			$facts = $controller->record->getFacts();
108			foreach ($controller->record->getSpouseFamilies() as $family) {
109				if ($family->canShow()) {
110					foreach ($family->getFacts() as $fact) {
111						$facts[] = $fact;
112					}
113				}
114			}
115			$this->facts = [];
116			foreach ($facts as $fact) {
117				if (preg_match('/(?:^1|\n\d) OBJE @' . WT_REGEX_XREF . '@/', $fact->getGedcom())) {
118					$this->facts[] = $fact;
119				}
120			}
121			Functions::sortFacts($this->facts);
122		}
123
124		return $this->facts;
125	}
126
127	/** {@inheritdoc} */
128	public function canLoadAjax() {
129		return false;
130	}
131
132	/** {@inheritdoc} */
133	public function getPreLoadContent() {
134		return '';
135	}
136}
137