Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 57 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
CargoTagCloudFormat | |
0.00% |
0 / 57 |
|
0.00% |
0 / 2 |
210 | |
0.00% |
0 / 1 |
allowedParameters | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
display | |
0.00% |
0 / 52 |
|
0.00% |
0 / 1 |
182 |
1 | <?php |
2 | /** |
3 | * @author Cindy Cicalese |
4 | * @ingroup Cargo |
5 | */ |
6 | |
7 | class CargoTagCloudFormat extends CargoDisplayFormat { |
8 | |
9 | public static function allowedParameters() { |
10 | return [ |
11 | 'template' => [ 'type' => 'string' ], |
12 | 'min size' => [ 'type' => 'int' ], |
13 | 'max size' => [ 'type' => 'int' ] |
14 | ]; |
15 | } |
16 | |
17 | /** |
18 | * @param array $valuesTagCloud Unused |
19 | * @param array $formattedValuesTagCloud |
20 | * @param array $fieldDescriptions |
21 | * @param array $displayParams |
22 | * @return string HTML |
23 | */ |
24 | public function display( $valuesTagCloud, $formattedValuesTagCloud, $fieldDescriptions, $displayParams ) { |
25 | $this->mOutput->addModuleStyles( [ 'ext.cargo.main' ] ); |
26 | |
27 | if ( count( $fieldDescriptions ) < 2 ) { |
28 | return ''; |
29 | } |
30 | |
31 | $fieldNames = array_keys( $fieldDescriptions ); |
32 | $tagFieldName = $fieldNames[0]; |
33 | $countFieldName = $fieldNames[1]; |
34 | |
35 | if ( $fieldDescriptions[$countFieldName]->mType != 'Integer' ) { |
36 | return ''; |
37 | } |
38 | |
39 | $tags = []; |
40 | |
41 | foreach ( $formattedValuesTagCloud as $row ) { |
42 | |
43 | $tag = $row[$tagFieldName]; |
44 | $count = $row[$countFieldName]; |
45 | |
46 | if ( strlen( $tag ) > 0 && is_numeric( $count ) && $count > 0 ) { |
47 | |
48 | $tags[$tag] = $count; |
49 | |
50 | } |
51 | |
52 | } |
53 | |
54 | if ( $tags == [] ) { |
55 | return ''; |
56 | } |
57 | |
58 | $maxSize = $displayParams['max size'] ?? 200; |
59 | $minSize = $displayParams['min size'] ?? 80; |
60 | |
61 | $maxSizeIncrease = $maxSize - $minSize; |
62 | |
63 | $minCount = min( $tags ); |
64 | $maxCount = max( $tags ); |
65 | |
66 | if ( $maxCount == $minCount ) { |
67 | $size = $minSize + $maxSizeIncrease / 2; |
68 | } else { |
69 | $denominator = log( $maxCount ) - log( $minCount ); |
70 | $sizes = []; |
71 | } |
72 | |
73 | $attrs = [ |
74 | 'class' => 'cargoTagCloud', |
75 | 'align' => 'justify' |
76 | ]; |
77 | |
78 | $text = Html::openElement( 'div', $attrs ); |
79 | |
80 | foreach ( $tags as $tag => $count ) { |
81 | if ( isset( $displayParams['template'] ) ) { |
82 | $tagstring = '{{' . $displayParams['template'] . |
83 | '|' . $tag . '|' . $count . '}}'; |
84 | $tagstring = CargoUtils::smartParse( $tagstring, null ); |
85 | } else { |
86 | $tagstring = $tag; |
87 | } |
88 | if ( $maxCount != $minCount ) { |
89 | $countstr = strval( $count ); |
90 | if ( !isset( $sizes[$countstr] ) ) { |
91 | $sizes[$countstr] = |
92 | $minSize + $maxSizeIncrease * |
93 | ( log( $count ) - log( $minCount ) ) / |
94 | $denominator; |
95 | } |
96 | $size = $sizes[$countstr]; |
97 | } |
98 | $text .= Html::rawElement( 'span', |
99 | [ |
100 | 'style' => 'font-size:' . $size . '%;white-space:nowrap;' |
101 | ], |
102 | $tagstring ); |
103 | $text .= ' '; |
104 | } |
105 | |
106 | $text .= Html::closeElement( 'div' ); |
107 | |
108 | return $text; |
109 | } |
110 | |
111 | } |