<?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 
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
  require('includes/application_top.php');
  require(DIR_WS_CLASSES . 'currencies.php');
  
  $currencies=new currencies;

	//added new
	$action=(isset($HTTP_GET_VARS['action'])?$HTTP_GET_VARS['action']:'');
	$events_id=(isset($HTTP_GET_VARS['events_id'])?$HTTP_GET_VARS['events_id']:0);
	switch($action){
		case 'sendreserve':
			$customer_query=tep_db_query("SELECT c.customers_id,count(*) as total_count from " . TABLE_CUSTOMERS . " c, " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o where c.customers_id=o.customers_id and op.orders_id=o.orders_id and op.products_type='E' and op.events_type='R' group by customers_id having total_count>0");
			while($customer_result=tep_db_fetch_array($customer_query)){
				$reserve_query=tep_db_query("SELECT min(s.start_date) as first_date,min(s.sessions_id) as session_id,o.orders_id,s.events_id from " . TABLE_ORDERS . " o, " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_EVENTS_SESSIONS . " s where o.customers_id='" . $customer_result['customers_id'] . "' and op.orders_id=o.orders_id and op.products_id=s.sessions_id group by o.orders_id,s.events_id");
				while($reserve_result=tep_db_fetch_array($reserve_query))
				{
					$first_date_string=strtotime($reserve_result['first_date']);
					$sms_query=tep_db_query("SELECT message_send from ". TABLE_EVENTS_SMS_MESSAGES . " where events_id='" . (int)$reserve_result['events_id'] . "' and message_type='RSR'");
					if (tep_db_num_rows($sms_query)>0){
						$sms_result=tep_db_fetch_array($sms_query);
						$diff_days=(int)$sms_result['message_send'];
						if (getServerDate()>=date('Y-m-d',DateSub($first_date_string,'day',$diff_days))){
						tep_send_event_reminder_sms_email($reserve_result['session_id'],$reserve_result['orders_id']);}
					}
				} 
			}
		tep_redirect(tep_href_link(FILENAME_SEND_SUBSCRIPTION_SMS_EMAIL,'action=success&events_id=' . $events_id));
		break;    
		case 'sendall':
			$message = tep_db_prepare_input($HTTP_POST_VARS['message']);
			$from=tep_db_prepare_input($HTTP_POST_VARS['from']);
			$message=strip_tags($message,'<br>');
			$message=str_replace('<br>',chr(13) . chr(10),$message);
			$message=str_replace('<BR>',chr(13) . chr(10),$message);
			$type=isset($HTTP_GET_VARS['type'])?$HTTP_GET_VARS['type']:'';
			

			$to_name=$HTTP_POST_VARS['customer_name'];
			$to_address=$HTTP_POST_VARS['customers_fax'];
	
			
			$customer_query=tep_db_query("SELECT c.customers_id,c.customers_email_address,c.customers_firstname,c.customers_lastname,c.customers_fax,op.products_id,o.customers_country from " . TABLE_CUSTOMERS . " c, " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o, " . TABLE_EVENTS_SESSIONS . " s where op.orders_id=o.orders_id and o.customers_id=c.customers_id and c.customers_reserve_newsletter='1'" . 
											" and op.products_type='E' and op.events_type='" . $type . "' and op.products_id=s.sessions_id and s.events_id='" . $events_id . "' and s.start_date>curdate() group by o.customers_id");

			if ($message!="" && tep_db_num_rows($customer_query)>0 && ($type=='R' || $type=='W')){			
				while($mail=tep_db_fetch_array($customer_query)){
					if ($mail['customers_fax']!=''){
						send_sms($mail['customers_fax'],SMS_FROM_NAME,$message,$mail["customers_country"]);
					}
				}
				$messageStack->add_session(TEXT_SEND_SMS,'success');
			}
			tep_redirect(tep_href_link(FILENAME_SUBSCRIPTION_SMS_MESSAGES));
			break;
  }
  $from_email=$HTTP_SESSION_VARS["login_email"];
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
<tr> 
	<!-- body_text //-->	  
    <td width=100% align=left valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
<?php if ($action=='success'){ ?>
	<tr><td><table border="0" width="100%" cellspacing="0" cellpadding="2">
	    <tr><td align=left class='pageHeading' colspan='2'><?php echo HEADING_SUCCESS; ?></td></tr>
	  </table></td>
	</tr>
	<tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','5'); ?></td></tr>
	<tr><td class=main><?php echo TEXT_MAILS_SEND; ?></td></tr>
	<tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','5'); ?></td></tr>
	<tr><td class=main><?php echo '<a href="' . tep_href_link(FILENAME_SUBSCRIPTION_SMS_MESSAGES,'events_id=' . $events_id) . '">' . tep_image_button('button_continue.gif',IMAGE_CONTINUE) . '</a>'; ?></td></tr>
<?php } else if ($action=='new_message') { 
        $events_id=(int)$HTTP_GET_VARS['events_id'];
	$cust_id=(int)$HTTP_GET_VARS['cust_id'];
	$opt_id=$HTTP_GET_VARS['opt_id'];
	$message_id=$HTTP_GET_VARS['message_id'];
	$mail_content=array('message_subject'=>'',
			    'message_text'=>'');
        $mail_info=new objectInfo($mail_content);
	$show_template=false;
        if ($events_id!=0 & $cust_id!=0){
          $message_event=array('RCF'=>TEXT_TYPE_RCF,'RSR'=>TEXT_TYPE_RSR,'RNR'=>TEXT_TYPE_RNR,'FDB'=>TEXT_TYPE_FDB,'WPR'=>TEXT_TYPE_WPR,
	                       'WPL'=>TEXT_TYPE_WPL,'WTR'=>TEXT_TYPE_WPL);

          $template_query=tep_db_query("SELECT message_type from " . TABLE_EVENTS_SMS_MESSAGES . " where events_id='" . $events_id ."'");
	  $template_array[]=array('id'=>0,'TEXT'=>'NONE');
	  while($template_result=tep_db_fetch_array($template_query)){
             $template_array[]=array('id'=>$template_result['message_type'],'text'=>$message_event[$template_result['message_type']]);
	  }
	  if (sizeof($template_array)>1) $show_template=true;

	  $cust_query=tep_db_query("SELECT concat(c.customers_firstname,' ',c.customers_lastname) as customer_name,c.customers_fax from " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o, " . TABLE_CUSTOMERS . " c where op.orders_products_id='" . $opt_id . "' and op.orders_id=o.orders_id and o.customers_id=c.customers_id");

	  $cust_result=tep_db_fetch_array($cust_query);
	  if ($message_id!=''){
	      $message_query=tep_db_query("SELECT message_subject,message_text from " . TABLE_EVENTS_SMS_MESSAGES . " where events_id='" . $events_id ."' and message_type='" . $message_id . "'");
	      if ($message_result=tep_db_fetch_array($message_query)){
	          $message_result['message_text']=tep_replace_event_email($opt_id,$message_id,$message_result['message_text']);
		  $mail_info=new objectInfo($message_result);
	      }
	      $type=$message_id;
	  }
	}
?>
       <tr><td><table border="0" width="100%" cellspacing="0" cellpadding="2">
	    <tr><td align=left class='pageHeading' colspan='2'><?php echo HEADING_NEW_MESSAGE; ?></td></tr>
	  </table></td>
	</tr>
      <?php if ($show_template==true){ ?>
	      <tr>
		<td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
	      </tr>
	      <tr><td><table border=0 cellspacing="2" cellpadding="2"><tr>
		  <td class="main"><b><?php echo TEXT_SELECT_TEMPLATE; ?></b></td>
		  <td class="main" valign=middle>
		  <?php 
		     echo tep_draw_form('select_template',FILENAME_SEND_SMS_EMAIL,'','get');
		     echo tep_draw_hidden_field('action','new_message');
		     echo tep_draw_hidden_field('events_id',$events_id);
		     echo tep_draw_hidden_field('cust_id',$cust_id);
		     echo tep_draw_hidden_field('opt_id',$opt_id);
		     echo tep_draw_pull_down_menu('message_id',$template_array,$message_id) . '&nbsp;';
		     echo tep_image_submit('button_select.gif',IMAGE_TEMPLATE,'align=absmiddle');
		     echo '</form>'
		  ?>
	      </tr></table></td></tr>
      <?php } ?>
      <tr>

	<tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','3'); ?></td></tr>
          <tr><?php echo tep_draw_form('mail', FILENAME_SEND_SUBSCRIPTION_SMS_EMAIL, 'action=sendall&type=' . $HTTP_GET_VARS['type'] . '&events_id=' . $events_id); ?>
            <td><table border="0" cellpadding="2" cellspacing="2" >
              <tr>
                <td class="main" width=100><b><?php echo TEXT_FROM; ?></b></td>
                <td><?php echo tep_draw_input_field('from', $from_email,'size=30'); ?></td>
              </tr>
              <tr>
                <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
              <tr>
                <td class="main" width=100><b><?php echo TEXT_TO; ?></b></td>
                <td class="main">
		<?php 
		  if ($show_template==true){
		     echo $cust_result['customer_name'] . '(' . $cust_result['customers_fax'] . ')';
		     echo tep_draw_hidden_field('customer_name',$cust_result['customer_name']);
    		     echo tep_draw_hidden_field('customers_fax',$cust_result['customers_fax']);
		  }else{
		   echo ($type=='R'?TEXT_RESERVE_HOLDERS:TEXT_WAITLIST_HOLDERS); 
                  }		      
		?>
		</td>
              </tr>
                <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
              <tr>
                <td valign="top" class="main"><b><?php echo TEXT_MESSAGE; ?></b></td>
                <td><?php echo tep_draw_textarea_field('message', 'soft', '60', '15',$mail_info->message_text); ?></td>

	  </tr>

          <tr><td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>

          <tr><td colspan="2" align="right">
                 <?php
		     if ($cust_id!=0)
		        echo '<a href="' . tep_href_link(FILENAME_SEARCH_RESERVATIONS) . '">' . tep_image_button('button_back.gif',IMAGE_BACK) . '</a>&nbsp;';
                     else
		        echo '<a href="' . tep_href_link(FILENAME_SUBSCRIPTION_SMS_MESSAGES,'events_id=' . $events_id) . '">' . tep_image_button('button_back.gif',IMAGE_BACK) . '</a>&nbsp;';
		      echo tep_image_submit('button_send.gif', IMAGE_SEND_EMAIL);?>
                </td></tr>
	  </table></td>
	</tr>
      <?php } ?>
      </table></td>
   </tr>
</table>
<!-- body_text_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>

<?php
function DateSub($timestamp, $unit, $amount) {
// 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"]);
   return $timestamp - $delta;
}
?>