<?php
/**
 * @version $Id: bfText.php 139 2010-01-03 20:45:41Z  $
 * @package Blue Flame Framework (bfFramework)
 * @copyright Copyright (C) 2003,2004,2005,2006,2007,2008,2009 Blue Flame IT Ltd. All rights reserved.
 * @license GNU General Public License
 * @link http://www.blueflameit.ltd.uk
 * @author Phil Taylor / Blue Flame IT Ltd.
 *
 * bfFramework 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 3 of the License, or
 * (at your option) any later version.
 *
 * bfFramework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this package.  If not, see http://www.gnu.org/licenses/
 */
defined ( '_JEXEC' ) or die ( 'Restricted access' );

final class bfText {
	
	private $debug = false;
	private $translation = array ();
	
	/**
	 * PHP5 constructor
	 */
	public function __construct() {
		global $mainframe;
		// Read in the BF framework and component language files
		// Read in the target language files if not English
		// Populate the translation hash
		

		// Work out the site language
		if (_BF_PLATFORM == 'JOOMLA1.0') {
			// The 1.0 Way
			global $mosConfig_lang;
			$language = $mosConfig_lang;
			//			$language = 'tester';
		} else {
			// The 1.5 Way
			$user = & bfUser::getInstance ();
			$lang = $user->get ( 'language' );
			
			// Make sure that the user's language exists
			if ($lang && JLanguage::exists ( $lang )) {
				$language = $lang;
			} else {
				$params = JComponentHelper::getParams ( 'com_languages' );
				$client = & JApplicationHelper::getClientInfo ( $mainframe->getClientId () );
				$language = $params->get ( $client->name, 'en-GB' );
			}
		}
		
		$this->parse_language_file ( bfCompat::getAbsolutePath () . DS . 'components' . DS . $mainframe->get ( 'component' ) . DS . 'language' . DS . 'english.lang' );
		$this->parse_language_file ( _BF_FRAMEWORK_LANGUAGE_DIR . DS . 'english' . '.lang' );
		if ($language != 'english') {
			$this->parse_language_file ( _BF_FRAMEWORK_LANGUAGE_DIR . DS . $language . '.lang' );
			$this->parse_language_file ( bfCompat::getAbsolutePath () . DS . 'components' . DS . $mainframe->get ( 'component' ) . DS . 'language' . DS . $language . '.lang' );
		}
	
	}
	
	/*
	* Parse the language file - fail silently if it does not exist
	*/
	private function parse_language_file($fname) {
		//		echo "Parsing $fname <br />";
		bfLoad ( 'bfCache' );
		$cache = & bfCache::getInstance ();
		
		$key = 'framework.language.' . $fname;
		
		$cached = $cache->get ( md5 ( $key ), 'language' );
		if ($cached) {
			$this->translation = $cached;
		} else {
			$fp = @fopen ( $fname, 'r' );
			if (! $fp)
				return;
			while ( $line = fgets ( $fp ) ) {
				list ( $from, $to ) = preg_split ( '/=/', $line, 2 );
				$this->translation [$from] = $to;
			}
			fclose ( $fp );
		}
		$cache->api_add ( md5 ( $key ), $this->translation, 'language' );
		$cache->save ();
	
	}
	
	/**
	 * I implement the 'singleton' design pattern.
	 */
	static public function &getInstance() {
		static $instance;
		if (! isset ( $instance )) {
			$c = __CLASS__;
			$instance = new $c ( );
		}
		return $instance;
	}
	
	static public function _($str) {
		$instance = & bfText::getInstance ();
		
		$translation = & $instance->translation;
		if (is_object ( $str ) || is_array ( $str ) || strlen ( trim ( $str ) ) < 1) {
			return;
		}
		$str2 = strtoupper ( $str );
		if (@$translation [$str2]) {
			if ($instance->debug) {
				return "&bull; " . trim ( $translation [$str2] ) . " &bull;";
			} else {
				return trim ( $translation [$str2] );
			}
		}
		
		// return $str;
		if ($instance->debug) {
			bfText::_logUnknown ( $str );
			return "? " . trim ( $str ) . " ?";
		} else {
			return trim ( $str );
		}
	}
	
	/*
	* Set up debugging - not in constructor as registry isn't ready yet
	*/
	public function setDebug($component) {
		$instance = & bfText::getInstance ( $component );
		$registry = & bfRegistry::getInstance ( $component, $component );
		$instance->debug = $registry->getValue ( 'config.langDebug' );
	}
	
	public function _logUnknown($str) {
		$mainframe = JFactory::getApplication ();
		if (preg_match ( '/&BULL;/', strtoupper ( $str ) )) {
			return;
		}
		
		if (preg_match ( '/\?/', strtoupper ( $str ) )) {
			return;
		}
		
		$data = null;
		//		$unknownsFile = _BF_FRAMEWORK_LANGUAGE_DIR . DS . 'unknowns.lang';
		$unknownsFile = JPATH_CONFIGURATION . DS . 'components/' . $mainframe->get ( 'component' ) . '/language/english.lang';
		if (file_exists ( $unknownsFile )) {
			
			$fp = fopen ( $unknownsFile, 'r' );
			if (! $fp) {
				die ( 'cannot read to unknowns' );
				return;
			}
			
			while ( $line = fgets ( $fp ) ) {
				$data .= $line;
			}
			fclose ( $fp );
		
		}
		
		$data = trim ( $data );
		
		if (! @preg_match ( '/'.strtoupper ( $str ).'/i', $data )) {
			$data .= "\n" . strtoupper ( $str ) . '=' . $str;
			if ($fp = fopen ( $unknownsFile, "w" )) {
				if (! $fp) {
					die ( 'cannot write to unknowns' );
					return;
				}
				fputs ( $fp, $data, strlen ( $data ) );
				fclose ( $fp );
			}
		}
	
	}
}