/**
  * Transforms an object (City) to a string.
  *
  * @param City|null $city
  * @return string
  */
 public function transform($city)
 {
     if (null === $city) {
         return "";
     }
     $cityName = $city->getName();
     $state = $city->getState();
     $stateName = $state->getAcronym();
     return sprintf("%s, %s", $cityName, $stateName);
 }
 private function checkAddressLocation(&$address, $form, &$em)
 {
     $form = $form->get('location');
     if (!$address->getCountry()) {
         $form->get('country')->addError(new FormError($this->get('translator')->trans('required.field')));
         return false;
     }
     $isPreferred = $em->getRepository('PROCERGSLoginCidadaoCoreBundle:Country')->isPreferred($address->getCountry());
     if (!$address->getState() && !$isPreferred) {
         $steppe = ucwords(strtolower(trim($form->get('statesteppe')->getData())));
         if ($steppe) {
             $repo = $em->getRepository('PROCERGSLoginCidadaoCoreBundle:State');
             $ent = $repo->findOneBy(array('name' => $steppe, 'country' => $address->getCountry()));
             if (!$ent) {
                 $ent = new State();
                 $ent->setName($steppe);
                 $ent->setCountry($address->getCountry());
                 $em->persist($ent);
             }
             $address->setState($ent);
         }
     }
     if (!$address->getState()) {
         $form->get('state')->addError(new FormError($this->get('translator')->trans('required.field')));
         return false;
     }
     if (!$address->getCity() && !$isPreferred) {
         $steppe = ucwords(strtolower(trim($form->get('citysteppe')->getData())));
         if ($address->getState()) {
             $state = $address->getState();
         } elseif (isset($ent)) {
             $state = $ent;
         } else {
             $state = null;
         }
         if ($state && $steppe) {
             $repo = $em->getRepository('PROCERGSLoginCidadaoCoreBundle:City');
             $ent = $repo->findOneBy(array('name' => $steppe, 'state' => $state));
             if (!$ent) {
                 $ent = new City();
                 $ent->setName($steppe);
                 $ent->setState($state);
                 $em->persist($ent);
             }
             $address->setCity($ent);
         }
     }
     if (!$address->getCity()) {
         $form->get('city')->addError(new FormError($this->get('translator')->trans('required.field')));
         return false;
     }
     return true;
 }
Exemplo n.º 3
0
 public function onProfileEditSuccess(FormEvent $event)
 {
     $user = $event->getForm()->getData();
     if (!$user->getState()) {
         $steppe = ucwords(strtolower(trim($event->getForm()->get('ufsteppe')->getData())));
         if ($steppe) {
             if ($user->getCountry()) {
                 $isPreferred = $this->em->getRepository('PROCERGSLoginCidadaoCoreBundle:Country')->isPreferred($user->getCountry());
                 if ($isPreferred) {
                     throw new LcValidationException('restrict.location.creation');
                 }
             } else {
                 throw new LcValidationException('required.field.country');
             }
             $repo = $this->em->getRepository('PROCERGSLoginCidadaoCoreBundle:State');
             $ent = $repo->findOneBy(array('name' => $steppe, 'country' => $user->getCountry()));
             if (!$ent) {
                 $ent = new State();
                 $ent->setName($steppe);
                 $ent->setCountry($user->getCountry());
                 $this->em->persist($ent);
             }
             $user->setState($ent);
         }
     }
     if (!$user->getCity()) {
         $steppe = ucwords(strtolower(trim($event->getForm()->get('citysteppe')->getData())));
         if ($steppe) {
             if ($user->getState()) {
                 $isPreferred = $this->em->getRepository('PROCERGSLoginCidadaoCoreBundle:Country')->isPreferred($user->getState()->getCountry());
                 if ($isPreferred) {
                     throw new LcValidationException('restrict.location.creation');
                 }
             } else {
                 throw new LcValidationException('required.field.state');
             }
             $repo = $this->em->getRepository('PROCERGSLoginCidadaoCoreBundle:City');
             $ent = $repo->findOneBy(array('name' => $steppe, 'state' => $user->getState()));
             if (!$ent) {
                 $ent = new City();
                 $ent->setName($steppe);
                 $ent->setState($user->getState());
                 $this->em->persist($ent);
             }
             $user->setCity($ent);
         }
     }
     $this->checkEmailChanged($user);
     // default:
     $url = $this->router->generate('fos_user_profile_edit');
     $event->setResponse(new RedirectResponse($url));
 }
Exemplo n.º 4
0
 private function registerTextualCity(\Symfony\Component\Form\FormEvent $event)
 {
     $data = $event->getForm()->getData();
     if (!$data instanceof SelectData) {
         return;
     }
     $form = $event->getForm();
     if ($form->has('city_text')) {
         $cityTextInput = $form->get('city_text')->getData();
         $cityText = ucwords(strtolower(trim($cityTextInput)));
         if (!$cityText) {
             return;
         }
         if (!$data->getState()) {
             throw new LcValidationException('required.field.state');
         }
         $isPreferred = $this->em->getRepository('PROCERGSLoginCidadaoCoreBundle:Country')->isPreferred($data->getCountry());
         if ($isPreferred) {
             throw new LcValidationException('restrict.location.creation');
         }
         $repo = $this->em->getRepository('PROCERGSLoginCidadaoCoreBundle:City');
         $city = $repo->findOneBy(array('name' => $cityText, 'state' => $data->getState()));
         if (!$city) {
             $city = new City();
             $city->setName($cityText);
             $city->setState($data->getState());
             $this->em->persist($city);
         }
         $data->setCity($city);
     }
 }