Completed
Push — develop ( 989811...4ba2ac )
by Greg
10:44
created

GedcomRecordController::getEditMenu()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 24
rs 6.7272
cc 7
eloc 11
nc 5
nop 0
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
 */
16
namespace Fisharebest\Webtrees\Controller;
17
18
use Fisharebest\Webtrees\Auth;
19
use Fisharebest\Webtrees\Family;
20
use Fisharebest\Webtrees\Filter;
21
use Fisharebest\Webtrees\FlashMessages;
22
use Fisharebest\Webtrees\GedcomRecord;
23
use Fisharebest\Webtrees\GedcomTag;
24
use Fisharebest\Webtrees\I18N;
25
use Fisharebest\Webtrees\Individual;
26
use Fisharebest\Webtrees\Media;
27
use Fisharebest\Webtrees\Menu;
28
use Fisharebest\Webtrees\Module;
29
use Fisharebest\Webtrees\Note;
30
use Fisharebest\Webtrees\Repository;
31
use Fisharebest\Webtrees\Source;
32
33
/**
34
 * Base controller for all GedcomRecord controllers
35
 */
36
class GedcomRecordController extends PageController {
37
	/**
38
	 * A genealogy record
39
	 *
40
	 * @var GedcomRecord|Individual|Family|Source|Repository|Media|Note
41
	 */
42
	public $record;
43
44
	/**
45
	 * Startup activity
46
	 *
47
	 * @param GedcomRecord|null $record
48
	 */
49
	public function __construct(GedcomRecord $record = null) {
50
		$this->record = $record;
51
52
		// Automatically fix broken links
53
		if ($this->record && $this->record->canEdit()) {
54
			$broken_links = 0;
55 View Code Duplication
			foreach ($this->record->getFacts('HUSB|WIFE|CHIL|FAMS|FAMC|REPO') as $fact) {
56
				if (!$fact->isPendingDeletion() && $fact->getTarget() === null) {
57
					$this->record->deleteFact($fact->getFactId(), false);
58
					FlashMessages::addMessage(/* I18N: %s are names of records, such as sources, repositories or individuals */ I18N::translate('The link from ā€œ%1$sā€ to ā€œ%2$sā€ has been deleted.', $this->record->getFullName(), $fact->getValue()));
59
					$broken_links = true;
60
				}
61
			}
62 View Code Duplication
			foreach ($this->record->getFacts('NOTE|SOUR|OBJE') as $fact) {
63
				// These can be links or inline. Only delete links.
64
				if (!$fact->isPendingDeletion() && $fact->getTarget() === null && preg_match('/^@.*@$/', $fact->getValue())) {
65
					$this->record->deleteFact($fact->getFactId(), false);
66
					FlashMessages::addMessage(/* I18N: %s are names of records, such as sources, repositories or individuals */ I18N::translate('The link from ā€œ%1$sā€ to ā€œ%2$sā€ has been deleted.', $this->record->getFullName(), $fact->getValue()));
67
					$broken_links = true;
68
				}
69
			}
70
			if ($broken_links) {
71
				// Reload the updated family
72
				$this->record = GedcomRecord::getInstance($this->record->getXref(), $this->record->getTree());
73
			}
74
		}
75
76
		parent::__construct();
77
78
		// We want robots to index this page
79
		$this->setMetaRobots('index,follow');
80
81
		// Set a page title
82
		if ($this->record) {
83
			if ($this->record->canShowName()) {
84
				// e.g. "John Doe" or "1881 Census of Wales"
85
				$this->setPageTitle($this->record->getFullName());
86
			} else {
87
				// e.g. "Individual" or "Source"
88
				$record = $this->record;
89
				$this->setPageTitle(GedcomTag::getLabel($record::RECORD_TYPE));
90
			}
91
		} else {
92
			// No such record
93
			$this->setPageTitle(I18N::translate('Private'));
94
		}
95
	}
96
97
	/**
98
	 * get edit menu
99
	 */
100
	public function getEditMenu() {
101
		if (!$this->record || $this->record->isPendingDeletion()) {
102
			return null;
103
		}
104
105
		// edit menu
106
		$menu = new Menu(I18N::translate('Edit'), '#', 'menu-record');
107
108
		// edit raw
109
		if (Auth::isAdmin() || Auth::isEditor($this->record->getTree()) && $this->record->getTree()->getPreference('SHOW_GEDCOM_RECORD')) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->record->getTree()...e('SHOW_GEDCOM_RECORD') of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
110
			$menu->addSubmenu(new Menu(I18N::translate('Edit raw GEDCOM'), '#', 'menu-record-editraw', array(
111
				'onclick' => 'return edit_raw("' . $this->record->getXref() . '");',
112
			)));
113
		}
114
115
		// delete
116
		if (Auth::isEditor($this->record->getTree())) {
117
			$menu->addSubmenu(new Menu(I18N::translate('Delete'), '#', 'menu-record-del', array(
118
				'onclick' => 'return delete_record("' . I18N::translate('Are you sure you want to delete ā€œ%sā€?', Filter::escapeJs(Filter::unescapeHtml($this->record->getFullName()))) . '", "' . $this->record->getXref() . '");',
119
			)));
120
		}
121
122
		return $menu;
123
	}
124
}
125