public function getImage($imageId) { $productImage = ProductImageQuery::create()->findPk($imageId); if ($productImage === null) { throw new FileNotFoundException(Translator::getInstance()->trans("The image id %id doesn't exist", ["%id" => $imageId], ShoppingFlux::MESSAGE_DOMAIN)); } $path = THELIA_LOCAL_DIR . "/media/images/product/" . $productImage->getFile(); if (!is_file($path) || !is_readable($path)) { throw new \ErrorException(Translator::getInstance()->trans("The file %file is not readable", ["%file" => $productImage->getFile()], ShoppingFlux::MESSAGE_DOMAIN)); } $data = file_get_contents($path); $mime = MimeTypeGuesser::getInstance()->guess($path); return new Response($data, 200, ["Content-Type" => $mime]); }
public function preImport() { // Delete table before proceeding ProductQuery::create()->deleteAll(); ProductImageQuery::create()->deleteAll(); ProductDocumentQuery::create()->deleteAll(); TaxRuleQuery::create()->deleteAll(); TaxQuery::create()->deleteAll(); ProductSaleElementsQuery::create()->deleteAll(); ProductPriceQuery::create()->deleteAll(); // Create T1 <-> T2 IDs correspondance tables $this->product_corresp->reset(); $this->tax_corresp->reset(); // Importer les taxes $this->importTaxes(); }
public function testAssociatePSEImage() { /** * Get a product sale elements which has a related product image */ $pse = ProductSaleElementsQuery::create()->useProductQuery()->joinProductImage()->endUse()->findOne(); if (null === $pse) { $this->markTestSkipped("You must have at least one product_sale_elements which has a product_image related to it's product"); } /** * Get this image and check if they are associated */ $productImage = ProductImageQuery::create()->findOneByProductId($pse->getProductId()); $association = ProductSaleElementsProductImageQuery::create()->filterByProductSaleElements($pse)->findOneByProductImageId($productImage->getId()); $isAssociated = $association !== null; $this->controller->getAssociationResponseData($pse->getId(), "image", $productImage->getId()); $newAssociation = ProductSaleElementsProductImageQuery::create()->filterByProductSaleElements($pse)->findOneByProductImageId($productImage->getId()); $isNowAssociated = $newAssociation !== null; $this->assertFalse($isAssociated === $isNowAssociated); }
/** * Gets the number of ChildProductImage objects related by a many-to-many relationship * to the current object by way of the product_sale_elements_product_image cross-reference table. * * @param Criteria $criteria Optional query object to filter the query * @param boolean $distinct Set to true to force count distinct * @param ConnectionInterface $con Optional connection object * * @return int the number of related ChildProductImage objects */ public function countProductImages($criteria = null, $distinct = false, ConnectionInterface $con = null) { if (null === $this->collProductImages || null !== $criteria) { if ($this->isNew() && null === $this->collProductImages) { return 0; } else { $query = ChildProductImageQuery::create(null, $criteria); if ($distinct) { $query->distinct(); } return $query->filterByProductSaleElements($this)->count($con); } } else { return count($this->collProductImages); } }
Model\CategoryQuery::create()->deleteAll(); Model\CategoryI18nQuery::create()->deleteAll(); Model\ProductQuery::create()->deleteAll(); Model\ProductI18nQuery::create()->deleteAll(); Model\CustomerQuery::create()->deleteAll(); Model\AdminQuery::create()->deleteAll(); Model\FolderQuery::create()->deleteAll(); Model\FolderI18nQuery::create()->deleteAll(); Model\ContentQuery::create()->deleteAll(); Model\ContentI18nQuery::create()->deleteAll(); Model\AccessoryQuery::create()->deleteAll(); Model\ProductSaleElementsQuery::create()->deleteAll(); Model\ProductPriceQuery::create()->deleteAll(); Model\BrandQuery::create()->deleteAll(); Model\BrandI18nQuery::create()->deleteAll(); Model\ProductImageQuery::create()->deleteAll(); Model\CategoryImageQuery::create()->deleteAll(); Model\FolderImageQuery::create()->deleteAll(); Model\ContentImageQuery::create()->deleteAll(); Model\BrandImageQuery::create()->deleteAll(); Model\ProductDocumentQuery::create()->deleteAll(); Model\CategoryDocumentQuery::create()->deleteAll(); Model\FolderDocumentQuery::create()->deleteAll(); Model\ContentDocumentQuery::create()->deleteAll(); Model\BrandDocumentQuery::create()->deleteAll(); Model\CouponQuery::create()->deleteAll(); Model\OrderQuery::create()->deleteAll(); Model\SaleQuery::create()->deleteAll(); Model\SaleProductQuery::create()->deleteAll(); Model\MetaDataQuery::create()->deleteAll(); $stmt = $con->prepare("SET foreign_key_checks = 1");
function clearTables($con) { echo "Clearing tables\n"; $productAssociatedContent = Thelia\Model\ProductAssociatedContentQuery::create()->find($con); $productAssociatedContent->delete($con); $categoryAssociatedContent = Thelia\Model\CategoryAssociatedContentQuery::create()->find($con); $categoryAssociatedContent->delete($con); $featureProduct = Thelia\Model\FeatureProductQuery::create()->find($con); $featureProduct->delete($con); $attributeCombination = Thelia\Model\AttributeCombinationQuery::create()->find($con); $attributeCombination->delete($con); $feature = Thelia\Model\FeatureQuery::create()->find($con); $feature->delete($con); $feature = Thelia\Model\FeatureI18nQuery::create()->find($con); $feature->delete($con); $featureAv = Thelia\Model\FeatureAvQuery::create()->find($con); $featureAv->delete($con); $featureAv = Thelia\Model\FeatureAvI18nQuery::create()->find($con); $featureAv->delete($con); $attribute = Thelia\Model\AttributeQuery::create()->find($con); $attribute->delete($con); $attribute = Thelia\Model\AttributeI18nQuery::create()->find($con); $attribute->delete($con); $attributeAv = Thelia\Model\AttributeAvQuery::create()->find($con); $attributeAv->delete($con); $attributeAv = Thelia\Model\AttributeAvI18nQuery::create()->find($con); $attributeAv->delete($con); $brand = Thelia\Model\BrandQuery::create()->find($con); $brand->delete($con); $brand = Thelia\Model\BrandI18nQuery::create()->find($con); $brand->delete($con); $category = Thelia\Model\CategoryQuery::create()->find($con); $category->delete($con); $category = Thelia\Model\CategoryI18nQuery::create()->find($con); $category->delete($con); $product = Thelia\Model\ProductQuery::create()->find($con); $product->delete($con); $product = Thelia\Model\ProductI18nQuery::create()->find($con); $product->delete($con); $folder = Thelia\Model\FolderQuery::create()->find($con); $folder->delete($con); $folder = Thelia\Model\FolderI18nQuery::create()->find($con); $folder->delete($con); $content = Thelia\Model\ContentQuery::create()->find($con); $content->delete($con); $content = Thelia\Model\ContentI18nQuery::create()->find($con); $content->delete($con); $accessory = Thelia\Model\AccessoryQuery::create()->find($con); $accessory->delete($con); $stock = \Thelia\Model\ProductSaleElementsQuery::create()->find($con); $stock->delete($con); $productPrice = \Thelia\Model\ProductPriceQuery::create()->find($con); $productPrice->delete($con); \Thelia\Model\ProductImageQuery::create()->find($con)->delete($con); $customer = Thelia\Model\CustomerQuery::create()->find($con); $customer->delete($con); echo "Tables cleared with success\n"; }
/** * Get the associated ChildProductImage object * * @param ConnectionInterface $con Optional Connection object. * @return ChildProductImage The associated ChildProductImage object. * @throws PropelException */ public function getProductImage(ConnectionInterface $con = null) { if ($this->aProductImage === null && $this->product_image_id !== null) { $this->aProductImage = ChildProductImageQuery::create()->findPk($this->product_image_id, $con); /* The following can be used additionally to guarantee the related object contains a reference to this object. This level of coupling may, however, be undesirable since it could result in an only partially populated collection in the referenced object. $this->aProductImage->addProductSaleElementsProductImages($this); */ } return $this->aProductImage; }
public function getAssociationResponseData($pseId, $type, $typeId) { $responseData = []; if (null !== ($msg = $this->checkFileType($type))) { throw new \Exception($msg); } $responseData["product_sale_elements_id"] = $pseId; $pse = ProductSaleElementsQuery::create()->findPk($pseId); if (null === $pse) { throw new \Exception($this->getTranslator()->trans("The product sale elements id %id doesn't exists", ["%id" => $pseId])); } $assoc = null; if ($type === "image") { $image = ProductImageQuery::create()->findPk($typeId); if (null === $image) { throw new \Exception($this->getTranslator()->trans("The product image id %id doesn't exists", ["%id" => $typeId])); } $assoc = ProductSaleElementsProductImageQuery::create()->filterByProductSaleElementsId($pseId)->findOneByProductImageId($typeId); if (null === $assoc) { $assoc = new ProductSaleElementsProductImage(); $assoc->setProductSaleElementsId($pseId)->setProductImageId($typeId)->save(); } else { $assoc->delete(); } $responseData["product_image_id"] = $typeId; $responseData["is-associated"] = (int) (!$assoc->isDeleted()); } elseif ($type === "document") { $image = ProductDocumentQuery::create()->findPk($typeId); if (null === $image) { throw new \Exception($this->getTranslator()->trans("The product document id %id doesn't exists", ["%id" => $pseId])); } $assoc = ProductSaleElementsProductDocumentQuery::create()->filterByProductSaleElementsId($pseId)->findOneByProductDocumentId($typeId); if (null === $assoc) { $assoc = new ProductSaleElementsProductDocument(); $assoc->setProductSaleElementsId($pseId)->setProductDocumentId($typeId)->save(); } else { $assoc->delete(); } $responseData["product_document_id"] = $typeId; $responseData["is-associated"] = (int) (!$assoc->isDeleted()); } elseif ($type === "virtual") { $image = ProductDocumentQuery::create()->findPk($typeId); if (null === $image) { throw new \Exception($this->getTranslator()->trans("The product document id %id doesn't exists", ["%id" => $pseId])); } $documentId = intval(MetaDataQuery::getVal('virtual', MetaData::PSE_KEY, $pseId)); if ($documentId === intval($typeId)) { $assocEvent = new MetaDataDeleteEvent('virtual', MetaData::PSE_KEY, $pseId); $this->dispatch(TheliaEvents::META_DATA_DELETE, $assocEvent); $responseData["is-associated"] = 0; } else { $assocEvent = new MetaDataCreateOrUpdateEvent('virtual', MetaData::PSE_KEY, $pseId, $typeId); $this->dispatch(TheliaEvents::META_DATA_UPDATE, $assocEvent); $responseData["is-associated"] = 1; } $responseData["product_document_id"] = $typeId; } return $responseData; }
/** * Delete a product entry * * @param \Thelia\Core\Event\Product\ProductDeleteEvent $event */ public function delete(ProductDeleteEvent $event) { if (null !== ($product = ProductQuery::create()->findPk($event->getProductId()))) { $con = Propel::getWriteConnection(ProductTableMap::DATABASE_NAME); $con->beginTransaction(); try { // Get product's files to delete after product deletion $fileList['images']['list'] = ProductImageQuery::create()->findByProductId($event->getProductId()); $fileList['images']['type'] = TheliaEvents::IMAGE_DELETE; $fileList['documentList']['list'] = ProductDocumentQuery::create()->findByProductId($event->getProductId()); $fileList['documentList']['type'] = TheliaEvents::DOCUMENT_DELETE; // Delete product $product->setDispatcher($event->getDispatcher())->delete($con); $event->setProduct($product); // Dispatch delete product's files event foreach ($fileList as $fileTypeList) { foreach ($fileTypeList['list'] as $fileToDelete) { $fileDeleteEvent = new FileDeleteEvent($fileToDelete); $event->getDispatcher()->dispatch($fileTypeList['type'], $fileDeleteEvent); } } $con->commit(); } catch (\Exception $e) { $con->rollback(); throw $e; } } }
/** * @covers \Thelia\Action\ProductSaleElement::clonePSEAssociatedFiles * @depends testUpdateClonePSE * @param array $params */ public function testClonePSEAssociatedFiles(array $params) { $event = $params['event']; $cloneProductId = $params['cloneProductId']; $clonePSEId = $params['clonePSEId']; $originalPSE = $params['originalPSE']; foreach ($event->getTypes() as $type) { switch ($type) { case 'images': $originalPSEFiles = ProductSaleElementsProductImageQuery::create()->findByProductSaleElementsId($originalPSE->getId()); // Call function to test $action = new ProductSaleElement(); $action->clonePSEAssociatedFiles($cloneProductId, $clonePSEId, $originalPSEFiles, 'image'); $originalPSEImages = ProductSaleElementsProductImageQuery::create()->findByProductSaleElementsId($originalPSE->getId()); $clonePSEImages = ProductSaleElementsProductImageQuery::create()->findByProductSaleElementsId($clonePSEId); $this->assertEquals(count($originalPSEImages), count($clonePSEImages), 'There must be the same quantity of PSE product image'); foreach ($clonePSEImages as $clonePSEImage) { $cloneProductImage = ProductImageQuery::create()->findOneById($clonePSEImage->getProductImageId()); $this->assertNotNull($cloneProductImage, 'Image linked to PSE must not be null'); $this->assertInstanceOf('Thelia\\Model\\ProductImage', $cloneProductImage, 'Instance of clone PSE\'s product image must be Thelia\\Model\\ProductImage'); $this->assertEquals($clonePSEId, $clonePSEImage->getProductSaleElementsId(), 'PSE ID must be equal'); } break; case 'documents': $originalPSEFiles = ProductSaleElementsProductDocumentQuery::create()->findByProductSaleElementsId($originalPSE->getId()); // Call function to test $action = new ProductSaleElement(); $action->clonePSEAssociatedFiles($cloneProductId, $clonePSEId, $originalPSEFiles, 'document'); $originalPSEDocuments = ProductSaleElementsProductDocumentQuery::create()->findByProductSaleElementsId($originalPSE->getId()); $clonePSEDocuments = ProductSaleElementsProductDocumentQuery::create()->findByProductSaleElementsId($clonePSEId); $this->assertEquals(count($originalPSEDocuments), count($clonePSEDocuments), 'There must be the same quantity of PSE product document'); foreach ($clonePSEDocuments as $clonePSEDocument) { $cloneProductDocument = ProductDocumentQuery::create()->findOneById($clonePSEDocument->getProductDocumentId()); $this->assertNotNull($cloneProductDocument, 'Document linked to PSE must not be null'); $this->assertInstanceOf('Thelia\\Model\\ProductDocument', $cloneProductDocument, 'Instance of clone PSE\'s product document must be Thelia\\Model\\ProductDocument'); $this->assertEquals($clonePSEId, $clonePSEDocument->getProductSaleElementsId(), 'PSE ID must be equal'); } break; } } }
$folder->delete(); $content = Thelia\Model\ContentQuery::create()->find(); $content->delete(); $content = Thelia\Model\ContentI18nQuery::create()->find(); $content->delete(); $accessory = Thelia\Model\AccessoryQuery::create()->find(); $accessory->delete(); $stock = \Thelia\Model\ProductSaleElementsQuery::create()->find(); $stock->delete(); $productPrice = \Thelia\Model\ProductPriceQuery::create()->find(); $productPrice->delete(); $brand = Thelia\Model\BrandQuery::create()->find(); $brand->delete(); $brand = Thelia\Model\BrandI18nQuery::create()->find(); $brand->delete(); \Thelia\Model\ProductImageQuery::create()->find()->delete(); \Thelia\Model\CategoryImageQuery::create()->find()->delete(); \Thelia\Model\FolderImageQuery::create()->find()->delete(); \Thelia\Model\ContentImageQuery::create()->find()->delete(); \Thelia\Model\BrandImageQuery::create()->find()->delete(); \Thelia\Model\ProductDocumentQuery::create()->find()->delete(); \Thelia\Model\CategoryDocumentQuery::create()->find()->delete(); \Thelia\Model\FolderDocumentQuery::create()->find()->delete(); \Thelia\Model\ContentDocumentQuery::create()->find()->delete(); \Thelia\Model\BrandDocumentQuery::create()->find()->delete(); \Thelia\Model\CouponQuery::create()->find()->delete(); \Thelia\Model\OrderQuery::create()->find()->delete(); \Thelia\Model\SaleQuery::create()->find()->delete(); \Thelia\Model\SaleProductQuery::create()->find()->delete(); \Thelia\Model\MetaDataQuery::create()->find()->delete(); $stmt = $con->prepare("SET foreign_key_checks = 1");
public function clonePSEAssociatedFiles($clonedProductId, $clonedProductPSEId, $originalProductPSEFiles, $type) { /** @var ProductSaleElementsDocument|ProductSaleElementsImage $originalProductPSEFile */ foreach ($originalProductPSEFiles as $originalProductPSEFile) { $originalProductFilePositionQuery = []; $originalProductPSEFileId = null; // Get file's original position switch ($type) { case 'image': $originalProductFilePositionQuery = ProductImageQuery::create(); $originalProductPSEFileId = $originalProductPSEFile->getProductImageId(); break; case 'document': $originalProductFilePositionQuery = ProductDocumentQuery::create(); $originalProductPSEFileId = $originalProductPSEFile->getProductDocumentId(); break; } $originalProductFilePosition = $originalProductFilePositionQuery->select(['POSITION'])->findPk($originalProductPSEFileId); // Get cloned file ID to link to the cloned PSE switch ($type) { case 'image': $clonedProductFileIdToLinkToPSEQuery = ProductImageQuery::create(); break; case 'document': $clonedProductFileIdToLinkToPSEQuery = ProductDocumentQuery::create(); break; } $clonedProductFileIdToLinkToPSE = $clonedProductFileIdToLinkToPSEQuery->filterByProductId($clonedProductId)->filterByPosition($originalProductFilePosition)->select(['ID'])->findOne(); // Save association switch ($type) { case 'image': $assoc = new ProductSaleElementsProductImage(); $assoc->setProductImageId($clonedProductFileIdToLinkToPSE); break; case 'document': $assoc = new ProductSaleElementsProductDocument(); $assoc->setProductDocumentId($clonedProductFileIdToLinkToPSE); break; } $assoc->setProductSaleElementsId($clonedProductPSEId)->save(); } }
/** * Returns the number of related ProductImage objects. * * @param Criteria $criteria * @param boolean $distinct * @param ConnectionInterface $con * @return int Count of related ProductImage objects. * @throws PropelException */ public function countProductImages(Criteria $criteria = null, $distinct = false, ConnectionInterface $con = null) { $partial = $this->collProductImagesPartial && !$this->isNew(); if (null === $this->collProductImages || null !== $criteria || $partial) { if ($this->isNew() && null === $this->collProductImages) { return 0; } if ($partial && !$criteria) { return count($this->getProductImages()); } $query = ChildProductImageQuery::create(null, $criteria); if ($distinct) { $query->distinct(); } return $query->filterByProduct($this)->count($con); } return count($this->collProductImages); }
public function cloneFile(ProductCloneEvent $event, $eventName, EventDispatcherInterface $dispatcher) { $originalProductId = $event->getOriginalProduct()->getId(); $clonedProduct = $event->getClonedProduct(); foreach ($event->getTypes() as $type) { $originalProductFiles = []; switch ($type) { case 'images': $originalProductFiles = ProductImageQuery::create()->findByProductId($originalProductId); break; case 'documents': $originalProductFiles = ProductDocumentQuery::create()->findByProductId($originalProductId); break; } // Set clone's files /** @var ProductDocument|ProductImage $originalProductFile */ foreach ($originalProductFiles as $originalProductFile) { $srcPath = $originalProductFile->getUploadDir() . DS . $originalProductFile->getFile(); if (file_exists($srcPath)) { $ext = pathinfo($srcPath, PATHINFO_EXTENSION); $clonedProductFile = []; switch ($type) { case 'images': $fileName = $clonedProduct->getRef() . '.' . $ext; $clonedProductFile = new ProductImage(); break; case 'documents': $fileName = pathinfo($originalProductFile->getFile(), PATHINFO_FILENAME) . '-' . $clonedProduct->getRef() . '.' . $ext; $clonedProductFile = new ProductDocument(); break; } // Copy a temporary file of the source file as it will be deleted by IMAGE_SAVE or DOCUMENT_SAVE event $srcTmp = $srcPath . '.tmp'; copy($srcPath, $srcTmp); // Get file mimeType $finfo = new \finfo(); $fileMimeType = $finfo->file($srcPath, FILEINFO_MIME_TYPE); // Get file event's parameters $clonedProductFile->setProductId($clonedProduct->getId())->setVisible($originalProductFile->getVisible())->setPosition($originalProductFile->getPosition())->setLocale($clonedProduct->getLocale())->setTitle($clonedProduct->getTitle()); $clonedProductCopiedFile = new UploadedFile($srcPath, $fileName, $fileMimeType, filesize($srcPath), null, true); // Create and dispatch event $clonedProductCreateFileEvent = new FileCreateOrUpdateEvent($clonedProduct->getId()); $clonedProductCreateFileEvent->setModel($clonedProductFile)->setUploadedFile($clonedProductCopiedFile)->setParentName($clonedProduct->getTitle()); $originalProductFileI18ns = []; switch ($type) { case 'images': $dispatcher->dispatch(TheliaEvents::IMAGE_SAVE, $clonedProductCreateFileEvent); // Get original product image I18n $originalProductFileI18ns = ProductImageI18nQuery::create()->findById($originalProductFile->getId()); break; case 'documents': $dispatcher->dispatch(TheliaEvents::DOCUMENT_SAVE, $clonedProductCreateFileEvent); // Get original product document I18n $originalProductFileI18ns = ProductDocumentI18nQuery::create()->findById($originalProductFile->getId()); break; } // Set temporary source file as original one rename($srcTmp, $srcPath); // Clone file's I18n $this->cloneFileI18n($originalProductFileI18ns, $clonedProductFile, $type, $event, $dispatcher); } else { Tlog::getInstance()->addWarning("Failed to find media file {$srcPath}"); } } } }
/** * Performs an INSERT on the database, given a ProductImage or Criteria object. * * @param mixed $criteria Criteria or ProductImage object containing data that is used to create the INSERT statement. * @param ConnectionInterface $con the ConnectionInterface connection to use * @return mixed The new primary key. * @throws PropelException Any exceptions caught during processing will be * rethrown wrapped into a PropelException. */ public static function doInsert($criteria, ConnectionInterface $con = null) { if (null === $con) { $con = Propel::getServiceContainer()->getWriteConnection(ProductImageTableMap::DATABASE_NAME); } if ($criteria instanceof Criteria) { $criteria = clone $criteria; // rename for clarity } else { $criteria = $criteria->buildCriteria(); // build Criteria from ProductImage object } if ($criteria->containsKey(ProductImageTableMap::ID) && $criteria->keyContainsValue(ProductImageTableMap::ID)) { throw new PropelException('Cannot insert a value for auto-increment primary key (' . ProductImageTableMap::ID . ')'); } // Set the correct dbName $query = ProductImageQuery::create()->mergeWith($criteria); try { // use transaction because $criteria could contain info // for more than one table (I guess, conceivably) $con->beginTransaction(); $pk = $query->doInsert($con); $con->commit(); } catch (PropelException $e) { $con->rollBack(); throw $e; } return $pk; }
/** * Removes this object from datastore and sets delete attribute. * * @param ConnectionInterface $con * @return void * @throws PropelException * @see ProductImage::setDeleted() * @see ProductImage::isDeleted() */ public function delete(ConnectionInterface $con = null) { if ($this->isDeleted()) { throw new PropelException("This object has already been deleted."); } if ($con === null) { $con = Propel::getServiceContainer()->getWriteConnection(ProductImageTableMap::DATABASE_NAME); } $con->beginTransaction(); try { $deleteQuery = ChildProductImageQuery::create()->filterByPrimaryKey($this->getPrimaryKey()); $ret = $this->preDelete($con); if ($ret) { $deleteQuery->delete($con); $this->postDelete($con); $con->commit(); $this->setDeleted(true); } else { $con->commit(); } } catch (Exception $e) { $con->rollBack(); throw $e; } }
public function testCopyUploadedFileProductImage() { $this->dotTestImageUpload(ProductImageQuery::create()->findOne(), 'product'); }
public function testSearchByProductId() { $image = ProductImageQuery::create()->findOne(); $this->baseTestSearchById($image->getId(), array('source' => 'product')); }