<?php

use SCF\Core\DI;
use SCF\Core\Database;
use SCF\Core\StatsGenerator;

$acl = DI::getInstance()->get('acl');


if($acl->acl('see_SKS') == 1) {
    //var_dump($statDays);
    $Download = [];
    $db_slave = new Database($config['dbext'], $config['dbhost'], $config['dbuser'], $config['dbpass'], 'sks_gametracking');

    $db_slave->query("SELECT * FROM sks_core_settings WHERE setting = 'sks_previous_versions'");
    $vArray = $db_slave->single();
    $versions = explode(',', $vArray['value']);

    foreach ($versions as $key => $val) {
        $db_slave->query("SELECT * FROM `sks_unique_dls` WHERE `dl_version` = '{$val}'");
        $db_slave->execute();
        $Download[$val] = $db_slave->rowCount();
    }

    $db_slave->query("SELECT * FROM `sks_unique_dls` WHERE `timestamp` != 0");
    $db_slave->execute();
    $Download['all'] = $db_slave->rowCount();

    $db_slave->query("SELECT COUNT(guid) AS visit_count, DAY(FROM_UNIXTIME(`timestamp`)) as visit_day, MONTH(FROM_UNIXTIME(`timestamp`)) as visit_month, YEAR(FROM_UNIXTIME(`timestamp`)) AS visit_year FROM sks_unique_dls GROUP BY visit_year DESC, visit_month DESC, visit_day DESC LIMIT 10");
    $downloadsPerDay = $db_slave->fetchArray();

    $db_slave->query("SELECT * FROM `sks_unique_user` WHERE `timestamp` != 0");
    $db_slave->execute();
    $allVistors = $db_slave->rowCount();

    $db_slave->query("SELECT COUNT(guid) AS visit_count, DAY(FROM_UNIXTIME(`timestamp`)) as visit_day, MONTH(FROM_UNIXTIME(`timestamp`)) as visit_month, YEAR(FROM_UNIXTIME(`timestamp`)) AS visit_year FROM sks_unique_user GROUP BY visit_year DESC, visit_month DESC, visit_day DESC LIMIT 10");
    $visitorsPerDay = $db_slave->fetchArray();


    $stats = new StatsGenerator($db_slave);

    $genTraffic = round((($Download['all']*555008)/1024)/1024,2);


    $tpl->assign('downloadArray', $downloadsPerDay);
    $tpl->assign('visitorArray', $visitorsPerDay);
    $tpl->assign('allVisitors', $allVistors);
    $tpl->assign('allDownloads', $Download['all']);
    $tpl->assign('traffic', $genTraffic);
    $tpl->assign('stats', $stats);

    //$tpl->assign('vardump', $stats);
    $tpl->display('Data/specific/sternenkindsaga.tpl');
}
else {
    $tpl->display('errorpages/AclError.tpl');
}