<?php
/**
 * Common language functions
 * 
 * Provides language related functions for internationalization
 * Includes desired languages files regarding current module
 * 
 * @package linea21\utils\lib
 * @author $Author$ - linea21 <info@linea21.com>
 * @version $Id$ 
 * @access public
 * @license http://opensource.org/licenses/gpl-3.0.html
 */

/**
 * IncludeLang()
 * Fait les includes de fichiers de langue en fonction de la rubrique active ( partie publique )
 *
 * @return void (include)
 */
function IncludeLang()
{
	global $lang;

  $language = U_L;
  include_once('../languages/' . $language . '/lang_common.' . CHARSET . '.php');
  
  if(MOD_TAG == 1)
  	include_once('../languages/' . $language . '/lang_tag.' . CHARSET . '.php');

  if(MOD_COMMENT == 1)
    include_once('../languages/' . $language . '/lang_comment.' . CHARSET . '.php');

  if (ActiveItemAlias($GLOBALS['activeitem']) == 'directory') {
    include_once('../languages/' . $language . '/lang_yellowpages.' . CHARSET . '.php');
  }
  if (ActiveItemAlias($GLOBALS['activeitem']) == 'dashboard') {
    include_once('../languages/' . $language . '/lang_sdi.' . CHARSET . '.php');
    include_once('../languages/' . $language . '/lang_dashboard.' . CHARSET . '.php');
  }
  if (ActiveItemAlias($GLOBALS['activeitem']) == 'workgroup') {
    include_once('../languages/' . $language . '/lang_workshop.' . CHARSET . '.php');
    include_once('../languages/' . $language . '/lang_workshoprep.' . CHARSET . '.php');
    include_once('../languages/' . $language . '/lang_publication.' . CHARSET . '.php');
    include_once('../languages/' . $language . '/lang_system.' . CHARSET . '.php');
  }
  if (ActiveItemAlias($GLOBALS['activeitem']) == 'project') {
    include_once('../languages/' . $language . '/lang_project.' . CHARSET . '.php');
    include_once('../languages/' . $language . '/lang_dashboard.' . CHARSET . '.php');
    include_once('../languages/' . $language . '/lang_sdi.' . CHARSET . '.php');
  }
  if (ActiveItemAlias($GLOBALS['activeitem']) == 'publication') {
    include_once('../languages/' . $language . '/lang_publication.' . CHARSET . '.php');
  }
  if (ActiveItemAlias($GLOBALS['activeitem']) == 'user-prefs') {
    include_once('../languages/' . $language . '/lang_user.' . CHARSET . '.php');
    include_once('../languages/' . $language . '/lang_workshop.' . CHARSET . '.php');
  }
  if (ActiveItemAlias($GLOBALS['activeitem']) == 'news') {
    include_once('../languages/' . $language . '/lang_news.' . CHARSET . '.php');
  }
  if (ActiveItemAlias($GLOBALS['activeitem']) == 'page') {
  	include_once('../languages/' . $language . '/lang_page.' . CHARSET . '.php');
  }
  if (ActiveItemAlias($GLOBALS['activeitem']) == 'subscribe-newsletter') {
    include_once('../languages/' . $language . '/lang_newsletter.' . CHARSET . '.php');
  }
  if (ActiveItemAlias($GLOBALS['activeitem']) == 'search') {
    include_once('../languages/' . $language . '/lang_search.' . CHARSET . '.php');
  }
  if (ActiveItemAlias($GLOBALS['activeitem']) == 'workgroup-subscription') {
    include_once('../languages/' . $language . '/lang_workshop.' . CHARSET . '.php');
    include_once('../languages/' . $language . '/lang_user.' . CHARSET . '.php');
  }

  IncludeUserfiles();
}

/**
 * IncludeAdminLang()
 * Fait les includes de fichiers de langue en fonction de la rubrique active ( partie admin )
 *
 * @return void (include)
 */
function IncludeAdminLang($rubrique)
{
	global $lang;

  $language = U_L;
  include_once('../languages/' . $language . '/lang_common.' . CHARSET . '.php');
  include_once('../languages/' . $language . '/lang_' . $rubrique . '.' . CHARSET . '.php');

  if ($rubrique === 'dashboard') {
    include_once('../languages/' . $language . '/lang_sdi.' . CHARSET . '.php');
  }
  if ($rubrique === 'sdi') {
    include_once('../languages/' . $language . '/lang_dashboard.' . CHARSET . '.php');
  }
  if($rubrique === 'project')  {
  	include_once('../languages/' . $language . '/lang_dashboard.' . CHARSET . '.php');
  	include_once('../languages/' . $language . '/lang_sdi.' . CHARSET . '.php');
  }
  if($rubrique === 'user') {
  	include_once('../languages/' . $language . '/lang_workshop.' . CHARSET . '.php');
  }
  if($rubrique === 'home' && MOD_COMMENT == 1) {
    include_once('../languages/' . $language . '/lang_comment.' . CHARSET . '.php');
  }

  IncludeUserfiles();

}

/**
 * _t()
 * Translate item
 * @param string $scope
 * @param string $item
 * @return string
 **/
function _t($scope, $item, $subvalue = null)
{
	$a = array('translation');
	// only two parameters passed
	if($subvalue === null) {
		
	  if(isset($GLOBALS['lang'][$scope][$item])) {
	    return $GLOBALS['lang'][$scope][$item];
	  } else {
	  	array_push($a, '$lang[\''.$scope.'\'][\''.$item.'\'] does not exist in '. U_L);
	  	logfile(LOG_MAINFILE, $a);
	    if(MOD_DEBUG == 1) return '<span class="debug_alert">_t(\''.$scope."', '".$item.'\') does not exist</span>';
	  }
	  
	  return $scope." ".$item;
	 
	  // Three parameters passed
	} else {
		
		if(isset($GLOBALS['lang'][$scope][$item][$subvalue])) {
			return $GLOBALS['lang'][$scope][$item][$subvalue];
		} else {
			array_push($a, '$lang[\''.$scope.'\'][\''.$item.'\'][\''.$subvalue.'\'] does not exist in '. U_L);
			logfile(LOG_MAINFILE, $a);
			if(MOD_DEBUG == 1) return '<span class="debug_alert">_t(\''.$scope."', '".$item.'\', \''.$subvalue.'\') does not exist</span>';
		}
		
		return $scope . " " . $item . " " . $subvalue;
		
	}
  
  
}

/**
 * _def()
 * Define a user language variable
 * override previous value if already defined
 * @param string $scope
 * @param string $item
 * @param string $str
 * @return string
 **/
function _def($scope, $item, $str)
{
  if(isset($GLOBALS['lang'][$scope][$item])) unset($GLOBALS['lang'][$scope][$item]);

  $GLOBALS['lang'][$scope][$item] = $str;
}

/**
 * IncludeUserfiles()
 * Include user files prefixed by '_' from languages folders
 *
 * @return void (include)
 */
function IncludeUserfiles() {
  $language = U_L;
  $paths = glob('../languages/' . $language . '/_*.php');
  if(CURRENT_APP == 'admin') {
    $pattern = THEME_ADMIN_PATH.'override/languages/' . $language . '/_*.php';
  } else {
    $pattern = THEME_PUBLIC_PATH.'override/languages/' . $language . '/_*.php';
  }
  $pattern2 = OVERRIDE_FOLDER.'languages/' . $language . '/_*.php';

  $paths = array_merge($paths, glob($pattern), glob($pattern2));
  if($paths != false) {
    foreach ($paths as $filename) {
      _debug('Including user defined language files : '. $filename);
      include_once($filename);
    }
  }
}

/**
 * IncludeLanguagesPluginfiles()
 * Include plugin files from specific plugin languages folders
 * @param object $plugin
 * @return void (include)
 */
function IncludeLanguagesPluginfiles($plugin) {

  // we try to include the current culture
  $paths = glob('../plugins/'.$plugin->getVar('name').'/languages/' . U_L . '/*.php');
  // if current culture not found, we include default language files
  if(count($paths)==0) $paths = glob('../plugins/'.$plugin->getVar('name').'/languages/' . $plugin->getVar('default_language') . '/*.php');

  if($paths != false) {
    foreach ($paths as $filename) {
      _debug('Including <em>'.$plugin.'</em> plugin language files : '. $filename);
      include_once($filename);
    }
  }

  return true;
}
?>