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

//相册基础api函数
function album_photo_read_base($fields="*",$condition="",$by_col="",$order="",$num="",$cache="",$cache_key=""){
	global $tablePreStr;
	$t_photo=$tablePreStr."photo";
	$t_album=$tablePreStr."album";
	$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_photo as p join $t_album as a on (p.album_id=a.album_id) where a.album_pass = 1 and a.album_secret = 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 album_photo_by_photoid($fields="*",$id){
	$fields=filt_fields($fields);
	$id_str=filt_num_array($id);
	$condition=" and p.photo_id in ($id_str) ";
	return album_photo_read_base($fields,$condition);
}

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

function album_photo_by_coms($fields="*",$num=10){
	$num=intval($num);
	return album_photo_read_base($fields,"","p.comments","desc",$num,1,"photo_coms_");
}

function album_photo_get_new($fields="*",$num=10){
	$num=intval($num);
	return album_photo_read_base($fields,"","p.add_time","desc",$num,1,"album_new_");
}

?>