예제 #1
0
 public function import($startRecord = 0)
 {
     $count = 0;
     $errors = 0;
     $hdl = $this->t1db->query(sprintf("select * from produit order by rubrique asc limit %d, %d", intval($startRecord), $this->getChunkSize()));
     $image_import = new ProductImageImport($this->dispatcher, $this->t1db);
     $document_import = new ProductDocumentImport($this->dispatcher, $this->t1db);
     while ($hdl && ($produit = $this->t1db->fetch_object($hdl))) {
         $count++;
         // Put contents on the root folder in a special folder
         if ($produit->rubrique == 0) {
             try {
                 $this->cat_corresp->getT2($produit->rubrique);
             } catch (\Exception $ex) {
                 // Create the '0' folder
                 $root = new Category();
                 $root->setParent(0)->setLocale('fr_FR')->setTitle("Rubrique racine Thelia 1")->setLocale('en_US')->setTitle("Thelia 1 root category")->setDescription("")->setChapo("")->setPostscriptum("")->setVisible(true)->save();
                 Tlog::getInstance()->warning("Created pseudo-root category to store products at root level");
                 $this->cat_corresp->addEntry(0, $root->getId());
             }
         }
         $rubrique = $this->cat_corresp->getT2($produit->rubrique);
         if ($rubrique > 0) {
             try {
                 $this->product_corresp->getT2($produit->id);
                 Tlog::getInstance()->warning("Product ID={$produit->id} already imported.");
                 continue;
             } catch (ImportException $ex) {
                 // Okay, the product was not imported.
             }
             try {
                 // Check if the product ref is not already defined, and create a new one if it's the case.
                 $origRef = $destRef = $produit->ref;
                 $refIdx = 1;
                 while (null !== ($dupProd = ProductQuery::create()->findOneByRef($destRef))) {
                     Tlog::getInstance()->warning("Duplicate product reference: '{$destRef}', generating alternate reference.");
                     $destRef = sprintf('%s-%d', $origRef, $refIdx++);
                 }
                 $event = new ProductCreateEvent();
                 $idx = 0;
                 $descs = $this->t1db->query_list("select * from produitdesc where produit = ? order by lang asc", array($produit->id));
                 // Prices should be without axes
                 $produit->prix = round($produit->prix / (1 + $produit->tva / 100), 2);
                 $produit->prix2 = round($produit->prix2 / (1 + $produit->tva / 100), 2);
                 $product_id = 0;
                 foreach ($descs as $objdesc) {
                     $lang = $this->getT2Lang($objdesc->lang);
                     // A title is required to create the rewritten URL
                     if (empty($objdesc->titre)) {
                         $objdesc->titre = sprintf("Untitled-%d-%s", $objdesc->id, $lang->getCode());
                     }
                     if ($idx == 0) {
                         $event->setRef($destRef)->setLocale($lang->getLocale())->setTitle($objdesc->titre)->setDefaultCategory($this->cat_corresp->getT2($produit->rubrique))->setVisible($produit->ligne == 1 ? true : false)->setBasePrice($produit->prix)->setBaseWeight($produit->poids)->setTaxRuleId($this->tax_corresp->getT2(1000 * $produit->tva))->setCurrencyId($this->getT2Currency()->getId());
                         $this->dispatcher->dispatch(TheliaEvents::PRODUCT_CREATE, $event);
                         $product_id = $event->getProduct()->getId();
                         // Set the product template (resets the product default price)
                         // -----------------------------------------------------------
                         try {
                             $pste = new ProductSetTemplateEvent($event->getProduct(), $this->tpl_corresp->getT2($produit->rubrique), $this->getT2Currency()->getId());
                             $this->dispatcher->dispatch(TheliaEvents::PRODUCT_SET_TEMPLATE, $pste);
                         } catch (ImportException $ex) {
                             Tlog::getInstance()->addWarning("No product template was found for product {$product_id}: ", $ex->getMessage());
                         }
                         // Create the default product sale element, and update it
                         // ------------------------------------------------------
                         $create_pse_event = new ProductSaleElementCreateEvent($event->getProduct(), array(), $this->getT2Currency()->getId());
                         $this->dispatcher->dispatch(TheliaEvents::PRODUCT_ADD_PRODUCT_SALE_ELEMENT, $create_pse_event);
                         $update_pse_event = new ProductSaleElementUpdateEvent($event->getProduct(), $create_pse_event->getProductSaleElement()->getId());
                         $update_pse_event->setReference($destRef)->setPrice($produit->prix)->setCurrencyId($this->getT2Currency()->getId())->setWeight($produit->poids)->setQuantity($produit->stock)->setSalePrice($produit->prix2)->setOnsale($produit->promo ? true : false)->setIsnew($produit->nouveaute ? true : false)->setIsdefault(true)->setEanCode('')->setTaxRuleId($this->tax_corresp->getT2(1000 * $produit->tva))->setFromDefaultCurrency(0);
                         $this->dispatcher->dispatch(TheliaEvents::PRODUCT_UPDATE_PRODUCT_SALE_ELEMENT, $update_pse_event);
                         // Update position
                         // ---------------
                         $update_position_event = new UpdatePositionEvent($product_id, UpdatePositionEvent::POSITION_ABSOLUTE, $produit->classement);
                         $this->dispatcher->dispatch(TheliaEvents::PRODUCT_UPDATE_POSITION, $update_position_event);
                         Tlog::getInstance()->info("Created product {$product_id} from {$objdesc->titre} ({$produit->id})");
                         $this->product_corresp->addEntry($produit->id, $product_id);
                         // Import related content
                         // ----------------------
                         $contents = $this->t1db->query_list("select * from contenuassoc where objet=? and type=1 order by classement", array($produit->id));
                         // type: 1 = produit, 0=rubrique
                         foreach ($contents as $content) {
                             try {
                                 $content_event = new ProductAddContentEvent($event->getProduct(), $this->content_corresp->getT2($content->contenu));
                                 $this->dispatcher->dispatch(TheliaEvents::PRODUCT_ADD_CONTENT, $content_event);
                             } catch (\Exception $ex) {
                                 Tlog::getInstance()->addError("Failed to create associated content {$content->contenu} for product {$product_id}: ", $ex->getMessage());
                                 $errors++;
                             }
                         }
                         // Update features (= caracteristiques) values
                         // -------------------------------------------
                         $caracvals = $this->t1db->query_list("select * from caracval where produit=?", array($produit->id));
                         foreach ($caracvals as $caracval) {
                             try {
                                 if (intval($caracval->caracdisp) != 0) {
                                     $feature_value = $this->feat_av_corresp->getT2($caracval->caracdisp);
                                     $is_text = false;
                                 } elseif ($caracval->valeur != '') {
                                     $feature_value = $caracval->valeur;
                                     $is_text = true;
                                 } else {
                                     continue;
                                 }
                                 $feature_value_event = new FeatureProductUpdateEvent($product_id, $this->feat_corresp->getT2($caracval->caracteristique), $feature_value, $is_text);
                                 $this->dispatcher->dispatch(TheliaEvents::PRODUCT_FEATURE_UPDATE_VALUE, $feature_value_event);
                             } catch (\Exception $ex) {
                                 Tlog::getInstance()->addError("Failed to update feature value with caracdisp ID={$caracval->caracdisp} (value='{$caracval->valeur}') for product {$product_id}: ", $ex->getMessage());
                                 $errors++;
                             }
                         }
                         // Update Attributes (= declinaisons) options
                         // ------------------------------------------
                         $rubdecs = $this->t1db->query_list("\n                                                                select\n                                                                    declinaison\n                                                                from\n                                                                    rubdeclinaison rd, declinaison d\n                                                                where\n                                                                    rd.declinaison=d.id\n                                                                and\n                                                                    rd.rubrique=?\n                                                                 order by\n                                                                    d.classement", array($produit->rubrique));
                         foreach ($rubdecs as $rubdec) {
                             $declidisps = $this->t1db->query_list("select id from declidisp where declinaison=?", array($rubdec->declinaison));
                             foreach ($declidisps as $declidisp) {
                                 $disabled = $this->t1db->query_list("select id from exdecprod where declidisp=? and produit=?", array($declidisp->id, $produit->id));
                                 if (count($disabled) > 0) {
                                     continue;
                                 }
                                 $stock = $this->t1db->query_obj("select * from stock where declidisp=?and produit=?", array($declidisp->id, $produit->id));
                                 if ($stock == false) {
                                     continue;
                                 }
                                 try {
                                     $pse_create_event = new ProductSaleElementCreateEvent($event->getProduct(), array($this->attr_av_corresp->getT2($stock->declidisp)), $this->getT2Currency()->getId());
                                     $this->dispatcher->dispatch(TheliaEvents::PRODUCT_ADD_PRODUCT_SALE_ELEMENT, $pse_create_event);
                                     $pse_update_event = new ProductSaleElementUpdateEvent($event->getProduct(), $pse_create_event->getProductSaleElement()->getId());
                                     $pse_update_event->setReference($destRef)->setPrice($produit->prix + $stock->surplus)->setCurrencyId($this->getT2Currency()->getId())->setWeight($produit->poids)->setQuantity($stock->valeur)->setSalePrice($produit->prix2 + $stock->surplus)->setOnsale($produit->promo ? true : false)->setIsnew($produit->nouveaute ? true : false)->setIsdefault(true)->setEanCode('')->setTaxRuleId($this->tax_corresp->getT2(1000 * $produit->tva))->setFromDefaultCurrency(0);
                                     $this->dispatcher->dispatch(TheliaEvents::PRODUCT_UPDATE_PRODUCT_SALE_ELEMENT, $pse_update_event);
                                 } catch (\Exception $ex) {
                                     Tlog::getInstance()->addError("Failed to update product sale element value with declidisp ID={$stock->declidisp} for product {$product_id}: ", $ex->getMessage());
                                     $errors++;
                                 }
                             }
                         }
                         // Import images and documents
                         // ---------------------------
                         $image_import->importMedia($produit->id, $product_id);
                         $document_import->importMedia($produit->id, $product_id);
                     }
                     Tlog::getInstance()->info(sprintf("Updating product ID=%d, data for lang %s.", $product_id, $lang->getCode()));
                     // Update the newly created product
                     $update_event = new ProductUpdateEvent($product_id);
                     $update_event->setRef($destRef)->setLocale($lang->getLocale())->setTitle($objdesc->titre)->setChapo($objdesc->chapo)->setDescription($objdesc->description)->setPostscriptum($objdesc->postscriptum)->setVisible($produit->ligne == 1 ? true : false)->setDefaultCategory($this->cat_corresp->getT2($produit->rubrique));
                     $this->dispatcher->dispatch(TheliaEvents::PRODUCT_UPDATE, $update_event);
                     // Update the rewritten URL, if one was defined
                     $this->updateRewrittenUrl($event->getProduct(), $lang->getLocale(), $objdesc->lang, "produit", "%id_produit={$produit->id}&id_rubrique={$produit->rubrique}%");
                     $idx++;
                 }
             } catch (\Exception $ex) {
                 Tlog::getInstance()->addError("Failed to import product ID={$produit->id}: ", $ex->getMessage());
                 $errors++;
             }
         } else {
             Tlog::getInstance()->addError("Cannot import product ID={$produit->id}, which is at root level (e.g., rubrique parent = 0).");
             $errors++;
         }
     }
     return new ImportChunkResult($count, $errors);
 }
예제 #2
0
파일: Product.php 프로젝트: savvagee/thelia
 public function updateClone(ProductCloneEvent $event, $originalProductDefaultPrice)
 {
     // Get original product's I18ns
     $originalProductI18ns = ProductI18nQuery::create()->findById($event->getOriginalProduct()->getId());
     foreach ($originalProductI18ns as $originalProductI18n) {
         $clonedProductUpdateEvent = new ProductUpdateEvent($event->getClonedProduct()->getId());
         $clonedProductUpdateEvent->setRef($event->getClonedProduct()->getRef())->setVisible($event->getClonedProduct()->getVisible())->setVirtual($event->getClonedProduct()->getVirtual())->setLocale($originalProductI18n->getLocale())->setTitle($originalProductI18n->getTitle())->setChapo($originalProductI18n->getChapo())->setDescription($originalProductI18n->getDescription())->setPostscriptum($originalProductI18n->getPostscriptum())->setBasePrice($originalProductDefaultPrice->getPrice())->setCurrencyId($originalProductDefaultPrice->getCurrencyId())->setBaseWeight($event->getOriginalProduct()->getDefaultSaleElements()->getWeight())->setTaxRuleId($event->getOriginalProduct()->getTaxRuleId())->setBrandId($event->getOriginalProduct()->getBrandId())->setDefaultCategory($event->getOriginalProduct()->getDefaultCategoryId());
         $event->getDispatcher()->dispatch(TheliaEvents::PRODUCT_UPDATE, $clonedProductUpdateEvent);
         // SEO info
         $clonedProductUpdateSeoEvent = new UpdateSeoEvent($event->getClonedProduct()->getId());
         $clonedProductUpdateSeoEvent->setLocale($originalProductI18n->getLocale())->setMetaTitle($originalProductI18n->getMetaTitle())->setMetaDescription($originalProductI18n->getMetaDescription())->setMetaKeywords($originalProductI18n->getMetaKeywords())->setUrl(null);
         $event->getDispatcher()->dispatch(TheliaEvents::PRODUCT_UPDATE_SEO, $clonedProductUpdateSeoEvent);
     }
     $event->setClonedProduct($clonedProductUpdateEvent->getProduct());
     // Set clone's template
     $clonedProductUpdateTemplateEvent = new ProductSetTemplateEvent($event->getClonedProduct(), $event->getOriginalProduct()->getTemplateId(), $originalProductDefaultPrice->getCurrencyId());
     $event->getDispatcher()->dispatch(TheliaEvents::PRODUCT_SET_TEMPLATE, $clonedProductUpdateTemplateEvent);
 }