<?php
/**
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
* @version $Id lis.inc.php 2 2008-08-24 11:39 $
*/
(!defined('IN_ADMIN') || !defined('IN_MUDDER')) && exit('Access Denied');

if(!$dosubmit) {

    $groups = $db->get_all("SELECT * FROM {$dbpre}usergroups ORDER BY groupid");
    if($job == 'edituser') {
        if(!is_numeric($uid) || $uid < 1) cpmsg('UID不正确。');
        if($admin->admintype != 1) cpmsg('权限不足。');
        $tplname = 'job.edituser';
        $uid = @intval($uid);
        $member = $db->get_one("SELECT username,email,point,coin,flowers,groupid FROM {$dbpre}members a WHERE uid='$uid'");
        if(!$member) cpmsg('用户不存在。');
    } else {
        $where = array();
        if($search && is_array($search)) {
           if($search['username']) {
               $where[] = "username like '%{$search['username']}%'";
           }
           if(intval($search['groupid']) > 0) {
               $where[] = "a.groupid='".@intval($search['groupid'])."'";
           }
        }
        $where = $where && is_array($where) ? implode(' AND ', $where) : ' 1=1 ';
        $result = $db->get_one("SELECT COUNT(*) as c FROM {$dbpre}members a LEFT JOIN {$dbpre}usergroups b ON(a.groupid=b.groupid) WHERE $where");
        $total = &$result['c'];
        if($total > 0) {
            $offset = 15;
            $limit = get_limit($page, $offset);
            $query = $db->query("SELECT a.*,b.groupname FROM {$dbpre}members a LEFT JOIN {$dbpre}usergroups b ON(a.groupid=b.groupid) WHERE $where LIMIT $limit");
            while($member = $db->fetch_array($query)) {
                $member['logintime'] = date('Y-m-d H:i',$member['logintime']);
                $members[] = $member;
            }
            $multiurl = 'admincp.php?action='.$action.'&file='.$file.'&search%5Busername%5D='.rawurlencode($search['username']).'&search%5Bgroupid%5D='.intval($search['groupid']);
            $multipage = multi($total, $offset, $page, $multiurl);
        }
    }
    foreach($groups as $group) {
        $selected = ($job == 'edituser' && $group['groupid'] == $member['groupid']) ? ' selected="selected"' : '';
        $group_opt .= '<option value="'.$group['groupid'].'"'.$selected.'>'.$group['groupname'].'</option>'."\n";
        
    }

    include cptpl('list', MOD_ROOT.'./admin/templates/');

} else {

    $update = array();
    if($job == 'edituser' && $memberupdate && is_array($memberupdate)) {
        if(!is_numeric($uid) || $uid < 1) cpmsg('UID不正确。');
        if($admin->admintype != 1) cpmsg('权限不足。','admincp.php?action=logout');
        $memberupdate['groupid'] = @intval($memberupdate['groupid']);
        $memberupdate['groupid'] = $memberupdate['groupid']<1 ? 10 : $memberupdate['groupid'];
        $update[] = "groupid='{$memberupdate['groupid']}'";
        if(!isemail($memberupdate['email'])) cpmsg('E-mail格式不正确，请返回修改。');
        $update[] = "email='{$memberupdate['email']}'";
        if(!is_numeric($memberupdate['point'])||$memberupdate['point']<0) cpmsg('积分格式不正确，请返回修改。');
        $update[] = "point='{$memberupdate['point']}'";
        if(!is_numeric($memberupdate['coin'])||$memberupdate['coin']<0) cpmsg('金币格式不正确，请返回修改。');
        $update[] = "coin='{$memberupdate['coin']}'";
        if(!is_numeric($memberupdate['flowers'])||$memberupdate['flowers']<0) cpmsg('鲜花量不正确，请返回修改。');
        $update[] = "flowers='{$memberupdate['flowers']}'";
        if($memberupdate['password']) {
            if(strlen($memberupdate['password']) < 6) {
                cpmsg('密码不能小于6位数，请返回修改。');
            } elseif($memberupdate['password'] != $memberupdate['repassword']) {
                cpmsg('2次输入的密码不一致，请返回修改。');
            } else {
                $update[] = "password='".md5($memberupdate['password'])."'";
            }
        }
        if($update && is_array($update)) {
            $update = implode(", ", $update);
            $db->query("UPDATE {$dbpre}members SET $update WHERE uid='$uid'");
        }
    } else {
        if($uid && is_array($uid)) foreach($uid as $val) {
            if(!is_numeric($val) || $val < 0) cpmsg('UID不正确。');
            $db->query("DELETE FROM {$dbpre}members WHERE uid='$val'");
            $db->query("DELETE FROM {$dbpre}spaces WHERE uid='$val'");
        }
    }
    unset($memberupdate, $update, $uid);
    cpmsg('用户更新完毕。');

}
?>