<?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 
*/
// 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,events_id - source event where the message is moved
  // template -type indicates email templates or sms templates

	$action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');
	$events_id = (isset($HTTP_GET_VARS['eID']) ?(int)$HTTP_GET_VARS['eID'] : 0);
	$template_type=(isset($HTTP_GET_VARS['type'])?strtolower($HTTP_GET_VARS['type']) : "ev");
	$templates=getTemplates($events_id,$template_type);
	//table name;
	switch($template_type){
		case "sm":
			$table_name=TABLE_EVENTS_SMS_MESSAGES;
			$back_url=FILENAME_EVENTS_SMS_MESSAGES;
			$fields_list="message_send,message_subject,message_text,events_id,message_type";
			break;
		case "ev":
		default:
			$fields_list="message_send,message_subject,message_reply_to,message_text,message_format,events_id,message_type";
			$table_name=TABLE_EVENTS_MESSAGES;
			$back_url=FILENAME_EVENTS_MESSAGES;
	}
	// get the action to update
	if ($action=="update"){
		$message_keys=&$HTTP_POST_VARS["select_message"];
		$events_keys=&$HTTP_POST_VARS["select_event"];
		if (sizeof($message_keys)<=0 || sizeof($events_keys)<=0 || $events_id<0)
			tep_redirect(tep_href_link($back_url,'events_id=' . $events_id));

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

		$message_query=tep_db_query("SELECT  " . $fields_list . " from " . $table_name . " where events_id='" . $events_id . "' and message_type in('" . $message_string . "')");
		// get the message status for events we have to copy
		$event_status_query=tep_db_query("SELECT count(*) as total,message_type,events_id from " . $table_name . " where events_id in(" . $events_string . ") and message_type in('" . $message_string . "') group by events_id,message_type");
		
		while($event_status=tep_db_fetch_array($event_status_query)){
			if (isset($status[$event_status["events_id"]])){
				$status[$event_status["events_id"]][$event_status["message_type"]]=$event_status["total"];
			}
		}
		while($message=tep_db_fetch_array($message_query)){
			reset($status);
			//iterate with the events we have to find insert or update
			while(list($ekey,)=each($status)){
				$message["events_id"]=$ekey;
				$count=isset($status[$ekey][$message["message_type"]])?$status[$ekey][$message["message_type"]]:0;
				if ($count>0){ // update operation
					tep_db_perform($table_name,$message,"update","events_id='" . $ekey . "' and message_type='" . $message["message_type"] . "'");
				} else { //insert operation
					tep_db_perform($table_name,$message);
				}
			}
		}

		tep_redirect(tep_href_link($back_url,'events_id=' . $events_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 events_id='" . $events_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';
		}
	}
	$events_array=array();
	$event_query=tep_db_query("SELECT e.events_id,ed.events_name from " . TABLE_EVENTS . " e, " . TABLE_EVENTS_DESCRIPTION . " ed where e.events_id=ed.events_id " . EVENTS_CONDITION_STATUS . " order by ed.events_name");
	while($event_result=tep_db_fetch_array($event_query)){
		if ($events_id!=$event_result["events_id"])
			$events_array[$event_result['events_id']]=array('text'=>$event_result['events_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_event[]");
					error_result[0]="<?php echo stripslashes(addslashes(ERR_SELECT_MESSAGE));?>";
					error_result[1]="<?php echo stripslashes(addslashes(ERR_SELECT_EVENT));?>";
					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_EVENTS_COPY_MESSAGES,'action=update&eID=' . $events_id . '&type=' . $template_type,"post",'onSubmit="javascript:return validateForm();"');?>
			<!-- <tr>
				<td class="pageHeading"><?php //echo sprintf(HEADING_MESSAGE_TITLE,tep_get_events_name($events_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_EVENTS;?></td>
								</tr>
								<tr>
									<td class="tableHeading"><?php echo customListBox("select_message",$templates,0,300,200);?></td>
									<td class="tableHeading"><?php echo customListBox("select_event",$events_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,'events_id=' . $events_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($events_id,$type){
		$templates=array();
		switch($type){
			case "sm": //sms messages
				$templates=array(	'RCF'=>array('text'=>TEXT_TYPE_RCF,'status'=>0,'color'=>'#FF0000'),
									'WCF'=>array('text'=>TEXT_TYPE_WCF,'status'=>0,'color'=>'#FF0000'),
									'RSR'=>array('text'=>TEXT_TYPE_RSR,'status'=>0,'color'=>'#FF0000'),
									'RNR'=>array('text'=>TEXT_TYPE_RNR,'status'=>0,'color'=>'#FF0000'),
									'FDB'=>array('text'=>TEXT_TYPE_FDB,'status'=>0,'color'=>'#FF0000'),
									'WPR'=>array('text'=>TEXT_TYPE_WPR,'status'=>0,'color'=>'#FF0000'),
									'WPL'=>array('text'=>TEXT_TYPE_WPL,'status'=>0,'color'=>'#FF0000')
									);
				break;
			case "ev": // events messages
			default:
				$templates=array(	'RCF'=>array('text'=>TEXT_TYPE_RCF,'status'=>0,'color'=>'#FF0000'),
									'WCF'=>array('text'=>TEXT_TYPE_WCF,'status'=>0,'color'=>'#FF0000'),
									'RNR'=>array('text'=>TEXT_TYPE_RNR,'status'=>0,'color'=>'#FF0000'),
									'FDB'=>array('text'=>TEXT_TYPE_FDB,'status'=>0,'color'=>'#FF0000'),
									'WPR'=>array('text'=>TEXT_TYPE_WPR,'status'=>0,'color'=>'#FF0000'),
									'WPL'=>array('text'=>TEXT_TYPE_WPL,'status'=>0,'color'=>'#FF0000'),
									'WTR'=>array('text'=>TEXT_TYPE_WTR,'status'=>0,'color'=>'#FF0000'),
									'INV'=>array('text'=>TEXT_TYPE_INV,'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];
		}
		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";
		}
		$result.='</table></div>' . "\n";
		echo $result;
	}
?>