/**
  * update LocalAuthor Location
  *
  * @param \LocalAuthor $localAuthor
  * @param Integer $cityId
  * @return \Location
  */
 public function updateLocalAuthorLocation($localAuthor, $countryName, $cityName)
 {
     // update parameters, localAuthor, country name, city name
     $location = $this->entityManager->getRepository('BugglMainBundle:Location')->getByLocalAuthor($localAuthor, true);
     $location->setLocalAuthor($localAuthor);
     $country = $this->entityManager->getRepository('BugglMainBundle:Country')->findOneByName($countryName);
     if (!is_null($country)) {
         $city = $this->entityManager->getRepository('BugglMainBundle:City')->findOneBy(array('name' => $cityName, 'country' => $country));
         if (is_null($city)) {
             $city = new City();
             $city->setCountry($country);
             $city->setName($cityName);
             $this->entityManager->persist($city);
         }
         $location->setCity($city);
         $this->entityManager->persist($location);
         if ($localAuthor->getIsLocalAuthor() == 0) {
             $localAuthor->setIsLocalAuthor(1);
             $this->entityManager->persist($localAuthor);
             $message = \Swift_Message::newInstance();
             $message->setSubject('You’ve decided to share your knowledge on Buggl');
             $message->setFrom($this->constants->get('BUGGL_EMAIL'));
             $message->setTo($localAuthor->getEmail());
             $message->setBody($this->templating->render('BugglMainBundle:Notification:localAuthorUpgradeNotification.html.twig'), 'text/html');
             $this->mailer->send($message);
         }
         $this->entityManager->persist($location);
         $this->entityManager->flush();
     }
     return $location;
 }
 private function saveEGuideCities($cities = array(), $coords = array())
 {
     // var_dump($cities);
     $guideToCities = $this->getDoctrine()->getRepository('BugglMainBundle:EGuideToCity')->findBy(array('e_guide' => $this->eguide));
     if (count($guideToCities)) {
         foreach ($guideToCities as $guideToCity) {
             $key = array_search($guideToCity->getCity()->getName(), $cities);
             if (!$key) {
                 $em = $this->getDoctrine()->getEntityManager();
                 $em->remove($guideToCity);
                 $em->flush();
             } else {
                 unset($cities[$key]);
                 $cities = array_values($cities);
             }
         }
     }
     // exit;
     $country = $this->eguide->getCountry();
     if (count($cities)) {
         foreach ($cities as $cName) {
             $em = $this->getDoctrine()->getEntityManager();
             $city = $this->getDoctrine()->getRepository('BugglMainBundle:City')->findOneBy(array('country' => $country, 'name' => $cName));
             if (!$city) {
                 $city = new City();
                 $city->setName($cName);
                 $city->setCountry($country);
                 $city->setLat($coords[$cName]['lat']);
                 $city->setLong($coords[$cName]['lng']);
                 $em->persist($city);
                 $em->flush();
             }
             $egtc = new EGuideToCity();
             $egtc->setEGuide($this->eguide);
             $egtc->setCity($city);
             $em->persist($egtc);
             $em->flush();
         }
     }
 }
 public function getLong()
 {
     $this->__load();
     return parent::getLong();
 }
 public function registerLocalAuthor($data)
 {
     $password = isset($data['password']['first']) ? $this->encoder->encodePassword($data['password']['first'], '') : null;
     $email = isset($data['fbId']) ? $data['email'] : $data['email']['first'];
     $localAuthor = new LocalAuthor();
     $localAuthor->setFirstName($data['firstName']);
     $localAuthor->setLastName($data['lastName']);
     $localAuthor->setEmail($email);
     $localAuthor->setPassword($password);
     $localAuthor->setEmailVerified(1);
     // (isset($data['fbEmail']) && $email == $data['fbEmail']) ?  $this->constants->get('EMAIL_VERIFIED') : $this->constants->get('EMAIL_UNVERIFIED')
     $localAuthor->setIsLocalAuthor($data['type']);
     $localAuthor->setDateJoined(new \DateTime(date('Y-m-d H:i:s')));
     $localAuthor->setStatus(0);
     $localAuthor->setStreetCredit(0);
     $localAuthor->setIsApproved(1);
     $localAuthor->setIsBetaParticipant(isset($data['betaToken']));
     $localAuthor->setLastActive(new \DateTime(date('Y-m-d H:i:s')));
     $localAuthor->setLastInactiveNotificationSent(new \DateTime(date('Y-m-d H:i:s')));
     $this->entityManager->persist($localAuthor);
     $bypassLocation = true;
     if ($data['type'] == 1 and !$bypassLocation) {
         // this is in preparation for when location would be non-required : Nash Lesigon - May 8, 2014
         if (strlen(trim($data['country'])) && strlen(trim($data['city']))) {
             $location = new Location();
             $location->setLocalAuthor($localAuthor);
             // find city by name, if not found create new city
             // data['city], data['country']
             $country = $this->entityManager->getRepository('BugglMainBundle:Country')->findOneByName($data['country']);
             if (!is_null($country)) {
                 $city = $this->entityManager->getRepository('BugglMainBundle:City')->findOneBy(array('name' => $data['city'], 'country' => $country));
                 if (is_null($city)) {
                     $city = new City();
                     $city->setCountry($country);
                     $city->setName($data['city']);
                     $this->entityManager->persist($city);
                 }
                 $location->setCity($city);
                 $this->entityManager->persist($location);
             }
         }
     }
     $this->entityManager->flush();
     $profile = new Profile();
     $profile->setLocalAuthor($localAuthor);
     if (isset($data['fbId'])) {
         $this->socialMediaService->saveFacebookInfo($data, $localAuthor);
         $tempPath = 'uploads/profile_pics';
         $uploadRootDir = $this->serviceContainer->get('kernel')->getRootdir() . '/../web/' . $tempPath;
         $filename = 'fb_' . $data['fbId'] . '.jpg';
         copy('https://graph.facebook.com/' . $data['fbId'] . '/picture?width=285&height=285', $uploadRootDir . '/' . $filename);
         $profile->setProfilePic($filename);
         $profile->setWork($data['fbWork']);
         $profile->setBirthDate($data['fbBirthday']);
         $this->entityManager->persist($profile);
         $this->entityManager->flush();
     } else {
         if (isset($data['twitterId'])) {
             $this->socialMediaService->saveTwitterInfo($data, $localAuthor);
         } else {
             if (isset($data['googlePlusId'])) {
                 $this->socialMediaService->saveGooglePlusInfo($data, $localAuthor);
             } else {
                 $this->entityManager->persist($profile);
                 $this->entityManager->flush();
             }
         }
     }
     return $localAuthor;
 }