Completed
Push — feature/code-analysis ( 0f93c5...1158f1 )
by Jonathan
03:21
created

FunctionsPrint::isDateWithinChartsRange()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 3
rs 10
cc 2
eloc 2
nc 2
nop 1
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
 * @todo snake_case
25
 */
26
class FunctionsPrint {
27
28
	/**
29
	 * Get an array converted to a list. For example
30
	 * array("red", "green", "yellow", "blue") => "red, green, yellow and blue"
31
	 *
32
	 * @param array $array Array to convert
33
	 * @return string List of elements
34
	 */
35
	static public function getListFromArray(array $array) {
36
		$n=count($array);
37
		switch ($n) {
38
			case 0:
39
				return '';
40
			case 1:
41
				return $array[0];
42
			default:
43
				return implode(
44
						/* I18N: list separator */ I18N::translate(', '), 
45
						array_slice($array, 0, $n-1)
46
					) .
47
					/* I18N: last list separator, " and " in English, " et " in French  */ I18N::translate(' and ') . 
48
					$array[$n-1];
49
		}
50
	}
51
52
	/**
53
	 * Return HTML code to include a flag icon in facts description
54
	 *
55
	 * @param \Fisharebest\Webtrees\Fact $fact Fact record
56
	 * @param \MyArtJaub\Webtrees\Map\MapProviderInterface $mapProvider Map Provider
57
	 * @return string HTML code of the inserted flag
58
	 */
59
	public static function htmlFactPlaceIcon(
60
			\Fisharebest\Webtrees\Fact $fact,
61
			\MyArtJaub\Webtrees\Map\MapProviderInterface $mapProvider
62
	) {
63
		$html='';
64
		if($place = $fact->getPlace()) {
65
			$iconPlace= $mapProvider->getPlaceIcon($place);	
66
			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...
67
				$html.=	'<div class="fact_flag">'. self::htmlPlaceIcon($place, $iconPlace, 50). '</div>';
68
			}
69
		}
70
		return $html;
71
	}
72
	
73
	/**
74
	 * Return HTML code to include a flag icon
75
	 * 
76
	 * @param \Fisharebest\Webtrees\Place $place
77
	 * @param string $icon_path
78
	 * @param number $size
79
	 * @return string HTML code of the inserted flag
80
	 */
81
	public static function htmlPlaceIcon(\Fisharebest\Webtrees\Place $place, $icon_path , $size = 50) {
82
	    return '<img class="flag_gm_h'. $size . '" src="' . $icon_path . '" title="' . $place->getGedcomName() . '" alt="' . $place->getGedcomName() . '" />';
83
	}
84
	
85
	/**
86
	 * Returns HTML code to display a tag cloud from a list.
87
	 * List must be a list of arrays (one for each item to display) containing the following parameters:
88
	 * 		- "text" : text to display
89
	 * 		- "count" : count of items
90
	 * 		- "url"	: url to the item
91
	 *
92
	 * @param array $list Array of items to display in the cloud
93
	 * @param bool $totals Display totals for an items
94
	 * @return string Tag Cloud HTML Code
95
	 */
96
	public static function htmlListCloud($list, $totals) {
97
		$minimum = PHP_INT_MAX;
98
		$maximum = 1;
99
		foreach ($list as $item => $params) {
100
			if(array_key_exists('count', $params)) {
101
				$maximum = max($maximum, $params['count']);
102
				$minimum = min($minimum, $params['count']);
103
			}
104
		}
105
		$html = '';
106
		foreach ($list as $item => $params) {
107
			$text = array_key_exists('text', $params) ? $params['text'] : '';
108
			$count = array_key_exists('count', $params) ? $params['count'] : 0;
109
			$url = array_key_exists('url', $params) ? $params['url'] : '';
110
			
111
			if ($maximum === $minimum) {			
112
				// All items occur the same number of times
113
					$size = 150.0;
114
			} else {
115
				$size = 75.0 + 125.0 * ($count - $minimum) / ($maximum - $minimum);
116
			}
117
			
118
			$html .= '<a style="font-size:' . $size . '%" href="' . $url . '">';
119
			if ($totals) {
120
				$html .= I18N::translate('%1$s (%2$s)', '<span dir="auto">' . $text . '</span>', I18N::number($count));
121
			} else {
122
				$html .= $text;
123
			}
124
			$html .= '</a>';
125
		}
126
		return '<div class="tag_cloud">' . $html . '</div>';
127
	}
128
	
129
130
	/**
131
	 * Returns HTML code to include a place cloud
132
	 *
133
	 * @param array $places Array of places to display in the cloud
134
	 * @param bool $totals Display totals for a place
135
	 * @param \Fisharebest\Webtrees\Tree $tree Tree
136
	 * @return string Place Cloud HTML Code
137
	 */
138
	public static function htmlPlacesCloud($places, $totals, Tree $tree) {
139
		$items = array();
140
		
141
		foreach ($places as $place => $count) {
142
			/** var \MyArtJaub\Webtrees\Place */
143
			$dplace = Place::getIntance($place, $tree);
144
			$items[] = array(
145
				'text' => $dplace->htmlFormattedName('%1 (%2)'),
146
				'count' => $count,
147
				'url' => $dplace->getDerivedPlace()->getURL()
148
			);
149
		}
150
		
151
		return self::htmlListCloud($items, $totals);
152
	}
153
154
	/**
155
	 * Return HTML Code to display individual in non structured list (e.g. Patronymic Lineages)
156
	 *
157
	 * @param \Fisharebest\Webtrees\Individual $individual Individual to print
158
	 * @param bool $isStrong Bolden the name ?
159
	 * @return string HTML Code for individual item
160
	 */
161
	public static function htmlIndividualForList(\Fisharebest\Webtrees\Individual $individual, $isStrong = true){
162
		$html = '';
163
		$tag = 'em';
164
		if($isStrong) $tag = 'strong';
165
		if($individual && $individual->canShow()){
166
			$dindi = new Individual($individual);
167
			$html = $individual->getSexImage();
168
			$html .= '<a class="list_item" href="'.
169
			$individual->getHtmlUrl().
170
				'" title="'.
171
			I18N::translate('Informations for individual %s', $individual->getXref()).
172
				'">';
173
			$html .= '<'.$tag.'>'.$individual->getFullName().'</'.$tag.'>&nbsp;('.$individual->getXref().')&nbsp;';
174
			$html .= FunctionsPrint::formatSosaNumbers($dindi->getSosaNumbers(), 1, 'small');
175
			$html .= '&nbsp;<span><small><em>'.$dindi->formatFirstMajorFact(WT_EVENTS_BIRT, 10).'</em></small></span>';
176
			$html .= '&nbsp;<span><small><em>'.$dindi->formatFirstMajorFact(WT_EVENTS_DEAT, 10).'</em></small></span>';
177
			$html .= '</a>';
178
		}
179
		else {
180
			$html .= '<span class=\"list_item\"><'.$tag.'>' . I18N::translate('Private') . '</'.$tag.'></span>';
181
		}
182
		return $html;
183
	}
184
185
	/**
186
	 * Format date to display short (just years)
187
	 *
188
	 * @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...
189
	 * @param boolean $anchor option to print a link to calendar
190
	 * @return string HTML code for short date
191
	 */
192
	public static function formatFactDateShort(\Fisharebest\Webtrees\Fact $fact, $anchor=false) {
193
		global $SEARCH_SPIDER;
194
195
		$html='';
196
		$date = $fact->getDate();
197
		if($date->isOK()){
198
			$html.=' '.$date->Display($anchor && !$SEARCH_SPIDER, '%Y');
199
		}
200
		else{
201
			// 1 DEAT Y with no DATE => print YES
202
			// 1 BIRT 2 SOUR @S1@ => print YES
203
			// 1 DEAT N is not allowed
204
			// It is not proper GEDCOM form to use a N(o) value with an event tag to infer that it did not happen.
205
			$factdetail = explode(' ', trim($fact->getGedcom()));
206
			if (isset($factdetail) && (count($factdetail) == 3 && strtoupper($factdetail[2]) == 'Y') || (count($factdetail) == 4 && $factdetail[2] == 'SOUR')) {
207
				$html.=I18N::translate('yes');
208
			}
209
		}
210
		return $html;
211
	}
212
213
	/**
214
	 * Format fact place to display short
215
	 *
216
	 * @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...
217
	 * @param string $format Format of the place
218
	 * @param boolean $anchor option to print a link to placelist
219
	 * @return string HTML code for short place
220
	 */
221
	public static function formatFactPlaceShort(\Fisharebest\Webtrees\Fact $fact, $format, $anchor=false){
222
		$html='';
223
		
224
		if ($fact === null) return $html;
225
		$place = $fact->getPlace();
226
		if($place){
227
			$dplace = new Place($place);
228
			$html .= $dplace->htmlFormattedName($format, $anchor);
229
		}
230
		return $html;
231
	}
232
233
	/**
234
	 * Format Sosa number to display next to individual details
235
	 * Possible format are:
236
	 * 	- 1 (default) : display an image if the individual is a Sosa, independently of the number of times he is
237
	 * 	- 2 : display a list of Sosa numbers, with an image, separated by an hyphen.
238
	 *
239
	 * @param array $sosatab List of Sosa numbers
240
	 * @param int $format Format to apply to the Sosa numbers
241
	 * @param string $size CSS size for the icon. A CSS style css_$size is required
242
	 * @return string HTML code for the formatted Sosa numbers
243
	 */
244
	public static function formatSosaNumbers(array $sosatab, $format = 1, $size = 'small'){
245
		$html = '';
246
		switch($format){
247
			case 1:
248
				if(count($sosatab)>0){
249
					$html = '<i class="icon-maj-sosa_'.$size.'" title="'.I18N::translate('Sosa').'"></i>';
250
				}
251
				break;
252
			case 2:
253
				if(count($sosatab)>0){
254
					ksort($sosatab);
255
					$tmp_html = array();
256
					foreach ($sosatab as $sosa => $gen) {
257
						$tmp_html[] = sprintf(
258
								'<i class="icon-maj-sosa_%1$s" title="'.I18N::translate('Sosa').'"></i>&nbsp;<strong>%2$d&nbsp;'.I18N::translate('(G%s)', $gen) .'</strong>',
259
								$size,
260
								$sosa
261
							);
262
					}
263
					$html = implode(' - ', $tmp_html);
264
				}
265
				break;
266
			default:
267
				break;
268
		}
269
		return $html;
270
	}
271
272
	/**
273
	 * Format IsSourced icons for display
274
	 * Possible format are:
275
	 * 	- 1 (default) : display an icon depending on the level of sources
276
	 *
277
	 * @param string $sourceType Type of the record : 'E', 'R'
278
	 * @param int $isSourced Level to display
279
	 * @param string $tag Fact to display status
280
	 * @param int $format Format to apply to the IsSourced parameter
281
	 * @param string $size CSS size for the icon. A CSS style css_$size is required
282
	 * @return string HTML code for IsSourced icon
283
	 */
284
	public static function formatIsSourcedIcon($sourceType, $isSourced, $tag='EVEN', $format = 1, $size='normal'){
285
		$html='';
286
		$image=null;
287
		$title=null;
288
		switch($format){
289
			case 1:
290
				switch($sourceType){
291
					case 'E':
292
						switch($isSourced){
293
							case 0:
294
								$image = 'event_unknown';
295
								$title = I18N::translate('%s not found', GedcomTag::getLabel($tag));
296
								break;
297
							case -1:
298
								$image = 'event_notprecise';
299
								$title = I18N::translate('%s not precise', GedcomTag::getLabel($tag));
300
								break;
301
							case -2:
302
								$image = 'event_notsourced';
303
								$title = I18N::translate('%s not sourced', GedcomTag::getLabel($tag));
304
								break;
305
							case 1:
306
								$image = 'event_sourced';
307
								$title = I18N::translate('%s sourced', GedcomTag::getLabel($tag));
308
								break;
309
							case 2:
310
								$image = 'event_sourcedcertif';
311
								$title = I18N::translate('%s sourced with a certificate', GedcomTag::getLabel($tag));
312
								break;
313
							case 3:
314
								$image = 'event_sourcedcertifdate';
315
								$title = I18N::translate('%s sourced with exact certificate', GedcomTag::getLabel($tag));
316
								break;
317
							default:
318
								break;
319
						}
320
						break;
321
					case 'R':
322
						switch($isSourced){
323
							case -1:
324
								$image = 'record_notsourced';
325
								$title = I18N::translate('%s not sourced', GedcomTag::getLabel($tag));
326
								break;
327
							case 1:
328
								$image = 'record_sourced';
329
								$title = I18N::translate('%s sourced', GedcomTag::getLabel($tag));
330
								break;
331
							case 2:
332
								$image = 'record_sourcedcertif';
333
								$title = I18N::translate('%s sourced with a certificate', GedcomTag::getLabel($tag));
334
								break;
335
							default:
336
								break;
337
						}
338
						break;
339
						break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
340
					default:
341
						break;
342
				}
343
				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...
344
				break;
345
			default:
346
				break;
347
		}
348
		return $html;
349
	}
350
	
351
	/**
352
	 * Check whether the date is compatible with the Google Chart range (between 1550 and 2030).
353
	 * 
354
	 * @param Date $date
355
	 * @return boolean
356
	 */
357
	public static function isDateWithinChartsRange(Date $date) {
358
	    return $date->gregorianYear() >= 1550 && $date->gregorianYear() < 2030;
359
	}
360
361
}
362