<?php
/*

	osCommRes, Services Oline 
	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();


  function get_dates_array($sInfo,$type)
  {
    $result_dates=array();
	  $caldate=$sInfo->sessions_start_date;
	  $edate=$sInfo->sessions_end_date;
	  $sdate=$sInfo->sessions_start_date;
	  //daily type
	  if ($type=='DD')
	  {
		  while ($caldate<=$edate)
		  {
		  	$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'];
		  }
	  }
	  // weekly type
	  else if ($type=='WW')
	  {
		 $weekno=$sInfo->sessions_type_weekly_weeks;
		 $weekday=$sInfo->sessions_type_weekly_days;

 	         $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'];
	     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;
   		    }
			$query = tep_db_query("select date_add('" . $caldate . "',interval '" . (($sInfo->sessions_type_weekly_weeks-1) * 7) . "' day) as datenew");
			$result=tep_db_fetch_array($query);
			$caldate=$result['datenew'];
		  }			 
	  }
	  //monthly day
	  else if ($type=='MD')
	  {
		 $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,'01',$year));
		  }			 
	  }
	  //Monthly month
	  else if ($type=='MM')
	  {
		 $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));
	     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));
		  }			 
	  }
	  else if ($type=='YD')
	  {
		 $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_yearly_monthdays;
		 $month=$sInfo->sessions_type_yearly_months;
		 $year=$result['year'];
		 if ($day<$result['day']) $year=$year+1;
     $caldate=date('Y-m-d',mktime(0,0,0,$month,$day,$year));
	     while ($caldate<=$edate)
		  {
                     $result_dates[]=$caldate;
		    $year=$year+1;
			$caldate=date('Y-m-d',mktime(0,0,0,$month,$day,$year));
		  }			 
	  }
	  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));
	     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));
		  }			 
	  }
   return $result_dates;
 }
 
 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'] . "'");
   }
 }
 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;
   }
   tep_db_perform(TABLE_EVENTS_SESSIONS, $sql_data_array);
 }
 function tep_delete_single_session($session_id)
 {
    if (!tep_check_linked_reservation($session_id))
     tep_db_query("delete from " . TABLE_EVENTS_SESSIONS . " where sessions_id='" . $session_id . "'");
    else
     tep_db_query("update " . TABLE_EVENTS_SESSIONS . " set sessions_type='' where sessions_id='" . $session_id . "'");
 }
 function tep_delete_recursive_session($session_id)
 {
    tep_db_query("delete from " . TABLE_RECURSIVE_SESSIONS . " where recursive_sessions_id='" . $session_id . "'");
 }
 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));
 }
 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();
 }
 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;
 }
?>
