<?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: class.bolangfile.inc.php,v 1.8.2.3 2003/04/07 14:50:48 ralfbecker Exp $ */

	class bolangfile
	{
		var $total;
		var $debug = False;
		var $so;
		var $loaded_apps = array();
		var $source_langarray = '';
		var $missing_langarray = '';
		var $target_langarray = '';
		var $common_langarray = '';
		var $src_file;
		var $src_apps = '';
		var $tgt_file;
		var $tgt_lang;

		function bolangfile()
		{
			$this->so = CreateObject('developer_tools.solangfile');
			settype($this->source_langarray,'string');
			settype($this->target_langarray,'string');
			settype($this->missing_langarray,'string');
		}

		/* Sessions used to save state and not reread the langfile between adding/deleting phrases */
		function save_sessiondata($source,$target)
		{
			if($this->debug) { echo '<br>Save:'; _debug_array($source); }
			$GLOBALS['phpgw']->session->appsession('developer_source_lang','developer_tools',$source);
			if($this->debug) { echo '<br>Save:'; _debug_array($target); }
			$GLOBALS['phpgw']->session->appsession('developer_target_lang','developer_tools',$target);
			$GLOBALS['phpgw']->session->appsession('developer_source_file','developer_tools',$this->src_file);
			$GLOBALS['phpgw']->session->appsession('developer_target_file','developer_tools',$this->tgt_file);
			$GLOBALS['phpgw']->session->appsession('developer_t_lang','developer_tools',$this->tgt_lang);
			$GLOBALS['phpgw']->session->appsession('developer_loaded_apps','developer_tools',$this->loaded_apps);
			$GLOBALS['phpgw']->session->appsession('developer_src_apps','developer_tools',$this->src_apps);
		}

		function read_sessiondata()
		{
			$source = $GLOBALS['phpgw']->session->appsession('developer_source_lang','developer_tools');
			if($this->debug) { echo '<br>Read:'; _debug_array($source); }

			$target = $GLOBALS['phpgw']->session->appsession('developer_target_lang','developer_tools');
			if($this->debug) { echo '<br>Read:'; _debug_array($target); }

			$src_file = $GLOBALS['phpgw']->session->appsession('developer_source_file','developer_tools');
			$tgt_file = $GLOBALS['phpgw']->session->appsession('developer_target_file','developer_tools');
			$tgt_lang = $GLOBALS['phpgw']->session->appsession('developer_t_lang','developer_tools');
			$loaded_apps = $GLOBALS['phpgw']->session->appsession('developer_loaded_apps','developer_tools');
			$src_apps = $GLOBALS['phpgw']->session->appsession('developer_src_apps','developer_tools');

			$this->set_sessiondata($source,$target,$src_file,$tgt_file,$tgt_lang,$loaded_apps,$src_apps);
		}

		function set_sessiondata($source,$target,$src_file,$tgt_file,$tgt_lang,$loaded_apps,$src_apps)
		{
			$this->source_langarray = $source;
			$this->target_langarray = $target;
			$this->src_file = $src_file;
			$this->tgt_file = $tgt_file;
			$this->tgt_lang = $tgt_lang;
			$this->loaded_apps = $loaded_apps;
			$this->src_apps = $src_apps;
		}

		function clear_sessiondata()
		{
			$GLOBALS['phpgw']->session->appsession('developer_source_lang','developer_tools','');
			$GLOBALS['phpgw']->session->appsession('developer_target_lang','developer_tools','');
			$GLOBALS['phpgw']->session->appsession('developer_source_file','developer_tools','');
			$GLOBALS['phpgw']->session->appsession('developer_target_file','developer_tools','');
			$GLOBALS['phpgw']->session->appsession('developer_t_lang','developer_tools','');
			$GLOBALS['phpgw']->session->appsession('developer_loaded_apps','developer_tools','');
		}

		function list_langs()
		{
			return $this->so->list_langs();
		}

		function addphrase($entry)
		{
			/* _debug_array($this->source_langarray);exit; */
			$mess_id = strtolower(trim($entry['message_id']));
			$this->source_langarray[$mess_id] = array(
				'message_id' => $mess_id,
				'content'    => $entry['content'],
				'app_name'   => $entry['app_name'],
				'lang'       => 'en'
			);
			@ksort($this->source_langarray);
		}

		function movephrase($mess='')
		{
			if ($mess !='' && ($this->missing_langarray[$mess]['message_id']))
			{
				$this->source_langarray[$mess] = $m = array(
					'message_id' => $this->missing_langarray[$mess]['message_id'],
					'content'    => $this->missing_langarray[$mess]['content'],
					'app_name'   => $this->missing_langarray[$mess]['app_name'],
					'lang'       => 'en'
				);
				@ksort($this->source_langarray);
				reset($this->source_langarray);
				
				if ($this->tgt_lang == 'en')
				{
					$this->target_langarray[$mess] = $m;

					@ksort($this->target_langarray);
					reset($this->target_langarray);
				}
			}
			//else echo "'$mess' not found in missing_langarray !!!<br>\n";
		}

		function missing_app($app,$userlang='en')
		{
			if (!is_array($this->common_langarray))
			{
				$this->common_langarray = $this->so->add_app('phpgwapi',$userlang);
			}
			$plist = $this->so->missing_app($app,$userlang);
			foreach($plist as $p => $loc)
			{
				$_mess_id = strtolower(trim($p));
				if (!empty($_mess_id) && !$this->source_langarray[$_mess_id] && !$this->common_langarray[$_mess_id])
				{
					$this->missing_langarray[$_mess_id]['message_id'] = $_mess_id;
					$this->missing_langarray[$_mess_id]['app_name']   = trim($app);
					$this->missing_langarray[$_mess_id]['content']    = $p;
				}
			}
			if (is_array($this->missing_langarray))
			{
				reset ($this->missing_langarray);
				@ksort($this->missing_langarray);			
			}
			return $this->missing_langarray;
		}

		function add_app($app,$userlang='en')
		{
			if(gettype($this->source_langarray) == 'array')
			{
				return $this->source_langarray;
			}
			$this->source_langarray = $this->so->add_app($app,$userlang);
			$this->src_file = $this->so->src_file;
			$this->loaded_apps = $this->so->loaded_apps;
			$this->src_apps = $this->so->src_apps;
			return $this->source_langarray;
		}

		function load_app($app,$userlang='en')
		{
			if(gettype($this->target_langarray) == 'array')
			{
				if ($this->tgt_lang == $userlang)
				{
					return $this->target_langarray;
				}
			}
			$this->target_langarray = $this->so->load_app($app,$userlang);
			$this->tgt_file = $this->so->tgt_file;
			$this->tgt_lang = $userlang;
			$this->loaded_apps = $this->so->loaded_apps;
			return $this->target_langarray;
		}

		function write_file($which,$app_name,$userlang)
		{
			switch ($which)
			{
				case 'source':
					$langarray = $this->source_langarray;
					break;
				case 'target':
					$langarray = $this->target_langarray;
					break;
				default:
					break;
			}
			$this->so->write_file($app_name,$langarray,$userlang);
			return;
		}

		function loaddb($app_name,$userlang)
		{
			return $this->so->loaddb($app_name,$userlang);
		}
	}
?>
