<?php

function get_site_url() {
	$site_url = default_url;
	
	return $site_url;
}


function get_file_url($append = '') {
	$view_url = get_site_url().'assets';
	
	if($append != '') {
		$view_url .= '/'.$append;
	}
	
	return $view_url;
}

function get_module_url($file, $append = '') {
	$module_url = get_site_url();
	
	$module_url_array = explode('\\',dirname($file));
	if(!(count($module_url_array)>1)) {
		$module_url_array = explode('/',dirname($file));
	}
	
	$started = false;
	foreach($module_url_array as $module_folder) {
		if($module_folder == "modules") {
			$started = true;
		}
		if($started) {
			$module_url.= '/'.$module_folder;
		}
	}
	if($append != '') {
		$module_url .= '/'.$append;
	}
	
	return $module_url;
}

function get_module_path($append = '') {
	$module_path = dirname(__DIR__).'/'.$append;
	
	return $module_path;
}

function redirect($url) {
	header('Location: '.$url, true, 302);
	exit;
}

function is_url($url) {
	if (filter_var($url, FILTER_VALIDATE_URL)) {
		return true;
	}
	return false;
}

function get_widget($id='', $title='', $callback = false) {
	if($callback != false && $id != '') {
		if(function_exists($callback)) {
			$return = call_user_func($callback);
			if($return) return $return;
		} 
	}
}

function is_logged_in() {
	
	if(isset($_SESSION) && !empty($_SESSION) && isset($_SESSION['agent_loggedin'])) {
		if($_SESSION['agent_loggedin']) {
			return true;
		} else {
			return false;
		}
	}
}

function get_agent_agency($agent_id) {
	$db = new DB();
	$sql = sprintf("SELECT b.estate_agent_name FROM agents a, agencies b WHERE a.agency = b.estate_agent_no AND agent_id='%s'",$agent_id);
	
	$result = $db->query($sql, true);
	if($result->num_rows > 0) {
		return $result->result['estate_agent_name'];
	} else {
		return '';
	}
}

function get_agent_cea($agent_id) {
	
	$db = new DB();

	$sql = sprintf("SELECT * FROM agents WHERE agent_id='%s'", $agent_id);
	$results = $db->query($sql,true);
	
	return array('cea_prefix' => $results->result['cea_prefix'], 'cea_number' => $results->result['cea']);
}
 