Пример #1
0
 /**
  * @Method({"GET", "POST"})
  * @Route(
  *      "/product/create",
  *      name="product_create",
  *      host="{domain_dashboard}",
  *      defaults={"_locale" = "%locale%", "domain_dashboard" = "%domain_dashboard%"},
  *      requirements={"_locale" = "%locale%", "domain_dashboard" = "%domain_dashboard%"}
  * )
  */
 public function createAction(Request $request)
 {
     if (!$this->_productBoundlessAccess->isGranted(ProductBoundlessAccess::PRODUCT_CREATE)) {
         throw $this->createAccessDeniedException('Access denied');
     }
     $productType = new ProductType($this->_translator, $this->_productBoundlessAccess->isGranted(ProductBoundlessAccess::PRODUCT_CREATE));
     $form = $this->createForm($productType, $product = new Product(), ['action' => $this->generateUrl('product_create')]);
     $form->handleRequest($request);
     if (!($form->isValid() && $this->_uploadedProductImageValidator->validate($form))) {
         $this->_breadcrumbs->add('product_read')->add('product_create');
         return $this->render('AppBundle:Entity/Product/CRUD:createItem.html.twig', ['form' => $form->createView()]);
     } else {
         // TODO: This large logic does not belong to Controller
         if (array_filter($form->getData()->getUploadedProductImages())) {
             foreach ($form->getData()->getUploadedProductImages() as $uploadedProductImage) {
                 $productImage = (new ProductImage())->setImageProductFile($uploadedProductImage)->setUpdatedAt(new DateTime());
                 $product->addProductImage($productImage);
                 $this->_manager->persist($productImage);
             }
             $this->_manager->persist($product);
         }
         $this->_manager->persist($product);
         $this->_manager->flush();
         $this->_messages->markCreateSuccess();
         if ($form->has('create_and_return') && $form->get('create_and_return')->isClicked()) {
             return $this->redirectToRoute('product_read');
         } else {
             return $this->redirectToRoute('product_update', ['id' => $product->getId()]);
         }
     }
 }