<?php

/**
 * ECSHOP 综合流量统计
 * ============================================================================
 * 版权所有 (C) 2005-2007 北京亿商互动科技发展有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * @author:     Weber Liu <weberliu@hotmail.com>
 * @version:    v2.1
 * ---------------------------------------------
 * $Author: paulgao $
 * $Date: 2007-05-16 17:30:48 +0800 (星期三, 16 五月 2007) $
 * $Id: flow_stats.php 8639 2007-05-16 09:30:48Z paulgao $
*/

define('IN_ECS', true);

require('includes/init.php');
require_once(ROOT_PATH.'includes/iconv/cls_iconv.php');
$iconv = new Chinese(ROOT_PATH);

/* act操作项的初始化 */
if (empty($_REQUEST['act']))
{
    $_REQUEST['act'] = 'view';
}
else
{
    $_REQUEST['act'] = trim($_REQUEST['act']);
}

if ($_REQUEST['act'] == 'view')
{
    admin_priv('client_flow_stats');

    /* 时间参数 */
    if (isset($_POST) && !empty($_POST))
    {
        $start_date = strtotime($_POST['start_dateYear'].'-'.$_POST['start_dateMonth'].'-'.$_POST['start_dateDay']);
        $end_date   = strtotime($_POST['end_dateYear'].'-'.$_POST['end_dateMonth'].'-'.$_POST['end_dateDay']);
    }
    else
    {
        $start_date = time()- 86400 * 7;
        $end_date   = time();
    }
    /* ------------------------------------- */
    /* --综合流量
    /* ------------------------------------- */
    $max = 0;
    $general_xml = "<graph caption='$_LANG[general_stats]' shownames='1' showvalues='1' decimalPrecision='0' yaxisminvalue='0' yaxismaxvalue='%d' animation='1' outCnvBaseFontSize='12' baseFontSize='12' xaxisname='$_LANG[date]' yaxisname='$_LANG[access_count]' >";

    $sql = "SELECT FROM_UNIXTIME(access_time, '%m-%d') AS access_date, COUNT(*) AS access_count".
            " FROM " .$ecs->table('stats').
            " WHERE access_time >= '$start_date' AND access_time <= " .($end_date + 86400).
            " GROUP BY access_date";
    $res = $db->query($sql);

    $key = 0;
    while ($val = $db->fetchRow($res))
    {
        $general_xml .= "<set name='$val[access_date]' value='$val[access_count]' color='" .chart_color($key). "' />";
        if ($val['access_count'] > $max)
        {
            $max = $val['access_count'];
        }
        $key++;
    }

    $general_xml .= '</graph>';
    $general_xml  = sprintf($general_xml, $max);

    /* ------------------------------------- */
    /* --地域分布
    /* ------------------------------------- */
    $area_xml  = '';
    $area_xml .= "<graph caption='".$_LANG['area_stats']."' shownames='1' showvalues='1' decimalPrecision='2' outCnvBaseFontSize='12' baseFontSize='12' pieYScale='45'  pieBorderAlpha='40' pieFillAlpha='70' pieSliceDepth='15' pieRadius='100'>";

    $sql = "SELECT COUNT(*) AS access_count, area FROM " . $ecs->table('stats') .
            " WHERE access_time >= '$start_date' AND access_time <= " .($end_date + 86400).
            " GROUP BY area ORDER BY access_count DESC LIMIT 20";
    $res = $db->query($sql);

    $key = 0;
    while ($val = $db->fetchRow($res))
    {
        $area = empty($val['area']) ? 'unknow' : $val['area'];

        $area_xml .= "<set name='$area' value='$val[access_count]' color='" .chart_color($key). "' />";
        $key++;
    }

    $area_xml .= '</graph>';

    /* ------------------------------------- */
    /* --来源网站
    /* ------------------------------------- */
    $from_xml = "<graph caption='$_LANG[from_stats]' shownames='1' showvalues='1' decimalPrecision='2' outCnvBaseFontSize='12' baseFontSize='12' pieYScale='45' pieBorderAlpha='40' pieFillAlpha='70' pieSliceDepth='15' pieRadius='100'>";

    $sql = "SELECT COUNT(*) AS access_count, referer_domain FROM " . $ecs->table('stats') .
            " WHERE access_time >= '$start_date' AND access_time <= " .($end_date + 86400).
            " GROUP BY referer_domain ORDER BY access_count DESC LIMIT 20";
    $res = $db->query($sql);

    $key = 0;
    while ($val = $db->fetchRow($res))
    {
        $from = empty($val['referer_domain']) ? $_LANG['input_url'] : $val['referer_domain'];

        $from_xml .= "<set name='".str_replace(array('http://', 'https://'), array('', ''), $from) . "' value='$val[access_count]' color='" . chart_color($key). "' />";

        $key++;
    }

    $from_xml .= '</graph>';

    /* ------------------------------------- */
    /* --关键字分析
    /* ------------------------------------- */
    $key_xml = "<graph caption='$_LANG[tab_keywords]' shownames='1' showvalues='1' decimalPrecision='2' outCnvBaseFontSize='12' baseFontSize='12'>";

    $sql = "SELECT COUNT(*) AS access_count, keywords FROM " . $ecs->table('stats') .
            " WHERE keywords > '' AND access_time >= '$start_date ' AND access_time <= " .($end_date + 86400).
            " GROUP BY keywords ORDER BY access_count DESC LIMIT 10";
    $res = $db->query($sql);

    $key = 0;
    while ($val = $db->fetchRow($res))
    {
        $val['keywords'] =  str_replace('"', '', $val['keywords']);
        $val['keywords'] =  str_replace('\'', '', $val['keywords']);

        $key_xml .= "<set name='$val[keywords]' value='$val[access_count]' color='" .chart_color($key). "' />";

        $key++;
    }

    $key_xml .= '</graph>';

    /* 模板赋值 */
    $smarty->assign('ur_here',      $_LANG['flow_stats']);
    $smarty->assign('general_data', $general_xml);
    $smarty->assign('area_data',    $area_xml);
    $smarty->assign('from_data',    $from_xml);
    $smarty->assign('key_data',     $key_xml);

    /* 显示日期 */
    $smarty->assign('start_date',   date('Y-m-d', $start_date));
    $smarty->assign('end_date',     date('Y-m-d', $end_date));

    /* 显示页面 */
    assign_query_info();
    $smarty->display('flow_stats.htm');
}

?>