<?
require_once('class.stdModule.php');
require_once($globalSettings->general->paths->absolute->inc.'class.xmltoarray.php');

class cghs0100 extends stdModule {

	function cghs0100() {
	
		$this->stdModule('cghs0100');
		$this->_settings->templates->vas			= "vas.tpl";
		$this->_settings->assignTemplateFunction	= '$this->_assignGlobals();';
		//$this->_variables->public->parent_id 		= 1;
	}

	function _getVasData($rea_id) {
		
		if (!is_numeric($rea_id)) return false;
	
		global $g_obj_trea0306;
		global $g_obj_html;
		$g_obj_html->createObject('trea0306', 'troyceReaction()');
		
		$reaction = $g_obj_trea0306->getReaction($rea_id);
		$xmlObj = new XmlToArray($reaction['rea_form']);
		$arrayData = $xmlObj->createArray();
		
		$rea_data = array();
		$rea_data['vas_rea_id'] 		= $reaction['rea_id'];
		$rea_data['vas_qst_name']		= $reaction['rea_usr_name'];
		$rea_data['vas_qst_email']		= $reaction['rea_usr_email'];
		$rea_data['vas_qst_date']		= $reaction['rea_createdate'];
		$rea_data['vas_question']		= $arrayData['reaction']['attributes'][0]['attribute'][2]['answer'];
		
		return $rea_data;
		
	}
	
	function printVasForm($rea_id) {
	
		if (!is_numeric($rea_id)) return false;
		
		$rea_data = $this->_getVasData($rea_id);
		
		$this->_smrt->assign('data', $rea_data);
		$this->_smrt->display($this->_settings->templates->vas);
	
	}
	
	function saveVasForm($data) {
		
		if (!is_numeric($data['vas_rea_id'])) return false;
		
		$rea_data = $this->_getVasData($data['vas_rea_id']);
		
		$check = $this->_db->Query("select count(vas_id) as count from ghs_vas where vas_rea_id = ".$data['vas_rea_id']);
		
		if ($check->Row('count') == '0') {
		
			$query = "insert into ghs_vas (
					vas_rea_id
					, vas_qst_name
					, vas_qst_email
					, vas_question
					, vas_qst_date
					, vas_specialist
					, vas_answer
					, vas_answer_date
				)
				values (
					'".mysql_escape_string($rea_data['vas_rea_id'])."'
					, '".mysql_escape_string($rea_data['vas_qst_name'])."'
					, '".mysql_escape_string($rea_data['vas_qst_email'])."'
					, '".mysql_escape_string($rea_data['vas_question'])."'
					, '".mysql_escape_string($rea_data['vas_qst_date'])."'
					, '".mysql_escape_string($data['vas_specialist'])."'
					, '".mysql_escape_string($data['vas_specialist'])."'
					, NOW()
				)";
			
		
			$ok = $this->_db->Execute($query);
		
			if ($ok) {
				$this->_sendVasAnswer($rea_data, $data);
				$this->_smrt->assign('info', 'Antwoord werzonden.');
				return true;
			}
	
		}
		$this->_smrt->assign('error', 'Deze vraag is al beantwoord.');
		return false;
	
	}
	
	function _sendVasAnswer($rea_data, $data) {
		
		if ($data['vas_specialist'] != '' && $data['vas_specialist'] != '') {
			$email_vars = array(
				'code'		=> 'VAS_ANSW',
				'org_id'	=> $this->_variables->public->org_id,
				'lng_code'	=> $this->_variables->public->lng_code,
				'address'	=> array('to' => $rea_data['vas_qst_email'], 'toname' => $rea_data['vas_qst_name']),
				'variables'	=> array(
									'NAME' 			=> $rea_data['vas_qst_name']
								,	'QUESTION' 		=> $rea_data['vas_question']
								,	'ANSWER' 		=> $data['vas_answer']
								,	'SPECIALIST'	=> $data['vas_specialist']
								
								)
				, 'debug'	=> false
			);
			$err = $this->_tcc->doCall("SEND_EMAIL", $email_vars, $return , true);
			if ($err->id != ICH_RESPONSE_OK) die($err->msg);
		}
		return true;
	}
	
	
	function saveSearchEntry($uri, $navcode) {
		
		if ($uri != '' && $navcode != '') {
			
			$reversed_subdomain_arr = array_flip($this->_settings->subdomains);
			
			$query = "insert into ghs_srch (srch_uri, srch_subdomain) 
						values (
						'".mysql_escape_string($uri)."'
						, '".mysql_escape_string($reversed_subdomain_arr[$navcode])."'
						)";
			$rs = $this->_db->Execute($query);
		}
	
	}
	
	function selectUrlPrefix ($uri) {
		
		$rs = $this->_db->Query("select srch_subdomain as subdomain from ghs_srch 
										where srch_uri = '".mysql_escape_string($uri)."'");
		return ($rs->Row('subdomain') != '') ? "http://".$rs->Row('subdomain').".".$_SESSION['session_domain'] : 'http://'.$_SESSION['session_maindomain'];

		
	}
		
	function truncateDbTable() {	
		$this->_db->Execute('truncate table ghs_srch');
		return true;
	}
	
	
	
}
?>