<?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(); 

	// utility function to create interval period of dates between recursive sessions
	function get_gap_dates_array($recursive_ids){
		$result_array=array();
		$ids_string=implode(",",$recursive_ids);
		//select all the recursive sessions 
		$gap_dates_query=tep_db_query("SELECT s.recursive_sessions_id,max(s.start_date) as sessions_last_date,min(s.start_date) as sessions_first_date from " . TABLE_EVENTS_SESSIONS . " s where s.recursive_sessions_id in (" . $ids_string . ") and (s.sessions_type!='P' || s.sessions_type!='') group by recursive_sessions_id order by sessions_first_date");
		 
		if (tep_db_num_rows($gap_dates_query)<=1) return $result_array;
		$gap_dates=tep_db_fetch_array($gap_dates_query);
		$end_date=$gap_dates["sessions_last_date"];
		$recursive_id=(int)$gap_dates["recursive_sessions_id"];

		// iterate between each recursive session to determine the gap
		while($gap_dates=tep_db_fetch_array($gap_dates_query))
		{
			$recursive_query=tep_db_query("SELECT * from " . TABLE_RECURSIVE_SESSIONS . " where recursive_sessions_id='" . $recursive_id . "'");
			$recursive_result=tep_db_fetch_array($recursive_query);
			$findInfo=new ObjectInfo($recursive_result);
			// determine start and end date 
			$findInfo->sessions_start_date=date("Y-m-d",date_operation(strtotime($end_date),"day",1,"add"));
			$findInfo->sessions_end_date=date("Y-m-d",date_operation(strtotime($gap_dates['sessions_first_date']),"day",1,"sub"));
			if ($findInfo->sessions_start_date<=$findInfo->sessions_end_date){
				// get the intermediate dates
				$result=get_dates_array($findInfo,$findInfo->sessions_type_option,false);
				if (sizeof($result)>0) $result_array=array_merge($result_array,$result);
			}
			$end_date=$gap_dates["sessions_last_date"];
			$recursive_id=(int)$gap_dates["recursive_sessions_id"];
		}
		return $result_array;
	}
	// get date sessions between two recursive dates
	function get_dates_array($sInfo,$type,$include_first=true)
	{
		$result_dates=array();
		$caldate=$sInfo->sessions_start_date;
		$edate=$sInfo->sessions_end_date;
		$sdate=$sInfo->sessions_start_date;
		if ($sdate=='' || $edate=='') return $result_dates;
		//daily type
		if ($type=='DD')
		{
			if ((int)$sInfo->sessions_type_daily_days<=0) return $result_dates;
			while ($caldate<=$edate)
			{
				//if ($include_first || ($sdate!=$caldate && $edate!=caldate))
					$result_dates[]=$caldate;
				//$query = tep_db_query("select date_add('" . $caldate . "',interval '" . $sInfo->sessions_type_daily_days . "' day) as datenew");
				//$result=tep_db_fetch_array($query);
				//$caldate=$result['datenew'];
				$caldate=date("Y-m-d",date_operation(strtotime($caldate),"day",$sInfo->sessions_type_daily_days,"add"));
				if ($caldate=='') break;
			}
		} else if ($type=='WW') { // weekly type
			$weekno=$sInfo->sessions_type_weekly_weeks;
			$weekday=$sInfo->sessions_type_weekly_days;
			if ((int)$sInfo->sessions_type_weekly_weeks<=0) return $result_dates;
			if ($sInfo->sessions_type_weekly_days=='') return $result_dates;
			$query = tep_db_query("select date_format('" . $caldate . "', '%w') as weekdate");
			$result=tep_db_fetch_array($query);
			$query = tep_db_query("select date_sub('" . $caldate . "',interval '" . ($result['weekdate']-1) . "' day) as caldate");

			$result=tep_db_fetch_array($query);
			$caldate=$result['caldate'];
			if ($caldate=='') return $result_dates;
	     while ($caldate<=$edate)
		  {
		   $query = tep_db_query("select date_format('" . $caldate . "', '%d') as day,date_format('" . $caldate .  "', '%m') as month,date_format('" . $caldate . "', '%Y') as year");
		   $result=tep_db_fetch_array($query);

		   $startday=$result['day'];  
		   $month=$result['month'];
		   $year=$result['year'];
		   
		   $caldate=date('Y-m-d',mktime(0,0,0,$month,$startday,$year));
		   if ($caldate>$edate) break;
		   for ($icnt=0;$icnt<7;$icnt++){
		         $caldate=date('Y-m-d',mktime(0,0,0,$month,$startday,$year));
		         if ($caldate>$edate) break;
			 if ($caldate>$sdate && substr($weekday,$icnt,1)=='1'){ 
				 $result_dates[]=$caldate;}
			$startday=$startday+1;
   		    }
		        if ($caldate>$edate) break;
			$query = tep_db_query("select date_add('" . $caldate . "',interval '" . ((($sInfo->sessions_type_weekly_weeks-1) * 7)+1) . "' day) as datenew");
			$result=tep_db_fetch_array($query);
			$caldate=$result['datenew'];
		        if ($caldate=='') break;
		  }			 
	  } else if ($type=='MD') {	//monthly day
		if ((int)$sInfo->sessions_type_monthly_days<=0) return $result_dates;
		if ((int)$sInfo->sessions_type_monthly_months<=0) return $result_dates;

		$query = tep_db_query("select date_format('" . $caldate . "', '%d') as day,date_format('" . $caldate .  "', '%m') as month,date_format('" . $caldate . "', '%Y') as year");
		$result=tep_db_fetch_array($query);
		$day=$sInfo->sessions_type_monthly_days;
		$month=$result['month'];
		$year=$result['year'];
		if ($day<$result['day']) $month=$month+$sInfo->sessions_type_monthly_months;
		$caldate=date('Y-m-d',mktime(0,0,0,$month,$day,$year));
		while ($caldate<=$edate)
		{
			$result_dates[]=$caldate;
			$month=$month+$sInfo->sessions_type_monthly_months;
			$caldate=date('Y-m-d',mktime(0,0,0,$month,$day,$year));
			if ($caldate=='') break;
		}			 
	} else if ($type=='MM') {	//Monthly month
		if ((int)$sInfo->sessions_type_monthly_weekmonths<=0) return $result_dates;
		$query = tep_db_query("select date_format('" . $caldate . "', '%d') as day,date_format('" . $caldate .  "', '%m') as month,date_format('" . $caldate . "', '%Y') as year");
		$result=tep_db_fetch_array($query);
		$weekno=$sInfo->sessions_type_monthly_weeks;
		$weekday=$sInfo->sessions_type_monthly_weekdays;
		$day=1;
		$month=$result['month'];
		$year=$result['year'];
		 //if ($day<$result['day']) $month=$month+$sInfo->sessions_type_monthly_months;
		$caldate=date('Y-m-d',mktime(0,0,0,$month,$day,$year));
		if ($caldate=='') return $result_dates;
		while ($caldate<=$edate)
		{
			$day='01';
			$query = tep_db_query("select date_format('" . $caldate . "', '%w') as weekdate");
			$result=tep_db_fetch_array($query);			  
			$weekdate=$result['weekdate'];
			if ($weekdate>$weekday)
				$day=$day+(7-$weekdate)+$weekday;
			else
				$day=$day+($weekday-$weekdate);
			if ($weekno>1) $day=$day+($weekno-1)*7; 
				$caldate=date('Y-m-d',mktime(0,0,0,$month,$day,$year));
			if ($caldate>$edate) break;
			if ($caldate>$sdate){
				$result_dates[]=$caldate;
			}
			$month=$month+$sInfo->sessions_type_monthly_weekmonths;
			$caldate=date('Y-m-d',mktime(0,0,0,$month,'01',$year));
			if ($caldate=='') break;
		}			 
	} else if ($type=='YM') { // yearly month
		if ((int)$sInfo->sessions_type_yearly_monthdays<=0) return $result_dates;
		$sql = "select date_format('" . $caldate . "', '%d') as day,date_format('" . $caldate .  "', '%m') as month,date_format('" . $caldate . "', '%Y') as year";
		$query = tep_db_query($sql);
		$result=tep_db_fetch_array($query);
		$day=$sInfo->sessions_type_yearly_monthdays;
		$month=$sInfo->sessions_type_yearly_months;
		$year=$result['year'];
		if ($month<$result['month']){
			 $year=$year+1;
		} else {
			if ($day<$result['day']) $year=$year+1;
		}
		$caldate=date('Y-m-d',mktime(0,0,0,$month,$day,$year));
		if ($caldate=='') return $result_dates;
		while ($caldate<=$edate)
		{
			$result_dates[]=$caldate;
			$year=$year+1;
			$caldate=date('Y-m-d',mktime(0,0,0,$month,$day,$year));
			if ($caldate=='') break;
		}			 
	} else if ($type=='YY') {
		$query = tep_db_query("select date_format('" . $caldate . "', '%d') as day,date_format('" . $caldate .  "', '%m') as month,date_format('" . $caldate . "', '%Y') as year");
		$result=tep_db_fetch_array($query);
		$weekno=$sInfo->sessions_type_yearly_weeks;
		$weekday=$sInfo->sessions_type_yearly_weekdays;
		$day=1;
		$month=$sInfo->sessions_type_yearly_weekmonths;
		$year=$result['year'];
		$caldate=date('Y-m-d',mktime(0,0,0,$month,$day,$year));
		if ($caldate=='') return $result_dates;
		while ($caldate<=$edate)
		{
			$day='1';
			$query = tep_db_query("select date_format('" . $caldate . "', '%w') as weekdate");
			$result=tep_db_fetch_array($query);			  
			$weekdate=$result['weekdate'];
			if ($weekdate>$weekday)
				$day=$day+(7-$weekdate)+$weekday;
			else
				$day=$day+($weekday-$weekdate);
			if ($weekno>1) $day=$day+($weekno-1)*7; 
		
			$caldate=date('Y-m-d',mktime(0,0,0,$month,$day,$year));
			if ($caldate>$edate) break;
			if ($caldate>$sdate){
				$result_dates[]=$caldate;		      
			}
			$year=$year+1;
			$caldate=date('Y-m-d',mktime(0,0,0,$month,'01',$year));
			if ($caldate=='') break;
		}
	}
   return $result_dates;
 }
 
 // delete all the sessions of recursive session
 function tep_delete_multiple_sessions($session_id)
 {
   $session_query=tep_db_query("select sessions_id from " . TABLE_EVENTS_SESSIONS . " where recursive_sessions_id='" . $session_id . "' and sessions_type='R'");

   while($result=tep_db_fetch_array($session_query))
   {
      if (!tep_check_linked_reservation($result['sessions_id']))
         tep_db_query("delete from " . TABLE_EVENTS_SESSIONS . " where sessions_id='" . $result['sessions_id'] . "'");
      else
         tep_db_query("update " . TABLE_EVENTS_SESSIONS . " set sessions_type='' where sessions_id='" . $result['sessions_id'] . "'");
   }
 }
 // copy the sessions one event to another event
 function tep_add_multiple_session($sql_data_array)
 {
   $session_query=tep_db_query("select sessions_id from " . TABLE_EVENTS_SESSIONS . " where events_id='" . $sql_data_array['events_id'] . "' and start_date='". $sql_data_array['start_date'] . "'");
   $cnt=tep_db_num_rows($session_query);
   if ($cnt>0){
     $result=tep_db_fetch_array($session_query);
     tep_db_query("update " . TABLE_EVENTS_SESSIONS . " set sessions_type='R', recursive_sessions_id='" . $sql_data_array['recursive_sessions_id'] . "' where sessions_id='" . $result['sessions_id'] . "'");
     return "update";
   }
   tep_db_perform(TABLE_EVENTS_SESSIONS, $sql_data_array);
   return "insert";
 }
 // delete the single session
 function tep_delete_single_session($session_id)
 {
 	// Donot use Update session type if delete option on Single session Delete IT
     tep_db_query("delete from " . TABLE_EVENTS_SESSIONS . " where sessions_id='" . $session_id . "'");
    //if (!tep_check_linked_reservation($session_id))
    //else
     //tep_db_query("update " . TABLE_EVENTS_SESSIONS . " set sessions_type='' where sessions_id='" . $session_id . "'");
 }
 // delete recursive session
 function tep_delete_recursive_session($session_id)
 {
    tep_db_query("delete from " . TABLE_RECURSIVE_SESSIONS . " where recursive_sessions_id='" . $session_id . "'");
 }
 
 // check for mismatch of sessions when changing from one event to another event
 function tep_change_event_valid($event_id,$old_id)
 {
   if ((int)$old_id=='') $old_id=0;
   if ($event_id==$old_id)
      return '';
      
   $session_type_new=tep_get_session_type($event_id);
   $session_type_old=tep_get_session_type($old_id);

   if ($session_type_new=='' && $session_type_old=='')
      return '';

   if ($session_type_new=='' && $session_type_old!='')
      return '';

   if ($session_type_new=='S' && $session_type_old=='S')
      return '';

   if ($session_type_new=='R' && $session_type_old=='R')
      return '';
   
   if ($session_type_new=='S' && $session_type_old!='S')
      return sprintf(TEXT_ERR_RECUR_SINGLE,tep_get_events_name($event_id));
   
   if ($session_type_new=='R' && $session_type_old!='R')
      return sprintf(TEXT_ERR_SINGLE_RECUR,tep_get_events_name($event_id));
   else
      return sprintf(TEXT_ERR_RECUR_SINGLE,tep_get_events_name($event_id));
 }
 // copy single session from one event to another event
 function tep_add_single_session($sql_data_array)
 {
   global $session_id;
   $session_query=tep_db_query("select sessions_id from " . TABLE_EVENTS_SESSIONS . " where events_id='" . $sql_data_array['events_id'] . "' and start_date='". $sql_data_array['start_date'] . "'");
   $cnt=tep_db_num_rows($session_query);
   if ($cnt>0){
     $result=tep_db_fetch_array($session_query);
     tep_db_query("update " . TABLE_EVENTS_SESSIONS . " set sessions_type='S' where sessions_id='" . $result['sessions_id'] . "'");
     return;
   }
   tep_db_perform(TABLE_EVENTS_SESSIONS, $sql_data_array);
   $session_id=tep_db_insert_id();
 }
 // check for session start and end dates mismatch
 function tep_check_dates_collide($start_date,$end_date,$session_id,$event_id){
 
    $date_query=tep_db_query("select sessions_start_date,sessions_end_date from " . TABLE_RECURSIVE_SESSIONS . " where ('" . $start_date . "' between sessions_start_date and sessions_end_date or '" . $end_date . "' between sessions_start_date and sessions_end_date or ('" . $start_date . "'< sessions_start_date and '" . $end_date . "'>sessions_end_date)) and recursive_sessions_id!='" . $session_id . "' and events_id='" . $event_id . "'");
	
    $cnt=tep_db_num_rows($date_query);
    if ($cnt>0) 
      return true;
    else
      return false;
 }
function date_operation($timestamp, $unit, $amount,$mode) {
// Possible $units are: "hr", "min", "sec",
//          "mon", "day", or "yr"
// $amount should be an integer
   $delta_vars = array("hr"=>0, "min"=>0,
   "sec"=>0, "mon"=>1,
   "day"=>1,"yr"=>1970);
   $delta_vars[$unit] += $amount;
   $delta = gmmktime($delta_vars["hr"],
   $delta_vars["min"],
   $delta_vars["sec"],
   $delta_vars["mon"],
   $delta_vars["day"],
   $delta_vars["yr"]);
   if ($mode=="add")
      return $timestamp + $delta;
   else
   	  return $timestamp - $delta;
}
?>
