<?php
/*
  osCommRes, Services Online 
http://www.oscommres.com 
Copyright (c) 2005 osCommRes 

Freeway eCommerce 
http://www.zac-ware.com/freeway 
Copyright (c) 2007 ZacWare 

Released under the GNU General Public License 
*/

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

  require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_EVENTS_MESSAGES_MAIL);
  //require(DIR_WS_CLASSES . 'sms.php');
  if(tep_session_is_registered('customer_id')){
	/*$is_blocked_sql = "select customers_id from " . TABLE_CUSTOMERS . " c where customers_id=" . (int)$customer_id . " and c.is_blocked='Y'";
	$is_blocked_query = tep_db_query($is_blocked_sql);
	$is_data = tep_db_fetch_array($group_query);
	if($is_data['customers_id'] != '') return true;
	//if (tep_db_num_rows($group_query)>0) return true;
	else return false;*/
  }
$menu_query=tep_db_query("select configuration_value from ".TABLE_CONFIGURATION ." where configuration_key='SHOP_SELL_ITEMS'")	 ;
$menu_result=tep_db_fetch_array($menu_query);
$prd=true;
for($i=0;$i<strlen($menu_result['configuration_value']);$i++){
	$menu_value=substr($menu_result['configuration_value'],$i,1);
	switch ($menu_value){
		case 'P':
			define('HIDE_FROM_BACKEND_MENU_PRODUCTS','false');
			break;
		case 'E':
			define('HIDE_FROM_BACKEND_MENU_EVENTS','false');
			break;
		case 'S':
			define('HIDE_FROM_BACKEND_MENU_SUBSCRIPTIONS','false');
			break;
		case 'V':
			define('HIDE_FROM_BACKEND_MENU_SERVICES','false');
			break;		
	}
}
  
  if (isset($HTTP_GET_VARS['event_action'])) {
    if ($session_started == false) {
      tep_redirect(tep_href_link(FILENAME_COOKIE_USAGE));
    }
	$element_type=isset($HTTP_GET_VARS['element_type'])?tep_db_prepare_input($HTTP_GET_VARS['element_type']):'P';  
    if (DISPLAY_CART == 'true') {	
	   $goto =  FILENAME_SHOPPING_CART;
       $parameters = array('event_action', 'ecPath', 'products_id','products_qty');
    } else {
      $goto = basename($PHP_SELF);
      if ($HTTP_GET_VARS['event_action'] == 'buy_cart') {
         $parameters = array('event_action', 'products_id');
	  } else {
	  	 $parameters = array('event_action');
	  }
    }
    if(defined('DISPLAY_AFTER_CART') && DISPLAY_AFTER_CART=='Proceed to checkout') 
  	 	$goto =  FILENAME_CHECKOUT_SHIPPING;
  }

  switch ($HTTP_GET_VARS['event_action']) {
      // customer wants to update the product quantity in their shopping cart
      case 'update_cart' : 	
	  						for ($i=0, $n=sizeof($HTTP_POST_VARS['products_id']); $i<$n; $i++) {
                                if (in_array($HTTP_POST_VARS['products_id'][$i], (is_array($HTTP_POST_VARS['cart_delete']) ? tep_db_prepare_input($HTTP_POST_VARS['cart_delete']) : array()))) {
								  $products_id=tep_db_prepare_input($HTTP_POST_VARS['products_id'][$i]);
								  //if ($HTTP_POST_VARS['products_type'][$i]=="E") $products_id='E' . $products_id;
								  //if ($HTTP_POST_VARS['products_type'][$i]=="S") $products_id='S' . $products_id;
                                  $cart->remove($products_id);
                                } else {
									$products_id=tep_db_prepare_input($HTTP_POST_VARS['products_id'][$i]);
									//if ($HTTP_POST_VARS['products_type'][$i]=="P" || $HTTP_POST_VARS['products_type'][$i]=="V")
									//$products_id=tep_get_prid($HTTP_POST_VARS['products_id'][$i]);
										  if (PHP_VERSION < 4) {
											// if PHP3, make correction for lack of multidimensional array.
											reset($HTTP_POST_VARS);
											while (list($key, $value) = each($HTTP_POST_VARS)) {
											  if (is_array($value)) {
												while (list($key2, $value2) = each($value)) {
												  if (ereg ("(.*)\]\[(.*)", $key2, $var)) {
													$id2[$var[1]][$var[2]] = $value2;
												  }
												}
											  }
											}
											$attributes = ($id2[$products_id]) ? $id2[$products_id] : '';
										  } else {
										    $attributes = ($HTTP_POST_VARS['id'][$products_id]) ? tep_db_prepare_input($HTTP_POST_VARS['id'][$products_id]) : '';
										  
										  } 
										$cart->add_cart($products_id, tep_db_prepare_input($HTTP_POST_VARS['cart_quantity'][$i]), $attributes, false,array(),0,tep_db_prepare_input($HTTP_POST_VARS['discount_ids'][$i]));
								  }
                              }
							 tep_redirect(tep_href_link($goto, tep_get_all_get_params($parameters)));
                         //     break;
      // performed by the 'buy now' button in product listings and review page
      case 'buy_product' : 
	  		if (!isset($HTTP_POST_VARS['session_id']))
				tep_redirect(tep_href_link(FILENAME_DEFAULT));
			
			// get products added by checking products_id[];
			$old_orders_id=(isset($HTTP_POST_VARS['old_orders_id'])?(int)$HTTP_POST_VARS['old_orders_id']:0);

			$question_type=(isset($HTTP_POST_VARS['event_question_type'])?tep_db_prepare_input($HTTP_POST_VARS['event_question_type']):'');
			$question_value=(isset($HTTP_POST_VARS['event_question_value_data'])?tep_db_prepare_input($HTTP_POST_VARS['event_question_value_data']):'');
			//group booking
			$no_attendees=(isset($HTTP_POST_VARS['no_attendees'])?(int)$HTTP_POST_VARS['no_attendees']:1);
			//group booking
			$products=tep_db_prepare_input($HTTP_POST_VARS['session_id']);
			$sessions=array();
			for ($icnt=0;$icnt<sizeof($products);$icnt++){
				if (isset($HTTP_POST_VARS["session_select_" . $HTTP_POST_VARS['session_id'][$icnt]])){
					$sessions[$HTTP_POST_VARS['session_id'][$icnt]]
										=array(	"type"=>tep_db_prepare_input($HTTP_POST_VARS["session_type"][$icnt]),
												"date"=>tep_db_prepare_input($HTTP_POST_VARS["session_date"][$icnt]),
												"fees"=>0,
												"group"=>tep_db_prepare_input($HTTP_POST_VARS["session_group"][$icnt]));
				}
			}
			$cart->add_cart_event($HTTP_POST_VARS['events_id'],$sessions,$old_orders_id,$question_type,$question_value,$no_attendees);
				//==============For stored subscription=============== //Added line 124- 140 by sobi
				
				$check_res=$cart->check_stored_subscription_purchased();
				if(tep_session_is_registered('customer_id'))
				{
					if($check_res==1 || $check_res==2 || $check_res==3) //in cart items or already purchased
						tep_redirect(tep_href_link($goto, tep_get_all_get_params($parameters)));
				}
				else
				{
					if($check_res==2  || $check_res==3 || $subscribed_already)
						tep_redirect(tep_href_link($goto, tep_get_all_get_params($parameters)));
				}
			    $subs_query=tep_db_query("select sc.subscription_categories_id as cat_id,s.subscription_id as id from " .TABLE_SUBSCRIPTIONS ." s," . TABLE_SUBSCRIPTION_TO_SUBSCRIPTION_CATEGORIES . " sc where s.subscription_id=sc.subscription_id and  s.subscription_type='M' and s.subscription_status='1' ");
				if(tep_db_num_rows($subs_query)>0)
				{
					$row=tep_db_fetch_array($subs_query);
					tep_redirect(tep_href_link(FILENAME_SUBSCRIPTION_INFO, 'sucPath=' . $row['cat_id']. '&stored_flag=y&subscription_id=' .$row['id']));
				}
			
            tep_redirect(tep_href_link($goto, tep_get_all_get_params($parameters)));
            break;
  }

  // check for subscription action
  if (isset($HTTP_GET_VARS['subs_action'])) {
    if ($session_started == false) {
      tep_redirect(tep_href_link(FILENAME_COOKIE_USAGE));
    }
    if (DISPLAY_CART == 'true') {	
	   $goto =  FILENAME_SHOPPING_CART;
       $parameters = array('subs_action', 'sucPath', 'subscription_id');
    } else {
      $goto = basename($PHP_SELF);
      if ($HTTP_GET_VARS['subs_action'] == 'buy_cart') {
         $parameters = array('subs_action', 'subscription_id');
	  } else {
	  	 $parameters = array('subs_action');
	  }
    }
  }

  switch ($HTTP_GET_VARS['subs_action']) {
      case 'add_subscription' : 
	  
	  $products_id=(int)$HTTP_POST_VARS['products_id'];
	  // product with linked subscription query
		$sub_query = tep_db_query("select * from " . TABLE_PRODUCTS_TO_SUBSCRIPTION . " where products_id = '" . tep_db_input($products_id) . "' ");
		if(tep_db_num_rows($sub_query) > 0){
        $check_sub = tep_db_fetch_array($sub_query);
	  				if($HTTP_POST_VARS['qty']==0) $HTTP_POST_VARS['qty']=1;
					$attributes="";
					if (isset($HTTP_POST_VARS['attrValues']) && isset($HTTP_POST_VARS['attrIds'])){
						for ($icnt=0,$n=count($HTTP_POST_VARS['attrValues']);$icnt<$n;$icnt++){
							$attributes[$HTTP_POST_VARS['attrIds'][$icnt]]=tep_db_prepare_input($HTTP_POST_VARS['attrValues'][$icnt]);
						}
					}
					$cart->add_cart($products_id, $cart->get_quantity(tep_get_uprid($products_id, tep_db_prepare_input($HTTP_POST_VARS['id'])))+tep_db_prepare_input($HTTP_POST_VARS['qty']), $attributes,true,array(),0);
		}	
	  
	 // echo $HTTP_GET_VARS['products_id'];
		 if (isset($HTTP_POST_VARS['subscription_id']) && is_numeric($HTTP_POST_VARS['subscription_id'])) {
			$products_id=(int)$HTTP_POST_VARS['subscription_id'];
	  		$subscription_start_date = isset($HTTP_POST_VARS['your_start_date'])?tep_convert_date_raw($HTTP_POST_VARS['your_start_date']):'0000-00-00';
			$cart->add_cart_subscription($products_id, 1, '','','','',$subscription_start_date);
			// check for forced products;
			$xsell_ids=(isset($HTTP_POST_VARS["xsell_forced_id"])?tep_db_prepare_input($HTTP_POST_VARS["xsell_forced_id"]):array());
			if (count($xsell_ids)>0){
				for ($lcnt=0;$lcnt<count($xsell_ids);$lcnt++){
					$cart->add_cart_subscription($xsell_ids[$lcnt], 1,'', true,array(),0,$subscription_start_date);
				}
			}
		 }
		  tep_redirect(tep_href_link($goto, tep_get_all_get_params($parameters)));
		  break;
   }
   
// calculate events_category path
  if (isset($HTTP_GET_VARS['ecPath'])) {
    $ecPath = tep_db_prepare_input($HTTP_GET_VARS['ecPath']);
	tep_set_session_value('prev_category_viewed','ecPath='.  $ecPath); 
  } elseif (isset($HTTP_GET_VARS['events_id'])) {
    $ecPath = tep_get_events_path(tep_db_prepare_input($HTTP_GET_VARS['events_id']));
  } else if (isset($HTTP_POST_VARS['post_action']) && $HTTP_POST_VARS['post_action']=="change_cat"){
  	$ecPath=tep_find_events_category_path(tep_db_prepare_input($HTTP_POST_VARS['category_id']));
  } else {
    $ecPath = '';
  }

  if (tep_not_null($ecPath)) {
    $ecPath_array = tep_parse_events_category_path($ecPath);
    $ecPath = implode('_', $ecPath_array);
    $current_events_category_id = $ecPath_array[(sizeof($ecPath_array)-1)];
  } else {
    $current_events_category_id = 0;
  }

  
  if (isset($ecPath_array)) {
    for ($i=0, $n=sizeof($ecPath_array); $i<$n; $i++) {
      $events_categories_query = tep_db_query("select events_categories_name from " . TABLE_EVENTS_CATEGORIES_DESCRIPTION . " where events_categories_id = '" . (int)$ecPath_array[$i] . "' and language_id = '" . (int)$languages_id . "'");
      if (tep_db_num_rows($events_categories_query) > 0) {
        $events_categories = tep_db_fetch_array($events_categories_query);
        $breadcrumb->add($events_categories['events_categories_name'], tep_href_link(FILENAME_DEFAULT, 'ecPath=' . implode('_', array_slice($ecPath_array, 0, ($i+1)))));
      } else {
        break;
      }
    }
  } 

// add the events name to the breadcrumb trail
  if (isset($HTTP_GET_VARS['events_id'])) {
    $event_query = tep_db_query("select events_name from " . TABLE_EVENTS_DESCRIPTION . " where events_id = '" . (int)$HTTP_GET_VARS['events_id'] . "' and language_id ='" . (int)$languages_id . "'" );
    if (tep_db_num_rows($event_query)) {
      $event = tep_db_fetch_array($event_query);
      $breadcrumb->add($event['events_name'], tep_href_link(FILENAME_EVENT_INFO, 'ecPath=' . $ecPath . '&events_id=' . tep_db_prepare_input($HTTP_GET_VARS['events_id'])));
    }
  }

 //calculate subscription array
 if (isset($HTTP_GET_VARS['sucPath'])) {
    $sucPath = tep_db_prepare_input($HTTP_GET_VARS['sucPath']);
	tep_set_session_value('prev_category_viewed','sucPath='.  $sucPath); 
  } elseif (isset($HTTP_GET_VARS['subscription_id'])) {
    $sucPath = (int)tep_get_subscription_path($HTTP_GET_VARS['subscription_id']);
  } else {
    $sucPath = '';
  }
  
 
  if (isset($HTTP_GET_VARS['stcPath'])) {
  
    $stcPath = (int)$HTTP_GET_VARS['stcPath'];
  } elseif (isset($HTTP_GET_VARS['page_id'])) {
    $stcPath = (int) tep_get_static_path($HTTP_GET_VARS['page_id']);
  } else {
    $stcPath = '';
  }
  
  if (tep_not_null($stcPath)) {
    $stcPath_array = tep_parse_static_category_path($stcPath);
    $stcPath = implode('_', $stcPath_array);
    $current_static_category_id = $stcPath_array[(sizeof($stcPath_array)-1)];
  } else {
    $current_static_category_id = 0;
  }
  
  
if (tep_not_null($sucPath)) {
    $sucPath_array = tep_parse_subscription_category_path($sucPath);
    $sucPath = implode('_', $sucPath_array);
    $current_subscription_category_id = $sucPath_array[(sizeof($sucPath_array)-1)];
  } else {
    $current_subscription_category_id = 0;
  }
  
  if (tep_not_null($ecPath) && $sucPath!=0){
 tep_set_session_value('prev_category_viewed',$sucPath); 
  }
  
if (isset($sucPath_array)) {
    for ($i=0, $n=sizeof($sucPath_array); $i<$n; $i++) {
      $subscription_categories_query = tep_db_query("select subscription_categories_name from " . TABLE_SUBSCRIPTION_CATEGORIES_DESCRIPTION . " where subscription_categories_id = '" . (int)$sucPath_array[$i] . "' and language_id = '" . (int)$languages_id . "'");
	  if (tep_db_num_rows($subscription_categories_query) > 0) {
        $subscription_categories = tep_db_fetch_array($subscription_categories_query);
        $breadcrumb->add($subscription_categories['subscription_categories_name'], tep_href_link(FILENAME_SUBSCRIPTIONS_INFO, 'sucPath=' . implode('_', array_slice($sucPath_array, 0, ($i+1)))));
	  } else {
        break;
      }
    }
  } 
  
    // add the events name to the breadcrumb trail
  if (isset($HTTP_GET_VARS['subscription_id'])) {
    $subscription_query = tep_db_query("select subscription_name from " . TABLE_SUBSCRIPTIONS_DESCRIPTION . " where subscription_id = '" . (int)$HTTP_GET_VARS['subscription_id'] . "' and language_id ='" . (int)$languages_id . "'" );
    if (tep_db_num_rows($subscription_query)) {
      $subscription = tep_db_fetch_array($subscription_query);
      $breadcrumb->add($subscription['subscription_name'], tep_href_link(FILENAME_SUBSCRIPTIONS_INFO, 'sucPath=' . $sucPath . '&subscription_id=' . tep_db_prepare_input($HTTP_GET_VARS['subscription_id'])));
    }
  }
  $CELL_PADDING_SUB=CELL_PADDING_SUB;
  $CONTENT_TOP_HEIGHT=10;
  require(DIR_WS_CLASSES . 'sms.php');
	$sms=new sms(SMS_GATEWAY);
?>