<?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();
   
   if (isset($HTTP_GET_VARS['service_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('service_action', 'secPath', 'service_id','qty');
    } else {
      $goto = basename($PHP_SELF);
      if ($HTTP_GET_VARS['service_action'] == 'buy_cart') {
         $parameters = array('service_action', 'service_id');
	  } else {
	  	 $parameters = array('service_action');
	  }
    }
  }

  switch ($HTTP_GET_VARS['service_action']) {
	case 'buy_service' : 
		$service_id=(int)$services->service_id;
		if ($service_id){
			$others["start_date"]=(($services->start_date)?$services->start_date:'0000-00-00');
			$others["end_date"]=(($services->end_date)?$services->end_date:'0000-00-00');
			$others["start_time"]=(($services->start_time)?$services->start_time:'00:00:00');
			$others["end_time"]=(($services->end_time)?$services->end_time:'00:00:00');
			$others["resource_id"]=(int)$services->resource_id;
			$others["resource_costs"]=((strpos($services->resource_costs,".")>0)?$services->resource_costs:(int)$services->resource_costs);
			$res=array();  					
  			$service_id_string = tep_get_service_uprid($service_id,$services->attributes);
  			if(tep_db_input($others["start_date"])) $start_date_string=strtotime(tep_db_input($others["start_date"]));
			if(tep_db_input($others["end_date"])) $end_date_string=strtotime(tep_db_input($others["end_date"]));
			if(tep_db_input($others["start_time"])) $start_time_string=strtotime(tep_db_input($others["start_time"]));
			if(tep_db_input($others["end_time"])) $end_time_string=strtotime(tep_db_input($others["end_time"]));
			$time_string=$start_date_string+$end_date_string+$start_time_string+$end_time_string;
			$service_id_string.="-".$time_string;
			if($others["resource_id"]) $service_id_string.='-'.$others["resource_id"];
			//$quantity=$services->qty+$cart->get_quantity($service_id_string);
			$quantity=$services->qty;
			$cart->add_cart_service($service_id,$quantity,$services->attributes,true,$others);
			reset($services);
			//==============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) //in cart items or already purchased
					tep_redirect(tep_href_link($goto, tep_get_all_get_params($parameters)));
			}
			else
			{
				if($check_res==2 || $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;
  }
// calculate services_category path
  if (isset($HTTP_GET_VARS['secPath'])) {
    $secPath =(int)$HTTP_GET_VARS['secPath'];
  } elseif (isset($HTTP_GET_VARS['service_id'])) {
    $secPath = (int)tep_get_services_path($HTTP_GET_VARS['service_id']);
  } else if (isset($HTTP_POST_VARS['post_action']) && $HTTP_POST_VARS['post_action']=="change_cat"){
  	$secPath= (int)tep_find_services_category_path($HTTP_POST_VARS['category_id']);
  } else {
    $secPath = '';
  }

  if (tep_not_null($secPath)) {
    $secPath_array = tep_parse_services_category_path($secPath);
    $secPath = implode('_', $secPath_array);
    $current_services_category_id = (int)$secPath_array[(sizeof($secPath_array)-1)];
  } else {
    $current_services_category_id = 0;
  }

  if (isset($secPath_array)) {
    for ($i=0, $n=sizeof($secPath_array); $i<$n; $i++) {
      $services_categories_query = tep_db_query("select services_categories_name from " . TABLE_SERVICES_CATEGORIES_DESCRIPTION . " where services_categories_id = '" . (int)$secPath_array[$i] . "' and language_id = '" . (int)$languages_id . "'");
      if (tep_db_num_rows($services_categories_query) > 0) {
        $services_categories = tep_db_fetch_array($services_categories_query);
        $breadcrumb->add($services_categories['services_categories_name'], tep_href_link(FILENAME_DEFAULT, 'secPath=' . implode('_', array_slice($secPath_array, 0, ($i+1)))));
      } else {
        break;
      }
    }
  } 

// add the services name to the breadcrumb trail
  if (isset($HTTP_GET_VARS['service_id'])) {
    $service_query = tep_db_query("select service_name from " . TABLE_SERVICES_DESCRIPTION . " where service_id = '" . (int)$HTTP_GET_VARS['service_id'] . "' and language_id ='" . (int)$languages_id . "'" );
    if (tep_db_num_rows($service_query)) {
      $service = tep_db_fetch_array($service_query);
      $breadcrumb->add($service['service_name'], tep_href_link(FILENAME_SERVICE_INFO, 'secPath=' . $secPath . '&service_id=' . (int)$HTTP_GET_VARS['service_id']));
    }
  }
   
 
  //calculate service array
 if (isset($HTTP_GET_VARS['secPath'])) {
    $secPath = tep_db_prepare_input($HTTP_GET_VARS['secPath']);
	tep_set_session_value('prev_category_viewed','secPath='.  $secPath); 
  } elseif (isset($HTTP_GET_VARS['service_id'])) {
    $secPath = (int) tep_get_services_path($HTTP_GET_VARS['service_id']);
  } else {
    $secPath = '';
  }

if (tep_not_null($secPath)) {
    $secPath_array = tep_parse_services_category_path($secPath);
    $secPath = implode('_', $secPath_array);
    $current_services_category_id = $secPath_array[(sizeof($secPath_array)-1)];
  } else {
    $current_services_category_id = 0;
  }
 
 
  if (!tep_session_is_registered("services")){
 	tep_session_register('services');
	$services=new services();
	
 }else{
	$services=$_SESSION['services'];
}
 
?>
