/**
  * 
  * @param AbstractType $form
  * @return \Symfony\Component\HttpFoundation\Response
  */
 protected function createResponseFromFormErrors(Form $form)
 {
     $errors = array();
     ErrorValidationHelper::processFormErrorsDeeply($form, $errors);
     $response = $this->createResponseAsJson($errors, 400);
     return $response;
 }
 public function processMergeTreatmentAction(Request $request)
 {
     $tokenForm = $this->buildTokenForm();
     $tokenForm->bind(array('_token' => $request->get('_token')));
     if ($tokenForm->isValid()) {
         $specialization = $this->getDoctrine()->getRepository('TreatmentBundle:Specialization')->find($request->get('specialization', 0));
         if (!$specialization) {
             throw $this->createNotFoundException('Invalid specialization');
         }
         $treatmentRepo = $this->getDoctrine()->getRepository('TreatmentBundle:Treatment');
         $fromTreatment = $treatmentRepo->find($request->get('fromTreatment', 0));
         $toTreatment = $treatmentRepo->find($request->get('toTreatment', 0));
         if (!$fromTreatment) {
             throw $this->createNotFoundException('Invalid `from treatment`');
         }
         if (!$toTreatment) {
             throw $this->createNotFoundException('Invalid `to treatment`');
         }
         // check that both treatments belong to $specialization
         if ($fromTreatment->getSpecialization()->getId() != $specialization->getId()) {
             throw $this->createNotFoundException('From treatment must belong to Specialization ' . $specialization->getId());
         }
         if ($toTreatment->getSpecialization()->getId() != $specialization->getId()) {
             throw $this->createNotFoundException('To treatment must belong to Specialization ' . $specialization->getId());
         }
         try {
             $oldFromTreatmentData = array('id' => $fromTreatment->getId(), 'name' => $fromTreatment->getName());
             // DO THE MERGING HERE
             $this->get('services.admin.treatmentMigrationTool')->mergeTreatmentToAnotherTreatment($fromTreatment, $toTreatment);
             $eventData = new LogEventData();
             $eventData->setMessage(\sprintf("Merged %s to %s", $oldFromTreatmentData['name'], $toTreatment->getName()));
             $eventData->setData(array('fromTreatment' => $oldFromTreatmentData, 'toTreatment' => array('id' => $toTreatment->getId(), 'name' => $toTreatment->getName())));
             // FIRE EVENT
             $this->get('event_dispatcher')->dispatch(AdminBundleEvents::ON_ADMIN_MERGE_TREATMENT, $this->get('events.factory')->create(AdminBundleEvents::ON_ADMIN_MERGE_TREATMENT, $eventData));
             $responseStatus = 200;
             $responseData = array('toTreatmentId' => $toTreatment->getId(), 'redirectUrl' => $this->generateUrl('admin_specialization_manage', array('id' => $specialization->getId())));
         } catch (\Exception $e) {
             $responseStatus = 500;
             $responseData = array('error' => $e->getMessage());
         }
         $response = new Response(\json_encode($responseData), $responseStatus, array('content-type' => 'application/json'));
     } else {
         $errors = array();
         ErrorValidationHelper::processFormErrorsDeeply($tokenForm, $errors);
         $response = new Response(\json_encode(array('errors' => $errors)), 400, array('content-type' => 'application/json'));
     }
     return $response;
 }