/** * @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; }
/** * @param EmbeddedForm $formEntity * @param FormInterface $form * * @return Layout */ public function getLayout(EmbeddedForm $formEntity, FormInterface $form = null) { $formTypeName = $formEntity->getFormType(); $customLayout = $this->formManager->getCustomFormLayoutByFormType($formTypeName); $layoutContext = new LayoutContext(); $layoutContext->getResolver()->setRequired(['embedded_form_type'])->setOptional(['embedded_form', 'embedded_form_custom_layout']); $layoutContext->set('theme', 'embedded_default'); $layoutContext->set('embedded_form', null === $form ? null : new FormAccessor($form)); $layoutContext->set('embedded_form_type', $formTypeName); $layoutContext->set('embedded_form_custom_layout', $customLayout); $layoutContext->data()->set('embedded_form_entity', '', $formEntity); $layoutBuilder = $this->layoutManager->getLayoutBuilder(); // TODO discuss adding root automatically $layoutBuilder->add('root', null, 'root'); return $layoutBuilder->getLayout($layoutContext); }
/** * @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; }
/** * @test */ public function shouldSetEntityPropertiesAndReturnBack() { $formType = uniqid('AnyFormType'); $css = uniqid('styles'); $title = uniqid('title'); $successMessage = uniqid('success message'); $formEntity = new EmbeddedForm(); $formEntity->setFormType($formType); $formEntity->setCss($css); $formEntity->setTitle($title); $formEntity->setSuccessMessage($successMessage); $this->assertEquals($formType, $formEntity->getFormType()); $this->assertEquals($css, $formEntity->getCss()); $this->assertEquals($title, $formEntity->getTitle()); $this->assertEquals($successMessage, $formEntity->getSuccessMessage()); }
/** * @param ObjectManager $om */ protected function persistDemoEmbeddedForm(ObjectManager $om) { $embeddedForm = new EmbeddedForm(); /** @var ContactRequestType $contactUs */ $contactUs = $this->container->get('orocrm_contact_us.embedded_form'); $embeddedForm->setFormType('orocrm_contact_us.embedded_form'); $embeddedForm->setCss($contactUs->getDefaultCss()); $embeddedForm->setSuccessMessage($contactUs->getDefaultSuccessMessage()); $embeddedForm->setTitle('Contact Us Form'); $embeddedForm->setOwner($this->organization); $embeddedForm->setDataChannel($this->dataChannel); $om->persist($embeddedForm); }
public function load(ObjectManager $manager) { /** @var Organization $organization */ $organization = $manager->getRepository('OroOrganizationBundle:Organization')->getFirst(); /** @var Organization $organization */ $branches = $manager->getRepository('DiamanteDeskBundle:Branch')->getAll(); $branch = current($branches); $ASCIIKey = ord('A'); for ($i = 1; $i <= 10; $i++) { $keySuffix = chr($ASCIIKey + $i); $form = new EmbeddedForm(); $form->setTitle('Form' . $keySuffix); $form->setFormType('diamante_embedded_form.form_type.available_embedded_form'); $form->setSuccessMessage('Ticket has been placed successfully'); $form->setCss(''); $form->setOwner($organization); $form->setBranch($branch); $manager->persist($form); } $manager->flush(); }
/** * @Route("view/{id}", name="oro_embedded_form_view", requirements={"id"="[-\d\w]+"}) * @Template() * @Acl( * id="oro_embedded_form_view", * type="entity", * permission="VIEW", * class="OroEmbeddedFormBundle:EmbeddedForm" * ) */ public function viewAction(EmbeddedForm $entity) { return ['entity' => $entity, 'label' => $this->getFormManager()->get($entity->getFormType())]; }