<?php

/**
 * ECSHOP 列出所以分类及品牌
 * ============================================================================
 * 版权所有 (C) 2005-2007 北京亿商互动科技发展有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * @author:     wj <wjzhangq@126.com>
 * @version:    v2.1
 * ---------------------------------------------
 * $Author: paulgao $
 * $Date: 2007-04-19 18:11:35 +0800 (星期四, 19 四月 2007) $
 * $Id: catalog.php 8272 2007-04-19 10:11:35Z paulgao $
*/

define('IN_ECS', true);

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

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

if (!$smarty->is_cached('catalog.dwt'))
{
    /* 取出所有分类 */
    $cat_list = cat_list(0, 0, false);

    /* 查出底级分类的商品数量 */
    $sql = "SELECT c.cat_id, COUNT(*) AS goods_num FROM ".$ecs->table('category'). " AS c, ".$ecs->table('goods'). " AS g ".
            " WHERE c.cat_id = g.cat_id AND c.is_leaf = 1 AND g.is_delete = 0 AND g.is_alone_sale = 1 GROUP BY c.cat_id";
    $rs  = $db->query($sql);

    if (!empty($cat_list))
    {
        $max_level = 0;
        while ($row = $db->fetchRow($rs))
        {
            $cat_list[$row['cat_id']]['goods_num'] = $row['goods_num'];

            if ($cat_list[$row['cat_id']]['level'] > $max_level)
            {
                $max_level = $cat_list[$row['cat_id']]['level'];
            }
        }

        /* 完成url */
        foreach ($cat_list AS $key => $cat)
        {
            if ($cat['is_leaf'] == 1 && empty($cat['goods_num']))
            {
                $cat_list[$key]['goods_num'] = 0;

                if ($cat['level'] > $max_level)
                {
                    $max_level = $cat['level'];
                }
            }
            $cat_list[$key]['url'] = build_uri('category', array('cid'=>$cat['cat_id']), $cat['cat_name']);
        }

        /* 计算各级分类的商品数量 */
        for ($i = $max_level - 1; $i >= 0; $i--)
        {
            foreach ($cat_list AS $key => $cat)
            {
                if ($cat['level'] == $i && $cat['is_leaf'] == 0)
                {
                    $cat_list[$key]['goods_num'] = calculate_goods_num($cat_list, $key);
                }
            }
        }
    }

    assign_template();
    assign_dynamic('catalog');
    $position = assign_ur_here(0, $_LANG['catalog']);
    $smarty->assign('page_title', $position['title']);   // 页面标题
    $smarty->assign('ur_here',    $position['ur_here']); // 当前位置

    $smarty->assign('nav_list',   get_navigator()); // 导航栏
    $smarty->assign('helps',      get_shop_help()); // 网店帮助
    $smarty->assign('cat_list',   $cat_list);       // 分类列表
    $smarty->assign('brand_list', get_brands());    // 所以品牌赋值
}

$smarty->display('catalog.dwt');

/**
 * 计算指定分类的商品数量
 *
 * @access public
 * @param   integer     $cat_id
 *
 * @return void
 */
function calculate_goods_num($cat_list, $cat_id)
{
    $goods_num = 0;

    foreach ($cat_list AS $cat)
    {
        if ($cat['parent_id'] == $cat_id && !empty($cat['goods_num']))
        {
            $goods_num += $cat['goods_num'];
        }
    }

    return $goods_num;
}

?>