Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

ezplanguageswitcher.php
Go to the documentation of this file.
1 <?php
18 class ezpLanguageSwitcher implements ezpLanguageSwitcherCapable
19 {
20 protected $origUrl;
21 protected $userParamString;
22 protected $queryString;
23
24 protected $destinationSiteAccess;
25 protected $destinationLocale;
26
27 protected $baseDestinationUrl;
28
29 protected $destinationSiteAccessIni;
30
31 function __construct( $params = null )
32 {
33 if ( $params === null )
34 {
35 return $this;
36 }
37
38 // Removing the first part, which is the SA param.
39 array_shift( $params['Parameters'] );
40 $this->origUrl = join( '/', $params['Parameters']);
41
42 $this->userParamString = '';
43 $userParams = $params['UserParameters'];
44 foreach ( $userParams as $key => $value )
45 {
46 $this->userParamString .= "/($key)/$value";
47 }
48
49 $this->queryString = isset( $params['QueryString'] ) ? $params['QueryString'] : '';
50 }
51
57 protected function getSiteAccessIni()
58 {
59 if ( $this->destinationSiteAccessIni === null )
60 {
61 $this->destinationSiteAccessIni = eZSiteAccess::getIni( $this->destinationSiteAccess, 'site.ini' );
62 }
63 return $this->destinationSiteAccessIni;
64 }
65
75 protected function isUrlPointingToModule( $url )
76 {
77 // Grab the first URL element, representing the possible module name
78 $urlElements = explode( '/', $url );
79 $moduleName = $urlElements[0];
80
81 // Look up for a match in the module list
82 $moduleIni = eZINI::instance( 'module.ini' );
83 $availableModules = $moduleIni->variable( 'ModuleSettings', 'ModuleList' );
84 return in_array( $moduleName, $availableModules, true );
85 }
86
100 protected function isLocaleAvailableAsFallback()
101 {
102 $currentContentObjectLocale = eZINI::instance()->variable( 'RegionalSettings', 'ContentObjectLocale' );
103 $saIni = $this->getSiteAccessIni();
104 $siteLanguageList = $saIni->variable( 'RegionalSettings', 'SiteLanguageList' );
105 return in_array( $currentContentObjectLocale, $siteLanguageList, true );
106 }
107
108
116 protected static function addPathPrefixIfNeeded( $url )
117 {
118 $siteINI = eZINI::instance( 'site.ini' );
119 if ( $siteINI->hasVariable( 'SiteAccessSettings', 'PathPrefix' ) )
120 {
121 $pathPrefix = $siteINI->variable( 'SiteAccessSettings', 'PathPrefix' );
122 if ( !empty( $pathPrefix ) )
123 {
124 $url = $pathPrefix . '/' . $url;
125 }
126 }
127 return $url;
128 }
129
138 protected static function removePathPrefixIfNeeded( eZINI $saIni, &$url )
139 {
140 if ( $saIni->hasVariable( 'SiteAccessSettings', 'PathPrefix' ) )
141 {
142 $pathPrefix = $saIni->variable( 'SiteAccessSettings', 'PathPrefix' );
143 if ( !empty( $pathPrefix ) )
144 {
145 if ( ( strpos( $url, $pathPrefix . '/' ) === 0 ) || ( $pathPrefix === $url ) )
146 {
147 $url = substr( $url, strlen( $pathPrefix ) + 1 );
148 }
149 else
150 {
151 // PathPrefix exists, but not matched in url.
152 return false;
153 }
154 }
155 }
156 return true;
157 }
158
164 public function destinationUrl()
165 {
166 $nodeId = $this->origUrl;
167 $urlAlias = '';
168
169 if ( !is_numeric( $this->origUrl ) )
170 {
171 if ( !$this->isUrlPointingToModule( $this->origUrl ) )
172 {
173 $this->origUrl = self::addPathPrefixIfNeeded( $this->origUrl );
174 }
175
176 $nodeId = eZURLAliasML::fetchNodeIDByPath( $this->origUrl );
177 }
178
179 $siteLanguageList = $this->getSiteAccessIni()->variable( 'RegionalSettings', 'SiteLanguageList' );
180 // set prioritized languages of destination SA, and fetch corresponding (prioritized) URL alias
181 eZContentLanguage::setPrioritizedLanguages( $siteLanguageList );
182 $destinationElement = eZURLAliasML::fetchByAction( 'eznode', $nodeId, false, true );
183
184 eZContentLanguage::clearPrioritizedLanguages();
185
186 if ( empty( $destinationElement ) || ( !isset( $destinationElement[0] ) && !( $destinationElement[0] instanceof eZURLAliasML ) ) )
187 {
188 // If the return of fetchByAction is empty, it can mean a couple
189 // of different things:
190 // Either we are looking at a module, and we should pass the
191 // original URL on
192 //
193 // Or we are looking at URL which does not exist in the
194 // destination siteaccess, for instance an untranslated object. In
195 // which case we will point to the root of the site, unless it is
196 // available as a fallback.
197 if ( $nodeId )
198 {
199 $urlAlias = $this->origUrl;
200
201 // if applicable, remove destination PathPrefix from url
202 if ( !self::removePathPrefixIfNeeded( $this->getSiteAccessIni(), $urlAlias ) )
203 {
204 // If destination siteaccess has a PathPrefix but url is not matched,
205 // also check current SA's prefix, and remove if it matches.
206 self::removePathPrefixIfNeeded( eZINI::instance( 'site.ini' ), $urlAlias );
207 }
208 }
209 else
210 {
211 if ( $this->isUrlPointingToModule( $this->origUrl ) )
212 {
213 $urlAlias = $this->origUrl;
214 }
215 }
216 }
217 else
218 {
219 // Translated object found, forwarding to new URL.
220 $urlAlias = $destinationElement[0]->getPath( $this->destinationLocale, $siteLanguageList );
221
222 // if applicable, remove destination PathPrefix from url
223 self::removePathPrefixIfNeeded( $this->getSiteAccessIni(), $urlAlias );
224
225 $urlAlias .= $this->userParamString;
226 }
227
228 $this->baseDestinationUrl = rtrim( $this->baseDestinationUrl, '/' );
229
230 $ini = eZINI::instance();
231
232 if ( $GLOBALS['eZCurrentAccess']['type'] === eZSiteAccess::TYPE_URI &&
233 !( $ini->variable( 'SiteAccessSettings', 'RemoveSiteAccessIfDefaultAccess' ) === "enabled" &&
234 $ini->variable( 'SiteSettings', 'DefaultAccess' ) == $this->destinationSiteAccess ) )
235 {
236 $finalUrl = $this->baseDestinationUrl . '/' . $this->destinationSiteAccess . '/' . $urlAlias;
237 }
238 else
239 {
240 $finalUrl = $this->baseDestinationUrl . '/' . $urlAlias;
241 }
242 if ( $this->queryString != '' )
243 {
244 $finalUrl .= '?' . $this->queryString;
245 }
246 return $finalUrl;
247 }
248
255 public function setDestinationSiteAccess( $saName )
256 {
257 $this->destinationSiteAccess = $saName;
258 }
259
268 public function process()
269 {
270 $saIni = $this->getSiteAccessIni();
271 $this->destinationLocale = $saIni->variable( 'RegionalSettings', 'ContentObjectLocale' );
272
273 // Detect the type of siteaccess we are dealing with. Initially URI and Host are supported.
274 // We don't want the siteaccess part here, since we are inserting our siteaccess name.
275 $indexFile = trim( eZSys::indexFile( false ), '/' );
276 switch ( $GLOBALS['eZCurrentAccess']['type'] )
277 {
278 case eZSiteAccess::TYPE_URI:
279 eZURI::transformURI( $host, true, 'full' );
280 break;
281
282 default:
283 $host = $saIni->variable( 'SiteSettings', 'SiteURL' );
284 $host = eZSys::serverProtocol()."://".$host;
285 break;
286 }
287 $this->baseDestinationUrl = "{$host}{$indexFile}";
288 }
289
302 public static function setupTranslationSAList( $url = null )
303 {
304 $ini = eZINI::instance();
305 if ( !$ini->hasVariable( 'RegionalSettings', 'TranslationSA' ) )
306 {
307 return array();
308 }
309
310 $ret = array();
311 $translationSiteAccesses = $ini->variable( 'RegionalSettings', 'TranslationSA' );
312 foreach ( $translationSiteAccesses as $siteAccessName => $translationName )
313 {
314 $switchLanguageLink = "/switchlanguage/to/{$siteAccessName}/";
315 if ( $url !== null && ( is_string( $url ) || is_numeric( $url ) ) )
316 {
317 $switchLanguageLink .= $url;
318 }
319 $ret[$siteAccessName] = array(
320 'url' => $switchLanguageLink,
321 'text' => $translationName,
322 'locale' => eZSiteAccess::getIni( $siteAccessName )->variable( 'RegionalSettings', 'ContentObjectLocale' )
323 );
324 }
325 return $ret;
326 }
327 }
328
329 ?>
eZContentLanguage\clearPrioritizedLanguages
static clearPrioritizedLanguages()
Definition: ezcontentlanguage.php:394
eZContentLanguage\setPrioritizedLanguages
static setPrioritizedLanguages( $languages)
Definition: ezcontentlanguage.php:382
eZINI
Reads and writes .ini style configuration files.
eZSiteAccess\TYPE_URI
const TYPE_URI
Definition: ezsiteaccess.php:25
eZSiteAccess\getIni
static getIni( $siteAccess, $settingFile='site.ini')
Definition: ezsiteaccess.php:632
eZSys\serverProtocol
static serverProtocol()
Definition: ezsys.php:780
eZSys\indexFile
static indexFile( $withAccessPath=true)
Definition: ezsys.php:619
eZURI\transformURI
static transformURI(&$href, $ignoreIndexDir=false, $serverURL=null, $htmlEscape=true)
Definition: ezuri.php:600
$ini
$ini
Definition: classes/admin/linklist.php:64
ezpLanguageSwitcher
Definition: ezplanguageswitcher.php:19
ezpLanguageSwitcher\setDestinationSiteAccess
setDestinationSiteAccess( $saName)
Definition: ezplanguageswitcher.php:255
ezpLanguageSwitcher\$origUrl
$origUrl
Definition: ezplanguageswitcher.php:20
ezpLanguageSwitcher\$destinationSiteAccess
$destinationSiteAccess
Definition: ezplanguageswitcher.php:24
ezpLanguageSwitcher\$destinationLocale
$destinationLocale
Definition: ezplanguageswitcher.php:25
ezpLanguageSwitcher\$queryString
$queryString
Definition: ezplanguageswitcher.php:22
ezpLanguageSwitcher\$userParamString
$userParamString
Definition: ezplanguageswitcher.php:21
ezpLanguageSwitcher\process
process()
Definition: ezplanguageswitcher.php:268
ezpLanguageSwitcher\removePathPrefixIfNeeded
static removePathPrefixIfNeeded(eZINI $saIni, &$url)
Definition: ezplanguageswitcher.php:138
ezpLanguageSwitcher\setupTranslationSAList
static setupTranslationSAList( $url=null)
Definition: ezplanguageswitcher.php:302
ezpLanguageSwitcher\isLocaleAvailableAsFallback
isLocaleAvailableAsFallback()
Definition: ezplanguageswitcher.php:100
ezpLanguageSwitcher\getSiteAccessIni
getSiteAccessIni()
Definition: ezplanguageswitcher.php:57
ezpLanguageSwitcher\isUrlPointingToModule
isUrlPointingToModule( $url)
Definition: ezplanguageswitcher.php:75
ezpLanguageSwitcher\$baseDestinationUrl
$baseDestinationUrl
Definition: ezplanguageswitcher.php:27
ezpLanguageSwitcher\addPathPrefixIfNeeded
static addPathPrefixIfNeeded( $url)
Definition: ezplanguageswitcher.php:116
ezpLanguageSwitcher\__construct
__construct( $params=null)
Definition: ezplanguageswitcher.php:31
ezpLanguageSwitcher\destinationUrl
destinationUrl()
Definition: ezplanguageswitcher.php:164
ezpLanguageSwitcher\$destinationSiteAccessIni
$destinationSiteAccessIni
Definition: ezplanguageswitcher.php:29
$ret
$ret
Definition: ezsitemanager/admin/filelist.php:88
ezpLanguageSwitcherCapable
Definition: ezplanguageswitchercapable.php:18
$url
$url
Definition: messagereviewlist.php:153
$indexFile
$indexFile
Definition: singlelist.php:34
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.