/** * @depends testCreate */ public function testUpdate(ProductModel $product) { $event = new ProductUpdateEvent($product->getId()); $defaultCategory = CategoryQuery::create()->select('id')->addAscendingOrderByColumn('RAND()')->findOne(); $brandId = BrandQuery::create()->findOne()->getId(); $event->setLocale('fr_FR')->setTitle('test MAJ titre en français')->setDescription('test description fr')->setChapo('test chapo fr')->setPostscriptum('test postscriptum fr')->setVisible(1)->setDefaultCategory($defaultCategory)->setBrandId($brandId)->setDispatcher($this->getDispatcher()); $action = new Product(); $action->update($event); $updatedProduct = $event->getProduct(); $this->assertInstanceOf('Thelia\\Model\\Product', $updatedProduct); $updatedProduct->setLocale('fr_FR'); $this->assertEquals('test MAJ titre en français', $updatedProduct->getTitle()); $this->assertEquals('test description fr', $updatedProduct->getDescription()); $this->assertEquals('test chapo fr', $updatedProduct->getChapo()); $this->assertEquals('test postscriptum fr', $updatedProduct->getPostscriptum()); $this->assertEquals(1, $updatedProduct->getVisible()); $this->assertEquals($defaultCategory, $updatedProduct->getDefaultCategoryId()); $PSE = $updatedProduct->getProductSaleElementss(); $this->assertEquals(1, count($PSE)); return $updatedProduct; }
public function indexProduct(ProductUpdateEvent $event) { OpenSearchServerSearchHelper::indexProduct($event->getProduct()); }
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); }
protected function getUpdateEvent($formData) { $changeEvent = new ProductUpdateEvent($formData['id']); $changeEvent->setLocale($formData['locale'])->setRef($formData['ref'])->setTitle($formData['title'])->setChapo($formData['chapo'])->setDescription($formData['description'])->setPostscriptum($formData['postscriptum'])->setVisible($formData['visible'])->setVirtual($formData['virtual'])->setDefaultCategory($formData['default_category'])->setBrandId($formData['brand_id'])->setVirtualDocumentId($formData['virtual_document_id']); // Create and dispatch the change event return $changeEvent; }
public function updateAction($productId) { $this->checkAuth(AdminResources::PRODUCT, [], AccessManager::UPDATE); $this->checkProductExists($productId); $request = $this->getRequest(); $form = new ProductModificationForm($request, 'form', ['id' => $productId], ['csrf_protection' => false, 'method' => 'PUT']); $data = $request->request->all(); $data['id'] = $productId; $request->request->add($data); try { $updateForm = $this->validateForm($form); $event = new ProductUpdateEvent($productId); $event->bindForm($updateForm); $this->dispatch(TheliaEvents::PRODUCT_UPDATE, $event); return JsonResponse::create(null, 204); } catch (\Exception $e) { return JsonResponse::create(['error' => $e->getMessage()], 500); } }
/** * Change a product * * @param \Thelia\Core\Event\Product\ProductUpdateEvent $event */ public function update(ProductUpdateEvent $event) { if (null !== ($product = ProductQuery::create()->findPk($event->getProductId()))) { $con = Propel::getWriteConnection(ProductTableMap::DATABASE_NAME); $con->beginTransaction(); try { $product->setDispatcher($event->getDispatcher())->setRef($event->getRef())->setLocale($event->getLocale())->setTitle($event->getTitle())->setDescription($event->getDescription())->setChapo($event->getChapo())->setPostscriptum($event->getPostscriptum())->setVisible($event->getVisible() ? 1 : 0)->setVirtual($event->getVirtual() ? 1 : 0)->setBrandId($event->getBrandId() <= 0 ? null : $event->getBrandId())->save($con); // Update default category (if required) $product->updateDefaultCategory($event->getDefaultCategory()); $event->setProduct($product); $con->commit(); } catch (PropelException $e) { $con->rollBack(); throw $e; } } }
/** * Change a product * * @param \Thelia\Core\Event\Product\ProductUpdateEvent $event */ public function update(ProductUpdateEvent $event) { if (null !== ($product = ProductQuery::create()->findPk($event->getProductId()))) { $product->setDispatcher($event->getDispatcher())->setRef($event->getRef())->setLocale($event->getLocale())->setTitle($event->getTitle())->setDescription($event->getDescription())->setChapo($event->getChapo())->setPostscriptum($event->getPostscriptum())->setVisible($event->getVisible() ? 1 : 0)->setVirtual($event->getVirtual() ? 1 : 0)->setBrandId($event->getBrandId() <= 0 ? null : $event->getBrandId())->save(); // Update default category (ifd required) $product->updateDefaultCategory($event->getDefaultCategory()); $event->setProduct($product); } }
/** * Change a product * * @param \Thelia\Core\Event\Product\ProductUpdateEvent $event */ public function update(ProductUpdateEvent $event) { if (null !== ($product = ProductQuery::create()->findPk($event->getProductId()))) { $con = Propel::getWriteConnection(ProductTableMap::DATABASE_NAME); $con->beginTransaction(); try { $prevRef = $product->getRef(); $product->setDispatcher($event->getDispatcher())->setRef($event->getRef())->setLocale($event->getLocale())->setTitle($event->getTitle())->setDescription($event->getDescription())->setChapo($event->getChapo())->setPostscriptum($event->getPostscriptum())->setVisible($event->getVisible() ? 1 : 0)->setVirtual($event->getVirtual() ? 1 : 0)->setBrandId($event->getBrandId() <= 0 ? null : $event->getBrandId())->save($con); // Update default PSE (if product has no attributes and the product's ref change) $defaultPseRefChange = $prevRef !== $product->getRef() && 0 === $product->getDefaultSaleElements()->countAttributeCombinations(); if ($defaultPseRefChange) { $defaultPse = $product->getDefaultSaleElements(); $defaultPse->setRef($product->getRef())->save(); } // Update default category (if required) $product->updateDefaultCategory($event->getDefaultCategory()); $event->setProduct($product); $con->commit(); } catch (PropelException $e) { $con->rollBack(); throw $e; } } }
/** * @param ProductUpdateEvent $updateEvent * * @return Response */ protected function performAdditionalUpdateAction($updateEvent) { // Associate the file if it's a virtual product // and with only 1 PSE $virtualDocumentId = intval($updateEvent->getVirtualDocumentId()); if ($virtualDocumentId >= 0) { $defaultPSE = ProductSaleElementsQuery::create()->filterByProductId($updateEvent->getProductId())->filterByIsDefault(true)->findOne(); if (null !== $defaultPSE) { if ($virtualDocumentId !== 0) { $assocEvent = new MetaDataCreateOrUpdateEvent('virtual', MetaData::PSE_KEY, $defaultPSE->getId(), $virtualDocumentId); $this->dispatch(TheliaEvents::META_DATA_UPDATE, $assocEvent); } else { $assocEvent = new MetaDataDeleteEvent('virtual', MetaData::PSE_KEY, $defaultPSE->getId()); $this->dispatch(TheliaEvents::META_DATA_DELETE, $assocEvent); } } } return null; }