xref: /webtrees/app/Module/MediaTabModule.php (revision 3e983931fdde6db78f1490364106d7d46e77dea7)
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="table wt-facts-table">';
62		echo '<tbody>';
63		foreach ($this->getFactsWithMedia() as $fact) {
64			if ($fact->getTag() == 'OBJE') {
65				FunctionsPrintFacts::printMainMedia($fact, 1);
66			} else {
67				for ($i = 2; $i < 4; ++$i) {
68					FunctionsPrintFacts::printMainMedia($fact, $i);
69				}
70			}
71		}
72		?>
73		</tbody>
74		</table>
75		<?php
76		if (!$this->getFactsWithMedia()) {
77			echo '<p>', I18N::translate('There are no media objects for this individual.'), '</p>';
78		}
79
80		return '<div id="' . $this->getName() . '_content">' . ob_get_clean() . '</div>';
81	}
82
83	/**
84	 * Get all the facts for an individual which contain media objects.
85	 *
86	 * @return Fact[]
87	 */
88	private function getFactsWithMedia() {
89		global $controller;
90
91		if ($this->facts === null) {
92			$facts = $controller->record->getFacts();
93			foreach ($controller->record->getSpouseFamilies() as $family) {
94				if ($family->canShow()) {
95					foreach ($family->getFacts() as $fact) {
96						$facts[] = $fact;
97					}
98				}
99			}
100			$this->facts = [];
101			foreach ($facts as $fact) {
102				if (preg_match('/(?:^1|\n\d) OBJE @' . WT_REGEX_XREF . '@/', $fact->getGedcom())) {
103					$this->facts[] = $fact;
104				}
105			}
106			Functions::sortFacts($this->facts);
107		}
108
109		return $this->facts;
110	}
111
112	/** {@inheritdoc} */
113	public function canLoadAjax() {
114		return false;
115	}
116
117	/** {@inheritdoc} */
118	public function getPreLoadContent() {
119		return '';
120	}
121}
122