<?php
require_once(dirname(__FILE__)."/config.php");
require_once(dirname(__FILE__)."/../include/inc_functions.php");
require_once(dirname(__FILE__)."/../include/inc_photograph.php");
require_once(dirname(__FILE__)."/../include/pub_oxwindow.php");
require_once(dirname(__FILE__)."/inc/inc_archives_functions.php");
CheckPurview('group_Edit');
$ID = ereg_replace("[^0-9]","",$ID);
if($ID < 1){
	ShowMsg("含有非法操作!.","-1");
	exit();
}
$db = new DedeSql(false);

//取出圈子信息
$row = $db->GetOne("SELECT * FROM #@__groups WHERE groupid='{$ID}'");
$groupsname = $row['groupname'];
$groupstoreid = $row['storeid'];
$groupishidden = $row['ishidden'];
$groupissystem = $row['issystem'];
$groupcreater = $row['creater'];
$groupimg     = $row['groupimg'];
$ismaster     = $row['ismaster'];
$groupdes     = $row['des'];
$groupisindex = $row['isindex'];
$groupsmalltype = $row['smalltype'];
//编译小分类成数组
$smalltypes    = $row['smalltype'];
$lists 		   = array();
$smalltypes    = @explode(",",$smalltypes);
foreach($smalltypes as $k){
	$kk = @explode("|",$k);
	array_push($lists,$kk[1]);
}
//====function Save Group Info=====//
if($action=="save"){
 $groupname = cn_substr($groupname,75);
 $storeid = ereg_replace("[^0-9]","",$store);
 $issystem = ereg_replace("[^0-1]","",$issystem);
 $ishidden = ereg_replace("[^0-1]","",$ishidden);
 $isindex  = ereg_replace("[^0-1]","",$isindex);
 $creater =  cn_substr($creater,15);
 $master =  cn_substr($master,70);
 $description = cn_substr($des,100);
 $row = $db->GetOne("SELECT tops FROM #@__store_groups WHERE storeid='{$storeid}'");
 if($row['tops'] >0 ) $rootstoreid = $row['tops'];
 else $rootstoreid = $storeid;
 //处理上传的缩略图
 if(empty($ddisremote)) $ddisremote = 0;
 $litpic = GetDDImage('litpic',$picname,$ddisremote); 
 if(empty($litpic)) $litpic = $groupimg;
 if(empty($smalltype)) $smalltype = $groupsmalltype;
 $smalltype = @implode(",",$smalltype);
 if(empty($smalltype)) $smalltype = 0;
 if($isindex < 1) $isindex = 0;
 $inQuery = "UPDATE #@__groups SET groupname='".$groupname."',des='".$description."',groupimg='".$litpic."',rootstoreid='{$rootstoreid}',storeid='{$storeid}',creater='".$creater."',ismaster='".$master."',issystem='{$issystem}',ishidden='{$ishidden}',smalltype='".$smalltype."',isindex='".$isindex."' WHERE groupid='{$ID}'";
 $db->SetQuery($inQuery);
 if(!$db->ExecuteNoneQuery()){
 	echo $db->GetError();
 	$db->Close();
	ShowMsg("把数据更新到数据库groups表时出错，请检查！","-1");
	exit(); 
 }else{
	ShowMsg("成功更改圈子设置！","-1");
	exit(); 
 }
}
//更新数据完毕
if(!$groupimg||empty($groupimg)) $groupimg = "img/pview.gif";
//类目递归
$db->SetQuery("SELECT * FROM #@__store_groups WHERE tops=0 ORDER BY orders ASC");
$db->Execute(1);
while($rs = $db->GetArray(1)){
	$selected = "";
	if($rs['storeid']==$groupstoreid) $selected = "selected='selected'";
	$option .= "<option value='".$rs['storeid']."' ".$selected.">".$rs['storename']."</option>\n";
	$v = $rs['storeid'];
	$db->SetQuery("SELECT * FROM #@__store_groups WHERE tops='{$v}' ORDER BY orders ASC");
	$db->Execute(2);
	while($rs = $db->GetArray(2)){
		$selected = "";
		if($rs['storeid']==$groupstoreid) $selected = "selected='selected'";
		$option .= "<option value='".$rs['storeid']."' ".$selected.">--".$rs['storename']."</option>\n";
	}
}
$db->SetQuery("SELECT * FROM #@__smalltypes ORDER BY disorder ASC");
$db->Execute();
while($rs = $db->GetArray()){
	$selected = "";
	if(in_array($rs['id'],$lists)) $selected = "selected='selected'";
	$smalltypes_option .= "<option value='".$rs['name']."|".$rs['id']."' ".$selected.">".$rs['name']."</option>\n";
}


require_once(dirname(__FILE__)."/templets/group_edit.htm");

$db->Close();

?>