Exemplo n.º 1
0
    public function actionCreate()
    {
        $product = new Product();
        $product->save();
        $product_id = $product->getPrimaryKey();

        $allLangs = LanguageModel::model()->findAll('1', array('order'=> 'id ASC'));
        foreach ($allLangs as $language) {
            $product_description = new ProductDescription();
            $product_description->product_id = $product_id;
            $product_description->language_id = $language['id'];
            $product_description->save();
        }

        $Category2product = new Category2product();
        $Category2product->product_id = $product_id;
        $Category2product->save();

        $this->redirect(Yii::app()->baseUrl . '/admin/product/update/id/' . $product_id);
        return false;

        $model = new Product;
        $lang_model = new LanguageModel;
        $ProdDescModel = new ProductDescription;
        $Category2product = new Category2product;
        $CadDescModel = new CategoryDescription;
        $aCategories = Category::model()->getCategoriesTree();

        $AttributeModel = new Attribute;
        $AttributeDescModel = new AttributeDescription;
        $Attribute2value = new Attribute2value;

    $ValueModel = new Value;
    $ValueDescription = new ValueDescription;

    $allLangs = $lang_model->findAll('1', array('order'=> 'id ASC'));

    $allLangsJS = array();
    foreach ($allLangs as $language) {
      $allLangsJS[$language['id']] = $language['code'];
    }

    $aAttributes = $AttributeModel->findAll();
    $aReadyAttrsValues = array();
    foreach ($aAttributes as $attribute) {
      //descriptions
      $aAttrDescr = $AttributeDescModel->find("language_id='" . $this->language_id . "' AND attribute_id='" . $attribute['attribute_id'] . "'");
      $aReadyAttrsValues[$attribute['attribute_id']]['attribute_name'] = $aAttrDescr['attribute_name'];
      //values
      $aValues = $Attribute2value->findAll('attribute_id=' . $attribute['attribute_id']);
      foreach ($aValues as $value) {
        $aValDescrs = $ValueDescription->findAll("value_id='" . $value['value_id'] . "' AND language_id='" . $this->language_id . "'");
        foreach ($aValDescrs as $value_description) {
          $aReadyAttrsValues[$attribute['attribute_id']]['values'][$value['value_id']] = $value_description['value_name'];
        }
      }
    }

                if (isset($_POST['Product'])) {

                    $model->attributes = $_POST['Product'];
                    if ($model->save()) {
                        $pk = $model->getPrimaryKey();
                        $Category2product->attributes = array('product_id' => $pk, 'category_id' => $_POST['Category2product']['category_id']);

                        $Category2product->save();

                        foreach ($_POST['ProductDescription']['product_name'] as $langId => $prodName) {
                            $prod_desc_model = new ProductDescription;

                            $prod_desc_model->attributes = array(
                                'product_id' => $pk,
                                'product_name' => $prodName,
                                'language_id' => $langId,
                            );
                            $prod_desc_model->save();

                        }
                        if (isset($_POST['NewVariation'])) {

                            foreach ($_POST['NewVariation'] as $specId => $variationData) {
                                $Variation = new Variation;

                                $Variation->attributes = array(
                                    'variation_image' => 'some_image.jpg',
                                    'status' => 1,
                                );
                                $Variation->save();

                                $variation_id = $Variation->getPrimaryKey();
                                $Product2variation = new Product2variation;
                                $Product2variation->attributes = array(
                                    'product_id' => $pk,
                                    'variation_id' => $variation_id,
                                );
                                $Product2variation->save();
                                foreach ($variationData as $lang_id => $variation_name) {
                                    $VariationDescription = new VariationDescription;
                                    $VariationDescription->attributes = array(
                                        'variation_name' => $variation_name,
                                        'variation_id' => $variation_id,
                                        'language_id' => $lang_id,
                                    );
                                    $VariationDescription->save();
                                }
                                if (isset($_POST['NewAttribute'][$specId])) {
                                    foreach ($_POST['NewAttribute'][$specId] as $attr_id => $value) {
                                        if (isset($_POST['NewAttributeValue'][$specId][$attr_id]) && $_POST['NewAttributeValue'][$specId][$attr_id] > 0) {
                                            $Variation2attribute = new Variation2attribute;
                                            $Variation2attribute->attributes = array(
                                                'variation_id' => $variation_id,
                                                'attribute_id' => $attr_id,
                                            );
                                            $Variation2attribute->save();

                                            $Variation2value = new Variation2value;
                                            $Variation2value->attributes = array(
                                                'variation_id' => $variation_id,
                                                'value_id' => $_POST['NewAttributeValue'][$specId][$attr_id],
                                            );
                                            $Variation2value->save();
                                        }
                                    }
                                }
                            }
                        }
                        $this->redirect(array('index'));
                    }
                }

        $this->render('create', array(
            'model' => $model,
            'allLangs' => $allLangs,
            'ProdDescModel' => $ProdDescModel,
            'aCategories' => $aCategories,
            'Category2product' => $Category2product,
            'allLangsJS' => json_encode($allLangsJS),
            'aReadyAttrsValues' => $aReadyAttrsValues,
        ));
    }
Exemplo n.º 2
0
 public function save()
 {
     $product = Product::model()->findByPk($this->id);
     if (is_null($product)) {
         // insert
         // Product
         $product = new Product();
         $product->model = $this->model;
         $product->sku = $this->sku;
         $product->upc = $this->upc;
         $product->ean = $this->ean;
         $product->jan = $this->jan;
         $product->isbn = $this->isbn;
         $product->mpn = $this->mpn;
         $product->location = $this->location;
         $product->price = $this->price;
         $product->tax_class_id = $this->taxClass;
         $product->quantity = $this->quantity;
         $product->minimum = $this->minimumQuantity;
         $product->subtract = $this->subtractStock;
         $product->stock_status_id = $this->outOfStockStatus;
         $product->shipping = $this->requiresShipping;
         $product->image = $this->image;
         $product->date_available = $this->dateAvailable;
         $product->length = $this->dimensionL;
         $product->height = $this->dimensionH;
         $product->width = $this->dimensionW;
         $product->weight = $this->weight;
         $product->weight_class_id = $this->weightClass;
         $product->sort_order = $this->sortOrder;
         $product->status = $this->status;
         $product->manufacturer_id = $this->manufacturer;
         $product->save();
         // Description
         $description = new ProductDescription();
         $description->product_id = $product->product_id;
         $description->language_id = 1;
         // TODO: make this dynamic
         $description->name = $this->name;
         $description->meta_description = $this->metaTagDescription;
         $description->meta_keyword = $this->metaTagKeywords;
         $description->description = $this->description;
         $description->tag = $this->productTags;
         $description->save();
     } else {
         // update
         // Product
         $product->model = $this->model;
         $product->sku = $this->sku;
         $product->upc = $this->upc;
         $product->ean = $this->ean;
         $product->jan = $this->jan;
         $product->isbn = $this->isbn;
         $product->mpn = $this->mpn;
         $product->location = $this->location;
         $product->price = $this->price;
         $product->tax_class_id = $this->taxClass;
         $product->quantity = $this->quantity;
         $product->minimum = $this->minimumQuantity;
         $product->subtract = $this->subtractStock;
         $product->stock_status_id = $this->outOfStockStatus;
         $product->shipping = $this->requiresShipping;
         $product->image = $this->image;
         $product->date_available = $this->dateAvailable;
         $product->length = $this->dimensionL;
         $product->height = $this->dimensionH;
         $product->width = $this->dimensionW;
         $product->weight = $this->weight;
         $product->weight_class_id = $this->weightClass;
         $product->sort_order = $this->sortOrder;
         $product->status = $this->status;
         $product->manufacturer_id = $this->manufacturer;
         $product->save();
         // Description
         $product->description->name = $this->name;
         $product->description->meta_description = $this->metaTagDescription;
         $product->description->meta_keyword = $this->metaTagKeywords;
         $product->description->description = $this->description;
         $product->description->tag = $this->productTags;
         $product->description->save();
     }
     // SEO Keyword
     $product->updateSEOKeyword($this->seoKeyword);
     // Filters
     $product->clearAllFiltersRelations();
     if (isset($this->filters) && count($this->filters) > 0) {
         foreach ($this->filters as $filterId) {
             $product->addFilter($filterId);
         }
     }
     // Categories
     $product->clearAllCategoriesRelations();
     if (isset($this->categories) && count($this->categories)) {
         foreach ($this->categories as $categoryId) {
             $product->addToCategory($categoryId);
         }
     }
     // Stores
     $product->clearAllStoresRelations();
     if (isset($this->stores) && count($this->stores)) {
         foreach ($this->stores as $storeId) {
             $product->addToStore($storeId);
         }
     }
     // Downloads
     $product->clearAllDownloadsRelations();
     if (isset($this->downloads) && count($this->downloads)) {
         foreach ($this->downloads as $downloadId) {
             $product->addToDownload($downloadId);
         }
     }
     // Related Products
     $product->clearAllRelatedProductsRelations();
     if (isset($this->relatedProducts) && count($this->relatedProducts)) {
         foreach ($this->relatedProducts as $relatedId) {
             $product->addRelatedProduct($relatedId);
         }
     }
 }