<?php
!defined('M_P') && exit('Forbidden');
!$winduid && Showmsg('not_login');
include_once(D_P.'data/bbscache/o_config.php');
require_once(R_P.'require/showimg.php');
list($faceurl) = showfacedesign($winddb['icon'],1);
//TODO 我的群组没了?

InitGP(array('a'));

if (empty($a)) {
	
	InitGP(array('page'), null, 2);
	$page < 1 && $page = 1;
	$db_perpage = 10;
	$total = $db->get_value("SELECT COUNT(*) AS sum FROM pw_cmembers cm LEFT JOIN pw_argument a ON a.gid=cm.colonyid AND a.tpcid='0' WHERE cm.uid=" . pwEscape($winduid) . ' AND a.tid IS NOT NULL');
	$pages = numofpage($total, $page, ceil($total/$db_perpage), "{$basename}q=$q&");

	$threaddb = $group = $apply = array();
	$query = $db->query("SELECT cm.colonyid,c.cname,a.tid,a.author,a.authorid,a.lastpost,subject FROM pw_cmembers cm LEFT JOIN pw_colonys c ON cm.colonyid=c.id LEFT JOIN pw_argument a ON cm.colonyid=a.gid AND a.tpcid='0' WHERE cm.uid=" . pwEscape($winduid) . ' AND a.tid IS NOT NULL ORDER BY a.lastpost DESC' . pwLimit(($page - 1) * $db_perpage, $db_perpage));
	while ($rt = $db->fetch_array($query)) {
		$rt['lastpost'] = get_date($rt['lastpost']);
		$threaddb[] = $rt;
	}
	
	$query = $db->query("SELECT cm.ifadmin,cm.addtime,c.id,c.cname,c.cnimg,c.admin FROM pw_cmembers cm LEFT JOIN pw_colonys c ON cm.colonyid=c.id WHERE cm.uid=" . pwEscape($winduid) . " ORDER BY cm.colonyid DESC");
	while ($rt = $db->fetch_array($query)) {
		if ($rt['cnimg']) {
			list($rt['cnimg']) = geturl("cn_img/$rt[cnimg]",'lf');
		} else {
			$rt['cnimg'] = $pwModeImg.'/nophoto.gif';
		}
		$rt['addtime'] = get_date($rt['addtime'], 'Y-m-d');
		if ($rt['ifadmin'] == '-1') {
			$apply[] = $rt;
		} else {
			$group[] = $rt;
		}
	}
	require_once(R_P.'require/header.php');
	require_once PrintEot('m_groups');
	footer();

} elseif ($a == 'all') {

	//TODO 已加入或审核中的区分...
	InitGP(array('page','cid'), null, 2);
	$page < 1 && $page = 1;
	$db_perpage = 10;
	if ($cid) {
		$total = $db->get_value("SELECT cnsum FROM pw_cnclass WHERE cid=" . pwEscape($cid));
		$sql = ' AND classid=' . pwEscape($cid);
	} else {
		$total = $db->get_value("SELECT COUNT(*) FROM pw_colonys");
		$sql = '';
	}
	$pages = numofpage($total, $page, ceil($total/$db_perpage), "{$basename}q=$q&a=all&");

	$group = array();
	$query = $db->query("SELECT id,classid,cname,members,cnimg FROM pw_colonys WHERE 1{$sql} ORDER BY id DESC " . pwLimit(($page-1) * $db_perpage, $db_perpage));
	while ($rt = $db->fetch_array($query)) {
		if ($rt['cnimg']) {
			list($rt['cnimg']) = geturl("cn_img/$rt[cnimg]",'lf');
		} else {
			$rt['cnimg'] = $pwModeImg.'/nophoto.gif';
		}
		$group[] = $rt;
	}
	require_once(R_P.'require/header.php');
	require_once PrintEot('m_groups');
	footer();

} elseif ($a == 'create') {

	!$o_newcolony && Showmsg('colony_reglimit');
	$o_groups && strpos($o_groups,",$groupid,") === false && Showmsg('colony_groupright');

	require_once(R_P.'require/credit.php');
	$moneyname = $credit->cType[$o_moneytype];
	if ($o_createmoney > $credit->get($winduid,$o_moneytype)) {
		Showmsg('colony_creatfailed');
	}

	if ($o_allowcreate && $o_allowcreate <= $db->get_value("SELECT COUNT(*) AS sum FROM pw_colonys WHERE admin=" . pwEscape($windid))) {
		Showmsg('colony_numlimit');
	}
	if (empty($_POST['step'])) {
		require_once(R_P.'require/header.php');
		require_once PrintEot('m_groups');
		footer();

	} else {

		InitGP(array('cname','descrip'),'P');
		InitGP(array('cid'), 'P', 2);
		!$cname && Showmsg('colony_emptyname');
		(!$cname || strlen($cname) > 255) && Showmsg('colony_descrip');
		!$cid && Showmsg('colony_class');

		$rt = $db->get_one("SELECT id FROM pw_colonys WHERE cname=".pwEscape($newname));
		$rt['id'] > 0 && Showmsg('colony_samename');

		$credit->addLog('hack_cycreate',array($o_moneytype => -$o_createmoney),array(
			'uid'		=> $winduid,
			'username'	=> $windid,
			'ip'		=> $onlineip,
			'cnname'	=> stripslashes($cname)
		));
		$credit->set($winduid,$o_moneytype,-$o_createmoney);

		$db->update("INSERT INTO pw_colonys SET " . pwSqlSingle(array(
				'cname'		=> $cname,
				'classid'	=> $cid,
				'admin'		=> $windid,
				'members'	=> 1,
				'ifcheck'	=> 1,
				'createtime'=> $timestamp,
				'descrip'	=> $descrip
		)));
		$cyid = $db->insert_id();

		$db->update("INSERT INTO pw_cmembers SET " . pwSqlSingle(array(
				'uid'		=> $winduid,
				'username'	=> $windid,
				'ifadmin'	=> 1,
				'colonyid'	=> $cyid
		)));

		$db->update("UPDATE pw_cnclass SET cnsum=cnsum+1 WHERE cid=" . pwEscape($cid));

		pwAddFeed($winduid, 'colony', '', array(
			'lang'		=> 'colony_create',
			'colonyid'	=> $cid,
			'cname'		=> $cname,
			'link'		=> "{$db_bbsurl}/{$basename}q=group&cyid=$cyid"
		));

		refreshto("{$basename}q=group&cyid=$cyid&a=set",'colony_regsuccess');
	}
}
?>