From 87d5a089605b85c0f532f83b18bc63f7ca155274 Mon Sep 17 00:00:00 2001 From: Jorge Oscar Gianotti Date: Wed, 7 Oct 2020 09:35:49 -0300 Subject: [PATCH] =?UTF-8?q?Se=20agrega=20validaci=C3=B3n=20en=20la=20selec?= =?UTF-8?q?ci=C3=B3n=20del=20usuario=20propietario=20durante=20la=20creaci?= =?UTF-8?q?=C3=B3n=20de=20un=20pedido,=20creado=20por=20un=20administrador?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controller/AdminOrderController.php | 215 ++++++++++++------ 1 file changed, 142 insertions(+), 73 deletions(-) diff --git a/src/Celsius3/CoreBundle/Controller/AdminOrderController.php b/src/Celsius3/CoreBundle/Controller/AdminOrderController.php index 5fd8a8cef..8863aae18 100644 --- a/src/Celsius3/CoreBundle/Controller/AdminOrderController.php +++ b/src/Celsius3/CoreBundle/Controller/AdminOrderController.php @@ -29,6 +29,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; +use Symfony\Component\Form\FormError; use Symfony\Component\HttpFoundation\Request; /** @@ -40,23 +41,23 @@ class AdminOrderController extends OrderController { protected function getSortDefaults() { - return array( + return [ 'defaultSortFieldName' => 'o.updatedAt', 'defaultSortDirection' => 'asc', - ); + ]; } protected function listQuery($name) { return $this->getDoctrine()->getManager() - ->getRepository('Celsius3CoreBundle:' . $name) + ->getRepository('Celsius3CoreBundle:'.$name) ->findForInstance($this->getInstance()); } protected function findQuery($name, $id) { return $this->getDoctrine()->getManager() - ->getRepository('Celsius3CoreBundle:' . $name) + ->getRepository('Celsius3CoreBundle:'.$name) ->findOneForInstance($this->getInstance(), $id); } @@ -79,16 +80,14 @@ public function indexAction() * @Route("/{id}/show", name="admin_order_show", options={"expose"=true}) * @Template() * - * @param string $id The entity ID + * @param string $id The entity ID * * @return array * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException If entity doesn't exists */ public function showAction($id) - { - return $this->baseShow('Order', $id); } @@ -109,44 +108,93 @@ public function newAction(Request $request) } else { $user = null; } - - return $this->baseNew('Order', new Order(), OrderType::class, array( - 'instance' => $this->getInstance(), - 'user' => $user, - 'operator' => $this->getUser(), - 'actual_user' => $this->getUser(), - 'create' => true, - )); + + return $this->baseNew( + 'Order', + new Order(), + OrderType::class, + [ + 'instance' => $this->getInstance(), + 'user' => $user, + 'operator' => $this->getUser(), + 'actual_user' => $this->getUser(), + 'create' => true, + ] + ); } /** * Creates a new Order entity. * - * @Route("/create", name="admin_order_create") - * @Method("POST") - * @Template("Celsius3CoreBundle:AdminOrder:new.html.twig") - * - * @return array + * @Route("/create", name="admin_order_create", methods={"POST"}) */ public function createAction(Request $request) { - $options = array( + $options = [ 'instance' => $this->getInstance(), 'material' => $this->getMaterialType(), 'operator' => $this->getUser(), 'actual_user' => $this->getUser(), 'create' => true, - 'user' => $this->getDoctrine()->getManager() - ->getRepository('Celsius3CoreBundle:BaseUser') - ->find($request->request->get('order')['originalRequest']['owner']), - ); - - if ($this->getMaterialType() === JournalTypeType::class){ + 'user' => $this->getDoctrine()->getManager() + ->getRepository('Celsius3CoreBundle:BaseUser') + ->find($request->request->get('order')['originalRequest']['owner']), + ]; + + if ($this->getMaterialType() === JournalTypeType::class) { $options['other'] = $request->request->get('order')['materialData']['journal_autocomplete']; $options['journal_id'] = $request->request->get('order')['materialData']['journal']; } - return $this->baseCreate('Order', new Order(), OrderType::class, $options, 'administration'); + $order = new Order(); + $type = OrderType::class; + $route = 'administration'; + + $form = $this->createForm($type, $order, $options); + $form->handleRequest($request); + + if (!$order->getOriginalRequest()->getOwner()) { + $form->get('originalRequest') + ->get('owner_autocomplete') + ->addError(new FormError('El usuario seleccionado no es vĂ¡lido')); + } + + if ($form->isSubmitted() && $form->isValid()) { + if ($this->getMaterialType() === 'Celsius3\CoreBundle\Form\Type\JournalTypeType') { + $journal = $this->getDoctrine()->getManager()->getRepository('Celsius3CoreBundle:Journal')->find( + $request->request->get('order')['materialData']['journal'] + ); + if ($journal === null) { + $order->getMaterialData()->setOther( + $request->request->get('order')['materialData']['journal_autocomplete'] + ); + $order->getMaterialData()->setJournal(null); + } + } + + $this->persistEntity($order); + $this->get('session') + ->getFlashBag() + ->add('success', 'The Order was successfully created.'); + + if ($form->has('save_and_show') && $form->get('save_and_show')->isClicked()) { + return $this->redirect($this->generateUrl('admin_order_show', ['id' => $order->getId()])); + } + + return $this->redirect($this->generateUrl($route)); + } + + $this->get('session') + ->getFlashBag() + ->add('error', 'There were errors creating the Order.'); + + return $this->render( + 'Celsius3CoreBundle:AdminOrder:new.html.twig', + [ + 'entity' => $order, + 'form' => $form->createView(), + ] + ); } /** @@ -155,7 +203,7 @@ public function createAction(Request $request) * @Route("/{id}/edit", name="admin_order_edit", options={"expose"=true}) * @Template() * - * @param string $id The entity ID + * @param string $id The entity ID * * @return array * @@ -173,30 +221,34 @@ public function editAction($id) if ($entity->getMaterialData() instanceof \Celsius3\CoreBundle\Entity\JournalType) { $journal = $entity->getMaterialData()->getJournal(); - } else { $journal = null; } - $other = ($entity->getMaterialData() instanceof \Celsius3\CoreBundle\Entity\JournalType) ? $entity->getMaterialData()->getOther() : ''; - - $editForm = $this->createForm(OrderType::class, $entity, array( - 'instance' => $this->getInstance(), - 'material' => $this->getMaterialType($materialClass), - 'user' => $entity->getOriginalRequest()->getOwner(), - 'operator' => $this->getUser(), - 'actual_user' => $this->getUser(), - 'journal' => $journal, - 'other' => $other, - 'journal_id' => !is_null($journal) ? $journal->getId() : '', - - )); + $other = ($entity->getMaterialData( + ) instanceof \Celsius3\CoreBundle\Entity\JournalType) ? $entity->getMaterialData()->getOther() : ''; + + $editForm = $this->createForm( + OrderType::class, + $entity, + [ + 'instance' => $this->getInstance(), + 'material' => $this->getMaterialType($materialClass), + 'user' => $entity->getOriginalRequest()->getOwner(), + 'operator' => $this->getUser(), + 'actual_user' => $this->getUser(), + 'journal' => $journal, + 'other' => $other, + 'journal_id' => !is_null($journal) ? $journal->getId() : '', + + ] + ); - - return array('entity' => $entity, + return [ + 'entity' => $entity, 'edit_form' => $editForm->createView(), - ); + ]; } /** @@ -221,7 +273,13 @@ public function duplicateAction($id) //Clonar Orden original $duplicatedOrder = clone $order; - $request = $this->get('celsius3_core.lifecycle_helper')->createRequest($duplicatedOrder, $order->getOriginalRequest()->getOwner(), $order->getOriginalRequest()->getType(), $this->getInstance(), $order->getOriginalRequest()->getCreator()); + $request = $this->get('celsius3_core.lifecycle_helper')->createRequest( + $duplicatedOrder, + $order->getOriginalRequest()->getOwner(), + $order->getOriginalRequest()->getType(), + $this->getInstance(), + $order->getOriginalRequest()->getCreator() + ); $duplicatedOrder->setOriginalRequest($request); $duplicatedMaterialData = clone $order->getMaterialData(); $duplicatedOrder->setMaterialData($duplicatedMaterialData); @@ -232,7 +290,8 @@ public function duplicateAction($id) $journal = null; } - $other = ($duplicatedMaterialData instanceof \Celsius3\CoreBundle\Entity\JournalType) ? $duplicatedMaterialData->getOther() : ''; + $other = ($duplicatedMaterialData instanceof \Celsius3\CoreBundle\Entity\JournalType) ? $duplicatedMaterialData->getOther( + ) : ''; //Se registra duplicado en la base de datos $entity_manager->persist($duplicatedOrder); @@ -241,20 +300,24 @@ public function duplicateAction($id) $materialClass = get_class($duplicatedOrder->getMaterialData()); - $editForm = $this->createForm(OrderType::class, $duplicatedOrder, array( - 'instance' => $this->getInstance(), - 'material' => $this->getMaterialType($materialClass), - 'user' => $duplicatedOrder->getOriginalRequest()->getOwner(), - 'operator' => $this->getUser(), - 'actual_user' => $this->getUser(), - 'journal' => $journal, - 'other' => $other, - )); + $editForm = $this->createForm( + OrderType::class, + $duplicatedOrder, + [ + 'instance' => $this->getInstance(), + 'material' => $this->getMaterialType($materialClass), + 'user' => $duplicatedOrder->getOriginalRequest()->getOwner(), + 'operator' => $this->getUser(), + 'actual_user' => $this->getUser(), + 'journal' => $journal, + 'other' => $other, + ] + ); - return array( + return [ 'entity' => $duplicatedOrder, 'edit_form' => $editForm->createView(), - ); + ]; } /** @@ -264,7 +327,7 @@ public function duplicateAction($id) * @Method("post") * @Template("Celsius3CoreBundle:AdminOrder:edit.html.twig") * - * @param string $id The entity ID + * @param string $id The entity ID * * @return array * @@ -284,13 +347,17 @@ public function updateAction($id, Request $request) $user = $this->getDoctrine()->getManager()->getRepository('Celsius3CoreBundle:BaseUser') ->find($request->request->get('order', null)['originalRequest']['owner']); - $editForm = $this->createForm(OrderType::class, $entity, array( - 'instance' => $this->getInstance(), - 'material' => $this->getMaterialType(), - 'user' => $user, - 'operator' => $this->getUser(), - 'actual_user' => $this->getUser(), - )); + $editForm = $this->createForm( + OrderType::class, + $entity, + [ + 'instance' => $this->getInstance(), + 'material' => $this->getMaterialType(), + 'user' => $user, + 'operator' => $this->getUser(), + 'actual_user' => $this->getUser(), + ] + ); $editForm->handleRequest($request); @@ -299,8 +366,10 @@ public function updateAction($id, Request $request) $journal = $this->getDoctrine()->getManager()->getRepository('Celsius3CoreBundle:Journal')->find( $request->request->get('order', null)['materialData']['journal'] ); - if (is_null($journal) ) { - $entity->getMaterialData()->setOther($request->request->get('order', null)['materialData']['journal_autocomplete']); + if (is_null($journal)) { + $entity->getMaterialData()->setOther( + $request->request->get('order', null)['materialData']['journal_autocomplete'] + ); $entity->getMaterialData()->setJournal(null); } } @@ -310,17 +379,17 @@ public function updateAction($id, Request $request) if ($editForm->has('save_and_show')) { if ($editForm->get('save_and_show')->isClicked()) { - return $this->redirect($this->generateUrl('admin_order_show', array('id' => $id))); + return $this->redirect($this->generateUrl('admin_order_show', ['id' => $id])); } } - return $this->redirect($this->generateUrl('admin_order_edit', array('id' => $id))); + return $this->redirect($this->generateUrl('admin_order_edit', ['id' => $id])); } - return array( + return [ 'entity' => $entity, 'edit_form' => $editForm->createView(), - ); + ]; } /**