<?php
/**
 * File handling open static Pages WriteMode
 *
 * PHP version 5.0
 *
 * @category	Anova
 * @package		StaticPage
 * @author 		shankar_76ag08
 * @copyright	Copyright (c) 2010 {@link http://www.agriya.com Agriya Infoway}
 * @license		http://www.agriya.com Agriya Infoway Licence
 * @version		SVN: $Id:
 * @since 		2008-12-22
 **/
require_once('../common/configs/config.inc.php');
$CFG['site']['is_module_page'] = $CFG['admin']['index']['anova_module'];
$CFG['is']['ajax_page'] = false;
if(isset($_REQUEST['ajax_page']) and $_REQUEST['ajax_page'])
	{
		$CFG['is']['ajax_page'] = true;
		$CFG['html']['is_use_header'] = false;
		$CFG['html']['is_use_footer'] = false;
	}

$CFG['mods']['is_include_only']['non_html_header_files'] = true;
require($CFG['site']['project_path'].'common/application_top.inc.php');
echo  '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';

class SuggestHandler extends FormHandler
	{
		/**
		 * SuggestHandler::getSuggestions()
		 *
		 * @param mixed $keyword
		 * @return
		 */
		public function getSuggestions($keyword)
		  	{
				$output = '<response>';
				if($keyword != '')
				  	{
					  	$sql = 'SELECT question, seo_title'.
								' FROM ' .$this->CFG['db']['tbl']['questions'].
							 	' WHERE question LIKE "' .'%'. $keyword . '%"'.
							 	' AND publish_status =\'Yes\''.
							 	' AND status NOT IN(\'Blocked\',\'ToActivate\',\'Deleted\')'.
								' ORDER BY date_asked DESC'.
							 	' LIMIT 0,'.$this->CFG['admin']['suggest_records_allowed'] ;
				  	}
				else
				 	{
				 		 $sql = 'SELECT question, seo_title'.
			  		     		' FROM ' .$this->CFG['db']['tbl']['questions'].
						 		' WHERE question=""';
					}
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
			    if (!$rs)
				    trigger_db_error($this->dbObj);
				if ($rs->PO_RecordCount())
					{
						while($row = $rs->FetchRow())
							{
								$output .= '<name>' . wordWrapManual($row['question'],$this->CFG['admin']['question']['suggest_line_length']) . '</name>';
								$output .= '<link>' . getAnovaUrl('answer', '?title='.$row['seo_title'], $row['seo_title'].'/') . '</link>';
							}
					}
				 $output .= '</response>';
				return $output;
		  }

		/**
		 * SuggestHandler::setXmlHeaderStart()
		 *
		 * @return
		 */
		public function setXmlHeaderStart()
			{
				global $CFG;
				ob_start();
				header("Pragma: no-cache");
				header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
				header("Expires: 0"); // Date in the past
				header('Content-Type: text/xml; charset=utf-8');
			}
	}
//<<<<<-------------- Class SuggestHandler begins ---------------//
//-------------------- Code begins -------------->>>>>//
$suggest = new SuggestHandler();
//form fields
$suggest->setFormField('keyword', '');
$suggest->sanitizeFormInputs($_REQUEST);
$keyword = $suggest->getFormField('keyword');
// clear the output
// headers are sent to prevent browsers from caching
$suggest->setXmlHeaderStart();
// send the results to the client
// send the results to the client
if(ob_get_length())
	ob_clean();
echo $suggest->getSuggestions($keyword);
$suggest->includeAjaxFooter();
?>