<?php
  /**************************************************************************\
  * phpGroupWare - Translation Editor                                        *
  * http://www.phpgroupware.org                                              *
  * --------------------------------------------                             *
  *  This program is free software; you can redistribute it and/or modify it *
  *  under the terms of the GNU General Public License as published by the   *
  *  Free Software Foundation; either version 2 of the License, or (at your  *
  *  option) any later version.                                              *
  \**************************************************************************/

  /* $Id: createnewlangset.php,v 1.19.2.1 2001/06/20 17:57:49 milosch Exp $ */

	$phpgw_info['flags']['currentapp'] = 'transy';
	include('../header.inc.php');

	echo '<FORM METHOD="post" ACTION="' . $phpgw->link('/transy/createnewlangset.php',"DO_ACTION=getstrings") . '">';

	$phpgw->db->query("SELECT lang_id,lang_name FROM languages ORDER BY lang_name");
	$i = 0;
	while ($phpgw->db->next_record())
	{
		$languages[$i]['lang_id']   = $phpgw->db->f('lang_id');
		$languages[$i]['lang_name'] = $phpgw->db->f('lang_name');
		$i++;
	}
?>
  <TABLE>
   <tr>
    <td><?php echo lang('Source Language'); ?></td>
    <td>
     <select name="sourcelang">
<?php
	while ($lang = each($languages))
	{
		echo "<option value=\"" . $lang[1]['lang_id'] . "\"";
		if ($sourcelang)
		{
			if ($lang[1]['lang_id'] == $sourcelang)
			{
				echo " selected";
			}
		}
		elseif ($lang[1]['lang_id'] == "EN")
		{
			echo " selected";
		}
		echo ">" . $lang[1]['lang_name'] . "</option>";
	}
?>
     </select>
    </td>
   </tr>
   <tr>
    <td><?php echo lang('Target Language'); ?></td>
    <td>
     <select name="targetlang">
<?php
	reset($languages);

	while ($lang = each($languages))
	{
		echo "<option value=\"" . $lang[1]['lang_id'] . "\"";
		if ($targetlang)
		{
			if ($lang[1]['lang_id'] == $targetlang)
			{
				echo " selected";
			}
		}
		elseif ($lang[1]['lang_id'] == $phpgw_info['user']['preferences']['lang'])
		{
			echo " selected";
		}
		echo ">" . $lang[1]['lang_name'] . "</option>";
	}
?>
     </select>
    </td>
   </tr>
   <tr>
    <td><?php echo lang('Application'); ?></td>
    <td>
     <select name="application_name">
      <option value="all"<?php if ($application_name=='all') { echo " selected";} ?>><?php echo lang("all"); ?></option>
      <option value="common"<?php if ($application_name=='common') { echo " selected";} ?>><?php echo lang("common"); ?></option>
<?php
	$db_perms = $phpgw->acl->get_user_applications($phpgw_info["user"]["account_id"]);
	@ksort($db_perms);
	@reset($db_perms);
	while (list($userapp) = each($db_perms))
	{
		if ($phpgw_info['apps'][$userapp]['enabled'])
		{
			echo "<option value=\"" . $userapp . "\"";
			if ($application_name == $userapp)
			{
				echo " selected";
			}
			elseif ($phpgw_info['user']['preferences']['default_app'] == $userapp)
			{
				echo " selected";
			}
			echo ">" . lang($phpgw_info['apps'][$userapp]['title']) . "</option>";
		}
	}
?>
     </select>
    </td>
   </tr>
   <tr><TD><input type="Submit" name="submit" value="<?php echo lang("Submit"); ?>"></TD>
  </FORM>
  <FORM METHOD="post" ACTION="<?php echo $phpgw->link('/transy')?>">
    <TD><input type="submit" name="cancel" value="<?php echo lang("Cancel"); ?>"></TD>
    </tr>
  </TABLE>
  </FORM>

  <HR>

<?php
	if ($DO_ACTION == 'putstrings')
	{
		while ($t = each($translations))
		{
			if ($t[1])
			{
				$phpgw->db->query("DELETE FROM lang WHERE message_id='" . $t[0] . "' AND lang='$targetlang' AND app_name='$app_name'");
				$phpgw->db->query("INSERT INTO lang (message_id, app_name, lang, content) VALUES ('"
					. $t[0] . "','" . $app_name . "','" . $targetlang . "','" . $t[1] . "')");
			}
		}
	}

	echo "<TABLE>";
	echo '<FORM METHOD="post" ACTION="' . $phpgw->link("/transy/createnewlangset.php/","DO_ACTION=putstrings") . '">';
 
	$dbSource = $phpgw->db;
	$dbTarget = $phpgw->db;
	$dbcheck  = $phpgw->db;

	if ($application_name != 'all')
	{
		$appfiltermethod = "and app_name = '$application_name'";
	}

	$dbSource->query("SELECT message_id, content FROM lang WHERE lang='".$sourcelang."' "
		. $appfiltermethod);

	echo '<TR>';
	if ($application_name == 'all')
	{
		echo '<TH ALIGN="left">'.lang('application').'</TH>';
	}

	echo '<TH ALIGN="left">' . lang('Message') . '</TH><TH ALIGN="left">' . lang('Original')
		. '</TH><TH ALIGN="left">' . lang('Translation') . '</TH></TR>';

	while ($dbSource->next_record())
	{
		$mess_id        = addslashes($dbSource->f('message_id'));
		$source_content = addslashes($dbSource->f('content'));

		$sql = "SELECT available FROM languages WHERE lang_id='".$targetlang."'";
		$dbcheck->query($sql);
		$dbcheck->next_record();
		if ($dbcheck->f(0) == 'Yes')
		{
			echo "<TR>";
			if ($application_name == 'all')
			{
				$sql = "SELECT app_name,content FROM lang WHERE message_id='$mess_id' AND lang='".$targetlang."'";
			}
			else
			{
				$sql = "SELECT content FROM lang WHERE message_id='$mess_id' AND lang='".$targetlang."' ". $appfiltermethod;
			}

			$dbTarget->query($sql);

			if ($dbTarget->next_record())
			{
				if ($application_name == 'all')
				{
					echo '<TD>'.$phpgw->strip_html($dbTarget->f('app_name')).'</TD>';
				}
				echo "<TD>" . $phpgw->strip_html($mess_id) . "</TD>";
				echo "<TD>" . $phpgw->strip_html($source_content) . "</TD>";
				$content = $phpgw->strip_html($dbTarget->f('content'));
				echo '<TD><INPUT NAME="translations[' . $mess_id . ']" TYPE="text" SIZE="50" MAXLENGTH="255" '
					. 'VALUE="' . $content . '"></TD>';
			}
			else
			{
				if ($application_name == 'all')
				{
					echo '<TD>none</TD>';
				}
				echo "<TD>" . $phpgw->strip_html($mess_id) . "</TD>";
				echo "<TD>" . $phpgw->strip_html($source_content) . "</TD>";
				echo '<TD><INPUT NAME="translations[' . $mess_id . ']" TYPE="text" SIZE="50" MAXLENGTH="255"></TD>';
			}
		}
		echo "</TR>\n";
	}

	$action = "UPDATE_TRANS";
	$app_name = $application_name;
?>

   <TR VALIGN="top"><INPUT NAME="app_name" TYPE="hidden" VALUE="<?php echo $app_name ?>"></TR>
   <TR VALIGN="top"><INPUT NAME="targetlang" TYPE="hidden" VALUE="<?php echo $targetlang ?>"></TR>

    <TR VALIGN="top">
     <TD><input type="submit" name="submit" value="<?php echo lang('Submit'); ?>"></TD>
   </FORM>
   <FORM METHOD="post" ACTION="<?php echo $phpgw->link('/transy')?>">
     <TD><input type="submit" name="cancel" value="<?php echo lang('Cancel'); ?>"></TD>
   </TR>
   </FORM>
</TABLE>
<?php
	$phpgw->common->phpgw_footer();
?>
