<?php

#require_once LIBRARY.'modules/controller.php';

class XmlHttpController extends Controller {
    
    private 
$request;
    private 
$response;
    
    public function 
process() {
        
        
RootController::setOutputModuleOnly(true);
        
        
$rawPostData file_get_contents('php://input'); # get raw post data (json), i.e: {"ding":"hallo","xyz":123}
        
$this->request json_decode($rawPostData);            # convert to php object
        
        
$this->response = new stdClass();
        
$this->response->error null;
        
$this->response->result null;
        
        
$this->response->request $this->request;
        
//$respone->postData = $postData;
        
        
if ($this->request !== null) {
            if (
$this->requestField('action')) {
                if (
$this->requestFieldValueIs('action''change_listConfig')) {
                    if (
$this->requestField('listModule')) {
                        if (
$this->requestFieldValueIs('configField''sortField')) {
                            
$currentSortField Session::getInstance()->getListConfig('sortField'null$this->request->listModule);
                            
$currentSortMode  Session::getInstance()->getListConfig('sortMode'null$this->request->listModule);
                            
# toggle sortMode if sortField not changed
                            
if        ($this->requestFieldValueIs('configValue'$currentSortField)  &&  $currentSortMode == 'ASC'  ) {
                                
Session::getInstance()->setListConfig('sortMode''DESC'$this->request->listModule);
                            } else if (
$this->requestFieldValueIs('configValue'$currentSortField)  &&  $currentSortMode == 'DESC'  ) {
                                
Session::getInstance()->setListConfig('sortMode''ASC'$this->request->listModule);
                            }
                            
# change sortField, use ASC for sortMode
                            
else {
                                
Session::getInstance()->setListConfig('sortField'$this->request->configValue$this->request->listModule);
                                
Session::getInstance()->setListConfig('sortMode''ASC'$this->request->listModule);
                            }
                            
$this->response->result 'ok';
                        }
# else { $this->response->error = 'no sortField specified.';
                        #}
                    
} else { $this->response->error 'no listModule specified.';
                    }
                } else { 
$this->response->error 'unkown action specified.';
                }
            } else { 
$this->response->error 'no action specified.';
            }
        } else { 
$this->response->error 'decoding json request failed.';
        }
        
        
$json json_encode($this->response);
        
print_r($json);
    }
    
    private function 
requestFieldValueIs($prop$val) {
        if (isset(
$this->request->$prop)) {
            if (
$this->request->$prop === $val) {
                return 
true;
            }
        }
        return 
false;
    }
    
    private function 
requestField($prop) {
        if (isset(
$this->request->$prop)) {
            return 
true;
        }
        return 
false;
    }
    
}