Passed
Push — master ( eb013e...37f747 )
by Jonathan
04:35
created

GedcomRecord::isFactSourced()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 6
nop 1
dl 0
loc 19
rs 9.2222
c 0
b 0
f 0
1
<?php
2
/**
3
 * webtrees-lib: MyArtJaub library for webtrees
4
 *
5
 * @package MyArtJaub\Webtrees
6
 * @author Jonathan Jaubart <[email protected]>
7
 * @copyright Copyright (c) 2011-2016, Jonathan Jaubart
8
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
9
 */
10
11
namespace MyArtJaub\Webtrees;
12
13
use MyArtJaub\Webtrees\Globals;
14
15
/**
16
 * Decorator class to extend native webtrees GedcomRecord class.
17
 * 
18
 * @see \Fisharebest\Webtrees\GedcomRecord
19
 */
20
class GedcomRecord {
21
22
	/** @var \Fisharebest\Webtrees\GedcomRecord Underlying base GedcomRecord */
23
	protected $gedcomrecord;
24
25
	/** @var bool Is the GedcomRecord sourced (cache) */ 
26
	protected $_issourced=null;
27
28
	/**
29
	 * Contructor for the decorator
30
	 *
31
	 * @param \Fisharebest\Webtrees\GedcomRecord $gedcomrecord_in The GedcomRecord to extend
32
	 */
33
	public function __construct(\Fisharebest\Webtrees\GedcomRecord $gedcomrecord_in){
34
		$this->gedcomrecord = $gedcomrecord_in;
35
	}
36
37
	/**
38
	 * Return the native gedcom record embedded within the decorator
39
	 *
40
	 * @return \Fisharebest\Webtrees\GedcomRecord Embedded gedcom record
41
	 */
42
	public function getDerivedRecord(){
43
		return $this->gedcomrecord;
44
	}
45
	
46
	/**
47
	 * Return whether the record is a new addition (and not just a modification)
48
	 * 
49
	 * @return boolean True id new addition
50
	 */
51
	public function isNewAddition() {
52
		return $this->gedcomrecord->isPendingAddtion()
53
			&& $this->gedcomrecord->privatizeGedcom(\Fisharebest\Webtrees\Auth::PRIV_HIDE) === null;
54
	}
55
		
56
	/**
57
	 * Add additional options to the core formatFirstMajorFact function.
58
	 * If no option is suitable, it will try returning the core function.
59
	 *
60
	 * Option 10 : display <i>factLabel shortFactDate shortFactPlace</i>
61
	 *
62
	 * @uses \MyArtJaub\Webtrees\Functions\FunctionsPrint
63
	 * @param string $facts List of facts to find information from
64
	 * @param int $style Style to apply to the information. Number >= 10 should be used in this function, lower number will return the core function.
65
	 * @return string Formatted fact description
66
	 */
67
	public function formatFirstMajorFact($facts, $style) {
68
		foreach ($this->gedcomrecord->getFacts($facts) as $fact) {
69
			// Only display if it has a date or place (or both)
70
			if (($fact->getDate() || $fact->getPlace()) && $fact->canShow()) {
71
				switch ($style) {
72
					case 10:
73
					    return '<i>'.$fact->getLabel().' '. \MyArtJaub\Webtrees\Functions\FunctionsPrint::formatFactDateShort($fact) .'&nbsp;'. \MyArtJaub\Webtrees\Functions\FunctionsPrint::formatFactPlaceShort($fact, '%1') .'</i>';
74
					default:
75
						return $this->gedcomrecord->formatFirstMajorFact($facts, $style);
76
				}
77
			}
78
		}
79
		return '';
80
	}
81
82
	/**
83
	 * Check if the IsSourced information can be displayed
84
	 *
85
	 * @param int $access_level
86
	 * @return boolean
87
	 */
88
	public function canDisplayIsSourced($access_level = null){
89
		if(!$this->gedcomrecord->canShow($access_level)) return false;
90
		if($access_level === null )
91
		    $access_level = \Fisharebest\Webtrees\Auth::accessLevel($this->gedcomrecord->getTree());
92
93
		$global_facts = Globals::getGlobalFacts();
94
		if (isset($global_facts['SOUR'])) {
95
			return $global_facts['SOUR'] >= $access_level;
96
		}
97
98
		return true;
99
	}
100
101
	/**
102
	 * Check if a gedcom record is sourced
103
	 * Values:
104
	 * 		- -1, if the record has no sources attached
105
	 * 		- 1, if the record has a source attached
106
	 * 		- 2, if the record has a source, and a certificate supporting it
107
	 *
108
	 * @return int Level of sources
109
	 */
110
	public function isSourced(){
111
		if($this->_issourced !== null) return $this->_issourced;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_issourced returns the type boolean which is incompatible with the documented return type integer.
Loading history...
112
		$this->_issourced=-1;
0 ignored issues
show
Documentation Bug introduced by
The property $_issourced was declared of type boolean, but -1 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
113
		$sourcesfacts = $this->gedcomrecord->getFacts('SOUR');
114
		foreach($sourcesfacts as $sourcefact){
115
			$this->_issourced=max($this->_issourced, 1);
116
			if($sourcefact->getAttribute('_ACT')){
117
				$this->_issourced=max($this->_issourced, 2);
118
			}
119
		}
120
		return $this->_issourced;
121
	}
122
123
	/**
124
	 * Check is an event associated to this record is sourced
125
	 *
126
	 * @param string $eventslist
127
	 * @return int Level of sources
128
	 */
129
	public function isFactSourced($eventslist){
130
	    if(empty($eventslist)) return 0;
131
		$isSourced=0;
132
		$facts = $this->gedcomrecord->getFacts($eventslist);
133
		foreach($facts as $fact){
134
			if($isSourced < Fact::MAX_IS_SOURCED_LEVEL){
135
				$dfact = new Fact($fact);
136
				$tmpIsSourced = $dfact->isSourced();
137
				if($tmpIsSourced != 0) {
138
					if($isSourced==0) {
139
						$isSourced =  $tmpIsSourced;
140
					}
141
					else{
142
						$isSourced = max($isSourced, $tmpIsSourced);
143
					}
144
				}
145
			}
146
		}
147
		return $isSourced;
148
	}
149
150
151
}
152
153
154
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...