예제 #1
0
 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;
 }
예제 #2
0
 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();
     }
 }