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

  // action - course of action to move or display page contents,subscription_id - source subscription where the message is moved
  // template -type indicates email templates or sms templates

	$action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');
	$subscription_id = (isset($HTTP_GET_VARS['suID']) ?(int)$HTTP_GET_VARS['suID'] : 0);
	$template_type=(isset($HTTP_GET_VARS['type'])?strtolower($HTTP_GET_VARS['type']) : "su");
	$templates=getTemplates($subscription_id,$template_type);
	//table name;
	switch($template_type){
		case "su":
		default:
			$table_name=TABLE_SUBSCRIPTION_MESSAGES;
			$back_url=FILENAME_SUBSCRIPTION_MESSAGES;
			$fields_list="message_send,message_send1,message_subject,message_reply_to,message_text,message_format,subscription_id,message_type";
	}
	// get the action to update
	if ($action=="update"){
		$message_keys=&$HTTP_POST_VARS["select_message"];
		$subscription_keys=&$HTTP_POST_VARS["select_subscription"];
		if (sizeof($message_keys)<=0 || sizeof($subscription_keys)<=0 || $subscription_id<0)
			tep_redirect(tep_href_link($back_url,'subscription_id=' . $subscription_id));

		// fetch subscription into total array to find and subscription has already message
		$status=array();
		reset($subscription_keys);
		for ($icnt=0;$icnt<sizeof($subscription_keys);$icnt++){
			$status[$subscription_keys[$icnt]]=array();
		}
		//get the input message_ids,subscription_ids
		$message_string=join("','",$message_keys);
		$subscription_string=join(",",$subscription_keys);

		$message_query=tep_db_query("SELECT  " . $fields_list . " from " . $table_name . " where subscription_id='" . $subscription_id . "' and message_type in('" . $message_string . "')");

		// get the message status for subscriptions we have to copy
		$subscription_status_query=tep_db_query("SELECT count(*) as total,message_type,subscription_id from " . $table_name . " where subscription_id in(" . $subscription_string . ") and message_type in('" . $message_string . "') group by subscription_id,message_type");
		
		while($subscription_status=tep_db_fetch_array($subscription_status_query)){
			if (isset($status[$subscription_status["subscription_id"]])){
				$status[$subscription_status["subscription_id"]][$subscription_status["message_type"]]=$subscription_status["total"];
			}
		}
		while($message=tep_db_fetch_array($message_query)){
			reset($status);
			//iterate with the subscription we have to find insert or update
			while(list($sukey,)=each($status)){
				$message["subscription_id"]=$sukey;
				$count=isset($status[$sukey][$message["message_type"]])?$status[$sukey][$message["message_type"]]:0;
				if ($count>0){ // update operation
					tep_db_perform($table_name,$message,"update","subscription_id='" . $sukey . "' and message_type='" . $message["message_type"] . "'");
				} else { //insert operation
					tep_db_perform($table_name,$message);
				}
			}
		}
	
		tep_redirect(tep_href_link($back_url,'subscription_id=' . $subscription_id));
	}
	// get the message list
	$type_params="";
	reset($templates);
	while(list($key,)=each($templates)){
		if ($type_params=="") $type_params.=" and message_type in(";
		$type_params.="'" . $key . "',";
	}
	$type_params=substr($type_params,0,-1) . ")";
	$message_query=tep_db_query("SELECT count(*) as total,message_type from " . $table_name . " where subscription_id='" . $subscription_id . "' " . $type_params . " group by message_type");
	while($message=tep_db_fetch_array($message_query)){
		if ((int)$message["total"]>0 && isset($templates[$message['message_type']])){
			$templates[$message['message_type']]['status']=1;
			$templates[$message['message_type']]['color']='#0000FF';
		}
	}
	$subscription_array=array();
	$subscription_query=tep_db_query("SELECT su.subscription_id,sud.subscription_name from " . TABLE_SUBSCRIPTIONS . " su, " . TABLE_SUBSCRIPTIONS_DESCRIPTION . " sud where su.subscription_id=sud.subscription_id " . SUBSCRIPTION_CONDITION_STATUS . " order by sud.subscription_name");
	while($subscription_result=tep_db_fetch_array($subscription_query)){
		if ($subscription_id!=$subscription_result["subscription_id"])
			$subscription_array[$subscription_result['subscription_id']]=array('text'=>$subscription_result['subscription_name'],'color'=>"#000000");
	}
?>
<!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" onLoad="SetFocus();">
<!-- 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%" valign="top">
		<table border="0" width="100%" cellspacing="0" cellpadding="2">
		<?php if ($action=="") { ?>
			<script language=javascript>
				function validateForm(){
					var icnt;
					var listbox;
					var error_result=Array();
					var listbox_name=Array("select_message[]","select_subscription[]");
					error_result[0]="<?php echo stripslashes(addslashes(ERR_SELECT_MESSAGE));?>";
					error_result[1]="<?php echo stripslashes(addslashes(ERR_SELECT_SUBSCRIPTION));?>";
					for(ecnt=0;ecnt<listbox_name.length;ecnt++){
						listbox=document.copy_message.elements[listbox_name[ecnt]];
						if (listbox==null) break;
						if (listbox.length){
							for(icnt=0;icnt<listbox.length;icnt++){
								if (listbox[icnt].checked){
									 error_result[ecnt]="";
									 break;
								}
							}
						} else {
							if (listbox.checked) error_result[ecnt]="";
						}
					}
					if (error_result[0]!=""){
						alert(error_result[0]);
						return false;
					}
					if (error_result[1]!=""){
						alert(error_result[1]);
						return false;
					}
					return true;
				}
			</script>		
			<?php echo tep_draw_form("copy_message",FILENAME_SUBSCRIPTION_COPY_MESSAGES,'action=update&suID=' . $subscription_id . '&type=' . $template_type,"post",'onSubmit="javascript:return validateForm();"');?>
			<!-- <tr>
				<td class="pageHeading"><?php //echo sprintf(HEADING_TITLE,tep_get_subscription_name($subscription_id));?></td>
			</tr> !-->
			<tr>
				<td><?php echo tep_draw_separator('pixel_trans.gif','1','5');?></td>
			</tr>
			<tr>
				<td>
				<table border="0" cellpadding="0" cellspacing="0">
					<tr>
						<td>
							<table border="0" cellpadding="2" cellspacing="5" >
								<tr class="attributes-even">
									<td class="tableHeading"><?php echo SELECT_MESSAGES;?></td>
									<td class="tableHeading"><?php echo SELECT_SUBSCRIPTIONS;?></td>
								</tr>
								<tr>
									<td class="tableHeading"><?php echo customListBox("select_message",$templates,0,300,200);?></td>
									<td class="tableHeading"><?php echo customListBox("select_subscription",$subscription_array,0,500,200);?></td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td><?php echo tep_draw_separator('pixel_trans.gif','1','5');?></td>
					</tr>
					<tr>
						<td>
							<table border="0" cellpadding="2" cellspacing="5"  width="100%">
								<tr>
									<td class="main" align="left"><?php echo '<a href="' . tep_href_link($back_url,'subscription_id=' . $subscription_id) . '"' . tep_image_button("button_back.gif",IMAGE_BACK) . '</a>';?></td>
									<td class="main" align="right"><?php echo tep_image_submit("button_copy_messages.gif",IMAGE_COPY_MESSAGES);?></td>
								</tr>
							</table>
						</td>
					</tr>
				</table>
				</td>
			</tr>
			</form>
		<?php }?>
		</table>
		</td>
	</tr>
</table>
<!-- body_eof //-->

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


<?php  // function used in this page

	function &getTemplates($subscription_id,$type){
		$templates=array();
		switch($type){
			case "su": //subscription messages
			default:
				$templates=array(	'RCF'=>array('text'=>TEXT_TYPE_SUB_RCF,'status'=>0,'color'=>'#FF0000'),
									'INV'=>array('text'=>TEXT_TYPE_SUB_INV,'status'=>0,'color'=>'#FF0000'),
									'RN1'=>array('text'=>TEXT_TYPE_SUB_RN1,'status'=>0,'color'=>'#FF0000'),
									'RN2'=>array('text'=>TEXT_TYPE_SUB_RN2,'status'=>0,'color'=>'#FF0000'),
									'RN3'=>array('text'=>TEXT_TYPE_SUB_RN3,'status'=>0,'color'=>'#FF0000'),
									'RN4'=>array('text'=>TEXT_TYPE_SUB_RN4,'status'=>0,'color'=>'#FF0000'),
									'RN5'=>array('text'=>TEXT_TYPE_SUB_RN5,'status'=>0,'color'=>'#FF0000'));

		}
		return $templates;
	}
	function customListBox($name,&$list,$selected="",$width=300,$height=100){
	$result="";
		$result.='<div id="' . $name . '_box" style="background-color: #FFFFFF;border-color: #7b9ebd; border-style: solid; border-width: 1px;overflow:auto;width:' . $width . ';height:' . $height . '">' . "\n";
		$result.='<table border="0" cellspacing="0" cellpadding="2" width="100%" valign="middle" align="center" class="BoxTable">' . "\n";
		reset($list);

		$selected=array();
		if (isset($HTTP_POST_VARS[$name]) && is_array($HTTP_POST_VARS[$name])){
			$selected=&$HTTP_POST_VARS[$name];
		}
		if (count($list)>0){
			while(list($key,)=each($list)){
				$result.='<tr class="attributes-even"><td class="main" width="20">' . "\n";
				$result.='<input type="checkbox" name="' . $name . '[]" value="' . $key . '" ' . (in_array($key,$selected)?"checked":"") . ' >';
				$result.='<td class="main"><font color="' . $list[$key]['color'] . '">' . $list[$key]['text'] .'</font></td></tr>' ."\n";
			}
		} else {
			$result.='<tr><td class="main">' . TEXT_NO_SUBSCRIPTIONS . '</td></tr>';
		}
		$result.='</table></div>' . "\n";
		echo $result;
	}
?>