/**
  * @Route("/{id}", name="view_profile", requirements={"id" = "\d+"})
  * @Template()
  * @ParamConverter("Profile", class="ConnectionUserBundle:Profile")
  */
 public function viewAction(Profile $profile)
 {
     $user = $profile->getUser();
     if ($user->getHide() || $user->getAdmin()) {
         throw new NotFoundHttpException("Profile Not Found");
     }
     if ($this->getUser() == $user) {
         return new RedirectResponse($this->get('router')->generate('edit_user_profile'));
     }
     $userPhotos = $this->getDoctrine()->getRepository('ConnectionUserBundle:Profile\\Image')->getGroupedByGalleryImages($user->getId());
     return array('user' => $user, 'userPhotos' => $userPhotos, 'events' => $user->getEvents());
 }
 public function createNewUser($socialProfile, $type)
 {
     $em = $this->container->get('doctrine')->getManager();
     $user = new User();
     $profile = new Profile();
     $tokenGenerator = $this->container->get('fos_user.util.token_generator');
     $plainPassword = substr($tokenGenerator->generateToken(), 0, 12);
     $password = $this->generatePassword($plainPassword, $user);
     $profile->setSocialId($type, $socialProfile['id']);
     $user->setEnabled(true);
     if (!empty($socialProfile['birthday'])) {
         $profile->setBirthdate(new \DateTime($socialProfile['birthday']));
     }
     if (!empty($socialProfile['first_name'])) {
         $user->setFirstName($socialProfile['first_name']);
     }
     if (!empty($socialProfile['last_name'])) {
         $user->setLastName($socialProfile['last_name']);
     }
     if (empty($socialProfile['username'])) {
         $socialProfile['username'] = implode(" ", array($socialProfile['first_name'], $socialProfile['last_name']));
         if (empty($socialProfile['username'])) {
             $socialProfile['username'] = $socialProfile['id'];
         }
     }
     $user->setUsername($socialProfile['username']);
     if (!empty($socialProfile['email'])) {
         $user->setEmail($socialProfile['email']);
     }
     $user->setPassword($password);
     $user->setProfile($profile);
     $em->persist($user);
     $em->flush();
     //  Send Confirmation Email
     $this->sendEmail($user, $plainPassword);
     return $user;
 }
 /**
  * {@inheritDoc}
  */
 public function prePersist()
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'prePersist', array());
     return parent::prePersist();
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     /* @var $em \Doctrine\ORM\EntityManager */
     $em = $this->getContainer()->get('doctrine')->getManager();
     $userRepo = $em->getRepository('ConnectionUserBundle:User');
     $countryRepo = $em->getRepository('ConnectionCoreBundle:Country');
     $stateRepo = $em->getRepository('ConnectionCoreBundle:State');
     $genderRepo = $em->getRepository('ConnectionUserBundle:Profile\\Gender');
     $eventCategoryRepo = $em->getRepository('ConnectionEventBundle:Category');
     $userManager = $this->getContainer()->get('fos_user.user_manager');
     $rootDir = $this->getContainer()->get('kernel')->getRootDir() . '/../web/';
     $userArray = $this->_getUserArray();
     foreach ($userArray as $userItem) {
         $output->writeln('Creating user: '******'username']);
         $user = $userRepo->findOneBy(array('username' => $userItem['username']));
         if ($user) {
             $em->remove($user);
             $em->flush();
         }
         $email = strtolower($userItem['username']) . '@mail.com';
         $user = new User();
         $user->setUsername($userItem['username']);
         $user->setEmail($email);
         $user->setPlainPassword('123456');
         $user->setEnabled(true);
         $profile = new Profile();
         $gender = $genderRepo->find($userItem['profile']['gender']);
         $profile->setGender($gender);
         $seek = $genderRepo->find($userItem['profile']['seek']);
         $profile->setSeek($seek);
         $profile->setBirthdate($userItem['profile']['birthdate']);
         $country = $countryRepo->findOneBy(array('iso' => $userItem['profile']['country']));
         $profile->setCountry($country);
         $state = $stateRepo->findOneBy(array('name' => $userItem['profile']['state']));
         $profile->setState($state);
         $user->setProfile($profile);
         $em->persist($user);
         $em->flush();
         foreach ($userItem['events'] as $eventItem) {
             $event = new Event();
             $event->setContactName($userItem['username']);
             $event->setTitle($eventItem['title']);
             $event->setDescription($eventItem['description']);
             $event->setEventDate($eventItem['date']);
             $event->setEmail($email);
             $event->setPhone('12345678');
             $event->setUser($user);
             $collection = new ArrayCollection();
             foreach ($eventItem['categories'] as $categoryItem) {
                 $category = $eventCategoryRepo->findOneBy(array('name' => $categoryItem));
                 $collection->add($category);
             }
             $event->setCategory($collection);
             $image = new Image();
             $imageName = $userItem['username'] . 'Event.jpeg';
             $imagePath = 'uploads/user/event/' . $user->getId();
             $pathFrom = $rootDir . 'uploads/user/fixtures/' . $imageName;
             $pathTo = $rootDir . $imagePath . '/' . $imageName;
             mkdir($rootDir . $imagePath);
             copy($pathFrom, $pathTo);
             $image->setName($imageName);
             $image->setPath('/' . $imagePath . '/' . $imageName);
             $event->setImage($image);
             $em->persist($event);
             $em->flush();
         }
         $gallery = $user->getGalleries()->first();
         $gallery->setUser($user);
         $gallery->setTitle('Profile Images');
         $gallery->setDefault(true);
         $image = new Image();
         $imageName = $userItem['username'] . '.jpeg';
         $imagePath = 'uploads/user/profile/' . $user->getId();
         $pathFrom = $rootDir . 'uploads/user/fixtures/' . $imageName;
         $pathTo = $rootDir . $imagePath . '/' . $imageName;
         mkdir($rootDir . $imagePath);
         copy($pathFrom, $pathTo);
         $image->setName($imageName);
         $image->setPath('/' . $imagePath . '/' . $imageName);
         $profile->setAvatar('/' . $imagePath . '/' . $imageName);
         $image->setGallery($gallery);
         $collection = new ArrayCollection();
         $collection->add($image);
         $gallery->setImages($collection);
         $em->persist($gallery);
         $em->flush();
     }
 }