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

	// current file selected to edit
	$lang_file=(isset($HTTP_GET_VARS["select_file"])?$HTTP_GET_VARS["select_file"]:'sample.php');
	// current language selected
	$select_lang=(isset($HTTP_GET_VARS["select_lang"])?$HTTP_GET_VARS["select_lang"]:'english');
	// check for translation flag
	$translate=(isset($HTTP_GET_VARS["translate"])?$HTTP_GET_VARS["translate"]:'N');
	// current action to be done
	$get_action=(isset($HTTP_GET_VARS["get_action"])?$HTTP_GET_VARS["get_action"]:'');
	$post_action=(isset($HTTP_POST_VARS["post_action"])?$HTTP_POST_VARS["post_action"]:'');

	// get all the installed languages
	$languages = tep_get_languages();
	
	// update the changes by post_action
	if ($post_action=="save"){
		$data_lines=$HTTP_POST_VARS["data_lines"];
		$language_content=$HTTP_POST_VARS["language_content"];
		
		for ($icnt=0;$icnt<count($data_lines);$icnt++){
			// is the normal data or constant data to be written
			if (is_array($data_lines[$icnt])){
				$data_lines[$icnt]["key"]=addslashes(stripslashes($data_lines[$icnt]["key"]));
				reset($language_content);
				while(list($key,)=each($language_content)){				
					$value=addslashes(stripslashes($data_lines[$icnt][$key]));
					$language_content[$key].="define(\"" . $data_lines[$icnt]["key"] . "\",\"" . $value . "\");";
				}
			} else {
				reset($language_content);
				while(list($key,)=each($language_content)){
					//$write_data=addslashes(stripslashes($data_lines[$icnt]));
					$language_content[$key].=addslashes(stripslashes($data_lines[$icnt]));
				}
			}
		}
		// replacement for return key 
		$error=false;
		reset($language_content);
		while(list($key,$write_data)=each($language_content)){
			$write_data=str_replace("<e>",chr(13) . chr(10),$write_data);
			if (substr($lang_file,0,strpos($lang_file,"."))==$select_lang){
				$update_file=DIR_FS_CATALOG_LANGUAGES . "1_" . $key . ".php";
			} else {
				$update_file=DIR_FS_CATALOG_LANGUAGES . $key . "/1_" . $lang_file;
			}
			$handle=@fopen($update_file,"w");
			// error opening file for update
			if (!$handle){
				$messageStack->add(sprintf(ERROR_UPDATING_FILE, $lang_file), 'error');
				$error=true;
			} else {
				fwrite($handle,$write_data,strlen($write_data));
				fclose($handle);
			}
		}
		
		if (!error) $messageStack->add(sprintf(SUCCESS_FILE_UPDATED, $file), 'success');
		tep_redirect(tep_href_link(FILENAME_LANGUAGE_TOOL,"sel_lang=" . $sel_lang));
		return;
	} 

	// get action 
	if ($get_action=="define"){
		// default action
		if (substr($lang_file,0,strpos($lang_file,"."))==$select_lang){
			$edit_file=DIR_FS_CATALOG_LANGUAGES . $lang_file;
		} else {
			$edit_file=DIR_FS_CATALOG_LANGUAGES . $language . "/" . $lang_file;
		}
		$handle=@fopen($edit_file,"r");
		if (!$handle){
			$messageStack->add(sprintf(ERROR_OPENING_FILE, $lang_file), 'error');
			tep_redirect(tep_href_link(FILENAME_LANGUAGE_TOOL,"sel_lang=" . $sel_lang));
		}
		$content=fread($handle,filesize($edit_file));
		$content=str_replace(chr(13) . chr(10),"<e>",$content);
		
		$offset=0;
	}
	
	// create the languages to get as input
	$input_languages=array($select_lang=>"");
	
	// get the current languages
	$languages_array = array();
	for ($i=0, $n=sizeof($languages); $i<$n; $i++) {
		$languages_array[] = array('id' => $languages[$i]['directory'],
								   'text' => $languages[$i]['name']);
		if ($translate=='Y' || $select_lang==$languages[$i]['directory']){
			$input_languages[$languages[$i]['directory']]=$languages[$i]['name'];
		}
	}
?>
<!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>
<script language="javascript">
	function edit_file(file_name){
		frm=document.choose_lang;
		translate='N';
		if (frm.translate.checked) translate='Y';
		location.href="<?php echo tep_href_link("language_tool.php","select_lang=" . $select_lang . "&select_file=");?>"+file_name+"&get_action=define&translate="+translate;
	}
	function select_language(){
		document.choose_lang.submit();
	}
	function check_translate(){
		frm=document.choose_lang;
		translate='N';
		if (frm.translate.checked) translate='Y';
		file_name="<?php echo $lang_file;?>";
		<?php if ($get_action=="define") { ?>		
			location.href="<?php echo tep_href_link("language_tool.php","select_lang=" . $select_lang . "&select_file=");?>"+file_name+"&get_action=define&translate="+translate;
		<?php } ?>
	}
</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" cellpadding="2" cellspacing="0" width="100%">
			<tr>
				<td>
				<table border="0" cellpadding="0" cellspacing="0" width="100%">
					<td class="pageHeading" width="40%"><?php echo HEADING_TITLE;?></td>
					<td align="right" class="main">
						<?php 
								echo tep_draw_form("choose_lang",FILENAME_LANGUAGE_TOOL,"","get");
								echo tep_draw_hidden_field("get_action",$get_action);
								if ($select_file!="") echo tep_draw_hidden_field("select_file",$select_file);
								echo TEXT_CHOOSE_LANGUAGE . '&nbsp;&nbsp;' . tep_draw_pull_down_menu("select_lang",$languages_array,$select_lang,'onChange="javascript:select_language();"') .
								   '&nbsp;&nbsp;' . tep_draw_checkbox_field("translate","Y",($translate=='Y'?true:false),'','onClick="javascript:check_translate();"') . TEXT_CREATE_TRANSLATION;
								echo '</form>';
						?>
					</td>
				</table>
			</tr>
			<tr>
				<td><?php echo tep_draw_separator('pixel_trans.gif','1','10');?></td>
			</tr>
			<?php if ($get_action=="define") {  // editing language file
					$width=(75/count($input_languages));
			?>
					<tr>
						<td>
						<table border="0" cellpadding="1" cellspacing="0" width="100%">
							<tr>
								<td height="15" valign="middle" class="smallText"><b><?php echo $input_languages[$select_lang] . " : " . $lang_file;?></b></td>
							</tr>
							<tr>
								<td><?php echo tep_draw_separator('pixel_trans.gif','1','10');?></td>
							</tr>
							<?php
									echo tep_draw_form("save_content",'language_tool.php','select_lang=' . $select_lang . '&select_file=' . $lang_file);
									echo tep_draw_hidden_field("post_action","save");
							?>
							<tr>
								<td>
								<table border="0" cellpadding="1" cellspacing="0" width="100%">
								<tr class="dataTableHeadingRow">
									<td width=25% class="dataTableHeadingContent"><?php echo TEXT_LANGUAGE_KEY;?></td>
									<?php 
										reset($input_languages);
										while(list($key,$value)=each($input_languages)) {  // print titles for input languages
											echo tep_draw_hidden_field("language_content[$key]","");
									?>
										<td width="<?php echo $width; ?>" class="dataTableHeadingContent"><?php echo sprintf(TEXT_LANGUAGE_VALUE,$value);?></td>
									<?php } ?>
								</tr>
								<?php
									$old_offset=0;
									$icnt=0;
									while(true){
										$match=preg_match("/define\((.*?)\);/",$content,$matches,PREG_OFFSET_CAPTURE,$offset);
										if ($match<=0) break;
										$splt_line=split(",",$matches[1][0]);
										$key=substr(trim($splt_line[0]),1,-1);
										//$value=str_replace("<e>",chr(13) . chr(10),substr(trim($splt_line[1]),1,-1));
										$lang_value=substr(trim($splt_line[1]),1,-1);
										$middle_content=substr($content,$old_offset,($matches[0][1]-$old_offset));
										
										echo tep_draw_hidden_field("data_lines[$icnt]",$middle_content);
										//echo '<tr><td colspan=2><pre>' . $middle_content . '</pre></td></tr>';
										$icnt++;
									?>
										<tr>
											<td class="main" valign="top" width="25%">
												<?php 
													echo tep_draw_hidden_field("data_lines[$icnt][key]",$key);
													echo $key;
												?>
											</td>
											<?php 
												reset($input_languages);
												while(list($key,$value)=each($input_languages)) {  // print titles for input languages
											?>
												<td class="main" valign="top">
													<?php 
														echo tep_draw_textarea_field("data_lines[$icnt][$key]","off",20,5,$lang_value);
													?>
												</td>
											<?php } // while language?>
										</tr>
									<?php
									
										$offset=$matches[0][1]+strlen($matches[0][0]);
										$old_offset=$offset;
										$icnt++;
									}
									$middle_content=substr($content,$offset);
									
									echo tep_draw_hidden_field("data_lines[$icnt]",$middle_content);
									//echo '<tr><td colspan=2><pre>' . $middle_content . '</pre></td></tr>';
								?>
									<tr>
										<td><?php echo tep_draw_separator('pixel_trans.gif','1','10');?></td>
									</tr>
									<tr>
										<td>
											<?php 
												echo tep_image_submit("button_save.gif",IMAGE_SUBMIT) . '&nbsp;';
												echo '<a href="' . tep_href_link(FILENAME_LANGUAGE_TOOL,'select_lang=' . $select_lang . '&translate=' . $translate) . '">' . tep_image_button("button_back.gif",IMAGE_BACK) . '</a>';
											?>
										</td>
									</tr>
									</form>
									</table>
									</td>
									</tr>
								</table>
							</td>
						</tr>
				<?php } else { // default action show all selected language files ?>
					<tr>
						<td>
						<table border="0" cellpadding="0" cellspacing="0" width="100%">
							<tr class="dataTableHeadingRow" height="15" valign="middle">
								<td class="dataTableHeadingContent"><?php echo TEXT_LANGUAGE_FILES;?></td>
							</tr>
							<tr>
								<td><?php echo tep_draw_separator('pixel_trans.gif','1','10');?></td>
							</tr>
							<tr>
								<td>
									<table border="0" cellpadding="0" cellspacing="0" width="100%">
										<tr height="10">
											<td class="smallText"><?php echo '<a href="javascript:edit_file(\'' .$select_lang . '.php\');">' . '<b>' . $select_lang . '.php' . '</b></a>';?></td>
									<?php  // load files from directory
											$col_count=1;
											$lang_dir=DIR_FS_CATALOG_LANGUAGES . $select_lang;
											$file_extension = substr($PHP_SELF, strrpos($PHP_SELF, '.'));
											if ($dir=dir($lang_dir)){
												while($file=$dir->read()){
													if ($col_count%3==0) echo '<tr>';
													if (substr($file,strpos($file,"."))==$file_extension){
														echo '<td class="smallText"><a href="javascript:edit_file(\'' . $file . '\');">' . $file . '</a></td>';
														$col_count++;
													}
												}
											}
									?>
									</table>
								</td>
							</tr>
						</table>
						<td>
					</tr>
				<?php } // check action ?>
		</table>
	</td>
</table>
<td>
</tr>
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>