public function RegistedAction(Request $request)
 {
     try {
         $Regis = new RegisF();
         // Create's the register form
         $form = $this->createForm(new RegisFType(), $Regis);
         $form->handleRequest($request);
         // takes the data from the $_POST
         if ($form->isValid()) {
             $factory = $this->get('security.encoder_factory');
             // Creates the security class from the config file
             $user = new \WebVL\WebVillasBundle\Entity\Users();
             // Creates the User object
             $user->setUsername($Regis->getUsername());
             $user->setActivated(false);
             $user->setEmail($Regis->getEmail());
             $ActiveCode = rand(1000000000, 9999999999.0);
             //  var_dump( $ActiveCode);
             $user->setActivationCode($ActiveCode);
             $encoder = $factory->getEncoder($user);
             // Create's the encode for the exact user
             $password = $encoder->encodePassword($Regis->getPassword(), $user->getSalt());
             // Encodes the password
             $user->setPassword($password);
             $user->setJoined(new \DateTime());
             $em = $this->getDoctrine()->getManager();
             $roles = $em->createQuery("SELECT r FROM WebVLWebVillasBundle:Roles r WHERE r.name = 'User'")->getSingleResult();
             // Takes the Roles from the database
             $user->addRole($roles);
             // adding Roles to the user
             $em = $this->getDoctrine()->getManager();
             $em->persist($user);
             $em->flush();
             // saves user to the database
             mail($user->getEmail(), 'Registration on WebVillas', 'Hello ' . $user->getUsername() . " and thank you for registering to WebVillas " . PHP_EOL . 'Your Activation Code Is: ' . $user->getActivationCode() . PHP_EOL . "Click this -> " . $this->generateUrl('_Reg_Activation', array('usern' => $user->getUsername(), 'Activation' => $user->getActivationCode()), true));
             return $this->redirect($this->generateUrl('_homepage', array('Registered' => 'Thank you for' . ' your registration ' . $Regis->getUsername() . '!' . PHP_EOL . " We will send you e-mail with your activation Code!")));
         } else {
             return $this->render('WebVLWebVillasBundle:UserF:Register.html.twig', array('form' => $form->createView()));
         }
     } catch (\PDOException $e) {
         return $this->render('WebVLWebVillasBundle:Default:Errors.html.twig', array('Errors' => array("Name" => "Database Error", "Message" => "We have some problem with our database!")));
     }
 }
 public function CreateTestAction()
 {
     //Creates The Admin User and some fake users as well Villa's
     try {
         $em = $this->getDoctrine()->getManager();
         //<editor-fold defaultstate="collapsed" desc="Admin User">
         $AdminUser = $em->createQuery("SELECT r FROM WebVLWebVillasBundle:Users r WHERE r.username = '******'")->getSingleResult();
         if (count($AdminUser) == 0) {
             $admin = \WebVL\WebVillasBundle\Entity\Users();
             $factory = $this->get('security.encoder_factory');
             $admin->setUsername("Administrator");
             $admin->setActivated(true);
             $admin->setEmail("*****@*****.**");
             $ActiveCode = rand(1000000000, 9999999999.0);
             //  var_dump( $ActiveCode);
             $admin->setActivationCode($ActiveCode);
             $encoder = $factory->getEncoder($admin);
             $password = $encoder->encodePassword('Adm1n1str@t0r', $admin->getSalt());
             $admin->setPassword($password);
             $admin->setJoined(new \DateTime());
             $AdminRole = $em->createQuery("SELECT r FROM WebVLWebVillasBundle:Roles r WHERE r.name = 'Admin'")->getSingleResult();
             $admin->addRole($AdminRole);
             $em->persist($admin);
             $em->flush();
         }
         //</editor-fold>
         $roleUser = $em->createQuery("SELECT r FROM WebVLWebVillasBundle:Roles r WHERE r.name = 'User'")->getSingleResult();
         if (true == false) {
             //<editor-fold defaultstate="collapsed" desc="Users">
             for ($i = 0; $i < 40; $i++) {
                 $user = new \WebVL\WebVillasBundle\Entity\Users();
                 $factory = $this->get('security.encoder_factory');
                 $user->setUsername("User{$i}");
                 $user->setActivated(true);
                 $user->setEmail("User{$i}@plus.gr");
                 $ActiveCode = rand(1000000000, 9999999999.0);
                 //  var_dump( $ActiveCode);
                 $user->setActivationCode($ActiveCode);
                 $encoder = $factory->getEncoder($user);
                 $password = $encoder->encodePassword('0123456789AA', $user->getSalt());
                 $user->setPassword($password);
                 $user->setJoined(new \DateTime());
                 $user->addRole($roleUser);
                 $em->persist($user);
             }
             $em->flush();
             //</editor-fold>
         }
         //<editor-fold defaultstate="collapsed" desc="Villas">
         $rr = $this->getDoctrine()->getManager();
         $roleOwner = $rr->createQuery("SELECT r FROM WebVLWebVillasBundle:Roles r WHERE r.name = 'Villas Owner'")->getSingleResult();
         for ($i = 0; $i < 40; $i++) {
             $User = $rr->createQuery("SELECT u FROM WebVLWebVillasBundle:Users u WHERE u.username = '******'")->getSingleResult();
             $User->removeRole($roleUser);
             $User->addRole($roleOwner);
             $villas = new \WebVL\WebVillasBundle\Entity\Villas();
             $villas->setAddress("{$i} Paradise");
             $villas->setDetails("Somethings {$i}");
             $villas->setView("Views WhatEver {$i}");
             $villas->setGeox(37.94130964422101 - $i);
             $villas->setGeoy(23.90991121530533 - $i);
             $villas->setPrice(20000 + $i * 2000);
             $villas->setRestrooms(rand(1, 10));
             $villas->setRooms(rand(1, 15));
             $villas->setSize(rand(1, 20));
             $villas->setUsers($User);
             $state = new \WebVL\WebVillasBundle\Entity\States();
             $st = $this->getDoctrine()->getManager();
             $query = $st->createQuery("SELECT r FROM WebVLWebVillasBundle:States r WHERE r.id = :id")->setParameter(':id', (int) rand(1, 15));
             $state = $query->getSingleResult();
             $villas->setStates($state);
             $em = $this->getDoctrine()->getManager();
             $em->persist($villas);
             $em->flush();
             $st = $this->getDoctrine()->getManager();
             $query = $st->createQuery("SELECT count(r) FROM WebVLWebVillasBundle:AvailEquipment r ");
             $id = $query->getSingleResult();
             $Avail = new \WebVL\WebVillasBundle\Entity\AvailEquipment($villas, $id[1] + 1);
             $Avail->setParking(rand(0, 5));
             if ($i == 2 || $i == 5 || $i == 10 || $i == 18) {
                 $Avail->setGym(true);
             } else {
                 $Avail->setGym(false);
             }
             if ($i == 2 || $i == 4 || $i == 10 || $i == 14) {
                 $Avail->setPool(true);
             } else {
                 $Avail->setPool(false);
             }
             if ($i == 2 || $i == 3 || $i == 14 || $i == 18) {
                 $Avail->setSauna(true);
             } else {
                 $Avail->setSauna(false);
             }
             $em->persist($Avail);
             $em->flush();
         }
         //</editor-fold>
         return new \Symfony\Component\HttpFoundation\Response('Test Vars Created!');
     } catch (\PDOException $e) {
         return $this->render('WebVLWebVillasBundle:Default:Errors.html.twig', array('Errors' => array("Name" => "Database Error", "Message" => "We have some problem with our database!")));
     }
 }