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; }
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; }
/** * Creates a new Lots entity. * */ public function createAction(Request $request) { $entity = new Lots(); $form = $this->createCreateForm($entity); $form->handleRequest($request); if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); $user = $this->container->get('security.context')->getToken()->getUser(); $entity->setUser($user); $uploadedFiles = $request->files->get('myfile'); $numUploadedFiles = count($uploadedFiles); if ($numUploadedFiles > 0) { $tmp_gallery = new Gallery(); $mediaManager = $this->container->get('sonata.media.manager.media'); $uploadedFiles = $request->files->get('myfile'); $tmp_gallery->setName($entity->getTitle()); $tmp_gallery->setContext('default'); $tmp_gallery->setDefaultFormat('default_small'); $tmp_gallery->setEnabled(true); // $em->persist($tmp_gallery); // $em->refresh($tmp_gallery); for ($i = 0; $i < $numUploadedFiles; $i++) { $tmp_media = new Media(); $tmp_galleryHasMedia = new GalleryHasMedia(); $tmp_media->setBinaryContent($uploadedFiles[$i]->getRealPath()); $tmp_media->setContext('default'); $tmp_media->setName($uploadedFiles[$i]->getClientOriginalName()); $mediaManager->save($tmp_media, 'default', 'sonata.media.provider.image'); $tmp_galleryHasMedia->setMedia($tmp_media); $tmp_galleryHasMedia->setEnabled(true); $tmp_galleryHasMedia->setGallery($tmp_gallery); $em->persist($tmp_galleryHasMedia); } $em->persist($tmp_gallery); $em->flush(); $em->refresh($tmp_gallery); $entity->setGallery($tmp_gallery); } $offer = new Offer(); $em->persist($offer); $entity->setOffer($offer); $em->persist($entity); $em->flush(); return $this->redirect($this->generateUrl('lots_show', array('id' => $entity->getId()))); } return $this->render('AESlandsBundle:Lots:new.html.twig', array('entity' => $entity, 'form' => $form->createView())); }
/** * @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 setName($name) { $this->__initializer__ && $this->__initializer__->__invoke($this, 'setName', array($name)); return parent::setName($name); }
/** * @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; }
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); }
protected function addPhotosToCategory($message) { $categoryName = $message->getValue('categoryName'); $filePath = $message->getValue('filePath'); $category = $this->enityManager->getRepository('AldorGalleryBundle:PhotoCategory')->findOneByName($categoryName); $zip = new ExtractZip($filePath); $files = $zip->extract(); $counter = 0; $maxSequence = $this->enityManager->getRepository('AldorGalleryBundle:Photo')->getMaxSequenceByCategory($category->getId()); foreach ($files as $pos => $file) { $title = explode('/', $pos); $fileName = $title[count($title) - 1]; $fileName = str_replace('DSC_', '', $fileName); $title = $categoryName . '-' . 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); $photo->setSequence($maxSequence++); $this->enityManager->persist($photo); } $this->enityManager->flush(); $zip->clean(); }