<?php
 
 
//SmartAjaxController functions as output generator for AJAX response
 
class SmartAjaxController {
 
 
    //here we store the array sent as response
 
    private $returnArray=array();
 
    
 
    //here we store all the input variables
 
    private $inputVariables=array();
 
    
 
    public function __construct(){
 
        if(extension_loaded('zlib')){ob_start('ob_gzhandler');} else {ob_start();}
 
    }
 
    
 
    //Here we parse the serialized input variable string to proper input variables
 
    public function setInputVariables($data){
 
        if(!empty($data)){
 
            parse_str($data,$this->inputVariables);
 
        }
 
    }
 
    
 
    //Here we return an input variable for use
 
    public function getVar($key){
 
        if(isset($this->inputVariables[$key])){
 
            return $this->inputVariables[$key];
 
        } else {
 
            return false;
 
        }
 
    }
 
    
 
    //assigning a new key for returning
 
    public function setKey($key,$value){
 
        if($key!=''){
 
            $this->returnArray[$key]=$value;
 
        }
 
    }
 
    
 
    // When class serves no further purpose we echo the response
 
    public function output(){
 
        header('Content-Type: text/html; charset=utf-8');
 
        header('Content-Encoding: gzip');
 
        if(empty($this->returnArray)){
 
            $this->setKey('error',1);
 
            $this->setKey('message','Nothing to return!');
 
        }
 
        echo json_encode($this->returnArray);
 
        ob_end_flush();
 
    }
 
    
 
}
 
?>
 
 |