<?php
include(dirname(__file__)."/../includes.php");

//群组基础api函数
function group_sub_read_base($fields="*",$condition="",$by_col="",$order="",$num="",$cache="",$cache_key=""){
	global $tablePreStr;
	$t_group_subject=$tablePreStr."group_subject";
	$t_group=$tablePreStr."groups";
	$plugin_rs=array();
	$dbo=new dbex;
  dbplugin('r');
  if($num==''||$num >= 1000){
  	$limit=" limit 1000 ";
  }else{
  	$limit=" limit $num ";
  }
  if($by_col!=''){
  	$by_col=" order by $by_col ";
  }
  $sql=" select $fields from $t_group_subject as s join $t_group as g on (g.group_id=s.group_id) where g.group_pass = 1 $condition $by_col $order $limit ";
  if($cache==1&&in_array($num,array(10,20,50))&&$cache_key!=''){
		$key=$cache_key.$order.'_'.$num;
		$key_mt=$cache_key.'mt_'.$order.'_'.$num;
		$plugin_rs=model_cache($key,$key_mt,$dbo,$sql);
	}
	if(empty($plugin_rs)){
		$plugin_rs=$dbo->getRs($sql);
	}
	return $plugin_rs;
}

function group_sub_by_sid($fields="*",$id){
	$fields=filt_fields($fields);
	$id_str=filt_num_array($id);
	$condition=" and s.subject_id in ($id_str) ";
	return group_sub_read_base($fields,$condition,"s.add_time","desc");
}

function group_sub_by_gid($fields="*",$id,$num=10){
	$fields=filt_fields($fields);
	$id_str=filt_num_array($id);	
	$condition=" and s.group_id in ($id_str) ";
	return group_sub_read_base($fields,$condition,"s.add_time","desc",$num);
}

function group_sub_by_uid($fields="*",$id,$num=10){
	$fields=filt_fields($fields);
	$id_str=filt_num_array($id);
	$condition=" and s.user_id in ($id_str) ";
	return group_sub_read_base($fields,$condition,"s.add_time","desc",$num);
}

function group_sub_by_date($fields="*",$date='',$col_by='hits',$order='desc',$num=10){
	$fields=filt_fields($fields);
	if($col_by!='hits'){
		$col_by="comments";
	}
	$order=filt_order($order);
	$condition=str_replace("{date}","add_time",date_filter($date));
	return group_sub_read_base($fields,$condition,$col_by,$order,$num);
}
?>