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