<?php

  
// {{{ Benchmark Start
  
require_once('Benchmark/Timer.php');
  
$timer = new Benchmark_Timer();
  
$timer->start();
  
// }}}

  
require_once('Search.php');

  
define('SMARTY_DIR',dirname(__FILE__).'/Smarty/');
  require_once(
SMARTY_DIR.'Smarty.class.php');

  
$tplFile 'search.tpl';

  
$smarty = new Smarty();
  
$smarty->template_dir './tpl/templates/';
  
$smarty->compile_dir  './tpl/templates_c/';
  
$smarty->config_dir   './tpl/config/';
  
$smarty->cache_dir    './tpl/cache/';
  
$smarty->caching      true;
  
$smarty->cache_id     = (isset($_GET['q'])) ? $_GET['q'] : '';


  if (
count($_GET) && !$smarty->is_cached($tplFile,$smarty->cache_id)) {
      try {
          
$search = new Search;
      } catch (
Exception $error) {
          die(
$error->getMessage());
      }

      
$search->table 'tb_content';
      
$search->addSearchField('title',2);
      
$search->addSearchField('body',1);

      
$search->setQuery($_GET['q']);
      
$result $search->runSearch();
      if (
PEAR::isError($result)) {
          echo 
$result->getMessage();
      } else {
          
$searchResults $result;

      }
  } else {
      
$searchResults = array();
  }

  if (!isset(
$_GET['q'])) {
      
$_GET['q'] = '';
  } elseif (isset(
$search) && $search instanceof Search) {
      
$n 0;
      foreach (
$searchResults as $key => $row) {
          if (
$n++ == 0) {
              
$highScore $row['relevancy'];
          } 
    
          
$searchResults[$key]['myScore'] = (($row['relevancy'] / $highScore) * 100);
      }

      
$smarty->assign('altSearch',$search->getAlternateSearch());
      
$smarty->assign('sql',$search->getSQL());
  }


  
$timer->stop();

  
$smarty->assign('timer',$timer);
  
$smarty->assign('results',$searchResults);
  
$smarty->assign('q',$_GET['q']);

  if (
$smarty->is_cached($tplFile,$smarty->cache_id)) {
      echo 
'<div id="cache">You are viewing cached results ...</div>';
  }

  
$smarty->display($tplFile,$smarty->cache_id);

?>