<?php

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

define('IN_ECS', true);

require('includes/init.php');
$exc = new exchange($ecs->table("category"), $db, 'cat_id', 'cat_name');

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

/*------------------------------------------------------ */
//-- 商品分类列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
    /* 获取分类列表 */
    $cat_list = cat_list(0, 0, false);

    /* 模板赋值 */
    $smarty->assign('ur_here',      $_LANG['03_category_list']);
    $smarty->assign('action_link',  array('href' => 'category.php?act=add', 'text' => $_LANG['04_category_add']));
    $smarty->assign('full_page',    1);

    $smarty->assign('cat_info',     $cat_list);

    /* 列表页面 */
    assign_query_info();
    $smarty->display('category_list.htm');
}

/*------------------------------------------------------ */
//-- 排序、分页、查询
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
    $cat_list = cat_list(0, 0, false);
    $smarty->assign('cat_info',     $cat_list);

    make_json_result($smarty->fetch('category_list.htm'));
}
/*------------------------------------------------------ */
//-- 添加商品分类
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'add')
{
    /* 权限检查 */
    admin_priv('cat_manage');

    /* 模板赋值 */
    $smarty->assign('ur_here',     $_LANG['04_category_add']);
    $smarty->assign('action_link', array('href' => 'category.php?act=list', 'text' => $_LANG['03_category_list']));

    $smarty->assign('cat_select',  cat_list(0, 0, true));
    $smarty->assign('form_act',    'insert');

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

/*------------------------------------------------------ */
//-- 商品分类添加时的处理
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'insert')
{
    /* 权限检查 */
    admin_priv('cat_manage');

    /* 初始化变量 */
    $cat_id       = !empty($_POST['cat_id'])       ? intval($_POST['cat_id'])     : 0;
    $parent_id    = !empty($_POST['parent_id'])    ? intval($_POST['parent_id'])  : 0;
    $sort_order   = !empty($_POST['sort_order'])   ? intval($_POST['sort_order']) : 0;
    $keywords     = !empty($_POST['keywords'])     ? trim($_POST['keywords'])     : '';
    $cat_desc     = !empty($_POST['cat_desc'])     ? $_POST['cat_desc']           : '';
    $measure_unit = !empty($_POST['measure_unit']) ? trim($_POST['measure_unit']) : '';
    $cat_name     = !empty($_POST['cat_name'])     ? trim($_POST['cat_name'])     : '';

    /* 同级别下不能有重复的分类名称 */
    $sql = "SELECT COUNT(*) FROM " .$ecs->table('category').
           " WHERE parent_id = '$parent_id' AND cat_name = '$cat_name'";
    if ($db->getOne($sql) > 0)
    {
       $link[] = array('text' => $_LANG['go_back'], 'href' => 'javascript:history.back(-1)');
       sys_msg($_LANG['catname_exist'], 0, $link);
    }

    /* 入库的操作 */
    $sql = "INSERT INTO " .$ecs->table('category'). " (cat_name, keywords, cat_desc, parent_id, sort_order, is_leaf, template_file, measure_unit, show_in_nav) ".
            "VALUES ('$cat_name', '$keywords', '$cat_desc', '$parent_id', '$sort_order', '$_POST[is_leaf]', '', '$measure_unit', '$_POST[show_in_nav]')";
    if ($db->query($sql))
    {
        /* 记录管理员操作 */
        admin_log($_POST['cat_name'], 'add', 'category');

        /* 清除缓存 */
        clear_cache_files();

        /*添加链接*/
        $link[0]['text'] = $_LANG['continue_add'];
        $link[0]['href'] = 'category.php?act=add';

        $link[1]['text'] = $_LANG['back_list'];
        $link[1]['href'] = 'category.php?act=list';

        sys_msg($_LANG['catadd_succed'],0, $link);
    }
 }

/*------------------------------------------------------ */
//-- 编辑商品分类信息
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'edit')
{
    /* 权限检查 */
    admin_priv('cat_manage');

    /* 查询分类信息数据 */
    $sql = "SELECT cat_id,cat_name,keywords,cat_desc,parent_id,sort_order,is_leaf,measure_unit,show_in_nav ".
           "FROM " .$ecs->table('category'). " WHERE cat_id='$_REQUEST[cat_id]'";
    $cat_info = $db->getRow($sql);

    /* 模板赋值 */
    $smarty->assign('ur_here',     $_LANG['category_edit']);
    $smarty->assign('action_link', array('text' => $_LANG['03_category_list'], 'href' => 'category.php?act=list'));

    $smarty->assign('cat_info',    $cat_info);
    $smarty->assign('form_act',    'update');
    $smarty->assign('cat_select',  cat_list(0, $cat_info['parent_id'], true));

    /* 显示页面 */
    assign_query_info();
    $smarty->display('category_info.htm');
}
/*------------------------------------------------------ */
//-- 编辑商品分类信息
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'update')
{
    /* 权限检查 */
    admin_priv('cat_manage');

    /* 初始化变量 */
    $cat_id       = !empty($_POST['cat_id'])       ? intval($_POST['cat_id'])     : 0;
    $parent_id    = !empty($_POST['parent_id'])    ? intval($_POST['parent_id'])  : 0;
    $sort_order   = !empty($_POST['sort_order'])   ? intval($_POST['sort_order']) : 0;
    $keywords     = !empty($_POST['keywords'])     ? trim($_POST['keywords'])     : '';
    $cat_desc     = !empty($_POST['cat_desc'])     ? $_POST['cat_desc']           : '';
    $measure_unit = !empty($_POST['measure_unit']) ? trim($_POST['measure_unit']) : '';
    $cat_name     = !empty($_POST['cat_name'])     ? trim($_POST['cat_name'])     : '';

    /* 如果是底级分类,数量单位不显示 */
    $measure_unit = ($_REQUEST['is_leaf'] == '0') ? '' : trim($_REQUEST['measure_unit']);

    if ($_REQUEST['is_leaf'] == '1')
    {
        /* 如果当前分类底下有子分类时不能修改为底级 */
        $sql = "SELECT COUNT(*) FROM " .$ecs->table('category'). " WHERE parent_id = '$cat_id'";
        if ($db->getOne($sql) > 0)
        {
           $link[] = array('text' => $_LANG['go_back'], 'href' => 'javascript:history.back(-1)');
           sys_msg($_POST['cat_name'] . "&nbsp;" . $_LANG['cat_noleaf'], 0, $link);
        }
    }
    else
    {
        /* 当前分类下是否存在商品,有商品时不能改为非底级 */
        $sql = "SELECT COUNT(*) FROM " .$ecs->table('goods'). " WHERE cat_id = '$cat_id'";
        if ($db->getOne($sql) > 0)
        {
             $link[] = array('text' => $_LANG['go_back'], 'href' => 'javascript:history.back(-1)');
             sys_msg($_LANG["cat_is_goods"], 0, $link);
        }
    }

    /* 如果当前分类的上级分类是它本身时 */
    if ($cat_id == $parent_id)
    {
       $link[] = array('text' => $_LANG['go_back'], 'href' => 'javascript:history.back(-1)');
       sys_msg($_LANG["cat_leaf_same"], 0, $link);
    }

    /* 上级分类不能是当前分类的下级分类 */
    if ($parent_id > 0)
    {
        $sql = "SELECT parent_id FROM " .$ecs->table('category'). " WHERE cat_id = '$parent_id'";
        $cat_parent_id = $db->getOne($sql);
        if ($cat_parent_id == $cat_id)
        {
           $link[] = array('text' => $_LANG['go_back'], 'href' => 'javascript:history.back(-1)');
           sys_msg($_LANG["is_leaf_error"], 0, $link);
        }
    }

    /* 更新数据信息 */
    $sql = "UPDATE " .$ecs->table('category'). " SET ".
               "cat_name     = '$cat_name', ".
               "keywords     = '$keywords', ".
               "cat_desc     = '$cat_desc', ".
               "parent_id    = '$parent_id', ".
               "is_leaf      = '$_REQUEST[is_leaf]', ".
               "measure_unit = '$measure_unit', ".
               "sort_order   = '$sort_order', ".
               "show_in_nav  = '$_POST[show_in_nav]' ".
               "WHERE cat_id = '$cat_id'";

    if ($db->query($sql))
    {
        /* 清除缓存 */
        clear_cache_files();

        /* 记录管理员操作 */
        admin_log($_POST['cat_name'], 'edit', 'category');

        /* 提示信息 */
        $link[] = array('text' => $_LANG['back_list'], 'href' => 'category.php?act=list');
        sys_msg($_LANG['catedit_succed'], 0, $link);
    }
}

/*------------------------------------------------------ */
//-- 批量转移商品分类页面
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'move')
{
    /* 权限检查 */
    admin_priv('cat_drop');

    $cat_id = !empty($_REQUEST['cat_id']) ? intval($_REQUEST['cat_id']) : 0;

    /* 模板赋值 */
    $smarty->assign('ur_here',     $_LANG['move_goods']);
    $smarty->assign('action_link', array('href' => 'category.php?act=list', 'text' => $_LANG['03_category_list']));

    $smarty->assign('cat_select', cat_list(0, $cat_id, true));
    $smarty->assign('form_act',   'move_cat');

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

/*------------------------------------------------------ */
//-- 处理批量转移商品分类的处理程序
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'move_cat')
{
    /* 权限检查 */
    admin_priv('cat_drop');

    $cat_id        = !empty($_POST['cat_id'])        ? intval($_POST['cat_id'])        : 0;
    $target_cat_id = !empty($_POST['target_cat_id']) ? intval($_POST['target_cat_id']) : 0;

    /* 商品分类不允许为空 */
    if ($cat_id == 0 || $target_cat_id == 0)
    {
        $link[] = array('text' => $_LANG['go_back'], 'href' => 'category.php?act=move');
        sys_msg($_LANG['cat_move_empty'], 0, $link);
    }

    /* 更新商品分类 */
    $sql = "UPDATE " .$ecs->table('goods'). " SET cat_id = '$target_cat_id' ".
           "WHERE cat_id = '$cat_id'";
    if ($db->query($sql))
    {
        /* 清除缓存 */
        clear_cache_files();

        /* 提示信息 */
        $link[] = array('text' => $_LANG['go_back'], 'href' => 'category.php?act=list');
        sys_msg($_LANG['move_cat_success'], 0, $link);
    }
}

/*------------------------------------------------------ */
//-- 编辑排序序号
/*------------------------------------------------------ */

if ($_REQUEST['act'] == 'edit_sort_order')
{
    check_authz_json('cat_manage');

    $id = intval($_POST['id']);
    $val = intval($_POST['val']);

    $sql = "UPDATE " .$ecs->table('category'). " SET sort_order = '$val' WHERE cat_id = '$id'";
    if ($db->query($sql, 'SILENT'))
    {
        /* 清除缓存 */
        clear_cache_files();

        make_json_result($val);
    }
    else
    {
        make_json_error($db->error());
    }
}

/*------------------------------------------------------ */
//-- 切换是否显示在导航栏
/*------------------------------------------------------ */

if ($_REQUEST['act'] == 'toggle_show_in_nav')
{
    check_authz_json('cat_manage');

    $id = intval($_POST['id']);
    $val = intval($_POST['val']);

    $sql = "UPDATE " . $ecs->table('category') . " SET show_in_nav = '$val' ".
            "WHERE cat_id = '$id' LIMIT 1";
    if ($db->query($sql))
    {
        clear_cache_files();
        make_json_result($val);
    }
    else
    {
        make_json_error($db->error());
    }
}

/*------------------------------------------------------ */
//-- 删除商品分类
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'remove')
{
    check_authz_json('cat_manage');

    /* 初始化分类ID并取得分类名称 */
    $cat_id   = intval($_GET['id']);
    $cat_name = $db->getOne('SELECT cat_name FROM ' .$ecs->table('category'). " WHERE cat_id='$cat_id'");

    /* 当前分类下是否有子分类 */
    $cat_count = $db->getOne('SELECT COUNT(*) FROM ' .$ecs->table('category'). " WHERE parent_id='$cat_id'");

    /* 当前分类下是否存在商品 */
    $goods_count = $db->getOne('SELECT COUNT(*) FROM ' .$ecs->table('goods'). " WHERE cat_id='$cat_id'");

    /* 如果不存在下级子分类和商品，则删除之 */
    if ($cat_count == 0 && $goods_count == 0)
    {
        /* 删除分类 */
        $sql = 'DELETE FROM ' .$ecs->table('category'). " WHERE cat_id = '$cat_id'";
        if ($db->query($sql))
        {
            clear_cache_files();
            admin_log($cat_name, 'remove', 'category');
        }
    }
    else
    {
        make_json_error($cat_name .' '. $_LANG['cat_isleaf']);
    }

    $url = 'category.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);

    header("Location: $url\n");
    exit;
}

?>