<?php

/**
 * ECSHOP 生成商品列表
 * ============================================================================
 * 版权所有 (C) 2005-2007 北京亿商互动科技发展有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * @author:     scott ye <scott.yell@gmail.com>
 * @version:    v2.1
 * ---------------------------------------------
 * $Author: paulgao $
 * $Date: 2007-05-22 13:11:46 +0800 (星期二, 22 五月 2007) $
 * $Id: goods_script.php 8686 2007-05-22 05:11:46Z paulgao $
 */

define('IN_ECS', true);
define('INIT_NO_USERS', true);

require('./includes/init.php');

if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}

$charset = empty($_GET['charset']) ? 'UTF8' : $_GET['charset'];
header('content-type: application/x-javascript; charset=' . ($charset == 'UTF8' ? 'utf-8' : $charset));

/*------------------------------------------------------ */
//-- 判断是否存在缓存，如果存在则调用缓存，反之读取相应内容
/*------------------------------------------------------ */
/* 缓存编号 */
$cache_id = sprintf('%X', crc32($_SERVER['QUERY_STRING']));

$tpl = ROOT_PATH . 'data/goods_script.html';
if (!$smarty->is_cached($tpl, $cache_id))
{
    /* 创建字符集转换对象 */
    if ($charset != 'UTF8')
    {
        include_once(ROOT_PATH . 'includes/iconv/cls_iconv.php');
        $iconv = new Chinese(ROOT_PATH);
    }

    /* 根据参数生成查询语句 */
    $time = date('Y-m-d');
    $sql  = 'SELECT goods_id, goods_name, market_price, goods_thumb, RAND() AS rnd, ' .
            "IF(is_promote = 1 AND '$time' >= promote_start AND '$time' <= promote_end, promote_price, shop_price) AS goods_price " .
            'FROM ' . $ecs->table('goods') . ' AS g ' .
            " WHERE is_delete = '0' AND is_on_sale = '1' AND is_alone_sale = '1' ";

    if (!empty($_GET['cat_id']))
    {
        $sql .= ' AND ' . get_children(intval($_GET['cat_id']));
    }
    if (!empty($_GET['brand_id']))
    {
        $sql .= " AND brand_id = '" . intval($_GET['brand_id']) . "'";
    }
    if (!empty($_GET['intro_type']))
    {
        $_GET['intro_type'] = trim($_GET['intro_type']);

        if ($_GET['intro_type'] == 'is_best' || $_GET['intro_type'] == 'is_new' || $_GET['intro_type'] == 'is_hot' || $_GET['intro_type'] == 'is_promote' || $_GET['intro_type'] == 'is_random')
        {
            if ($_GET['intro_type'] == 'is_random')
            {
                $sql .= ' ORDER BY rnd';
            }
            else
            {
                $sql .= " AND " . $_GET['intro_type'] . " = 1";
                if ($_GET['intro_type'] == 'is_promote')
                {
                    $sql  .= " AND promote_start <= '$time' AND promote_end >= '$time'";
                }
            }
        }
    }
    $sql .= " LIMIT " . (!empty($_GET['goods_num']) ? intval($_GET['goods_num']) : 10);
    $res = $db->query($sql);

    $goods_list = array();
    while ($goods = $db->fetchRow($res))
    {
        // 转换编码
        $goods['goods_price'] = price_format($goods['goods_price']);
        if ($charset != 'UTF8')
        {
            $goods['goods_name']  = $iconv->convert('UTF8', $charset, htmlentities($goods['goods_name'], ENT_QUOTES, 'UTF-8'));
            $goods['goods_price'] = $iconv->convert('UTF8', $charset, $goods['goods_price']);
        }
        $goods_list[] = $goods;
    }
    $smarty->assign('goods_list', $goods_list);

    /* 排列方式 */
    $arrange = empty($_GET['arrange']) || !in_array($_GET['arrange'], array('h', 'v')) ? 'h' : $_GET['arrange'];
    $smarty->assign('arrange', $arrange);

    /* 是否需要图片 */
    $need_image = empty($_GET['need_image']) || $_GET['need_image'] == 'true' ? 1 : 0;
    $smarty->assign('need_image', $need_image);

    /* 图片大小 */
    $smarty->assign('thumb_width', intval($_CFG['thumb_width']));
    $smarty->assign('thumb_height', intval($_CFG['thumb_height']));

    /* 网站根目录 */
    $smarty->assign('url', $ecs->url());

    /* 商品页面连接 */
    $goods_url = $ecs->url() . 'affiche.php?ad_id=-1&amp;from=' . urlencode(@$_GET['sitename']) . '&amp;goods_id=';
    $smarty->assign('goods_url', $goods_url);
}

$output = $smarty->fetch($tpl, $cache_id);
$output = str_replace("\r", '', $output);
$output = str_replace("\n", '', $output);

echo "document.write('$output');";

?>