/** * Edit Unit * @param Request $request * @param $id * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response */ public function editAction(Request $request, $id) { $em = $this->getDoctrine()->getManager(); $unit = $em->getRepository('AppBundle:Unit')->find($id); $period = new UnitPeriod(); $unitPeriods = $em->getRepository('AppBundle:UnitPeriod')->getAll($unit->getId()); $unitGallery = new UnitGallery(); $priceExtraRep = $em->getRepository('AppBundle:PriceExtra'); $priceExtra = $priceExtraRep->findAll(); $form = $this->createForm(new UnitType(), $unit)->remove('accommodation'); # Period form $formPeriod = $this->createForm(new PeriodType($unit), $period); # Gallery form $formGallery = $this->createForm(new UnitGalleryType(array($unit)), $unitGallery); # All POST data $postData = $request->request->all(); $form->handleRequest($request); if ($form->isValid()) { $em->persist($unit); $em->flush(); // echo'<pre>'; // exit(\Doctrine\Common\Util\Debug::dump($postData['priceExtra'])); // echo'</pre>'; $em->getRepository('AppBundle:UnitPriceExtra')->destroyAllUnit($unit); # Add price extra to join table with unit foreach ($postData['priceExtra'] as $priceExtra) { if ($priceExtra['availability']) { $unitPriceExtra = new UnitPriceExtra(); $pExtra = $priceExtraRep->find($priceExtra['priceExtraId']); $unitPriceExtra->setUnit($unit); $unitPriceExtra->setPriceExtra($pExtra); $unitPriceExtra->setPrice($priceExtra['price']); $unitPriceExtra->setAvailability($priceExtra['availability']); $em->persist($unitPriceExtra); $em->flush(); } } $session = $request->getSession(); $session->getFlashBag()->add('msgSuccess', $this->get('translator')->trans('edit_success')); return $this->redirect($this->generateUrl('admin_unit_edit', array('id' => $id, 'unit' => $unit, 'priceExtra' => $priceExtra))); } return $this->render('AdminBundle:Unit:edit.html.twig', array('form' => $form->createView(), 'unit' => $unit, 'priceExtra' => $priceExtra, 'periods' => $unitPeriods, 'formPeriod' => $formPeriod->createView(), 'formGallery' => $formGallery->createView())); }
/** * Edit unit * @param Request $request * @param $id * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response */ public function editAction(Request $request, Unit $unit) { $em = $this->getDoctrine()->getManager(); $period = new UnitPeriod(); # If user isn't owner -> 403 if ($this->getUser()->getId() != $unit->getAccommodation()->getUser()->getId()) { return $this->redirect($this->generateUrl('user_403')); } $unitPeriods = $em->getRepository('AppBundle:UnitPeriod')->getAll($unit->getId()); $unitGallery = new UnitGallery(); $priceExtraRep = $em->getRepository('AppBundle:PriceExtra'); $priceExtra = $priceExtraRep->findAll(); // price for form foreach ($unit->getUnitPriceExtra() as $pe) { if (!is_null($pe->getPrice())) { $newPrice = $this->get("currencyService")->fromEuro($pe->getPrice()); $pe->setPrice(round($newPrice, 2)); } } # Unit form $form = $this->createForm(new UnitType(), $unit); # Period form $formPeriod = $this->createForm(new PeriodType($unit), $period); # Gallery form $formGallery = $this->createForm(new UnitGalleryType(array($unit)), $unitGallery); $form->handleRequest($request); # All POST data $postData = $request->request->all(); if ($form->isValid()) { $em->persist($unit); $em->flush(); // echo'<pre>'; // exit(\Doctrine\Common\Util\Debug::dump($request->request->get('accommodation')['wifi'])); // echo'</pre>'; $em->getRepository('AppBundle:UnitPriceExtra')->destroyAllUnit($unit); # Add price extra to join table with unit foreach ($postData['priceExtra'] as $priceExtra) { if ($priceExtra['availability'] > 0) { $unitPriceExtra = new UnitPriceExtra(); $pExtra = $priceExtraRep->find($priceExtra['priceExtraId']); $unitPriceExtra->setUnit($unit); $unitPriceExtra->setPriceExtra($pExtra); $price = $this->get("currencyService")->toEuro($priceExtra['price']); $unitPriceExtra->setPrice($price); $unitPriceExtra->setAvailability($priceExtra['availability']); $em->persist($unitPriceExtra); $em->flush(); } } $session = $request->getSession(); $session->getFlashBag()->add('msgSuccess', $this->get('translator')->trans('edit_success')); return $this->redirect($this->generateUrl('app_unit_edit', array('id' => $unit->getId(), 'priceExtra' => $priceExtra))); } return $this->render('AppBundle:Unit:edit.html.twig', array('form' => $form->createView(), 'unit' => $unit, 'periods' => $unitPeriods, 'formPeriod' => $formPeriod->createView(), 'formGallery' => $formGallery->createView(), 'priceExtra' => $priceExtra)); }
/** * Step4 - Unit Data * @param Request $request * @param Accommodation $accommodation * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response */ public function step4Action(Request $request, Accommodation $accommodation) { $em = $this->getDoctrine()->getManager(); $unit = new Unit(); $priceExtraRep = $em->getRepository('AppBundle:PriceExtra'); $priceExtra = $priceExtraRep->findAll(); if (!$this->get("accommodationService")->moreUnitAllowedSteps($accommodation)) { $request->getSession()->getFlashBag()->add('msgError', $this->get('translator')->trans('max_unit_exceeded')); return $this->redirect($this->generateUrl('app_profile_step_4a', array('id' => $accommodation->getId()))); } // price for form foreach ($unit->getUnitPriceExtra() as $pe) { if (!is_null($pe->getPrice())) { $newPrice = $this->get("currencyService")->fromEuro($pe->getPrice()); $pe->setPrice(round($newPrice, 2)); } } $form = $this->createForm(new UnitType(), $unit); $form->handleRequest($request); # All POST data $postData = $request->request->all(); if ($form->isValid()) { $unit->setAccommodation($accommodation); $unit->setBasicPrice($this->get("currencyService")->toEuro($postData['unit']['basicPrice'])); $em->persist($unit); $em->flush(); # Add price extra to join table with unit foreach ($postData['priceExtra'] as $priceExtra) { $unitPriceExtra = new UnitPriceExtra(); $pExtra = $priceExtraRep->find($priceExtra['priceExtraId']); $unitPriceExtra->setUnit($unit); $unitPriceExtra->setPriceExtra($pExtra); $price = $this->get("currencyService")->toEuro($priceExtra['price']); $unitPriceExtra->setPrice($price); $unitPriceExtra->setAvailability($priceExtra['availability']); $em->persist($unitPriceExtra); $em->flush(); } return $this->redirect($this->generateUrl('app_profile_step_4a', array('id' => $accommodation->getId()))); } return $this->render('AppBundle:Profile/Steps:step4.html.twig', array('form' => $form->createView(), 'unit' => $unit, 'currencies' => $this->get("currencyService")->getCurrencies(), 'priceExtra' => $priceExtra)); }