/**
  *
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function setupInstitutionMedicalCenterAction(Request $request)
 {
     $error_message = false;
     if ($this->institutionService->isSingleCenter($this->institution)) {
         // this is not part of the sign up flow of  single center institution
         throw $this->createNotFoundException();
     }
     $this->currentSignUpStep = $this->signUpService->getMultipleCenterSignUpStepByRoute($request->attributes->get('_route'));
     // TODO: check current sign up status
     // We don't assume that there is a medical center instance here already since this is also where we redirect from multiple center institution sign up
     if (!$this->institutionMedicalCenter instanceof InstitutionMedicalCenter) {
         $this->institutionMedicalCenter = new InstitutionMedicalCenter();
         $this->institutionMedicalCenter->setInstitution($this->institution);
     }
     $this->get('services.contact_detail')->initializeContactDetails($this->institutionMedicalCenter, array(ContactDetailTypes::PHONE), $this->institution->getCountry());
     $form = $this->createForm(new InstitutionMedicalCenterFormType($this->institution), $this->institutionMedicalCenter, array(InstitutionMedicalCenterFormType::OPTION_BUBBLE_ALL_ERRORS => false));
     if ($this->request->isMethod('POST')) {
         $formRequestData = $request->get($form->getName());
         if ((bool) $request->get('isSameAddress')) {
             $formRequestData['address'] = json_decode($this->institution->getAddress1(), true);
             $this->institutionMedicalCenter->setAddressHint($this->institution->getAddressHint());
             $this->institutionMedicalCenter->setCoordinates($this->institution->getCoordinates());
         }
         $form->bind($formRequestData);
         if ($form->isValid()) {
             $this->institutionMedicalCenter = $form->getData();
             $institutionMedicalCenterService = $this->get('services.institution_medical_center');
             $this->get('services.contact_detail')->removeInvalidContactDetails($this->institutionMedicalCenter);
             $institutionMedicalCenterService->clearBusinessHours($this->institutionMedicalCenter);
             foreach ($this->institutionMedicalCenter->getBusinessHours() as $_hour) {
                 $_hour->setInstitutionMedicalCenter($this->institutionMedicalCenter);
             }
             $institutionMedicalCenterService->saveAsDraft($this->institutionMedicalCenter);
             // update sign up step status of institution
             $this->_updateInstitutionSignUpStepStatus($this->currentSignUpStep, true);
             // redirect to next step
             $nextStepRoute = $this->signUpService->getMultipleCenterSignUpNextStep($this->currentSignUpStep)->getRoute();
             // TODO: Update this when we have formulated a strategy for our events system
             // We can't use InstitutionBundleEvents; we don't know the consequences of the event firing up other listeners.
             $this->get('event_dispatcher')->dispatch(MailerBundleEvents::NOTIFICATIONS_CLINIC_CREATED, new GenericEvent($this->institutionMedicalCenter, array('userEmail' => $request->getSession()->get('userEmail'))));
             return $this->redirect($this->generateUrl($nextStepRoute, array('imcId' => $this->institutionMedicalCenter->getId())));
         }
         $form_errors = $this->get('validator')->validate($form);
         if ($form_errors) {
             $error_message = 'We need you to correct some of your input. Please check the fields in red.';
         }
     }
     return $this->render('InstitutionBundle:SignUp:setupInstitutionMedicalCenter.html.twig', array('form' => $form->createView(), 'institution' => $this->institution, 'institutionMedicalCenter' => $this->institutionMedicalCenter, 'error_message' => $error_message));
 }
 /**
  * Ajax handler for updating institution medical center by field
  * @param Request $request
  * @author acgvelarde
  */
 public function ajaxUpdateByFieldAction(Request $request)
 {
     $output = array();
     $propertyService = $this->get('services.institution_medical_center_property');
     if (true) {
         try {
             $formVariables = $request->get(InstitutionMedicalCenterFormType::NAME);
             unset($formVariables['_token']);
             $removedFields = \array_diff(InstitutionMedicalCenterFormType::getFieldNames(), array_keys($formVariables));
             $this->get('services.contact_detail')->initializeContactDetails($this->institutionMedicalCenter, array(ContactDetailTypes::PHONE));
             $form = $this->createForm(new InstitutionMedicalCenterFormType($this->institution), $this->institutionMedicalCenter, array(InstitutionMedicalCenterFormType::OPTION_BUBBLE_ALL_ERRORS => false, InstitutionMedicalCenterFormType::OPTION_REMOVED_FIELDS => $removedFields));
             $form->bind($request);
             if ($form->isValid()) {
                 $institutionMedicalCenterService = $this->get('services.institution_medical_center');
                 $this->get('services.contact_detail')->removeInvalidContactDetails($this->institutionMedicalCenter);
                 $responseContent = array();
                 if (isset($formVariables['businessHours'])) {
                     $institutionMedicalCenterService->clearBusinessHours($this->institutionMedicalCenter);
                     foreach ($this->institutionMedicalCenter->getBusinessHours() as $hour) {
                         $hour->setInstitutionMedicalCenter($this->institutionMedicalCenter);
                     }
                     $institutionMedicalCenterService->save($this->institutionMedicalCenter);
                     $responseContent = array('businessHours' => $formVariables['businessHours']);
                 } elseif (isset($form['services'])) {
                     $propertyService->addPropertyForInstitutionMedicalCenterByType($this->institution, $form['services']->getData(), InstitutionPropertyType::TYPE_ANCILLIARY_SERVICE, $this->institutionMedicalCenter);
                     $responseContent = array('services' => $form['services']);
                 } elseif ($awardType = $request->get('awardTypeKey')) {
                     $awardsData = isset($form['awards']) ? $form['awards']->getData() : array();
                     $propertyService->addPropertyForInstitutionMedicalCenterByType($this->institution, $awardsData, InstitutionPropertyType::TYPE_GLOBAL_AWARD, $this->institutionMedicalCenter);
                     $globalAwards = $propertyService->getGlobalAwardPropertiesByInstitutionMedicalCenter($this->institutionMedicalCenter);
                     $responseContent['awardsType'] = $awardType;
                     $responseContent['awardsHtml'] = $this->renderView('InstitutionBundle:Widgets/Profile:globalAwards.html.twig', array('eachAward' => array('list' => $globalAwards[$awardType]), 'type' => $awardType . 's', 'toggleBtnId' => 'clinic-edit-' . $awardType . 's-btn'));
                 } else {
                     $institutionMedicalCenterService->save($this->institutionMedicalCenter);
                     if ($this->isSingleCenter) {
                         // also update the instituion name and description
                         $this->institution->setName($this->institutionMedicalCenter->getName());
                         $this->institution->setDescription($this->institutionMedicalCenter->getDescription());
                         $this->get('services.institution.factory')->save($this->institution);
                     }
                     if (isset($formVariables['address'])) {
                         $formVariables['stringAddress'] = $this->get('services.institutionMedicalCenter.twig.extension')->getCompleteAddressAsString($this->institutionMedicalCenter);
                     }
                     if (isset($formVariables['contactDetails'])) {
                         $formVariables['contactDetails'] = $this->get('services.contact_detail')->getContactDetailsStringValue($this->institutionMedicalCenter->getContactDetails());
                     }
                     $responseContent = array('institutionMedicalCenter' => $formVariables);
                 }
                 // Invalidate InstitutionMedicalCenterProfile memcache
                 $this->get('services.memcache')->delete(FrontendMemcacheKeysHelper::generateInsitutionMedicalCenterProfileKey($this->institutionMedicalCenter->getId()));
                 // Invalidate InstitutionProfile memcache
                 $this->get('services.memcache')->delete(FrontendMemcacheKeysHelper::generateInsitutionProfileKey($this->institutionMedicalCenter->getInstitution()->getId()));
                 $response = new Response(\json_encode($responseContent), 200, array('content-type' => 'application/json'));
             } else {
                 $errors = array();
                 foreach ($form->getChildren() as $field) {
                     if (\count($eachErrors = $field->getErrors())) {
                         $errors[] = array('field' => $field->getName(), 'error' => $eachErrors[0]->getMessageTemplate());
                     }
                 }
                 $response = new Response(\json_encode(array('errors' => $errors)), 400, array('content-type' => 'application/json'));
             }
         } catch (\Exception $e) {
             return new Response($e->getMessage(), 500);
         }
     }
     return $response;
 }