<?php

	/*
	 * @author	Martijn Polak
	 * @since	3 aug 2004
	 * @name	General purpose hepler functions
	 */

	// Includes a component from the inc/components/ dir. The first argument can be an array containing all 
	// arguments (when called from a template) or just the component name (when called manually). When
	// calling manually use $param_b for all other variables.
	// Note: use template names without the .tpl extention.
	function include_component($param_a, $param_b = null) {
		
		global $componentSettings;
		global $globalSettings;
		global $g_obj_html;
		global $g_obj_ti3a0365;
		global $g_obj_tich0105;
		global $g_arr_css;

		if (!is_array($param_a) && is_array($param_b)) {
			$param_b['name'] = $param_a;
			$param_a = $param_b;
		}

		$file = $globalSettings->general->paths->absolute->inc.'components/'.$param_a['name'].'.php';

		if (file_exists($file)) {

			if ($param_a['template']) $param_a['template'] .= '.tpl';
			$componentSettings = $param_a;
			include $file;
			unset($componentSettings);

		}

	}

	function tpl_assign_nav_path($params) {
		global $g_obj_html, $g_obj_tdyn0302;

		$g_obj_html->createObject('tdyn0302', 'troyceDynamicNavigation()');

		if (is_object($g_obj_tdyn0302) && is_object($g_obj_html)) {
			if (isset($params['object']) && isset($GLOBALS[$params['object']])) {

				$GLOBALS[$params['object']]->setTemplateVariable('navigation_path', $g_obj_tdyn0302->getNavigationPath($params['id']));
			} else {
				$g_obj_html->setTemplateVariable('navigation_path', $g_obj_tdyn0302->getNavigationPath($params['id']));
			}
			
		}

	}

	function array_rearrange($params,&$smarty) {
		$arr = $params['arr'];
		$index_of_value = $params['index_of_value'];
		$index_of_index = $params['index_of_index'];

		$return = array();
		foreach ($arr as $index => $row) {
			if ($index_of_index) {
				$return[$row[$index_of_index]] = $row[$index_of_value];
			} else {
				$return[$index] = $row[$index_of_value];
			
			}
		}
		
		if ($params['assign']) {
			$smarty->assign($params['assign'],$return);
		} else {
			return $return;
		}
	}


	function rs2arr(&$rs, $key_as_index=false) {
	
		$new_arr = array();

		while ($r = $rs->row()) {
			if (count($r) == 1) {
				$value = current($r);
			} elseif (count($r) == 2 && $key_as_index !== false) {
				$tmp_r = $r;
				unset($tmp_r[$key_as_index]);
				$value = current($tmp_r);
			} else {
				$value = $r;
				
			}
			if ($key_as_index && isset($r[$key_as_index])) {
				$new_arr[$r[$key_as_index]] = $value;
			} else {
				$new_arr[] = $value;
			}		

			$rs->moveNext();
		}
		$rs->Close();

		return $new_arr;

	}
?>