Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. public function addProdctAction(Request $request)
  2. {
  3. $user = $this->container->get('security.token_storage')->getToken()->getUser();
  4. $produit = new Produit();
  5. $form = $this->createFormBuilder($produit)
  6. ->add('nom', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
  7. ->add('description', TextareaType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
  8. ->add('quantity', NumberType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
  9. ->add('prix', NumberType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
  10. ->add('imageId', FileType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
  11. ->add('category', EntityType::class, [
  12. // looks for choices from this entity
  13. 'class' => Category::class,
  14. 'choice_label' => 'category',])
  15.  
  16. ->getForm();
  17.  
  18. $form->handleRequest($request);
  19. if ($form->isSubmitted() && $form->isValid()) {
  20.  
  21. /** @var UploadedFile $file */
  22. $file = $produit->getImageId();
  23.  
  24. $fileName = md5(uniqid()) . '.' . $file->guessExtension();
  25.  
  26. // Move the file to the directory where brochures are stored
  27.  
  28. $file->move(
  29. $this->getParameter('images_shop'),
  30. $fileName
  31. );
  32.  
  33.  
  34. $produit->setImageId($fileName);
  35.  
  36. $nom = $form['nom']->getData();
  37. $description = $form['description']->getData();
  38. $stock = $form['quantity']->getData();
  39. $prix = $form['prix']->getData();
  40. $cat = $form['category']->getData();
  41. $now = new\DateTime('now');
  42.  
  43. $produit->setNom($nom);
  44. $produit->setDescription($description);
  45. $produit->setQuantity($stock);
  46. $produit->setPrix($prix);
  47. $produit->setUtilisateur($user);
  48. $produit->setCategory($cat);
  49. $produit->setStars(0);
  50. $produit->setDate($now);
  51.  
  52. $sn = $this->getDoctrine()->getManager();
  53. $sn->persist($produit);
  54. $sn->flush();
  55.  
  56. $this->addFlash(
  57. 'notice',
  58. 'todo added'
  59. );
  60.  
  61. }
  62.  
  63. $panierlist = $this->getDoctrine()->getRepository('ShopBundle:Panier')->findByUser($user);
  64. $cat = $this->getDoctrine()->getRepository('ShopBundle:Category')->findAll();
  65.  
  66. $count = count($panierlist);
  67. $total = 0;
  68. foreach ($panierlist as $prix) {
  69.  
  70. $total = $total + $prix->getPrix();
  71. }
  72.  
  73.  
  74.  
  75. return $this->render('AdminBundle:Default:addprod.html.twig', array(
  76.  
  77. 'cat' => $cat,
  78. 'form' => $form->createView(),'nbrp' => $count, 'panier' => $panierlist, 'total' => $total
  79.  
  80. ));
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement