<?php
function u_relsql($fname,$ids,$mode=0){//$ids 可以单个，也可以是数组
	return caccsql($fname,$ids,$mode);
}
function u_url_arr($caid,$coid,$limits=0){
	global $sid,$cnodes;
	load_cache('cnodes',$sid);
	$items = array();
	if(!$caid || !$coid) return $items;
	$coclasses = read_cache('coclasses',$coid);	
	$i = 0;
	foreach($coclasses as $k => $v){
		$cnstr = 'caid='.$caid.'&ccid'.$coid.'='.$k;
		if(isset($cnodes[$cnstr])){
			$i ++;
			if($limits && $i > $limits) break;
			$v['caid'] = $caid;
			$v['ccid'.$coid] = $v['ccid'];
			$items[] = $v;
		}
	}
	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_view_select($chid=0,$ename='',$value=''){
	$ret = '';
	if(!$chid || !$ename || !$value) return $ret;
	$valarr = explode("\t",$value);
	if($valarr && $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];
				if(in_array($temparr[0],$valarr)) $ret .= ($ret ? ',' : '').$temparr[1];
			}
		}
	}
	unset($field,$temps,$temparr);
	return $ret;
}

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;
	$chid = 0;
	if(!$caid) return $chid;
	$catalog = read_cache('catalog',$caid,'',$sid);
	$chids = array_filter(explode(',',$catalog['chids']));
	foreach($chids as $k){
		$channel = read_cache('channel',$k);
		if($channel['isalbum']) return $k;
	}
	return $chid;
}
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 u_setparam($var,$val){
	global $G;
	$G[$var] = $val;
}
function u_urlparams(&$temparr){
	global $G;
	foreach(array(1,2) as $var) if(isset($temparr['ccid'.$var])) $G['nccid'.$var] = $temparr['ccid'.$var];
	$G['fieldstr'] = 'searchword';
	empty($temparr['searchword']) || $G['searchword'] = $temparr['searchword'];
	!empty($temparr['orderby']) && $temparr['orderby'] != 'createdate' && $G['orderby'] = $temparr['orderby'];
	empty($temparr['ordermode']) || $G['ordermode'] = $temparr['ordermode'];
	$fields = u_searchfields($G['ncaid']);
	foreach($fields as $k => $v){
		$G['fieldstr'] .= ",$v[ename]";
		if(!empty($temparr[$v['ename']])) $G[$v['ename']] = $temparr[$v['ename']];
	}
#	$G['fieldstr'] = substr($G['fieldstr'], 1);
	$G['nchid'] = u_chidbycaid($G['ncaid']);
	unset($fields);
}
function u_cata_urls($caid,$coid,$class='sel',$p_item=0){//分析当前页面参数，包含搜索字段，则拼接搜索url，否则节点url
	global $sid,$cnodes,$G,$cms_abs,$selected_params;
	$items = array();
	if($p_item){
		$items[] = array(
			'ccid' => 0,
			'title' => $p_item
		);
	}
	if(!$coid || $G['ncaid'] != $caid) return $items;
	load_cache('cnodes',$sid);
	$nchid = $G['nchid'];

	$coclasses = read_cache('coclasses',$coid);
	foreach($coclasses as $k => $v){
		$cnstr = 'caid='.$G['ncaid'].'&ccid'.$coid.'='.$v['ccid'];
		if(isset($cnodes[$cnstr])){
			$v['ccid'.$coid] = $v['ccid'];
			$items[] = $v;
		}
	}
	$fcnstr = '';
	$fields = explode(',',$G['fieldstr']);
	foreach($fields as $k) if(!empty($G[$k])) $fcnstr .= '&'.$k.'='.rawurlencode($G[$k]);
	foreach($items as $k => $v){
		$cnstr = 'caid='.$G['ncaid'];
		foreach(array(2,1) as $var){
			if($coid == $var){
				$v['ccid'] && $cnstr .= '&ccid'.$var.'='.$v['ccid'.$var];
			}elseif(!empty($G['nccid'.$var])) $cnstr .= '&ccid'.$var.'='.$G['nccid'.$var];
		}
		$cnstr .= $fcnstr;
		if(isset($cnodes[$cnstr])){
			$cnode = cnodearr($cnstr,$sid);
			$items[$k]['url'] = $cnode['indexurl1'];
		}else $items[$k]['url'] = $cms_abs.'search.php?searchsubmit=1&'.$cnstr.'&chid='.$nchid.'&sid='.$sid;
		if($v['ccid'] == @$G["nccid$coid"]){
			$items[$k]['class'] = $class;
			isset($selected_params["ccid$coid"])||$selected_params["ccid$coid"] = array($v['ccid'], $v['title']);
		}else{
			$items[$k]['class'] = '';
		}
	}
	return $items;
}
function u_searchfields_n($caid = 0, $len = 0){
	global $G;
	$items = array();
	if($caid != $G['ncaid'] || !($chid = $G['nchid'])) return $items; 
	$fields = read_cache('fields',$chid);
	$i=1;
	foreach($fields as $k => $v){
		if($v['available'] && $v['issearch'] && $v['tbl'] == 'custom'){
			$items[] = array('row' =>$i++, 'ename' => $k,'cname' => $v['cname']);
			if($len && $i > $len)break;
		}
	}
	return $items;
}
function u_fields_options_n($ename,$class='sel',$p_item=0){
	global $sid,$cnodes,$G,$cms_abs,$selected_params;
	$items = array();
	$chid = $G['nchid'];
	$cnstr = 'caid='.$G['ncaid'];
	foreach(array('ccid2','ccid1') as $var)empty($G['n'.$var]) || $cnstr .= "&$var=".$G['n'.$var];
	$fields = explode(',',$G['fieldstr']);
	foreach($fields as $k) if(!empty($G[$k]) && $k != $ename) $cnstr .= '&'.$k.'='.rawurlencode($G[$k]);
	$url = "{$cms_abs}search.php?searchsubmit=1&sid=$sid&chid=$G[nchid]&$cnstr";
	if($p_item){
		load_cache('cnodes',$sid);
		if(isset($cnodes[$cnstr])){
			$cnode = cnodearr($cnstr,$sid);
			$cnstr = $cnode['indexurl1'];
		}else $cnstr = $url;
		$items[] = array(
			'ccid' => 0,
			'title' => $p_item,
			'url' => $cnstr,
			'class' => empty($G[$ename]) ? $class : ''
		);
	}
	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];
				if($temparr[0] == @$G[$ename]){
					$items[] = array('title' => $temparr[1],'url' => $url.'&'.$ename.'='.rawurlencode($temparr[0]),'class' => $class);
					isset($selected_params[$ename])||$selected_params[$ename] = $temparr;
				}else{
					$items[] = array('title' => $temparr[1],'url' => $url.'&'.$ename.'='.rawurlencode($temparr[0]),'class' => '');
				}
			}
		}
	}
	unset($field,$temps,$temparr);
	return $items;
}
function u_firstsonid($caid){
	global $catalogs,$sid;
	load_cache('catalogs',$sid);
	foreach($catalogs as $k => $v) if($v['pid'] == $caid){$sonid = $k;break;}
	return $sonid;
}
function u_sonurl($caid){
	global $catalogs,$sid,$G;
	load_cache('catalogs',$sid);
	foreach($catalogs as $k => $v) if($v['pid'] == $caid){$sonid = $k;break;}
	$cnode = cnodearr('caid='.$sonid,$sid);
	return '<a href="'.(empty($cnode) ? '#' : $cnode['indexurl1']).'" class="'.($G['ncaid'] == @$sonid ? 'nor' : 'act').'">'.@$catalogs[$caid]['title'].'</a>';
}
function u_display($caid){
	global $G;
	return $caid == $G['ncaid'] ? 'class="show"' : 'style="display:none"';
}
function u_selected_items(){
	global $sid,$cnodes,$G,$cms_abs,$selected_params;
	$selected_params || $selected_params = array();
	$items=array();$urls = array("caid=$G[ncaid]");
	foreach($selected_params as $k => $v)if(empty($v[0]))unset($selected_params[$k]);else $items[$k] = "$k=" . rawurlencode($v[0]);
	foreach(array(2,1) as $v){
		if(isset($items[$k = "ccid$v"])){
			$urls[$k] = $items[$k];
			unset($items[$k]);
		}
	}
	foreach($items as $k => $v)$urls[$k] = $v;
	load_cache('cnodes',$sid);
	$items = array();
	foreach($selected_params as $k => $v){
		$cnstr = $urls;
		unset($cnstr[$k]);
		$cnstr = implode('&',$cnstr);
		if(isset($cnodes[$cnstr])){
			$cnode = cnodearr($cnstr,$sid);
			$cnstr = $cnode['indexurl1'];
		}else $cnstr = "{$cms_abs}search.php?searchsubmit=1&sid=$sid&chid=$G[nchid]&$cnstr";
		$items[] = array(
			'class' => 'bg_style',
			'text' => $v[1],
			'url' => $cnstr
		);
	}
	$selected_params = array();
	return $items;
}
function u_order_urls($bys, $nochid = 0){
	global $sid,$cnodes,$G,$cms_abs;
	$items  = array();
	$orders = array();
	foreach($bys as $k => $v){
		$k = explode('-', $k, 2);
		isset($k[1]) || $k[1] = '0';
		$k[2] = $v;
		$orders[] = $k;
	}
	$search = "{$cms_abs}search.php?searchsubmit=1&sid=$sid&";
	$nochid || $search .= "chid=$G[nchid]&";
	$cnstr = "caid=$G[ncaid]";
	foreach(array('ccid2','ccid1') as $var)empty($G['n'.$var]) || $cnstr .= "&$var=".$G['n'.$var];
	$fields = explode(',',$G['fieldstr']);
	foreach($fields as $k)empty($G[$k]) || $cnstr .= "&$k=".rawurlencode($G[$k]);
	$orderby = empty($G['orderby']) ? '' : $G['orderby'];
	$ordermode = empty($G['ordermode']) ? '0' : $G['ordermode'];
	foreach($orders as $v){
		if(!$nochid && $orderby && !$v[0] && isset($cnodes[$cnstr])){
			load_cache('cnodes',$sid);
			$cnode = cnodearr($cnstr,$sid);
			$url = $cnode['indexurl1'];
		}else{
			$url = "$search$cnstr&orderby=$v[0]&ordermode=$v[1]";
		}
		$items[] = array('row' => $orderby == $v[0] && $ordermode == $v[1] ? '<b style="cursor: default;">'.$v[2].'</b>' : "<a href=\"$url\"><span>$v[2]</span></a>");
	}
	return $items;
}
function u_caidlist_func($ncaid, $title){
	global $sid,$caid,$G,$cms_abs;
	static $search_caid_url;
	if(!$search_caid_url){
		$url = '';
		foreach(array('ccid2','ccid1') as $var)empty($G['n'.$var]) ||$url .= "&$var=".$G['n'.$var];
		$fields = explode(',',$G['fieldstr'] . ',orderby,ordermode');
		foreach($fields as $var)empty($G[$var]) || $url .= "&$var=".rawurlencode($G[$var]);
		$search_caid_url = array(
			"{$cms_abs}search.php?searchsubmit=1&sid=$sid",
			''
		);
		$url && $search_caid_url[] = $url;
	}
	$search_caid_url[1] = "&caid=$ncaid";
	$url = implode('',$search_caid_url);
	$class = $ncaid == $caid ? ' class="bg_style"' : '';
	return "<a href=\"$url\"$class>$title</a>";
}
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 pro_xianxi_con_w($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'  align='left' valign='middle' class='param_td1' >" . $fields[$k]['cname'] . "</td>" ;
      $returnhtml .= "<td id='param_5091_2'  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'] . ",";
                  
              }
          }
      }
      else{
          $returnhtml .= "$v";
      }
      $returnhtml .= "</td>";
      $returnhtml .= "</tr>";
  }
  echo $returnhtml;
}

function duibi_result($addid){
	global $db,$tblprefix;
	$num = 4;//对比文档数
	$arr1 = explode('_',$addid);
	$chid = max(0,intval($arr1[0]));
	if(empty($chid) || empty($arr1[1])) return array();
	$arr2 = explode(',',$arr1[1]);
	$aids = array();
	$i = 0;
	foreach($arr2 as $v){
		if($i >= $num) continue;
		if($v = max(0,intval($v))) $aids[] = $v;
		$i ++;
	}
	unset($arr1,$arr2);
	if(!$aids) return array();
	
	$fields = read_cache('fields',$chid);
	
	$items = array();
	
	
	$j = -1;
	foreach($fields as $k => $v){
		$j ++;
		$items['cnt_head'][$j] = $v['cname'];
	}
	
	for($i=0;$i<$num;$i++){
		$item = array();
		if($aid = @$aids[$i]){
			$item = $db->fetch_one("SELECT a.*,c.* FROM ".$tblprefix."archives AS a LEFT JOIN ".$tblprefix."archives_$chid AS c ON (c.aid=a.aid) WHERE a.aid=$aid");
			arc_parse($item);
			foreach($item as $k => $v){
				if($fields[$k]["datatype"] == "select" || $fields[$k]["datatype"] == "mselect"){
					$queryitems = explode("	",$v);
					$item[$k] = '';
					foreach($queryitems as $qv){
						$fieldsitems = u_fields_options($chid,$k,0);
						foreach($fieldsitems as $fv){
							if($fv['value'] == $qv) $item[$k] .= "$fv[title]&nbsp;";
						}
					}
				}
			}
		}
	
	
		$j = -1;
		foreach($fields as $k => $v){
			$j ++;
			u_handel($item,$k,$v['datatype']);
			$items['cnt_'.$i][$j] = empty($item[$k]) ? '-' : $item[$k];
		}
	
	
	}
	$items = uarray_flip_keys($items);
	return $items;
}

function uarray_flip_keys($arr) {
	$arr2 = array();
	$arrkeys = array_keys($arr);
	list(, $first) = each(array_slice($arr, 0, 1));
	if($first) {
		foreach($first as $k=>$v) {
			foreach($arrkeys as $key) {
				$arr2[$k][$key] = $arr[$key][$k];
			}
		}
	}
	return $arr2;
}
function u_handel(&$item,$ename,$datatype){
	if($datatype == 'image'){
		global $cms_abs;
		$item[$ename] = $item[$ename] ? '<a target="_blank" href="'.$item['arcurl'].'"><img src="'.($cms_abs.$item[$ename]).'"  width="190" height="140"></a>' : '';
	}elseif($datatype == 'date'){
		$item[$ename] = $item[$ename] ? date("Y-m-d",$item[$ename]) : '';
	}
}
function get_topcaid($caid){
	global $acatalogs;
	if(empty($caid)){
		$topcaid = 0;
	}else{
		$topcaid = $caid;
		while($acatalogs[$topcaid]['pid'])$topcaid = $acatalogs[$topcaid]['pid'];
	}
	return $topcaid;
}
?>