public function testAssociatePSEDocument()
 {
     /**
      * Get a product sale elements which has a related product image
      */
     $pse = ProductSaleElementsQuery::create()->useProductQuery()->joinProductDocument()->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
      */
     $productDocument = ProductDocumentQuery::create()->findOneByProductId($pse->getProductId());
     $association = ProductSaleElementsProductDocumentQuery::create()->filterByProductSaleElements($pse)->findOneByProductDocumentId($productDocument->getId());
     $isAssociated = $association !== null;
     $this->controller->getAssociationResponseData($pse->getId(), "document", $productDocument->getId());
     $newAssociation = ProductSaleElementsProductDocumentQuery::create()->filterByProductSaleElements($pse)->findOneByProductDocumentId($productDocument->getId());
     $isNowAssociated = $newAssociation !== null;
     $this->assertFalse($isAssociated === $isNowAssociated);
 }
 /**
  * this method returns a Propel ModelCriteria
  *
  * @return \Propel\Runtime\ActiveQuery\ModelCriteria
  */
 public function buildModelCriteria()
 {
     $query = ProductSaleElementsProductDocumentQuery::create();
     if (null !== ($id = $this->getId())) {
         $query->filterById($id);
     }
     if (null !== ($pseId = $this->getProductSaleElementsId())) {
         $query->filterByProductSaleElementsId($pseId);
     }
     if (null !== ($productDocumentId = $this->getProductDocumentId())) {
         $query->filterByProductDocumentId($id);
     }
     foreach ($this->getOrder() as $order) {
         switch ($order) {
             case "position":
                 $query->useProductDocumentQuery()->orderByPosition(Criteria::ASC)->endUse();
                 break;
             case "position-reverse":
                 $query->useProductDocumentQuery()->orderByPosition(Criteria::DESC)->endUse();
                 break;
         }
     }
     return $query;
 }
 /**
  * Performs an INSERT on the database, given a ProductSaleElementsProductDocument or Criteria object.
  *
  * @param mixed               $criteria Criteria or ProductSaleElementsProductDocument 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(ProductSaleElementsProductDocumentTableMap::DATABASE_NAME);
     }
     if ($criteria instanceof Criteria) {
         $criteria = clone $criteria;
         // rename for clarity
     } else {
         $criteria = $criteria->buildCriteria();
         // build Criteria from ProductSaleElementsProductDocument object
     }
     if ($criteria->containsKey(ProductSaleElementsProductDocumentTableMap::ID) && $criteria->keyContainsValue(ProductSaleElementsProductDocumentTableMap::ID)) {
         throw new PropelException('Cannot insert a value for auto-increment primary key (' . ProductSaleElementsProductDocumentTableMap::ID . ')');
     }
     // Set the correct dbName
     $query = ProductSaleElementsProductDocumentQuery::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 ProductSaleElementsProductDocument::setDeleted()
  * @see ProductSaleElementsProductDocument::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(ProductSaleElementsProductDocumentTableMap::DATABASE_NAME);
     }
     $con->beginTransaction();
     try {
         $deleteQuery = ChildProductSaleElementsProductDocumentQuery::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;
     }
 }
 /**
  * If this collection has already been initialized with
  * an identical criteria, it returns the collection.
  * Otherwise if this ProductSaleElements is new, it will return
  * an empty collection; or if this ProductSaleElements has previously
  * been saved, it will retrieve related ProductSaleElementsProductDocuments from storage.
  *
  * This method is protected by default in order to keep the public
  * api reasonable.  You can provide public methods for those you
  * actually need in ProductSaleElements.
  *
  * @param      Criteria $criteria optional Criteria object to narrow the query
  * @param      ConnectionInterface $con optional connection object
  * @param      string $joinBehavior optional join type to use (defaults to Criteria::LEFT_JOIN)
  * @return Collection|ChildProductSaleElementsProductDocument[] List of ChildProductSaleElementsProductDocument objects
  */
 public function getProductSaleElementsProductDocumentsJoinProductDocument($criteria = null, $con = null, $joinBehavior = Criteria::LEFT_JOIN)
 {
     $query = ChildProductSaleElementsProductDocumentQuery::create(null, $criteria);
     $query->joinWith('ProductDocument', $joinBehavior);
     return $this->getProductSaleElementsProductDocuments($query, $con);
 }
 protected function getPSEDocuments(ProductSaleElementsModel $pse)
 {
     /**
      * Compute documents with the associated loop
      */
     $documentLoop = new Document($this->container);
     $documentLoop->initializeArgs(["product" => $pse->getProductId(), "visible" => BooleanOrBothType::ANY]);
     $documents = $documentLoop->exec($documentPagination);
     $documentAssoc = ProductSaleElementsProductDocumentQuery::create()->useProductSaleElementsQuery()->filterById($pse->getId())->endUse()->find()->toArray();
     $data = [];
     /** @var \Thelia\Core\Template\Element\LoopResultRow $document */
     for ($documents->rewind(); $documents->valid(); $documents->next()) {
         $document = $documents->current();
         $isAssociated = $this->arrayHasEntries($documentAssoc, ["ProductDocumentId" => $document->get("ID"), "ProductSaleElementsId" => $pse->getId()]);
         $data[] = ["id" => $document->get("ID"), "url" => $document->get("DOCUMENT_URL"), "title" => $document->get("TITLE"), "is_associated" => $isAssociated, "filename" => $document->model->getFile()];
     }
     return $data;
 }
 /**
  * @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;
         }
     }
 }
 /**
  * Clone product's PSEs and associated datas
  *
  * @param ProductCloneEvent $event
  */
 public function clonePSE(ProductCloneEvent $event)
 {
     $clonedProduct = $event->getClonedProduct();
     // Get original product's PSEs
     $originalProductPSEs = ProductSaleElementsQuery::create()->orderByIsDefault(Criteria::DESC)->findByProductId($event->getOriginalProduct()->getId());
     /**
      * Handle PSEs
      *
      * @var int  $key
      * @var ProductSaleElements $originalProductPSE
      */
     foreach ($originalProductPSEs as $key => $originalProductPSE) {
         $currencyId = ProductPriceQuery::create()->filterByProductSaleElementsId($originalProductPSE->getId())->select('CURRENCY_ID')->findOne();
         // The default PSE, created at the same time as the clone product, is overwritten
         $clonedProductPSEId = $this->createClonePSE($event, $originalProductPSE, $currencyId);
         $this->updateClonePSE($event, $clonedProductPSEId, $originalProductPSE, $key);
         // PSE associated images
         $originalProductPSEImages = ProductSaleElementsProductImageQuery::create()->findByProductSaleElementsId($originalProductPSE->getId());
         if (null !== $originalProductPSEImages) {
             $this->clonePSEAssociatedFiles($clonedProduct->getId(), $clonedProductPSEId, $originalProductPSEImages, $type = 'image');
         }
         // PSE associated documents
         $originalProductPSEDocuments = ProductSaleElementsProductDocumentQuery::create()->findByProductSaleElementsId($originalProductPSE->getId());
         if (null !== $originalProductPSEDocuments) {
             $this->clonePSEAssociatedFiles($clonedProduct->getId(), $clonedProductPSEId, $originalProductPSEDocuments, $type = 'document');
         }
     }
 }