PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/kernel/private/classes/ezplanguageswitcher.php

https://github.com/eeggenberger/ezpublish
PHP | 248 lines | 130 code | 25 blank | 93 comment | 13 complexity | dfcf9688d164941e03f2c78dbb22af0e MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the ezpLanguageSwitcher class
  4. *
  5. * @copyright Copyright (C) 1999-2011 eZ Systems AS. All rights reserved.
  6. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  7. * @version //autogentag//
  8. * @package kernel
  9. */
  10. /**
  11. * Utility class for transforming URLs between siteaccesses.
  12. *
  13. * This class will generate URLs for various siteaccess, and translate
  14. * URL-aliases into other languages as necessary.
  15. */
  16. class ezpLanguageSwitcher implements ezpLanguageSwitcherCapable
  17. {
  18. protected $origUrl;
  19. protected $userParamString;
  20. protected $destinationSiteAccess;
  21. protected $destinationLocale;
  22. protected $baseDestinationUrl;
  23. protected $destinationSiteAccessIni;
  24. function __construct( $params = null )
  25. {
  26. if ( $params === null )
  27. {
  28. return $this;
  29. }
  30. // Removing the first part, which is the SA param.
  31. array_shift( $params['Parameters'] );
  32. $this->origUrl = join( $params['Parameters'] , '/' );
  33. $this->userParamString = '';
  34. $userParams = $params['UserParameters'];
  35. foreach ( $userParams as $key => $value )
  36. {
  37. $this->userParamString .= "/($key)/$value";
  38. }
  39. }
  40. /**
  41. * Get instance siteaccess specific site.ini
  42. *
  43. * @param string $sa
  44. * @return void
  45. */
  46. protected function getSiteAccessIni()
  47. {
  48. if ( $this->destinationSiteAccessIni === null )
  49. {
  50. $this->destinationSiteAccessIni = eZSiteAccess::getIni( $this->destinationSiteAccess, 'site.ini' );
  51. }
  52. return $this->destinationSiteAccessIni;
  53. }
  54. /**
  55. * Checks if the given $url points to a module.
  56. *
  57. * We use this method to check whether we should pass on the original URL
  58. * to the destination translation siteaccess.
  59. *
  60. * @param string $url
  61. * @return bool
  62. */
  63. protected function isUrlPointingToModule( $url )
  64. {
  65. // Grab the first URL element, representing the possible module name
  66. $urlElements = explode( '/', $url );
  67. $moduleName = $urlElements[0];
  68. // Look up for a match in the module list
  69. $moduleIni = eZINI::instance( 'module.ini' );
  70. $availableModules = $moduleIni->variable( 'ModuleSettings', 'ModuleList' );
  71. return in_array( $moduleName, $availableModules, true );
  72. }
  73. /**
  74. * Checks if the current content object locale is available in destination siteaccess.
  75. *
  76. * This is used to check whether we should pass on the original URL to the
  77. * destination translation siteaccess, when no translation of an object
  78. * exists in the destination locale.
  79. *
  80. * If the current content object locale exists as a fallback in the
  81. * destination siteaccess, the original URL should be available there as
  82. * well.
  83. *
  84. * @return bool
  85. */
  86. protected function isLocaleAvailableAsFallback()
  87. {
  88. $currentContentObjectLocale = eZINI::instance()->variable( 'RegionalSettings', 'ContentObjectLocale' );
  89. $saIni = $this->getSiteAccessIni();
  90. $siteLanguageList = $saIni->variable( 'RegionalSettings', 'SiteLanguageList' );
  91. return in_array( $currentContentObjectLocale, $siteLanguageList, true );
  92. }
  93. /**
  94. * Returns URL alias for the specified <var>$locale</var>
  95. *
  96. * @param string $url
  97. * @param string $locale
  98. * @return void
  99. */
  100. public function destinationUrl()
  101. {
  102. $nodeId = $this->origUrl;
  103. if ( !is_numeric( $this->origUrl ) )
  104. {
  105. $nodeId = eZURLAliasML::fetchNodeIDByPath( $this->origUrl );
  106. }
  107. $destinationElement = eZURLAliasML::fetchByAction( 'eznode', $nodeId, $this->destinationLocale, false );
  108. if ( empty( $destinationElement ) || ( !isset( $destinationElement[0] ) && !( $destinationElement[0] instanceof eZURLAliasML ) ) )
  109. {
  110. // If the return of fetchByAction is empty, it can mean a couple
  111. // of different things:
  112. // Either we are looking at a module, and we should pass the
  113. // original URL on
  114. //
  115. // Or we are looking at URL which does not exist in the
  116. // destination siteaccess, for instance an untranslated object. In
  117. // which case we will point to the root of the site, unless it is
  118. // available as a fallback.
  119. if ( $this->isUrlPointingToModule( $this->origUrl ) ||
  120. $this->isLocaleAvailableAsFallback() )
  121. {
  122. // We have a module, we're keeping the orignal url.
  123. $urlAlias = $this->origUrl;
  124. }
  125. else
  126. {
  127. // We probably have an untranslated object, which is not
  128. // available with SiteLanguageList setting, we direct to root.
  129. $urlAlias = '';
  130. }
  131. }
  132. else
  133. {
  134. // Translated object found, forwarding to new URL.
  135. $saIni = $this->getSiteAccessIni();
  136. $siteLanguageList = $saIni->variable( 'RegionalSettings', 'SiteLanguageList' );
  137. $urlAlias = $destinationElement[0]->getPath( $this->destinationLocale, $siteLanguageList );
  138. $urlAlias .= $this->userParamString;
  139. }
  140. $this->baseDestinationUrl = rtrim( $this->baseDestinationUrl, '/' );
  141. if ( $GLOBALS['eZCurrentAccess']['type'] === eZSiteAccess::TYPE_URI )
  142. {
  143. $finalUrl = $this->baseDestinationUrl . '/' . $this->destinationSiteAccess . '/' . $urlAlias;
  144. }
  145. else
  146. {
  147. $finalUrl = $this->baseDestinationUrl . '/' . $urlAlias;
  148. }
  149. return $finalUrl;
  150. }
  151. /**
  152. * Sets the siteaccess name, $saName, we want to redirect to.
  153. *
  154. * @param string $saName
  155. * @return void
  156. */
  157. public function setDestinationSiteAccess( $saName )
  158. {
  159. $this->destinationSiteAccess = $saName;
  160. }
  161. /**
  162. * This is a hook which is called by the language switcher module on
  163. * implementation classes.
  164. *
  165. * In this implementation it is doing initialisation as an example.
  166. *
  167. * @return void
  168. */
  169. public function process()
  170. {
  171. $saIni = $this->getSiteAccessIni();
  172. $this->destinationLocale = $saIni->variable( 'RegionalSettings', 'ContentObjectLocale' );
  173. // Detect the type of siteaccess we are dealing with. Initially URI and Host are supported.
  174. // We don't want the siteaccess part here, since we are inserting our siteaccess name.
  175. $indexFile = trim( eZSys::indexFile( false ), '/' );
  176. switch ( $GLOBALS['eZCurrentAccess']['type'] )
  177. {
  178. case eZSiteAccess::TYPE_URI:
  179. eZURI::transformURI( $host, true, 'full' );
  180. break;
  181. default:
  182. $host = $saIni->variable( 'SiteSettings', 'SiteURL' );
  183. $host = "http://{$host}/";
  184. break;
  185. }
  186. $this->baseDestinationUrl = "{$host}{$indexFile}";
  187. }
  188. /**
  189. * Creates an array of corresponding language switcher links and logical names.
  190. *
  191. * This mapping is set up in site.ini.[RegionalSettings].TranslationSA.
  192. * The purpose of this method is to assist creation of language switcher
  193. * links into the available translation siteaccesses on the system.
  194. *
  195. * This is used by the language_switcher template operator.
  196. *
  197. * @param string $url
  198. * @return void
  199. */
  200. public static function setupTranslationSAList( $url = null )
  201. {
  202. $ini = eZINI::instance();
  203. if ( !$ini->hasVariable( 'RegionalSettings', 'TranslationSA' ) )
  204. {
  205. return array();
  206. }
  207. $ret = array();
  208. $translationSiteAccesses = $ini->variable( 'RegionalSettings', 'TranslationSA' );
  209. foreach ( $translationSiteAccesses as $siteAccessName => $translationName )
  210. {
  211. $switchLanguageLink = "/switchlanguage/to/{$siteAccessName}/";
  212. if ( $url !== null && ( is_string( $url ) || is_numeric( $url ) ) )
  213. {
  214. $switchLanguageLink .= $url;
  215. }
  216. $ret[$siteAccessName] = array( 'url' => $switchLanguageLink,
  217. 'text' => $translationName
  218. );
  219. }
  220. return $ret;
  221. }
  222. }
  223. ?>