Esempio n. 1
0
 public function sponsorsUpdateAction(Request $request, $id)
 {
     //Get the Sponsor object from the database
     $sponsor = $this->getDoctrine()->getRepository('AppBundle:Sponsor')->find($id);
     //Get the entity manager
     $em = $this->getDoctrine()->getEntityManager();
     if (array_key_exists('delete', $request->request->get('sponsor'))) {
         //Delete the sponsor object from the database
         $em->remove($sponsor);
     } else {
         //Update the sponsor object in the database
         //Empty file-field in the form is allowed
         if ($request->files->get('sponsor')['logoImagePath'] != null) {
             //First move the logo image file to its folder
             $targetFolder = $this->container->getParameter('logo_images') . '/';
             //Create a FileUploader with target folder and allowed file types as parameters
             $uploader = new FileUploader($targetFolder, ['image/gif', 'image/jpeg', 'image/png']);
             //Move the file to target folder
             $result = $uploader->upload($request);
             //return new Response(var_dump($result));
             //Get the path of the image file as now on the server:  todo: now assumes only one image is contained in the request, as it should be.
             $path = $result[array_keys($result)[0]];
             //todo: duplicated code this line and those above it. see editProfilePhotoAction in ProfileController
             $sponsor->setLogoImagePath($path);
         }
         //Get the sponsor name from the request (value entered in the form)
         $name = $request->request->get('sponsor')['name'];
         $sponsor->setName($name);
         //Get the sponsor's url
         $url = $request->request->get('sponsor')['url'];
         $sponsor->setUrl($url);
         //Save to database
         $em->persist($sponsor);
     }
     $em->flush();
     return $this->redirectToRoute('sponsors_edit');
 }
Esempio 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);
 }