src/Flowcode/ShopBundle/Controller/AdminProductController.php
/**
     * Finds and displays a Product entity.
     *
     * @Route("/{id}/upload", name="admin_product_upload_images")
     * @Method("POST")
     */
Finds and displays a Product entity.
Finds and displays a Product entity .
public function uploadAction(Product $product)
    {
        if (!$product) {
            throw $this->createNotFoundException('Unable to find Product entity.');
        }
        $productService = $this->get('amulen.shop.product');
        $response = $productService->uploadImage($product);
        $httpResponse = Response::HTTP_BAD_REQUEST;
        if ($response['upload_ok'] != 0) {
            $path = $response['target_file'];
            $em = $this->getDoctrine()->getManager();
            $mediaTypeRepository = $em->getRepository(MediaType::class);
            $mediaType = $mediaTypeRepository->findOneBy([
                'name' => 'image'
            ]);
            $media = new Media();
            $name = basename($_FILES["file"]["name"]);
            $slugName = strtolower(str_replace(' ','-',$name));
            $media->setName($slugName);
            $media->setPath($path);
            $media->setMediaType($mediaType);
            $em->persist($media);
            $galleryItem = new GalleryItem();
            $gallery = $product->getMediaGallery();
            $gallery->addGalleryItem($galleryItem);
            $galleryItem->setGallery($gallery);
            $galleryItem->setMedia($media);
            $position = $gallery->getGalleryItems()->count() + 1;
            $galleryItem->setPosition($position);
            $em->persist($galleryItem);
            $em->flush();
            $httpResponse = Response::HTTP_OK;
        }
        if ($response['upload_ok'] == 2) {
            $httpResponse = Response::HTTP_BAD_REQUEST;
        }
        return new JsonResponse($response['message'], $httpResponse);
    }
public function uploadAction ( Product $ product ) { if ( ! $ product ) { throw $ this -> createNotFoundException ( 'Unable to find Product entity.' ) ; } $ productService = $ this -> get ( 'amulen.shop.product' ) ; $ response = $ productService -> uploadImage ( $ product ) ; $ httpResponse = Response :: HTTP_BAD_REQUEST ; if ( $ response [ 'upload_ok' ] != 0 ) { $ path = $ response [ 'target_file' ] ; $ em = $ this -> getDoctrine ( ) -> getManager ( ) ; $ mediaTypeRepository = $ em -> getRepository ( MediaType :: class ) ; $ mediaType = $ mediaTypeRepository -> findOneBy ( [ 'name' => 'image' ] ) ; $ media = new Media ( ) ; $ name = basename ( $ _FILES [ " file " ] [ " name " ] ) ; $ slugName = strtolower ( str_replace ( ' ' , '-' , $ name ) ) ; $ media -> setName ( $ slugName ) ; $ media -> setPath ( $ path ) ; $ media -> setMediaType ( $ mediaType ) ; $ em -> persist ( $ media ) ; $ galleryItem = new GalleryItem ( ) ; $ gallery = $ product -> getMediaGallery ( ) ; $ gallery -> addGalleryItem ( $ galleryItem ) ; $ galleryItem -> setGallery ( $ gallery ) ; $ galleryItem -> setMedia ( $ media ) ; $ position = $ gallery -> getGalleryItems ( ) -> count ( ) + 1 ; $ galleryItem -> setPosition ( $ position ) ; $ em -> persist ( $ galleryItem ) ; $ em -> flush ( ) ; $ httpResponse = Response :: HTTP_OK ; } if ( $ response [ 'upload_ok' ] == 2 ) { $ httpResponse = Response :: HTTP_BAD_REQUEST ; } return new JsonResponse ( $ response [ 'message' ] , $ httpResponse ) ; }
Finds and displays a Product entity.
Finds and displays a Product entity .