<?php

class Response {
 
	var $m = array();

	function Response(){}

 	static function sendResponse($success, $data=null, $cat=null, $name=null){
		
	 	$retObj = (object) array();
		$retObj->success = $success;
		$retObj->data = $data;
		if($cat!=null){
		 	$retObj->messageId = "backend_".$cat."_".$name;
		 	$retObj->message = Message::getMessage($cat, $name);
		}
		
		//save to logs
		if(Config::get("logger"))
			Logger::saveResponse($retObj);
		
		// add systemError
		if(count(Error::getErrorArray())>0)
			$retObj->systemError = Error::getErrorArray();
		
		echo DataSerializer::serialize($retObj);
		//echo DataSerializer::serialize($retObj);
		exit;
 	}
 	static function sendErrorResponse($error_id, $error_meesage, $data=null){
 	 
		global $config, $ds;
		
	 	$retObj = (object) array();
		$retObj->success = false;
		$retObj->data = $data;
		$retObj->message = "(".$error_id.") ". $error_meesage;
		
		$retObj->messageId = "backend_err_no_".$error_id;
		
		//save to logs
		if(Config::get("logger"))
			Logger::saveResponse($retObj);
			
		// add systemError
		if(count(Error::getErrorArray())>0)
			$retObj->systemError = Error::getErrorArray();
		
		echo DataSerializer::serialize($retObj);
		exit;
	  	
 	}	
	static function sendOKResponse($data=null, $cat=null, $name=null){
		self::sendResponse(true, $data, $cat, $name);
	}
	
	static function sendNotice($cat, $name){
		self::sendResponse(false, null, $cat, $name);
	  	//self::sendErrorResponse("NOTICE", Message::getMessage($cat, $name));
	}
	
	static function sendBackendMessageResponse($message){
 	 
		global $config, $ds;
		
	 	$retObj = (object) array();
		$retObj->success = false;
		$retObj->backendMessage = $message;

		//save to logs
		if(Config::get("logger"))
			Logger::saveResponse($retObj);
			
		// add systemError
		if(count(Error::getErrorArray())>0)
			$retObj->systemError = Error::getErrorArray();
		
		echo DataSerializer::serialize($retObj);
		exit;
	  	
 	}	

}