<?php

/*
	[SupeSite/X-Space] (C)2001-2006 Comsenz Inc.
	搜索功\能

	$RCSfile: batch.search.php,v $
	$Revision: 1.27 $
	$Date: 2006/11/01 01:52:11 $
*/

include_once('./include/main.inc.php');
include_once(S_ROOT.'./include/common.inc.php');

$perpage = 100;
$urlplus = $wheresql = $message = $multipage = '';
$wherearr = $iarr = array();

empty($_GET['page'])?$page = 1:$page = intval($_GET['page']);
$start = ($page-1)*$perpage;

$subjectsearch = postget('subjectsearch');	//獲取標題搜索按鈕值
$authorsearch = postget('authorsearch');	//獲取作者搜索按鈕值
$messagesearch = postget('messagesearch');	//獲取內容搜索按鈕值
$usersearch = postget('usersearch');	//獲取人員搜索按鈕值
$goodscitysearch = postget('goodscitysearch');

if($subjectsearch || $authorsearch || $messagesearch || $usersearch || $goodscitysearch) {
	getcookie(1);
	if(empty($_SGLOBAL['supe_uid'])) {
		messagebox('error', '您未登錄，系統不允許\搜索', geturl('action/login'));
	}
	if(!empty($_SCONFIG['searchinterval'])) {
		if($_SGLOBAL['timestamp'] - $_SGLOBAL['member']['lastsearchtime'] < $_SCONFIG['searchinterval']) {
			messagebox('error', '對不起，您兩次查詢的時間間隔太短，請間隔'.$_SCONFIG['searchinterval'].'秒後再進行查詢。');
		}
	}
}

if(!empty($subjectsearch) || !empty($authorsearch)) {
	$searchkey = checkkey('searchkey', 1);
	$type = postget('type');
	if(!in_array($type, array('news', 'blog', 'file', 'image', 'goods', 'link'))) $type = '';
	//組合翻頁的參數
	$urlplus = 'searchkey='.rawurlencode($searchkey).'&type='.rawurlencode($type);

	if(!empty($type)) $wherearr[] = 'type=\''.$type.'\'';
	$wherearr[] = 'folder=\'1\'';
	if(!empty($subjectsearch)) {
		$wherearr[] = 'subject LIKE \'%'.$searchkey.'%\'';
		$urlplus .= '&subjectsearch=1';
	} else {
		$wherearr[] = 'username LIKE \'%'.$searchkey.'%\'';
		$urlplus .= '&authorsearch=1';
	}
	$wheresql = implode(' AND ', $wherearr);	//鏈接搜索條件
	$query = $_SGLOBAL['db']->query('SELECT COUNT(*) FROM '.tname('spaceitems').' WHERE '.$wheresql);	//統計記錄數
	$listcount = $_SGLOBAL['db']->result($query, 0);
	if($listcount) {
		$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname('spaceitems').' WHERE '.$wheresql.' LIMIT '.$start.','.$perpage);
		while ($item = $_SGLOBAL['db']->fetch_array($query)) {
			if($item['type'] == 'news') {
				$item['url'] = geturl('action/viewnews/itemid/'.$item['itemid']);
			} else {
				$item['url'] = geturl('uid/'.$item['uid'].'/action/viewspace/itemid/'.$item['itemid']);
			}
			$iarr[] = $item;
		}
		$multipage = multi($listcount, $perpage, $page, S_URL.'/batch.search.php?'.$urlplus);	//分頁
	} else {
		messagebox('ok', '對不起，沒有找到相關數據。');
	}
}else if(!empty($messagesearch)) {
	$searchkey = checkkey('searchkey', 1);

	$type = postget('type');
	if(!in_array($type, array('news', 'blog', 'file', 'image', 'goods', 'link'))) {
		messagebox('error', '對不起，搜索信息類型不正確.');
	}
	//組合翻頁的參數
	$urlplus = 'searchkey='.rawurlencode($searchkey).'&type='.rawurlencode($type).'&messagesearch=1';

	if(!empty($type)) $wherearr[] = 'i.type=\''.$type.'\'';
	$wherearr[] = 't.itemid = i.itemid';
	$wherearr[] = 't.message LIKE \'%'.$searchkey.'%\'';

	$tablename = gettypetablename($type);
	
	$wheresql = implode(' AND ', $wherearr);	//鏈接搜索條件
	
	$query = $_SGLOBAL['db']->query('SELECT COUNT(*) FROM '.tname('spaceitems').' i, '.tname($tablename).' t WHERE '.$wheresql);
	$listcount = $_SGLOBAL['db']->result($query, 0);
	if($listcount) {
		$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname('spaceitems').' i, '.tname($tablename).' t WHERE '.$wheresql.' LIMIT '.$start.','.$perpage);
		while ($item = $_SGLOBAL['db']->fetch_array($query)) {
			if($item['type'] == 'news') {
				$item['url'] = geturl('action/viewnews/itemid/'.$item['itemid']);
			} else {
				$item['url'] = geturl('uid/'.$item['uid'].'/action/viewspace/itemid/'.$item['itemid']);
			}
			$iarr[] = $item;
		}
		$multipage = multi($listcount, $perpage, $page, S_URL.'/batch.search.php?'.$urlplus);
	} else {
		messagebox('ok', '對不起，沒有找到相關數據。');
	}
}else if(!empty($usersearch)) {

	$interest = $native = array();
	$username = checkkey('username');
	$spacename = checkkey('spacename');
	$nickname = checkkey('nickname');
	$birthyear = checkkey('birthyear');
	$birthmonth = checkkey('birthmonth');
	$birthday = checkkey('birthday');
	$folk = checkkey('folk');
	$sex = checkkey('sex');
	$job = checkkey('job');
	$province = checkkey('province');
	$city = checkkey('city');
	$marriage = checkkey('marriage');
	$star = checkkey('star');
	$edu = checkkey('edu');
	$school = checkkey('school');
	$position = checkkey('position');
	$bloodtype = checkkey('bloodtype');
	$tall = checkkey('tall');
	$figure = checkkey('figure');
	$weight = checkkey('weight');
	$income = checkkey('income');
	$housing = checkkey('housing');
	$native = checkkey('native');
	$interest = checkkey('interest');
	
	$plusarr = $where1arr = array();
	
	if(!empty($username)) {
		$plusarr[] = 'username='.rawurlencode($username);
		$where1arr[] = 'u.username LIKE \'%'.$username.'%\'';
	}
	if(!empty($spacename)) {
		$plusarr[] = 'spacename='.rawurlencode($spacename);
		$where1arr[] = 'u.spacename LIKE \'%'.$spacename.'%\'';
	}
	if(!empty($province)) {
		$plusarr[] = 'province='.rawurlencode($province);
		$where1arr[] = 'u.province = \''.$province.'\'';
	}
	if(!empty($city)) {
		$plusarr[] = 'city='.rawurlencode($city);
		$where1arr[] = 'u.city = \''.$city.'\'';
	}
	if(!empty($nickname)) {
		$plusarr[] = 'nickname='.rawurlencode($nickname);
		$wherearr[] = 'f.nickname LIKE \'%'.$nickname.'%\'';
	}
	if($sex!="") {
		$plusarr[] = 'sex='.rawurlencode($sex);
		$wherearr[] = 'f.sex = \''.$sex.'\'';
	}
	if(!empty($birthyear)) {
		$plusarr[] = 'birthyear='.rawurlencode($birthyear);
		$wherearr[] = 'f.birthyear = \''.$birthyear.'\'';
	}
	if(!empty($birthmonth)) {
		$plusarr[] = 'birthmonth='.rawurlencode($birthmonth);
		$wherearr[] = 'f.birthmonth = \''.$birthmonth.'\'';
	}
	if(!empty($birthday)) {
		$plusarr[] = 'birthday='.rawurlencode($birthday);
		$wherearr[] = 'f.birthday = \''.$birthday.'\'';
	}
	if(!empty($folk)) {
		$plusarr[] = 'folk='.rawurlencode($folk);
		$wherearr[] = 'f.folk = \''.$folk.'\'';
	}
	if(!empty($job)) {
		$plusarr[] = 'job='.rawurlencode($job);
		$wherearr[] = 'f.job = \''.$job.'\'';
	}
	if(!empty($marriage)) {
		$plusarr[] = 'marriage='.rawurlencode($marriage);
		$wherearr[] = 'f.marriage = \''.$marriage.'\'';
	}
	if(!empty($star)) {
		$plusarr[] = 'star='.rawurlencode($star);
		$wherearr[] = 'f.star = \''.$star.'\'';
	}
	if(!empty($edu)) {
		$plusarr[] = 'edu='.rawurlencode($edu);
		$wherearr[] = 'f.edu = \''.$edu.'\'';
	}
	if(!empty($school)) {
		$plusarr[] = 'school='.rawurlencode($school);
		$wherearr[] = 'f.school = \''.$school.'\'';
	}
	if(!empty($position)) {
		$plusarr[] = 'position='.rawurlencode($position);
		$wherearr[] = 'f.position = \''.$position.'\'';
	}
	if(!empty($bloodtype)) {
		$plusarr[] = 'bloodtype='.rawurlencode($bloodtype);
		$wherearr[] = 'f.bloodtype = \''.$bloodtype.'\'';
	}
	if(!empty($tall)) {
		$plusarr[] = 'tall='.rawurlencode($tall);
		$wherearr[] = 'f.tall = \''.$tall.'\'';
	}
	if(!empty($figure)) {
		$plusarr[] = 'figure='.rawurlencode($figure);
		$wherearr[] = 'f.figure = \''.$figure.'\'';
	}
	if(!empty($weight)) {
		$plusarr[] = 'weight='.rawurlencode($weight);
		$wherearr[] = 'f.weight = \''.$weight.'\'';
	}
	if(!empty($income)) {
		$plusarr[] = 'income='.rawurlencode($income);
		$wherearr[] = 'f.income = \''.$income.'\'';
	}
	if(!empty($housing)) {
		$plusarr[] = 'housing='.rawurlencode($housing);
		$wherearr[] = 'f.housing = \''.$housing.'\'';
	}
	if(!empty($native)) {
		$nativearr = array();
		foreach($native as $val) {
			$plusarr[] = 'native[]='.rawurlencode($val);
			$nativearr[] = 'f.native LIKE \'%'.$val.'%\'';
		}
		$wherearr[] = '('.implode(' OR ', $nativearr).')';
	}
	if(!empty($interest)) {
		$interestarr = array();
		foreach($interest as $val) {
			$plusarr[] = 'interest[]='.rawurlencode($val);
			$interestarr[] = 'f.interest LIKE \'%'.$val.'%\'';
		}
		$wherearr[] = '('.implode(' OR ', $interestarr).')';
	}
	//取出自定字段
	$query = $_SGLOBAL['db']->query('SELECT proid FROM '.tname('userprofile').' WHERE available > 0 ORDER BY displayorder');
	while ($fields = $_SGLOBAL['db']->fetch_array($query)) {
		$proval = checkkey('profile_'.$fields['proid']);
		if(!empty($proval)) {
			$plusarr[] = 'profile_'.$fields['proid'].'='.rawurlencode($proval);
			$wherearr[] = 'f.profile_'.$fields['proid'].' = \''.$proval.'\'';
		}
	}
	$countsql = $listsql = '';
	if(!empty($wherearr)) {
		$wherearr = array_merge ($wherearr, $where1arr);
		if(empty($wherearr)) messagebox('error', '對不起，請至少選擇一個條件進行查詢');
		$wherearr[] = 'f.uid=u.uid';
		$wheresql = implode(' AND ', $wherearr);
		$countsql = 'SELECT COUNT(*) FROM '.tname('userspaces').' u, '.tname('userfields').' f WHERE '.$wheresql;
		$listsql = 'SELECT * FROM '.tname('userspaces').' u, '.tname('userfields').' f WHERE '.$wheresql.' LIMIT '.$start.','.$perpage;
	} else {
		if(empty($where1arr)) messagebox('error', '對不起，請至少選擇一個條件進行查詢');
		$wheresql = implode(' AND ', $where1arr);
		$countsql = 'SELECT COUNT(*) FROM '.tname('userspaces').' u  WHERE '.$wheresql;
		$listsql = 'SELECT * FROM '.tname('userspaces').' u WHERE '.$wheresql.' LIMIT '.$start.','.$perpage;
	}
	
	$plusarr[] = 'usersearch=1';
	if(!empty($plusarr)) {
		$urlplus = implode('&', $plusarr);
	}
	
	$query = $_SGLOBAL['db']->query($countsql);
	$listcount = $_SGLOBAL['db']->result($query, 0);
	if($listcount) {
		$query = $_SGLOBAL['db']->query($listsql);
		while ($item = $_SGLOBAL['db']->fetch_array($query)) {
			$item['url'] = geturl('uid/'.$item['uid'].'/action/space');
			$iarr[] = $item;
		}
		$multipage = multi($listcount, $perpage, $page, S_URL.'/batch.search.php?'.$urlplus);
	} else {
		messagebox('ok', '對不起，沒有找到相關數據。');
	}
	
}else if(!empty($goodscitysearch)) {
	
	$province = checkkey('province');
	$city = checkkey('city');
	$plusarr = array();
	$wherearr[] = 'i.type=\'goods\'';
	$wherearr[] = 'g.itemid=i.itemid';
	if(!empty($province)) {
		$plusarr[] = 'province='.rawurlencode($province);
		$wherearr[] = 'g.province = \''.$province.'\'';
	}
	if(!empty($city)) {
		$plusarr[] = 'city='.rawurlencode($city);
		$wherearr[] = 'g.city = \''.$city.'\'';
	}
	$plusarr[] = 'goodscitysearch=1';
	$wheresql = implode(' AND ', $wherearr);

	if(!empty($plusarr)) {
		$urlplus = implode('&', $plusarr);
	}
	$query = $_SGLOBAL['db']->query('SELECT COUNT(*) FROM '.tname('spaceitems').' i, '.tname('spacegoods').' g WHERE '.$wheresql);
	$listcount = $_SGLOBAL['db']->result($query, 0);
	if($listcount) {
		$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname('spaceitems').' i, '.tname('spacegoods').' g WHERE '.$wheresql.' LIMIT '.$start.','.$perpage);
		while ($item = $_SGLOBAL['db']->fetch_array($query)) {
			$item['url'] = geturl('uid/'.$item['uid'].'/action/viewspace/itemid/'.$item['itemid']);
			$iarr[] = $item;
		}
		$multipage = multi($listcount, $perpage, $page, S_URL.'/batch.search.php?'.$urlplus);
	} else {
		messagebox('ok', '對不起，沒有找到相關數據。');
	}
}

if(empty($iarr)) {
	$requiredselect = $requiredtext = '';
	$fieldarr = $requiredjs = array();
	$query = $_SGLOBAL['db']->query('SELECT proid, title, description, selective, choices, required FROM '.tname('userprofile').' WHERE available > 0 ORDER BY displayorder');
	while ($fields = $_SGLOBAL['db']->fetch_array($query)) {
		$fieldarr['profile_'.$fields['proid']] = array(
		'title' => $fields['title'],
		'description' => $fields['description'],
		'selective' => $fields['selective'],
		'choices' => $fields['choices'],
		'required' => $fields['required']
		);
	}
	if ($fieldarr) {
		foreach ($fieldarr as $key => $fields) {

			if ($fields['selective']) {
				$selective = '<select name="'.$key.'" id="'.$key.'">';
				$selective .= '<option value="">------------</option>';
				foreach (explode("\n", $fields['choices']) as $item) {
					list($index, $choice) = explode('=', $item);
					$selective .='<option value="'.trim($index).'">'.trim($choice).'</option>';
				}
				$selective .= '</select>';
				$requiredselect .= '<div><label for="'.$key.'">'.$fields['title'].'</label>'.$selective.'</div>';
			} else {
				unset($fields['choices']);
				$requiredtext .= '<div><label for="'.$key.'">'.$fields['title'].'</label><input type="text" value="" id="'.$key.'" name="'.$key.'" /></div>';
			}
		}
	}
} else {
	//更新搜索時間
	$_SGLOBAL['db']->query('UPDATE '.tname('members').' SET lastsearchtime=\''.$_SGLOBAL['timestamp'].'\' WHERE uid=\''.$_SGLOBAL['supe_uid'].'\'');
}

$title = '搜索';

include_once(template('site_search'));

function checkkey($str, $ischeck=0) {
	$str = stripsearchkey(postget($str));
	if($ischeck) {
		if(empty($str)) {
			messagebox('error', '對不起，請輸入您要查詢的關鍵字。', S_URL.'/batch.search.php');
		}elseif(strlen($str) < 2) {
			messagebox('error', '對不起，您指定的查詢關鍵字太短，請輸入至少2個字符進行查詢。', S_URL.'/batch.search.php');
		}
	}
	return $str;
}
?>