<?php
/**
* 系统工具
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
* @version $Id: tools.inc.php 21 2008-10-21 3:35 $
*/
(!defined('IN_ADMIN') || !defined('IN_MUDDER')) && exit('Access Denied');
if(!check_submit('toolssubmit')) {
    if($op == 'rebuild') {
        $tplname = 'rebuild';
    } elseif($op == 'batchpm') {

        if($ucenter_enable) {
            require_once(MUDDER_ROOT."./member/model/message_uc.php");
        } else {
            require_once(MUDDER_ROOT."./member/model/message.php");
        }

        $groups = $db->get_all("SELECT * FROM {$dbpre}usergroups ORDER BY groupid");
        foreach($groups as $group) {
            $selected = ($job == 'edituser' && $group['groupid'] == $member['groupid']) ? ' selected="selected"' : '';
            $group_opt .= '<option value="'.$group['groupid'].'"'.$selected.'>'.$group['groupname'].'</option>'."\n";
        }
        if($search && is_array($search)) {
            $where = array();
            if($search['username']) {
                if(strposex($search['username'], ',')) {
                    $search['username'] = str_replace(",", "','" ,$search['username']);
                    $where_add[] = "username in ('{$search['username']}')";
                } else {
                    $where_add[] = "username like '%{$search['username']}%'";
                }
            }
            if($search['groupid'] && is_numeric($search['groupid'])) {
                $where_add[] = "groupid='{$search['groupid']}'";
            }
            $where = $where_add ? "WHERE " . implode(',', $where_add) : "WHERE uid<>'{$user->uid}'";
            $total = $db->get_one("SELECT COUNT(*) AS count FROM {$dbpre}members $where");
            if($batchsubmit) {
                if(!($batch['subject'] = trim($batch['subject'])) || !($batch['message'] = trim($batch['message']))) {
                    cpmsg('未填写通知主题或内容，请返回修改填写。');
                } else {
                    $batchmsg = add_slashes( serialize(array('subject'=>$batch['subject'],'message'=>$batch['message'])));
                    $db->query("REPLACE INTO {$dbpre}config (variable, value) VALUES ('batchmsg','$batchmsg')");
                    $batch['current'] = 0;
                    header("Location: admincp.php?action=$action&file=$file&op=$op&sendsubmit=yes&search%5Busername%5D=".rawurlencode($search['username'])."&search%5Bgroupid%5D=".$search['groupid']."&batch%5Bpertask%5D=".$batch['pertask']."&batch%5Bcurrent%5D=".$batch['current']);
                }
            } elseif($sendsubmit=='yes') {
                $batchmsg = $db->get_one("SELECT value FROM {$dbpre}config WHERE variable='batchmsg'");
                $batchmsg = $batchmsg ? unserialize($batchmsg['value']) : array();
                if($batchmsg['subject'] && $batchmsg['message']) {
                    $batch['pertask'] = @intval($batch['pertask']);
                    $batch['current'] = @intval($batch['current']);
                    $query = $db->query("SELECT uid,username FROM {$dbpre}members $where LIMIT {$batch['current']}, {$batch['pertask']}");
                    if($db->num_rows($query)>0) {
                        $recvuids = array();
                        while($row = $db->fetch_array($query)) {
                            $recvuids[]=$row['uid'];
                        }
                        $db->free_result($query);
                        if(count($recvuids)>0) MessageModel::send(0, implode(',',$recvuids), $batchmsg['subject'], $batchmsg['message']);
                        cpmsg("正在发送...".$batch['current'].' 到 '.($batch['current']+$batch['pertask']),"admincp.php?action=$action&file=$file&op=$op&sendsubmit=yes&search%5Busername%5D=".rawurlencode($search['username'])."&search%5Bgroupid%5D=".$search['groupid']."&batch%5Bpertask%5D=".$batch['pertask']."&batch%5Bcurrent%5D=".($batch['current'] + $batch['pertask']));
                    } else {
                        cpmsg("网站通知发送成功。","admincp.php?action=$action&file=$file&op=batchpm");
                    }
                } else {
                    cpmsg("未填写通知主题或内容，请返回修改填写。");
                }
            }
        }
        $tplname = 'batchpm';
    
    } else {
        $op = 'updatecache';
        $tplname = 'updatecache';
    }

} else {

    if($op == 'rebuild') {
        
        empty($rebuild) && cpmsg('未选择重建数据项，请返回选择。');
        if($rebuild == 'classtotal') {
            $classes = $db->get_all("SELECT classcode FROM {$dbpre}classcode WHERE classsort=2");
            if($classes) foreach($classes as $class) {
                unset($row);
                $classcode = $class['classcode'];
                $row = $db->get_one("SELECT COUNT(*) as total FROM {$dbpre}shops WHERE classcode='$classcode' AND status<>2");
                $total = $row['total'];
                $db->query("UPDATE {$dbpre}classcode SET total='$total' WHERE classcode='$classcode'");
            }
        }
        cpmsg('数据重建完毕！','admincp.php?action=modoer&file=tools&op=rebuild');

    } elseif($op == 'updatecache') {

        if($static_cache) foreach($static_cache as $flag) {
            cache($flag);
        }
        //if($dynamic_cache) foreach($dynamic_cache as $flag) {
        //    $cacheclass->Write($flag);
        //}
        if($templates_cache) foreach($templates_cache as $flag) {
            deleteTemplateCache($flag);
        }
        cpmsg('更新完毕。');

    } else {

        cpmsg('未定义操作。');

    }
}
include cptpl('tools_'.$tplname);
?>
