<?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'] : '');
	$service_id = (isset($HTTP_GET_VARS['sID']) ?(int)$HTTP_GET_VARS['sID'] : 0);
	$template_type=(isset($HTTP_GET_VARS['type'])?strtolower($HTTP_GET_VARS['type']) : "se");
	$templates=getTemplates($service_id,$template_type);
	//table name;
	switch($template_type){
		case "sm":
			$table_name=TABLE_SERVICES_SMS_MESSAGES;
			$back_url=FILENAME_SERVICES_SMS_MESSAGES;
			$fields_list="message_send,message_subject,message_text,service_id,message_type,message_send1";
			break;
		case "se":
		default:
			$table_name=TABLE_SERVICES_MESSAGES;
			$back_url=FILENAME_SERVICES_MESSAGES;
			$fields_list="message_send,message_subject,message_reply_to,message_text,message_format,service_id,message_type,message_send1";
	}
	// get the action to update
	if ($action=="update"){
		$message_keys=&$HTTP_POST_VARS["select_message"];
		$service_keys=&$HTTP_POST_VARS["select_service"];
		if (sizeof($message_keys)<=0 || sizeof($service_keys)<=0 || $service_id<0)
			tep_redirect(tep_href_link($back_url,'service_id=' . $service_id));

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

		$message_query=tep_db_query("SELECT  " . $fields_list . " from " . $table_name . " where service_id='" . $service_id . "' and message_type in('" . $message_string . "')");
		// get the message status for services we have to copy
		$service_status_query=tep_db_query("SELECT count(*) as total,message_type,service_id from " . $table_name . " where service_id in(" . $service_string . ") and message_type in('" . $message_string . "') group by service_id,message_type");
		
		while($service_status=tep_db_fetch_array($service_status_query)){
			if (isset($status[$service_status["service_id"]])){
				$status[$service_status["service_id"]][$service_status["message_type"]]=$service_status["total"];
			}
		}
		while($message=tep_db_fetch_array($message_query)){
			reset($status);
			//iterate with the service we have to find insert or update
			while(list($sukey,)=each($status)){
				$message["service_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","service_id='" . $sukey . "' and message_type='" . $message["message_type"] . "'");
				} else { //insert operation
					tep_db_perform($table_name,$message);
				}
			}
		}
	
		tep_redirect(tep_href_link($back_url,'service_id=' . $service_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 service_id='" . $service_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';
		}
	}
	
	//to fetch the service names
	$service_array=array();
	$service_query=tep_db_query("SELECT se.service_id,sed.service_name from " . TABLE_SERVICES . " se, " . TABLE_SERVICES_DESCRIPTION . " sed where se.service_id=sed.service_id " . SERVICES_CONDITION_STATUS . " order by sed.service_name");
	
	while($service_result=tep_db_fetch_array($service_query)){
		if ($service_id!=$service_result["service_id"])
			$service_array[$service_result['service_id']]=array('text'=>$service_result['service_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_service[]");
					error_result[0]="<?php echo stripslashes(addslashes(ERR_SELECT_MESSAGE));?>";
					error_result[1]="<?php echo stripslashes(addslashes(ERR_SELECT_SERVICES));?>";
					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_SERVICES_COPY_MESSAGES,'action=update&sID=' . $service_id . '&type=' . $template_type,"post",'onSubmit="javascript:return validateForm();"');?>
			<!-- <tr>
				<td class="pageHeading"><?php //echo sprintf(HEADING_MESSAGE_TITLE,tep_get_service_name($service_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_SERVICES;?></td>
								</tr>
								<tr>
									<td class="tableHeading"><?php echo customListBox("select_message",$templates,0,300,200);?></td>
									<td class="tableHeading"><?php echo customListBox("select_service",$service_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,'service_id=' . $service_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($service_id,$type){
		$templates=array();
		switch($type){
			case "sm": //sms messages
				$templates=array(	'RCF'=>array('text'=>TEXT_TYPE_SER_RCF,'status'=>0,'color'=>'#FF0000'),
									'WTR'=>array('text'=>TEXT_TYPE_SER_WTR,'status'=>0,'color'=>'#FF0000'),
									'INV'=>array('text'=>TEXT_TYPE_SER_INV,'status'=>0,'color'=>'#FF0000'),
									'RN1'=>array('text'=>TEXT_TYPE_SER_RN1,'status'=>0,'color'=>'#FF0000'),
									'RN2'=>array('text'=>TEXT_TYPE_SER_RN2,'status'=>0,'color'=>'#FF0000'),
									'RN3'=>array('text'=>TEXT_TYPE_SER_RN3,'status'=>0,'color'=>'#FF0000'),
									'RN4'=>array('text'=>TEXT_TYPE_SER_RN4,'status'=>0,'color'=>'#FF0000'),
									'RN5'=>array('text'=>TEXT_TYPE_SER_RN5,'status'=>0,'color'=>'#FF0000'),
									'MSV'=>array('text'=>TEXT_TYPE_SER_MSV,'status'=>0,'color'=>'#FF0000'),
									'RS1'=>array('text'=>TEXT_TYPE_SER_RS1,'status'=>0,'color'=>'#FF0000'),
									'RS2'=>array('text'=>TEXT_TYPE_SER_RS2,'status'=>0,'color'=>'#FF0000'),
									'RE1'=>array('text'=>TEXT_TYPE_SER_RE1,'status'=>0,'color'=>'#FF0000'),
									'RE2'=>array('text'=>TEXT_TYPE_SER_RE2,'status'=>0,'color'=>'#FF0000'));
				break;
			case "se": //service messages
			default:
				$templates=array(	'RCF'=>array('text'=>TEXT_TYPE_SER_RCF,'status'=>0,'color'=>'#FF0000'),
									'WTR'=>array('text'=>TEXT_TYPE_SER_WTR,'status'=>0,'color'=>'#FF0000'),
									'INV'=>array('text'=>TEXT_TYPE_SER_INV,'status'=>0,'color'=>'#FF0000'),
									'RN1'=>array('text'=>TEXT_TYPE_SER_RN1,'status'=>0,'color'=>'#FF0000'),
									'RN2'=>array('text'=>TEXT_TYPE_SER_RN2,'status'=>0,'color'=>'#FF0000'),
									'RN3'=>array('text'=>TEXT_TYPE_SER_RN3,'status'=>0,'color'=>'#FF0000'),
									'RN4'=>array('text'=>TEXT_TYPE_SER_RN4,'status'=>0,'color'=>'#FF0000'),
									'RN5'=>array('text'=>TEXT_TYPE_SER_RN5,'status'=>0,'color'=>'#FF0000'),
									'MSV'=>array('text'=>TEXT_TYPE_SER_MSV,'status'=>0,'color'=>'#FF0000'),
									'RS1'=>array('text'=>TEXT_TYPE_SER_RS1,'status'=>0,'color'=>'#FF0000'),
									'RS2'=>array('text'=>TEXT_TYPE_SER_RS2,'status'=>0,'color'=>'#FF0000'),
									'RE1'=>array('text'=>TEXT_TYPE_SER_RE1,'status'=>0,'color'=>'#FF0000'),
									'RE2'=>array('text'=>TEXT_TYPE_SER_RE2,'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];
			
		}
			
		//	echo (count($list));
		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_SERVICES . '</td></tr>';
		}
		$result.='</table></div>' . "\n";
		echo $result;
	}
?>