Ejemplo n.º 1
0
 public function preUpload(PreUploadEvent $event)
 {
     //echo "preUpload";die();
     /*$response = $event->getResponse();
       $msg = "test";
       $response->setSuccess(false);
       $response->setError($msg);*/
     $galleryId = $event->getRequest()->get('galleryId');
     $file = $event->getFile();
     $media = new Media();
     //$media->setName($file->getBasename());
     $media->setName($file->getClientOriginalName());
     $media->setBinaryContent($file);
     $media->setEnabled(true);
     $media->setContext('default');
     $media->setProviderName('sonata.media.provider.image');
     $this->em = $this->doctrine[0]->getEntityManager();
     $gallery = $this->em->getRepository('ApplicationSonataMediaBundle:Gallery')->find($galleryId);
     $galleryHasMedia = new GalleryHasMedia();
     $galleryHasMedia->setMedia($media);
     $galleryHasMedia->setEnabled(true);
     $gallery->addGalleryHasMedias($galleryHasMedia);
     $this->em->persist($galleryHasMedia);
     $this->em->persist($media);
     $this->em->flush();
     die;
 }
Ejemplo n.º 2
0
 public function getMedia($name, $context = 'default')
 {
     $media = new Media();
     $media->setBinaryContent(__DIR__ . '/../data/' . $name);
     $media->setContext($context);
     $media->setProviderName('sonata.media.provider.image');
     $this->container->get('sonata.media.manager.media')->save($media, $andFlush = true);
     return $media;
 }
Ejemplo n.º 3
0
 protected function addCategory($message)
 {
     $categoryName = $message->getValue('categoryName');
     $gallery = $message->getValue('gallery');
     $filePath = $message->getValue('filePath');
     $donwnloadable = $message->getValue('donwnloadable');
     $prefixNameForm = $message->getValue('prefix_name');
     $category = new PhotoCategory();
     $category->setName($categoryName);
     $galleries = $this->enityManager->getRepository('AldorGalleryBundle:Gallery')->findAll();
     foreach ($galleries as $gal) {
         if ($gal->getName() == $gallery) {
             $category->setGallery($gal);
             break;
         }
     }
     $zip = new ExtractZip($filePath);
     $files = $zip->extract();
     if ($donwnloadable) {
         $zipFile = new Media();
         $zipFile->setBinaryContent($zip->getZipPath());
         $zipFile->setEnabled(true);
         $zipFile->setContext('default');
         $zipFile->setProviderName('sonata.media.provider.file');
         $category->setZip($zipFile);
     }
     $this->enityManager->persist($category);
     $prefixName = $categoryName;
     if ($prefixNameForm && $prefixNameForm != '') {
         $prefixName = $prefixNameForm;
     }
     $counter = 0;
     foreach ($files as $pos => $file) {
         $title = explode('/', $pos);
         $fileName = $title[count($title) - 1];
         $fileName = str_replace('DSC_', '', $fileName);
         $title = $prefixName . '-' . explode('.', $fileName)[0];
         $photo = new Photo();
         $photo->setTitle($title);
         $media = new Media();
         $media->setBinaryContent($file);
         $media->setEnabled(true);
         $media->setContext('gallery');
         $media->setName($categoryName . '-' . $fileName);
         $photo->setImage($media);
         $photo->addCategory($category);
         // if (count($photo->getCategories()) == 0) {
         //     print_r("jksdrgt-------------");
         //     throw "err";
         //     return 12313;
         // }
         $this->enityManager->persist($photo);
     }
     $this->enityManager->flush();
     $zip->clean();
 }
Ejemplo n.º 4
0
 public function generateMediaFromFile($file)
 {
     $media = new Media();
     $media->setBinaryContent(new File($file));
     $media->setContext('default');
     $media->setProviderName('sonata.media.provider.image');
     $media->setName($this->getFileName($file));
     $media->setEnabled(true);
     return $media;
 }
 /**
  * @param UploadedFile $uploadedFile
  * @param string $context
  * @return Image
  */
 private function saveImage(UploadedFile $uploadedFile, $context)
 {
     $media = new Media();
     $media->setBinaryContent($uploadedFile);
     $media->setContext($context);
     $media->setProviderName('sonata.media.provider.image');
     $this->mediaManager->save($media);
     $image = new Image();
     $image->setFile($media);
     $this->imageManager->save($image);
     return $image;
 }
 public function testExecuteCommand()
 {
     $kernel = $this->createKernel();
     $kernel->boot();
     $application = new Application($kernel);
     $application->add(new RemoveBrokenMediaObjectsCommand());
     $command = $application->find('app:remove-image');
     $commandTester = new CommandTester($command);
     $commandTester->execute([]);
     $this->assertContains("Deleted media without reference object was successful", $commandTester->getDisplay());
     $commandTester->execute([]);
     $this->assertNotContains("Remove media with", $commandTester->getDisplay());
     $media = new Media();
     $media->setBinaryContent(__DIR__ . '/../../DataFixtures/data/avatars/anna-bobrova.jpg');
     $media->setContext('employee');
     $media->setProviderName('sonata.media.provider.image');
     $this->getContainer()->get('sonata.media.manager.media')->save($media, $andFlush = true);
     $this->getEm()->persist($media);
     $this->getEm()->flush();
     $commandTester->execute([]);
     $this->assertContains("Removed media with id", $commandTester->getDisplay());
 }
Ejemplo n.º 7
0
 /**
  * @Route("/media/upload/excerpt", name="ed_blog_admin_media_upload_excerpt")
  */
 public function uploadExcerptAction(Request $request)
 {
     $user = $this->getBlogUser();
     $mediaManager = $this->container->get('sonata.media.manager.media');
     $builder = $this->createFormBuilder();
     $form = $builder->add('excerptImage', 'file', array('required' => false))->getForm();
     if ($request->getMethod() == 'POST') {
         $form->handleRequest($request);
         $attachment = $form['excerptImage']->getData();
         $media = new Media();
         $media->setBinaryContent($attachment);
         $media->setContext('default');
         $media->setProviderName('sonata.media.provider.image');
         $media->setName($attachment->getClientOriginalName());
         $mediaManager->save($media);
         return new JsonResponse(array('success' => 'true', 'id' => $media->getId(), 'href' => $this->generateUrl('ed_blog_admin_media_delete', array('id' => $media->getId())), 'media' => $this->renderView('EDBlogBundle:Media:image.html.twig', array('media' => $media))));
     }
     return new JsonResponse(array('success' => 'false'));
 }
 /**
  * {@inheritDoc}
  */
 public function setProviderName($providerName)
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'setProviderName', array($providerName));
     return parent::setProviderName($providerName);
 }
Ejemplo n.º 9
0
 /**
  * @Route("/upload", name="ed_blog_admin_article_upload")
  */
 public function uploadAction(Request $request)
 {
     $user = $this->getBlogUser();
     $mediaManager = $this->container->get('sonata.media.manager.media');
     if (!$request->get('excerpt', false)) {
         $form = $this->createForm(new ArticlePhotoType());
     } else {
         $form = $this->createForm(new ArticleExcerptType());
     }
     if ($request->getMethod() == 'POST') {
         $form->handleRequest($request);
         $dispatcher = $this->get('event_dispatcher');
         $attachment = $form['media']->getData();
         //to support single file upload (multiple=false)
         if (!is_array($attachment)) {
             $attachment = array($attachment);
         }
         $mediaArray = new ArrayCollection();
         $errorArray = array();
         foreach ($attachment as $attached) {
             if (in_array($attached->getMimeType(), array('image/pjpeg', 'image/jpeg', 'image/png', 'image/x-png', 'image/gif'))) {
                 try {
                     $media = new Media();
                     $media->setBinaryContent($attached);
                     $media->setContext('default');
                     $media->setProviderName('sonata.media.provider.image');
                     $media->setName($attached->getClientOriginalName());
                     $media->setEnabled(true);
                     $mediaManager->save($media);
                     $mediaArray->add($media);
                 } catch (\Exception $e) {
                     $errorArray[] = array("name" => $attached->getClientOriginalName(), "message" => $e->getMessage());
                 }
             } else {
                 $errorArray[] = array("name" => $attached->getClientOriginalName(), "message" => "The file is unsupported");
             }
         }
         $dispatcher->dispatch(EDBlogEvents::ED_BLOG_MEDIA_LIBRARY_MEDIA_UPLOADED, new MediaArrayEvent($mediaArray));
     }
     $paginator = $this->get('blog.paginator');
     $response = $this->getPaginated($paginator, array('errors' => $errorArray));
     return $response;
 }
Ejemplo n.º 10
0
 public function load(ObjectManager $manager)
 {
     $introImage1 = new Media();
     $introImage1->setName('sample_thumb.jpeg');
     $introImage1->setEnabled(0);
     $introImage1->setProviderName('sonata.media.provider.image');
     $introImage1->setProviderStatus(1);
     $introImage1->setProviderReference('sample_thumb.jpeg');
     $introImage1->setMetadataValue('filename', 'sample_thumb.jpeg');
     $introImage1->setWidth(622);
     $introImage1->setHeight(415);
     $introImage1->setContentType('image/jpeg');
     $introImage1->setSize(8043);
     $introImage1->setCategory($manager->merge($this->getReference('category2')));
     $introImage1->setContext('intro');
     $introImage1->setCreatedAt(new \DateTime());
     $introImage1->setUpdatedAt(new \DateTime());
     $manager->persist($introImage1);
     $introImage2 = new Media();
     $introImage2->setName('sample_thumb.jpeg');
     $introImage2->setEnabled(0);
     $introImage2->setProviderName('sonata.media.provider.image');
     $introImage2->setProviderStatus(1);
     $introImage2->setProviderReference('sample_thumb.jpeg');
     $introImage2->setMetadataValue('filename', 'sample_thumb.jpeg');
     $introImage2->setWidth(622);
     $introImage2->setHeight(415);
     $introImage2->setContentType('image/jpeg');
     $introImage2->setSize(8043);
     $introImage2->setCategory($manager->merge($this->getReference('category2')));
     $introImage2->setContext('intro');
     $introImage2->setCreatedAt(new \DateTime());
     $introImage2->setUpdatedAt(new \DateTime());
     $manager->persist($introImage2);
     $introImage3 = new Media();
     $introImage3->setName('sample_thumb.jpeg');
     $introImage3->setEnabled(0);
     $introImage3->setProviderName('sonata.media.provider.image');
     $introImage3->setProviderStatus(1);
     $introImage3->setProviderReference('sample_thumb.jpeg');
     $introImage3->setMetadataValue('filename', 'sample_thumb.jpeg');
     $introImage3->setWidth(622);
     $introImage3->setHeight(415);
     $introImage3->setContentType('image/jpeg');
     $introImage3->setSize(8043);
     $introImage3->setCategory($manager->merge($this->getReference('category2')));
     $introImage3->setContext('intro');
     $introImage3->setCreatedAt(new \DateTime());
     $introImage3->setUpdatedAt(new \DateTime());
     $manager->persist($introImage3);
     $introImage4 = new Media();
     $introImage4->setName('sample_thumb.jpeg');
     $introImage4->setEnabled(0);
     $introImage4->setProviderName('sonata.media.provider.image');
     $introImage4->setProviderStatus(1);
     $introImage4->setProviderReference('sample_thumb.jpeg');
     $introImage4->setMetadataValue('filename', 'sample_thumb.jpeg');
     $introImage4->setWidth(622);
     $introImage4->setHeight(415);
     $introImage4->setContentType('image/jpeg');
     $introImage4->setSize(8043);
     $introImage4->setCategory($manager->merge($this->getReference('category2')));
     $introImage4->setContext('intro');
     $introImage4->setCreatedAt(new \DateTime());
     $introImage4->setUpdatedAt(new \DateTime());
     $manager->persist($introImage4);
     $introImage5 = new Media();
     $introImage5->setName('sample_thumb.jpeg');
     $introImage5->setEnabled(0);
     $introImage5->setProviderName('sonata.media.provider.image');
     $introImage5->setProviderStatus(1);
     $introImage5->setProviderReference('sample_thumb.jpeg');
     $introImage5->setMetadataValue('filename', 'sample_thumb.jpeg');
     $introImage5->setWidth(622);
     $introImage5->setHeight(415);
     $introImage5->setContentType('image/jpeg');
     $introImage5->setSize(8043);
     $introImage5->setCategory($manager->merge($this->getReference('category2')));
     $introImage5->setContext('intro');
     $introImage5->setCreatedAt(new \DateTime());
     $introImage5->setUpdatedAt(new \DateTime());
     $manager->persist($introImage5);
     $introImage6 = new Media();
     $introImage6->setName('sample_thumb.jpeg');
     $introImage6->setEnabled(0);
     $introImage6->setProviderName('sonata.media.provider.image');
     $introImage6->setProviderStatus(1);
     $introImage6->setProviderReference('sample_thumb.jpeg');
     $introImage6->setMetadataValue('filename', 'sample_thumb.jpeg');
     $introImage6->setWidth(622);
     $introImage6->setHeight(415);
     $introImage6->setContentType('image/jpeg');
     $introImage6->setSize(8043);
     $introImage6->setCategory($manager->merge($this->getReference('category2')));
     $introImage6->setContext('intro');
     $introImage6->setCreatedAt(new \DateTime());
     $introImage6->setUpdatedAt(new \DateTime());
     $manager->persist($introImage6);
     $introImage7 = new Media();
     $introImage7->setName('sample_thumb.jpeg');
     $introImage7->setEnabled(0);
     $introImage7->setProviderName('sonata.media.provider.image');
     $introImage7->setProviderStatus(1);
     $introImage7->setProviderReference('sample_thumb.jpeg');
     $introImage7->setMetadataValue('filename', 'sample_thumb.jpeg');
     $introImage7->setWidth(622);
     $introImage7->setHeight(415);
     $introImage7->setContentType('image/jpeg');
     $introImage7->setSize(8043);
     $introImage7->setCategory($manager->merge($this->getReference('category2')));
     $introImage7->setContext('intro');
     $introImage7->setCreatedAt(new \DateTime());
     $introImage7->setUpdatedAt(new \DateTime());
     $manager->persist($introImage7);
     $introImage8 = new Media();
     $introImage8->setName('sample_thumb.jpeg');
     $introImage8->setEnabled(0);
     $introImage8->setProviderName('sonata.media.provider.image');
     $introImage8->setProviderStatus(1);
     $introImage8->setProviderReference('sample_thumb.jpeg');
     $introImage8->setMetadataValue('filename', 'sample_thumb.jpeg');
     $introImage8->setWidth(622);
     $introImage8->setHeight(415);
     $introImage8->setContentType('image/jpeg');
     $introImage8->setSize(8043);
     $introImage8->setCategory($manager->merge($this->getReference('category2')));
     $introImage8->setContext('intro');
     $introImage8->setCreatedAt(new \DateTime());
     $introImage8->setUpdatedAt(new \DateTime());
     $manager->persist($introImage8);
     $homeBanner1 = new Media();
     $homeBanner1->setName('sample_thumb.jpeg');
     $homeBanner1->setEnabled(0);
     $homeBanner1->setProviderName('sonata.media.provider.image');
     $homeBanner1->setProviderStatus(1);
     $homeBanner1->setProviderReference('sample_thumb.jpeg');
     $homeBanner1->setMetadataValue('filename', 'sample_thumb.jpeg');
     $homeBanner1->setWidth(622);
     $homeBanner1->setHeight(415);
     $homeBanner1->setContentType('image/jpeg');
     $homeBanner1->setSize(8043);
     $homeBanner1->setCategory($manager->merge($this->getReference('category3')));
     $homeBanner1->setContext('bgimage');
     $homeBanner1->setCreatedAt(new \DateTime());
     $homeBanner1->setUpdatedAt(new \DateTime());
     $manager->persist($homeBanner1);
     $homeBanner2 = new Media();
     $homeBanner2->setName('sample_thumb.jpeg');
     $homeBanner2->setEnabled(0);
     $homeBanner2->setProviderName('sonata.media.provider.image');
     $homeBanner2->setProviderStatus(1);
     $homeBanner2->setProviderReference('sample_thumb.jpeg');
     $homeBanner2->setMetadataValue('filename', 'sample_thumb.jpeg');
     $homeBanner2->setWidth(622);
     $homeBanner2->setHeight(415);
     $homeBanner2->setContentType('image/jpeg');
     $homeBanner2->setSize(8043);
     $homeBanner2->setCategory($manager->merge($this->getReference('category3')));
     $homeBanner2->setContext('bgimage');
     $homeBanner2->setCreatedAt(new \DateTime());
     $homeBanner2->setUpdatedAt(new \DateTime());
     $manager->persist($homeBanner2);
     $manager->flush();
     $this->addReference('introImage1', $introImage1);
     $this->addReference('introImage2', $introImage2);
     $this->addReference('introImage3', $introImage3);
     $this->addReference('introImage4', $introImage4);
     $this->addReference('introImage5', $introImage5);
     $this->addReference('introImage6', $introImage6);
     $this->addReference('introImage7', $introImage7);
     $this->addReference('introImage8', $introImage8);
     $this->addReference('homeBanner1', $homeBanner1);
     $this->addReference('homeBanner2', $homeBanner2);
 }