/** * Returns the localization model based on the localization's slug key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param str $slug the slug key of the model to be loaded * @return ProductLocalization the loaded model * @throws CHttpException */ public function loadProductModelSlug($slug) { $model = ProductLocalization::model()->findByAttributes(array('slug' => $slug)); if ($model === null) { throw new CHttpException(404, Yii::t('app', 'La page demandée n\'existe pas.')); } if ($model->locale_id !== Yii::app()->language) { // Current slug doesn't exist in the appropriate language. Redirect the user to the appropriate language $appropriate_model = $model->product->localizationForLanguage(Yii::app()->language, $accept_substitute = false); if ($appropriate_model) { // Redirect to that localized model $redict_url = $this->createUrl(Yii::app()->controller->action->id, array('slug' => $appropriate_model->slug, 'language' => $appropriate_model->locale_id)); $this->redirect($redict_url); } throw new CHttpException(404, Yii::t('app', 'La page demandée n\'existe pas.')); } return $model; }
/** * Returns a localization for the specified product and language. * @param string $language The two letter locale id for the language. * @param boolean $accept_substitute if true, will return any other localization in lieu if the one asked is unavailable. * @return ProductLocalization the static model class */ public function localizationForLanguage($language, $accept_substitute = false) { if ($this->_localizationForLanguage !== null) { return $this->_localizationForLanguage; } $criteria = new CDbCriteria(); $criteria->addCondition('product_id=' . $this->id); $criteria->addCondition('locale_id="' . $language . '"'); $localizationForProduct = ProductLocalization::model()->find($criteria); if ($localizationForProduct === null && $accept_substitute) { // No localization exists for the current page localization. User might have added the product from a page in another language than switched language. Just pick the first localization available for the product and call it a day. // We need to run on multiple hosts, some supporting PHP 5.3 and others supporting 5.4 and up if (version_compare(PHP_VERSION, '5.4.0', '>=')) { // 5.4 and up // We can't use the new PHP 5.4 syntax directly since 5.3 will throw a syntax error. eval('$localizationForProduct = $this->productLocalizations[0];'); } else { // Older than 5.4 $localizationForProduct = array_shift(array_values($this->productLocalizations)); } } $this->_localizationForLanguage = $localizationForProduct; return $localizationForProduct; }