Location: PHPKode > scripts > Thesaurus tooltip > controller.php
<?PHP
/*
* Thesaurus
*
* @package Thesaurus
* @author $Author: sheiko $  
* @version $Id: controller.php, v 2.0 2007/06/05 15:58:15 sheiko Exp $ 
* @copyright (c) Dmitry Sheiko http://www.cmsdevelopment.com 
*/ 

/**
* Thesaurus Controller Class 
* @package Thesaurus
* @author $Author: sheiko $ 
*/

class Thesaurus {
	
	var $DataSourceFile;
	var $Output;
	var $BodyContent;
	var $Term;
	var $ErrorMsg;
	var $Length;
	var $TermList;
	
	/** 
    * Class Constructor
    * @return object
    */ 	
	
	function Thesaurus() {
		$this->DataSourceFile = "sample.csv";
		$this->BodyContent = false;
		$this->ErrorMsg = false;
		$this->Term = false;
		$this->TermList = "";
		$this->Length = 0;
		$this->Output = '';		
		
		if(isset($_POST["term"])) {
			$this->Term = urldecode($_POST["term"]);
			$this->Term = $this->str2lower($this->Term);
			$this->Term = trim($this->Term);
		}
		return $this;
	}
	/** 
    * Convert UTF-8 string 
    * 
    * @return boolean
    */ 	
	function str2lower($str) {
		if($_POST["casesensitive"]=="on") return $str;
		if(function_exists("mb_strtolower") )
			return  mb_strtolower($str, ((isset($_POST["charset"]) AND $_POST["charset"])?$_POST["charset"]:"UTF-8") );
		else 
			return strtolower($str); 		
	}
	
	/** 
    * Get data and filter data
    * 
    * @return boolean
    */ 		
	function getDataAndFitlering() {
		if(!file_exists($this->DataSourceFile)) { $this->ErrorMsg="Can not find file {$this->DataSourceFile}"; return false; }
		$data = file($this->DataSourceFile);
		$this->Length = count($data);
		foreach ($data as $key => $line) {
			$arr = split(";", $line);
			if($this->str2lower($arr[0])==$this->Term) $this->Output = $arr[1];
			unset($data[$key]);
		}
	}
	
	/** 
    * Get data and parse body content
    * 
    * @return boolean
    */ 		
	function getDataAndParsing() {
		if(!file_exists($this->DataSourceFile)) { $this->ErrorMsg="Can not find file {$this->DataSourceFile}"; return false; }
		$data = file($this->DataSourceFile);
		$this->Length = count($data);
		foreach ($data as $key => $line) {
			$arr = split(";", $line);
			$this->TermList .= "'{$arr[0]}',";
		}
		$this->TermList = preg_replace("/,$/", "", $this->TermList);
	}	

	/** 
    * Respond with JSON generation
    * 
    * @return boolean
    */ 		
	function execRespond() {
		if(isset($_POST["charset"]) AND $_POST["charset"])
			header("Content-type: text/html; charset={$_POST["charset"]}");
		else
			header("Content-type: text/html; charset=UTF-8");

		switch ($_POST["call"]) {
			case "getDataAndParsing":
				$this->getDataAndParsing();
				break;
			case "getDataAndFitlering":
				$this->getDataAndFitlering();
				break;				
		}
		
		if($this->Length==0) return false;
		$out = '{
		"thlength" : '.$this->Length.',
		"termlist" : ['.$this->TermList.'],
		"errormsg" : "'.$this->ErrorMsg.'",
		"content" : "'.addslashes($this->Output).'",
		"term" : "'.(isset($_POST["term"])?urldecode($_POST["term"]):'').'"
		}';
		print $out;
	}
	
}

$th = new Thesaurus();
$th->execRespond();
?>
Return current item: Thesaurus tooltip