public function apiPreferencesAction(Request $request)
 {
     $em = $this->getDoctrine()->getManager();
     if ($this->get('request')->getMethod() == 'OPTIONS') {
         $returnResponse = new Response(json_encode(array('accept' => true)));
         $returnResponse->headers->set('Content-Type', 'application/json');
         $returnResponse->headers->set('Access-Control-Allow-Origin', '*');
         $returnResponse->headers->set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
         $returnResponse->headers->set('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS');
         return $returnResponse;
     }
     $return = array();
     $em = $this->getDoctrine()->getManager();
     $userEntity = $em->getRepository('InfotapAdminBundle:User')->findOneBy(array('accessToken' => $request->query->get('token')));
     if ($userEntity) {
         $userPreferences = $em->getRepository('InfotapAdminBundle:UserPreference')->findBy(array('user' => $userEntity));
         foreach ($userPreferences as $userPreference) {
             $userdeptarr[] = $userPreference->getDept()->getId();
             $em->remove($userPreference);
         }
         $em->flush();
         $userPreferencesStr = $request->query->get('departments');
         if ($userPreferencesStr) {
             $userPreferences = explode(",", $userPreferencesStr);
             foreach ($userPreferences as $userPreferenceId) {
                 $userDept = $em->getRepository('InfotapAdminBundle:Department')->find($userPreferenceId);
                 $userPreference = new UserPreference();
                 $userPreference->setDept($userDept);
                 $userPreference->setUser($userEntity);
                 $em->persist($userPreference);
             }
         }
         $em->flush();
         $return['success'] = true;
         $return['message'] = "Preferences saved successfully";
     } else {
         $return['success'] = false;
         $return['message'] = "Something went wrong";
     }
     $returnResponse = new Response(json_encode($return));
     $returnResponse->headers->set('Content-Type', 'application/json');
     $returnResponse->headers->set('Access-Control-Allow-Origin', '*');
     $returnResponse->headers->set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
     $returnResponse->headers->set('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS');
     return $returnResponse;
 }
 public function apiRegister2Action(Request $request)
 {
     $em = $this->getDoctrine()->getManager();
     if ($this->get('request')->getMethod() == 'OPTIONS') {
         $returnResponse = new Response(json_encode(array('accept' => true)));
         $returnResponse->headers->set('Content-Type', 'application/json');
         $returnResponse->headers->set('Access-Control-Allow-Origin', '*');
         $returnResponse->headers->set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
         $returnResponse->headers->set('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS');
         return $returnResponse;
     }
     $return = new \stdClass();
     $userEntity = null;
     $userEntity = $em->getRepository('InfotapAdminBundle:User')->findOneBy(array('mobile' => $request->get('mobile')));
     if ($request->get('code') && $userEntity) {
         if ($request->get('code') == $userEntity->getOtpCode()) {
             $userEntity->setAadharId($request->query->get('uid'));
             $userEntity->setName($request->query->get('name'));
             $userEntity->setEmail($request->query->get('email'));
             $userEntity->setMobile($request->query->get('mobile'));
             $userEntity->setLocation($request->query->get('location'));
             $userEntity->setArea($request->query->get('area'));
             $userEntity->setCity($request->query->get('city'));
             $userEntity->setState($request->query->get('state'));
             $userEntity->setAndroidRegId($request->query->get('androidRegId'));
             if (is_array($request->query->get('gender'))) {
                 $gender = $request->query->get('gender');
                 $userEntity->setGender($gender[1]);
             }
             $day = null;
             $month = null;
             $year = null;
             if (is_array($request->query->get('day'))) {
                 $dayarr = $request->query->get('day');
                 $day = $dayarr[1];
             }
             if (is_array($request->query->get('month'))) {
                 $montharr = $request->query->get('month');
                 $month = $montharr[1];
             }
             if (is_array($request->query->get('year'))) {
                 $yeararr = $request->query->get('year');
                 $year = $yeararr[1];
             }
             if ($day && $month && $year) {
                 $dob = \DateTime::createFromFormat('d-m-Y', $day . '-' . $month . '-' . $year);
                 $userEntity->setDob($dob);
             }
             $userEntity->setStatus(1);
             $userEntity->setOtpCode(null);
             $em->persist($userEntity);
             $departments = $em->getRepository('InfotapAdminBundle:Department')->findAll();
             foreach ($departments as $department) {
                 $userPreference = new UserPreference();
                 $userPreference->setDept($department);
                 $userPreference->setUser($userEntity);
                 $em->persist($userPreference);
             }
             $userEntity->setAccessToken($userEntity->getId() . '-' . bin2hex(openssl_random_pseudo_bytes(16)));
             $em->flush();
             $userObject = new \stdClass();
             $userObject->token = $userEntity->getAccessToken();
             $userObject->name = $userEntity->getName();
             $return->success = true;
             $return->user = $userObject;
             $return->message = "Mobile Verification successful.";
         } else {
             $return->success = false;
             $return->message = "Invalid verification code.";
         }
     } else {
         $return->success = false;
         $return->message = "Invalid verification code.";
     }
     $returnResponse = new Response(json_encode($return));
     $returnResponse->headers->set('Content-Type', 'application/json');
     $returnResponse->headers->set('Access-Control-Allow-Origin', '*');
     $returnResponse->headers->set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
     $returnResponse->headers->set('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS');
     return $returnResponse;
 }