<?php

class Handler extends RootRegistry {

	var $DB;
	var $Security;
	var $Server;

	function __construct($name = 'Handler', $calledbyson = true) {
		global $DatabaseBroker, $Server;
		//print '<li>init - '.$name.'<br>';
		//$this->RootRegistry($name);
		parent::__construct($name);
		$this->DB = $DatabaseBroker;
		$this->Server = $Server;
		if($calledbyson) {
			if (isset($_SESSION['SecurityHandler']))
				$this->Security = $_SESSION['SecurityHandler'];
		}
	}

	function justLog($request=array()) {
		$this->DB->begin();
		empty($request['type']) and $request['type'] = 0;
		empty($request['entity']) and $request['entity'] = 0;
		empty($request['event']) and $request['event'] = 0;
		empty($request['value']) and $request['value'] = ' ';
		empty($request['object']) and $request['object'] = 0;
		$log = $this->setLogEvent($request['type'],$request['entity'],$request['event'],$request['value'],$request['object']);
		$this->DB->commit();
		return $log;
	}

	function setLogEvent($type = 0,$entity = 0,$event = 0,$value = '',$object = 0) {
		$log = new Log();
		$log->type = $type;
		$log->entity = $entity;
		$log->date = 'now()';
		$log->member = is_object($this->Security)? $this->Security->currentuser->id : $this->currentuser->id;
		$log->event = $event;
		$log->value = $value;
		$log->object = $object;
		$this->DB->processObject($log);
		return $log;
	}

	function getSettings($request=array()) {
		if(!empty($request['moduleid'])) {
			$module = new Module();
			$module->settings = new ObjectsList();
			$module->settings->type = new ModuleSetting();
			$module->id = $request['moduleid'];
			$this->DB->initObject($module);
			return $module;
		}
	}

	function parseRequest($request = array(), $obj) {
		$property_values = get_object_vars($obj);
		$property_names= array_keys($property_values);
		foreach($request as $key => $value){
			if(in_array($key, $property_names)){
				$this->parseObject($key, $property_values[$key], $value, $obj);
			}
		}
		return $obj;
	}

	function parseObject($property_name, $property_value, $request_value, $obj) {
		$type = gettype($property_value);
		switch($type){
			case 'object':
				if(strtolower(get_class($property_value)) == 'objectslist') $obj->{$property_name} = $this->parseObjectsList($request_value, clone $obj->{$property_name});
				else $obj->{$property_name} = $this->parseRequest($request_value, clone $obj->{$property_name});
			break;
			default: $obj->{$property_name} = $request_value; break;
		}
	}

	function parseObjectsList($request_value, $obj) {
		if(is_array($request_value)){
			foreach($request_value as $index => $value ) {
				$obj->data[] = $this->parseRequest($value, clone $obj->type);
			}
		}
		return $obj;
	}

	function cleanError(&$object) {
		$object->_error = '';
	}

	function getLimit(&$request) {
		$limit = '';
		if(empty($request['curPageNumber'])) {
			$request['curPageNumber']='1';
			$limit = array('0',$request['pageRecordsQty']);
		}
		else {
			$offset = $request['pageRecordsQty']*($request['curPageNumber']-1);
			$limit = array($offset,$request['pageRecordsQty']);
		}
		return $limit;
	}

	function getOrderBy($request) {
		$orderby = '';
		if(empty($request['sortColumn'])) $request['sortColumn']='';
		if(empty($request['sortType'])) $request['sortType']='';
		if(empty($request['sortColumn'])) $orderby='id';
		else $orderby = $request['sortColumn']." ".$request['sortType'];
		return $orderby;
	}

	function countListingsQty($catergory_id,$listingtable,$categorytable) {
		$catlist[]=$catergory_id;
		$catlist=$this->getSubCategoriesList($catergory_id,$categorytable,$catlist);
		$selected=$this->DB->SelectArray(Array($listingtable),'count(*) as qty',array('category'=>array('in'=>$catlist)),0,0,0,array(),'CND_COMPLEX',array());		
		return $selected[0]['qty'];
	}

	function getSubCategoriesList($catergory_id,$categorytable,$catidarray=array()){
		$selected=$this->DB->SelectArray(Array($categorytable),'id',Array('parent_category'=>$catergory_id));
		if(!empty($selected)){
			foreach($selected as $k=>$v){
				$catidarray[]=$v['id'];
				$catidarray=array_merge($catidarray,$this->getSubCategoriesList($v['id'],$categorytable));
			}
		}
		return $catidarray;
	}

	function getGrangParentCategory($catid){
		$gpid=0;
		$catTree=$this->selectCategoriesTree();
		foreach($catTree as $k=>$v) {
			if($k==$catid) {
				$gpid=$k;
				break;
			}
			else if($this->walkForCatTree($catTree[$k],$catid)){
				$gpid=$k;
				break;
			}
		}
		return $gpid;
		
	}

	function walkForCatTree($catTree,$catid){
		if(is_array($catTree['subcat'])) {
			foreach($catTree['subcat'] as $k=>$v){
				if($k==$catid)	return true;
				else if($this->walkForCatTree($catTree['subcat'][$k],$catid)) return true;
			}
		}
		return false;
	}

	function justStub($request){
		return $request;
	}

	function getUID($table = 'module') {
		$uid = md5(uniqid(time(),1));
		$UID = $this->DB->SelectSingle($table,'count(uid)',array('uid' => $uid));
		return empty($UID)? $uid : $this->getUID();
	}
	
	function _parseObject($object) {
		if(strtolower(get_class($object)) == 'objectslist') return $object->data;
		else return get_object_vars($object);
	}

	function _parseContent($object) {
		isset($TemplatedElements) or $TemplatedElements = array();
		foreach(is_object($object)? $this->_parseObject($object) : $object as $key => $value)
			$TemplatedElements[$key] = is_object($value) || is_array($value)? $this->_parseContent($value) : $value;
		return $TemplatedElements;
	}
}
?>