<?php
/*
* 功能为搜索模块.
* 程序及模板都依search_开始, 放在当前模板目录中.
* 编辑时间 2012/5/12 上午 10:20:15 Yuan(fenanr@hotmail.com)
*/
defined('IN_SYSTEM') === false && exit('PHPnew Blog.');

$run = $default_parem[2];

switch($run){
    case 'index';
        // TODO: 搜索功能.
        $html['title'] = '信息搜索';
        $keys = '';
        $default_parem[3] && $keys = $default_parem[3];
        
        # 假如没有登录, 跳到百度
        if(empty($user_uid) === true){
               $hosts = $_SERVER["HTTP_HOST"];
               $url = 'http://www.baidu.com/s?wd=site%3A'.$hosts.'+'.urlencode($keys);
               fun_header($url);
               exit();
        }
        
        common_function_template('search_index');
        break;
    case 'ajax';
            // TODO: 搜索功能ajax实现支持.无界面.
            if(empty($user_uid) === true){
               echo '0';
               exit();
            }
            if(empty($default_get['searchs']) === true){
                echo '0';
                exit();
            }
                
            $search = fun_enhtml($default_get['searchs']);
            $search = trim($search,'_');
            $search = trim($search,'%');
            $search = trim($search,'&');
            $search = trim($search,'*');
            $types = $default_get['types']+0;
            if(empty($search) === true){
               echo '0';
               exit(); 
            }
            $where = '';
            
            if($types !== 0){
                $where = "thread_fid = '$types' AND ";
            }
            
            $query = mysql::query("SELECT thread_id,thread_state, thread_fid, thread_title, thread_view,thread_date FROM DB_thread WHERE $where thread_title LIKE '%{$search}%' ORDER BY thread_id DESC LIMIT 50");
            $new_list = array();
            while(($row = mysql::fetch_array($query)) !== false){
                if(empty($row['thread_title']) === true)
                    continue;
                $flink = fun_parse_url("./index.php?main=cat&{$row['thread_fid']}.html");
                $row['thread_fid'] = '<a href="'.$flink.'" target="_blank">'.$cat_array[$row['thread_fid']]['c_name'].'</a>'; ;
                $row['thread_title'] = str_ireplace($search,'<span style="color:red">'.$search.'</span>',$row['thread_title']);
                $row['thread_date'] = fun_date($row['thread_date']);
                if($row['thread_state']){
                    $row['thread_state'] = '<img src="./Static/Images/stamp/'.$ico_array[$row['thread_state']].'.gif" />';;
                }else{
                    $row['thread_state'] = ' 普通主题 ';
                }
                $row['thread_link'] = fun_parse_url("./index.php?main=read&{$row['thread_id']}.html");
                $new_list[] = $row;
            }
            if(!$new_list)
                exit('0');
            echo json::encode($new_list);
        break;
    default:
        // TODO: 默认跳转到首页.
        fun_header();
        break;
}

