<?php
/**
 * Copyright (C) 2009  Honghao Geng (genghonghao@gmail.com)
 * $Id: index.php 554 2010-01-24 05:00:10Z genghonghao $
 */

define('IN_GEN', true);
include './includes/common.inc.php';
require GEN_ROOT . 'includes/page.class.php';

// 判断网站是否关闭
close_blog();

// 取出广告数据
@include GEN_ROOT . '/data/ad_conf.php';
if (isset($ad_array)) {
    $tpl->assign('ad', $ad_array);
}

$temp = $db->result("SELECT COUNT(*) AS num FROM " . $db->get_table(TB_ARTICLE));
$total = $temp[0]['num'];
$perpage = (isset($_CONF['index_view_number']) && !empty($_CONF['index_view_number'])) ? intval($_CONF['index_view_number']) : 10;
if (!isset($_REQUEST['page']) || is_int($_REQUEST['page'])) {
    $p = 0;
}
else {
    $p = ((int)$_REQUEST['page'] - 1) * $perpage;
    if ($p >= $total) {
        $p = 0;
    }
}

$tpl->assign('base_url', $base_url);

$best_goods = array();
for($i = 0; $i< 30; $i++) {
    $best_goods[$i]['url'] = 'test'.$i;
    $best_goods[$i]['thumb'] = 'test'.$i;
    $best_goods[$i]['name'] = 'test'.$i;
    $best_goods[$i]['short_style_name'] = 'test'.$i;
}
$tpl->assign('best_goods', $best_goods);

// 查询文章列表
$result = $db->result("SELECT a.*, u.nickname FROM " . $db->get_table(TB_ARTICLE) . " AS a," . $db->get_table(TB_ADMIN) . " AS u WHERE a.user_id=u.admin_id ORDER BY article_id DESC LIMIT {$p}, {$perpage}");
// 添加文章内容中的代码的样式
foreach($result as $k=>$v) {
    // 不显示全文
    if (0 == $_CONF['index_view_is_full']) {
        $result[$k]['content'] = sub_str(($v['content']),  $_CONF['index_view_cut_number']);
    } else {
        $result[$k]['content'] = str_replace('[code]', "<pre class='prettyprint  lang-php'>", str_replace('[/code]', '</pre>', $result[$k]['content']));
    }
}
// 把文章数据传递到模板
$tpl->assign('article_list', $result);

if (!isset($_SESSION['user']['visit_count'])) {
    $q = $db->query("UPDATE ". $db->get_table(TB_STATISTICS) . " SET visit_count=visit_count+1 WHERE sta_id=1", '', 1);
    $_SESSION['user']['visit_count'] = TRUE;
}

// 分页
$page = new page(array('total'=>$total,'perpage'=>$perpage));
$tpl->assign('page', $page->show(4));

// 右侧导航数据
assign_right_list();

// 页面执行时间
list($usec, $sec) = explode(' ', microtime());
$time_diff = number_format(((float)$usec + (float)$sec) - $start_time, 6);
$tpl->assign('time_diff', $time_diff);

// 取出头部导航数据
$nav_list = $db->result("SELECT * FROM " . $db->get_table(TB_NAVIGATION) . " WHERE is_unlock='1' ORDER BY display_order ASC");
$tpl->assign('nav_list', $nav_list);

// SQL执行次数
$tpl->assign('queries', $db->get_num_queries());

// 当前时间
$tpl->assign('current_time', 'GMT' . date("O, Y-m-d H:i:s"));

$tpl->display('index.html');
?>
