<?  // PHPmyGallery - backend core - statistics

/* File description:

This file processes the statistics which the frontend script
writes to the database when viewing Groups, Galleries, or Pictures.
The script also crops entries from the statistics, if the rank of
an entry is too low. The minimum rank can be changed in the main setup.

### What this script does:
###
### Included from: adminmenu.index.php
### |
### | (Uses templates from adminmenu/statistics)
### +- Statistics View        (Load Statistics, Crop old Entries, display Statistics) 
###
*/ 

$filenameGET = $_GET['filename'];
$pktpl_vars['statfile'] = $filenameGET;

$dateYear = date('Y');
$showMaxYears = 5; // Go 5 years backwards at maximum
$statsfound  = false;
$statsloaded = false;

// topX = Top10, Top25, Top50...
$topX = $_GET['topX'];
if ($topX < 10||$topX > $conf['statentries_til_garbage_collection']) {
    $topX = 10;
} // if

// Reset Stats navigation
$pktpl_vars['template_admin_statsNavi'] = '';

// Process statistics (if found)
if (statfilesExist()) {
    // looks up on statistics for the last $showMaxYears years
    for($year = $dateYear;$year > ($dateYear - $showMaxYears);$year--) {
        $statsforthisYear = false;
        // Iterates through every month (01-12), $m is set in language files
        foreach($m as $month=>$m_name) {
            $dbEntry = $year.'.'.$month;
            
            // On an existing file, make link
            if (allocateData('statistics',$dbEntry)) {
                // Fill tpl vars
                $pktpl_vars['link_'.$month.'month'] = $year.'.'.$month;
                $pktpl_vars['desc_'.$month.'month'] = $m_name;
                $pktpl_vars['text_if'.$month.'notexists'] = '';
                
                // true if file found at all
                $statsFound = true;
                // true if file found for this year
                $statsforthisYear = true;
            } // if
            // If file does not exist, only display text
            else {
                $pktpl_vars['link_'.$month.'month'] = '';
                $pktpl_vars['desc_'.$month.'month'] = '';
                $pktpl_vars['text_if'.$month.'notexists'] = $m_name;
            } // else
        } // foreach
        
        // if file found for this year
        if ($statsforthisYear==true) {
            $pktpl_vars['text_year'] = $year;
            $pktpl_vars['template_admin_statsNavi'] .= pktpl('adminmenu/statistics/Navi.html');
        } // if
    } // for
    
    // If there are no stats for the last 5 years, display this...
    if ($statsFound==false) {
        // Fill tpl vars
        // TODO: extra help for this case
        $pktpl_vars['text_headline']      = $l['no_stats_found'];
        $pktpl_vars['text_headline_help'] = $l['no_stats_found_help'];
    } // if
} // if
// If there are no stats, tell user
else {
    // Fill tpl vars
    $pktpl_vars['text_headline']      = $l['no_stats_found'];
    $pktpl_vars['text_headline_help'] = $l['no_stats_found_help'];
} // else


/* Statistics Table */

// Get statistics from database
if ($filenameGET != '' && allocateData('statistics',$filenameGET)) {
    $selectedStats = getData('statistics',$filenameGET);
    $statsloaded = true;
} // if
elseif(allocateData('statistics',date('Y.m'))) {
    $filenameGET = date('Y.m');
    $selectedStats = getData('statistics',$filenameGET);
    $statsloaded = true;
} // elseif

// no file selected
if (!$statsloaded && $statsFound) {
    $pktpl_vars['text_headline']      = $l['select_file'];
    $pktpl_vars['text_headline_help'] = $l['select_file_help'];
    
    $pktpl_vars['template_stats'] = '';
} // if
// File selected (processing stats)
elseif ($filenameGET!='' && $statsFound) {
    $pktpl_vars['text_headline']      = $l['view_statistics'];
    $pktpl_vars['text_headline_help'] = $l['view_statistics_help'];
    
    // Prepare arrays and include stats
    $pic_stats   = $selectedStats[STATS_PICS];
    $dir_stats   = $selectedStats[STATS_GALLERIES];
    $group_stats = $selectedStats[STATS_GROUPS];
    
    natsort($pic_stats);
    natsort($dir_stats);
    natsort($group_stats);
    
    $pic_stats   = array_reverse($pic_stats);
    $dir_stats   = array_reverse($dir_stats);
    $group_stats = array_reverse($group_stats);
    
    // Garbage collection
    $pic_stats_count = count($pic_stats);
    $dir_stats_count = count($dir_stats);
    $grp_stats_count = count($group_stats);
    $toomany_pic_stats = ($pic_stats_count > $conf['statentries_til_garbage_collection']);
    $toomany_dir_stats = ($dir_stats_count > $conf['statentries_til_garbage_collection']);
    $toomany_grp_stats = ($grp_stats_count > $conf['statentries_til_garbage_collection']);
    if ($toomany_pic_stats) {
        $pic_stats = array_slice($pic_stats,0,$conf['statentries_til_garbage_collection']);
    } // if
    if ($toomany_dir_stats) {
        $dir_stats = array_slice($dir_stats,0,$conf['statentries_til_garbage_collection']);
    } // if
    if ($toomany_grp_stats) {
        $dir_stats = array_slice($dir_stats,0,$conf['statentries_til_garbage_collection']);
    } // if
    
    if ($toomany_pic_stats||$toomany_dir_stats||$toomany_grp_stats) {
        $data = array();
        $data[STATS_PICS]      = $pic_stats;
        $data[STATS_GROUPS]    = $group_stats;
        $data[STATS_GALLERIES] = $dir_stats;
        
        putData($data,'statistics',$filenameGET);
    } // if
    
    $pktpl_vars['template_stats'] = '';
    
    // Group stats
    $pktpl_vars['template_statsTableSubtablerows'] = '';
    reset($group_stats);
    for($x = 1;$x<=$topX;$x++) {
        // Get `$x`th group entry
        $current = current($group_stats);
        $cur_key = key($group_stats);
        
        // Make template vars
        $pktpl_vars['text_rank'] = $x;
        $pktpl_vars['text_name'] = stripslashes($cur_key);
        $pktpl_vars['text_hits'] = $current;
        $pktpl_vars['link']      = $conf['pathToGroups'].$conf['myGfilename'].'?group='.urlencode($cur_key);
        // Make template
        $pktpl_vars['template_statsTableSubtablerows'] .= pktpl('adminmenu/statistics/TableSubtablerow.html');
        
        // Proceed to next directory entry
        next($group_stats);
    } // for
    $pktpl_vars['headline']        = $l['statsGrpheadline'];
    $pktpl_vars['template_stats'] .= pktpl('adminmenu/statistics/Table.html');
    
    
    // Directory stats
    $pktpl_vars['template_statsTableSubtablerows'] = '';
    reset($dir_stats);
    for($x = 1;$x<=$topX;$x++) {
        // Get `$x`th directory entry
        $current = current($dir_stats);
        $cur_key = key($dir_stats);
        // Seperates group and directory from filename
        $tmp = explode('/',$cur_key);
        
        // Make template vars
        $pktpl_vars['text_rank'] = $x;
        $pktpl_vars['text_name'] = stripslashes($tmp[1]);
        $pktpl_vars['text_hits'] = $current;
        $pktpl_vars['link']      = $conf['pathToGroups'].$conf['myGfilename'].'?group='.urlencode($tmp[0]).'&dir='.urlencode($tmp[1]);
        // Make template
        $pktpl_vars['template_statsTableSubtablerows'] .= pktpl('adminmenu/statistics/TableSubtablerow.html');
        
        // Proceed to next directory entry
        next($dir_stats);
    } // for
    $pktpl_vars['headline']        = $l['statsDirheadline'];
    $pktpl_vars['template_stats'] .= pktpl('adminmenu/statistics/Table.html');
    
    // Picture stats
    $pktpl_vars['template_statsTableSubtablerows'] = '';
    reset($pic_stats);
    for($x = 1;$x<=$topX;$x++) {
        // Get `$x`th picture entry
        $current = current($pic_stats);
        $cur_key = key($pic_stats);
        // Seperates directoryname from filename
        $tmp = explode('/',$cur_key);
        
        // Make template vars
        $pktpl_vars['text_rank'] = $x;
        $pktpl_vars['text_name'] = stripslashes($tmp[2]);
        $pktpl_vars['text_hits'] = $current;
        $pktpl_vars['link']      = $conf['pathToGroups'].$conf['myGfilename'].'?group='.urlencode($tmp[0]).'&dir='.urlencode($tmp[1]).'&pic='.urlencode($tmp[2]);
        
        // Make template
        $pktpl_vars['template_statsTableSubtablerows'] .= pktpl('adminmenu/statistics/TableSubtablerow.html');
        
        // Proceed to next picture entry
        next($pic_stats);
        
    } // for
    $pktpl_vars['headline']        = $l['statsPicheadline'];
    $pktpl_vars['template_stats'] .= pktpl('adminmenu/statistics/Table.html');
    
} // elseif

$pktpl_vars['template_admin_table'] = pktpl('adminmenu/statistics/stats.html');

?>