<?php
/**
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
* @version $Id 3 2008-08-25 18:35 $
*/
!defined('IN_MUDDER') && exit('Access Denied');

if($in_ajax) {

    if($op == 'add') {
        $caption = '添加好友';
        if(!$user->isLogin) dialog($caption, '', 'login');
        if(!is_numeric($friendid) || $friendid<0) dialog($caption, '您添加的好友不存在。');
        
        if($friendid == $user->uid) {
            dialog($caption, '您不能添加自己为好友。');
        }

        $exists = $db->get_value("SELECT COUNT(*) FROM {$dbpre}friends WHERE uid='{$user->uid}' AND fuid='$friendid'") > 0;
        $exists && dialog($caption, '您已经将该会员加为好友了，无需再次添加。');

        $friend = $db->get_one("SELECT uid,username,groupid FROM {$dbpre}members WHERE uid='$friendid'");
        if(!$friend) {
            dialog($caption, '该会员不存在，无法加为好友。');
        } elseif(in_array($friend['groupid'], array('1','2','3'))) {
            dialog($caption, '无法添加被系统限制会员为好友。');
        }

        require_once template('member_dlg_addfriend');
        output();

    } else {
        dialog('系统提示','未定义的操作。');
    }

} elseif(check_submit('addfriendsubmit')) {

    if(!function_exists('joinFriend')) {
        require_once(MUDDER_ROOT.'./member/include/fun_member.php');
    }
    echo joinFriend($user, $friendid, $message);
    output();

} elseif(!check_submit('mysubmit')) {

    if($ucenter_enable) {
        $data = $user->Get_Friendlist($page, $offset);
        $total = $data['total'];
        $friends = $data['result'];
        $total = !$total ? 0 : $total;
    } else {
        $row = $db->get_one("SELECT COUNT(*) as c FROM {$dbpre}friends WHERE `uid`='{$user->uid}'");
        $total = $row['c'];
        if($total > 0) {
            $friends = $db->get_all("SELECT a.fid, FROM_UNIXTIME(a.addtime,'%Y-%m-%d %H:%i:%s') as addtime, b.uid, b.username,b.point,b.reviews, FROM_UNIXTIME(b.logintime,'%Y-%m-%d %H:%i:%s') as logintime FROM {$dbpre}friends a LEFT JOIN {$dbpre}members b ON(a.fuid=b.uid) WHERE a.uid='{$user->uid}' ORDER BY addtime LIMIT $limit");
        }
    }
    $total > 0 && $multipage = multi($total, $offset, $page, "index.php?ac=$ac");

} else {

    if(!is_array($friendIds)||count($friendIds)<=0){
        redirect('未选择，请返回修改，至少选择一项。');
    } else {
        foreach($friendIds as $friendId){
            $db->update("DELETE FROM {$dbpre}friends WHERE fid='$friendId' AND uid='{$user->uid}'");
        }
        if($ucenter_enable) {
            $user->Delete_Friend($friendIds);
            foreach($friendIds as $friendId){
                $db->update("DELETE FROM {$dbpre}friends WHERE fuid='$friendId' AND uid='{$user->uid}'");
            }
        }
        redirect('删除完毕！',"index.php?ac=$ac");
    }

}
?>