<?php
/**
 * @version        $Id: actionsearch_class.php 1 8:26 2010年7月12日Z tianya $
 * @package        DedeCMS.Administrator
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license        http://help.dedecms.com/usersguide/license.html
 * @link           http://www.dedecms.com
 */
class ActionSearch 
{
    var $keyword;
    var $asarray = array();
    var $result    = array();
    
    //初始化系統
    function ActionSearch($keyword){
        $this->asarray = $this->GetSearchstr();
        $this->keyword = $keyword;
    }
    
    function GetSearchstr()
    {
        require_once(dirname(__FILE__)."/inc/inc_action_info.php");
        return is_array($actionSearch)? $actionSearch : array();
    }
    
    function search(){
        $this->searchkeyword();
        return $this->result;
    }
    
    /**
     *  遍歷功能配置項進行關鍵詞匹配
     *
     * @return    void
     */
    function searchkeyword(){
        $i = 0; //數組序列索引
        foreach ($this->asarray as $key=>$value) 
        {
            //對二級項目進行匹配
            if(is_array($this->asarray[$key]['soniterm']))
            {
                foreach ($this->asarray[$key]['soniterm'] as $k=> $val) 
                {
                    //進行權限判斷
                    if(TestPurview($val['purview']))
                    {
                        //如果有操作權限
                        if($this->_strpos($val['title'], $this->keyword) !== false || $this->_strpos($val['description'], $this->keyword)!== false)
                        {
                            //一級項目匹配
                            $this->result[$i]['toptitle'] = $this->redColorKeyword($this->asarray[$key]['toptitle']);
                            $this->result[$i]['title'] = $this->redColorKeyword($this->asarray[$key]['title']);
                            $this->result[$i]['description'] = $this->redColorKeyword($this->asarray[$key]['description']);
                            //二級項目匹配
                            $this->result[$i]['soniterm'][] = $this->redColorKeyword($val);
                        }
                    }
                }
            }
            $i++;
        }
    }

    /**
     *  加亮關鍵詞
     *
     * @access    public
     * @param     string  $text  關鍵詞
     * @return    string
     */
    function redColorKeyword($text){
        if(is_array($text))
        {
            foreach ($text as $key => $value) {
                if($key == 'title' || $key == 'description') 
               {
                    //僅對title,description進行數組替換
                    $text[$key] = str_replace($this->keyword,'<font color="red">'.$this->keyword.'</font>',$text[$key]);
               }
            }
        } else {
            $text = str_replace($this->keyword,'<font color="red">'.$this->keyword.'</font>',$text);
        }
        return $text;
    }
    
    function _strpos($string,$find) 
    {
        if (function_exists('stripos'))  return stripos($string,$find);
        return strpos($string,$find);
    }
}