]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-infos.php
Big cosmetic commit
[Github/YOURLS.git] / includes / functions-infos.php
1 <?php
2
3 // Echoes an image tag of Google Charts map from sorted array of 'country_code' => 'number of visits' (sort by DESC)
4 function yourls_stats_countries_map( $countries ) {
5         $map = array(
6                 'cht' => 't',
7                 'chs' => '440x220',
8                 'chtm'=> 'world',
9                 'chco'=> 'FFFFFF,9090AA,202040',
10                 'chld'=> join('' , array_keys( $countries ) ),
11                 'chd' => 't:'. join(',' ,  $countries ),
12                 'chf' => 'bg,s,EAF7FE'
13         );
14         $map_src = 'http://chart.apis.google.com/chart?' . http_build_query( $map );
15         echo "<img src='$map_src' witdh='440' height='220' border='0' />";
16 }
17
18 // Echoes an image tag of Google Charts pie from sorted array of 'data' => 'value' (sort by DESC). Optional $limit = (integer) limit list of X first countries, sorted by most visits
19 function yourls_stats_pie( $data, $limit = 10, $size = '340x220', $colors = '202040,9090AA' ) {
20         // Trim array: $limit first item + the sum of all others
21         if ( count( $data ) > $limit ) {
22                 $i= 0;
23                 $trim_data = array('Others' => 0);
24                 foreach( $data as $item=>$value ) {
25                         $i++;
26                         if( $i <= $limit ) {
27                                 $trim_data[$item] = $value;
28                         } else {
29                                 $trim_data['Others'] += $value;
30                         }
31                 }
32                 $data = $trim_data;
33         }
34         
35         // Scale items (biggest = 100)
36         $max = max( $data );
37         foreach( $data as $k=>$v ) {
38                 $data[$k] = intval( $v / $max * 100 );
39         }
40         
41         // Hmmm, pie
42         $pie = array(
43                 'cht' => 'p',
44                 'chs' => $size,
45                 'chd' => 't:'.( join(',' ,  $data ) ),
46                 'chco'=> $colors,
47                 'chl' => join('|' , array_keys( $data ) )
48         );
49         $pie_src = 'http://chart.apis.google.com/chart?' . http_build_query( $pie );
50         echo "<img src='$pie_src' witdh='440' height='220' border='0' />";
51 }
52
53 // Echoes an image tag of Google Charts bar graph from list of chronologically sorted array of [year][month][day] => 'number of clicks'
54 function yourls_stats_clicks_line( $dates ) {
55         /* Say we have an array like:
56         $dates = array (
57                 2009 => array (
58                         '08' => array (
59                                 29 => 15,
60                                 30 => 5,
61                                 ),
62                     '09' => array (
63                                 '02' => 3,
64                                 '03' => 5,
65                                 '04' => 2,
66                                 '05' => 99,
67                                 ),
68                         ),
69                 )
70         */
71
72         // Get first & last years from our range. In our example: 2009 & 2009
73         $first_year = key( $dates );
74         $last_year  = end( array_keys($dates) );
75         reset( $dates );
76
77         // Get first & last months from our range. In our example: 08 & 09
78         $first_month = key( $dates[$first_year] );
79         $last_month  = end( array_keys($dates[$last_year]) );
80         reset( $dates );
81         
82         // Get first & last days from our range. In our example: 29 & 05
83         $first_day = key( $dates[$first_year][$first_month] );
84         $last_day  = end( array_keys($dates[$last_year][$last_month]) );
85
86         // Now build a list of all years (2009), month (08 & 09) and days (all from 2009-08-29 to 2009-09-05)
87         $list_of_years = array();
88         $list_of_months = array();
89         $list_of_days = array();
90         for ( $year = $first_year; $year <= $last_year; $year++ ) {
91                 $_year = sprintf('%04d', $year);
92                 $list_of_years[$_year] = $_year;
93                 $current_first_month = ( $year == $first_year ? $first_month : '01' );
94                 $current_last_month  = ( $year == $last_year ? $last_month : '12' );
95                 for ( $month = $current_first_month; $month <= $current_last_month; $month++ ) {
96                         $_month = sprintf('%02d', $month);
97                         $list_of_months[$_month] = $_month;
98                         $current_first_day = ( $year == $first_year && $month == $first_month ? $first_day : '01' );
99                         $current_last_day  = ( $year == $last_year && $month == $last_month ? $last_day : yourls_days_in_month($month, $year) );
100                         for ( $day = $current_first_day; $day <= $current_last_day; $day++ ) {
101                                 $day = sprintf('%02d', $day);
102                                 $list_of_days[] = isset( $dates[$_year][$_month][$day] ) ? $dates[$_year][$_month][$day] : 0;
103                         }
104                 }
105         }
106
107         // Make the chart
108         $label_years = $first_year != $last_year ? join('|', $list_of_years ) : $first_year.'|'.$last_year;
109         $label_months = count( $list_of_months ) > 1 ? join('|', $list_of_months) : $first_month.'|'.$first_year;
110         $max = max( $list_of_days );
111         $label_clicks = '0|'.intval( $max / 4 ).'|'.intval( $max / 2 ).'|'.intval( $max / 1.5 ).'|'.$max;
112         $line = array(
113                 'cht' => 'lc',
114                 'chs' => '1000x300',
115                 'chs' => '440x220',
116                 'chxt'=> 'x,x,y',
117                 'chd' => 't:'.( join(',' ,  $list_of_days ) ),
118                 'chxl'=> '0:|'. $label_years .'|1:|'. $label_months .'|2:|'. $label_clicks
119         );
120         $line_src = 'http://chart.apis.google.com/chart?' . http_build_query( $line );
121         echo "<img src='$line_src' />";
122 }
123
124 // Return the number of days in a month. From php.net, used if PHP built without calendar functions
125 function yourls_days_in_month($month, $year) {
126         // calculate number of days in a month
127         return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
128 }
129
130 // Get max value from date array of [year][month][day] = 'hits'
131 function yourls_stats_get_best_day( $dates ) {
132         $max = 0; $day = 0;
133         foreach( $dates as $year=>$months ) {
134                 foreach( $months as $month=>$days ) {
135                         foreach( $days as $day=>$visits ) {
136                                 if( $visits > $max ) {
137                                         $max = intval($visits);
138                                         $bday = "$year-$month-$day";
139                                 }
140                         }
141                 }
142         }
143
144         return array( 'day' => $bday, 'max' => $max );
145
146 }
147