<?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: user_msg.php 8272 2007-04-19 10:11:35Z paulgao $
*/

define('IN_ECS', true);

require('includes/init.php');

/*初始化数据交换对象 */
$exc = new exchange($ecs->table("feedback"), $db, 'msg_id', 'msg_title');

/*------------------------------------------------------ */
//-- 列出所有留言
/*------------------------------------------------------ */
if ($_REQUEST['act']=='list_all')
{
    assign_query_info();
    $msg_list = msg_list();

    $smarty->assign('msg_list',     $msg_list['msg_list']);
    $smarty->assign('filter',       $msg_list['filter']);
    $smarty->assign('record_count', $msg_list['record_count']);
    $smarty->assign('page_count',   $msg_list['page_count']);
    $smarty->assign('full_page',    1);
    $smarty->assign('sort_msg_id', '<img src="images/sort_desc.gif">');

    $smarty->assign('ur_here',      $_LANG['list_all']);
    $smarty->assign('full_page',    1);
    $smarty->display('msg_list.htm');
}

/*------------------------------------------------------ */
//-- ajax显示留言列表
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
    $msg_list = msg_list();

    $smarty->assign('msg_list',     $msg_list['msg_list']);
    $smarty->assign('filter',       $msg_list['filter']);
    $smarty->assign('record_count', $msg_list['record_count']);
    $smarty->assign('page_count',   $msg_list['page_count']);

    $sort_flag  = sort_flag($msg_list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    make_json_result($smarty->fetch('msg_list.htm'), '', array('filter' => $msg_list['filter'], 'page_count' => $msg_list['page_count']));
}
/*------------------------------------------------------ */
//-- ajax 删除留言
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'remove')
{
    $msg_id = intval($_REQUEST['id']);

    /* 检查权限 */
    check_authz_json('feedback_priv');

    $msg_title = $exc->get_name($msg_id);
    $img = $exc->get_name($msg_id, 'message_img');
    if ($exc->drop($msg_id))
    {
        /* 删除图片 */
        if (!empty($img))
        {
             @unlink(ROOT_PATH.'data/feedbackimg/'.$img);
        }
        $sql = "DELETE FROM " . $ecs->table('feedback') . " WHERE parend_id = '$msg_id' LIMIT 1";
        $db->query($sql, 'SILENT');

        admin_log(addslashes($msg_title), 'remove', 'message');
        $url = 'user_msg.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);
        header("Location: $url\n");
        exit;
    }
    else
    {
        make_json_error($GLOBALS['db']->error());
    }
}
/*------------------------------------------------------ */
//-- 回复留言
/*------------------------------------------------------ */
elseif ($_REQUEST['act']=='view')
{
    /* 权限判断 */
    admin_priv('feedback_priv');

    if (empty($_REQUEST['reply']))
    {
        $sql = "SELECT * FROM ".$ecs->table('feedback'). " WHERE msg_id ='".$_REQUEST['id']."'";
        $msg = $db->GetRow($sql);
        if ($msg)
        {
            $msg['msg_time'] = date($_CFG['time_format'], $msg['msg_time']);
        }
    }
    else
    {
        $sql = "SELECT T1.*, T2.msg_id AS reply_id, T2.user_name  AS reply_name,  T2.msg_content AS reply_content , T2.msg_time AS reply_time, T2.user_name AS reply_name FROM ".$ecs->table('feedback'). " AS T1 , ".$ecs->table('feedback'). " AS T2 WHERE T1.msg_id = T2.parend_id AND T1.msg_id = '".$_REQUEST['id']."'";
        $msg = $db->GetRow($sql);
        if ($msg)
        {
            $msg['msg_time']   = date($_CFG['time_format'], $msg['msg_time']);
            $msg['reply_time'] = date($_CFG['time_format'], $msg['reply_time']);
        }
    }

    $sql = "SELECT email FROM ". $ecs->table('admin_user'). " WHERE user_id ='".$_SESSION['admin_id']."'";
    $msg['reply_email'] = $db->GetOne($sql);

    assign_query_info();
    $smarty->assign('msg',         $msg);
    $smarty->assign('ur_here',     $_LANG['reply']);
    $smarty->assign('action_link', array('text' => $_LANG['08_unreply_msg'], 'href'=>'user_msg.php?act=list_all'));

    assign_query_info();
    $smarty->display('msg_info.htm');
}
elseif ($_REQUEST['act']=='action')
{
    /* 权限判断 */
    admin_priv('feedback_priv');

    if (empty($_REQUEST['parend_id']))
    {
        $sql = "INSERT INTO ".$ecs->table('feedback')." (msg_title, msg_time, user_id, user_name ,user_email, parend_id, msg_content) VALUES('reply', '".time()."', '".$_SESSION['admin_id']."', '".$_SESSION['admin_name']."', '".$_POST['user_email']."', '".$_REQUEST['msg_id']."', '".$_POST['msg_content']."') ";
        $db->query($sql);
        $sql = "UPDATE ".$ecs->table('feedback'). " SET reply = '1' WHERE msg_id ='".$_REQUEST['msg_id']."'";
        $db->query($sql);
        header("Location: ?act=view&id=".$_REQUEST['msg_id']."&reply=1\n");
        exit;

    }
    else
    {
        $sql = "UPDATE ".$ecs->table('feedback')." SET user_email = '".$_POST['user_email']."', msg_content='".$_POST['msg_content']."', msg_time = '".time()."' WHERE msg_id = '".$_REQUEST['parend_id']."'";
        $db->query($sql);
        header("Location: ?act=view&id=".$_REQUEST['msg_id']."&reply=1\n");
        exit;
    }
}

/*------------------------------------------------------ */
//-- 删除会员上传的文件
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'drop_file')
{
    /* 权限判断 */
    admin_priv('feedback_priv');

    /* 删除上传的文件 */
    $file = $_GET['file'];
    @unlink('../data/feedbackimg/'.$file);

    /* 更新数据库 */
    $db->query("UPDATE ".$ecs->table('feedback')." SET message_img = '' WHERE msg_id = '$_GET[id]'");

    header("Location: user_msg.php?act=view&id=".$_GET['id']."\n");
    exit;
}

/**
 *
 *
 * @access  public
 * @param
 *
 * @return void
 */
function msg_list()
{
    /* 过滤条件 */
    $filter['keywords']   = empty($_REQUEST['keywords']) ? '' : trim($_REQUEST['keywords']);
    $filter['msg_type']   = isset($_REQUEST['msg_type']) ? intval($_REQUEST['msg_type']) : -1;
    $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'msg_id' : trim($_REQUEST['sort_by']);
    $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

    $where = '';
    if ($filter['keywords'])
    {
        $where .= " AND msg_title LIKE '%" . mysql_like_quote($filter['keywords']) . "%' ";
    }
    if ($filter['msg_type'] != -1)
    {
        $where .= " AND msg_type = '$filter[msg_type]' ";
    }

    $sql = "SELECT count(*) FROM " .$GLOBALS['ecs']->table('feedback').
           " WHERE parend_id = '0' " . $where;
    $filter['record_count'] = $GLOBALS['db']->getOne($sql);

    /* 分页大小 */
    $filter = page_and_size($filter);

    $sql = "SELECT msg_id, user_name, msg_title,msg_type,  msg_time, reply" .
           " FROM " . $GLOBALS['ecs']->table('feedback') .
           " WHERE parend_id = 0 " . $where .
           " ORDER by " . $filter['sort_by'] . ' ' . $filter['sort_order'] .
           " LIMIT " . $filter['start'] . ', ' . $filter['page_size'];

    $msg_list = $GLOBALS['db']->getAll($sql);
    foreach ($msg_list AS $key => $value)
    {
        $msg_list[$key]['msg_time'] = date($GLOBALS['_CFG']['time_format'], $value['msg_time']);
        $msg_list[$key]['msg_type'] = $GLOBALS['_LANG']['type'][$value['msg_type']];
    }
    $filter['keywords'] = stripslashes($filter['keywords']);
    $arr = array('msg_list' => $msg_list, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);

    return $arr;
}

?>