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

  $action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');
  $mail_template_id=(isset($HTTP_GET_VARS['teID'])?(int)$HTTP_GET_VARS['teID']: 0);
  $server_date = getServerDate(true);
  
 	switch($action){
  		// delete the template 
		case 'delete_template_confirm':
			$mail_template_id=(int)$HTTP_POST_VARS['mail_template_id'];
			if ($mail_template_id>0){
				tep_db_query("delete  from " . ' '. TABLE_MAIL_TEMPLATES .' '."where mail_template_id=" . $mail_template_id);
			}
			tep_redirect(tep_href_link(FILENAME_MAIL_TEMPLATES));
			break;	
		// save the content into database
	case 'update_template':
	case 'insert_template':
			// prepare to fetch the content from url,file or typed content;
			$mail_template_content="";
			$mail_template_url=tep_db_prepare_input($HTTP_POST_VARS["mail_template_url"]);
			$template_file = new upload('mail_template_file');
			$template_file->set_destination(DIR_FS_CATALOG_IMAGES);

			if ($template_file->parse() && $template_file->save()) { // get content from uploaded file
				$template_file_name = $template_file->filename;
				$handle=@fopen(DIR_FS_CATALOG_IMAGES . $template_file_name,"r");
				if ($handle){
					$mail_template_content=@fread($handle,filesize(DIR_FS_CATALOG_IMAGES . $template_file_name));
					@fclose($handle);
				}
			} else if ($mail_template_url!=""){
				$mail_template_content=@file_get_contents($mail_template_url);
				if (!$mail_template_content){
					$messageStack->add_session(sprintf(ERROR_FAILED_TO_GET_URL,$mail_template_url), 'error');
				}
			} else {
				$mail_template_content=tep_db_prepare_input($HTTP_POST_VARS["mail_template_content"]);
			}
			
			$sql_array=array();
			$sql_array=array('mail_template_name'=>tep_db_prepare_input($HTTP_POST_VARS['mail_template_name']),
								'mail_template_content'=>$mail_template_content,
								'mail_template_format'=>tep_db_prepare_input($HTTP_POST_VARS['mail_template_format']),
			
								);
			$mail_template_id=isset($HTTP_POST_VARS["mail_template_id"])?$HTTP_POST_VARS["mail_template_id"]:0;
			if($action=="update_template" && $mail_template_id>0){
				$sql_array['last_modified']=$server_date;
				tep_db_perform(TABLE_MAIL_TEMPLATES,$sql_array,'update','mail_template_id="' . $mail_template_id . '"');
			} else {
				$sql_array['date_added']=$server_date;
				tep_db_perform(TABLE_MAIL_TEMPLATES,$sql_array);
			}
			tep_redirect(tep_href_link(FILENAME_MAIL_TEMPLATES));
		break;
	// copy template for selected template
	case 'copy_template_confirm':
		$mail_template_id=$HTTP_POST_VARS['mail_template_id'];
		
		$template_query=tep_db_query("select mail_template_name,mail_template_content,mail_template_format from " . TABLE_MAIL_TEMPLATES . " where mail_template_id='" . $mail_template_id . "'");
		$template_result=tep_db_fetch_array($template_query);
		$template_result['mail_template_name']=$HTTP_POST_VARS["copy_template_name"];
		$template_result['date_added']=$server_date;
		
		tep_db_perform(TABLE_MAIL_TEMPLATES,$template_result);
		tep_redirect(tep_href_link(FILENAME_MAIL_TEMPLATES));
		break;
  }			
	// array for template format
	$formats=array(array('id'=>'T','text'=>TEXT_FORMAT_TEXT),
	array('id'=>'H','text'=>TEXT_FORMAT_HTML),
	array('id'=>'B','text'=>TEXT_FORMAT_BOTH));
	
	$formats_array=array('T'=>TEXT_FORMAT_TEXT,'H'=>TEXT_FORMAT_HTML,'B'=>TEXT_FORMAT_BOTH);
?>
<!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">
<style type="text/css">
	#pup {position:absolute; visibility:hidden; z-index:200; width:130; }
</style>
<?php
// WebMakers.com Added: Java Scripts
include(DIR_WS_INCLUDES . 'javascript/' . 'webmakers_added_js.php')
?>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF" onLoad="SetFocus();">
<script language="Javascript1.2"><!-- // load htmlarea
			// MaxiDVD Added WYSIWYG HTML Area Box + Admin Function v1.7 - 2.2 MS2 events Description HTML - Head
			_base_url="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG;?>";
			_editor_url = "<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_ADMIN; ?>htmlarea/";  // URL to htmlarea files
			var win_ie_ver = parseFloat(navigator.appVersion.split("MSIE")[1]);
			if (navigator.userAgent.indexOf('Mac')        >= 0) { win_ie_ver = 0; }
			if (navigator.userAgent.indexOf('Windows CE') >= 0) { win_ie_ver = 0; }
			if (navigator.userAgent.indexOf('Opera')      >= 0) { win_ie_ver = 0; }
			<?php if (HTML_AREA_WYSIWYG_BASIC_PD == 'Basic'){ ?>  if (win_ie_ver >= 5.5) {
			document.write('<scr' + 'ipt src="' +_editor_url+ 'editor_basic.js"');
			document.write(' language="Javascript1.2"></scr' + 'ipt>');
			} else { document.write('<scr'+'ipt>function editor_generate() { return false; }</scr'+'ipt>'); }
			<?php } else{ ?> if (win_ie_ver >= 5.5) {
			document.write('<scr' + 'ipt src="' +_editor_url+ 'editor_advanced.js"');
			document.write(' language="Javascript1.2"></scr' + 'ipt>');
			} else { document.write('<scr'+'ipt>function editor_generate() { return false; }</scr'+'ipt>'); }
			<?php }?>
// --></script>
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
<script language="javascript" src="includes/popup.js"></script>
			<script language="JavaScript">
			// function for popup
				var baby=new Array();
				baby[1]="";
				
				function showpopup(type)
				{
					var contentText;
					
					switch(type){
						case "file_from_url":
							contentText="if you have already uploaded your newsletter as a HTML file,enter the URL of the file and it will be downloaded and saved as the content of your newsletter.";
							break;
						case "file_from":
							contentText="Select a file from your local hard drive to use as content for this newsletter.";
							break;
						case "copy_template":
							contentText="Enter Copy template name";
							break;
					}
					content='<table border="0" cellspacing="0" cellpadding="4" class="formArea" width="100%"><tr><td class=smallText>';
					content+=contentText;
					content+='</td></tr></table>';
					popwidth=250;
					baby[1]=content;
					if (baby[1]!="")
					{
						popup("1","#ffffff");
					}	
				}
			// form validation
			function validateForm()
			{
				var error_result="";
				if(document.insert_template.template_name.value=="")	
					error_result="<?php echo ERROR_TEMPLATE_NAME;?>";
				
			 if(error_result!="")
			 {
			 	alert(error_result);
				return false;
			}
			return;
			}
			function AddField(){
				var select_field;
				var select_id;
				if (document.insert_template.merge_fields.selectedIndex<=-1) return;

				select_id=document.insert_template.merge_fields.options[document.insert_template.merge_fields.selectedIndex].value;
				if (select_id.length>3) return;

				select_field="%%" + document.insert_template.merge_fields.options[document.insert_template.merge_fields.selectedIndex].innerText + "%%";
				editor_insertHTML("mail_template_content",select_field);
			}
			</script>
<!-- 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"  cellspacing="1" cellpadding="1" width="100%">
		<tr>
			<td>
			<?php // create new template
			if($action=='create_template' || $action=='edit_template'){
				$teDetails=array(	'mail_template_name'=>'',
									'mail_template_format'=>'H',
									'date_added'=>'',
									'last_modified'=>'',
									'mail_template_content'=>''
									);
				if ($mail_template_id>0){
					$template_query=tep_db_query("SELECT mail_template_name,mail_template_content,mail_template_format,date_added,last_modified from " . TABLE_MAIL_TEMPLATES 
													. " where mail_template_id='" . $mail_template_id . "'");
					$template_result=tep_db_fetch_array($template_query);
					$teInfo=new objectInfo($template_result);
					$form_action="update_template";
				} else {
					$teInfo=new objectInfo($teDetails);
					$form_action="insert_template";
				}
				echo tep_draw_form('insert_template',FILENAME_MAIL_TEMPLATES, 'action=' .$form_action ,'post','enctype="multipart/form-data" onSubmit="return validateForm();"');
				if ($mail_template_id>0) echo tep_draw_hidden_field('mail_template_id',$mail_template_id);
				
				$merge_fields=array(array('id'=>'EN','text'=>TEXT_EN),
									array('id'=>'EL','text'=>TEXT_EL),
									array('id'=>'SI','text'=>TEXT_SI),
									array('id'=>'EI','text'=>TEXT_EI),
									array('id'=>'SST','text'=>TEXT_SST),
									array('id'=>'SHL','text'=>TEXT_SHL),
									array('id'=>'OCL','text'=>TEXT_OCL),
									array('id'=>'CNM','text'=>TEXT_CNM),
									array('id'=>'BMG','text'=>TEXT_BMG)
									);
				?>
					<table border="0" cellspacing="0" cellpadding="2">
						<tr>
							<td class="pageHeading"><?php echo sprintf(($mail_template_id>0?HEADING_TEMPLATE_EDIT:HEADING_TEMPLATE_NEW),$teInfo->mail_template_name);?>
							</td>
						</tr>
						<tr>
							<td><?php echo tep_draw_separator('pixel_trans.gif',1,15);?></td>
						</tr>
						<tr>
							<td>
							<table border="0" cellspacing="0" cellpadding="2" class="formArea">
								<tr>
									<td  class="main"><?php echo TEMPLATE_NAME;?></td>
									<Td  class="main"><?php echo tep_draw_separator('pixel_trans.gif',10,1) . tep_draw_input_field('mail_template_name',$teInfo->mail_template_name,'size="40"');?></Td>
								</tr>
								<tr>
									<td><?php echo tep_draw_separator('pixel_trans.gif',1,10);?></td>
								</tr>
								<tr class="main" valign="top">
									<td valign="top" colspan="2">
									<table border="0" cellpadding="0" cellspacing="0">
										<tr>
											<Td class="main"><?php echo tep_draw_textarea_field('mail_template_content','soft','80','10',$teInfo->mail_template_content);?></td>
											<td bgcolor="buttonface" valign="bottom">
												<?php echo '<div class="main"><b>' . TEXT_MERGE_FIELDS . '</b></div><br>' . tep_draw_pull_down_menu('merge_fields',$merge_fields,'','style="height:' . HTML_AREA_WYSIWYG_HEIGHT .' px" size=15 ondblClick="AddField()"');?>
											</td>
										</tr>
									</table>
									</td>
								</tr>
								<tr>
									<td><?php echo tep_draw_separator('pixel_trans.gif',1,10);?></td>
								</tr>
								<tr>
									<td class="main"><?php echo TEXT_MESSAGE_FORMAT; ?>
									<td class="main"><?php echo tep_draw_separator('pixel_trans.gif',10,1) . tep_draw_pull_down_menu('mail_template_format',$formats,$teInfo->mail_template_format); ?></td>
								</tr>
								<tr>
									<td><?php echo tep_draw_separator('pixel_trans.gif',1,10);?></td>
								</tr>
								<tr>
									<Td class="main"><?php echo TEXT_CONTENT_URL;?></Td>
									<td class="main"><?php echo tep_draw_separator('pixel_trans.gif',10,1) . tep_draw_input_field('mail_template_url','','size="40"');?>&nbsp;
										<span onMouseOver="javascript:showpopup('file_from_url')" onMouseOut="javascript:kill();">
										<?php echo tep_image(DIR_WS_ICONS . "help_icon.gif");?></span>
									</td>
								</Tr>
								<tr>
									<td class="main"><?php echo TEXT_CONTENT_FILE; ?></td>
									<td class="main"><?php echo tep_draw_separator('pixel_trans.gif',10,1);?><input type="file" name="mail_template_file" value="" size="40">&nbsp;
									<span onMouseOver="javascript:showpopup('file_from')" onMouseOut="javascript:kill();"><?php echo tep_image(DIR_WS_ICONS . "help_icon.gif");?></span></td>
								</tr>
								<tr>
									<td><?php echo tep_draw_separator('pixel_trans.gif',1,10);?></td>
								</tr>

							</table>
						</td>
					</tr>
					<tr>
						<td><?php echo tep_draw_separator('pixel_trans.gif',1,10);?></td>
					</tr>
					<Tr>
						<td class="main" align="left">
						<?php echo '<a href="'.FILENAME_MAIL_TEMPLATES.'">'.tep_image_button('button_cancel.gif',BUTTON_CANCEL).'</a>'.'&nbsp;&nbsp;&nbsp;'.tep_image_submit('button_save.gif',SAVE_TEMPLATE);?>
						</td>
					</Tr>
				</table>
				</form>
	<?php
	  if (HTML_AREA_WYSIWYG_DISABLE == 'Disable') {} else { ?>
            <script language="JavaScript1.2" defer>
             var config = new Object();  // create new config object
             config.width = "<?php echo HTML_AREA_WYSIWYG_WIDTH; ?>px";
             config.height = "<?php echo HTML_AREA_WYSIWYG_HEIGHT; ?>px";
             config.bodyStyle = 'background-color: <?php echo HTML_AREA_WYSIWYG_BG_COLOUR; ?>; font-family: "<?php echo HTML_AREA_WYSIWYG_FONT_TYPE; ?>"; color: <?php echo HTML_AREA_WYSIWYG_FONT_COLOUR; ?>; font-size: <?php echo HTML_AREA_WYSIWYG_FONT_SIZE; ?>pt;';
             config.debug = <?php echo HTML_AREA_WYSIWYG_DEBUG; ?>;
 			 editor_generate('mail_template_content',config);
	       </script>
		<?php }
		
		} else if ($action=="new_template_preview"){ 
					$template_query=tep_db_query("SELECT mail_template_name,mail_template_format,mail_template_content from " .
													TABLE_MAIL_TEMPLATES . " where mail_template_id='" . $mail_template_id . "'");
					$template_result=tep_db_fetch_array($template_query);
					$teInfo=new objectInfo($template_result);
		?>
			<table border="0" width="100%" cellspacing="0" cellpadding="0">
				<tr>
					<Td class="pageHeading" valign="top">
						<?php echo $teInfo->mail_template_name;?>
					</Td>
				</tr>
				<tr>
					<td><?php echo tep_draw_separator('pixel_trans.gif',1,10);?></td>
				</tr>
				<tr>
					<td class="main"><?php echo '<b>' . TEXT_MESSAGE_FORMAT . '<b>' . $formats_array[$teInfo->mail_template_format];?> </td>
				</tr>
				<tr>
					<td class="smallText"><?php echo $teInfo->mail_template_content;?></td>
				</tr>
				<tr>
					<td><?php echo tep_draw_separator('pixel_trans.gif',1,10);?></td>
				</tr>
				<tr>
					<td><?php echo '<a href="' . tep_href_link(FILENAME_MAIL_TEMPLATES,'teID=' . $mail_template_id) . '">' . tep_image_button('button_back.gif',IMAGE_BACK) . '</a>';?></td>
				</tr>
			</table>
		<?php } else { 
		?>
			<table border="0" width="100%" cellspacing="0" cellpadding="0">
				<tr>
					<Td class="pageHeading" valign="top">
						<?php echo HEADING_TITLE;?>
					</Td>
				</tr>
				<tr>
					<td><?php echo tep_draw_separator('pixel_trans.gif',1,10);?></td>
				</tr>
				<tr>
					<td valign="top">
					<table border="0" cellpadding="0" cellspacing="0" width="100%">
						<Tr class="dataTableHeadingRow">
							<Td class="dataTableHeadingContent">
								<?php echo TEMPLATE_NAME;?>
							</Td>
							<td class="dataTableHeadingContent">
								<?php echo TEMPLATE_FORMAT;?>
							</td>
							<Td></Td>
						</Tr>
				<?php	$template_query=tep_db_query("SELECT mail_template_id,mail_template_name,mail_template_format,mail_template_content,date_added,last_modified from " . 
												TABLE_MAIL_TEMPLATES . " order by mail_template_name");
						
						$template_count=0;
						
						
						while($template=tep_db_fetch_array($template_query)){
							$template_count++;
							if (($mail_template_id<=0 || ($mail_template_id>0 && $mail_template_id == $template['mail_template_id'])) && !isset($teInfo)) {
								$teInfo=new objectInfo($template);
							}								
							  if (isset($teInfo) && is_object($teInfo) && ($template['mail_template_id'] == $teInfo->mail_template_id)) {
								echo '              <tr height="15" id="defaultSelected" class="dataTableRowSelected" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href=\'' . tep_href_link(FILENAME_MAIL_TEMPLATES, 'teID=' . $template['mail_template_id'] . '&action=new_template_preview') . '\'">' . "\n";
							  } else {
								echo '              <tr height="15" class="dataTableRow" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href=\'' . tep_href_link(FILENAME_MAIL_TEMPLATES, 'teID=' . $template['mail_template_id']) . '\'">' . "\n";
							  }
				?>
								<td class="dataTableContent"><?php echo '<b>' . $template['mail_template_name'] . '</b>'; ?></td>
								<td class="dataTableContent" align="left"><?php echo $formats_array[$template['mail_template_format']]; ?></td>
								<td class="dataTableContent" align="right"><?php if (isset($teInfo) && is_object($teInfo) && ($template['mail_template_id'] == $mail_template_id) ) { echo tep_image(DIR_WS_IMAGES . 'icon_arrow_right.gif', ''); } else { echo '<a href="' . tep_href_link(FILENAME_MAIL_TEMPLATES, '&teID=' . $template['mail_template_id'] . '&action=new_template_preview') . '">' . tep_image(DIR_WS_IMAGES . 'icon_info.gif', IMAGE_ICON_INFO) . '</a>'; } ?>&nbsp;</td></tr>
				<?php  } // while ?>
								<tr>
									<td colspan="3">
									<table border="0" width="100%" cellspacing="0" cellpadding="2">
										<tr>
											<td class="smallText"><?php echo TEXT_TEMPLATE_COUNT . '&nbsp;' . $templates_count; ?></td>
											<td align="right" class="smallText"><?php echo '<a href="' . tep_href_link(FILENAME_MAIL_TEMPLATES, 'action=new_template') . '">' . tep_image_button('button_new_template.gif', IMAGE_NEW_TEMPLATE) . '</a>';?></td>
										</tr>
									</table>
									</td>
								</tr>
							</table>
							</td>
							<?php
								$heading = array();
								$contents = array();
								switch ($action) {
									case 'delete_template':
										if (isset($teInfo) && is_object($teInfo)){
											$heading[] = array('text' => '<b>' . TEXT_INFO_HEADING_DELETE_TEMPLATE . '</b>');
											$contents = array('form' => tep_draw_form('template', FILENAME_MAIL_TEMPLATES, 'action=delete_template_confirm') . tep_draw_hidden_field('mail_template_id', $teInfo->mail_template_id));
											$contents[] = array('text' => TEXT_DELETE_TEMPLATE_INTRO);
											$contents[] = array('text' => '<br><b>' . $teInfo->mail_template_name . '</b>');
											$contents[] = array('align' => 'center', 'text' => '<br>' . tep_image_submit('button_delete.gif', IMAGE_DELETE_TEMPLATE) . ' <a href="' . tep_href_link(FILENAME_MAIL_TEMPLATES, 'teID=' . $teInfo->mail_template_id) . '">' . tep_image_button('button_cancel.gif', IMAGE_CANCEL) . '</a>');
										}
										break;
									case 'copy_template':
										if (isset($teInfo) && is_object($teInfo)){
											$heading[] = array('text' => '<b>' . TEXT_INFO_HEADING_COPY_TEMPLATE . '</b>');
											$contents = array('form' => tep_draw_form('template', FILENAME_MAIL_TEMPLATES, 'action=copy_template_confirm') . tep_draw_hidden_field('mail_template_id', $teInfo->mail_template_id));
											$contents[] = array('text' => TEXT_COPY_TEMPLATE_INTRO);
											$contents[] = array('text' => '<br><b>' . $teInfo->mail_template_name . '</b>');
											$contents[] = array('text' => '<br><b>' . tep_draw_input_field('copy_template_name',$teInfo->mail_template_name,'size=15') . '</b>');
											$contents[] = array('align' => 'center', 'text' => '<br>' . tep_image_submit('button_copy.gif', IMAGE_COPY_TEMPLATE) . ' <a href="' . tep_href_link(FILENAME_MAIL_TEMPLATES, 'teID=' . $teInfo->mail_template_id) . '">' . tep_image_button('button_cancel.gif', IMAGE_CANCEL) . '</a>');
										}
										break;
									default:
										if ($template_count>0){
											if (isset($teInfo) && is_object($teInfo)) { // template info box contents
												$heading[] = array('text' => '<b>' . $teInfo->mail_template_name . '</b>');
												$contents[] = array('align' => 'center', 'text' => '<a href="' . tep_href_link(FILENAME_MAIL_TEMPLATES, 'teID=' . $teInfo->mail_template_id . '&action=edit_template') . '">' . tep_image_button('button_edit.gif', IMAGE_EDIT_TEMPLATE) . '</a>&nbsp;' .
																				'<a href="' . tep_href_link(FILENAME_MAIL_TEMPLATES, 'teID=' . $teInfo->mail_template_id . '&action=delete_template') . '">' . tep_image_button('button_delete.gif', IMAGE_DELETE) . '</a><br>' 
																	. '<a href="' . tep_href_link(FILENAME_MAIL_TEMPLATES, 'teID=' . $teInfo->mail_template_id . '&action=copy_template') . '">' . tep_image_button('button_copy.gif', IMAGE_COPY_TEMPLATE) . '</a>');
												$contents[] = array('text' => '<br>' . TEXT_DATE_ADDED . ' ' . format_date($teInfo->date_added));
												if (tep_not_null($teInfo->last_modified)) $contents[] = array('text' => TEXT_LAST_MODIFIED . ' ' . format_date($teInfo->last_modified));
											}
										} else {
											  $heading[] = array('text' => '<b>' . EMPTY_TEMPLATE . '</b>');
											  $contents[] = array('text' => TEXT_NO_TEMPLATES);
										}
								}
								if ( (tep_not_null($heading)) && (tep_not_null($contents)) ) {
								  echo '            <td width="25%" valign="top">' . "\n";
							
								  $box = new box;
								  echo $box->infoBox($heading, $contents);
							
								  echo '            </td>' . "\n";
								}
							?>
				</tr>
			</table>
		<?php } // action?>		
			</td>
		</tr>
	</table>
	</td>
</table>
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
