<?php
/*

  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

  Released under the GNU General Public License
  
  Freeway eCommerce from ZacWare
http://www.zac-ware.com/freeway

Copyright 2007 ZacWare Pty. Ltd
*/

 // Check to ensure this file is included in Freeway!
defined('_FEXEC') or die(); 

 function tep_get_subscription_output_tree($parent_id = '0',&$content,$default,$subscription_category_id=0) {
    global $languages_id,$selected_name,$login_id,$login_groups_type;

	// iterate with nested category
	
	$subscription_where = "";
	if($subscription_category_id>0)$subscription_where = " suc.subscription_categories_id='" . $subscription_category_id . "' ";
	$subscription_category_query = tep_db_query("select suc.subscription_categories_id, sucd.subscription_categories_name, suc.parent_id from " . TABLE_SUBSCRIPTION_CATEGORIES . " suc, " . TABLE_SUBSCRIPTION_CATEGORIES_DESCRIPTION . " sucd where suc.subscription_categories_id = sucd.subscription_categories_id and sucd.language_id = '" . (int)$languages_id . "' and suc.parent_id = '" . (int)$parent_id . "' " . $subscription_where . " order by suc.sort_order, sucd.subscription_categories_name");
	while ($subscription_category = tep_db_fetch_array($subscription_category_query)) {
		$content.= '<optgroup style="color:#0000FF" label="' . write_subscription_category_space($subscription_category['subscription_categories_id']) . tep_output_string($subscription_category['subscription_categories_name'], array('"' => '&quot;', '\'' => '&#039;', '<' => '&lt;', '>' => '&gt;')) . '">';
		// select the list of subscription of particular category
		$subscription_query=tep_db_query("select sud.subscription_name,sud.subscription_id from " . TABLE_SUBSCRIPTIONS . ' su, ' . TABLE_SUBSCRIPTIONS_DESCRIPTION . " sud, " . TABLE_SUBSCRIPTION_TO_SUBSCRIPTION_CATEGORIES . " sucd where sucd.subscription_categories_id='" . $subscription_category['subscription_categories_id'] . "' and sud.language_id = " . (int)$languages_id . " " . SUBSCRIPTION_CONDITION_STATUS. " and su.subscription_id=sucd.subscription_id and su.subscription_id=sud.subscription_id and su.subscription_type!='P' and su.subscription_type!='M' order by sud.subscription_name");
	//	echo "select sud.subscription_name,sud.subscription_id from " . TABLE_SUBSCRIPTIONS . ' su, ' . TABLE_SUBSCRIPTIONS_DESCRIPTION . " sud, " . TABLE_SUBSCRIPTION_TO_SUBSCRIPTION_CATEGORIES . " sucd where sucd.subscription_categories_id='" . $subscription_category['subscription_categories_id'] . "' and sud.language_id = " . (int)$languages_id . " " . SUBSCRIPTION_CONDITION_STATUS. " and su.subscription_id=sucd.subscription_id and su.subscription_id=sud.subscription_id and su.subscription_type!='P' order by sud.subscription_name";
		while ($subscription=tep_db_fetch_array($subscription_query))
		{
			$content .= '<option style="color:#000000" value="' . tep_output_string($subscription['subscription_id']) . '"';
			if ($default == $subscription['subscription_id']){
				$selected_name=$subscription['subscription_name'];
				$content .= ' SELECTED';
			}
			$content .= '>' . tep_output_string($subscription['subscription_name'], array('"' => '&quot;', '\'' => '&#039;', '<' => '&lt;', '>' => '&gt;')) . '</option>';
		}    
		tep_get_subscription_output_tree($subscription_category['subscription_categories_id'], $content,$default);
		$content.='</optgroup>';
	}

}
	function write_subscription_category_space($category_id){
	    global $languages_id,$selected_name,$login_id,$login_groups_type;
		$sql = "select parent_id,sc.subscription_categories_id,subscription_categories_name from " . TABLE_SUBSCRIPTION_CATEGORIES . " sc," . TABLE_SUBSCRIPTION_CATEGORIES_DESCRIPTION . " scd where sc.subscription_categories_id=scd.subscription_categories_id and scd.language_id = '" . (int)$languages_id . "' and sc.subscription_categories_id=" . $category_id;
		$query = tep_db_query($sql);
		$result = tep_db_fetch_array($query);
		if($result['parent_id']>0){
			return "&nbsp;&nbsp;" . write_subscription_category_space($result['parent_id']);
		}else{
			return "";
		}
	}
	// Output a form pull down menu
	function tep_draw_subscription_select_menu($name, $values, $default = '', $parameters = '', $required = false,$initial_category_id=0,$need_all=true,$initial_parent_id=0) {
		global $selected_name;
		$field = '<select name="' . tep_output_string($name) . '"';
	
		if (tep_not_null($parameters)) $field .= ' ' . $parameters;
		$field.=' class="inputNormal" onBlur="javascript:toggle_focus(this,1);" onFocus="javascript:toggle_focus(this,2);"';

		$field .= '>';
		if($need_all=='true')
		$field .= '<option style="color:#000000" value="-1">' . TEXT_ALL_SUBSCRIPTIONS . '</option>';
		else if($need_all=='1')
		$field .= '<option style="color:#000000" value="-1">' . TEXT_SELECT . '</option>';
		
		if (empty($default) && isset($GLOBALS[$name])) $default = stripslashes($GLOBALS[$name]);
	
		$content="";
		tep_get_subscription_output_tree($initial_parent_id,$content,$default,$initial_category_id);
		$field.=$content;
	$field .= '</select>';
	
	if ($required == true) $field .= TEXT_FIELD_REQUIRED;
	
	return $field;
	}
	// get the list of category and events in a tree view
	function tep_get_subscription_display_tree_grouped($parent_id = '0', $spacing = 0, $exclude = '', $subscription_category_tree_array = '', $include_itself = false) {
		global $languages_id,$login_id,$login_groups_type;
	
		$login_option="";
		if ($login_groups_type==TEXT_INSTRUCTOR_ENTRY){
			$login_option=" and e.events_instructor=" . $login_id;
		}
		if (!is_array($subscription_category_tree_array)) $subscription_category_tree_array = array();
		//if ( (sizeof($events_category_tree_array) < 1) && ($exclude != '0') ) $events_category_tree_array[] = array('id' => '0', 'text' => TEXT_TOP,'type'=>'C','spacing'=>$spacing);
	
		if ($include_itself) {
		  $subscription_category_query = tep_db_query("select subscription_name from " . TABLE_SUBSCRIPTIONS_DESCRIPTION . " where language_id = '" . (int)$languages_id );
		  $subscription_category = tep_db_fetch_array($category_query);
		  $subscription_category_tree_array[] = array('id' => $parent_id, 'text' => $subscription_category['subscription_name'],'type'=>'C','spacing'=>$spacing);
		}
		$subscription_categories_query = tep_db_query("select subscription_id, subscription_name from " . TABLE_SUBSCRIPTION . " language_id = '" . (int)$languages_id . "' and order by subscription_name");
		while ($subscription_categories = tep_db_fetch_array($subscription_categories_query)) {
		  if ($exclude != $subscription_categories['subscription_id']) $subscription_category_tree_array[] = array('id' => $subscription_categories['subscription_id'], 'text' =>$subscription_categories['subscription_name'],'type'=>'C','spacing'=>$spacing);
		  $subscription_category_tree_array = tep_get_subscription_display_tree_grouped($subscription_categories['subscription_categories_id'], $spacing+1, $exclude, $subscription_category_tree_array);
		}
	
		$subscription_query=tep_db_query("select subscription_name,subscription_id from " . TABLE_SUBSCRIPTIONS_DESCRIPTION  . " order by subscription_name");
		while ($subscription=tep_db_fetch_array($subscription_query))
		{
			$subscription_category_tree_array[] = array('id' => $subscription['subscription_id'], 'text' =>$subscription['subscription_name'],'type'=>'E','spacing'=>$spacing);
		}
		return $subscription_category_tree_array;
	}
	// subscription tree
	function tep_get_subscription_array_single($first=''){
		global $languages_id;
		$subscription_array=array();
		//echo "SELECT su.subscription_id,sud.subscription_name from " . TABLE_SUBSCRIPTIONS . " su, " . TABLE_SUBSCRIPTIONS_DESCRIPTION . " sud where su.subscription_id=sud.subscription_id and language_id='" . $languages_id . "' " . SUBSCRIPTION_CONDITION_STATUS . " order by sud.subscription_name";
		$subscription_query=tep_db_query("SELECT su.subscription_id,sud.subscription_name from " . TABLE_SUBSCRIPTIONS . " su, " . TABLE_SUBSCRIPTIONS_DESCRIPTION . " sud where su.subscription_id=sud.subscription_id and language_id='" . $languages_id . "' " . SUBSCRIPTION_CONDITION_STATUS . " order by sud.subscription_name");
		
		while($subscription_result=tep_db_fetch_array($subscription_query)){
			$subscription_array[]=array('id'=>$subscription_result['subscription_id'],'text'=>$subscription_result['subscription_name']);
			
		}
		//print_r ($subscription_array);
		return $subscription_array;
	} 
?>
