<?php
   ini_set('display_errors', 1);
   ini_set('error_reporting', E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED & ~E_WARNING);
   //ini_set('error_reporting', E_ALL);
   

// karena PHP4 ndak support JSON
require_once 'main/lib/JSON.php';

// Future-friendly json_encode
if (!function_exists('json_encode')) {

    function json_encode($data) {
        $json = new Services_JSON();
        return( $json->encode($data) );
    }

}

// Future-friendly json_decode
if (!function_exists('json_decode')) {

    function json_decode($data, $condition = true) {
        $json = new Services_JSON();
        //return( $json->decode($data) );
        $ret = $json->decode($data);
        if ($condition) {
            return ObjToArr($ret);
        } else {
            return $ret;
        }
    }

}

function ObjToArr($data) {
    if (is_object($data)) $data = get_object_vars($data);
    return is_array($data) ? array_map(__FUNCTION__, $data) : $data;
}

   //Load Configuration
	require_once 'config/configuration.class.php';
	$cfg = new Configuration();
	
   $cfg->Load('base.conf.php');

	//Load Library Class
	require_once $cfg->GetValue('app_lib') . 'adodb/adodb.inc.php';
		
   //Load Application Class
	require_once $cfg->GetValue('app_service') . 'server/base_server.service.class.php';	
   require_once $cfg->GetValue('app_data') . 'database_connected.class.php';	   
	   
	$server->register("Dispatch");
   //$HTTP_RAW_POST_DATA = "";
   
   function Dispatch($serviceParams, $dataParams) {
      global $cfg;      
      /*
         $serviceParams contains array for request handler
         pModule : Nama Module
         pSub : Sub Module. Ex : di Module Manajemen User, ada Sub Modul Kategori User
         pAct: Prefix Sub Module. Ex : View, Add, Edit, atau lainnya..
      */
      header('Cache-Control: no-cache, must-revalidate');
      header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
/*
      $serviceParams = $_REQUEST['serviceParams'];
      $dataParams = $_REQUEST['dataParams'];
*/
      if(!empty($serviceParams)){
         foreach($serviceParams as $key => $value) {
            $$key = $value;         
         }
      }
      $valid = false;
      if (!empty($pModule) && !empty($pAct) && !empty($pSub)) {
         $fileStatus = $cfg->GetServiceBusinessHandler($pModule, $pSub);
         $valid = true;
      } else {         
         $fileStatus = false;
         $valid = false;
      }      
      if (false !== $valid){
         if (false == $fileStatus) { 
            // return no handler message
            $server_fault = 'No file handler found for this request';
            return json_encode($server_fault);
         } else {
            //dirty hack :: use ob_start() for avoiding nusoap resulting response is not type of xml
            ob_start(); 
            require_once $fileStatus;
            
            $className = str_replace(' ', '', ucwords($pSub));
            $functionName = ucfirst($pAct);
            $classHandler = new $className($cfg, $dataParams); 
               // check if connection has failed to inisiate ??
            if($classHandler->GetProperty("IsError")) {
               ob_end_clean();
               $server_fault = $classHandler->GetProperty("ErrorMessage");
               $classHandler->DestroyObject();
               return json_encode($server_fault);              
            }
            else {
               // check is there any method with $functionName belong to classhandler declared ???
               if(method_exists($classHandler, $functionName)) {
                  $result = $classHandler->$functionName();
                  ob_end_clean();
                  
                  // echo data as soapval if not false
                  if(false !== $result) {
                     $return_value = $result;
                     //$classHandler->DestroyObject();
                     return json_encode($return_value);
                  }
                  else {
                     $server_fault = $classHandler->GetProperty("ErrorMessage");
                     $classHandler->DestroyObject();
                     return json_encode($server_fault);
                  }
               }
               else {
                  ob_end_clean();
                  $server_fault = 'No method declared named '.$functionName.' on class '.$className;
                  $classHandler->DestroyObject();
                  return json_encode($server_fault);
               }
            }
         }
      } else {
         // return no handler message
         $server_fault = 'Incorrect service request params, operation not succeed';
         echo json_encode($server_fault);
      }      
   }
   
   //comment this line for debugging purpose
   
   //run patch query 
   $reqServiceParams = array('pModule' => 'apps_debug', 'pSub' => 'apps_debug', 'pAct' => 'patchDB');
   $reqServiceData = array();
   //$reqServiceData = array();
   $result1 = Dispatch($reqServiceParams, $reqServiceData);
   //$result = Dispatch();
   echo '<pre>';
   print_r($result1);
   // echo "<br />";
  //$server->service(trim($HTTP_RAW_POST_DATA));
   //Dispatch();
   /**/
   //uncomment this part for debugging purpose
   
   //$reqServiceParams = array('pModule' => 'apps_home', 'pSub' => 'apps_home', 'pAct' => 'getDataSettingSemester');
   $reqServiceParams = array('pModule' => 'apps_home', 'pSub' => 'apps_home', 'pAct' => 'setSemPelaporan');
   $reqServiceData = array('20132','2013/2014 Genap');
   //$reqServiceData = array();
   echo '<pre>';
    $result = Dispatch($reqServiceParams, $reqServiceData);
   //$result = Dispatch();
   print_r($result);
   echo '</pre>';/**/
?>