<?php
function u_mcn_sql($caid,$ccid1,$ccid11){
	if($caid){
		$id = $caid;
		$var = 'caid';
		$arr = read_cache('acatalogs');
		$mode = 1;
	}elseif($ccid1){
		$id = $ccid1;
		$var = 'ccid1';
		$arr = read_cache('coclasses',1);
		$mode = 1;
	}elseif($ccid11){
		$id = $ccid11;
		$var = 'ccid11';
		$arr = read_cache('coclasses',11);
		$mode = 0;
	}else return '';
	$ids = cnsonids($id,$arr);
	return caccsql('s.'.$var,$ids,$mode);
}
function u_relsql($fname,$ids,$mode=0){//$ids 可以单个，也可以是数组
	return caccsql($fname,$ids,$mode);
}
function u_field_names($caid){
	global $sid;
	$catalog = read_cache('catalog',$caid,'',$sid);
	$items = array();
	if(!empty($catalog['search_list'])){
		$temp = explode('=',$catalog['search_list']);
		if(!empty($temp[0]) && !empty($temp[1])){
			$chid = max(0,intval($temp[0]));
			$arr = explode(',',trim($temp[1]));
			foreach($arr as $k){
				if($field = read_cache('field',$chid,$k)){
					$items[] = array('chid' => $field['chid'],'ename' => $field['ename'],'cname' => $field['cname'],'caid' => $caid);
				}
			}
		}
	}
	unset($catalog,$field,$temp,$chid,$arr);
	return $items;
}
function u_fields_options($chid,$ename,$caid){
	$items = array();
	if($field = read_cache('field',$chid,$ename)){
		if(!empty($field['innertext'])){
			$temps = explode("\n",$field['innertext']);
			foreach($temps as $v){
				$temparr = explode('=',str_replace(array("\r","\n"),'',$v));
				$temparr[1] = isset($temparr[1]) ? $temparr[1] : $temparr[0];
				$items[] = array('ename' => $ename,'value' => $temparr[0],'title' => $temparr[1],'caid' => $caid);
			}
		}
	}
	unset($field,$temps,$temparr);
	return $items;
}
function u_searchfields($caid = 0){
	$items = array();
	if(!($chid = u_chidbycaid($caid))) return $items;
	$fields = read_cache('fields',$chid);
	foreach($fields as $k => $v){
		if($v['available'] && $v['issearch'] && $v['tbl'] == 'custom'){
			$items[] = array('chid' => $chid,'ename' => $k,'cname' => $v['cname'],'caid' => $caid);
		}
	}
	return $items;
}
function u_chidbycaid($caid = 0){
	global $sid,$aaltypes;
	$chid = 0;
	if(!$caid) return $chid;
	$catalog = read_cache('catalog',$caid,'',$sid);
	$atids = array_filter(explode(',',$catalog['atids']));
	if(count($atids) == 1){
		$chid = $aaltypes[$atids[0]]['chid'];
	}
	return $chid;
}
function u_bus_offers($caid =0,$limits=10){
	global $db,$tblprefix,$timestamp;
	$items = array();
	$limits = !$limits ? 10 : $limits;
	$query = $db->query("SELECT cu.*,a.caid,a.subject,a.thumb FROM {$tblprefix}offers cu LEFT JOIN {$tblprefix}archives a ON (a.aid=cu.aid) WHERE cu.checked='1' AND cu.uccid1='1'".($caid ? " AND a.caid='$caid'" : '')." AND (cu.enddate = 0 OR cu.enddate>'$timestamp') ORDER BY cu.cid DESC LIMIT 0,$limits");
	while($row = $db->fetch_array($query)) $items[] = $row;
	return $items;
}
function u_offers_arr($aid = 0,$addid=0,$nowpage=1,$limits=10){//按会员所属的区域来取得报价列表
	global $db,$tblprefix,$timestamp;
	$items = array();
	if(!$aid) return $items;
	$limits = !$limits ? 10 : $limits;
	$query = $db->query("SELECT cu.*,m.*,s.* FROM {$tblprefix}offers cu LEFT JOIN {$tblprefix}members m ON (m.mid=cu.mid) LEFT JOIN {$tblprefix}members_sub s ON (s.mid=cu.mid) WHERE cu.aid='$aid' AND cu.checked='1'".($addid ? " AND s.ccid11='$addid'" : '')." AND (cu.enddate = 0 OR cu.enddate>'$timestamp') ORDER BY cu.uccid1 DESC,cu.cid DESC LIMIT ".(($nowpage-1)*$limits).",$limits");
	while($row = $db->fetch_array($query)){
		cu_checkend($row,'offer');
		$items[] = $row;
	}
	return $items;
}
function u_offer_counts($aid = 0,$addid=0,$nowpage=0,$limits=10){
	global $db,$tblprefix,$timestamp;
	if(!$aid) return $items;
	return $db->result_one("SELECT COUNT(*) FROM {$tblprefix}offers cu LEFT JOIN {$tblprefix}members m ON (m.mid=cu.mid) LEFT JOIN {$tblprefix}members_sub s ON (s.mid=cu.mid) WHERE cu.aid='$aid' AND cu.checked='1'".($addid ? " AND s.ccid11='$addid'" : '')." AND (cu.enddate = 0 OR cu.enddate>'$timestamp')");
}
function pro_xianxi_con($chid,$caid,$aid){
  global $db,$tblprefix;
  $sql = "SELECT * FROM ".$tblprefix."archives_$chid WHERE aid = $aid";
  $query = $db->fetch_one($sql);
  unset($query["aid"]);
  $fields = read_cache('fields',$chid);
  $returnhtml = "";
  if(is_array($query)){
      $tmp = array();
      foreach($fields as $k => $v)array_key_exists($k, $query) && $tmp[$k] = $query[$k];
      $query = &$tmp;
      unset($tmp);
  }
  foreach($query as $k => $v){
      if(empty($v)){                              //清空无数据字段
          unset($query[$k]);
          continue;
      }
      $returnhtml .= "<tr>";
      $returnhtml .= "<td id='param_5091_1' bgcolor='#FFFFFF' align='left' valign='middle' class='param_td1' >" . $fields[$k]['cname'] . "</td>" ;
      $returnhtml .= "<td id='param_5091_2' bgcolor='#FFFFFF' class='param_td2'>";
      if($fields[$k]["datatype"] == "select" || $fields[$k]["datatype"] == "mselect"){
          $fieldsitems = u_fields_options($chid,$k,$caid);
          $queryitems = explode("\t",$v);
          foreach($queryitems as $qv){
              foreach($fieldsitems as $fv){
                  if($fv['value'] == $qv) $returnhtml .= $fv['title'] . "&nbsp;";
                  
              }
          }
      }
      else{
          $returnhtml .= "$v";
      }
      $returnhtml .= "</td>";
      $returnhtml .= "</tr>";
  }
  echo $returnhtml;
}
function get_topcaid($caid){
	global $acatalogs;
	if(empty($caid)){
		$topcaid = 0;
	}else{
		$topcaid = $caid;
		while($acatalogs[$topcaid]['pid'])$topcaid = $acatalogs[$topcaid]['pid'];
	}
	return $topcaid;
}
?>