Esempio n. 1
0
 public static function getMediaObject(Image $media)
 {
     $annotations = array();
     foreach ($media->getAnnotations() as $annotation) {
         $annotations[] = array('annotation' => JSEntities::getAnnotationObject($annotation));
     }
     return array('id' => $media->getId(), 'title' => $media->getTitle(), 'metaData' => JSEntities::getMetaDataObject($media->getMetaData()), 'resource' => JSEntities::getResourceObject($media->getResource()), 'annotations' => $annotations);
 }
 public function addImagesAction(Request $request)
 {
     $user = $this->getUser();
     if (!$this->container->get('image_annotator.authentication_manager')->isAuthenticated($request)) {
         return $this->redirect($this->generateUrl('fos_user_security_login'));
     }
     $userManager = $this->container->get('fos_user.user_manager');
     $userObject = $userManager->findUserByUsername($user->getUsername());
     if ($userObject == null) {
         throw new NotFoundHttpException("This user does not exist");
     }
     $imageMedia = array();
     $formFactory = $this->container->get('form.factory');
     $form = $formFactory->create(new ImageMediaMultipleFormType(), $imageMedia, array());
     $logger = $this->container->get('logger');
     // if ($request->isXmlHttpRequest ()) {
     // throw new BadRequestHttpException();
     // }
     // Debug::dump($request->files->get('image_annotator_image_media')['resource']['file']);
     if ('POST' === $request->getMethod()) {
         $form->bind($request);
         // Debug::dump($request->files->get('image_annotator_image_media')['resource']['file']);
         $data = $form->getData();
         $dataset = $data['dataset'];
         $titles = $data['titles'];
         $resources = $request->files->get('image_annotator_image_media')['resource']['file'];
         // Debug::dump($resources);
         // Debug::dump($titles);
         // Debug::dump($request->files->get('image_annotator_image_media'));
         // $logger->info($dataset->getId());
         if ($form->isValid()) {
             $logger->info('Form isvalid');
             // flush object to database
             $eventDispatcher = $this->container->get('event_dispatcher');
             $em = $this->container->get('doctrine')->getManager();
             $images = array();
             $files = $resources;
             for ($i = 0; $i < count($files); $i++) {
                 $image = new Image();
                 $image->setDataset($dataset);
                 $resource = new ResourceFile();
                 $resource->setFile($files[$i]);
                 $resource->setMedia($image);
                 $image->setResource($resource);
                 $image->setTitle($titles[$i]);
                 $em->persist($resource);
                 $em->persist($image);
                 $images[] = $image;
                 // $uploadedEvent = new UploadEvent ( $image );
                 // $eventDispatcher->dispatch ( UploadEvent::EVENT_UPLOAD, $uploadedEvent );
             }
             $em->flush();
             $jsImages = array();
             foreach ($images as $image) {
                 $uploadedEvent = new UploadEvent($image);
                 $eventDispatcher->dispatch(UploadEvent::EVENT_UPLOAD, $uploadedEvent);
                 $jsImages[] = JSEntities::getMediaObject($image);
             }
             // $this->container->get ( 'session' )->getFlashBag ()->add ( 'media', 'Image file uploaded successfully!' );
             // $uploadedEvent = new UploadEvent ( $imageMedia );
             // $eventDispatcher->dispatch ( UploadEvent::EVENT_UPLOAD, $uploadedEvent );
             // $uploadedEvent->getResponse();
             if ($request->isXmlHttpRequest()) {
                 $response = array('page' => null, 'finished' => true, 'media' => $jsImages);
                 $response = json_encode($response);
                 // json encode the array
                 return new Response($response, 200, array('Content-Type' => 'application/json'));
             } else {
                 $response = new RedirectResponse($this->generateUrl('image_annotator_annotations_list_images'));
             }
             return $response;
         }
         // foreach ($form->getChildren() as $child)
         // {
         $logger->info("form is invalid");
         $logger->info($form->getErrorsAsString());
         // }
     }
     // form not valid, show the basic form
     if ($request->isXmlHttpRequest()) {
         $return = array('page' => null, 'finished' => false);
         $return = json_encode($return);
         // json encode the array
         $response = new Response($return, 400, array('Content-Type' => 'application/json'));
     } else {
         $response = $this->render('ImageAnnotatorBundle:AddFileGateway:' . 'addFile.html.twig', array('form' => $form->createView(), 'postUrl' => $this->generateUrl('image_annotator_image_add_image')));
     }
     return $response;
 }
 /**
  * Action to export a specific dataset as a json array
  *
  * @param Request $request        	
  * @param unknown $datasetId        	
  * @return \martingerdzhev\ImageAnnotatorBundle\Controller\StreamedResponse
  */
 public function exportAction(Request $request, $datasetId)
 {
     if (!$this->container->get('image_annotator.authentication_manager')->isAuthenticated($request)) {
         return $this->redirect($this->generateUrl('fos_user_security_login'));
     }
     $em = $this->get('doctrine')->getManager();
     $dataset = $em->getRepository('ImageAnnotatorBundle:Dataset')->find($datasetId);
     $responseURL = "";
     if ($dataset == null) {
         throw new EntityNotFoundException("Cannot find dataset with that ID");
     }
     $return = array('dataset' => JSEntities::exportDatasetObject($dataset));
     // get the service container to pass to the closure
     $return = json_encode($return);
     $return = str_replace('uploads\\/', '', $return);
     $response = new Response($return, 200);
     $response->headers->set('Content-Type', 'application/force-download');
     $response->headers->set('Content-Disposition', 'attachment; filename="export.json"');
     return $response;
 }
 /**
  * An Ajax function that previews a media with a specific media ID
  *
  * @param Request $request        	
  * @param unknown_type $mediaId        	
  */
 public function previewMediaAction(Request $request, $mediaId)
 {
     if (!$this->container->get('image_annotator.authentication_manager')->isAuthenticated($request)) {
         return $this->redirect($this->generateUrl('fos_user_security_login'));
     }
     if (!$request->isXmlHttpRequest()) {
         throw new BadRequestHttpException('Only Ajax POST calls accepted');
     }
     $user = $this->getUser();
     $em = $this->get('doctrine')->getManager();
     /**
      *
      * @var $media martingerdzhev\ImageAnnotatorBundle\Entity\Image
      */
     $media = $em->getRepository('ImageAnnotatorBundle:Image')->find($mediaId);
     $responseURL = "";
     if ($request->isXmlHttpRequest()) {
         $prefix = "ajax.";
     }
     // form not valid, show the basic form
     if ($media !== null) {
         $responseURL = 'ImageAnnotatorBundle:Media:' . $prefix . 'previewImage.html.twig';
     } else {
         throw new EntityNotFoundException("Cannot find media with that ID");
     }
     $response = $this->render($responseURL, array('mediaFile' => $media));
     if ($request->isXmlHttpRequest()) {
         $return = array('page' => $response->getContent(), 'media' => JSEntities::getMediaObject($media));
         $return = json_encode($return);
         // json encode the array
         $response = new Response($return, 200, array('Content-Type' => 'application/json'));
     }
     return $response;
 }
 /**
  * An Ajax function that previews a media with a specific media ID
  *
  * @param Request $request        	
  * @param unknown_type $mediaId        	
  */
 public function viewAction(Request $request, $imageId)
 {
     if (!$this->container->get('image_annotator.authentication_manager')->isAuthenticated($request)) {
         return $this->redirect($this->generateUrl('fos_user_security_login'));
     }
     // 		if (! $request->isXmlHttpRequest())
     // 			throw new BadRequestHttpException('Only Ajax POST calls accepted');
     $user = $this->getUser();
     $em = $this->get('doctrine')->getManager();
     /**
      *
      * @var $media martingerdzhev\ImageAnnotatorBundle\Entity\Image
      */
     $media = $em->getRepository('ImageAnnotatorBundle:Image')->find($imageId);
     $responseURL = "";
     $prefix = '';
     if ($request->isXmlHttpRequest()) {
         $prefix = "ajax.";
     }
     // form not valid, show the basic form
     if ($media !== null) {
         $responseURL = 'ImageAnnotatorBundle:ImageGateway:' . $prefix . 'view.html.twig';
         $images = $media->getDataset()->getImages();
         $previousImage = -1;
         $nextImage = -1;
         for ($i = 0; $i < count($images); $i++) {
             if ($images[$i]->getId() == $media->getId()) {
                 if ($i > 0) {
                     $previousImage = $images[$i - 1]->getId();
                 }
                 if ($i < count($images) - 1) {
                     $nextImage = $images[$i + 1]->getId();
                 }
                 break;
             }
         }
     } else {
         throw new EntityNotFoundException("Cannot find media with that ID");
     }
     $response = $this->render($responseURL, array('mediaFile' => $media, 'previousImageId' => $previousImage, 'nextImageId' => $nextImage));
     if ($request->isXmlHttpRequest()) {
         $return = array('page' => $response->getContent(), 'media' => JSEntities::getMediaObject($media), 'previousImageId' => $previousImage, 'nextImageId' => $nextImage);
         $return = json_encode($return);
         // json encode the array
         $response = new Response($return, 200, array('Content-Type' => 'application/json'));
     }
     return $response;
 }