<?php
/**
 * Implementation of lang switcher helper
 *
 * @package koubel homepage
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */

/**
 * Lang switcher helper
 *
 * Prints list of available translation with the highlighted actual translation.
 *  
 * @package koubel homepage
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */
class HomeWebApp_View_Helper_LangSwitcher
{
    /**
     * Main function.
     * @return string xhtml content for the language switcher with the 
     * highlighted actual translation and links to alternatives.  
     */ 
    public function langSwitcher()
    {
        //translate and locale are in a registry, alternatively you can get these
        //from bootstrap
        $translate = Zend_Registry::get('Zend_Translate');
        $locale = Zend_Registry::get('Zend_Locale');
        
        $currentLang = $locale->getLanguage();
        //be aware that original pathinfo was modified in the front controller plugin
        $currentPathInfo = Zend_Controller_Front::getInstance()->getRequest()->getPathInfo();

        $xhtml="";

        foreach($translate->getList() as $langStr) {
            if (!empty($xhtml)) $xhtml.=" | ";

            $longLangStr = $locale->getLanguageTranslation($langStr,$langStr);

            if ($langStr == $currentLang) {
                $xhtml.="<span style=\"font-weight:bold\">$longLangStr</span>";
            } else {
                $newPathInfo = ($langStr=="cs") ? $currentPathInfo : "/$langStr".$currentPathInfo;
                $newUri = AppBaseUrl::getBaseUrl().$newPathInfo;
                if (strpos($newUri,"?") !== false ) {
                    $newUri = preg_replace("/\?.*/","",$newUri);
                }  
                $xhtml .= "<a href=\"$newUri\">$longLangStr</a>";
            }

        }//end foreach

        return $xhtml;
    }
}
