<?php
/**
 * file semmelstatzR_history.php
 *
 * @package     SemmelstatzR
 * @version     $Rev$
 * @since       1.0.0 Beta
 * @author      SEM-Team
 * @copyright   (c)2011 SEM-Team
 * @link        http://sourceforge.net/projects/semmelstatz/
 * @license     http://www.gnu.org/licenses/gpl-3.0.html
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.  
 *
 * $Id$
 * 
 */
/**  
 * based upon semmelstatz
 * Copyright (c) 2005-2009 Andreas 'Redunzl' Mueller (redunzl@gmx.de)
 *    
 * @license http://www.gnu.org/licenses/gpl-2.0.html
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License (version 2) as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
 
 if(!current_user_can('manage_statz')) {
	die('Zugriff verweigert');
}

include_once plugin_dir_path(__FILE__).'includes/pChart/class/pData.class.php';
include_once plugin_dir_path(__FILE__).'includes/pChart/class/pDraw.class.php';
include_once plugin_dir_path(__FILE__).'includes/pChart/class/pImage.class.php';

function sem_drawLast30DaysGoo() {
    global $wpdb;

    //$sql = $wpdb->get_results("ALTER TABLE ".$wpdb->statzhist." ORDER BY date ASC");
    $results = $wpdb->get_results("SELECT DAY(date) as days, visitors FROM ".$wpdb->statzhist." WHERE date >= NOW() - INTERVAL 31 DAY ORDER BY date ASC");

    print "<div class='statz'>Besucher: Die letzten 30 Tage</div>";
    print "<div align='left'>";

    if (empty($results)) {
    print "<small>... noch keine Eintr&auml;ge</small></div>";
    return;
    }

    foreach ($results as $result) {
    $visitors[] = $result->visitors;
    $days[] = $result->days;
    }

    $maxvalue = max($visitors);
    $daylabels = implode('|', $days);
    
    $MyData = new pData();  
    $MyData->addPoints($visitors,"Visitors");
    $MyData->setAxisName(0,"Hits");
    $MyData->addPoints($days,"Days");
    $MyData->setAbscissa("Days");
   
    $myPicture = new pImage(780,150,$MyData);    
    $myPicture->Antialias = FALSE;    
    $myPicture->drawRectangle(0,0,779,149,array("R"=>0,"G"=>0,"B"=>0));   
    $myPicture->setFontProperties(array("FontName"=>plugin_dir_path(__FILE__)."includes/pChart/fonts/Bedizen.ttf","FontSize"=>8));    
    $myPicture->setGraphArea(30,20,760,130);
    $scaleSettings = array("GridR"=>200,"GridG"=>200,"GridB"=>200);
    $myPicture->drawScale($scaleSettings);
    $myPicture->drawBarChart();
    @$myPicture->Render(WP_CONTENT_DIR."/cache/Last30DaysGoo.png"); 

    print "<div class='chart'>";

    //print "<img src=http://chart.apis.google.com/chart?chg=25,25&chxl=0:|".$daylabels."|1:|0|".$maxvalue."&chbh=a&cht=bvs&chs=600x150&chxt=x,y&chd=".sem_encodeChartData($visitors).">";
    if(is_readable(WP_CONTENT_DIR."/cache/Last30DaysGoo.png")) {
        print '<img src="'.WP_CONTENT_URL.'/cache/Last30DaysGoo.png" />';
    } else if(!is_dir(WP_CONTENT_DIR."/cache")) {
        print semr_i18n('No Chart: Cannot find cache-folder!');
    } else if(!is_writable(WP_CONTENT_DIR."/cache")) {
        print semr_i18n('No Chart: Missing rights for cache-folder!');
    }
    print "</div>";

    print "</div>";
    print "<hr />";
	}
	
### HISTORY Monatsgrafik GOOGLE-API

	function sem_drawAllMonthGoo() {
		global $wpdb;
        
		$nummonths = $wpdb->get_var("SELECT MONTH(date) FROM ".$wpdb->statzhist);
		$results = $wpdb->get_results("SELECT MONTH(date) as months, SUM(visitors) as visitors FROM ".$wpdb->statzhist." GROUP BY months ORDER BY date ASC");
		
		print "<div class='statz'>Besucher: Die letzten ".$nummonths." Monate</div>";
        print "<div align='left'>";
		
			if (empty($results)) {
				print "<small>... noch keine Eintr&auml;ge</small></div>";
				return;
			}
			
			foreach ($results as $result) {
				$visitors[] = $result->visitors;
				$months[] = $result->months;
			}
			
			$maxvalue = max($visitors);			
			$monthlabels = implode('|', $months);
   
   $MyData = new pData();  
    $MyData->addPoints($visitors,"Visitors");
    $MyData->setAxisName(0,"Hits");
    $MyData->addPoints($months,"Month");
    $MyData->setAbscissa("Month");
   
    $myPicture = new pImage(780,150,$MyData);    
    $myPicture->Antialias = FALSE;    
    $myPicture->drawRectangle(0,0,779,149,array("R"=>0,"G"=>0,"B"=>0));   
    $myPicture->setFontProperties(array("FontName"=>plugin_dir_path(__FILE__)."includes/pChart/fonts/Bedizen.ttf","FontSize"=>8));    
    $myPicture->setGraphArea(30,20,760,130);
    $scaleSettings = array("GridR"=>200,"GridG"=>200,"GridB"=>200);
    $myPicture->drawScale($scaleSettings);
    $myPicture->drawBarChart();
    @$myPicture->Render(WP_CONTENT_DIR."/cache/AllMonthGoo.png"); 
			
			print "<div class='chart'>";
		
				//print "<img src=http://chart.apis.google.com/chart?chg=25,25&chxt=x,y&chxl=0:|".$monthlabels."|1:|0|".$maxvalue."&chbh=a&cht=bvs&chs=600x150&chd=".sem_encodeChartData($visitors).">";
			if(is_readable(WP_CONTENT_DIR."/cache/AllMonthGoo.png")) {
        print '<img src="'.WP_CONTENT_URL.'/cache/AllMonthGoo.png" />';
    } else if(!is_dir(WP_CONTENT_DIR."/cache")) {
        print semr_i18n('No Chart: Cannot find cache-folder!');
    } else if(!is_writable(WP_CONTENT_DIR."/cache")) {
        print semr_i18n('No Chart: Missing rights for cache-folder!');
    }
			print "</div>";
			
		print "</div>";
		print "<hr />";
	}
	
### HISTORY ALLE historischen Tage

function sem_drawAllDaysGoo() {
    /* @var $wpdb wpdb */
    global $wpdb;

    $numdays = $wpdb->get_var("SELECT COUNT(date) FROM ".$wpdb->statzhist);

    $results = $wpdb->get_results("SELECT referers, visitors, hits, substring(date,1,10) AS date
    FROM ".$wpdb->statzhist." GROUP BY date ORDER BY date ASC");
    

    print "<div class='statz'>Besucher: ".$numdays." Tage zur&uuml;ck</div>";
    print "<div align='left'>";

    if (empty($results)) {
    print "<small>... noch keine Eintr&auml;ge</small></div>";
    return;
    }

    foreach($results as $result) {
    $visitors[] = $result->visitors;
    $hits[] = $result->hits;
    $referers[] = $result->referers;
    }

    $maxvalue = max($visitors);
    
    $MyData = new pData();  
    $MyData->addPoints($visitors,"Visitors");
    $MyData->setAxisName(0,"Hits");
    $MyData->addPoints($hits,"Hits");
    $MyData->setAbscissa("Hits");
   
    $myPicture = new pImage(780,150,$MyData);    
    $myPicture->Antialias = FALSE;    
    $myPicture->drawRectangle(0,0,779,149,array("R"=>0,"G"=>0,"B"=>0));   
    $myPicture->setFontProperties(array("FontName"=>plugin_dir_path(__FILE__)."includes/pChart/fonts/Bedizen.ttf","FontSize"=>8));    
    $myPicture->setGraphArea(30,20,760,130);
    $scaleSettings = array("GridR"=>200,"GridG"=>200,"GridB"=>200);
    $myPicture->drawScale($scaleSettings);
    $myPicture->drawBarChart();
    @$myPicture->Render(WP_CONTENT_DIR."/cache/AllDaysGoo.png"); 
    
    print "<div class='chart'>";
    //print "<img src=http://chart.apis.google.com/chart?chg=25,25&cht=lc&chs=600x200&chd=".sem_encodeChartData($visitors)."&chxt=y&chxl=0:|0|".$maxvalue.">";
    if(is_readable(WP_CONTENT_DIR."/cache/AllDaysGoo.png")) {
        print '<img src="'.WP_CONTENT_URL.'/cache/AllDaysGoo.png" />';
    } else if(!is_dir(WP_CONTENT_DIR."/cache")) {
        print semr_i18n('No Chart: Cannot find cache-folder!');
    } else if(!is_writable(WP_CONTENT_DIR."/cache")) {
        print semr_i18n('No Chart: Missing rights for cache-folder!');
    }
    print "</div>";

    print "</div>";
    print "<hr />";
}
	
	function sem_showAllDaysInNumbers() {
		global $wpdb;
		
        $numdays = $wpdb->get_var("SELECT COUNT(date) as num FROM $wpdb->statzhist");
		
        $results = $wpdb->get_results("SELECT referers, visitors, hits, substring(date,1,10) AS date
		FROM ".$wpdb->statzhist." GROUP BY date ORDER BY date DESC");
        
        print "<div class='statz'>Die letzten ".$numdays." Tage</div>";
        if (empty($results)) {
            print "<small>... noch keine Eintr&auml;ge</small>";
            return;
        }
        print "<table class='statzelement'>";
        print "<thead>";
        print "<tr>";
        print "<th style='width:15%;text-align:center;'>Datum</th>";
        print "<th style='width:10%;text-align:right;'>Besucher</th>";
        print "<th style='width:10%;text-align:right;'>Hits</th>";
        print "<th style='width:10%;text-align:right;'>Referer</th>";
		print "<th style='text-align:right;'>&nbsp;</th>";
        print "</tr>";
        print "</thead>";
        print "<tbody>";
        $sum_visitors = 0;
        $sum_hits = 0;
        $sum_referers = 0;
            foreach($results as $key=>$result) {
                $date = mysql2date('d.m.Y',$result->date);
                $visitors = $result->visitors; 
                $sum_visitors += $visitors;
                $hits = $result->hits; 
                $sum_hits += $hits;
                $referers = $result->referers; 
                $sum_referers += $referers;
                print "<tr"; if(1&$key) print " class='alternate'"; print ">";
                print "<td style='text-align:center;'>".$date."</td>";
                print "<td style='text-align:right;'>".$visitors."</td>";
                print "<td style='text-align:right;'>".$hits."</td>";
                print "<td style='text-align:right;'>".$referers."</td>";
				print "<td style='text-align:right;'>&nbsp;</td>";
                print "</tr>";
            }
			
        print "<tr><td>&nbsp;</td></tr>";
        print "<tr class='alternate'"; print ">";
        print "<td style='text-align:center;'><b>Gesamt</b></td>";
        print "<td style='text-align:right;'><b>".$sum_visitors."</b></td>";
        print "<td style='text-align:right;'><b>".$sum_hits."</b></td>";
        print "<td style='text-align:right;'><b>".$sum_referers."</b></td>";
        print "</tr>";
        print "<tr>";
        print "<td style='text-align:center;'><b>Durchschnitt</b></td>";
        print "<td style='text-align:right;'>".ceil($sum_visitors / $numdays)."</td>";
        print "<td style='text-align:right;'>".ceil($sum_hits / $numdays)."</td>";
        print "<td style='text-align:right;'>".ceil($sum_referers / $numdays)."</td>";
        print "</tr>";
        print "</tbody>";
        print "</table>";
		print "<hr />";
	}

	
### ANZEIGE HISTORY
	
	print "<div class='wrap'>";
		print "<div id='icon-semmelstatzR' class='icon32'><br /></div>";
		print "<h2>SemmelstatzR &rsaquo; Historie</h2>";
		
		sem_drawLast30DaysGoo();
		sem_drawAllMonthGoo();
		sem_drawAllDaysGoo();
		sem_showAllDaysInNumbers();
		sem_showCopyright();
	
	print "</div>";
		
?>