<?php
/**************************************************
*  Created:  2010-06-08
*
*  搜索
*
*  @Xweibo (C)1996-2099 SINA Inc.
*  @Author zhenquan <zhenquan@staff.sina.com.cn>
*
***************************************************/

class search
{
	function search() {
		session_start();
	}
	/**
	 * search for message
	 *
	 */
	function default_action(){
		$k = V('g:k', false);
		if ($k !== false) {
			$k = urldecode($k);
			$wb = APP::N('weibo');
			$p = V('g:p', 1);
			$each_page = 40;
			$rs = $wb->search($k, $p, $each_page);
			$result = $rs['results'];
			$count_all_result = count($result);
			for ($i=0,$count=count($result); $i < $count && $i< $each_page; $i++) {
				$result[$i]['user'] = array(
							'id' => $result[$i]['to_user_id'],
							'profile_image_url' => $result[$i]['profile_image_url'],
							'screen_name' => $result[$i]['from_user'],
							'verified' => false
							);
			}
			TPL :: assign('list', $result);
			//实例化分页pager
			if ($count_all_result > 20) {
				$pager = APP::N('pager');
				$makePage = $pager->pagehtml(null,40);
				TPL::assign('makePage', $makePage);
			}
		}
		$db = APP :: ADP('db');
		$rs = $db->query('SELECT *FROM ' . $db->getTable(T_FOLLOWERS_COUNT) . ' WHERE uid NOT IN(SELECT uid FROM ' .$db->getTable(T_DISABLED_FOLLOWERS). ') ORDER BY `followers_count` DESC LIMIT 10');
		TPL :: assign('top_followers', $rs);
		TPL :: display('search/search_mblog');
	}

	/**
	 * search for user
	 *
	 */
	function user() {
		$k = V('g:k', false);
		$page = V('g:page', 1);
		if ($k !== false && !empty($k)) {
			$k = urldecode($k);
			$wb = APP::N('weibo');
			$s = V('g:s', 2);
			$q = array(
				'q' => $k,
				'snick' => 1,
				'sort' => $s,
				'count' =>WB_API_LIMIT * 2,
				'page' => $page
			);
			$rs = $wb->searchUser($q);
			if (sizeof($rs)) {
				$rs = APP::F('user_filter', $rs);
				$pager = APP::N('pager');
				$html = $pager->pagehtml(array(),sizeof($rs));
				TPL :: assign('pager', $html);
				//实例化存储
				$storage = APP::N('clientUser');
				$uid = $storage->getInfo('sina_uid');
				TPL :: assign('sina_uid', $uid);
				$ids = $wb->getFriendIds($uid, null, null, -1, 5000);
				$rs = array_slice($rs, 0, WB_API_LIMIT);
				foreach($rs as $key => $row) {
					if (in_array($row['id'], $ids['ids'])) {
						$rs[$key]['following'] = true;
					}
				}
				TPL :: assign('data', $rs);
			}
		}
		$db = APP :: ADP('db');
		$rs = $db->query('SELECT *FROM ' . $db->getTable(T_FOLLOWERS_COUNT) . ' WHERE uid NOT IN(SELECT uid FROM ' .$db->getTable(T_DISABLED_FOLLOWERS). ') ORDER BY `followers_count` DESC LIMIT 10');
		TPL :: assign('top_followers', $rs);
		TPL::display('search/search_user');
	}
}
