ï»??php
define('IN_ECTOUCH', true);
require(dirname(__FILE__) . '/include/init.php');
if ((DEBUG_MODE & 2) != 2) {
$smarty->caching = true;
}
/* 获得请求的分�ID */
if (isset($_REQUEST['id'])) {
$cat_id = intval($_REQUEST['id']);
}
elseif (isset($_REQUEST['category'])){
$cat_id = intval($_REQUEST['category']);
}
else{
/* 如果分类ID�,则返回首页 */
ecs_header("Location: ./\n");
exit;
}
/* åˆå§‹åŒ–分页信æ?*/
$page = isset($_REQUEST['page']) && intval($_REQUEST['page']) > 0 ? intval($_REQUEST['page']) : 1;
$size = isset($_CFG['page_size']) && intval($_CFG['page_size']) > 0 ? intval($_CFG['page_size']) : 10;
$brand = isset($_REQUEST['brand']) && intval($_REQUEST['brand']) > 0 ? intval($_REQUEST['brand']) : 0;
$price_max = isset($_REQUEST['price_max']) && intval($_REQUEST['price_max']) > 0 ? intval($_REQUEST['price_max']) : 0;
$price_min = isset($_REQUEST['price_min']) && intval($_REQUEST['price_min']) > 0 ? intval($_REQUEST['price_min']) : 0;
$filter_attr_str = isset($_REQUEST['filter_attr']) ? htmlspecialchars(trim($_REQUEST['filter_attr'])) : '0';
$filter_attr_str = trim(urldecode($filter_attr_str));
$filter_attr_str = preg_match('/^[\d\.]+$/',$filter_attr_str) ? $filter_attr_str : '';
$filter_attr = empty($filter_attr_str) ? '' : explode('.', $filter_attr_str);
/* 排åºã€æ˜¾ç¤ºæ–¹å¼ä»¥åŠç±»åž?*/
$default_display_type = $_CFG['show_order_type'] == '1' ? 'list' : ($_CFG['show_order_type'] == '0' ? 'grid' : 'album'); /* 显示模å¼, */
/* 上一å¥åŽŸä»£ç :$default_display_type = $_CFG['show_order_type'] == '0' ? 'list' : ($_CFG['show_order_type'] == '1' ? 'grid' : 'album'); */
$default_sort_order_method = $_CFG['sort_order_method'] == '0' ? 'ASC' : 'DESC'; /* æŽ’åºæ–¹å¼ */
$default_sort_order_type = $_CFG['sort_order_type'] == '0' ? 'g.sort_order,goods_id' : ($_CFG['sort_order_type'] == '1' ? 'shop_price' : 'last_update');
$sort = (isset($_REQUEST['sort']) && in_array(trim(strtolower($_REQUEST['sort'])), array('g.sort_order', 'goods_id', 'shop_price', 'last_update', 'click_count', 'sales_count'))) ? trim($_REQUEST['sort']) : $default_sort_order_type; // å¢žåŠ æŒ‰äººæ°”ã€æŒ‰é”€é‡æŽ’åº?
$order = (isset($_REQUEST['order']) && in_array(trim(strtoupper($_REQUEST['order'])), array('ASC', 'DESC'))) ? trim($_REQUEST['order']) : $default_sort_order_method;
$display = (isset($_REQUEST['display']) && in_array(trim(strtolower($_REQUEST['display'])), array('list', 'grid', 'album'))) ? trim($_REQUEST['display']) : (isset($_COOKIE['ECS']['display']) ? $_COOKIE['ECS']['display'] : $default_display_type);
$display = in_array($display, array('list', 'grid', 'album')) ? $display : 'list';
setcookie('ECS[display]', $display, gmtime() + 86400 * 7);
/* 页é¢çš„缓å˜ID */
$cache_id = sprintf('%X', crc32($cat_id . '-' . $display . '-' . $sort .'-' . $order .'-' . $page . '-' . $size . '-' . $_SESSION['user_rank'] . '-' .$_CFG['lang'] .'-'. $brand. '-' . $price_max . '-' .$price_min . '-' . $filter_attr_str));
if (!$smarty->is_cached('category.dwt', $cache_id)){
/* å¦‚æžœé¡µé¢æ²¡æœ‰è¢«ç¼“å˜åˆ™é‡æ–°èŽ·å–页é¢çš„内å®?*/
$children = get_children($cat_id);
$cat = get_cat_info($cat_id); // 获得分类的相关信æ?
if (!empty($cat)){
$smarty->assign('keywords', htmlspecialchars($cat['keywords']));
$smarty->assign('description', htmlspecialchars($cat['cat_desc']));
$smarty->assign('cat_style', htmlspecialchars($cat['style']));
$smarty->assign('catname', htmlspecialchars($cat['cat_name']));
}
else{
/* 如果分类ä¸å˜åœ¨åˆ™è¿”回首页 */
ecs_header("Location: ./\n");
exit;
}
/* 赋值固定内�*/
if ($brand > 0){
$sql = "SELECT brand_name FROM " .$GLOBALS['ecs']->table('brand'). " WHERE brand_id = '$brand'";
$brand_name = $db->getOne($sql);
}
else{
$brand_name = '';
}
/* 获å–ä»·æ ¼åˆ†çº§ */
if ($cat['grade'] == 0 && $cat['parent_id'] != 0){
$cat['grade'] = get_parent_grade($cat_id); //如果当å‰åˆ†ç±»çº§åˆ«ä¸ºç©ºï¼Œå–最近的上级分类
}
if ($cat['grade'] > 1){
$sql = "SELECT min(g.shop_price) AS min, max(g.shop_price) as max "." FROM " . $ecs->table('goods'). " AS g "." WHERE ($children OR " . get_extension_goods($children) . ') AND g.is_delete = 0 AND g.is_on_sale = 1 AND g.is_alone_sale = 1 ';
//获得当å‰åˆ†ç±»ä¸‹å•†å“ä»·æ ¼çš„æœ€å¤§å€¼ã€æœ€å°å€?
$row = $db->getRow($sql);
// å–å¾—ä»·æ ¼åˆ†çº§æœ€å°å•ä½çº§æ•°ï¼Œæ¯”如,åƒå…ƒå•†å“最å°ä»¥100为级æ•?
$price_grade = 0.0001;
for($i=-2; $i<= log10($row['max']); $i++){
$price_grade *= 10;
}
//跨度
$dx = ceil(($row['max'] - $row['min']) / ($cat['grade']) / $price_grade) * $price_grade;
if($dx == 0){
$dx = $price_grade;
}
for($i = 1; $row['min'] > $dx * $i; $i ++);
for($j = 1; $row['min'] > $dx * ($i-1) + $price_grade * $j; $j++);
$row['min'] = $dx * ($i-1) + $price_grade * ($j - 1);
for(; $row['max'] >= $dx * $i; $i ++);
$row['max'] = $dx * ($i) + $price_grade * ($j - 1);
$sql = "SELECT (FLOOR((g.shop_price - $row[min]) / $dx)) AS sn, COUNT(*) AS goods_num "." FROM " . $ecs->table('goods') . " AS g "." WHERE ($children OR " . get_extension_goods($children) . ') AND g.is_delete = 0 AND g.is_on_sale = 1 AND g.is_alone_sale = 1 '." GROUP BY sn ";
$price_grade = $db->getAll($sql);
foreach ($price_grade as $key=>$val){
$temp_key = $key + 1;
$price_grade[$temp_key]['goods_num'] = $val['goods_num'];
$price_grade[$temp_key]['start'] = $row['min'] + round($dx * $val['sn']);
$price_grade[$temp_key]['end'] = $row['min'] + round($dx * ($val['sn'] + 1));
$price_grade[$temp_key]['price_range'] = $price_grade[$temp_key]['start'] . ' - ' . $price_grade[$temp_key]['end'];
$price_grade[$temp_key]['formated_start'] = price_format($price_grade[$temp_key]['start']);
$price_grade[$temp_key]['formated_end'] = price_format($price_grade[$temp_key]['end']);
$price_grade[$temp_key]['url'] = build_uri('category', array('cid'=>$cat_id, 'bid'=>$brand, 'price_min'=>$price_grade[$temp_key]['start'], 'price_max'=>$price_grade[$temp_key]['end'], 'filter_attr'=>$filter_attr_str), $cat['cat_name']);
/* 判æ–ä»·æ ¼åŒºé—´æ˜¯å¦è¢«é€‰ä¸ */
if (isset($_REQUEST['price_min']) && $price_grade[$temp_key]['start'] == $price_min && $price_grade[$temp_key]['end'] == $price_max){
$price_grade[$temp_key]['selected'] = 1;
}
else{
$price_grade[$temp_key]['selected'] = 0;
}
}
$price_grade[0]['start'] = 0;
$price_grade[0]['end'] = 0;
$price_grade[0]['price_range'] = $_LANG['all_attribute'];
$price_grade[0]['url'] = build_uri('category', array('cid'=>$cat_id, 'bid'=>$brand, 'price_min'=>0, 'price_max'=> 0, 'filter_attr'=>$filter_attr_str), $cat['cat_name']);
$price_grade[0]['selected'] = empty($price_max) ? 1 : 0;
$smarty->assign('price_grade', $price_grade);
}
/* å“牌ç›é€?*/
$sql = "SELECT b.brand_id, b.brand_name, COUNT(*) AS goods_num "."FROM " . $GLOBALS['ecs']->table('brand') . "AS b, ".$GLOBALS['ecs']->table('goods') . " AS g LEFT JOIN ". $GLOBALS['ecs']->table('goods_cat') . " AS gc ON g.goods_id = gc.goods_id " ."WHERE g.brand_id = b.brand_id AND ($children OR " . 'gc.cat_id ' . db_create_in(array_unique(array_merge(array($cat_id), array_keys(cat_list($cat_id, 0, false))))) . ") AND b.is_show = 1 " ." AND g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 "."GROUP BY b.brand_id HAVING goods_num > 0 ORDER BY b.sort_order, b.brand_id ASC";
$brands = $GLOBALS['db']->getAll($sql);
foreach ($brands AS $key => $val){
$temp_key = $key + 1;
$brands[$temp_key]['brand_id'] = $val['brand_id']; //åŒæ¥ç»‘定å“牌åç§°å’Œå“牌ID by wang
$brands[$temp_key]['brand_name'] = $val['brand_name'];
$brands[$temp_key]['url'] = build_uri('category', array('cid' => $cat_id, 'bid' => $val['brand_id'], 'price_min'=>$price_min, 'price_max'=> $price_max, 'filter_attr'=> $filter_attr_str), $cat['cat_name']);
/* 判æ–å“牌是å¦è¢«é€‰ä¸ */
if ($brand == $val['brand_id']){ //ä¿®æ£å½“å‰å“牌的ID
$brands[$temp_key]['selected'] = 1;
}
else{
$brands[$temp_key]['selected'] = 0;
}
}
unset($brands[0]); //清空索引�的项�
$brands[0]['brand_id'] = 0; //新增默认�
$brands[0]['brand_name'] = $_LANG['all_attribute'];
$brands[0]['url'] = build_uri('category', array('cid' => $cat_id, 'bid' => 0, 'price_min'=>$price_min, 'price_max'=> $price_max, 'filter_attr'=>$filter_attr_str), $cat['cat_name']);
$brands[0]['selected'] = empty($brand) ? 1 : 0;
ksort($brands); //å¢žåŠ å†æŽ’åº?
$smarty->assign('brands', $brands);
/* 属性ç›é€?*/
$ext = ''; //商哿Ÿ¥è¯¢æ¡ä»¶æ‰©å±•
if ($cat['filter_attr'] > 0){
$cat_filter_attr = explode(',', $cat['filter_attr']); //æå–出æ¤åˆ†ç±»çš„ç›é€‰å±žæ€?
$all_attr_list = array();
foreach ($cat_filter_attr AS $key => $value){
$sql = "SELECT a.attr_name FROM " . $ecs->table('attribute') . " AS a, " . $ecs->table('goods_attr') . " AS ga, " . $ecs->table('goods') . " AS g WHERE ($children OR " . get_extension_goods($children) . ") AND a.attr_id = ga.attr_id AND g.goods_id = ga.goods_id AND g.is_delete = 0 AND g.is_on_sale = 1 AND g.is_alone_sale = 1 AND a.attr_id='$value'";
if($temp_name = $db->getOne($sql)){
$all_attr_list[$key]['filter_attr_id'] = $value; //æ–°å¢žå±žæ€§æ ‡è¯?by wang
$all_attr_list[$key]['filter_attr_name'] = $temp_name;
$sql = "SELECT a.attr_id, MIN(a.goods_attr_id ) AS goods_id, a.attr_value AS attr_value FROM " . $ecs->table('goods_attr') . " AS a, " . $ecs->table('goods') ." AS g" ." WHERE ($children OR " . get_extension_goods($children) . ') AND g.goods_id = a.goods_id AND g.is_delete = 0 AND g.is_on_sale = 1 AND g.is_alone_sale = 1 '.
" AND a.attr_id='$value' ".
" GROUP BY a.attr_value";
$attr_list = $db->getAll($sql);
$temp_arrt_url_arr = array();
for ($i = 0; $i < count($cat_filter_attr); $i++) { //获å–当å‰urlä¸å·²é€‰æ‹©å±žæ€§çš„值,并ä¿ç•™åœ¨æ•°ç»„ä¸?
$temp_arrt_url_arr[$i] = !empty($filter_attr[$i]) ? $filter_attr[$i] : 0;
}
$temp_arrt_url_arr[$key] = 0; //“全部â€çš„ä¿¡æ¯ç”Ÿæˆ
$temp_arrt_url = implode('.', $temp_arrt_url_arr);
$all_attr_list[$key]['attr_list'][0]['attr_id'] = 0; //默认数�by wang
$all_attr_list[$key]['attr_list'][0]['attr_value'] = $_LANG['all_attribute'];
$all_attr_list[$key]['attr_list'][0]['url'] = build_uri('category', array('cid'=>$cat_id, 'bid'=>$brand, 'price_min'=>$price_min, 'price_max'=>$price_max, 'filter_attr'=>$temp_arrt_url), $cat['cat_name']);
$all_attr_list[$key]['attr_list'][0]['selected'] = empty($filter_attr[$key]) ? 1 : 0;
foreach ($attr_list as $k => $v){
$temp_key = $k + 1;
$temp_arrt_url_arr[$key] = $v['goods_id']; //为urlä¸ä»£è¡¨å½“å‰ç›é€‰å±žæ€§çš„ä½ç½®å˜é‡èµ‹å€?并生æˆä»¥â€?’分隔的ç›é€‰å±žæ€§å—符串
$temp_arrt_url = implode('.', $temp_arrt_url_arr);
$all_attr_list[$key]['attr_list'][$temp_key]['attr_id'] = $v['goods_id']; //æ–°å¢žå±žæ€§å‚æ•?by wang
$all_attr_list[$key]['attr_list'][$temp_key]['attr_value'] = $v['attr_value'];
$all_attr_list[$key]['attr_list'][$temp_key]['url'] = build_uri('category', array('cid'=>$cat_id, 'bid'=>$brand, 'price_min'=>$price_min, 'price_max'=>$price_max, 'filter_attr'=>$temp_arrt_url), $cat['cat_name']);
if (!empty($filter_attr[$key]) AND $filter_attr[$key] == $v['goods_id']){
$all_attr_list[$key]['attr_list'][$temp_key]['selected'] = 1;
}
else{
$all_attr_list[$key]['attr_list'][$temp_key]['selected'] = 0;
}
}
}
}
$smarty->assign('filter_attr_list', $all_attr_list);
/* æ‰©å±•å•†å“æŸ¥è¯¢æ¡ä»¶ */
if (!empty($filter_attr)){
$ext_sql = "SELECT DISTINCT(b.goods_id) FROM " . $ecs->table('goods_attr') . " AS a, " . $ecs->table('goods_attr') . " AS b " . "WHERE ";
$ext_group_goods = array();
foreach ($filter_attr AS $k => $v) // æŸ¥å‡ºç¬¦åˆæ‰€æœ‰ç›é€‰å±žæ€§æ¡ä»¶çš„商å“id */
{
if (is_numeric($v) && $v !=0 &&isset($cat_filter_attr[$k])){
$sql = $ext_sql . "b.attr_value = a.attr_value AND b.attr_id = " . $cat_filter_attr[$k] ." AND a.goods_attr_id = " . $v;
$ext_group_goods = $db->getColCached($sql);
$ext .= ' AND ' . db_create_in($ext_group_goods, 'g.goods_id');
}
}
}
}
assign_template('c', array($cat_id));
$position = assign_ur_here($cat_id, $brand_name);
$smarty->assign('page_title', $position['title']); // 页颿 ‡é¢˜
$smarty->assign('ur_here', $position['ur_here']); // 当å‰ä½ç½®
$smarty->assign('shop_notice', $_CFG['shop_notice']); // 商店公告
$smarty->assign('categories1', get_categories(get_parent($cat_id)));
$smarty->assign('parent_id', get_parent($cat_id)); //顶级分类id
$smarty->assign('helps', get_shop_help()); // 网店帮助
$smarty->assign('top_goods', get_top10()); // 销售排�
$smarty->assign('show_marketprice', $_CFG['show_marketprice']);
$smarty->assign('category', $cat_id);
$smarty->assign('brand_id', $brand);
$smarty->assign('price_max', $price_max);
$smarty->assign('price_min', $price_min);
$smarty->assign('filter_attr', $filter_attr_str);
$smarty->assign('feed_url', ($_CFG['rewrite'] == 1) ? "feed-c$cat_id.xml" : 'feed.php?cat=' . $cat_id); // RSS URL
if ($brand > 0){
$arr['all'] = array('brand_id' => 0,
'brand_name' => $GLOBALS['_LANG']['all_goods'],
'brand_logo' => '',
'goods_num' => '',
'url' => build_uri('category', array('cid'=>$cat_id), $cat['cat_name']));
}
else{
$arr = array();
}
$brand_list = array_merge($arr, get_brands($cat_id, 'category'));
$smarty->assign('data_dir', DATA_DIR);
$smarty->assign('brand_list', $brand_list);
$smarty->assign('promotion_info', get_promotion_info());
/* 调查 */
$vote = get_vote();
if (!empty($vote)){
$smarty->assign('vote_id', $vote['id']);
$smarty->assign('vote', $vote['content']);
}
$smarty->assign('best_goods', get_category_recommend_goods('best', $children, $brand, $price_min, $price_max, $ext));
$smarty->assign('promotion_goods', get_category_recommend_goods('promote', $children, $brand, $price_min, $price_max, $ext));
$smarty->assign('hot_goods', get_category_recommend_goods('hot', $children, $brand, $price_min, $price_max, $ext));
$count = get_cagtegory_goods_count($children, $brand, $price_min, $price_max, $ext);
$max_page = ($count> 0) ? ceil($count / $size) : 1;
if ($page > $max_page){
$page = $max_page;
}
/* å¼‚æ¥æ˜¾ç¤ºå•†å“列表 */
if ($_GET['act'] == 'asynclist'){
$asyn_last = intval($_POST['last']) + 1;
$size = $_POST['amount'];
$page = ($asyn_last > 0) ? ceil($asyn_last / $size) : 1;
}
$goodslist = category_get_goods($children, $brand, $price_min, $price_max, $ext, $size, $page, $sort, $order);
if ($_GET['act'] == 'asynclist'){
$sayList = array();
if (is_array($goodslist)){
foreach ($goodslist as $vo){
$shop_price = empty($vo['promote_price']) ? $vo['shop_price']:$vo['promote_price'];
$vedio_img = empty($vo['vedio_img']) ? '':'';
$new_img = empty($vo['new_img']) ? '':' æ–°å“ ';
$best_img = empty($vo['best_img']) ? '':' 推è ';
$hot_img = empty($vo['hot_img']) ? '':' çƒå– ';
$adequate_img = empty($vo['adequate_img']) ? '':' 暂时缺货 ';
$sayList[] = array(
'pro-inner' => '