Source of file MultilingualModelAsController.php
Size: 7,725 Bytes - Last Modified: 2021-12-23T10:58:00+00:00
/var/www/docs.ssmods.com/process/src/code/control/MultilingualModelAsController.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 | <?php class MultilingualModelAsController extends ModelAsController { private static $extensions=array('MultilingualOldPageRedirector'); /** * @uses ModelAsController::getNestedController() * @return SS_HTTPResponse */ public function handleRequest(SS_HTTPRequest $request, DataModel $model) { $this->request=$request; $this->setDataModel($model); $this->pushCurrent(); //Get the locale from the language param if(MultilingualRootURLController::config()->use_country_only) { $locale=MultilingualRootURLController::get_locale_from_country($request->param('Language')); if(empty($locale)) { //Locale not found 404 try { $response=ErrorPage::response_for(404); $this->httpError(404, ($response ? $response:'The requested page could not be found.')); }catch(SS_HTTPResponse_Exception $responseException) { $result=$responseException->getResponse(); } return $result; } }else if(MultilingualRootURLController::config()->UseLocaleURL) { if(MultilingualRootURLController::config()->UseDashLocale) { //Language is missing a dash 404 if(strpos($request->param('Language'), '-')===false) { //Locale not found 404 try { $response=ErrorPage::response_for(404); $this->httpError(404, ($response ? $response:'The requested page could not be found.')); }catch(SS_HTTPResponse_Exception $responseException) { $result=$responseException->getResponse(); } return $result; } $locale=explode('-', $request->param('Language')); $locale[1]=strtoupper($locale[1]); //Make sure that the language is all lowercase if($request->param('Language')==implode('-', $locale)) { //Locale not found 404 try { $response=ErrorPage::response_for(404); $this->httpError(404, ($response ? $response:'The requested page could not be found.')); }catch(SS_HTTPResponse_Exception $responseException) { $result=$responseException->getResponse(); } return $result; } $locale=implode('_', $locale); }else { //Language is missing an underscore 404 if(strpos($request->param('Language'), '_')===false) { //Locale not found 404 try { $response=ErrorPage::response_for(404); $this->httpError(404, ($response ? $response:'The requested page could not be found.')); }catch(SS_HTTPResponse_Exception $responseException) { $result=$responseException->getResponse(); } return $result; } $locale=$request->param('Language'); } }else if(strpos($request->param('Language'), '_')!==false || strpos($request->param('Language'), '-')!==false) {//If the url has a locale in it when the settings are off //Locale not found 404 try { $response=ErrorPage::response_for(404); $this->httpError(404, ($response ? $response:'The requested page could not be found.')); }catch(SS_HTTPResponse_Exception $responseException) { $result=$responseException->getResponse(); } return $result; }else {//Potentially a language code $locale=i18n::get_locale_from_lang($request->param('Language')); } if(in_array($locale, Translatable::get_allowed_locales())) { //Set the current locale and remember it Cookie::set('language', $locale); Translatable::set_current_locale($locale); i18n::set_locale($locale); }else { //Locale not found 404 try { $response=ErrorPage::response_for(404); $this->httpError(404, ($response ? $response:'The requested page could not be found.')); }catch(SS_HTTPResponse_Exception $responseException) { $result=$responseException->getResponse(); } return $result; } //Handle the home page for the language $urlSegment=$request->param('URLSegment'); if(empty($urlSegment)) { $controller=new MultilingualRootURLController(); $response=$controller->handleRequest($request, $model); $this->popCurrent(); return $response; } //Normal page request so handle that $response=parent::handleRequest($request, $model); $this->popCurrent(); return $response; } /** * Overrides the default getNestedController() to maintain the language restrictions * @return ContentController */ public function getNestedController() { $request = $this->request; if(!$URLSegment = $request->param('URLSegment')) { throw new Exception('ModelAsController->getNestedController(): was not passed a URLSegment value.'); } // Find page by link $sitetree = DataObject::get_one( 'SiteTree', sprintf( '"URLSegment" = \'%s\' %s', Convert::raw2sql(rawurlencode($URLSegment)), (SiteTree::nested_urls() ? 'AND "ParentID" = 0' : null) ) ); if(!$sitetree) { $response = ErrorPage::response_for(404); $this->httpError(404, $response ? $response : 'The requested page could not be found.'); } // Enforce current language setting to the loaded SiteTree object if(class_exists('Translatable') && $sitetree->Locale) { Cookie::set('language', $sitetree->Locale); Translatable::set_current_locale($sitetree->Locale); } if(isset($_REQUEST['debug'])) { Debug::message("Using record #$sitetree->ID of type $sitetree->class with link {$sitetree->Link()}"); } return self::controller_for($sitetree, $this->request->param('Action')); } /** * @deprecated 3.2 Use MultilingualOldPageRedirector::find_old_page instead * * @param string $URLSegment A subset of the url. i.e in /home/contact/ home and contact are URLSegment. * @param int $parentID The ID of the parent of the page the URLSegment belongs to. * @return SiteTree */ public static function find_old_page($URLSegment, $parent = null, $ignoreNestedURLs = false) { Deprecation::notice('3.2', 'Use MultilingualOldPageRedirector::find_old_page instead'); if ($parent) { $parent = SiteTree::get()->byId($parent); } $url = MultilingualOldPageRedirector::find_old_page(array($URLSegment), $parent); return SiteTree::get_by_link($url); } } ?> |