<?php
/* ====================
[BEGIN_COT_EXT]
Hooks=tools
[END_COT_EXT]
==================== */

/**
 * Administration panel - Hits
 *
 * @package Cotonti
 * @copyright (c) Cotonti Team
 * @license https://github.com/Cotonti/Cotonti/blob/master/License.txt
 */

(defined('COT_CODE') && defined('COT_ADMIN')) or die('Wrong URL.');

list(Cot::$usr['auth_read'], Cot::$usr['auth_write'], Cot::$usr['isadmin']) = cot_auth('plug', 'hits');
cot_block(Cot::$usr['auth_read']);

require_once cot_langfile('hits', 'plug');
require_once cot_incfile('hits', 'plug');
$tt = new XTemplate(cot_tplfile('hits.admin', 'plug', true));

//$adminHelp = $L['adm_help_hits'];
$adminTitle = Cot::$L['hits'];

$f = cot_import('f', 'G', 'TXT');
$v = cot_import('v', 'G', 'TXT');

/* === Hook === */
foreach (cot_getextplugins('hits.admin.first') as $pl) {
	include $pl;
}
/* ===== */

if ($f == 'year' || $f == 'month') {
	$adminPath[] = [cot_url('admin', 'm=other&p=hits&f=' . $f . '&v=' . $v), '(' . $v . ')'];
	$sql = Cot::$db->query("SELECT * FROM $db_stats WHERE stat_name LIKE '" . Cot::$db->prep($v) . "%' ORDER BY stat_name DESC");
	
	while ($row = $sql->fetch()) {
		$y = mb_substr($row['stat_name'], 0, 4);
		$m = mb_substr($row['stat_name'], 5, 2);
		$d = mb_substr($row['stat_name'], 8, 2);
		$dat = cot_date('date_full', mktime(0, 0, 0, $m, $d, $y), false);
		$hits_d[$dat] = $row['stat_value'];
	}
	$sql->closeCursor();
	$hits_d_max = max($hits_d);
	$ii = 0;
	/* === Hook - Part1 : Set === */
	$extp = cot_getextplugins('hits.admin.loop');
	/* ===== */
	foreach ($hits_d as $day => $hits) {
		$percentbar = floor(($hits / $hits_d_max) * 100);
		$tt->assign([
			'ADMIN_HITS_ROW_DAY' => $day,
			'ADMIN_HITS_ROW_HITS' => $hits,
			'ADMIN_HITS_ROW_PERCENTBAR' => $percentbar,
			'ADMIN_HITS_ROW_ODDEVEN' => cot_build_oddeven($ii),
		]);
		
		/* === Hook - Part2 : Include === */
		foreach ($extp as $pl) {
			include $pl;
		}
		/* ===== */
		
		$tt->parse('MAIN.YEAR_OR_MONTH.ROW');
		$ii++;
	}
	
	$tt->parse('MAIN.YEAR_OR_MONTH');

} else {
    $rowmax = null;
	$sqlmax = Cot::$db->query("SELECT * FROM $db_stats WHERE stat_name LIKE '20%' ORDER BY stat_value DESC LIMIT 1");
	if ($sqlmax->rowCount() > 0) {
		$rowmax = $sqlmax->fetch();
		$sqlmax->closeCursor();
	}
    $sql = Cot::$db->query("SELECT * FROM $db_stats WHERE stat_name LIKE '20%' ORDER BY stat_name DESC");

	if ($sql->rowCount() > 0 && $rowmax) {
		$max_date = $rowmax['stat_name'];
		$max_hits = $rowmax['stat_value'];

		$ii = 0;
		$hits_m = [];
		$hits_w = [];

		while ($row = $sql->fetch()) {
			$y = mb_substr($row['stat_name'], 0, 4);
			$m = mb_substr($row['stat_name'], 5, 2);
			$d = mb_substr($row['stat_name'], 8, 2);
			$w = cot_date('W', mktime(0, 0, 0, $m, $d, $y));

            if (!isset($hits_w[$y.'-W'.$w])) {
                $hits_w[$y.'-W'. $w] = 0;
            }
            if (!isset($hits_m[$y.'-'.$m])) {
                $hits_m[$y.'-'.$m] = 0;
            }
            if (!isset($hits_y[$y])) {
                $hits_y[$y] = 0;
            }

            $hits_w[$y . '-W' . $w] += $row['stat_value'];
			$hits_m[$y . '-' . $m] += $row['stat_value'];
			$hits_y[$y] += $row['stat_value'];
		}
		$sql->closeCursor();

		$hits_w_max = max($hits_w);
		$hits_m_max = max($hits_m);
		$hits_y_max = max($hits_y);
		/* === Hook - Part1 : Set === */
		$extp = cot_getextplugins('hits.admin.loop');
		/* ===== */
		$ii = 0;
		foreach ($hits_y as $year => $hits) {
			$percentbar = floor(($hits / $hits_y_max) * 100);
			$tt->assign([
				'ADMIN_HITS_ROW_YEAR_URL' => cot_url('admin', 'm=other&p=hits&f=year&v=' . $year),
				'ADMIN_HITS_ROW_YEAR' => $year,
				'ADMIN_HITS_ROW_YEAR_HITS' => $hits,
				'ADMIN_HITS_ROW_YEAR_PERCENTBAR' => $percentbar,
			]);
			/* === Hook - Part2 : Include === */
			foreach ($extp as $pl) {
				include $pl;
			}
			/* ===== */
			$tt->parse('MAIN.DEFAULT.ROW_YEAR');
			$ii++;
		}
		$ii = 0;
		foreach ($hits_m as $month => $hits) {
			$percentbar = floor(($hits / $hits_m_max) * 100);
			$tt->assign([
				'ADMIN_HITS_ROW_MONTH_URL' => cot_url('admin', 'm=other&p=hits&f=month&v=' . $month),
				'ADMIN_HITS_ROW_MONTH' => $month,
				'ADMIN_HITS_ROW_MONTH_HITS' => $hits,
				'ADMIN_HITS_ROW_MONTH_PERCENTBAR' => $percentbar,
			]);
			/* === Hook - Part2 : Include === */
			foreach ($extp as $pl) {
				include $pl;
			}
			/* ===== */
			$tt->parse('MAIN.DEFAULT.ROW_MONTH');
			$ii++;
		}
		$ii = 0;
		foreach ($hits_w as $week => $hits) {
			$ex = explode('-W', $week);
			$percentbar = floor(($hits / $hits_w_max) * 100);
			$tt->assign([
				'ADMIN_HITS_ROW_WEEK' => $week,
				'ADMIN_HITS_ROW_WEEK_HITS' => $hits,
				'ADMIN_HITS_ROW_WEEK_PERCENTBAR' => $percentbar,
			]);
			/* === Hook - Part2 : Include === */
			foreach ($extp as $pl) {
				include $pl;
			}
			/* ===== */
			$tt->parse('MAIN.DEFAULT.ROW_WEEK');
			$ii++;
		}

		$tt->assign([
			'ADMIN_HITS_MAXHITS' => sprintf(Cot::$L['hits_maxhits'], $max_date, $max_hits),
		]);
	}
	$tt->parse('MAIN.DEFAULT');
}

/* === Hook  === */
foreach (cot_getextplugins('hits.admin.tags') as $pl) {
	include $pl;
}
/* ===== */

$tt->parse('MAIN');
$pluginBody = $tt->text('MAIN');