Example #1
0
 /**
  * This method moves a uploaded file into a permanent folder.
  */
 public function moveFileAction($campaignId, $tmpData)
 {
     // Create an instance of the Campaign:
     $campaign = $this->getDoctrine()->getRepository('CampaignBundle:Campaign')->findOneById($campaignId);
     // Populate the File object with data:
     $file = new ProjectFile();
     // Set temporary file to be of UploadedFile type:
     $temporaryFile = new File(__DIR__ . '/../../../../../web/uploads/tmp/' . 'tmp_' . $tmpData['tmpUuid'] . '.tmp');
     $file->setFile($temporaryFile);
     $file->setUuid(Uuid::uuid4()->toString());
     $file->setOriginalName($tmpData['fileName']);
     $file->setContentType($file->getFileExtension($file->getOriginalName()));
     $file->setFileLength($tmpData['fileLength']);
     $file->setFileName($file->getUuid() . $file->getContentType());
     $file->setNotVisible(FALSE);
     // Specify User Creator:
     $file->setUser($this->getUser());
     // Set time for when the file was created:
     $creationDate = new \DateTime();
     $creationDate->setTimezone(self::timezoneUTC());
     $file->setCreatedAt($creationDate);
     $file->setUpdatedAt($creationDate);
     // Set Matrix file version and uuid:
     if ($campaign->getMatrixfileUuid() == null) {
         // Matrix file not yet created.
         $file->setVersion(1);
         $campaign->setMatrixfileUuid($file->getUuid());
         $campaign->setMatrixfileVersion($file->getVersion());
     } else {
         // Increment matrix file version.
         // Retrieve latest version number of matrix file for this campaign:
         $fileRepository = $this->getDoctrine()->getRepository('FileBundle:File');
         $versionQuery = $fileRepository->createQueryBuilder('f')->where('f.campaign = :campaign and f.task IS NULL')->setParameter('campaign', $campaign)->orderBy('f.version', 'DESC')->setMaxResults(1)->getQuery();
         $latestMatrixVersion = $versionQuery->getSingleResult()->getVersion();
         // Set file version:
         $file->setVersion($latestMatrixVersion + 1);
         $campaign->setMatrixfileUuid($file->getUuid());
         $campaign->setMatrixfileVersion($file->getVersion());
     }
     // End of version IF.
     $file->setCampaign($campaign);
     // Get validator service to check for errors:
     $validator = $this->get('validator');
     $errors = $validator->validate($file);
     $response = new Response();
     // Create and prepare Response object to be sent back to client.
     // Validate the values entered in the File object:
     if (count($errors) > 0) {
         $ok = false;
         // Return $errors in JSON format:
         $serializer = $this->get('jms_serializer');
         $serializer->serialize($errors, 'json');
         return array($errors, $ok);
     }
     // End of errors IF.
     // If no errors were found, handle the File data:
     $ok = true;
     $em = $this->getDoctrine()->getManager();
     $targetDir = 'campaign_files/' . $campaign->getId();
     // Specify target directory for file.
     $file->upload($targetDir);
     // Move matrix file in target directory.
     $campaign->setUpdatedAt($creationDate);
     $em->persist($file);
     $em->flush();
     return array($file->getUuid(), $ok);
 }