public function putEditAction(Request $request) { $institutionInquiry = $this->getDoctrine()->getRepository('InstitutionBundle:InstitutionInquiry')->find($request->get('id', 0)); if (!$institutionInquiry) { throw $this->createNotFoundException('Invalid institution inquiry'); } $form = $this->createForm(new InstitutionInquiryApiFormType(), $institutionInquiry); $contentData = \json_decode($request->getContent(false), true); $formData = $contentData[$form->getName()]; $oldStatus = $institutionInquiry->getStatus(); $form->bind($formData); //$form->bind($this->getRequest()); if ($form->isValid()) { $this->get('services.institution.inquiry')->save($institutionInquiry); $response = $this->createResponseAsJson(InstitutionInquiryService::toArray($institutionInquiry), 200); //Listener for NOTIFICATIONS_INQUIRIES events is configured to rethrow any exceptions encountered. try { // only fire mailer event if status was changed from unapproved if ($oldStatus == InstitutionInquiry::STATUS_UNAPPROVED && $institutionInquiry->getStatus() != InstitutionInquiry::STATUS_UNAPPROVED) { $inquiry = $this->get('event_dispatcher')->dispatch(MailerBundleEvents::NOTIFICATIONS_INQUIRIES, new GenericEvent($institutionInquiry)); } } catch (\Exception $e) { //TODO: Mark this inquiry as having failed to send notifications //ignored for now } } else { $response = $this->createResponseFromFormErrors($form); } return $response; }
/** * TODO: rename function since this is misleading * * @param Request $request * @return \Symfony\Component\HttpFoundation\Response */ public function ajaxSaveInquiryAction(Request $request) { $institutionInquiry = new InstitutionInquiry(); $form = $this->createForm(new InstitutionInquiryFormType(), $institutionInquiry); $form->bindRequest($request); if ($form->isValid()) { if ($request->get('imcId')) { $imc = $this->getDoctrine()->getRepository('InstitutionBundle:InstitutionMedicalCenter')->find($request->get('imcId')); $institution = $imc->getInstitution(); $institutionInquiry->setInstitutionMedicalCenter($imc); } else { $institution = $this->getDoctrine()->getRepository('InstitutionBundle:Institution')->find($request->get('institutionId')); } $institutionInquiry->setInstitution($institution); $institutionInquiry->setRemoteAddress($request->server->get('REMOTE_ADDR')); $institutionInquiry->setHttpUseAgent($request->server->get('HTTP_USER_AGENT')); $institutionInquiry->setStatus(InstitutionInquiry::STATUS_UNAPPROVED); $em = $this->getDoctrine()->getEntityManager(); $em->persist($institutionInquiry); $em->flush(); $subscribed = false; if ($form->get('newsletterSubscription')->getData()) { $subscribed = $this->get('services.mailchimp')->listSubscribe($institutionInquiry->getInquirerEmail()); } $responseData = array('institutionInquiry' => InstitutionInquiryService::toArray($institutionInquiry), 'subscribed' => !($subscribed === false)); $response = new Response(\json_encode($responseData), 200, array('content-type' => 'application/json')); } else { $errors = array(); $form_errors = $this->get('validator')->validate($form); foreach ($form_errors as $_err) { $errors[] = array('field' => str_replace('data.', '', $_err->getPropertyPath()), 'error' => $_err->getMessage()); } $captchaError = $form->get('captcha')->getErrors(); if (count($captchaError)) { $errors[] = array('field' => $form->get('captcha')->getName(), 'error' => $captchaError[0]->getMessageTemplate()); } $response = new Response(\json_encode(array('html' => $errors)), 400, array('content-type' => 'application/json')); } return $response; }