Completed
Push — master ( 0f93c5...bdc52b )
by Jonathan
04:53
created

FunctionsPrint   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 335
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

Changes 6
Bugs 3 Features 0
Metric Value
wmc 57
c 6
b 3
f 0
lcom 0
cbo 9
dl 0
loc 335
rs 5.1724

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getListFromArray() 0 16 3
A htmlFactPlaceIcon() 0 13 4
A htmlPlaceIcon() 0 3 1
D htmlListCloud() 0 32 9
A htmlPlacesCloud() 0 15 2
B htmlIndividualForList() 0 23 4
B formatFactDateShort() 0 20 8
A formatFactPlaceShort() 0 11 3
B formatSosaNumbers() 0 27 6
C formatIsSourcedIcon() 0 65 15
A isDateWithinChartsRange() 0 3 2

How to fix   Complexity   

Complex Class

Complex classes like FunctionsPrint often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use FunctionsPrint, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * webtrees-lib: MyArtJaub library for webtrees
4
 *
5
 * @package MyArtJaub\Webtrees
6
 * @subpackage Functions
7
 * @author Jonathan Jaubart <[email protected]>
8
 * @copyright Copyright (c) 2011-2016, Jonathan Jaubart
9
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
10
 */
11
namespace MyArtJaub\Webtrees\Functions;
12
13
use Fisharebest\Webtrees\GedcomTag;
14
use Fisharebest\Webtrees\I18N;
15
use Fisharebest\Webtrees\Tree;
16
use MyArtJaub\Webtrees\Individual;
17
use MyArtJaub\Webtrees\Place;
18
use Fisharebest\Webtrees\Date;
19
20
21
/**
22
 * Additional functions to display information
23
 */
24
class FunctionsPrint {
25
26
	/**
27
	 * Get an array converted to a list. For example
28
	 * array("red", "green", "yellow", "blue") => "red, green, yellow and blue"
29
	 *
30
	 * @param array $array Array to convert
31
	 * @return string List of elements
32
	 */
33
	public static function getListFromArray(array $array) {
34
		$n=count($array);
35
		switch ($n) {
36
			case 0:
37
				return '';
38
			case 1:
39
				return $array[0];
40
			default:
41
				return implode(
42
						/* I18N: list separator */ I18N::translate(', '), 
43
						array_slice($array, 0, $n-1)
44
					) .
45
					/* I18N: last list separator, " and " in English, " et " in French  */ I18N::translate(' and ') . 
46
					$array[$n-1];
47
		}
48
	}
49
50
	/**
51
	 * Return HTML code to include a flag icon in facts description
52
	 *
53
	 * @param \Fisharebest\Webtrees\Fact $fact Fact record
54
	 * @param \MyArtJaub\Webtrees\Map\MapProviderInterface $mapProvider Map Provider
55
	 * @return string HTML code of the inserted flag
56
	 */
57
	public static function htmlFactPlaceIcon(
58
			\Fisharebest\Webtrees\Fact $fact,
59
			\MyArtJaub\Webtrees\Map\MapProviderInterface $mapProvider
60
	) {
61
		$html='';
62
		if($place = $fact->getPlace()) {
63
			$iconPlace= $mapProvider->getPlaceIcon($place);	
64
			if($iconPlace && strlen($iconPlace) > 0){
0 ignored issues
show
Bug Best Practice introduced by
The expression $iconPlace 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...
65
				$html.=	'<div class="fact_flag">'. self::htmlPlaceIcon($place, $iconPlace, 50). '</div>';
66
			}
67
		}
68
		return $html;
69
	}
70
	
71
	/**
72
	 * Return HTML code to include a flag icon
73
	 * 
74
	 * @param \Fisharebest\Webtrees\Place $place
75
	 * @param string $icon_path
76
	 * @param number $size
77
	 * @return string HTML code of the inserted flag
78
	 */
79
	public static function htmlPlaceIcon(\Fisharebest\Webtrees\Place $place, $icon_path , $size = 50) {
80
	    return '<img class="flag_gm_h'. $size . '" src="' . $icon_path . '" title="' . $place->getGedcomName() . '" alt="' . $place->getGedcomName() . '" />';
81
	}
82
	
83
	/**
84
	 * Returns HTML code to display a tag cloud from a list.
85
	 * List must be a list of arrays (one for each item to display) containing the following parameters:
86
	 * 		- "text" : text to display
87
	 * 		- "count" : count of items
88
	 * 		- "url"	: url to the item
89
	 *
90
	 * @param array $list Array of items to display in the cloud
91
	 * @param bool $totals Display totals for an items
92
	 * @return string Tag Cloud HTML Code
93
	 */
94
	public static function htmlListCloud($list, $totals) {
95
		$minimum = PHP_INT_MAX;
96
		$maximum = 1;
97
		foreach ($list as $params) {
98
			if(array_key_exists('count', $params)) {
99
				$maximum = max($maximum, $params['count']);
100
				$minimum = min($minimum, $params['count']);
101
			}
102
		}
103
		$html = '';
104
		foreach ($list as $params) {
105
			$text = array_key_exists('text', $params) ? $params['text'] : '';
106
			$count = array_key_exists('count', $params) ? $params['count'] : 0;
107
			$url = array_key_exists('url', $params) ? $params['url'] : '';
108
			
109
			if ($maximum === $minimum) {			
110
				// All items occur the same number of times
111
					$size = 150.0;
112
			} else {
113
				$size = 75.0 + 125.0 * ($count - $minimum) / ($maximum - $minimum);
114
			}
115
			
116
			$html .= '<a style="font-size:' . $size . '%" href="' . $url . '">';
117
			if ($totals) {
118
				$html .= I18N::translate('%1$s (%2$s)', '<span dir="auto">' . $text . '</span>', I18N::number($count));
119
			} else {
120
				$html .= $text;
121
			}
122
			$html .= '</a>';
123
		}
124
		return '<div class="tag_cloud">' . $html . '</div>';
125
	}
126
	
127
128
	/**
129
	 * Returns HTML code to include a place cloud
130
	 *
131
	 * @param array $places Array of places to display in the cloud
132
	 * @param bool $totals Display totals for a place
133
	 * @param \Fisharebest\Webtrees\Tree $tree Tree
134
	 * @return string Place Cloud HTML Code
135
	 */
136
	public static function htmlPlacesCloud($places, $totals, Tree $tree) {
137
		$items = array();
138
		
139
		foreach ($places as $place => $count) {
140
			/** var \MyArtJaub\Webtrees\Place */
141
			$dplace = Place::getIntance($place, $tree);
142
			$items[] = array(
143
				'text' => $dplace->htmlFormattedName('%1 (%2)'),
144
				'count' => $count,
145
				'url' => $dplace->getDerivedPlace()->getURL()
146
			);
147
		}
148
		
149
		return self::htmlListCloud($items, $totals);
150
	}
151
152
	/**
153
	 * Return HTML Code to display individual in non structured list (e.g. Patronymic Lineages)
154
	 *
155
	 * @param \Fisharebest\Webtrees\Individual $individual Individual to print
156
	 * @param bool $isStrong Bolden the name ?
157
	 * @return string HTML Code for individual item
158
	 */
159
	public static function htmlIndividualForList(\Fisharebest\Webtrees\Individual $individual, $isStrong = true){
160
		$html = '';
161
		$tag = 'em';
162
		if($isStrong) $tag = 'strong';
163
		if($individual && $individual->canShow()){
164
			$dindi = new Individual($individual);
165
			$html = $individual->getSexImage();
166
			$html .= '<a class="list_item" href="'.
167
			$individual->getHtmlUrl().
168
				'" title="'.
169
			I18N::translate('Informations for individual %s', $individual->getXref()).
170
				'">';
171
			$html .= '<'.$tag.'>'.$individual->getFullName().'</'.$tag.'>&nbsp;('.$individual->getXref().')&nbsp;';
172
			$html .= FunctionsPrint::formatSosaNumbers($dindi->getSosaNumbers(), 1, 'small');
173
			$html .= '&nbsp;<span><small><em>'.$dindi->formatFirstMajorFact(WT_EVENTS_BIRT, 10).'</em></small></span>';
174
			$html .= '&nbsp;<span><small><em>'.$dindi->formatFirstMajorFact(WT_EVENTS_DEAT, 10).'</em></small></span>';
175
			$html .= '</a>';
176
		}
177
		else {
178
			$html .= '<span class=\"list_item\"><'.$tag.'>' . I18N::translate('Private') . '</'.$tag.'></span>';
179
		}
180
		return $html;
181
	}
182
183
	/**
184
	 * Format date to display short (just years)
185
	 *
186
	 * @param \Fisharebest\Webtrees\Fact $eventObj Fact to display date
0 ignored issues
show
Bug introduced by
There is no parameter named $eventObj. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
187
	 * @param boolean $anchor option to print a link to calendar
188
	 * @return string HTML code for short date
189
	 */
190
	public static function formatFactDateShort(\Fisharebest\Webtrees\Fact $fact, $anchor=false) {
191
		global $SEARCH_SPIDER;
192
193
		$html='';
194
		$date = $fact->getDate();
195
		if($date->isOK()){
196
			$html.=' '.$date->Display($anchor && !$SEARCH_SPIDER, '%Y');
197
		}
198
		else{
199
			// 1 DEAT Y with no DATE => print YES
200
			// 1 BIRT 2 SOUR @S1@ => print YES
201
			// 1 DEAT N is not allowed
202
			// It is not proper GEDCOM form to use a N(o) value with an event tag to infer that it did not happen.
203
			$factdetail = explode(' ', trim($fact->getGedcom()));
204
			if (isset($factdetail) && (count($factdetail) == 3 && strtoupper($factdetail[2]) == 'Y') || (count($factdetail) == 4 && $factdetail[2] == 'SOUR')) {
205
				$html.=I18N::translate('yes');
206
			}
207
		}
208
		return $html;
209
	}
210
211
	/**
212
	 * Format fact place to display short
213
	 *
214
	 * @param \Fisharebest\Webtrees\Fact $eventObj Fact to display date
0 ignored issues
show
Bug introduced by
There is no parameter named $eventObj. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
215
	 * @param string $format Format of the place
216
	 * @param boolean $anchor option to print a link to placelist
217
	 * @return string HTML code for short place
218
	 */
219
	public static function formatFactPlaceShort(\Fisharebest\Webtrees\Fact $fact, $format, $anchor=false){
220
		$html='';
221
		
222
		if ($fact === null) return $html;
223
		$place = $fact->getPlace();
224
		if($place){
225
			$dplace = new Place($place);
226
			$html .= $dplace->htmlFormattedName($format, $anchor);
227
		}
228
		return $html;
229
	}
230
231
	/**
232
	 * Format Sosa number to display next to individual details
233
	 * Possible format are:
234
	 * 	- 1 (default) : display an image if the individual is a Sosa, independently of the number of times he is
235
	 * 	- 2 : display a list of Sosa numbers, with an image, separated by an hyphen.
236
	 *
237
	 * @param array $sosatab List of Sosa numbers
238
	 * @param int $format Format to apply to the Sosa numbers
239
	 * @param string $size CSS size for the icon. A CSS style css_$size is required
240
	 * @return string HTML code for the formatted Sosa numbers
241
	 */
242
	public static function formatSosaNumbers(array $sosatab, $format = 1, $size = 'small'){
243
		$html = '';
244
		switch($format){
245
			case 1:
246
				if(count($sosatab)>0){
247
					$html = '<i class="icon-maj-sosa_'.$size.'" title="'.I18N::translate('Sosa').'"></i>';
248
				}
249
				break;
250
			case 2:
251
				if(count($sosatab)>0){
252
					ksort($sosatab);
253
					$tmp_html = array();
254
					foreach ($sosatab as $sosa => $gen) {
255
						$tmp_html[] = sprintf(
256
								'<i class="icon-maj-sosa_%1$s" title="'.I18N::translate('Sosa').'"></i>&nbsp;<strong>%2$d&nbsp;'.I18N::translate('(G%s)', $gen) .'</strong>',
257
								$size,
258
								$sosa
259
							);
260
					}
261
					$html = implode(' - ', $tmp_html);
262
				}
263
				break;
264
			default:
265
				break;
266
		}
267
		return $html;
268
	}
269
270
	/**
271
	 * Format IsSourced icons for display
272
	 * Possible format are:
273
	 * 	- 1 (default) : display an icon depending on the level of sources
274
	 *
275
	 * @param string $sourceType Type of the record : 'E', 'R'
276
	 * @param int $isSourced Level to display
277
	 * @param string $tag Fact to display status
278
	 * @param int $format Format to apply to the IsSourced parameter
279
	 * @param string $size CSS size for the icon. A CSS style css_$size is required
280
	 * @return string HTML code for IsSourced icon
281
	 */
282
	public static function formatIsSourcedIcon($sourceType, $isSourced, $tag='EVEN', $format = 1, $size='normal'){
283
		$html='';
284
		$image=null;
285
		$title=null;
286
		switch($format){
287
			case 1:
288
				switch($sourceType){
289
					case 'E':
290
						switch($isSourced){
291
							case 0:
292
								$image = 'event_unknown';
293
								$title = I18N::translate('%s not found', GedcomTag::getLabel($tag));
294
								break;
295
							case -1:
296
								$image = 'event_notprecise';
297
								$title = I18N::translate('%s not precise', GedcomTag::getLabel($tag));
298
								break;
299
							case -2:
300
								$image = 'event_notsourced';
301
								$title = I18N::translate('%s not sourced', GedcomTag::getLabel($tag));
302
								break;
303
							case 1:
304
								$image = 'event_sourced';
305
								$title = I18N::translate('%s sourced', GedcomTag::getLabel($tag));
306
								break;
307
							case 2:
308
								$image = 'event_sourcedcertif';
309
								$title = I18N::translate('%s sourced with a certificate', GedcomTag::getLabel($tag));
310
								break;
311
							case 3:
312
								$image = 'event_sourcedcertifdate';
313
								$title = I18N::translate('%s sourced with exact certificate', GedcomTag::getLabel($tag));
314
								break;
315
							default:
316
								break;
317
						}
318
						break;
319
					case 'R':
320
						switch($isSourced){
321
							case -1:
322
								$image = 'record_notsourced';
323
								$title = I18N::translate('%s not sourced', GedcomTag::getLabel($tag));
324
								break;
325
							case 1:
326
								$image = 'record_sourced';
327
								$title = I18N::translate('%s sourced', GedcomTag::getLabel($tag));
328
								break;
329
							case 2:
330
								$image = 'record_sourcedcertif';
331
								$title = I18N::translate('%s sourced with a certificate', GedcomTag::getLabel($tag));
332
								break;
333
							default:
334
								break;
335
						}
336
						break;
337
					default:
338
						break;
339
				}
340
				if($image && $title) $html = '<i class="icon-maj-sourced-'.$size.'_'.$image.'" title="'.$title.'"></i>';
0 ignored issues
show
Bug Best Practice introduced by
The expression $image 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...
Bug Best Practice introduced by
The expression $title 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...
341
				break;
342
			default:
343
				break;
344
		}
345
		return $html;
346
	}
347
	
348
	/**
349
	 * Check whether the date is compatible with the Google Chart range (between 1550 and 2030).
350
	 * 
351
	 * @param Date $date
352
	 * @return boolean
353
	 */
354
	public static function isDateWithinChartsRange(Date $date) {
355
	    return $date->gregorianYear() >= 1550 && $date->gregorianYear() < 2030;
356
	}
357
358
}
359