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

//日志基础api函数
function blog_read_base($fields="*",$condition="",$by_col="",$order="",$num="",$cache="",$cache_key=""){
	global $tablePreStr;
	$t_blog=$tablePreStr."blog";
	$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_blog where log_pass = 1 and blog_limit = 0 $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 blog_self_by_bid($fields="*",$id){
	$fields=filt_fields($fields);
	$id_str=filt_num_array($id);
	$condition=" and log_id in ($id_str) ";
	return blog_read_base($fields,$condition);
}

function blog_self_by_uid($fields="*",$id){
	$fields=filt_fields($fields);
	$id_str=filt_num_array($id);
	$condition=" and user_id in ($id_str) ";
	return blog_read_base($fields,$condition);
}

function blog_self_by_hits($fields="*",$order="desc",$num=10){
	$num=intval($num);
	$order=filt_order($order);
	return blog_read_base($fields,"","hits",$order,$num,1,"blog_hits_");
}

function blog_self_get_new($fields="*",$num=10){
	$num=intval($num);
	return blog_read_base($fields,"","add_time","desc",$num,1,"blog_new_");
}

function blog_self_by_date($fields="*",$date,$by_col='hits',$order="desc",$num=10){
	$num=intval($num);
	$fields=filt_fields($fields);
	$order=filt_order($order);
	if($by_col!='hits'){
		$by_col='comments';
	}
	$condition=str_replace("{date}","add_time",date_filter($date));
	return blog_read_base("*",$condition,$by_col,$order,$num);
}

?>