/** * @Route( * "/submit-ticket/{id}", * name="diamante_embedded_form_submit", * requirements={"id"="[-\d\w]+"}, * ) */ public function formAction(EmbeddedForm $formEntity) { $response = new Response(); $response->setPublic(); //$response->setEtag($formEntity->getId() . $formEntity->getUpdatedAt()->format(\DateTime::ISO8601)); if ($response->isNotModified($this->getRequest())) { return $response; } $command = new EmbeddedTicketCommand(); $formManager = $this->get('oro_embedded_form.manager'); $form = $formManager->createForm($formEntity->getFormType()); if (in_array($this->getRequest()->getMethod(), ['POST', 'PUT'])) { $data = $this->getRequest()->get('diamante_embedded_form'); //Initialize Reporter $diamanteUserRepository = $this->get('diamante.user.repository'); $diamanteUser = $diamanteUserRepository->findUserByEmail($data['emailAddress']); if (is_null($diamanteUser)) { $diamanteUser = $this->get('diamante.user_factory')->create($data['emailAddress'], $data['firstName'], $data['lastName']); $diamanteUserRepository->store($diamanteUser); } $reporterId = $diamanteUser->getId(); $reporter = new User($reporterId, User::TYPE_DIAMANTE); //Set Command for embedded form $command->reporter = $reporter; $command->priority = Priority::PRIORITY_MEDIUM; $command->source = Source::WEB; $command->status = Status::NEW_ONE; $command->branch = $formEntity->getBranch(); $command->subject = $data['subject']; $command->description = $data['description']; if ($formEntity->getBranch() && $formEntity->getBranch()->getDefaultAssignee()) { $assignee = $formEntity->getBranch()->getDefaultAssignee(); } else { $assignee = null; } $command->assignee = $assignee; $form->handleRequest($this->getRequest()); if ($form->isValid()) { $command->branch = $formEntity->getBranch()->getId(); $this->get('diamante.ticket.service')->createTicket($command); return $this->redirect($this->generateUrl('oro_embedded_form_success', ['id' => $formEntity->getId()])); } } $formView = $form->createView(); $formView->children['attachmentsInput']->vars = array_replace($formView->children['attachmentsInput']->vars, array('full_name' => 'diamante_embedded_form[attachmentsInput][]')); // TODO: Next code should be refactored. // TODO: Should be changed due to new EmbeddedFormBundle requirements $formResponse = $this->render('DiamanteEmbeddedFormBundle::embeddedForm.html.twig', ['form' => $formView, 'formEntity' => $formEntity]); $layoutManager = $this->get('oro_embedded_form.embed_form_layout_manager'); $layoutContent = $layoutManager->getLayout($formEntity, $form)->render(); $replaceString = '<div id="page">'; $response->setContent(str_replace($replaceString, $replaceString . $formResponse->getContent(), $layoutContent)); return $response; }
/** * @Route("/submit/{id}", name="oro_embedded_form_submit", requirements={"id"="[-\d\w]+"}) */ public function formAction(EmbeddedForm $formEntity, Request $request) { $response = new Response(); $response->setPublic(); $response->setEtag($formEntity->getId() . $formEntity->getUpdatedAt()->format(\DateTime::ISO8601)); if ($response->isNotModified($request)) { return $response; } /** @var EntityManager $em */ $em = $this->get('doctrine.orm.entity_manager'); /** @var EmbeddedFormManager $formManager */ $formManager = $this->get('oro_embedded_form.manager'); $form = $formManager->createForm($formEntity->getFormType()); if (in_array($request->getMethod(), ['POST', 'PUT'])) { $dataClass = $form->getConfig()->getOption('data_class'); if (isset($dataClass) && class_exists($dataClass)) { $ref = new \ReflectionClass($dataClass); $constructor = $ref->getConstructor(); $data = $constructor && $constructor->getNumberOfRequiredParameters() ? $ref->newInstanceWithoutConstructor() : $ref->newInstance(); $form->setData($data); } else { $data = []; } $event = new EmbeddedFormSubmitBeforeEvent($data, $formEntity); $eventDispatcher = $this->get('event_dispatcher'); $eventDispatcher->dispatch(EmbeddedFormSubmitBeforeEvent::EVENT_NAME, $event); $form->submit($request); $event = new EmbeddedFormSubmitAfterEvent($data, $formEntity, $form); $eventDispatcher->dispatch(EmbeddedFormSubmitAfterEvent::EVENT_NAME, $event); } if ($form->isValid()) { $entity = $form->getData(); /** * Set owner ID (current organization) to concrete form entity */ $entityClass = ClassUtils::getClass($entity); $config = $this->get('oro_entity_config.provider.ownership'); $entityConfig = $config->getConfig($entityClass); $formEntityConfig = $config->getConfig($formEntity); if ($entityConfig->get('owner_type') === OwnershipType::OWNER_TYPE_ORGANIZATION) { $accessor = PropertyAccess::createPropertyAccessor(); $accessor->setValue($entity, $entityConfig->get('owner_field_name'), $accessor->getValue($formEntity, $formEntityConfig->get('owner_field_name'))); } $em->persist($entity); $em->flush(); return $this->redirect($this->generateUrl('oro_embedded_form_success', ['id' => $formEntity->getId()])); } /** @var EmbedFormLayoutManager $layoutManager */ $layoutManager = $this->get('oro_embedded_form.embed_form_layout_manager'); $response->setContent($layoutManager->getLayout($formEntity, $form)->render()); return $response; }