protected function updateProductFromPayload($product, $payload) { $product->sku = $payload->sku; $product->barcode = $payload->barcode; $product->brand_id = $payload->brand; // We use the actual brand id as is $product->discontinued = $payload->discontinued; $product->visible = $payload->enabled; // Pay attention: small name change here $product->taxable = $payload->taxable; $product->price = $payload->price; $product->weight = $payload->weight; $product->parent_product_id = $payload->parent_product_id; $product->save(); // We insert the images before the localization so we can save the localizations with an image for quick elasticsearch reference // Clean up all previous images foreach ($product->productImages as $image) { $image->delete(); } foreach ($payload->images as $image) { $productimg = new ProductImage(); $productimg->product_id = $product->id; $productimg->extension = $image->extension; $productimg->identifier = $image->id; $productimg->position = $image->position; $productimg->locale_id = Locale::localeIdFromLongCode($image->locale); $productimg->save(); } // Clean up all previous localizations foreach ($product->productLocalizations as $localization) { $localization->delete(); } foreach ($payload->localizations as $localization) { $productLocalization = new ProductLocalization(); $productLocalization->product_id = $product->id; $productLocalization->locale_id = Locale::localeIdFromLongCode($localization->locale); $productLocalization->name = $localization->name; $productLocalization->short_description = $localization->short_description; $productLocalization->long_description = $localization->long_description; $productLocalization->visible = $localization->enabled; $productLocalization->save(); } $previousRelationships = ProductHasCategory::model()->findAll("product_id=:product_id", array(':product_id' => $product->id)); foreach ($previousRelationships as $relat) { $relat->delete(); } foreach ($payload->categories as $category) { // Check for existing relationships... we encounter some duplicates sometimes $existingRelationship = ProductHasCategory::model()->find("product_id=:product_id AND category_id=:category_id", array(':product_id' => $product->id, ':category_id' => $category)); if ($existingRelationship === null) { $catrel = new ProductHasCategory(); $catrel->product_id = $product->id; $catrel->category_id = $category; $catrel->save(); } } return $product; }
/** * 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; }