<?php
/**
* 网站管理
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
* @version $Id: class.inc.php 3 2007-12-28 09:52 $
*/

(!defined('IN_ADMIN') || !defined('IN_MUDDER')) && exit('Access Denied');

if(!check_submit('areasubmit')) {

    $tplname = 'editarea';
    if($areacode || $areasort) {
        if($areacode) {
            $area = $db->get_one("SELECT * FROM {$dbpre}areacode WHERE areacode='$areacode'");
            if(!$area) cpmsg('所编辑的地区不存在。');
            $iscity = strlen($areacode) == 2;
        } else {
            if($areasort < 1 || $areasort > 3) cpmsg('未知的对象参数。');
        }
    } elseif($delete == 'confirm') {
        $total = $db->get_one("SELECT COUNT(*) as count FROM {$dbpre}areacode WHERE areacode like '{$deleteareacode}__%'");
        if($total['count'] > 0) cpmsg('下级地区不为空，请返回删除下级地区。');
    } else {
        $query = $db->query("SELECT * FROM {$dbpre}areacode");
        while($area = $db->fetch_array($query)) {
            if($area['areasort'] == 1)
                $areas['city'][] = $area;
            elseif($area['areasort'] == 2)
                $areas['borough'][substr($area['areacode'],0,2)][] = $area;
            elseif($area['areasort'] == 3)
                $areas['street'][substr($area['areacode'],0,4)][] = $area;
        }
    }
    include cptpl('area');

} else {

    $areasort = @intval($areasort);
    if($delete == 'yes') {
        if(!$deleteareacode) cpmsg('未指明需要删除地区。');
        $db->query("DELETE FROM {$dbpre}areacode WHERE areacode='$deleteareacode'");
        cache_areacode();
        cache('panels');
        cpmsg("指定的地区删除完毕。");
    } else {
        if($areacode && $areaname) {

            $default_mappoint = trim($default_mappoint);
            /**
            if(strlen($areacode) == 2) {
                $default_mappoint = strtolower(trim($default_mappoint));
                if(!preg_match('/^[A-Za-z0-9\.\-]+,[A-Za-z0-9\.\-]+$/', $default_mappoint)) {
                    cpmsg('未设置地图默认位置，请返回修改。');
                }
            } else {
                $default_mappoint = '';
            }
            */
            $areaname = html_special_chars_ex($areaname);
            $db->update("UPDATE {$dbpre}areacode SET areaname='$areaname',default_mappoint='$default_mappoint' WHERE areacode='$areacode'");
            cache('areacode');
            cache('panels');
            cpmsg("地区更新完毕。");
        } elseif($areasort && $areaname) {
            if($areasort < 1 || $areasort > 3) cpmsg('未知的地区类型。');
            if($areasort != 1 && !$upareacode) cpmsg('未指定上一级地区。');
            $areacode_list = $db->get_all("SELECT areacode FROM {$dbpre}areacode WHERE areasort='$areasort' AND areacode like '$upareacode%'");
            $areacode_arr = array();
            if($areacode_list) {
                foreach($areacode_list as $code)
                    $areacode_arr[] = $code['areacode']; 
            }
            if($areasort == 1) {
                $newareacode = 10;
                while(in_array($newareacode,$areacode_arr)) $newareacode++;
            } else {
                if(strlen($upareacode) < ($areasort - 1) * 2) cpmsg("未知的上一级地区。");
                $p_areacode = substr($upareacode, 0, ($areasort - 1) * 2);
                $newareacode = 1;
                while(in_array($p_areacode.sprintf("%02d", $newareacode), $areacode_arr)) 
                    $newareacode++;
                $newareacode = $p_areacode.sprintf("%02d", $newareacode);
            }
            $db->query("INSERT INTO {$dbpre}areacode (areaname,areacode,areasort,default_mappoint) VALUES('$areaname','$newareacode','$areasort','$default_mappoint')");
            cache('areacode');
            cache('panels');
            cpmsg('地区添加完毕。');
        } elseif(!$areaname) {
            cpmsg('未填写地区名称，请返回修改。');
        } else {
            cpmsg('未知操作。');
        }
    }
}
?>