Ejemplo n.º 1
0
 public function testSetAddedByUser()
 {
     $photo = new Photo();
     $user = new User();
     $photo->setAddedByUser($user);
     $this->assertEquals($user, $photo->getAddedByUser());
 }
Ejemplo n.º 2
0
 /**
  * Uploads all the photos when OK i pressed on the upload photo form.
  * @return Response
  * @throws \Exception
  */
 public function uploadAction()
 {
     $request = Request::createFromGlobals();
     //Get the target folder for the photo from paramters.yml
     $output_dir = $this->container->getParameter('gallery_folder') . '/';
     //Create a FileUploader
     $uploader = new FileUploader($output_dir, $this->allowed_file_types);
     //Upload the files aand get the new names created for each file
     $mappedFileNames = $uploader->upload($request);
     //Now all the files are copied to their destination folder
     //The array $mappedFileNames contains entries: $orginalFileName => fileNameWithPathOnSystemNow
     $responseText = '<html> Lastet opp følgende filer: <br>';
     $em = $this->getDoctrine()->getManager();
     foreach ($mappedFileNames as $key => $val) {
         $responseText .= $key . " to " . $val . "<br>";
         //Update the database
         //The text sent with the picture is a parameter with the same name as image + '_text'
         //http://stackoverflow.com/questions/68651/get-php-to-stop-replacing-characters-in-get-or-post-arrays
         //todo: the above SO thing is annoying.
         $commentParameterName = str_replace(".", "_", $key);
         $commentParameterName = str_replace(" ", "_", $commentParameterName);
         $text = $request->get($commentParameterName . '_text');
         $takenDate = $request->get($commentParameterName . '_taken_date_text');
         //Get taken date
         //Get the gallery id
         $gallery_id = $request->get('gallery_id');
         //Create an instance of the entity
         $photo = new Photo();
         $photo->setComment($text);
         $gallery = $this->getDoctrine()->getRepository("AppBundle:Gallery")->find($gallery_id);
         $photo->setGallery($gallery);
         $photo->setDateAdded(new \DateTime("now"));
         $photo->setDateTaken(new \DateTime($takenDate));
         $photo->setAddedByUser($this->get('security.context')->getToken()->getUser());
         $photo->setPathToFile($val);
         //Manage
         $em->persist($photo);
     }
     //Persist
     $em->flush();
     $responseText = $responseText . '</html>';
     return new Response($responseText);
 }