/**
  *
  * @param Request $request
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function ajaxLoadSpecializationTreatmentsAction(Request $request)
 {
     $selectedTreatments = array();
     $specializationId = $request->get('specializationId');
     if ($this->institutionSpecialization && $this->institutionSpecialization->getTreatments()) {
         foreach ($this->institutionSpecialization->getTreatments() as $treatment) {
             $selectedTreatments[] = $treatment->getId();
         }
     }
     $form = $this->createForm(new InstitutionSpecializationFormType(), new InstitutionSpecialization());
     $specializationTreatments = $this->get('services.treatment_bundle')->getTreatmentsBySpecializationIdGroupedBySubSpecialization($specializationId);
     $html = $this->renderView('InstitutionBundle:Specialization/Widgets:form.specializationTreatments.html.twig', array('form' => $form->createView(), 'formName' => InstitutionSpecializationFormType::NAME, 'specializationId' => $specializationId, 'selectedTreatments' => $selectedTreatments, 'specializationTreatments' => $specializationTreatments, 'isId' => $this->institutionSpecialization ? $this->institutionSpecialization->getId() : null));
     return new Response(\json_encode(array('html' => $html)), 200, array('content-type' => 'application/json'));
 }
 public function getAvailableTreatments(InstitutionSpecialization $institutionSpecialization)
 {
     $qb = $this->_em->createQueryBuilder();
     $sql = "SELECT i.`treatment_id` as treatment_id FROM `institution_treatments` i WHERE i.`institution_specialization_id` = :institutionSpecializationId";
     $statement = $this->getEntityManager()->getConnection()->prepare($sql);
     $statement->execute(array('institutionSpecializationId' => $institutionSpecialization->getId()));
     $result = array();
     $ids = array();
     while ($row = $statement->fetch(Query::HYDRATE_ARRAY)) {
         $ids[] = $row['treatment_id'];
     }
     $hasIds = $statement->rowCount() > 0;
     if ($hasIds) {
         $dql = "SELECT t, s FROM TreatmentBundle:Treatment t LEFT JOIN t.subSpecializations s WHERE " . " t.id NOT IN (?1) " . "AND t.specialization = :specializationId AND t.status = :status ";
         $query = $this->_em->createQuery($dql)->setParameter(1, $ids)->setParameter('specializationId', $institutionSpecialization->getSpecialization()->getId())->setParameter('status', Treatment::STATUS_ACTIVE);
     } else {
         $dql = "SELECT t, s FROM TreatmentBundle:Treatment t LEFT JOIN t.subSpecializations s WHERE t.specialization = :specializationId AND t.status = :status";
         $query = $this->_em->createQuery($dql)->setParameter('specializationId', $institutionSpecialization->getSpecialization()->getId())->setParameter('status', Treatment::STATUS_ACTIVE);
     }
     return $query->getResult();
 }
 /**
  * Add a new specialization to medical center
  *
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function addSpecializationAction(Request $request)
 {
     $service = $this->get('services.institution_medical_center');
     if ($request->isMethod('POST')) {
         $submittedSpecializations = $request->get(InstitutionSpecializationFormType::NAME);
         $em = $this->getDoctrine()->getEntityManager();
         if (\count($submittedSpecializations) > 0) {
             foreach ($submittedSpecializations as $specializationId => $_data) {
                 $specialization = $this->get('services.treatment_bundle')->getSpecialization($specializationId);
                 $_institutionSpecialization = new InstitutionSpecialization();
                 $_institutionSpecialization->setSpecialization($specialization);
                 $_institutionSpecialization->setInstitutionMedicalCenter($this->institutionMedicalCenter);
                 $_institutionSpecialization->setStatus(InstitutionSpecialization::STATUS_ACTIVE);
                 $_institutionSpecialization->setDescription('');
                 // set passed treatments as choices
                 $default_choices = array();
                 if ($_data['treatments'] != '') {
                     $_treatment_choices = $this->get('services.treatment_bundle')->findTreatmentsByIds($_data['treatments']);
                     foreach ($_treatment_choices as $_t) {
                         $default_choices[$_t->getId()] = $_t->getName();
                         // add the treatment
                         $_institutionSpecialization->addTreatment($_t);
                     }
                     $form = $this->createForm(new InstitutionSpecializationFormType(), $_institutionSpecialization, array('default_choices' => $default_choices));
                     $form->bind($_data);
                     if ($form->isValid()) {
                         $em->persist($_institutionSpecialization);
                         $em->flush();
                         // Invalidate InstitutionMedicalCenter Profile cache
                         $this->get('services.memcache')->delete(FrontendMemcacheKeysHelper::generateInsitutionMedicalCenterProfileKey($this->institutionMedicalCenter->getId()));
                         // Invalidate Institution Profile cache
                         $this->get('services.memcache')->delete(FrontendMemcacheKeysHelper::generateInsitutionProfileKey($this->institutionMedicalCenter->getInstitution()->getId()));
                         return $this->redirect($this->generateUrl('admin_institution_medicalCenter_view', array('institutionId' => $this->institution->getId(), 'imcId' => $this->institutionMedicalCenter->getId())));
                     } else {
                         $request->getSession()->setFlash('notice', '<ul><li>Unable to save specializations. Please try again.</li></ul>');
                     }
                 } else {
                     $request->getSession()->setFlash('notice', '<ul><li> Please provide at least one treatment.</li></ul>');
                 }
             }
         } else {
             $request->getSession()->setFlash('notice', '<ul><li> Please provide at least one specialization.</li></ul>');
         }
     } else {
         $form = $this->createForm(new InstitutionSpecializationSelectorFormType());
         $assignedSpecialization = $this->getDoctrine()->getRepository('InstitutionBundle:InstitutionSpecialization')->findByInstitutionMedicalCenter($this->institutionMedicalCenter);
         $specializations = $this->getDoctrine()->getRepository('TreatmentBundle:Specialization')->getAvailableSpecializations($assignedSpecialization);
         $specializationArr = array();
         foreach ($specializations as $e) {
             $specializationArr[] = array('value' => $e->getName(), 'id' => $e->getId());
         }
     }
     $params = array('form' => $form->createView(), 'institution' => $this->institution, 'institutionMedicalCenter' => $this->institutionMedicalCenter, 'selectedSubMenu' => 'centers', 'specializationsJSON' => \json_encode($specializationArr));
     return $this->render('AdminBundle:InstitutionSpecialization:addSpecializations.html.twig', $params);
 }