xref: /webtrees/app/Module/SourcesTabModule.php (revision 9f2390a04226d0058d1862402c80d50fe6e79aa1)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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\Fact;
19use Fisharebest\Webtrees\Functions\Functions;
20use Fisharebest\Webtrees\I18N;
21use Fisharebest\Webtrees\Individual;
22
23/**
24 * Class SourcesTabModule
25 */
26class SourcesTabModule extends AbstractModule implements ModuleTabInterface {
27	/** @var Fact[] All facts belonging to this source. */
28	private $facts;
29
30	/** {@inheritdoc} */
31	public function getTitle() {
32		return /* I18N: Name of a module */ I18N::translate('Sources');
33	}
34
35	/** {@inheritdoc} */
36	public function getDescription() {
37		return /* I18N: Description of the “Sources” module */ I18N::translate('A tab showing the sources linked to an individual.');
38	}
39
40	/** {@inheritdoc} */
41	public function defaultTabOrder() {
42		return 30;
43	}
44
45	/** {@inheritdoc} */
46	public function hasTabContent(Individual $individual) {
47		return $individual->canedit() || $this->getFactsWithSources($individual);
48	}
49
50	/** {@inheritdoc} */
51	public function isGrayedOut(Individual $individual) {
52		return !$this->getFactsWithSources($individual);
53	}
54
55	/** {@inheritdoc} */
56	public function getTabContent(Individual $individual) {
57		return view('tabs/sources', [
58			'can_edit'   => $individual->canEdit(),
59			'individual' => $individual,
60			'facts'      => $this->getFactsWithSources($individual),
61		]);
62	}
63
64	/**
65	 * Get all the facts for an individual which contain sources.
66	 *
67	 * @param Individual $individual
68	 *
69	 * @return Fact[]
70	 */
71	private function getFactsWithSources(Individual $individual) {
72		if ($this->facts === null) {
73			$facts = $individual->getFacts();
74			foreach ($individual->getSpouseFamilies() as $family) {
75				if ($family->canShow()) {
76					foreach ($family->getFacts() as $fact) {
77						$facts[] = $fact;
78					}
79				}
80			}
81			$this->facts = [];
82			foreach ($facts as $fact) {
83				if (preg_match('/(?:^1|\n\d) SOUR/', $fact->getGedcom())) {
84					$this->facts[] = $fact;
85				}
86			}
87			Functions::sortFacts($this->facts);
88		}
89
90		return $this->facts;
91	}
92
93	/** {@inheritdoc} */
94	public function canLoadAjax() {
95		return false;
96	}
97}
98