<?php
/**
 * File: functions.semr_template.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
*/
### Zeigt die letzten $num_posts-Posts an: Titel und absolutes Datum - CSS-id: #lastposts
    function sem_showLastPosts($num_posts = 10) {
        global $wpdb;
        $lastposts = $wpdb->get_results("SELECT ID, post_title FROM ".$wpdb->posts." WHERE post_status = 'publish'
		AND post_type = 'post' ORDER BY post_date DESC LIMIT 0, ".$num_posts);
        print "<div id='lastposts'>";
        if (empty($lastposts)) {
            print "... noch keine Beitr&auml;ge";
        }
        else {
            print "<b>Die letzten ".$num_posts." Beitr&auml;ge</b><br />";
            foreach ($lastposts as $lastpost) {
                $posttitle = $lastpost->post_title;
                $permalink = get_permalink($lastpost->ID);
                $postdate = $lastpost->post_date;
                print "<a href='".$permalink."'>".$posttitle."</a><br />\n";
            }
        }
        print "</div>";
    }

### Zeigt die letzten $num_comments-Kommentare an: Autor und Post mit relativem Datum - CSS-id: #lastcomments
    function sem_showLastComments($num_comments = 10) {
        global $wpdb;
        $comments = $wpdb->get_results("SELECT comment_author, comment_ID, comment_post_ID, comment_date
        FROM ".$wpdb->comments." WHERE comment_approved = '1' ORDER BY comment_date DESC LIMIT 0, $num_comments");
        print "<div id='lastcomments'>";
        if (empty($comments)) {
            print "Keine Kommentare";
        }
        else {
            print "<b>Die letzten ".$num_comments." Kommentare</b><br />";
            foreach ($comments as $comment) {
                $commentday = strtotime(substr($comment->comment_date,1,10));
                $today = time();
                $difference = ceil(($today - $commentday) / (60*60*24))-1;
                if($difference == 0) {
                    $commentdate = "Heute&nbsp;um&nbsp;" . mysql2date("G:i", $comment->comment_date);
                }
                elseif($difference == 1) {
                    $commentdate = "Gestern&nbsp;um&nbsp;" . mysql2date("G:i", $comment->comment_date);
                }
                elseif($difference == 2) {
                    $commentdate = "Vorgestern&nbsp;um&nbsp;" . mysql2date("G:i", $comment->comment_date);
                }
                else {
                    $commentdate = mysql2date("j. F G:i", $comment->comment_date);
                }

                print "<a href='" . get_permalink($comment->comment_post_ID) . "#comment-$comment->comment_ID'>$comment->comment_author@". get_the_title($comment->comment_post_ID) .", $commentdate</a><br />\n";
            }
        }
        print "</div>";
    }

### Zeigt die $num_reads meist gelesenen Posts an - CSS-id: #mostreads
    function sem_showMostReads($num_reads = 10) {
        global $wpdb;
        $mostreads = $wpdb->get_results("SELECT post_title AS posttitle, guid AS postid, hits AS hits FROM ".$wpdb->posts." WHERE hits != '0' GROUP BY postid ORDER BY hits DESC LIMIT 0, $num_reads");
        print "<div id='mostreads'>";
        if (empty($mostreads)) {
            print "... noch keine Eintr&auml;ge";
        }
        else {
            print "<b>Die ".$num_reads." meist gelesenen Beitr&auml;ge</b><br />";
            foreach ($mostreads as $mostread) {
                $posttitle = $mostread->posttitle;
                $postid = $mostread->postid;
                //$permalink = get_permalink($mostread->postid);
                $hits = $mostread->hits;
                print "<a target='_blank' href='".$postid."'>".$posttitle." (".$hits.")</a><br />\n";
            }
        }
        print "</div>";
    }

### Zeigt die $num_reads meist gelesenen Posts an - CSS-id: #mostreads - ACHTUNG: Das Ergebnis der Funktion basiert auf der statz-Tabelle
    function sem_showMostReadsOld($num_reads = 10) {
        global $wpdb;
        $mostreads = $wpdb->get_results("SELECT post_title AS posttitle, ".$wpdb->posts.".guid AS postid,
		COUNT(".$wpdb->statz.".page) AS count FROM ".$wpdb->posts.", ".$wpdb->statz." WHERE
		".$wpdb->statz.".page = ".$wpdb->posts.".ID GROUP BY postid ORDER BY count DESC LIMIT 0, $num_reads");
        print "<div id='mostreads'>";
        if (empty($mostreads)) {
            print "... noch keine Eintr&auml;ge";
        }
        else {
            print "<b>Die ".$num_reads." meist gelesenen Beitr&auml;ge</b><br />";
            foreach ($mostreads as $mostread) {
                $posttitle = $mostread->posttitle;
                $postid = $mostread->postid;
                //$permalink = get_permalink($mostread->postid);
                $count = $mostread->count;
                print "<a target='_blank' href='".$postid."'>".$posttitle." (".$count.")</a><br />\n";
            }
        }
        print "</div>";
    }

### Zeigt die $num_commented meist kommentierten Posts an - CSS-id: #mostcommented
    function sem_showMostCommented($num_commented = 10) {
        global $wpdb;
        $mostcommented = $wpdb->get_results("SELECT guid, id, post_title, COUNT(*) AS count FROM ".$wpdb->comments.",". $wpdb->posts." WHERE
        comment_approved = '1' AND comment_post_id = id GROUP BY id, post_title ORDER BY count DESC LIMIT 0, $num_commented");

        print "<div id='mostcommented'>";
        if (empty($mostcommented)) {
            print "... noch keine Eintr&auml;ge";
        }
        else {
            print "<b>Die ".$num_commented." meist kommentierten Beitr&auml;ge</b><br />";

            foreach ($mostcommented as $key => $mostcommented) {
                $posttitle = $mostcommented->post_title;
                $count = $mostcommented->count;
                $postid = $mostcommented->guid;
                print "<a target='_blank' href='".$postid."'>".$posttitle." (".$count.")</a><br />\n";
            }
        }
        print "</div>";
    }

### Zeigt einen Z�hler der Hits im jeweiligen Post. ACHTUNG: FUNKTIONIERT NUR IM LOOP (index.php, single.php)
	function sem_showPostHitsInLoop($postid) {
		global $wpdb;
		$hits = $wpdb->get_var("SELECT hits FROM $wpdb->posts WHERE ID = $postid LIMIT 0,1");
		return $hits;
	}

### Anzahl aller Posts/Pages
    function sem_showTotalPosts() {
        global $wpdb;
        $totalposts = $wpdb->get_var("SELECT COUNT(ID) AS num FROM ".$wpdb->posts." WHERE post_status = 'publish' LIMIT 0,1");
        return $totalposts; // Ganzzahl
    }

### Anzahl aller Kommentare
    function sem_showTotalComments() {
        global $wpdb;
        $totalcomments = $wpdb->get_var("SELECT COUNT(comment_ID) AS num FROM ".$wpdb->comments." WHERE comment_approved = '1' LIMIT 0,1");
        return $totalcomments; // Ganzzahl
    }

### Anzahl aller User seit Beginn der Statzaufzeichnung
    function sem_showTotalUsers() {
        global $wpdb;
        $totalusers = $wpdb->get_var("SELECT SUM(visitors) FROM ".$wpdb->statzhist." LIMIT 0,1");
        if(!$totalusers) $totalusers = 0;
        return $totalusers; // Ganzzahl
    }

### Anzahl User pro Tag im Durchschnitt
    function sem_showAverageUserPerDay() {
        $averageuser = ceil(sem_showTotalUsers() / sem_showStatzDays());
        return $averageuser;
    }

### Datum des 1. Posts = Blogstart
    function sem_showFirstPostDate() {
        global $wpdb;
        $firstpostdate = $wpdb->get_var("SELECT MIN(post_date) FROM ".$wpdb->posts." WHERE post_status = 'publish' AND post_date != '0000-00-00 00:00:00' LIMIT 0,1");
        $firstpostdate = mysql2date("j.n.Y", $firstpostdate);
        return $firstpostdate; // String T.M.JJJJ
    }

### Datum des 1. Kommentars
    function sem_showFirstCommentDate() {
        global $wpdb;
        $firstcommentdate = $wpdb->get_var("SELECT MIN(comment_date) FROM ".$wpdb->comments." WHERE comment_approved = '1'
        AND comment_date != '0000-00-00 00:00:00' LIMIT 0,1");
        $firstcommentdate = mysql2date("j.n.Y", $firstcommentdate);
        return $firstcommentdate; // String T.M.JJJJ
    }

### Anzahl der Kommentierer nach Nicknamen
    function sem_showNumCommenters() {
        global $wpdb;
        $totalcommenters = $wpdb->get_var("SELECT COUNT(DISTINCT comment_author) FROM ".$wpdb->comments." WHERE comment_approved = '1' LIMIT 0,1");
        if(!$totalcommenters) $totalcommenters = 0;
        return $totalcommenters; // Ganzzahl
    }

### Blog besteht seit x Tagen
    function sem_showPostDays() {
        global $wpdb;
        $firstdate = $wpdb->get_var("SELECT MIN(post_date) AS date FROM ".$wpdb->posts." WHERE post_status = 'publish' AND post_date != '0000-00-00 00:00' LIMIT 0,1");
        $firstdate = strtotime($firstdate); $now = time();
        $blogdays = (int) (($now - $firstdate) / (60*60*24));
        $blogdays = $blogdays + 1;
        return $blogdays; // Ganzzahl
    }

### Tage seit 1. Kommentar
    function sem_showCommentDays() {
        global $wpdb;
        $firstdate = $wpdb->get_var("SELECT MIN(comment_date) AS date FROM ".$wpdb->comments." LIMIT 0,1");
        $firstdate = strtotime($firstdate); $now = time();
        $commentdays = (int) (($now - $firstdate) / (60*60*24));
        $commentdays = $commentdays + 1;
        return $commentdays; // Ganzzahl
    }

### durchschn. Posts pro Tag
    function sem_showPostsPerDay() {
        global $wpdb;
        $postsperday = (int) sem_showTotalPosts() / (int) sem_showPostDays();
        return $postsperday; // Ganzzahl
    }

### durchschn. Kommentare pro Tag
    function sem_showCommentsPerDay() {
        global $wpdb;
        $commentsperday = (int) sem_showTotalComments() / (int) sem_showCommentDays();
        return $commentsperday; // Ganzzahl
    }

### Startdatum der Gesamtstatistik (Basis: statzhist)
    function sem_showStatzStartDate() {
        global $wpdb;
        $statzhiststartdate = $wpdb->get_var("SELECT MIN(date) FROM ".$wpdb->statzhist." WHERE date != '0000-00-00 00:00' LIMIT 0,1");
        $statzhiststartdate = mysql2date("j.n.Y", $statzhiststartdate);
        // if(!$statzhiststartdate) $statzhiststartdate= "xx.xx.xxxx";
        return $statzhiststartdate; // String T.M.JJJJ
    }

### Dauer der Statistikaufzeichnung in Tagen (Basis: statzhist)
    function sem_showStatzDays() {
        global $wpdb;
        $firstdate = $wpdb->get_var("SELECT MIN(date) FROM ".$wpdb->statzhist." LIMIT 0,1");
        if(!$firstdate) {
            return 1;
        }
        else {
            $firstdate = strtotime($firstdate); $now = time();
            $statzdays = (int) (($now - $firstdate) / (60*60*24));
            return $statzdays; // Ganzzahl
        }
    }

### Anzahl der Online-Besucher - Verweildauer in den Einstellungen
    function sem_showNumUsersOnline() {
        global $wpdb;
		$sem_options = get_option('semmelstatzR_options'); ### optionsarray auslesen
        $onlinetime = $sem_options["statz_online_time"];
        $numonline = $wpdb->get_var("SELECT COUNT(DISTINCT ip) FROM ".$wpdb->statz." WHERE time > Now() - INTERVAL ".$onlinetime." SECOND");
        return $numonline; // Ganzzahl
    }

### Anzahl der heutigen Besucher
    function sem_showUsersToday() {
        global $wpdb; $today = date("Y-m-d");
        $todayusers = $wpdb->get_var("SELECT COUNT(DISTINCT ip) FROM ".$wpdb->statz." WHERE time >= '$today' LIMIT 0, 1");
        return $todayusers;
	}

### Anzahl der gestrigen Besucher
    function sem_showUsersYesterday() {
        global $wpdb;
        $yesterdaysusers = $wpdb->get_var("SELECT visitors FROM ".$wpdb->statzhist." WHERE date = substr(DATE_SUB(NOW(), INTERVAL 1 DAY),1,10) LIMIT 0, 1");
        return $yesterdaysusers;
	}

##################################################### Ende TEMPLATE-Funktionen
?>